blob: e97fe855de83e0cf985b5f794eb88e5f9d8dd66a [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030030#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Derek Foreman493d9792015-03-04 16:26:25 -060042#include <stdbool.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040043
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070069#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070070#include "shared/helpers.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050071#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060072#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020073#include "workspaces-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070074#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050075
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050076#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050077
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090078#include <sys/types.h>
79#include "ivi-application-client-protocol.h"
80#define IVI_SURFACE_ID 9000
81
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070082struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030083
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040084struct global {
85 uint32_t name;
86 char *interface;
87 uint32_t version;
88 struct wl_list link;
89};
90
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050091struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050092 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040093 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050094 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030095 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040096 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040097 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060098 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020099 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500100 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900101 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500103 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200104 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200105 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400106 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400107
108 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700109 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400110 struct task display_task;
111
112 int epoll_fd;
113 struct wl_list deferred_list;
114
Pekka Paalanen826d7952011-12-15 10:14:07 +0200115 int running;
116
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400117 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400118 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400119 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500120 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400121
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400122 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400123
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300124 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300125 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400126
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200127 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400128 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800129 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200130
131 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100132
133 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200134
135 uint32_t workspace;
136 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200137
138 /* A hack to get text extents for tooltips */
139 cairo_surface_t *dummy_surface;
140 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200141
142 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100143 int seat_version;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530144 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500145};
146
Rob Bradford7507b572012-05-15 17:55:34 +0100147struct window_output {
148 struct output *output;
149 struct wl_list link;
150};
151
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200152struct toysurface {
153 /*
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200157 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160 * Returns the Cairo surface to draw to.
161 */
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200163 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200164 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200165
166 /*
167 * Post the surface to the server, returning the server allocation
168 * rectangle. The Cairo surface from prepare() must be destroyed
169 * after calling this.
170 */
171 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200172 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200173 struct rectangle *server_allocation);
174
175 /*
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
178 */
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
180
181 /*
182 * Release the toysurface from the EGL context, returning control
183 * to Cairo.
184 */
185 void (*release)(struct toysurface *base);
186
187 /*
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
190 */
191 void (*destroy)(struct toysurface *base);
192};
193
Pekka Paalanen4e373742013-02-13 16:17:13 +0200194struct surface {
195 struct window *window;
196
197 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300198 struct wl_subsurface *subsurface;
199 int synchronized;
200 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200201 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200202 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300203 int redraw_needed;
204 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300205 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200206
207 struct rectangle allocation;
208 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200210 struct wl_region *input_region;
211 struct wl_region *opaque_region;
212
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200213 enum window_buffer_type buffer_type;
214 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200215 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200216
217 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300218
219 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200220};
221
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500222struct window {
223 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100224 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500225 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200226 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400227 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500228 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200229 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500230 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400231 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300232 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400233 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400234 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500235 int custom;
236 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400237
Pekka Paalanen99436862012-11-19 17:15:59 +0200238 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400239
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500240 int fullscreen;
241 int maximized;
242
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200243 enum preferred_format preferred_format;
244
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500245 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500246 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500249 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400250 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200251 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700252 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400253
Pekka Paalanen4e373742013-02-13 16:17:13 +0200254 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500255 struct xdg_surface *xdg_surface;
256 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300257
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700258 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800259 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500260
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900261 struct ivi_surface *ivi_surface;
262
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500263 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500264
Pekka Paalanen35e82632013-04-25 13:57:48 +0300265 /* struct surface::link, contains also main_surface */
266 struct wl_list subsurface_list;
267
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500268 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400269 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500270};
271
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500272struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500273 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200274 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300275 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500276 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400277 struct wl_list link;
278 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500279 widget_resize_handler_t resize_handler;
280 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500281 widget_enter_handler_t enter_handler;
282 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500283 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500284 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700285 widget_touch_down_handler_t touch_down_handler;
286 widget_touch_up_handler_t touch_up_handler;
287 widget_touch_motion_handler_t touch_motion_handler;
288 widget_touch_frame_handler_t touch_frame_handler;
289 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200290 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400291 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500292 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300293 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500294 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000295 /* If this is set to false then no cairo surface will be
296 * created before redrawing the surface. This is useful if the
297 * redraw handler is going to do completely custom rendering
298 * such as using EGL directly */
299 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400300};
301
Rusty Lynch041815a2013-08-08 21:20:38 -0700302struct touch_point {
303 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800304 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct widget *widget;
306 struct wl_list link;
307};
308
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400309struct input {
310 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100311 struct wl_seat *seat;
312 struct wl_pointer *pointer;
313 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700314 struct wl_touch *touch;
315 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316 struct window *pointer_focus;
317 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700318 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300319 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300320 uint32_t cursor_anim_start;
321 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500322 uint32_t cursor_timer_start;
323 uint32_t cursor_anim_current;
324 int cursor_delay_fd;
325 bool cursor_timer_running;
326 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300327 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400328 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400329 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400330 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400331 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400332 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400333
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500334 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500335 struct widget *grab;
336 uint32_t grab_button;
337
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400338 struct wl_data_device *data_device;
339 struct data_offer *drag_offer;
340 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800341 uint32_t touch_grab;
342 int32_t touch_grab_id;
343 float drag_x, drag_y;
344 struct window *drag_focus;
345 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100346
347 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100348 struct xkb_keymap *keymap;
349 struct xkb_state *state;
350 xkb_mod_mask_t control_mask;
351 xkb_mod_mask_t alt_mask;
352 xkb_mod_mask_t shift_mask;
353 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400354
Jonny Lamb06959082014-08-12 14:58:27 +0200355 int32_t repeat_rate_sec;
356 int32_t repeat_rate_nsec;
357 int32_t repeat_delay_sec;
358 int32_t repeat_delay_nsec;
359
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400360 struct task repeat_task;
361 int repeat_timer_fd;
362 uint32_t repeat_sym;
363 uint32_t repeat_key;
364 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400365};
366
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500367struct output {
368 struct display *display;
369 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800370 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500371 struct rectangle allocation;
372 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600373 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200374 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500375 char *make;
376 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200377
378 display_output_handler_t destroy_handler;
379 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500380};
381
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500382struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500383 struct widget *widget;
384 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500385 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800386
387 uint32_t last_time;
388 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800389 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500390};
391
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500392struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400393 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500394 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500395 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500396 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700397 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500398 const char **entries;
399 uint32_t time;
400 int current;
401 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400402 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500403 menu_func_t func;
404};
405
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300406struct tooltip {
407 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300408 struct widget *widget;
409 char *entry;
410 struct task tooltip_task;
411 int tooltip_fd;
412 float x, y;
413};
414
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700415struct shm_pool {
416 struct wl_shm_pool *pool;
417 size_t size;
418 size_t used;
419 void *data;
420};
421
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400422enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300423 CURSOR_DEFAULT = 100,
424 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400425};
426
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200427static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428
Pekka Paalanen97777442013-05-22 10:20:05 +0300429/* #define DEBUG */
430
431#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300432
433static void
434debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435__attribute__ ((format (printf, 4, 5)));
436
437static void
438debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
439{
440 va_list ap;
441 struct timeval tv;
442
443 gettimeofday(&tv, NULL);
444 fprintf(stderr, "%8ld.%03ld ",
445 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
446
447 if (proxy)
448 fprintf(stderr, "%s@%d ",
449 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
450
451 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
452 fprintf(stderr, "%s ", func);
453
454 va_start(ap, fmt);
455 vfprintf(stderr, fmt, ap);
456 va_end(ap);
457}
458
459#define DBG(fmt, ...) \
460 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
461
462#define DBG_OBJ(obj, fmt, ...) \
463 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
464
465#else
466
467#define DBG(...) do {} while (0)
468#define DBG_OBJ(...) do {} while (0)
469
470#endif
471
Alexander Larsson1818e312013-05-22 14:41:31 +0200472static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200473surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200474{
475 int32_t tmp;
476
477 switch (buffer_transform) {
478 case WL_OUTPUT_TRANSFORM_90:
479 case WL_OUTPUT_TRANSFORM_270:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
481 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
482 tmp = *width;
483 *width = *height;
484 *height = tmp;
485 break;
486 default:
487 break;
488 }
489
490 *width *= buffer_scale;
491 *height *= buffer_scale;
492}
493
494static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200495buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200496{
497 int32_t tmp;
498
499 switch (buffer_transform) {
500 case WL_OUTPUT_TRANSFORM_90:
501 case WL_OUTPUT_TRANSFORM_270:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
503 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
504 tmp = *width;
505 *width = *height;
506 *height = tmp;
507 break;
508 default:
509 break;
510 }
511
512 *width /= buffer_scale;
513 *height /= buffer_scale;
514}
515
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500516#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518struct egl_window_surface {
519 struct toysurface base;
520 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521 struct display *display;
522 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 struct wl_egl_window *egl_window;
524 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525};
526
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527static struct egl_window_surface *
528to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100529{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100531}
532
533static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200535 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200536 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100537{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538 struct egl_window_surface *surface = to_egl_window_surface(base);
539
Alexander Larsson1818e312013-05-22 14:41:31 +0200540 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
541
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200542 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
543 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
544
545 return cairo_surface_reference(surface->cairo_surface);
546}
547
548static void
549egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200550 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551 struct rectangle *server_allocation)
552{
553 struct egl_window_surface *surface = to_egl_window_surface(base);
554
555 cairo_gl_surface_swapbuffers(surface->cairo_surface);
556 wl_egl_window_get_attached_size(surface->egl_window,
557 &server_allocation->width,
558 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200559
560 buffer_to_surface_size (buffer_transform, buffer_scale,
561 &server_allocation->width,
562 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563}
564
565static int
566egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
567{
568 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200569 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100570
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 device = cairo_surface_get_device(surface->cairo_surface);
572 if (!device)
573 return -1;
574
575 if (!ctx) {
576 if (device == surface->display->argb_device)
577 ctx = surface->display->argb_ctx;
578 else
579 assert(0);
580 }
581
582 cairo_device_flush(device);
583 cairo_device_acquire(device);
584 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
585 surface->egl_surface, ctx))
586 fprintf(stderr, "failed to make surface current\n");
587
588 return 0;
589}
590
591static void
592egl_window_surface_release(struct toysurface *base)
593{
594 struct egl_window_surface *surface = to_egl_window_surface(base);
595 cairo_device_t *device;
596
597 device = cairo_surface_get_device(surface->cairo_surface);
598 if (!device)
599 return;
600
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200601 if (!eglMakeCurrent(surface->display->dpy,
602 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200603 fprintf(stderr, "failed to make context current\n");
604
605 cairo_device_release(device);
606}
607
608static void
609egl_window_surface_destroy(struct toysurface *base)
610{
611 struct egl_window_surface *surface = to_egl_window_surface(base);
612 struct display *d = surface->display;
613
614 cairo_surface_destroy(surface->cairo_surface);
615 eglDestroySurface(d->dpy, surface->egl_surface);
616 wl_egl_window_destroy(surface->egl_window);
617 surface->surface = NULL;
618
619 free(surface);
620}
621
622static struct toysurface *
623egl_window_surface_create(struct display *display,
624 struct wl_surface *wl_surface,
625 uint32_t flags,
626 struct rectangle *rectangle)
627{
628 struct egl_window_surface *surface;
629
Pekka Paalanenb3627362012-11-19 17:16:00 +0200630 if (display->dpy == EGL_NO_DISPLAY)
631 return NULL;
632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface = calloc(1, sizeof *surface);
634 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 return NULL;
636
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200637 surface->base.prepare = egl_window_surface_prepare;
638 surface->base.swap = egl_window_surface_swap;
639 surface->base.acquire = egl_window_surface_acquire;
640 surface->base.release = egl_window_surface_release;
641 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200643 surface->display = display;
644 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200646 surface->egl_window = wl_egl_window_create(surface->surface,
647 rectangle->width,
648 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100650 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100651 weston_platform_create_egl_surface(display->dpy,
652 display->argb_config,
653 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200655 surface->cairo_surface =
656 cairo_gl_surface_create_for_egl(display->argb_device,
657 surface->egl_surface,
658 rectangle->width,
659 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100660
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200661 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100662}
663
Pekka Paalanenb3627362012-11-19 17:16:00 +0200664#else
665
666static struct toysurface *
667egl_window_surface_create(struct display *display,
668 struct wl_surface *wl_surface,
669 uint32_t flags,
670 struct rectangle *rectangle)
671{
672 return NULL;
673}
674
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675#endif
676
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200677struct shm_surface_data {
678 struct wl_buffer *buffer;
679 struct shm_pool *pool;
680};
681
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682struct wl_buffer *
683display_get_buffer_for_surface(struct display *display,
684 cairo_surface_t *surface)
685{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200686 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400687
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200688 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
690 return data->buffer;
691}
692
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500693static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700694shm_pool_destroy(struct shm_pool *pool);
695
696static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400697shm_surface_data_destroy(void *p)
698{
699 struct shm_surface_data *data = p;
700
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200701 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700702 if (data->pool)
703 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300704
705 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706}
707
Kristian Høgsberg16626282012-04-03 11:21:27 -0400708static struct wl_shm_pool *
709make_shm_pool(struct display *display, int size, void **data)
710{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400711 struct wl_shm_pool *pool;
712 int fd;
713
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300714 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400715 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300716 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
717 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400718 return NULL;
719 }
720
721 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400722 if (*data == MAP_FAILED) {
723 fprintf(stderr, "mmap failed: %m\n");
724 close(fd);
725 return NULL;
726 }
727
728 pool = wl_shm_create_pool(display->shm, fd, size);
729
730 close(fd);
731
732 return pool;
733}
734
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700735static struct shm_pool *
736shm_pool_create(struct display *display, size_t size)
737{
738 struct shm_pool *pool = malloc(sizeof *pool);
739
740 if (!pool)
741 return NULL;
742
743 pool->pool = make_shm_pool(display, size, &pool->data);
744 if (!pool->pool) {
745 free(pool);
746 return NULL;
747 }
748
749 pool->size = size;
750 pool->used = 0;
751
752 return pool;
753}
754
755static void *
756shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
757{
758 if (pool->used + size > pool->size)
759 return NULL;
760
761 *offset = pool->used;
762 pool->used += size;
763
764 return (char *) pool->data + *offset;
765}
766
767/* destroy the pool. this does not unmap the memory though */
768static void
769shm_pool_destroy(struct shm_pool *pool)
770{
771 munmap(pool->data, pool->size);
772 wl_shm_pool_destroy(pool->pool);
773 free(pool);
774}
775
776/* Start allocating from the beginning of the pool again */
777static void
778shm_pool_reset(struct shm_pool *pool)
779{
780 pool->used = 0;
781}
782
783static int
784data_length_for_shm_surface(struct rectangle *rect)
785{
786 int stride;
787
788 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
789 rect->width);
790 return stride * rect->height;
791}
792
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500793static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700794display_create_shm_surface_from_pool(struct display *display,
795 struct rectangle *rectangle,
796 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797{
798 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400799 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400800 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200801 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700802 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400803 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804
805 data = malloc(sizeof *data);
806 if (data == NULL)
807 return NULL;
808
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200809 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
810 cairo_format = CAIRO_FORMAT_RGB16_565;
811 else
812 cairo_format = CAIRO_FORMAT_ARGB32;
813
814 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700815 length = stride * rectangle->height;
816 data->pool = NULL;
817 map = shm_pool_allocate(pool, length, &offset);
818
819 if (!map) {
820 free(data);
821 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400822 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400824 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200825 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 rectangle->width,
827 rectangle->height,
828 stride);
829
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200830 cairo_surface_set_user_data(surface, &shm_surface_data_key,
831 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200833 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
834 format = WL_SHM_FORMAT_RGB565;
835 else {
836 if (flags & SURFACE_OPAQUE)
837 format = WL_SHM_FORMAT_XRGB8888;
838 else
839 format = WL_SHM_FORMAT_ARGB8888;
840 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400841
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200842 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
843 rectangle->width,
844 rectangle->height,
845 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400846
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700847 return surface;
848}
849
850static cairo_surface_t *
851display_create_shm_surface(struct display *display,
852 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200853 struct shm_pool *alternate_pool,
854 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700855{
856 struct shm_surface_data *data;
857 struct shm_pool *pool;
858 cairo_surface_t *surface;
859
Pekka Paalanen99436862012-11-19 17:15:59 +0200860 if (alternate_pool) {
861 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700862 surface = display_create_shm_surface_from_pool(display,
863 rectangle,
864 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200865 alternate_pool);
866 if (surface) {
867 data = cairo_surface_get_user_data(surface,
868 &shm_surface_data_key);
869 goto out;
870 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700871 }
872
873 pool = shm_pool_create(display,
874 data_length_for_shm_surface(rectangle));
875 if (!pool)
876 return NULL;
877
878 surface =
879 display_create_shm_surface_from_pool(display, rectangle,
880 flags, pool);
881
882 if (!surface) {
883 shm_pool_destroy(pool);
884 return NULL;
885 }
886
887 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200888 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700889 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890
Pekka Paalanen99436862012-11-19 17:15:59 +0200891out:
892 if (data_ret)
893 *data_ret = data;
894
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400895 return surface;
896}
897
nobled7b87cb02011-02-01 18:51:47 +0000898static int
899check_size(struct rectangle *rect)
900{
901 if (rect->width && rect->height)
902 return 0;
903
904 fprintf(stderr, "tried to create surface of "
905 "width: %d, height: %d\n", rect->width, rect->height);
906 return -1;
907}
908
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909cairo_surface_t *
910display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400912 struct rectangle *rectangle,
913 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400914{
nobled7b87cb02011-02-01 18:51:47 +0000915 if (check_size(rectangle) < 0)
916 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200917
918 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200919 return display_create_shm_surface(display, rectangle, flags,
920 NULL, NULL);
921}
922
Pekka Paalanena4eda732012-11-19 17:16:02 +0200923struct shm_surface_leaf {
924 cairo_surface_t *cairo_surface;
925 /* 'data' is automatically destroyed, when 'cairo_surface' is */
926 struct shm_surface_data *data;
927
928 struct shm_pool *resize_pool;
929 int busy;
930};
931
932static void
933shm_surface_leaf_release(struct shm_surface_leaf *leaf)
934{
935 if (leaf->cairo_surface)
936 cairo_surface_destroy(leaf->cairo_surface);
937 /* leaf->data already destroyed via cairo private */
938
939 if (leaf->resize_pool)
940 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200941
942 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200943}
944
Pekka Paalanenaef02542013-04-25 13:57:47 +0300945#define MAX_LEAVES 3
946
Pekka Paalanen99436862012-11-19 17:15:59 +0200947struct shm_surface {
948 struct toysurface base;
949 struct display *display;
950 struct wl_surface *surface;
951 uint32_t flags;
952 int dx, dy;
953
Pekka Paalanenaef02542013-04-25 13:57:47 +0300954 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200955 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200956};
957
958static struct shm_surface *
959to_shm_surface(struct toysurface *base)
960{
961 return container_of(base, struct shm_surface, base);
962}
963
Pekka Paalanena4eda732012-11-19 17:16:02 +0200964static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300965shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
966{
967#ifdef DEBUG
968 struct shm_surface_leaf *leaf;
969 char bufs[MAX_LEAVES + 1];
970 int i;
971
972 for (i = 0; i < MAX_LEAVES; i++) {
973 leaf = &surface->leaf[i];
974
975 if (leaf->busy)
976 bufs[i] = 'b';
977 else if (leaf->cairo_surface)
978 bufs[i] = 'a';
979 else
980 bufs[i] = ' ';
981 }
982
983 bufs[MAX_LEAVES] = '\0';
984 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
985#endif
986}
987
988static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200989shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
990{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200991 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300992 struct shm_surface_leaf *leaf;
993 int i;
994 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300995
996 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200997
Pekka Paalanenaef02542013-04-25 13:57:47 +0300998 for (i = 0; i < MAX_LEAVES; i++) {
999 leaf = &surface->leaf[i];
1000 if (leaf->data && leaf->data->buffer == buffer) {
1001 leaf->busy = 0;
1002 break;
1003 }
1004 }
1005 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001006
Pekka Paalanenaef02542013-04-25 13:57:47 +03001007 /* Leave one free leaf with storage, release others */
1008 free_found = 0;
1009 for (i = 0; i < MAX_LEAVES; i++) {
1010 leaf = &surface->leaf[i];
1011
1012 if (!leaf->cairo_surface || leaf->busy)
1013 continue;
1014
1015 if (!free_found)
1016 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001017 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001018 shm_surface_leaf_release(leaf);
1019 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001020
Pekka Paalanen97777442013-05-22 10:20:05 +03001021 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001022}
1023
1024static const struct wl_buffer_listener shm_surface_buffer_listener = {
1025 shm_surface_buffer_release
1026};
1027
Pekka Paalanen99436862012-11-19 17:15:59 +02001028static cairo_surface_t *
1029shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001030 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001031 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001032{
Pekka Paalanenec076692012-11-30 13:37:27 +02001033 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001034 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001035 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 struct shm_surface_leaf *leaf = NULL;
1037 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001038
1039 surface->dx = dx;
1040 surface->dy = dy;
1041
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001042 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001043 for (i = 0; i < MAX_LEAVES; i++) {
1044 if (surface->leaf[i].busy)
1045 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001046
Pekka Paalanenaef02542013-04-25 13:57:47 +03001047 if (!leaf || surface->leaf[i].cairo_surface)
1048 leaf = &surface->leaf[i];
1049 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001050 DBG_OBJ(surface->surface, "pick leaf %d\n",
1051 (int)(leaf - &surface->leaf[0]));
1052
Pekka Paalanenaef02542013-04-25 13:57:47 +03001053 if (!leaf) {
1054 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001056 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001058 }
1059
Pekka Paalanena4eda732012-11-19 17:16:02 +02001060 if (!resize_hint && leaf->resize_pool) {
1061 cairo_surface_destroy(leaf->cairo_surface);
1062 leaf->cairo_surface = NULL;
1063 shm_pool_destroy(leaf->resize_pool);
1064 leaf->resize_pool = NULL;
1065 }
1066
Alexander Larsson1818e312013-05-22 14:41:31 +02001067 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1068
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 if (leaf->cairo_surface &&
1070 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1071 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001072 goto out;
1073
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 if (leaf->cairo_surface)
1075 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001077#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001078 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001079 /* Create a big pool to allocate from, while continuously
1080 * resizing. Mmapping a new pool in the server
1081 * is relatively expensive, so reusing a pool performs
1082 * better, but may temporarily reserve unneeded memory.
1083 */
1084 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001085 leaf->resize_pool = shm_pool_create(surface->display,
1086 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001087 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001088#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001089
Alexander Larsson1818e312013-05-22 14:41:31 +02001090 rect.width = width;
1091 rect.height = height;
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001094 display_create_shm_surface(surface->display, &rect,
1095 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 leaf->resize_pool,
1097 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001098 if (!leaf->cairo_surface)
1099 return NULL;
1100
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001102 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103
1104out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 surface->current = leaf;
1106
1107 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001108}
1109
1110static void
1111shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001112 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 struct rectangle *server_allocation)
1114{
1115 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001117
1118 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001119 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001120 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001121 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001122
Alexander Larsson1818e312013-05-22 14:41:31 +02001123 buffer_to_surface_size (buffer_transform, buffer_scale,
1124 &server_allocation->width,
1125 &server_allocation->height);
1126
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001128 surface->dx, surface->dy);
1129 wl_surface_damage(surface->surface, 0, 0,
1130 server_allocation->width, server_allocation->height);
1131 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001132
Pekka Paalanen71233882013-04-25 13:57:53 +03001133 DBG_OBJ(surface->surface, "leaf %d busy\n",
1134 (int)(leaf - &surface->leaf[0]));
1135
Pekka Paalanena4eda732012-11-19 17:16:02 +02001136 leaf->busy = 1;
1137 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001138}
1139
1140static int
1141shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1142{
1143 return -1;
1144}
1145
1146static void
1147shm_surface_release(struct toysurface *base)
1148{
1149}
1150
1151static void
1152shm_surface_destroy(struct toysurface *base)
1153{
1154 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001155 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001156
Pekka Paalanenaef02542013-04-25 13:57:47 +03001157 for (i = 0; i < MAX_LEAVES; i++)
1158 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001159
1160 free(surface);
1161}
1162
1163static struct toysurface *
1164shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1165 uint32_t flags, struct rectangle *rectangle)
1166{
1167 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001168 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001169
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001170 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001171 surface->base.prepare = shm_surface_prepare;
1172 surface->base.swap = shm_surface_swap;
1173 surface->base.acquire = shm_surface_acquire;
1174 surface->base.release = shm_surface_release;
1175 surface->base.destroy = shm_surface_destroy;
1176
1177 surface->display = display;
1178 surface->surface = wl_surface;
1179 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001180
1181 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001182}
1183
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001184/*
1185 * The following correspondences between file names and cursors was copied
1186 * from: https://bugs.kde.org/attachment.cgi?id=67313
1187 */
1188
1189static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001190 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001191 "sw-resize",
1192 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001193};
1194
1195static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001196 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001197 "se-resize",
1198 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001199};
1200
1201static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001202 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001203 "s-resize",
1204 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001205};
1206
1207static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209 "closedhand",
1210 "208530c400c041818281048008011002"
1211};
1212
1213static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215 "default",
1216 "top_left_arrow",
1217 "left-arrow"
1218};
1219
1220static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "w-resize",
1223 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "e-resize",
1229 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "nw-resize",
1235 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001240 "ne-resize",
1241 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242};
1243
1244static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001246 "n-resize",
1247 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248};
1249
1250static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001251 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252 "ibeam",
1253 "text"
1254};
1255
1256static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001257 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258 "pointer",
1259 "pointing_hand",
1260 "e29285e634086352946a0e7090d73106"
1261};
1262
1263static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001264 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001265 "wait",
1266 "0426c94ea35c87780ff01dc239897213"
1267};
1268
1269struct cursor_alternatives {
1270 const char **names;
1271 size_t count;
1272};
1273
1274static const struct cursor_alternatives cursors[] = {
1275 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1276 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1277 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1278 {grabbings, ARRAY_LENGTH(grabbings)},
1279 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1280 {left_sides, ARRAY_LENGTH(left_sides)},
1281 {right_sides, ARRAY_LENGTH(right_sides)},
1282 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1283 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1284 {top_sides, ARRAY_LENGTH(top_sides)},
1285 {xterms, ARRAY_LENGTH(xterms)},
1286 {hand1s, ARRAY_LENGTH(hand1s)},
1287 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001288};
1289
1290static void
1291create_cursors(struct display *display)
1292{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001293 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001294 struct weston_config *config;
1295 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001296 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001297 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001298 unsigned int i, j;
1299 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001300
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001301 config_file = weston_config_get_name_from_env();
1302 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001303 s = weston_config_get_section(config, "shell", NULL, NULL);
1304 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1305 weston_config_section_get_int(s, "cursor-size", &size, 32);
1306 weston_config_destroy(config);
1307
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001308 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001309 if (!display->cursor_theme) {
1310 fprintf(stderr, "could not load theme '%s'\n", theme);
1311 return;
1312 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001313 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001314 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001315 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001316
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001317 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001318 cursor = NULL;
1319 for (j = 0; !cursor && j < cursors[i].count; ++j)
1320 cursor = wl_cursor_theme_get_cursor(
1321 display->cursor_theme, cursors[i].names[j]);
1322
1323 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001324 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001325 cursors[i].names[0]);
1326
1327 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001328 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001329}
1330
1331static void
1332destroy_cursors(struct display *display)
1333{
1334 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001335 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001336}
1337
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001338struct wl_cursor_image *
1339display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001340{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001341 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001342
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001343 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001344}
1345
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001346static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001347surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001348{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001349 if (!surface->cairo_surface)
1350 return;
1351
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001352 if (surface->opaque_region) {
1353 wl_surface_set_opaque_region(surface->surface,
1354 surface->opaque_region);
1355 wl_region_destroy(surface->opaque_region);
1356 surface->opaque_region = NULL;
1357 }
1358
1359 if (surface->input_region) {
1360 wl_surface_set_input_region(surface->surface,
1361 surface->input_region);
1362 wl_region_destroy(surface->input_region);
1363 surface->input_region = NULL;
1364 }
1365
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001366 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001367 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001368 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001369
Pekka Paalanen89dee002013-02-13 16:17:20 +02001370 cairo_surface_destroy(surface->cairo_surface);
1371 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001372}
1373
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001374int
1375window_has_focus(struct window *window)
1376{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001377 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001378}
1379
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001380static void
1381window_close(struct window *window)
1382{
1383 if (window->close_handler)
1384 window->close_handler(window->user_data);
1385 else
1386 display_exit(window->display);
1387}
1388
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001389struct display *
1390window_get_display(struct window *window)
1391{
1392 return window->display;
1393}
1394
Pekka Paalanen89dee002013-02-13 16:17:20 +02001395static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001396handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1397 int32_t width, int32_t height)
1398{
1399 struct window *window = data;
1400
1401 window_schedule_resize(window, width, height);
1402}
1403
1404static const struct ivi_surface_listener ivi_surface_listener = {
1405 handle_ivi_surface_configure,
1406};
1407
1408static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001409surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001410{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001412 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001413
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414 if (!surface->toysurface && display->dpy &&
1415 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001416 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001417 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001419 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001420 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001421 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001422
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001423 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001425 surface->surface,
1426 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001427
Pekka Paalanen89dee002013-02-13 16:17:20 +02001428 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001429 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001430 allocation.width, allocation.height, flags,
1431 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001432}
1433
1434static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001435window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001436{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001437 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001438 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001439
Pekka Paalanenec076692012-11-30 13:37:27 +02001440 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001441 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001442
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001443 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1444 flags |= SURFACE_HINT_RGB565;
1445
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001446 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001447}
1448
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001449int
1450window_get_buffer_transform(struct window *window)
1451{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001452 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001453}
1454
1455void
1456window_set_buffer_transform(struct window *window,
1457 enum wl_output_transform transform)
1458{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001459 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001460 wl_surface_set_buffer_transform(window->main_surface->surface,
1461 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001462}
1463
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001464void
1465window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001466 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001467{
1468 window->main_surface->buffer_scale = scale;
1469 wl_surface_set_buffer_scale(window->main_surface->surface,
1470 scale);
1471}
1472
1473uint32_t
1474window_get_buffer_scale(struct window *window)
1475{
1476 return window->main_surface->buffer_scale;
1477}
1478
Alexander Larssond68f5232013-05-22 14:41:33 +02001479uint32_t
1480window_get_output_scale(struct window *window)
1481{
1482 struct window_output *window_output;
1483 struct window_output *window_output_tmp;
1484 int scale = 1;
1485
1486 wl_list_for_each_safe(window_output, window_output_tmp,
1487 &window->window_output_list, link) {
1488 if (window_output->output->scale > scale)
1489 scale = window_output->output->scale;
1490 }
1491
1492 return scale;
1493}
1494
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001495static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001496
Pekka Paalanen4e373742013-02-13 16:17:13 +02001497static void
1498surface_destroy(struct surface *surface)
1499{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001500 if (surface->frame_cb)
1501 wl_callback_destroy(surface->frame_cb);
1502
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001503 if (surface->input_region)
1504 wl_region_destroy(surface->input_region);
1505
1506 if (surface->opaque_region)
1507 wl_region_destroy(surface->opaque_region);
1508
Pekka Paalanen35e82632013-04-25 13:57:48 +03001509 if (surface->subsurface)
1510 wl_subsurface_destroy(surface->subsurface);
1511
Pekka Paalanen4e373742013-02-13 16:17:13 +02001512 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001513
1514 if (surface->toysurface)
1515 surface->toysurface->destroy(surface->toysurface);
1516
Pekka Paalanen35e82632013-04-25 13:57:48 +03001517 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001518 free(surface);
1519}
1520
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001521void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001522window_destroy(struct window *window)
1523{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001524 struct display *display = window->display;
1525 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001526 struct window_output *window_output;
1527 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001528
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001529 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001530
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001531 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001532 if (input->touch_focus == window)
1533 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001534 if (input->pointer_focus == window)
1535 input->pointer_focus = NULL;
1536 if (input->keyboard_focus == window)
1537 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001538 if (input->focus_widget &&
1539 input->focus_widget->window == window)
1540 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001541 }
1542
Rob Bradford7507b572012-05-15 17:55:34 +01001543 wl_list_for_each_safe(window_output, window_output_tmp,
1544 &window->window_output_list, link) {
1545 free (window_output);
1546 }
1547
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001548 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001549 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001550
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001551 if (window->xdg_surface)
1552 xdg_surface_destroy(window->xdg_surface);
1553 if (window->xdg_popup)
1554 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001555
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001556 if (window->ivi_surface)
1557 ivi_surface_destroy(window->ivi_surface);
1558
Pekka Paalanen4e373742013-02-13 16:17:13 +02001559 surface_destroy(window->main_surface);
1560
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001561 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001562
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001563 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001564 free(window);
1565}
1566
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001567static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001568widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001569{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001570 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001571
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001572 wl_list_for_each(child, &widget->child_list, link) {
1573 target = widget_find_widget(child, x, y);
1574 if (target)
1575 return target;
1576 }
1577
1578 if (widget->allocation.x <= x &&
1579 x < widget->allocation.x + widget->allocation.width &&
1580 widget->allocation.y <= y &&
1581 y < widget->allocation.y + widget->allocation.height) {
1582 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001583 }
1584
1585 return NULL;
1586}
1587
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001588static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001589window_find_widget(struct window *window, int32_t x, int32_t y)
1590{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001591 struct surface *surface;
1592 struct widget *widget;
1593
1594 wl_list_for_each(surface, &window->subsurface_list, link) {
1595 widget = widget_find_widget(surface->widget, x, y);
1596 if (widget)
1597 return widget;
1598 }
1599
1600 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001601}
1602
1603static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001604widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001605{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001606 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001607
Peter Huttererf3d62272013-08-08 11:57:05 +10001608 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001609 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001610 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001611 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001612 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001613 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001614 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001615 widget->tooltip = NULL;
1616 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001617 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001618 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619
1620 return widget;
1621}
1622
1623struct widget *
1624window_add_widget(struct window *window, void *data)
1625{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001626 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001627
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001628 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001629 wl_list_init(&widget->link);
1630 window->main_surface->widget = widget;
1631
1632 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001633}
1634
1635struct widget *
1636widget_add_widget(struct widget *parent, void *data)
1637{
1638 struct widget *widget;
1639
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001640 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001641 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001642
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001643 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001644}
1645
1646void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001647widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001648{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001649 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001650 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001651 struct input *input;
1652
Pekka Paalanen35e82632013-04-25 13:57:48 +03001653 /* Destroy the sub-surface along with the root widget */
1654 if (surface->widget == widget && surface->subsurface)
1655 surface_destroy(widget->surface);
1656
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001657 if (widget->tooltip)
1658 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001659
Pekka Paalanene156fb62012-01-19 13:51:38 +02001660 wl_list_for_each(input, &display->input_list, link) {
1661 if (input->focus_widget == widget)
1662 input->focus_widget = NULL;
1663 }
1664
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001665 wl_list_remove(&widget->link);
1666 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001667}
1668
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001669void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001670widget_set_default_cursor(struct widget *widget, int cursor)
1671{
1672 widget->default_cursor = cursor;
1673}
1674
1675void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001678 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001679}
1680
1681void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001682widget_set_size(struct widget *widget, int32_t width, int32_t height)
1683{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001684 widget->allocation.width = width;
1685 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001686}
1687
1688void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001689widget_set_allocation(struct widget *widget,
1690 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001691{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001692 widget->allocation.x = x;
1693 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001694 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695}
1696
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001697void
1698widget_set_transparent(struct widget *widget, int transparent)
1699{
1700 widget->opaque = !transparent;
1701}
1702
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001703void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001704widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001706 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001707}
1708
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001709static cairo_surface_t *
1710widget_get_cairo_surface(struct widget *widget)
1711{
1712 struct surface *surface = widget->surface;
1713 struct window *window = widget->window;
1714
Neil Roberts97b747c2013-12-19 16:17:12 +00001715 assert(widget->use_cairo);
1716
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001717 if (!surface->cairo_surface) {
1718 if (surface == window->main_surface)
1719 window_create_main_surface(window);
1720 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001721 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001722 }
1723
1724 return surface->cairo_surface;
1725}
1726
Alexander Larsson15901f02013-05-22 14:41:25 +02001727static void
1728widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1729{
1730 struct surface *surface = widget->surface;
1731 double angle;
1732 cairo_matrix_t m;
1733 enum wl_output_transform transform;
1734 int surface_width, surface_height;
1735 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001736 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001737
1738 surface_width = surface->allocation.width;
1739 surface_height = surface->allocation.height;
1740
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001741 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001742 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001743
Alexander Larsson15901f02013-05-22 14:41:25 +02001744 switch (transform) {
1745 case WL_OUTPUT_TRANSFORM_FLIPPED:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1749 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1750 break;
1751 default:
1752 cairo_matrix_init_identity(&m);
1753 break;
1754 }
1755
1756 switch (transform) {
1757 case WL_OUTPUT_TRANSFORM_NORMAL:
1758 default:
1759 angle = 0;
1760 translate_x = 0;
1761 translate_y = 0;
1762 break;
1763 case WL_OUTPUT_TRANSFORM_FLIPPED:
1764 angle = 0;
1765 translate_x = surface_width;
1766 translate_y = 0;
1767 break;
1768 case WL_OUTPUT_TRANSFORM_90:
1769 angle = M_PI_2;
1770 translate_x = surface_height;
1771 translate_y = 0;
1772 break;
1773 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1774 angle = M_PI_2;
1775 translate_x = surface_height;
1776 translate_y = surface_width;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_180:
1779 angle = M_PI;
1780 translate_x = surface_width;
1781 translate_y = surface_height;
1782 break;
1783 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1784 angle = M_PI;
1785 translate_x = 0;
1786 translate_y = surface_height;
1787 break;
1788 case WL_OUTPUT_TRANSFORM_270:
1789 angle = M_PI + M_PI_2;
1790 translate_x = 0;
1791 translate_y = surface_width;
1792 break;
1793 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1794 angle = M_PI + M_PI_2;
1795 translate_x = 0;
1796 translate_y = 0;
1797 break;
1798 }
1799
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001800 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001801 cairo_translate(cr, translate_x, translate_y);
1802 cairo_rotate(cr, angle);
1803 cairo_transform(cr, &m);
1804}
1805
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001806cairo_t *
1807widget_cairo_create(struct widget *widget)
1808{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001809 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001810 cairo_surface_t *cairo_surface;
1811 cairo_t *cr;
1812
1813 cairo_surface = widget_get_cairo_surface(widget);
1814 cr = cairo_create(cairo_surface);
1815
Alexander Larsson15901f02013-05-22 14:41:25 +02001816 widget_cairo_update_transform(widget, cr);
1817
Pekka Paalanen35e82632013-04-25 13:57:48 +03001818 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1819
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001820 return cr;
1821}
1822
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001823struct wl_surface *
1824widget_get_wl_surface(struct widget *widget)
1825{
1826 return widget->surface->surface;
1827}
1828
Neil Roberts5e10a042013-09-09 00:40:17 +01001829struct wl_subsurface *
1830widget_get_wl_subsurface(struct widget *widget)
1831{
1832 return widget->surface->subsurface;
1833}
1834
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001835uint32_t
1836widget_get_last_time(struct widget *widget)
1837{
1838 return widget->surface->last_time;
1839}
1840
1841void
1842widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1843{
1844 struct wl_compositor *comp = widget->window->display->compositor;
1845 struct surface *surface = widget->surface;
1846
1847 if (!surface->input_region)
1848 surface->input_region = wl_compositor_create_region(comp);
1849
1850 if (rect) {
1851 wl_region_add(surface->input_region,
1852 rect->x, rect->y, rect->width, rect->height);
1853 }
1854}
1855
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001856void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001857widget_set_resize_handler(struct widget *widget,
1858 widget_resize_handler_t handler)
1859{
1860 widget->resize_handler = handler;
1861}
1862
1863void
1864widget_set_redraw_handler(struct widget *widget,
1865 widget_redraw_handler_t handler)
1866{
1867 widget->redraw_handler = handler;
1868}
1869
1870void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001871widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001872{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001873 widget->enter_handler = handler;
1874}
1875
1876void
1877widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1878{
1879 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001880}
1881
1882void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001883widget_set_motion_handler(struct widget *widget,
1884 widget_motion_handler_t handler)
1885{
1886 widget->motion_handler = handler;
1887}
1888
1889void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001890widget_set_button_handler(struct widget *widget,
1891 widget_button_handler_t handler)
1892{
1893 widget->button_handler = handler;
1894}
1895
1896void
Rusty Lynch041815a2013-08-08 21:20:38 -07001897widget_set_touch_up_handler(struct widget *widget,
1898 widget_touch_up_handler_t handler)
1899{
1900 widget->touch_up_handler = handler;
1901}
1902
1903void
1904widget_set_touch_down_handler(struct widget *widget,
1905 widget_touch_down_handler_t handler)
1906{
1907 widget->touch_down_handler = handler;
1908}
1909
1910void
1911widget_set_touch_motion_handler(struct widget *widget,
1912 widget_touch_motion_handler_t handler)
1913{
1914 widget->touch_motion_handler = handler;
1915}
1916
1917void
1918widget_set_touch_frame_handler(struct widget *widget,
1919 widget_touch_frame_handler_t handler)
1920{
1921 widget->touch_frame_handler = handler;
1922}
1923
1924void
1925widget_set_touch_cancel_handler(struct widget *widget,
1926 widget_touch_cancel_handler_t handler)
1927{
1928 widget->touch_cancel_handler = handler;
1929}
1930
1931void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001932widget_set_axis_handler(struct widget *widget,
1933 widget_axis_handler_t handler)
1934{
1935 widget->axis_handler = handler;
1936}
1937
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001938static void
1939window_schedule_redraw_task(struct window *window);
1940
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001941void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001942widget_schedule_redraw(struct widget *widget)
1943{
Pekka Paalanen71233882013-04-25 13:57:53 +03001944 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001945 widget->surface->redraw_needed = 1;
1946 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001947}
1948
Neil Roberts97b747c2013-12-19 16:17:12 +00001949void
1950widget_set_use_cairo(struct widget *widget,
1951 int use_cairo)
1952{
1953 widget->use_cairo = use_cairo;
1954}
1955
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001956cairo_surface_t *
1957window_get_surface(struct window *window)
1958{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001959 cairo_surface_t *cairo_surface;
1960
1961 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1962
1963 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001964}
1965
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001966struct wl_surface *
1967window_get_wl_surface(struct window *window)
1968{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001969 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001970}
1971
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001972static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001973tooltip_redraw_handler(struct widget *widget, void *data)
1974{
1975 cairo_t *cr;
1976 const int32_t r = 3;
1977 struct tooltip *tooltip = data;
1978 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001979
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001980 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001981 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001982 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1983 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1984 cairo_paint(cr);
1985
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001986 width = widget->allocation.width;
1987 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001988 rounded_rect(cr, 0, 0, width, height, r);
1989
1990 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1991 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1992 cairo_fill(cr);
1993
1994 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1995 cairo_move_to(cr, 10, 16);
1996 cairo_show_text(cr, tooltip->entry);
1997 cairo_destroy(cr);
1998}
1999
2000static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002001get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002002{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002003 cairo_t *cr;
2004 cairo_text_extents_t extents;
2005
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002006 /* Use the dummy_surface because tooltip's surface was not
2007 * created yet, and parent does not have a valid surface
2008 * outside repaint, either.
2009 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002010 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002011 cairo_text_extents(cr, tooltip->entry, &extents);
2012 cairo_destroy(cr);
2013
2014 return extents;
2015}
2016
2017static int
2018window_create_tooltip(struct tooltip *tooltip)
2019{
2020 struct widget *parent = tooltip->parent;
2021 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002022 const int offset_y = 27;
2023 const int margin = 3;
2024 cairo_text_extents_t extents;
2025
2026 if (tooltip->widget)
2027 return 0;
2028
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002029 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002030
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002031 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002032 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002033 widget_set_allocation(tooltip->widget,
2034 tooltip->x, tooltip->y + offset_y,
2035 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002036
2037 return 0;
2038}
2039
2040void
2041widget_destroy_tooltip(struct widget *parent)
2042{
2043 struct tooltip *tooltip = parent->tooltip;
2044
2045 parent->tooltip_count = 0;
2046 if (!tooltip)
2047 return;
2048
2049 if (tooltip->widget) {
2050 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002051 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002052 }
2053
2054 close(tooltip->tooltip_fd);
2055 free(tooltip->entry);
2056 free(tooltip);
2057 parent->tooltip = NULL;
2058}
2059
2060static void
2061tooltip_func(struct task *task, uint32_t events)
2062{
2063 struct tooltip *tooltip =
2064 container_of(task, struct tooltip, tooltip_task);
2065 uint64_t exp;
2066
Martin Olsson8df662a2012-07-08 03:03:47 +02002067 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2068 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002069 window_create_tooltip(tooltip);
2070}
2071
2072#define TOOLTIP_TIMEOUT 500
2073static int
2074tooltip_timer_reset(struct tooltip *tooltip)
2075{
2076 struct itimerspec its;
2077
2078 its.it_interval.tv_sec = 0;
2079 its.it_interval.tv_nsec = 0;
2080 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2081 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2082 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2083 fprintf(stderr, "could not set timerfd\n: %m");
2084 return -1;
2085 }
2086
2087 return 0;
2088}
2089
2090int
2091widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2092{
2093 struct tooltip *tooltip = parent->tooltip;
2094
2095 parent->tooltip_count++;
2096 if (tooltip) {
2097 tooltip->x = x;
2098 tooltip->y = y;
2099 tooltip_timer_reset(tooltip);
2100 return 0;
2101 }
2102
2103 /* the handler might be triggered too fast via input device motion, so
2104 * we need this check here to make sure tooltip is fully initialized */
2105 if (parent->tooltip_count > 1)
2106 return 0;
2107
2108 tooltip = malloc(sizeof *tooltip);
2109 if (!tooltip)
2110 return -1;
2111
2112 parent->tooltip = tooltip;
2113 tooltip->parent = parent;
2114 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002115 tooltip->x = x;
2116 tooltip->y = y;
2117 tooltip->entry = strdup(entry);
2118 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2119 if (tooltip->tooltip_fd < 0) {
2120 fprintf(stderr, "could not create timerfd\n: %m");
2121 return -1;
2122 }
2123
2124 tooltip->tooltip_task.run = tooltip_func;
2125 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2126 EPOLLIN, &tooltip->tooltip_task);
2127 tooltip_timer_reset(tooltip);
2128
2129 return 0;
2130}
2131
2132static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002133workspace_manager_state(void *data,
2134 struct workspace_manager *workspace_manager,
2135 uint32_t current,
2136 uint32_t count)
2137{
2138 struct display *display = data;
2139
2140 display->workspace = current;
2141 display->workspace_count = count;
2142}
2143
2144static const struct workspace_manager_listener workspace_manager_listener = {
2145 workspace_manager_state
2146};
2147
2148static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002149frame_resize_handler(struct widget *widget,
2150 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002151{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002152 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002153 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002154 struct rectangle interior;
2155 struct rectangle input;
2156 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002157
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002158 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002159 interior.x = 0;
2160 interior.y = 0;
2161 interior.width = width;
2162 interior.height = height;
2163 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002164 frame_resize(frame->frame, width, height);
2165 frame_interior(frame->frame, &interior.x, &interior.y,
2166 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002167 }
2168
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 widget_set_allocation(child, interior.x, interior.y,
2170 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002171
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 if (child->resize_handler) {
2173 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174 child->user_data);
2175
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002176 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002177 width = child->allocation.width;
2178 height = child->allocation.height;
2179 } else {
2180 frame_resize_inside(frame->frame,
2181 child->allocation.width,
2182 child->allocation.height);
2183 width = frame_width(frame->frame);
2184 height = frame_height(frame->frame);
2185 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002186 }
2187
Scott Moreauf7e498c2012-05-14 11:39:29 -06002188 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002189
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 widget->surface->input_region =
2191 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002192 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 frame_input_rect(frame->frame, &input.x, &input.y,
2194 &input.width, &input.height);
2195 wl_region_add(widget->surface->input_region,
2196 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002197 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002199 }
2200
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002202
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002204 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002205 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2206 &opaque.width, &opaque.height);
2207
2208 wl_region_add(widget->surface->opaque_region,
2209 opaque.x, opaque.y,
2210 opaque.width, opaque.height);
2211 } else {
2212 wl_region_add(widget->surface->opaque_region,
2213 0, 0, width, height);
2214 }
Martin Minarik1998b152012-05-10 02:04:35 +02002215 }
2216
Martin Minarik1998b152012-05-10 02:04:35 +02002217
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219}
2220
2221static void
2222frame_redraw_handler(struct widget *widget, void *data)
2223{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002224 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002225 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226 struct window *window = widget->window;
2227
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002228 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002229 return;
2230
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002231 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002232
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002233 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234
2235 cairo_destroy(cr);
2236}
2237
2238static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002239frame_get_pointer_image_for_location(struct window_frame *frame,
2240 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002241{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002242 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002243
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002244 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002245 return CURSOR_LEFT_PTR;
2246
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002247 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002249 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002250 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002251 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002252 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002253 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002259 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002260 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002261 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002262 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002263 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002264 case THEME_LOCATION_EXTERIOR:
2265 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002266 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002267 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002268 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002269}
2270
Pekka Paalanen26237862014-09-10 15:10:30 +03002271static void
2272frame_menu_func(void *data, struct input *input, int index)
2273{
2274 struct window *window = data;
2275 struct display *display;
2276
2277 switch (index) {
2278 case 0: /* close */
2279 window_close(window);
2280 break;
2281 case 1: /* move to workspace above */
2282 display = window->display;
2283 if (display->workspace > 0)
2284 workspace_manager_move_surface(
2285 display->workspace_manager,
2286 window->main_surface->surface,
2287 display->workspace - 1);
2288 break;
2289 case 2: /* move to workspace below */
2290 display = window->display;
2291 if (display->workspace < display->workspace_count - 1)
2292 workspace_manager_move_surface(
2293 display->workspace_manager,
2294 window->main_surface->surface,
2295 display->workspace + 1);
2296 break;
2297 case 3: /* fullscreen */
2298 /* we don't have a way to get out of fullscreen for now */
2299 if (window->fullscreen_handler)
2300 window->fullscreen_handler(window, window->user_data);
2301 break;
2302 }
2303}
2304
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002305void
2306window_show_frame_menu(struct window *window,
2307 struct input *input, uint32_t time)
2308{
2309 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002310 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002311
Pekka Paalanen26237862014-09-10 15:10:30 +03002312 static const char *entries[] = {
2313 "Close",
2314 "Move to workspace above", "Move to workspace below",
2315 "Fullscreen"
2316 };
2317
2318 if (window->fullscreen_handler)
2319 count = ARRAY_LENGTH(entries);
2320 else
2321 count = ARRAY_LENGTH(entries) - 1;
2322
2323 input_get_position(input, &x, &y);
2324 window_show_menu(window->display, input, time, window,
2325 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002326}
2327
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002328static int
2329frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002330 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002331{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002332 struct window_frame *frame = data;
2333 enum theme_location location;
2334
2335 location = frame_pointer_enter(frame->frame, input, x, y);
2336 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2337 widget_schedule_redraw(frame->widget);
2338
2339 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002341
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342static int
2343frame_motion_handler(struct widget *widget,
2344 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002345 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002346{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002347 struct window_frame *frame = data;
2348 enum theme_location location;
2349
2350 location = frame_pointer_motion(frame->frame, input, x, y);
2351 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2352 widget_schedule_redraw(frame->widget);
2353
2354 return frame_get_pointer_image_for_location(data, location);
2355}
2356
2357static void
2358frame_leave_handler(struct widget *widget,
2359 struct input *input, void *data)
2360{
2361 struct window_frame *frame = data;
2362
2363 frame_pointer_leave(frame->frame, input);
2364 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2365 widget_schedule_redraw(frame->widget);
2366}
2367
2368static void
2369frame_handle_status(struct window_frame *frame, struct input *input,
2370 uint32_t time, enum theme_location location)
2371{
2372 struct window *window = frame->widget->window;
2373 uint32_t status;
2374
2375 status = frame_status(frame->frame);
2376 if (status & FRAME_STATUS_REPAINT)
2377 widget_schedule_redraw(frame->widget);
2378
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002379 if (status & FRAME_STATUS_MINIMIZE) {
2380 window_set_minimized(window);
2381 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2382 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002383
2384 if (status & FRAME_STATUS_MENU) {
2385 window_show_frame_menu(window, input, time);
2386 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2387 }
2388
2389 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002390 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002391 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2392 }
2393
2394 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002395 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002396 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002397 }
2398
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002399 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002401 xdg_surface_move(window->xdg_surface,
2402 input_get_seat(input),
2403 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002404
2405 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2406 }
2407
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002408 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002409 input_ungrab(input);
2410
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002411 xdg_surface_resize(window->xdg_surface,
2412 input_get_seat(input),
2413 window->display->serial,
2414 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002415
2416 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2417 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002419
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002420#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002422frame_button_handler(struct widget *widget,
2423 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002424 uint32_t button, enum wl_pointer_button_state state,
2425 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426
2427{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002428 struct window_frame *frame = data;
2429 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002430
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002431 frame->double_click = 0;
2432 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2433 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2434 frame->double_click = 1;
2435 frame->did_double = 1;
2436 } else
2437 frame->did_double = 0;
2438
2439 frame->last_time = time;
2440 } else if (frame->did_double == 1) {
2441 frame->double_click = 1;
2442 frame->did_double = 0;
2443 }
2444
2445 if (frame->double_click)
2446 location = frame_double_click(frame->frame, input,
2447 button, state);
2448 else
2449 location = frame_pointer_button(frame->frame, input,
2450 button, state);
2451
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002452 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002453}
2454
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002455static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002456frame_touch_down_handler(struct widget *widget, struct input *input,
2457 uint32_t serial, uint32_t time, int32_t id,
2458 float x, float y, void *data)
2459{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460 struct window_frame *frame = data;
2461
Xiong Zhang382de462014-06-12 11:06:26 +08002462 frame->double_click = 0;
2463 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2464 frame->last_id == id) {
2465 frame->double_click = 1;
2466 frame->did_double = 1;
2467 frame->double_id = id;
2468 } else
2469 frame->did_double = 0;
2470
2471 frame->last_time = time;
2472 frame->last_id = id;
2473
2474 if (frame->double_click)
2475 frame_double_touch_down(frame->frame, input, id, x, y);
2476 else
2477 frame_touch_down(frame->frame, input, id, x, y);
2478
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002479 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2480}
2481
2482static void
2483frame_touch_up_handler(struct widget *widget,
2484 struct input *input, uint32_t serial, uint32_t time,
2485 int32_t id, void *data)
2486{
2487 struct window_frame *frame = data;
2488
Xiong Zhang382de462014-06-12 11:06:26 +08002489 if (frame->double_id == id && frame->did_double) {
2490 frame->did_double = 0;
2491 frame->double_id = 0;
2492 frame_double_touch_up(frame->frame, input, id);
2493 } else
2494 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002495 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002496}
2497
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002499window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002500{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002501 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002502 uint32_t buttons;
2503
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002504 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002505 buttons = FRAME_BUTTON_NONE;
2506 } else {
2507 buttons = FRAME_BUTTON_ALL;
2508 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002509
Peter Huttererf3d62272013-08-08 11:57:05 +10002510 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002511 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002512 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002513
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002514 frame->widget = window_add_widget(window, frame);
2515 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002516
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002517 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2518 widget_set_resize_handler(frame->widget, frame_resize_handler);
2519 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002520 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002521 widget_set_motion_handler(frame->widget, frame_motion_handler);
2522 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002523 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002524 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002525
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002526 window->frame = frame;
2527
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002528 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002529}
2530
Kristian Høgsberga1627922012-06-20 17:30:03 -04002531void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002532window_frame_set_child_size(struct widget *widget, int child_width,
2533 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002534{
2535 struct display *display = widget->window->display;
2536 struct theme *t = display->theme;
2537 int decoration_width, decoration_height;
2538 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002539 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002540
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002541 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002542 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002543 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002544 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002545
2546 width = child_width + decoration_width;
2547 height = child_height + decoration_height;
2548 } else {
2549 width = child_width;
2550 height = child_height;
2551 }
2552
2553 window_schedule_resize(widget->window, width, height);
2554}
2555
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002556static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002557window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002558{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002559 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002560
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002561 /* frame->child must be destroyed by the application */
2562 widget_destroy(frame->widget);
2563 free(frame);
2564}
2565
2566static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002567input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002568 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002569{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002570 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002571 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002572
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002573 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002574 return;
2575
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002576 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002577 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002578 widget = old;
2579 if (input->grab)
2580 widget = input->grab;
2581 if (widget->leave_handler)
2582 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002583 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002584 }
2585
2586 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002587 widget = focus;
2588 if (input->grab)
2589 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002590 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002591 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002592 cursor = widget->enter_handler(focus, input, x, y,
2593 widget->user_data);
2594 else
2595 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002596
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002597 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002598 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002599}
2600
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002601void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002602touch_grab(struct input *input, int32_t touch_id)
2603{
2604 input->touch_grab = 1;
2605 input->touch_grab_id = touch_id;
2606}
2607
2608void
2609touch_ungrab(struct input *input)
2610{
2611 struct touch_point *tp, *tmp;
2612
2613 input->touch_grab = 0;
2614
2615 wl_list_for_each_safe(tp, tmp,
2616 &input->touch_point_list, link) {
2617 if (tp->id != input->touch_grab_id)
2618 continue;
2619 wl_list_remove(&tp->link);
2620 free(tp);
2621
2622 return;
2623 }
2624}
2625
2626void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002627input_grab(struct input *input, struct widget *widget, uint32_t button)
2628{
2629 input->grab = widget;
2630 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002631
2632 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002633}
2634
2635void
2636input_ungrab(struct input *input)
2637{
2638 struct widget *widget;
2639
2640 input->grab = NULL;
2641 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002642 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002643 input->sx, input->sy);
2644 input_set_focus_widget(input, widget, input->sx, input->sy);
2645 }
2646}
2647
2648static void
Derek Foreman118a4292015-04-22 17:23:35 -05002649cursor_delay_timer_reset(struct input *input, uint32_t duration)
2650{
2651 struct itimerspec its;
2652
2653 if (!duration)
2654 input->cursor_timer_running = false;
2655 else
2656 input->cursor_timer_running = true;
2657
2658 its.it_interval.tv_sec = 0;
2659 its.it_interval.tv_nsec = 0;
2660 its.it_value.tv_sec = duration / 1000;
2661 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2662 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2663 fprintf(stderr, "could not set cursor timerfd\n: %m");
2664}
2665
2666static void cancel_pointer_image_update(struct input *input)
2667{
2668 if (input->cursor_timer_running)
2669 cursor_delay_timer_reset(input, 0);
2670}
2671
2672static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002673input_remove_pointer_focus(struct input *input)
2674{
2675 struct window *window = input->pointer_focus;
2676
2677 if (!window)
2678 return;
2679
2680 input_set_focus_widget(input, NULL, 0, 0);
2681
2682 input->pointer_focus = NULL;
2683 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002684 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002685}
2686
2687static void
2688pointer_handle_enter(void *data, struct wl_pointer *pointer,
2689 uint32_t serial, struct wl_surface *surface,
2690 wl_fixed_t sx_w, wl_fixed_t sy_w)
2691{
2692 struct input *input = data;
2693 struct window *window;
2694 struct widget *widget;
2695 float sx = wl_fixed_to_double(sx_w);
2696 float sy = wl_fixed_to_double(sy_w);
2697
2698 if (!surface) {
2699 /* enter event for a window we've just destroyed */
2700 return;
2701 }
2702
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002703 window = wl_surface_get_user_data(surface);
2704 if (surface != window->main_surface->surface) {
2705 DBG("Ignoring input event from subsurface %p\n", surface);
2706 return;
2707 }
2708
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002709 input->display->serial = serial;
2710 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002711 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002712
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002713 input->sx = sx;
2714 input->sy = sy;
2715
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002716 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002717 input_set_focus_widget(input, widget, sx, sy);
2718}
2719
2720static void
2721pointer_handle_leave(void *data, struct wl_pointer *pointer,
2722 uint32_t serial, struct wl_surface *surface)
2723{
2724 struct input *input = data;
2725
2726 input->display->serial = serial;
2727 input_remove_pointer_focus(input);
2728}
2729
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002730static void
Daniel Stone37816df2012-05-16 18:45:18 +01002731pointer_handle_motion(void *data, struct wl_pointer *pointer,
2732 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002733{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002734 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002735 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002736 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002737 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002738 float sx = wl_fixed_to_double(sx_w);
2739 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002740
Paul Winwoodb22bf572013-08-29 10:52:54 +01002741 if (!window)
2742 return;
2743
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002744 input->sx = sx;
2745 input->sy = sy;
2746
Rob Bradford5f087742013-07-11 19:41:27 +01002747 /* when making the window smaller - e.g. after a unmaximise we might
2748 * still have a pending motion event that the compositor has picked
2749 * based on the old surface dimensions
2750 */
2751 if (sx > window->main_surface->allocation.width ||
2752 sy > window->main_surface->allocation.height)
2753 return;
2754
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002755 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002756 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002757 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002758 }
2759
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002760 if (input->grab)
2761 widget = input->grab;
2762 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002763 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002764 if (widget) {
2765 if (widget->motion_handler)
2766 cursor = widget->motion_handler(input->focus_widget,
2767 input, time, sx, sy,
2768 widget->user_data);
2769 else
2770 cursor = widget->default_cursor;
2771 } else
2772 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002773
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002774 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002775}
2776
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002777static void
Daniel Stone37816df2012-05-16 18:45:18 +01002778pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002779 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002780{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002781 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002782 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002783 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002784
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002785 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002786 if (input->focus_widget && input->grab == NULL &&
2787 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002788 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002789
Neil Roberts6b28aad2012-01-23 19:11:18 +00002790 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002791 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002792 (*widget->button_handler)(widget,
2793 input, time,
2794 button, state,
2795 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002796
Daniel Stone4dbadb12012-05-30 16:31:51 +01002797 if (input->grab && input->grab_button == button &&
2798 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002799 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002800}
2801
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002802static void
Daniel Stone37816df2012-05-16 18:45:18 +01002803pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002804 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002805{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002806 struct input *input = data;
2807 struct widget *widget;
2808
2809 widget = input->focus_widget;
2810 if (input->grab)
2811 widget = input->grab;
2812 if (widget && widget->axis_handler)
2813 (*widget->axis_handler)(widget,
2814 input, time,
2815 axis, value,
2816 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002817}
2818
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002819static const struct wl_pointer_listener pointer_listener = {
2820 pointer_handle_enter,
2821 pointer_handle_leave,
2822 pointer_handle_motion,
2823 pointer_handle_button,
2824 pointer_handle_axis,
2825};
2826
2827static void
2828input_remove_keyboard_focus(struct input *input)
2829{
2830 struct window *window = input->keyboard_focus;
2831 struct itimerspec its;
2832
2833 its.it_interval.tv_sec = 0;
2834 its.it_interval.tv_nsec = 0;
2835 its.it_value.tv_sec = 0;
2836 its.it_value.tv_nsec = 0;
2837 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2838
2839 if (!window)
2840 return;
2841
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002842 if (window->keyboard_focus_handler)
2843 (*window->keyboard_focus_handler)(window, NULL,
2844 window->user_data);
2845
2846 input->keyboard_focus = NULL;
2847}
2848
2849static void
2850keyboard_repeat_func(struct task *task, uint32_t events)
2851{
2852 struct input *input =
2853 container_of(task, struct input, repeat_task);
2854 struct window *window = input->keyboard_focus;
2855 uint64_t exp;
2856
2857 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2858 /* If we change the timer between the fd becoming
2859 * readable and getting here, there'll be nothing to
2860 * read and we get EAGAIN. */
2861 return;
2862
2863 if (window && window->key_handler) {
2864 (*window->key_handler)(window, input, input->repeat_time,
2865 input->repeat_key, input->repeat_sym,
2866 WL_KEYBOARD_KEY_STATE_PRESSED,
2867 window->user_data);
2868 }
2869}
2870
2871static void
2872keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2873 uint32_t format, int fd, uint32_t size)
2874{
2875 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002876 struct xkb_keymap *keymap;
2877 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002878 char *map_str;
2879
2880 if (!data) {
2881 close(fd);
2882 return;
2883 }
2884
2885 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2886 close(fd);
2887 return;
2888 }
2889
2890 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2891 if (map_str == MAP_FAILED) {
2892 close(fd);
2893 return;
2894 }
2895
Ran Benita2e1968f2014-08-19 23:59:51 +03002896 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2897 map_str,
2898 XKB_KEYMAP_FORMAT_TEXT_V1,
2899 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002900 munmap(map_str, size);
2901 close(fd);
2902
Rui Matos3eccb862013-10-10 19:44:22 +02002903 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002904 fprintf(stderr, "failed to compile keymap\n");
2905 return;
2906 }
2907
Rui Matos3eccb862013-10-10 19:44:22 +02002908 state = xkb_state_new(keymap);
2909 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002910 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002911 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002912 return;
2913 }
2914
Rui Matos3eccb862013-10-10 19:44:22 +02002915 xkb_keymap_unref(input->xkb.keymap);
2916 xkb_state_unref(input->xkb.state);
2917 input->xkb.keymap = keymap;
2918 input->xkb.state = state;
2919
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002920 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002921 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002922 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002923 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002924 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002925 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002926}
2927
2928static void
2929keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2930 uint32_t serial, struct wl_surface *surface,
2931 struct wl_array *keys)
2932{
2933 struct input *input = data;
2934 struct window *window;
2935
2936 input->display->serial = serial;
2937 input->keyboard_focus = wl_surface_get_user_data(surface);
2938
2939 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002940 if (window->keyboard_focus_handler)
2941 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002942 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002943}
2944
2945static void
2946keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2947 uint32_t serial, struct wl_surface *surface)
2948{
2949 struct input *input = data;
2950
2951 input->display->serial = serial;
2952 input_remove_keyboard_focus(input);
2953}
2954
Scott Moreau210d0792012-03-22 10:47:01 -06002955static void
Daniel Stone37816df2012-05-16 18:45:18 +01002956keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002957 uint32_t serial, uint32_t time, uint32_t key,
2958 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002959{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002960 struct input *input = data;
2961 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002962 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002963 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002964 const xkb_keysym_t *syms;
2965 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002966 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002967
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002968 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002969 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002970 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002971 return;
2972
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002973 /* We only use input grabs for pointer events for now, so just
2974 * ignore key presses if a grab is active. We expand the key
2975 * event delivery mechanism to route events to widgets to
2976 * properly handle key grabs. In the meantime, this prevents
2977 * key event devlivery while a grab is active. */
2978 if (input->grab && input->grab_button == 0)
2979 return;
2980
Ran Benita2e1968f2014-08-19 23:59:51 +03002981 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002982
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002983 sym = XKB_KEY_NoSymbol;
2984 if (num_syms == 1)
2985 sym = syms[0];
2986
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002987
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002988 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002989 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002990 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002991 } else if (sym == XKB_KEY_F11 &&
2992 window->fullscreen_handler &&
2993 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2994 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002995 } else if (sym == XKB_KEY_F4 &&
2996 input->modifiers == MOD_ALT_MASK &&
2997 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002998 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002999 } else if (window->key_handler) {
3000 (*window->key_handler)(window, input, time, key,
3001 sym, state, window->user_data);
3002 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003003
3004 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3005 key == input->repeat_key) {
3006 its.it_interval.tv_sec = 0;
3007 its.it_interval.tv_nsec = 0;
3008 its.it_value.tv_sec = 0;
3009 its.it_value.tv_nsec = 0;
3010 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003011 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3012 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003013 input->repeat_sym = sym;
3014 input->repeat_key = key;
3015 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003016 its.it_interval.tv_sec = input->repeat_rate_sec;
3017 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3018 its.it_value.tv_sec = input->repeat_delay_sec;
3019 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003020 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3021 }
3022}
3023
3024static void
Daniel Stone351eb612012-05-31 15:27:47 -04003025keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3026 uint32_t serial, uint32_t mods_depressed,
3027 uint32_t mods_latched, uint32_t mods_locked,
3028 uint32_t group)
3029{
3030 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003031 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003032
Matt Ropere61561f2013-06-24 16:52:43 +01003033 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3034 if (!input->xkb.keymap)
3035 return;
3036
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003037 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3038 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003039 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003040 XKB_STATE_MODS_DEPRESSED |
3041 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003042 input->modifiers = 0;
3043 if (mask & input->xkb.control_mask)
3044 input->modifiers |= MOD_CONTROL_MASK;
3045 if (mask & input->xkb.alt_mask)
3046 input->modifiers |= MOD_ALT_MASK;
3047 if (mask & input->xkb.shift_mask)
3048 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003049}
3050
Jonny Lamb06959082014-08-12 14:58:27 +02003051static void
3052set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3053{
3054 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3055 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3056
3057 /* a rate of zero disables any repeating, regardless of the delay's
3058 * value */
3059 if (rate == 0)
3060 return;
3061
3062 if (rate == 1)
3063 input->repeat_rate_sec = 1;
3064 else
3065 input->repeat_rate_nsec = 1000000000 / rate;
3066
3067 input->repeat_delay_sec = delay / 1000;
3068 delay -= (input->repeat_delay_sec * 1000);
3069 input->repeat_delay_nsec = delay * 1000 * 1000;
3070}
3071
3072static void
3073keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3074 int32_t rate, int32_t delay)
3075{
3076 struct input *input = data;
3077
3078 set_repeat_info(input, rate, delay);
3079}
3080
Daniel Stone37816df2012-05-16 18:45:18 +01003081static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003082 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003083 keyboard_handle_enter,
3084 keyboard_handle_leave,
3085 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003086 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003087 keyboard_handle_repeat_info
3088
Daniel Stone37816df2012-05-16 18:45:18 +01003089};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003090
3091static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003092touch_handle_down(void *data, struct wl_touch *wl_touch,
3093 uint32_t serial, uint32_t time, struct wl_surface *surface,
3094 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3095{
3096 struct input *input = data;
3097 struct widget *widget;
3098 float sx = wl_fixed_to_double(x_w);
3099 float sy = wl_fixed_to_double(y_w);
3100
Rusty Lynch1084da52013-08-15 09:10:08 -07003101 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003102 input->touch_focus = wl_surface_get_user_data(surface);
3103 if (!input->touch_focus) {
3104 DBG("Failed to find to touch focus for surface %p\n", surface);
3105 return;
3106 }
3107
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003108 if (surface != input->touch_focus->main_surface->surface) {
3109 DBG("Ignoring input event from subsurface %p\n", surface);
3110 input->touch_focus = NULL;
3111 return;
3112 }
3113
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003114 if (input->grab)
3115 widget = input->grab;
3116 else
3117 widget = window_find_widget(input->touch_focus,
3118 wl_fixed_to_double(x_w),
3119 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003120 if (widget) {
3121 struct touch_point *tp = xmalloc(sizeof *tp);
3122 if (tp) {
3123 tp->id = id;
3124 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003125 tp->x = sx;
3126 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003127 wl_list_insert(&input->touch_point_list, &tp->link);
3128
3129 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003130 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003131 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003132 sx, sy,
3133 widget->user_data);
3134 }
3135 }
3136}
3137
3138static void
3139touch_handle_up(void *data, struct wl_touch *wl_touch,
3140 uint32_t serial, uint32_t time, int32_t id)
3141{
3142 struct input *input = data;
3143 struct touch_point *tp, *tmp;
3144
Rusty Lynch041815a2013-08-08 21:20:38 -07003145 if (!input->touch_focus) {
3146 DBG("No touch focus found for touch up event!\n");
3147 return;
3148 }
3149
3150 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3151 if (tp->id != id)
3152 continue;
3153
3154 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003155 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003156 time, id,
3157 tp->widget->user_data);
3158
3159 wl_list_remove(&tp->link);
3160 free(tp);
3161
3162 return;
3163 }
3164}
3165
3166static void
3167touch_handle_motion(void *data, struct wl_touch *wl_touch,
3168 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3169{
3170 struct input *input = data;
3171 struct touch_point *tp;
3172 float sx = wl_fixed_to_double(x_w);
3173 float sy = wl_fixed_to_double(y_w);
3174
3175 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3176
3177 if (!input->touch_focus) {
3178 DBG("No touch focus found for touch motion event!\n");
3179 return;
3180 }
3181
3182 wl_list_for_each(tp, &input->touch_point_list, link) {
3183 if (tp->id != id)
3184 continue;
3185
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003186 tp->x = sx;
3187 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003188 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003189 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003190 id, sx, sy,
3191 tp->widget->user_data);
3192 return;
3193 }
3194}
3195
3196static void
3197touch_handle_frame(void *data, struct wl_touch *wl_touch)
3198{
3199 struct input *input = data;
3200 struct touch_point *tp, *tmp;
3201
3202 DBG("touch_handle_frame\n");
3203
3204 if (!input->touch_focus) {
3205 DBG("No touch focus found for touch frame event!\n");
3206 return;
3207 }
3208
3209 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3210 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003211 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003212 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003213 }
3214}
3215
3216static void
3217touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3218{
3219 struct input *input = data;
3220 struct touch_point *tp, *tmp;
3221
3222 DBG("touch_handle_cancel\n");
3223
3224 if (!input->touch_focus) {
3225 DBG("No touch focus found for touch cancel event!\n");
3226 return;
3227 }
3228
3229 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3230 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003231 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3232 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003233
3234 wl_list_remove(&tp->link);
3235 free(tp);
3236 }
3237}
3238
3239static const struct wl_touch_listener touch_listener = {
3240 touch_handle_down,
3241 touch_handle_up,
3242 touch_handle_motion,
3243 touch_handle_frame,
3244 touch_handle_cancel,
3245};
3246
3247static void
Daniel Stone37816df2012-05-16 18:45:18 +01003248seat_handle_capabilities(void *data, struct wl_seat *seat,
3249 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003250{
Daniel Stone37816df2012-05-16 18:45:18 +01003251 struct input *input = data;
3252
3253 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3254 input->pointer = wl_seat_get_pointer(seat);
3255 wl_pointer_set_user_data(input->pointer, input);
3256 wl_pointer_add_listener(input->pointer, &pointer_listener,
3257 input);
3258 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3259 wl_pointer_destroy(input->pointer);
3260 input->pointer = NULL;
3261 }
3262
3263 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3264 input->keyboard = wl_seat_get_keyboard(seat);
3265 wl_keyboard_set_user_data(input->keyboard, input);
3266 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3267 input);
3268 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3269 wl_keyboard_destroy(input->keyboard);
3270 input->keyboard = NULL;
3271 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003272
3273 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3274 input->touch = wl_seat_get_touch(seat);
3275 wl_touch_set_user_data(input->touch, input);
3276 wl_touch_add_listener(input->touch, &touch_listener, input);
3277 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3278 wl_touch_destroy(input->touch);
3279 input->touch = NULL;
3280 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003281}
3282
Rob Bradford08031182013-08-13 20:11:03 +01003283static void
3284seat_handle_name(void *data, struct wl_seat *seat,
3285 const char *name)
3286{
3287
3288}
3289
Daniel Stone37816df2012-05-16 18:45:18 +01003290static const struct wl_seat_listener seat_listener = {
3291 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003292 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003293};
3294
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003295void
3296input_get_position(struct input *input, int32_t *x, int32_t *y)
3297{
3298 *x = input->sx;
3299 *y = input->sy;
3300}
3301
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003302int
3303input_get_touch(struct input *input, int32_t id, float *x, float *y)
3304{
3305 struct touch_point *tp;
3306
3307 wl_list_for_each(tp, &input->touch_point_list, link) {
3308 if (tp->id != id)
3309 continue;
3310
3311 *x = tp->x;
3312 *y = tp->y;
3313 return 0;
3314 }
3315
3316 return -1;
3317}
3318
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003319struct display *
3320input_get_display(struct input *input)
3321{
3322 return input->display;
3323}
3324
Daniel Stone37816df2012-05-16 18:45:18 +01003325struct wl_seat *
3326input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003327{
Daniel Stone37816df2012-05-16 18:45:18 +01003328 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003329}
3330
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003331uint32_t
3332input_get_modifiers(struct input *input)
3333{
3334 return input->modifiers;
3335}
3336
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003337struct widget *
3338input_get_focus_widget(struct input *input)
3339{
3340 return input->focus_widget;
3341}
3342
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003343struct data_offer {
3344 struct wl_data_offer *offer;
3345 struct input *input;
3346 struct wl_array types;
3347 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003348
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003349 struct task io_task;
3350 int fd;
3351 data_func_t func;
3352 int32_t x, y;
3353 void *user_data;
3354};
3355
3356static void
3357data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3358{
3359 struct data_offer *offer = data;
3360 char **p;
3361
3362 p = wl_array_add(&offer->types, sizeof *p);
3363 *p = strdup(type);
3364}
3365
3366static const struct wl_data_offer_listener data_offer_listener = {
3367 data_offer_offer,
3368};
3369
3370static void
3371data_offer_destroy(struct data_offer *offer)
3372{
3373 char **p;
3374
3375 offer->refcount--;
3376 if (offer->refcount == 0) {
3377 wl_data_offer_destroy(offer->offer);
3378 for (p = offer->types.data; *p; p++)
3379 free(*p);
3380 wl_array_release(&offer->types);
3381 free(offer);
3382 }
3383}
3384
3385static void
3386data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003387 struct wl_data_device *data_device,
3388 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003389{
3390 struct data_offer *offer;
3391
Brian Lovinbc919262013-08-07 15:34:59 -07003392 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003393
3394 wl_array_init(&offer->types);
3395 offer->refcount = 1;
3396 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003397 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003398 wl_data_offer_add_listener(offer->offer,
3399 &data_offer_listener, offer);
3400}
3401
3402static void
3403data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003404 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003405 wl_fixed_t x_w, wl_fixed_t y_w,
3406 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003407{
3408 struct input *input = data;
3409 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003410 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003411 float x = wl_fixed_to_double(x_w);
3412 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003413 char **p;
3414
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003415 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003416 input->drag_enter_serial = serial;
3417 input->drag_focus = window,
3418 input->drag_x = x;
3419 input->drag_y = y;
3420
3421 if (!input->touch_grab)
3422 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003423
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003424 if (offer) {
3425 input->drag_offer = wl_data_offer_get_user_data(offer);
3426
3427 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3428 *p = NULL;
3429
3430 types_data = input->drag_offer->types.data;
3431 } else {
3432 input->drag_offer = NULL;
3433 types_data = NULL;
3434 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003435
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003436 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003437 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003438 window->user_data);
3439}
3440
3441static void
3442data_device_leave(void *data, struct wl_data_device *data_device)
3443{
3444 struct input *input = data;
3445
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003446 if (input->drag_offer) {
3447 data_offer_destroy(input->drag_offer);
3448 input->drag_offer = NULL;
3449 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003450}
3451
3452static void
3453data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003454 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003455{
3456 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003457 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003458 float x = wl_fixed_to_double(x_w);
3459 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003460 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003461
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003462 input->drag_x = x;
3463 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003464
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003465 if (input->drag_offer)
3466 types_data = input->drag_offer->types.data;
3467 else
3468 types_data = NULL;
3469
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003470 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003471 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003472 window->user_data);
3473}
3474
3475static void
3476data_device_drop(void *data, struct wl_data_device *data_device)
3477{
3478 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003479 struct window *window = input->drag_focus;
3480 float x, y;
3481
3482 x = input->drag_x;
3483 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003484
3485 if (window->drop_handler)
3486 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003487 x, y, window->user_data);
3488
3489 if (input->touch_grab)
3490 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003491}
3492
3493static void
3494data_device_selection(void *data,
3495 struct wl_data_device *wl_data_device,
3496 struct wl_data_offer *offer)
3497{
3498 struct input *input = data;
3499 char **p;
3500
3501 if (input->selection_offer)
3502 data_offer_destroy(input->selection_offer);
3503
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003504 if (offer) {
3505 input->selection_offer = wl_data_offer_get_user_data(offer);
3506 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3507 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003508 } else {
3509 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003510 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003511}
3512
3513static const struct wl_data_device_listener data_device_listener = {
3514 data_device_data_offer,
3515 data_device_enter,
3516 data_device_leave,
3517 data_device_motion,
3518 data_device_drop,
3519 data_device_selection
3520};
3521
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003522static void
3523input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003524{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003525 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003526 struct wl_cursor *cursor;
3527 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003528
Daniel Stone80972742012-11-07 17:51:39 +11003529 if (!input->pointer)
3530 return;
3531
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003532 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003533 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003534 return;
3535
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003536 if (index >= (int) cursor->image_count) {
3537 fprintf(stderr, "cursor index out of range\n");
3538 return;
3539 }
3540
3541 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003542 buffer = wl_cursor_image_get_buffer(image);
3543 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003544 return;
3545
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003546 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3547 wl_surface_damage(input->pointer_surface, 0, 0,
3548 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003549 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003550 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3551 input->pointer_surface,
3552 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003553}
3554
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003555static const struct wl_callback_listener pointer_surface_listener;
3556
Derek Foreman493d9792015-03-04 16:26:25 -06003557static bool
3558input_set_pointer_special(struct input *input)
3559{
3560 if (input->current_cursor == CURSOR_BLANK) {
3561 wl_pointer_set_cursor(input->pointer,
3562 input->pointer_enter_serial,
3563 NULL, 0, 0);
3564 return true;
3565 }
3566
3567 if (input->current_cursor == CURSOR_UNSET)
3568 return true;
3569
3570 return false;
3571}
3572
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003573static void
Derek Foreman118a4292015-04-22 17:23:35 -05003574schedule_pointer_image_update(struct input *input,
3575 struct wl_cursor *cursor,
3576 uint32_t duration,
3577 bool force_frame)
3578{
3579 /* Some silly cursor sets have enormous pauses in them. In these
3580 * cases it's better to use a timer even if it results in less
3581 * accurate presentation, since it will save us having to set the
3582 * same cursor image over and over again.
3583 *
3584 * This is really not the way we're supposed to time any kind of
3585 * animation, but we're pretending it's OK here because we don't
3586 * want animated cursors with long delays to needlessly hog CPU.
3587 *
3588 * We use force_frame to ensure we don't accumulate large timing
3589 * errors by running off the wrong clock.
3590 */
3591 if (!force_frame && duration > 100) {
3592 struct timespec tp;
3593
3594 clock_gettime(CLOCK_MONOTONIC, &tp);
3595 input->cursor_timer_start = tp.tv_sec * 1000
3596 + tp.tv_nsec / 1000000;
3597 cursor_delay_timer_reset(input, duration);
3598 return;
3599 }
3600
3601 /* for short durations we'll just spin on frame callbacks for
3602 * accurate timing - the way any kind of timing sensitive animation
3603 * should really be done. */
3604 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3605 wl_callback_add_listener(input->cursor_frame_cb,
3606 &pointer_surface_listener, input);
3607
3608}
3609
3610static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003611pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3612 uint32_t time)
3613{
3614 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003615 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003616 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003617 uint32_t duration;
3618 bool force_frame = true;
3619
3620 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003621
3622 if (callback) {
3623 assert(callback == input->cursor_frame_cb);
3624 wl_callback_destroy(callback);
3625 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003626 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003627 }
3628
Daniel Stone80972742012-11-07 17:51:39 +11003629 if (!input->pointer)
3630 return;
3631
Derek Foreman493d9792015-03-04 16:26:25 -06003632 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003633 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003634
Daniel Stonea494f1d2012-06-18 19:31:12 +01003635 cursor = input->display->cursors[input->current_cursor];
3636 if (!cursor)
3637 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003638
3639 /* FIXME We don't have the current time on the first call so we set
3640 * the animation start to the time of the first frame callback. */
3641 if (time == 0)
3642 input->cursor_anim_start = 0;
3643 else if (input->cursor_anim_start == 0)
3644 input->cursor_anim_start = time;
3645
Derek Foreman118a4292015-04-22 17:23:35 -05003646 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003647
Derek Foreman118a4292015-04-22 17:23:35 -05003648 if (time == 0 || input->cursor_anim_start == 0) {
3649 duration = 0;
3650 i = 0;
3651 } else
3652 i = wl_cursor_frame_and_duration(
3653 cursor,
3654 time - input->cursor_anim_start,
3655 &duration);
3656
3657 if (cursor->image_count > 1)
3658 schedule_pointer_image_update(input, cursor, duration,
3659 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003660
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003661 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003662}
3663
Derek Foreman118a4292015-04-22 17:23:35 -05003664static void
3665cursor_timer_func(struct task *task, uint32_t events)
3666{
3667 struct input *input = container_of(task, struct input, cursor_task);
3668 struct timespec tp;
3669 struct wl_cursor *cursor;
3670 uint32_t time;
3671 uint64_t exp;
3672
3673 if (!input->cursor_timer_running)
3674 return;
3675
3676 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3677 return;
3678
3679 cursor = input->display->cursors[input->current_cursor];
3680 if (!cursor)
3681 return;
3682
3683 clock_gettime(CLOCK_MONOTONIC, &tp);
3684 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3685 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3686}
3687
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003688static const struct wl_callback_listener pointer_surface_listener = {
3689 pointer_surface_frame_callback
3690};
3691
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003692void
3693input_set_pointer_image(struct input *input, int pointer)
3694{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003695 int force = 0;
3696
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003697 if (!input->pointer)
3698 return;
3699
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003700 if (input->pointer_enter_serial > input->cursor_serial)
3701 force = 1;
3702
3703 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003704 return;
3705
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003706 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003707 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003708 if (!input->cursor_frame_cb)
3709 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003710 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003711 /* The current frame callback may be stuck if, for instance,
3712 * the set cursor request was processed by the server after
3713 * this client lost the focus. In this case the cursor surface
3714 * might not be mapped and the frame callback wouldn't ever
3715 * complete. Send a set_cursor and attach to try to map the
3716 * cursor surface again so that the callback will finish */
3717 input_set_pointer_image_index(input, 0);
3718 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003719}
3720
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003721struct wl_data_device *
3722input_get_data_device(struct input *input)
3723{
3724 return input->data_device;
3725}
3726
3727void
3728input_set_selection(struct input *input,
3729 struct wl_data_source *source, uint32_t time)
3730{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003731 if (input->data_device)
3732 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003733}
3734
3735void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003736input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003737{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003738 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003739 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003740}
3741
3742static void
3743offer_io_func(struct task *task, uint32_t events)
3744{
3745 struct data_offer *offer =
3746 container_of(task, struct data_offer, io_task);
3747 unsigned int len;
3748 char buffer[4096];
3749
3750 len = read(offer->fd, buffer, sizeof buffer);
3751 offer->func(buffer, len,
3752 offer->x, offer->y, offer->user_data);
3753
3754 if (len == 0) {
3755 close(offer->fd);
3756 data_offer_destroy(offer);
3757 }
3758}
3759
3760static void
3761data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3762 data_func_t func, void *user_data)
3763{
3764 int p[2];
3765
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003766 if (pipe2(p, O_CLOEXEC) == -1)
3767 return;
3768
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003769 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3770 close(p[1]);
3771
3772 offer->io_task.run = offer_io_func;
3773 offer->fd = p[0];
3774 offer->func = func;
3775 offer->refcount++;
3776 offer->user_data = user_data;
3777
3778 display_watch_fd(offer->input->display,
3779 offer->fd, EPOLLIN, &offer->io_task);
3780}
3781
3782void
3783input_receive_drag_data(struct input *input, const char *mime_type,
3784 data_func_t func, void *data)
3785{
3786 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003787 input->drag_offer->x = input->drag_x;
3788 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003789}
3790
3791int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003792input_receive_drag_data_to_fd(struct input *input,
3793 const char *mime_type, int fd)
3794{
3795 if (input->drag_offer)
3796 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3797
3798 return 0;
3799}
3800
3801int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003802input_receive_selection_data(struct input *input, const char *mime_type,
3803 data_func_t func, void *data)
3804{
3805 char **p;
3806
3807 if (input->selection_offer == NULL)
3808 return -1;
3809
3810 for (p = input->selection_offer->types.data; *p; p++)
3811 if (strcmp(mime_type, *p) == 0)
3812 break;
3813
3814 if (*p == NULL)
3815 return -1;
3816
3817 data_offer_receive_data(input->selection_offer,
3818 mime_type, func, data);
3819 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003820}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003821
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003822int
3823input_receive_selection_data_to_fd(struct input *input,
3824 const char *mime_type, int fd)
3825{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003826 if (input->selection_offer)
3827 wl_data_offer_receive(input->selection_offer->offer,
3828 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003829
3830 return 0;
3831}
3832
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003833void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003834window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003835{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003836 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003837 return;
3838
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003839 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003840}
3841
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003842static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003843surface_set_synchronized(struct surface *surface)
3844{
3845 if (!surface->subsurface)
3846 return;
3847
3848 if (surface->synchronized)
3849 return;
3850
3851 wl_subsurface_set_sync(surface->subsurface);
3852 surface->synchronized = 1;
3853}
3854
3855static void
3856surface_set_synchronized_default(struct surface *surface)
3857{
3858 if (!surface->subsurface)
3859 return;
3860
3861 if (surface->synchronized == surface->synchronized_default)
3862 return;
3863
3864 if (surface->synchronized_default)
3865 wl_subsurface_set_sync(surface->subsurface);
3866 else
3867 wl_subsurface_set_desync(surface->subsurface);
3868
3869 surface->synchronized = surface->synchronized_default;
3870}
3871
3872static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003873surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003874{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003875 struct widget *widget = surface->widget;
3876 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003877
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003878 if (surface->input_region) {
3879 wl_region_destroy(surface->input_region);
3880 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003881 }
3882
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003883 if (surface->opaque_region)
3884 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003885
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003886 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003887
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003888 if (widget->resize_handler)
3889 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003890 widget->allocation.width,
3891 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003892 widget->user_data);
3893
Pekka Paalanen35e82632013-04-25 13:57:48 +03003894 if (surface->subsurface &&
3895 (surface->allocation.x != widget->allocation.x ||
3896 surface->allocation.y != widget->allocation.y)) {
3897 wl_subsurface_set_position(surface->subsurface,
3898 widget->allocation.x,
3899 widget->allocation.y);
3900 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003901 if (surface->allocation.width != widget->allocation.width ||
3902 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003903 window_schedule_redraw(widget->window);
3904 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003905 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003906
3907 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003908 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003909 widget->allocation.width,
3910 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003911}
3912
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003913static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003914hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3915{
3916 /*
3917 * This hack should be removed, when EGL respects
3918 * eglSwapInterval(0).
3919 *
3920 * If this window has sub-surfaces, especially a free-running
3921 * EGL-widget, we need to post the parent surface once with
3922 * all the old state to guarantee, that the EGL-widget will
3923 * receive its frame callback soon. Otherwise, a forced call
3924 * to eglSwapBuffers may end up blocking, waiting for a frame
3925 * event that will never come, because we will commit the parent
3926 * surface with all new state only after eglSwapBuffers returns.
3927 *
3928 * This assumes, that:
3929 * 1. When the EGL widget's resize hook is called, it pauses.
3930 * 2. When the EGL widget's redraw hook is called, it forces a
3931 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3932 * In a single threaded application condition 1 is a no-op.
3933 *
3934 * XXX: This should actually be after the surface_resize() calls,
3935 * but cannot, because then it would commit the incomplete state
3936 * accumulated from the widget resize hooks.
3937 */
3938 if (window->subsurface_list.next != &window->main_surface->link ||
3939 window->subsurface_list.prev != &window->main_surface->link)
3940 wl_surface_commit(window->main_surface->surface);
3941}
3942
3943static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003944window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003945{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003946 struct surface *surface;
3947
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003948 widget_set_allocation(window->main_surface->widget,
3949 window->pending_allocation.x,
3950 window->pending_allocation.y,
3951 window->pending_allocation.width,
3952 window->pending_allocation.height);
3953
3954 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003955
3956 /* The main surface is in the list, too. Main surface's
3957 * resize_handler is responsible for calling widget_set_allocation()
3958 * on all sub-surface root widgets, so they will be resized
3959 * properly.
3960 */
3961 wl_list_for_each(surface, &window->subsurface_list, link) {
3962 if (surface == window->main_surface)
3963 continue;
3964
3965 surface_set_synchronized(surface);
3966 surface_resize(surface);
3967 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003968
3969 if (!window->fullscreen && !window->maximized)
3970 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003971}
3972
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003973static void
3974idle_resize(struct window *window)
3975{
3976 window->resize_needed = 0;
3977 window->redraw_needed = 1;
3978
3979 DBG("from %dx%d to %dx%d\n",
3980 window->main_surface->server_allocation.width,
3981 window->main_surface->server_allocation.height,
3982 window->pending_allocation.width,
3983 window->pending_allocation.height);
3984
3985 hack_prevent_EGL_sub_surface_deadlock(window);
3986
3987 window_do_resize(window);
3988}
3989
3990static void
3991undo_resize(struct window *window)
3992{
3993 window->pending_allocation.width =
3994 window->main_surface->server_allocation.width;
3995 window->pending_allocation.height =
3996 window->main_surface->server_allocation.height;
3997
3998 DBG("back to %dx%d\n",
3999 window->main_surface->server_allocation.width,
4000 window->main_surface->server_allocation.height);
4001
4002 window_do_resize(window);
4003
4004 if (window->pending_allocation.width == 0 &&
4005 window->pending_allocation.height == 0) {
4006 fprintf(stderr, "Error: Could not draw a surface, "
4007 "most likely due to insufficient disk space in "
4008 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4009 exit(EXIT_FAILURE);
4010 }
4011}
4012
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004013void
4014window_schedule_resize(struct window *window, int width, int height)
4015{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004016 /* We should probably get these numbers from the theme. */
4017 const int min_width = 200, min_height = 200;
4018
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004019 window->pending_allocation.x = 0;
4020 window->pending_allocation.y = 0;
4021 window->pending_allocation.width = width;
4022 window->pending_allocation.height = height;
4023
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004024 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004025 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004026 window->min_allocation.width = min_width;
4027 else
4028 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004029 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004030 window->min_allocation.height = min_height;
4031 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004032 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004033 }
4034
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004035 if (window->pending_allocation.width < window->min_allocation.width)
4036 window->pending_allocation.width = window->min_allocation.width;
4037 if (window->pending_allocation.height < window->min_allocation.height)
4038 window->pending_allocation.height = window->min_allocation.height;
4039
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004040 window->resize_needed = 1;
4041 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004042}
4043
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004044void
4045widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4046{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004047 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004048}
4049
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004050static int
4051window_get_shadow_margin(struct window *window)
4052{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004053 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004054 return frame_get_shadow_margin(window->frame->frame);
4055 else
4056 return 0;
4057}
4058
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004059static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004060handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004061 int32_t width, int32_t height,
4062 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004063{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004064 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004065 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004066
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004067 window->maximized = 0;
4068 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004069 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004070 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004071
4072 wl_array_for_each(p, states) {
4073 uint32_t state = *p;
4074 switch (state) {
4075 case XDG_SURFACE_STATE_MAXIMIZED:
4076 window->maximized = 1;
4077 break;
4078 case XDG_SURFACE_STATE_FULLSCREEN:
4079 window->fullscreen = 1;
4080 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004081 case XDG_SURFACE_STATE_RESIZING:
4082 window->resizing = 1;
4083 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004084 case XDG_SURFACE_STATE_ACTIVATED:
4085 window->focused = 1;
4086 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004087 default:
4088 /* Unknown state */
4089 break;
4090 }
4091 }
4092
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004093 if (window->frame) {
4094 if (window->maximized) {
4095 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4096 } else {
4097 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4098 }
4099
4100 if (window->focused) {
4101 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4102 } else {
4103 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4104 }
4105 }
4106
Jasper St. Pierref184c382014-05-06 08:33:27 -04004107 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004108 /* The width / height params are for window geometry,
4109 * but window_schedule_resize takes allocation. Add
4110 * on the shadow margin to get the difference. */
4111 int margin = window_get_shadow_margin(window);
4112
4113 window_schedule_resize(window,
4114 width + margin * 2,
4115 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004116 } else {
4117 window_schedule_resize(window,
4118 window->saved_allocation.width,
4119 window->saved_allocation.height);
4120 }
4121
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004122 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004123
4124 if (window->state_changed_handler)
4125 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004126}
4127
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004128static void
4129handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4130{
4131 struct window *window = data;
4132 window_close(window);
4133}
4134
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004135static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004136 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004137 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004138};
4139
4140static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004141window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004142{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004143 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004144
4145 if (!window->xdg_surface)
4146 return;
4147
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004148 if (window->parent == window->last_parent)
4149 return;
4150
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004151 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004152 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004153 else
4154 parent_surface = NULL;
4155
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004156 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004157 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004158}
4159
4160static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004161window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004162{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004163 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004164 frame_input_rect(window->frame->frame,
4165 &geometry->x,
4166 &geometry->y,
4167 &geometry->width,
4168 &geometry->height);
4169 else
4170 window_get_allocation(window, geometry);
4171}
4172
4173static void
4174window_sync_geometry(struct window *window)
4175{
4176 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004177
4178 if (!window->xdg_surface)
4179 return;
4180
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004181 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004182 if (geometry.x == window->last_geometry.x &&
4183 geometry.y == window->last_geometry.y &&
4184 geometry.width == window->last_geometry.width &&
4185 geometry.height == window->last_geometry.height)
4186 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004187
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004188 xdg_surface_set_window_geometry(window->xdg_surface,
4189 geometry.x,
4190 geometry.y,
4191 geometry.width,
4192 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004193 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004194}
4195
4196static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004197window_flush(struct window *window)
4198{
4199 struct surface *surface;
4200
4201 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004202 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004203 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004204 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004205 }
4206 }
4207
4208 wl_list_for_each(surface, &window->subsurface_list, link) {
4209 if (surface == window->main_surface)
4210 continue;
4211
4212 surface_flush(surface);
4213 }
4214
4215 surface_flush(window->main_surface);
4216}
4217
4218static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004219menu_destroy(struct menu *menu)
4220{
4221 widget_destroy(menu->widget);
4222 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004223 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004224 free(menu);
4225}
4226
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004227void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004228window_get_allocation(struct window *window,
4229 struct rectangle *allocation)
4230{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004231 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004232}
4233
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004234static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004235widget_redraw(struct widget *widget)
4236{
4237 struct widget *child;
4238
4239 if (widget->redraw_handler)
4240 widget->redraw_handler(widget, widget->user_data);
4241 wl_list_for_each(child, &widget->child_list, link)
4242 widget_redraw(child);
4243}
4244
4245static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004246frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4247{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004248 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004249
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004250 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004251 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004252 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004253 surface->frame_cb = NULL;
4254
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004255 surface->last_time = time;
4256
Pekka Paalanen71233882013-04-25 13:57:53 +03004257 if (surface->redraw_needed || surface->window->redraw_needed) {
4258 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004259 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004260 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004261}
4262
4263static const struct wl_callback_listener listener = {
4264 frame_callback
4265};
4266
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004267static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004268surface_redraw(struct surface *surface)
4269{
Pekka Paalanen71233882013-04-25 13:57:53 +03004270 DBG_OBJ(surface->surface, "begin\n");
4271
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004272 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004273 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004274
4275 /* Whole-window redraw forces a redraw even if the previous has
4276 * not yet hit the screen.
4277 */
4278 if (surface->frame_cb) {
4279 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004280 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004281
Pekka Paalanen71233882013-04-25 13:57:53 +03004282 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004283 wl_callback_destroy(surface->frame_cb);
4284 }
4285
Neil Roberts97b747c2013-12-19 16:17:12 +00004286 if (surface->widget->use_cairo &&
4287 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004288 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4289 return -1;
4290 }
4291
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004292 surface->frame_cb = wl_surface_frame(surface->surface);
4293 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004294 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004295
4296 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004297 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004298 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004299 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004300 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004301}
4302
4303static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004304idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004305{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004306 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004307 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004308 int failed = 0;
4309 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004310
Pekka Paalanen71233882013-04-25 13:57:53 +03004311 DBG(" --------- \n");
4312
Pekka Paalaneneebff542013-04-25 13:57:52 +03004313 wl_list_init(&window->redraw_task.link);
4314 window->redraw_task_scheduled = 0;
4315
4316 if (window->resize_needed) {
4317 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004318 if (window->main_surface->frame_cb) {
4319 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004320 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004321 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004322
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004323 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004324 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004325 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004326
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004327 if (surface_redraw(window->main_surface) < 0) {
4328 /*
4329 * Only main_surface failure will cause us to undo the resize.
4330 * If sub-surfaces fail, they will just be broken with old
4331 * content.
4332 */
4333 failed = 1;
4334 } else {
4335 wl_list_for_each(surface, &window->subsurface_list, link) {
4336 if (surface == window->main_surface)
4337 continue;
4338
4339 surface_redraw(surface);
4340 }
4341 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004342
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004343 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004344 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004345
4346 wl_list_for_each(surface, &window->subsurface_list, link)
4347 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004348
4349 if (resized && failed) {
4350 /* Restore widget tree to correspond to what is on screen. */
4351 undo_resize(window);
4352 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004353}
4354
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004355static void
4356window_schedule_redraw_task(struct window *window)
4357{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004358 if (!window->redraw_task_scheduled) {
4359 window->redraw_task.run = idle_redraw;
4360 display_defer(window->display, &window->redraw_task);
4361 window->redraw_task_scheduled = 1;
4362 }
4363}
4364
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004365void
4366window_schedule_redraw(struct window *window)
4367{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004368 struct surface *surface;
4369
Pekka Paalanen71233882013-04-25 13:57:53 +03004370 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4371
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004372 wl_list_for_each(surface, &window->subsurface_list, link)
4373 surface->redraw_needed = 1;
4374
4375 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004376}
4377
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004378int
4379window_is_fullscreen(struct window *window)
4380{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004381 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004382}
4383
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004384void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004385window_set_fullscreen(struct window *window, int fullscreen)
4386{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004387 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004388 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004389
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004390 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004391 return;
4392
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004393 if (fullscreen)
4394 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4395 else
4396 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004397}
4398
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004399int
4400window_is_maximized(struct window *window)
4401{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004402 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004403}
4404
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004405void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004406window_set_maximized(struct window *window, int maximized)
4407{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004408 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004409 return;
4410
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004411 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004412 return;
4413
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004414 if (maximized)
4415 xdg_surface_set_maximized(window->xdg_surface);
4416 else
4417 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004418}
4419
Jasper St. Pierrede680992014-04-10 17:23:49 -07004420int
4421window_is_resizing(struct window *window)
4422{
4423 return window->resizing;
4424}
4425
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004426void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004427window_set_minimized(struct window *window)
4428{
4429 if (!window->xdg_surface)
4430 return;
4431
4432 xdg_surface_set_minimized(window->xdg_surface);
4433}
4434
4435void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004436window_set_user_data(struct window *window, void *data)
4437{
4438 window->user_data = data;
4439}
4440
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004441void *
4442window_get_user_data(struct window *window)
4443{
4444 return window->user_data;
4445}
4446
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004447void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004448window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004449 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004450{
4451 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004452}
4453
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004454void
4455window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004456 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004457{
4458 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004459}
4460
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004461void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004462window_set_data_handler(struct window *window, window_data_handler_t handler)
4463{
4464 window->data_handler = handler;
4465}
4466
4467void
4468window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4469{
4470 window->drop_handler = handler;
4471}
4472
4473void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004474window_set_close_handler(struct window *window,
4475 window_close_handler_t handler)
4476{
4477 window->close_handler = handler;
4478}
4479
4480void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004481window_set_fullscreen_handler(struct window *window,
4482 window_fullscreen_handler_t handler)
4483{
4484 window->fullscreen_handler = handler;
4485}
4486
4487void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004488window_set_output_handler(struct window *window,
4489 window_output_handler_t handler)
4490{
4491 window->output_handler = handler;
4492}
4493
4494void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004495window_set_state_changed_handler(struct window *window,
4496 window_state_changed_handler_t handler)
4497{
4498 window->state_changed_handler = handler;
4499}
4500
4501void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004502window_set_title(struct window *window, const char *title)
4503{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004504 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004505 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004506 if (window->frame) {
4507 frame_set_title(window->frame->frame, title);
4508 widget_schedule_redraw(window->frame->widget);
4509 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004510 if (window->xdg_surface)
4511 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004512}
4513
4514const char *
4515window_get_title(struct window *window)
4516{
4517 return window->title;
4518}
4519
4520void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004521window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4522{
4523 struct text_cursor_position *text_cursor_position =
4524 window->display->text_cursor_position;
4525
Scott Moreau9295ce02012-06-01 12:46:10 -06004526 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004527 return;
4528
4529 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004530 window->main_surface->surface,
4531 wl_fixed_from_int(x),
4532 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004533}
4534
4535void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004536window_damage(struct window *window, int32_t x, int32_t y,
4537 int32_t width, int32_t height)
4538{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004539 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004540}
4541
Casey Dahlin9074db52012-04-19 22:50:09 -04004542static void
4543surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004544 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004545{
Rob Bradford7507b572012-05-15 17:55:34 +01004546 struct window *window = data;
4547 struct output *output;
4548 struct output *output_found = NULL;
4549 struct window_output *window_output;
4550
4551 wl_list_for_each(output, &window->display->output_list, link) {
4552 if (output->output == wl_output) {
4553 output_found = output;
4554 break;
4555 }
4556 }
4557
4558 if (!output_found)
4559 return;
4560
Brian Lovinbc919262013-08-07 15:34:59 -07004561 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004562 window_output->output = output_found;
4563
4564 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004565
4566 if (window->output_handler)
4567 window->output_handler(window, output_found, 1,
4568 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004569}
4570
4571static void
4572surface_leave(void *data,
4573 struct wl_surface *wl_surface, struct wl_output *output)
4574{
Rob Bradford7507b572012-05-15 17:55:34 +01004575 struct window *window = data;
4576 struct window_output *window_output;
4577 struct window_output *window_output_found = NULL;
4578
4579 wl_list_for_each(window_output, &window->window_output_list, link) {
4580 if (window_output->output->output == output) {
4581 window_output_found = window_output;
4582 break;
4583 }
4584 }
4585
4586 if (window_output_found) {
4587 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004588
4589 if (window->output_handler)
4590 window->output_handler(window, window_output->output,
4591 0, window->user_data);
4592
Rob Bradford7507b572012-05-15 17:55:34 +01004593 free(window_output_found);
4594 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004595}
4596
4597static const struct wl_surface_listener surface_listener = {
4598 surface_enter,
4599 surface_leave
4600};
4601
Pekka Paalanen4e373742013-02-13 16:17:13 +02004602static struct surface *
4603surface_create(struct window *window)
4604{
4605 struct display *display = window->display;
4606 struct surface *surface;
4607
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004608 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004609 surface->window = window;
4610 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004611 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004612 wl_surface_add_listener(surface->surface, &surface_listener, window);
4613
Pekka Paalanen35e82632013-04-25 13:57:48 +03004614 wl_list_insert(&window->subsurface_list, &surface->link);
4615
Pekka Paalanen4e373742013-02-13 16:17:13 +02004616 return surface;
4617}
4618
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004619static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004620get_preferred_buffer_type(struct display *display)
4621{
4622#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004623 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004624 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4625#endif
4626
4627 return WINDOW_BUFFER_TYPE_SHM;
4628}
4629
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004630static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004631window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004632{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004633 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004634 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004635
Peter Huttererf3d62272013-08-08 11:57:05 +10004636 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004637 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004638 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004639
4640 surface = surface_create(window);
4641 window->main_surface = surface;
4642
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004643 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004644
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004645 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004646 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004647
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004648 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004649
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004650 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004651 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004652 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004653
Rob Bradford7507b572012-05-15 17:55:34 +01004654 wl_list_init (&window->window_output_list);
4655
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004656 return window;
4657}
4658
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004659struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004660window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004661{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004662 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004663 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004664
4665 window = window_create_internal(display, 0);
4666
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004667 if (window->display->xdg_shell) {
4668 window->xdg_surface =
4669 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4670 window->main_surface->surface);
4671 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004672
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004673 xdg_surface_set_user_data(window->xdg_surface, window);
4674 xdg_surface_add_listener(window->xdg_surface,
4675 &xdg_surface_listener, window);
4676 } else if (display->ivi_application) {
4677 /* auto generation of ivi_id based on process id + basement of id */
4678 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4679 window->ivi_surface =
4680 ivi_application_surface_create(display->ivi_application,
4681 id_ivisurf, window->main_surface->surface);
4682 fail_on_null(window->ivi_surface);
4683
4684 ivi_surface_add_listener(window->ivi_surface,
4685 &ivi_surface_listener, window);
4686 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004687
4688 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004689}
4690
4691struct window *
4692window_create_custom(struct display *display)
4693{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004694 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004695}
4696
Jasper St. Pierre53686042013-12-09 15:26:25 -05004697void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004698window_set_parent(struct window *window,
4699 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004700{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004701 window->parent = parent_window;
4702 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004703}
4704
4705struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004706window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004707{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004708 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004709}
4710
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004711static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004712menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004713{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004714 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004715 int next;
4716
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004717 frame_interior(menu->frame, &x, &y, &width, &height);
4718 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004719 if (menu->current != next) {
4720 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004721 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004722 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004723}
4724
4725static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004726menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004727 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004728 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004729{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004730 struct menu *menu = data;
4731
4732 if (widget == menu->widget)
4733 menu_set_item(data, y);
4734
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004735 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004736}
4737
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004738static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004739menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004740 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004741{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004742 struct menu *menu = data;
4743
4744 if (widget == menu->widget)
4745 menu_set_item(data, y);
4746
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004747 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004748}
4749
4750static void
4751menu_leave_handler(struct widget *widget, struct input *input, void *data)
4752{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004753 struct menu *menu = data;
4754
4755 if (widget == menu->widget)
4756 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004757}
4758
4759static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004760menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004761 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004762 uint32_t button, enum wl_pointer_button_state state,
4763 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004764
4765{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004766 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004767
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004768 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4769 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004770 /* Either relase after press-drag-release or
4771 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004772 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004773 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004774 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004775 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004776 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004777 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004778}
4779
4780static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004781menu_touch_up_handler(struct widget *widget,
4782 struct input *input,
4783 uint32_t serial,
4784 uint32_t time,
4785 int32_t id,
4786 void *data)
4787{
4788 struct menu *menu = data;
4789
4790 input_ungrab(input);
4791 menu_destroy(menu);
4792}
4793
4794static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004795menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004796{
4797 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004798 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004799 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004800
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004801 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004802
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004803 frame_repaint(menu->frame, cr);
4804 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004805
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004806 theme_set_background_source(menu->window->display->theme,
4807 cr, THEME_FRAME_ACTIVE);
4808 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004809 cairo_fill(cr);
4810
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004811 cairo_select_font_face(cr, "sans",
4812 CAIRO_FONT_SLANT_NORMAL,
4813 CAIRO_FONT_WEIGHT_NORMAL);
4814 cairo_set_font_size(cr, 12);
4815
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004816 for (i = 0; i < menu->count; i++) {
4817 if (i == menu->current) {
4818 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004819 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004820 cairo_fill(cr);
4821 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004822 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004823 cairo_show_text(cr, menu->entries[i]);
4824 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004825 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4826 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004827 cairo_show_text(cr, menu->entries[i]);
4828 }
4829 }
4830
4831 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004832}
4833
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004834static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004835handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004836{
4837 struct window *window = data;
4838 struct menu *menu = window->main_surface->widget->user_data;
4839
4840 input_ungrab(menu->input);
4841 menu_destroy(menu);
4842}
4843
4844static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004845 handle_popup_popup_done,
4846};
4847
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004848static struct menu *
4849create_menu(struct display *display,
4850 struct input *input, uint32_t time,
4851 menu_func_t func, const char **entries, int count,
4852 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004853{
4854 struct window *window;
4855 struct menu *menu;
4856
4857 menu = malloc(sizeof *menu);
4858 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004859 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004860
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004861 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004862 if (!window) {
4863 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004864 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004865 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004866
4867 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004868 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004869 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004870 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004871 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004872 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004873 menu->entries = entries;
4874 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004875 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004876 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004877 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004878 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004879 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004880
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004881 input_ungrab(input);
4882
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004883 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004884 widget_set_enter_handler(menu->widget, menu_enter_handler);
4885 widget_set_leave_handler(menu->widget, menu_leave_handler);
4886 widget_set_motion_handler(menu->widget, menu_motion_handler);
4887 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004888 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004889
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004890 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004891 frame_resize_inside(menu->frame, 200, count * 20);
4892 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4893 window_schedule_resize(window, frame_width(menu->frame),
4894 frame_height(menu->frame));
4895
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004896 return menu;
4897}
4898
4899struct window *
4900window_create_menu(struct display *display,
4901 struct input *input, uint32_t time,
4902 menu_func_t func, const char **entries, int count,
4903 void *user_data)
4904{
4905 struct menu *menu;
4906 menu = create_menu(display, input, time, func, entries, count, user_data);
4907
4908 if (menu == NULL)
4909 return NULL;
4910
4911 return menu->window;
4912}
4913
4914void
4915window_show_menu(struct display *display,
4916 struct input *input, uint32_t time, struct window *parent,
4917 int32_t x, int32_t y,
4918 menu_func_t func, const char **entries, int count)
4919{
4920 struct menu *menu;
4921 struct window *window;
4922 int32_t ix, iy;
4923
4924 menu = create_menu(display, input, time, func, entries, count, parent);
4925
4926 if (menu == NULL)
4927 return;
4928
4929 window = menu->window;
4930
4931 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4932 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4933
4934 window->x = x;
4935 window->y = y;
4936
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004937 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004938
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004939 if (!display->xdg_shell)
4940 return;
4941
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004942 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4943 window->main_surface->surface,
4944 parent->main_surface->surface,
4945 input->seat,
4946 display_get_serial(window->display),
4947 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08004948 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004949 fail_on_null(window->xdg_popup);
4950
4951 xdg_popup_set_user_data(window->xdg_popup, window);
4952 xdg_popup_add_listener(window->xdg_popup,
4953 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004954}
4955
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004956void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004957window_set_buffer_type(struct window *window, enum window_buffer_type type)
4958{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004959 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004960}
4961
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004962enum window_buffer_type
4963window_get_buffer_type(struct window *window)
4964{
4965 return window->main_surface->buffer_type;
4966}
4967
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004968void
4969window_set_preferred_format(struct window *window,
4970 enum preferred_format format)
4971{
4972 window->preferred_format = format;
4973}
4974
Pekka Paalanen35e82632013-04-25 13:57:48 +03004975struct widget *
4976window_add_subsurface(struct window *window, void *data,
4977 enum subsurface_mode default_mode)
4978{
4979 struct widget *widget;
4980 struct surface *surface;
4981 struct wl_surface *parent;
4982 struct wl_subcompositor *subcompo = window->display->subcompositor;
4983
Pekka Paalanen35e82632013-04-25 13:57:48 +03004984 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004985 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004986 widget = widget_create(window, surface, data);
4987 wl_list_init(&widget->link);
4988 surface->widget = widget;
4989
4990 parent = window->main_surface->surface;
4991 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4992 surface->surface,
4993 parent);
4994 surface->synchronized = 1;
4995
4996 switch (default_mode) {
4997 case SUBSURFACE_SYNCHRONIZED:
4998 surface->synchronized_default = 1;
4999 break;
5000 case SUBSURFACE_DESYNCHRONIZED:
5001 surface->synchronized_default = 0;
5002 break;
5003 default:
5004 assert(!"bad enum subsurface_mode");
5005 }
5006
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005007 window->resize_needed = 1;
5008 window_schedule_redraw(window);
5009
Pekka Paalanen35e82632013-04-25 13:57:48 +03005010 return widget;
5011}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005012
5013static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005014display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005015 struct wl_output *wl_output,
5016 int x, int y,
5017 int physical_width,
5018 int physical_height,
5019 int subpixel,
5020 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005021 const char *model,
5022 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005023{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005024 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005025
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005026 output->allocation.x = x;
5027 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005028 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005029
5030 if (output->make)
5031 free(output->make);
5032 output->make = strdup(make);
5033
5034 if (output->model)
5035 free(output->model);
5036 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005037}
5038
5039static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005040display_handle_done(void *data,
5041 struct wl_output *wl_output)
5042{
5043}
5044
5045static void
5046display_handle_scale(void *data,
5047 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005048 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005049{
5050 struct output *output = data;
5051
5052 output->scale = scale;
5053}
5054
5055static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005056display_handle_mode(void *data,
5057 struct wl_output *wl_output,
5058 uint32_t flags,
5059 int width,
5060 int height,
5061 int refresh)
5062{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005063 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005064 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005065
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005066 if (flags & WL_OUTPUT_MODE_CURRENT) {
5067 output->allocation.width = width;
5068 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005069 if (display->output_configure_handler)
5070 (*display->output_configure_handler)(
5071 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005072 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005073}
5074
5075static const struct wl_output_listener output_listener = {
5076 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005077 display_handle_mode,
5078 display_handle_done,
5079 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005080};
5081
5082static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005083display_add_output(struct display *d, uint32_t id)
5084{
5085 struct output *output;
5086
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005087 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005088 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005089 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005090 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005091 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005092 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005093 wl_list_insert(d->output_list.prev, &output->link);
5094
5095 wl_output_add_listener(output->output, &output_listener, output);
5096}
5097
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005098static void
5099output_destroy(struct output *output)
5100{
5101 if (output->destroy_handler)
5102 (*output->destroy_handler)(output, output->user_data);
5103
5104 wl_output_destroy(output->output);
5105 wl_list_remove(&output->link);
5106 free(output);
5107}
5108
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005109static void
5110display_destroy_output(struct display *d, uint32_t id)
5111{
5112 struct output *output;
5113
5114 wl_list_for_each(output, &d->output_list, link) {
5115 if (output->server_output_id == id) {
5116 output_destroy(output);
5117 break;
5118 }
5119 }
5120}
5121
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005122void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005123display_set_global_handler(struct display *display,
5124 display_global_handler_t handler)
5125{
5126 struct global *global;
5127
5128 display->global_handler = handler;
5129 if (!handler)
5130 return;
5131
5132 wl_list_for_each(global, &display->global_list, link)
5133 display->global_handler(display,
5134 global->name, global->interface,
5135 global->version, display->user_data);
5136}
5137
5138void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005139display_set_global_handler_remove(struct display *display,
5140 display_global_handler_t remove_handler)
5141{
5142 display->global_handler_remove = remove_handler;
5143 if (!remove_handler)
5144 return;
5145}
5146
5147void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005148display_set_output_configure_handler(struct display *display,
5149 display_output_handler_t handler)
5150{
5151 struct output *output;
5152
5153 display->output_configure_handler = handler;
5154 if (!handler)
5155 return;
5156
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005157 wl_list_for_each(output, &display->output_list, link) {
5158 if (output->allocation.width == 0 &&
5159 output->allocation.height == 0)
5160 continue;
5161
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005162 (*display->output_configure_handler)(output,
5163 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005164 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005165}
5166
5167void
5168output_set_user_data(struct output *output, void *data)
5169{
5170 output->user_data = data;
5171}
5172
5173void *
5174output_get_user_data(struct output *output)
5175{
5176 return output->user_data;
5177}
5178
5179void
5180output_set_destroy_handler(struct output *output,
5181 display_output_handler_t handler)
5182{
5183 output->destroy_handler = handler;
5184 /* FIXME: implement this, once we have way to remove outputs */
5185}
5186
5187void
Scott Moreau4e072362012-09-29 02:03:11 -06005188output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005189{
Scott Moreau4e072362012-09-29 02:03:11 -06005190 struct rectangle allocation = output->allocation;
5191
5192 switch (output->transform) {
5193 case WL_OUTPUT_TRANSFORM_90:
5194 case WL_OUTPUT_TRANSFORM_270:
5195 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5196 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5197 /* Swap width and height */
5198 allocation.width = output->allocation.height;
5199 allocation.height = output->allocation.width;
5200 break;
5201 }
5202
5203 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005204}
5205
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005206struct wl_output *
5207output_get_wl_output(struct output *output)
5208{
5209 return output->output;
5210}
5211
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005212enum wl_output_transform
5213output_get_transform(struct output *output)
5214{
5215 return output->transform;
5216}
5217
Alexander Larssonafd319a2013-05-22 14:41:27 +02005218uint32_t
5219output_get_scale(struct output *output)
5220{
5221 return output->scale;
5222}
5223
Jason Ekstrand738715d2014-04-02 19:53:50 -05005224const char *
5225output_get_make(struct output *output)
5226{
5227 return output->make;
5228}
5229
5230const char *
5231output_get_model(struct output *output)
5232{
5233 return output->model;
5234}
5235
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005236static void
Daniel Stone97f68542012-05-30 16:32:01 +01005237fini_xkb(struct input *input)
5238{
5239 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005240 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005241}
5242
5243static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005244display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005245{
5246 struct input *input;
5247
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005248 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005249 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005250 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005251 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005252 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005253 input->pointer_focus = NULL;
5254 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005255 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005256 wl_list_insert(d->input_list.prev, &input->link);
5257
Daniel Stone37816df2012-05-16 18:45:18 +01005258 wl_seat_add_listener(input->seat, &seat_listener, input);
5259 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005260
Jason Ekstranda669bd52014-04-02 19:53:51 -05005261 if (d->data_device_manager) {
5262 input->data_device =
5263 wl_data_device_manager_get_data_device(d->data_device_manager,
5264 input->seat);
5265 wl_data_device_add_listener(input->data_device,
5266 &data_device_listener,
5267 input);
5268 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005269
5270 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005271 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005272
Derek Foreman118a4292015-04-22 17:23:35 -05005273 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5274 TFD_CLOEXEC | TFD_NONBLOCK);
5275 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5276 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005277 set_repeat_info(input, 40, 400);
5278
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005279 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5280 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005281 input->repeat_task.run = keyboard_repeat_func;
5282 display_watch_fd(d, input->repeat_timer_fd,
5283 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005284}
5285
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005286static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005287input_destroy(struct input *input)
5288{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005289 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005290 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005291
5292 if (input->drag_offer)
5293 data_offer_destroy(input->drag_offer);
5294
5295 if (input->selection_offer)
5296 data_offer_destroy(input->selection_offer);
5297
kabeer khan6ce67ec2014-10-20 11:55:29 +05305298 if (input->data_device) {
5299 if(input->display->data_device_manager_version >= 2)
5300 wl_data_device_release(input->data_device);
5301 else
5302 wl_data_device_destroy(input->data_device);
5303 }
Rob Bradford08031182013-08-13 20:11:03 +01005304 if (input->display->seat_version >= 3) {
5305 if (input->pointer)
5306 wl_pointer_release(input->pointer);
5307 if (input->keyboard)
5308 wl_keyboard_release(input->keyboard);
5309 }
5310
Daniel Stone97f68542012-05-30 16:32:01 +01005311 fini_xkb(input);
5312
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005313 wl_surface_destroy(input->pointer_surface);
5314
Pekka Paalanene1207c72011-12-16 12:02:09 +02005315 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005316 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005317 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005318 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005319 free(input);
5320}
5321
5322static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005323init_workspace_manager(struct display *d, uint32_t id)
5324{
5325 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005326 wl_registry_bind(d->registry, id,
5327 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005328 if (d->workspace_manager != NULL)
5329 workspace_manager_add_listener(d->workspace_manager,
5330 &workspace_manager_listener,
5331 d);
5332}
5333
5334static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005335shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5336{
5337 struct display *d = data;
5338
5339 if (format == WL_SHM_FORMAT_RGB565)
5340 d->has_rgb565 = 1;
5341}
5342
5343struct wl_shm_listener shm_listener = {
5344 shm_format
5345};
5346
5347static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005348xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5349{
5350 xdg_shell_pong(shell, serial);
5351}
5352
5353static const struct xdg_shell_listener xdg_shell_listener = {
5354 xdg_shell_ping,
5355};
5356
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005357#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005358#ifdef static_assert
5359static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5360 "Interface version doesn't match implementation version");
5361#endif
5362
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005363static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005364registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5365 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005366{
5367 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005368 struct global *global;
5369
Brian Lovinbc919262013-08-07 15:34:59 -07005370 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005371 global->name = id;
5372 global->interface = strdup(interface);
5373 global->version = version;
5374 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005375
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005376 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005377 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005378 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005379 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005380 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005381 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005382 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005383 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005384 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005385 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005386 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005387 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
kabeer khan6ce67ec2014-10-20 11:55:29 +05305388 d->data_device_manager_version = MIN(version, 2);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005389 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005390 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305391 &wl_data_device_manager_interface,
5392 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005393 } else if (strcmp(interface, "xdg_shell") == 0) {
5394 d->xdg_shell = wl_registry_bind(registry, id,
5395 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005396 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005397 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005398 } else if (strcmp(interface, "text_cursor_position") == 0) {
5399 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005400 wl_registry_bind(registry, id,
5401 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005402 } else if (strcmp(interface, "workspace_manager") == 0) {
5403 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005404 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5405 d->subcompositor =
5406 wl_registry_bind(registry, id,
5407 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005408 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005409 else if (strcmp(interface, "ivi_application") == 0) {
5410 d->ivi_application =
5411 wl_registry_bind(registry, id,
5412 &ivi_application_interface, 1);
5413 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005414
5415 if (d->global_handler)
5416 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005417}
5418
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005419static void
5420registry_handle_global_remove(void *data, struct wl_registry *registry,
5421 uint32_t name)
5422{
5423 struct display *d = data;
5424 struct global *global;
5425 struct global *tmp;
5426
5427 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5428 if (global->name != name)
5429 continue;
5430
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005431 if (strcmp(global->interface, "wl_output") == 0)
5432 display_destroy_output(d, name);
5433
5434 /* XXX: Should destroy remaining bound globals */
5435
5436 if (d->global_handler_remove)
5437 d->global_handler_remove(d, name, global->interface,
5438 global->version, d->user_data);
5439
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005440 wl_list_remove(&global->link);
5441 free(global->interface);
5442 free(global);
5443 }
5444}
5445
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005446void *
5447display_bind(struct display *display, uint32_t name,
5448 const struct wl_interface *interface, uint32_t version)
5449{
5450 return wl_registry_bind(display->registry, name, interface, version);
5451}
5452
5453static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005454 registry_handle_global,
5455 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005456};
5457
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005458#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005459static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005460init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005461{
5462 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005463 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005464
Rob Clark6396ed32012-03-11 19:48:41 -05005465#ifdef USE_CAIRO_GLESV2
5466# define GL_BIT EGL_OPENGL_ES2_BIT
5467#else
5468# define GL_BIT EGL_OPENGL_BIT
5469#endif
5470
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005471 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005472 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005473 EGL_RED_SIZE, 1,
5474 EGL_GREEN_SIZE, 1,
5475 EGL_BLUE_SIZE, 1,
5476 EGL_ALPHA_SIZE, 1,
5477 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005478 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005479 EGL_NONE
5480 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005481
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005482#ifdef USE_CAIRO_GLESV2
5483 static const EGLint context_attribs[] = {
5484 EGL_CONTEXT_CLIENT_VERSION, 2,
5485 EGL_NONE
5486 };
5487 EGLint api = EGL_OPENGL_ES_API;
5488#else
5489 EGLint *context_attribs = NULL;
5490 EGLint api = EGL_OPENGL_API;
5491#endif
5492
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005493 d->dpy =
5494 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5495 d->display, NULL);
5496
Yuval Fledel45568f62010-12-06 09:18:12 -05005497 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005498 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005499 return -1;
5500 }
5501
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005502 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005503 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005504 return -1;
5505 }
5506
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005507 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5508 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005509 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005510 return -1;
5511 }
5512
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005513 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005514 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005515 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005516 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005517 return -1;
5518 }
5519
Benjamin Franzke0c991632011-09-27 21:57:31 +02005520 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5521 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005522 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005523 return -1;
5524 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005525
5526 return 0;
5527}
5528
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005529static void
5530fini_egl(struct display *display)
5531{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005532 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005533
5534 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5535 EGL_NO_CONTEXT);
5536
5537 eglTerminate(display->dpy);
5538 eglReleaseThread();
5539}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005540#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005541
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005542static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005543init_dummy_surface(struct display *display)
5544{
5545 int len;
5546 void *data;
5547
5548 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005549 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005550 display->dummy_surface =
5551 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5552 1, 1, len);
5553 display->dummy_surface_data = data;
5554}
5555
5556static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005557handle_display_data(struct task *task, uint32_t events)
5558{
5559 struct display *display =
5560 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005561 struct epoll_event ep;
5562 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005563
5564 display->display_fd_events = events;
5565
5566 if (events & EPOLLERR || events & EPOLLHUP) {
5567 display_exit(display);
5568 return;
5569 }
5570
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005571 if (events & EPOLLIN) {
5572 ret = wl_display_dispatch(display->display);
5573 if (ret == -1) {
5574 display_exit(display);
5575 return;
5576 }
5577 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005578
5579 if (events & EPOLLOUT) {
5580 ret = wl_display_flush(display->display);
5581 if (ret == 0) {
5582 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5583 ep.data.ptr = &display->display_task;
5584 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5585 display->display_fd, &ep);
5586 } else if (ret == -1 && errno != EAGAIN) {
5587 display_exit(display);
5588 return;
5589 }
5590 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005591}
5592
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005593static void
5594log_handler(const char *format, va_list args)
5595{
5596 vfprintf(stderr, format, args);
5597}
5598
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005599struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005600display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005601{
5602 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005603
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005604 wl_log_set_handler_client(log_handler);
5605
Peter Huttererf3d62272013-08-08 11:57:05 +10005606 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005607 if (d == NULL)
5608 return NULL;
5609
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005610 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005611 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005612 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005613 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005614 return NULL;
5615 }
5616
Rob Bradford5ab9c752013-07-26 16:29:43 +01005617 d->xkb_context = xkb_context_new(0);
5618 if (d->xkb_context == NULL) {
5619 fprintf(stderr, "Failed to create XKB context\n");
5620 free(d);
5621 return NULL;
5622 }
5623
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005624 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005625 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005626 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005627 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5628 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005629
5630 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005631 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005632 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005633 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005634
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005635 d->workspace = 0;
5636 d->workspace_count = 1;
5637
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005638 d->registry = wl_display_get_registry(d->display);
5639 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005640
Marek Chalupaa519d062014-12-05 13:49:40 +01005641 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005642 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5643 return NULL;
5644 }
5645
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005646#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005647 if (init_egl(d) < 0)
5648 fprintf(stderr, "EGL does not seem to work, "
5649 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005650#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005651
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005652 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005653
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005654 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005655
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005656 wl_list_init(&d->window_list);
5657
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005658 init_dummy_surface(d);
5659
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005660 return d;
5661}
5662
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005663static void
5664display_destroy_outputs(struct display *display)
5665{
5666 struct output *tmp;
5667 struct output *output;
5668
5669 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5670 output_destroy(output);
5671}
5672
Pekka Paalanene1207c72011-12-16 12:02:09 +02005673static void
5674display_destroy_inputs(struct display *display)
5675{
5676 struct input *tmp;
5677 struct input *input;
5678
5679 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5680 input_destroy(input);
5681}
5682
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005683void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005684display_destroy(struct display *display)
5685{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005686 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005687 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5688 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005689
5690 if (!wl_list_empty(&display->deferred_list))
5691 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5692
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005693 cairo_surface_destroy(display->dummy_surface);
5694 free(display->dummy_surface_data);
5695
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005696 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005697 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005698
Daniel Stone97f68542012-05-30 16:32:01 +01005699 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005700
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005701 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005702 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005703
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005704#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005705 if (display->argb_device)
5706 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005707#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005708
Pekka Paalanen35e82632013-04-25 13:57:48 +03005709 if (display->subcompositor)
5710 wl_subcompositor_destroy(display->subcompositor);
5711
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005712 if (display->xdg_shell)
5713 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005714
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005715 if (display->ivi_application)
5716 ivi_application_destroy(display->ivi_application);
5717
Pekka Paalanenc2052982011-12-16 11:41:32 +02005718 if (display->shm)
5719 wl_shm_destroy(display->shm);
5720
5721 if (display->data_device_manager)
5722 wl_data_device_manager_destroy(display->data_device_manager);
5723
5724 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005725 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005726
5727 close(display->epoll_fd);
5728
U. Artie Eoff44874d92012-10-02 21:12:35 -07005729 if (!(display->display_fd_events & EPOLLERR) &&
5730 !(display->display_fd_events & EPOLLHUP))
5731 wl_display_flush(display->display);
5732
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005733 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005734 free(display);
5735}
5736
5737void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005738display_set_user_data(struct display *display, void *data)
5739{
5740 display->user_data = data;
5741}
5742
5743void *
5744display_get_user_data(struct display *display)
5745{
5746 return display->user_data;
5747}
5748
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005749struct wl_display *
5750display_get_display(struct display *display)
5751{
5752 return display->display;
5753}
5754
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005755int
5756display_has_subcompositor(struct display *display)
5757{
5758 if (display->subcompositor)
5759 return 1;
5760
5761 wl_display_roundtrip(display->display);
5762
5763 return display->subcompositor != NULL;
5764}
5765
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005766cairo_device_t *
5767display_get_cairo_device(struct display *display)
5768{
5769 return display->argb_device;
5770}
5771
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005772struct output *
5773display_get_output(struct display *display)
5774{
5775 return container_of(display->output_list.next, struct output, link);
5776}
5777
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005778struct wl_compositor *
5779display_get_compositor(struct display *display)
5780{
5781 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005782}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005783
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005784uint32_t
5785display_get_serial(struct display *display)
5786{
5787 return display->serial;
5788}
5789
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005790EGLDisplay
5791display_get_egl_display(struct display *d)
5792{
5793 return d->dpy;
5794}
5795
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005796struct wl_data_source *
5797display_create_data_source(struct display *display)
5798{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005799 if (display->data_device_manager)
5800 return wl_data_device_manager_create_data_source(display->data_device_manager);
5801 else
5802 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005803}
5804
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005805EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005806display_get_argb_egl_config(struct display *d)
5807{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005808 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005809}
5810
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005811int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005812display_acquire_window_surface(struct display *display,
5813 struct window *window,
5814 EGLContext ctx)
5815{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005816 struct surface *surface = window->main_surface;
5817
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005818 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005819 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005820
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005821 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005822 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005823}
5824
5825void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005826display_release_window_surface(struct display *display,
5827 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005828{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005829 struct surface *surface = window->main_surface;
5830
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005831 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005832 return;
5833
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005834 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005835}
5836
5837void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005838display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005839{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005840 wl_list_insert(&display->deferred_list, &task->link);
5841}
5842
5843void
5844display_watch_fd(struct display *display,
5845 int fd, uint32_t events, struct task *task)
5846{
5847 struct epoll_event ep;
5848
5849 ep.events = events;
5850 ep.data.ptr = task;
5851 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5852}
5853
5854void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005855display_unwatch_fd(struct display *display, int fd)
5856{
5857 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5858}
5859
5860void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005861display_run(struct display *display)
5862{
5863 struct task *task;
5864 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005865 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005866
Pekka Paalanen826d7952011-12-15 10:14:07 +02005867 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005868 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005869 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005870 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005871 struct task, link);
5872 wl_list_remove(&task->link);
5873 task->run(task, 0);
5874 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005875
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005876 wl_display_dispatch_pending(display->display);
5877
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005878 if (!display->running)
5879 break;
5880
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005881 ret = wl_display_flush(display->display);
5882 if (ret < 0 && errno == EAGAIN) {
5883 ep[0].events =
5884 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5885 ep[0].data.ptr = &display->display_task;
5886
5887 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5888 display->display_fd, &ep[0]);
5889 } else if (ret < 0) {
5890 break;
5891 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005892
5893 count = epoll_wait(display->epoll_fd,
5894 ep, ARRAY_LENGTH(ep), -1);
5895 for (i = 0; i < count; i++) {
5896 task = ep[i].data.ptr;
5897 task->run(task, ep[i].events);
5898 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005899 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005900}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005901
5902void
5903display_exit(struct display *display)
5904{
5905 display->running = 0;
5906}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005907
5908void
5909keysym_modifiers_add(struct wl_array *modifiers_map,
5910 const char *name)
5911{
5912 size_t len = strlen(name) + 1;
5913 char *p;
5914
5915 p = wl_array_add(modifiers_map, len);
5916
5917 if (p == NULL)
5918 return;
5919
5920 strncpy(p, name, len);
5921}
5922
5923static xkb_mod_index_t
5924keysym_modifiers_get_index(struct wl_array *modifiers_map,
5925 const char *name)
5926{
5927 xkb_mod_index_t index = 0;
5928 char *p = modifiers_map->data;
5929
5930 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5931 if (strcmp(p, name) == 0)
5932 return index;
5933
5934 index++;
5935 p += strlen(p) + 1;
5936 }
5937
5938 return XKB_MOD_INVALID;
5939}
5940
5941xkb_mod_mask_t
5942keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5943 const char *name)
5944{
5945 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5946
5947 if (index == XKB_MOD_INVALID)
5948 return XKB_MOD_INVALID;
5949
5950 return 1 << index;
5951}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005952
5953void *
5954fail_on_null(void *p)
5955{
5956 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005957 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005958 exit(EXIT_FAILURE);
5959 }
5960
5961 return p;
5962}
5963
5964void *
5965xmalloc(size_t s)
5966{
5967 return fail_on_null(malloc(s));
5968}
5969
Peter Huttererf3d62272013-08-08 11:57:05 +10005970void *
5971xzalloc(size_t s)
5972{
5973 return fail_on_null(zalloc(s));
5974}
5975
Kristian Høgsbergce278412013-07-25 15:20:20 -07005976char *
5977xstrdup(const char *s)
5978{
5979 return fail_on_null(strdup(s));
5980}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005981
5982void *
5983xrealloc(char *p, size_t s)
5984{
5985 return fail_on_null(realloc(p, s));
5986}