blob: d75bc60d1b0c19e0af18b65389d6ebddac3ae8aa [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
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080027#include <stdbool.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030031#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040035#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020037#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050038#include <time.h>
39#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040040#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040041#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030042#include <sys/timerfd.h>
Derek Foreman493d9792015-03-04 16:26:25 -060043#include <stdbool.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050046#include <wayland-egl.h>
47
Rob Clark6396ed32012-03-11 19:48:41 -050048#ifdef USE_CAIRO_GLESV2
49#include <GLES2/gl2.h>
50#include <GLES2/gl2ext.h>
51#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050053#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <EGL/egl.h>
55#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040057#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030058#else /* HAVE_CAIRO_EGL */
59typedef void *EGLDisplay;
60typedef void *EGLConfig;
61typedef void *EGLContext;
62#define EGL_NO_DISPLAY ((EGLDisplay)0)
63#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050064
Daniel Stone9d4f0302012-02-15 16:33:21 +000065#include <xkbcommon/xkbcommon.h>
Daniel Stone5b015962016-10-20 14:45:58 +010066#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -070067#include <xkbcommon/xkbcommon-compose.h>
Daniel Stone5b015962016-10-20 14:45:58 +010068#endif
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030069#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040070
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050071#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020072#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070073#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070074#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070075#include "shared/xalloc.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080076#include "shared/zalloc.h"
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +080077#include "xdg-shell-unstable-v6-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060078#include "text-cursor-position-client-protocol.h"
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080079#include "pointer-constraints-unstable-v1-client-protocol.h"
80#include "relative-pointer-unstable-v1-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070081#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050082
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050083#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050084
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090085#include <sys/types.h>
86#include "ivi-application-client-protocol.h"
87#define IVI_SURFACE_ID 9000
88
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080089#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
90#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
91
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070092struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030093
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040094struct global {
95 uint32_t name;
96 char *interface;
97 uint32_t version;
98 struct wl_list link;
99};
100
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500101struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500102 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400103 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500104 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300105 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400106 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400107 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600108 struct text_cursor_position *text_cursor_position;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800109 struct zxdg_shell_v6 *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900110 struct ivi_application *ivi_application; /* ivi style shell */
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800111 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
112 struct zwp_pointer_constraints_v1 *pointer_constraints;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400113 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500114 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200115 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200116 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400117 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400118
119 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700120 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400121 struct task display_task;
122
123 int epoll_fd;
124 struct wl_list deferred_list;
125
Pekka Paalanen826d7952011-12-15 10:14:07 +0200126 int running;
127
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400128 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400129 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400130 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500131 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400132
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400133 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400134
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300135 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300136 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400137
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200138 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400139 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800140 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200141
142 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100143
144 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200145
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200146 /* A hack to get text extents for tooltips */
147 cairo_surface_t *dummy_surface;
148 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200149
150 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530151 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500152};
153
Rob Bradford7507b572012-05-15 17:55:34 +0100154struct window_output {
155 struct output *output;
156 struct wl_list link;
157};
158
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200159struct toysurface {
160 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500161 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800162 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200163 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200164 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200165 * If flags has SURFACE_HINT_RESIZE set, the user is
166 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200167 * Returns the Cairo surface to draw to.
168 */
169 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200170 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200171 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200172
173 /*
174 * Post the surface to the server, returning the server allocation
175 * rectangle. The Cairo surface from prepare() must be destroyed
176 * after calling this.
177 */
178 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200179 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200180 struct rectangle *server_allocation);
181
182 /*
183 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800184 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200185 */
186 int (*acquire)(struct toysurface *base, EGLContext ctx);
187
188 /*
189 * Release the toysurface from the EGL context, returning control
190 * to Cairo.
191 */
192 void (*release)(struct toysurface *base);
193
194 /*
195 * Destroy the toysurface, including the Cairo surface, any
196 * backing storage, and the Wayland protocol objects.
197 */
198 void (*destroy)(struct toysurface *base);
199};
200
Pekka Paalanen4e373742013-02-13 16:17:13 +0200201struct surface {
202 struct window *window;
203
204 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300205 struct wl_subsurface *subsurface;
206 int synchronized;
207 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200208 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200209 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300210 int redraw_needed;
211 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300212 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200213
214 struct rectangle allocation;
215 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200216
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200217 struct wl_region *input_region;
218 struct wl_region *opaque_region;
219
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200220 enum window_buffer_type buffer_type;
221 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200222 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200223
224 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300225
226 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200227};
228
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500229struct window {
230 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100231 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500232 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200233 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400234 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500235 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200236 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500237 int x, y;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800238 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400239 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300240 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400241 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400242 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500243 int custom;
244 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400245
Pekka Paalanen99436862012-11-19 17:15:59 +0200246 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400247
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500248 int fullscreen;
249 int maximized;
250
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200251 enum preferred_format preferred_format;
252
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500253 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500254 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400255 window_data_handler_t data_handler;
256 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500257 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400258 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200259 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700260 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400261
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800262 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
263
Pekka Paalanen4e373742013-02-13 16:17:13 +0200264 struct surface *main_surface;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800265 struct zxdg_surface_v6 *xdg_surface;
266 struct zxdg_toplevel_v6 *xdg_toplevel;
267 struct zxdg_popup_v6 *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300268
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700269 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800270 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500271
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900272 struct ivi_surface *ivi_surface;
273
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500274 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500275
Pekka Paalanen35e82632013-04-25 13:57:48 +0300276 /* struct surface::link, contains also main_surface */
277 struct wl_list subsurface_list;
278
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800279 struct zwp_relative_pointer_v1 *relative_pointer;
280 struct zwp_locked_pointer_v1 *locked_pointer;
281 struct input *locked_input;
282 bool pointer_locked;
283 locked_pointer_locked_handler_t pointer_locked_handler;
284 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
285 confined_pointer_confined_handler_t pointer_confined_handler;
286 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
287
288 struct zwp_confined_pointer_v1 *confined_pointer;
289 struct widget *confined_widget;
290 bool confined;
291
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500292 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400293 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500294};
295
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500296struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500297 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200298 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300299 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500300 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400301 struct wl_list link;
302 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500303 widget_resize_handler_t resize_handler;
304 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500305 widget_enter_handler_t enter_handler;
306 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500307 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500308 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700309 widget_touch_down_handler_t touch_down_handler;
310 widget_touch_up_handler_t touch_up_handler;
311 widget_touch_motion_handler_t touch_motion_handler;
312 widget_touch_frame_handler_t touch_frame_handler;
313 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200314 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000315 widget_pointer_frame_handler_t pointer_frame_handler;
316 widget_axis_source_handler_t axis_source_handler;
317 widget_axis_stop_handler_t axis_stop_handler;
318 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400319 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500320 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300321 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500322 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000323 /* If this is set to false then no cairo surface will be
324 * created before redrawing the surface. This is useful if the
325 * redraw handler is going to do completely custom rendering
326 * such as using EGL directly */
327 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400328};
329
Rusty Lynch041815a2013-08-08 21:20:38 -0700330struct touch_point {
331 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800332 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700333 struct widget *widget;
334 struct wl_list link;
335};
336
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400337struct input {
338 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100339 struct wl_seat *seat;
340 struct wl_pointer *pointer;
341 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700342 struct wl_touch *touch;
343 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400344 struct window *pointer_focus;
345 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700346 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300347 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300348 uint32_t cursor_anim_start;
349 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500350 uint32_t cursor_timer_start;
351 uint32_t cursor_anim_current;
352 int cursor_delay_fd;
353 bool cursor_timer_running;
354 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300355 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400356 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400357 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400358 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400359 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400360 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400361
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500362 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500363 struct widget *grab;
364 uint32_t grab_button;
365
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400366 struct wl_data_device *data_device;
367 struct data_offer *drag_offer;
368 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800369 uint32_t touch_grab;
370 int32_t touch_grab_id;
371 float drag_x, drag_y;
372 struct window *drag_focus;
373 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100374
375 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100376 struct xkb_keymap *keymap;
377 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +0100378#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700379 struct xkb_compose_table *compose_table;
380 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +0100381#endif
Daniel Stone97f68542012-05-30 16:32:01 +0100382 xkb_mod_mask_t control_mask;
383 xkb_mod_mask_t alt_mask;
384 xkb_mod_mask_t shift_mask;
385 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400386
Jonny Lamb06959082014-08-12 14:58:27 +0200387 int32_t repeat_rate_sec;
388 int32_t repeat_rate_nsec;
389 int32_t repeat_delay_sec;
390 int32_t repeat_delay_nsec;
391
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400392 struct task repeat_task;
393 int repeat_timer_fd;
394 uint32_t repeat_sym;
395 uint32_t repeat_key;
396 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500397 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400398};
399
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500400struct output {
401 struct display *display;
402 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800403 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500404 struct rectangle allocation;
405 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600406 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200407 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500408 char *make;
409 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200410
411 display_output_handler_t destroy_handler;
412 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500413};
414
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500415struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500416 struct widget *widget;
417 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500418 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800419
420 uint32_t last_time;
421 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800422 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500423};
424
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500425struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400426 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500427 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500428 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500429 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700430 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500431 const char **entries;
432 uint32_t time;
433 int current;
434 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400435 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500436 menu_func_t func;
437};
438
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300439struct tooltip {
440 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300441 struct widget *widget;
442 char *entry;
443 struct task tooltip_task;
444 int tooltip_fd;
445 float x, y;
446};
447
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700448struct shm_pool {
449 struct wl_shm_pool *pool;
450 size_t size;
451 size_t used;
452 void *data;
453};
454
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400455enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300456 CURSOR_DEFAULT = 100,
457 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400458};
459
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200460static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461
Pekka Paalanen97777442013-05-22 10:20:05 +0300462/* #define DEBUG */
463
464#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300465
466static void
467debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
468__attribute__ ((format (printf, 4, 5)));
469
470static void
471debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
472{
473 va_list ap;
474 struct timeval tv;
475
476 gettimeofday(&tv, NULL);
477 fprintf(stderr, "%8ld.%03ld ",
478 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
479
480 if (proxy)
481 fprintf(stderr, "%s@%d ",
482 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
483
484 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
485 fprintf(stderr, "%s ", func);
486
487 va_start(ap, fmt);
488 vfprintf(stderr, fmt, ap);
489 va_end(ap);
490}
491
492#define DBG(fmt, ...) \
493 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
494
495#define DBG_OBJ(obj, fmt, ...) \
496 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
497
498#else
499
500#define DBG(...) do {} while (0)
501#define DBG_OBJ(...) do {} while (0)
502
503#endif
504
Alexander Larsson1818e312013-05-22 14:41:31 +0200505static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200506surface_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 +0200507{
508 int32_t tmp;
509
510 switch (buffer_transform) {
511 case WL_OUTPUT_TRANSFORM_90:
512 case WL_OUTPUT_TRANSFORM_270:
513 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
514 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
515 tmp = *width;
516 *width = *height;
517 *height = tmp;
518 break;
519 default:
520 break;
521 }
522
523 *width *= buffer_scale;
524 *height *= buffer_scale;
525}
526
527static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200528buffer_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 +0200529{
530 int32_t tmp;
531
532 switch (buffer_transform) {
533 case WL_OUTPUT_TRANSFORM_90:
534 case WL_OUTPUT_TRANSFORM_270:
535 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
536 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
537 tmp = *width;
538 *width = *height;
539 *height = tmp;
540 break;
541 default:
542 break;
543 }
544
545 *width /= buffer_scale;
546 *height /= buffer_scale;
547}
548
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500549#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551struct egl_window_surface {
552 struct toysurface base;
553 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100554 struct display *display;
555 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556 struct wl_egl_window *egl_window;
557 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558};
559
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200560static struct egl_window_surface *
561to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100562{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100564}
565
566static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200567egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200568 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200569 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100570{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 struct egl_window_surface *surface = to_egl_window_surface(base);
572
Alexander Larsson1818e312013-05-22 14:41:31 +0200573 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
574
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200575 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
576 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
577
578 return cairo_surface_reference(surface->cairo_surface);
579}
580
581static void
582egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200583 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200584 struct rectangle *server_allocation)
585{
586 struct egl_window_surface *surface = to_egl_window_surface(base);
587
588 cairo_gl_surface_swapbuffers(surface->cairo_surface);
589 wl_egl_window_get_attached_size(surface->egl_window,
590 &server_allocation->width,
591 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200592
593 buffer_to_surface_size (buffer_transform, buffer_scale,
594 &server_allocation->width,
595 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200596}
597
598static int
599egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
600{
601 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200602 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100603
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200604 device = cairo_surface_get_device(surface->cairo_surface);
605 if (!device)
606 return -1;
607
608 if (!ctx) {
609 if (device == surface->display->argb_device)
610 ctx = surface->display->argb_ctx;
611 else
612 assert(0);
613 }
614
615 cairo_device_flush(device);
616 cairo_device_acquire(device);
617 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
618 surface->egl_surface, ctx))
619 fprintf(stderr, "failed to make surface current\n");
620
621 return 0;
622}
623
624static void
625egl_window_surface_release(struct toysurface *base)
626{
627 struct egl_window_surface *surface = to_egl_window_surface(base);
628 cairo_device_t *device;
629
630 device = cairo_surface_get_device(surface->cairo_surface);
631 if (!device)
632 return;
633
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200634 if (!eglMakeCurrent(surface->display->dpy,
635 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200636 fprintf(stderr, "failed to make context current\n");
637
638 cairo_device_release(device);
639}
640
641static void
642egl_window_surface_destroy(struct toysurface *base)
643{
644 struct egl_window_surface *surface = to_egl_window_surface(base);
645 struct display *d = surface->display;
646
647 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000648 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200649 wl_egl_window_destroy(surface->egl_window);
650 surface->surface = NULL;
651
652 free(surface);
653}
654
655static struct toysurface *
656egl_window_surface_create(struct display *display,
657 struct wl_surface *wl_surface,
658 uint32_t flags,
659 struct rectangle *rectangle)
660{
661 struct egl_window_surface *surface;
662
Pekka Paalanenb3627362012-11-19 17:16:00 +0200663 if (display->dpy == EGL_NO_DISPLAY)
664 return NULL;
665
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800666 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200667 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100668 return NULL;
669
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200670 surface->base.prepare = egl_window_surface_prepare;
671 surface->base.swap = egl_window_surface_swap;
672 surface->base.acquire = egl_window_surface_acquire;
673 surface->base.release = egl_window_surface_release;
674 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100675
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200676 surface->display = display;
677 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400678
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200679 surface->egl_window = wl_egl_window_create(surface->surface,
680 rectangle->width,
681 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100682
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100683 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100684 weston_platform_create_egl_surface(display->dpy,
685 display->argb_config,
686 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100687
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200688 surface->cairo_surface =
689 cairo_gl_surface_create_for_egl(display->argb_device,
690 surface->egl_surface,
691 rectangle->width,
692 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100693
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200694 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100695}
696
Pekka Paalanenb3627362012-11-19 17:16:00 +0200697#else
698
699static struct toysurface *
700egl_window_surface_create(struct display *display,
701 struct wl_surface *wl_surface,
702 uint32_t flags,
703 struct rectangle *rectangle)
704{
705 return NULL;
706}
707
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400708#endif
709
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200710struct shm_surface_data {
711 struct wl_buffer *buffer;
712 struct shm_pool *pool;
713};
714
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400715struct wl_buffer *
716display_get_buffer_for_surface(struct display *display,
717 cairo_surface_t *surface)
718{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200719 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400720
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200721 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400722
723 return data->buffer;
724}
725
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500726static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700727shm_pool_destroy(struct shm_pool *pool);
728
729static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400730shm_surface_data_destroy(void *p)
731{
732 struct shm_surface_data *data = p;
733
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200734 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700735 if (data->pool)
736 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300737
738 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400739}
740
Kristian Høgsberg16626282012-04-03 11:21:27 -0400741static struct wl_shm_pool *
742make_shm_pool(struct display *display, int size, void **data)
743{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400744 struct wl_shm_pool *pool;
745 int fd;
746
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300747 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400748 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300749 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
750 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400751 return NULL;
752 }
753
754 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400755 if (*data == MAP_FAILED) {
756 fprintf(stderr, "mmap failed: %m\n");
757 close(fd);
758 return NULL;
759 }
760
761 pool = wl_shm_create_pool(display->shm, fd, size);
762
763 close(fd);
764
765 return pool;
766}
767
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700768static struct shm_pool *
769shm_pool_create(struct display *display, size_t size)
770{
771 struct shm_pool *pool = malloc(sizeof *pool);
772
773 if (!pool)
774 return NULL;
775
776 pool->pool = make_shm_pool(display, size, &pool->data);
777 if (!pool->pool) {
778 free(pool);
779 return NULL;
780 }
781
782 pool->size = size;
783 pool->used = 0;
784
785 return pool;
786}
787
788static void *
789shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
790{
791 if (pool->used + size > pool->size)
792 return NULL;
793
794 *offset = pool->used;
795 pool->used += size;
796
797 return (char *) pool->data + *offset;
798}
799
800/* destroy the pool. this does not unmap the memory though */
801static void
802shm_pool_destroy(struct shm_pool *pool)
803{
804 munmap(pool->data, pool->size);
805 wl_shm_pool_destroy(pool->pool);
806 free(pool);
807}
808
809/* Start allocating from the beginning of the pool again */
810static void
811shm_pool_reset(struct shm_pool *pool)
812{
813 pool->used = 0;
814}
815
816static int
817data_length_for_shm_surface(struct rectangle *rect)
818{
819 int stride;
820
821 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
822 rect->width);
823 return stride * rect->height;
824}
825
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500826static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700827display_create_shm_surface_from_pool(struct display *display,
828 struct rectangle *rectangle,
829 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830{
831 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400832 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400833 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200834 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700835 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400836 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837
838 data = malloc(sizeof *data);
839 if (data == NULL)
840 return NULL;
841
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200842 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
843 cairo_format = CAIRO_FORMAT_RGB16_565;
844 else
845 cairo_format = CAIRO_FORMAT_ARGB32;
846
847 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700848 length = stride * rectangle->height;
849 data->pool = NULL;
850 map = shm_pool_allocate(pool, length, &offset);
851
852 if (!map) {
853 free(data);
854 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400855 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400857 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200858 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400859 rectangle->width,
860 rectangle->height,
861 stride);
862
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200863 cairo_surface_set_user_data(surface, &shm_surface_data_key,
864 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200866 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
867 format = WL_SHM_FORMAT_RGB565;
868 else {
869 if (flags & SURFACE_OPAQUE)
870 format = WL_SHM_FORMAT_XRGB8888;
871 else
872 format = WL_SHM_FORMAT_ARGB8888;
873 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400874
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200875 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
876 rectangle->width,
877 rectangle->height,
878 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700880 return surface;
881}
882
883static cairo_surface_t *
884display_create_shm_surface(struct display *display,
885 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200886 struct shm_pool *alternate_pool,
887 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700888{
889 struct shm_surface_data *data;
890 struct shm_pool *pool;
891 cairo_surface_t *surface;
892
Pekka Paalanen99436862012-11-19 17:15:59 +0200893 if (alternate_pool) {
894 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700895 surface = display_create_shm_surface_from_pool(display,
896 rectangle,
897 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200898 alternate_pool);
899 if (surface) {
900 data = cairo_surface_get_user_data(surface,
901 &shm_surface_data_key);
902 goto out;
903 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700904 }
905
906 pool = shm_pool_create(display,
907 data_length_for_shm_surface(rectangle));
908 if (!pool)
909 return NULL;
910
911 surface =
912 display_create_shm_surface_from_pool(display, rectangle,
913 flags, pool);
914
915 if (!surface) {
916 shm_pool_destroy(pool);
917 return NULL;
918 }
919
920 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200921 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700922 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400923
Pekka Paalanen99436862012-11-19 17:15:59 +0200924out:
925 if (data_ret)
926 *data_ret = data;
927
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928 return surface;
929}
930
nobled7b87cb02011-02-01 18:51:47 +0000931static int
932check_size(struct rectangle *rect)
933{
934 if (rect->width && rect->height)
935 return 0;
936
937 fprintf(stderr, "tried to create surface of "
938 "width: %d, height: %d\n", rect->width, rect->height);
939 return -1;
940}
941
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400942cairo_surface_t *
943display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100944 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400945 struct rectangle *rectangle,
946 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947{
nobled7b87cb02011-02-01 18:51:47 +0000948 if (check_size(rectangle) < 0)
949 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200950
951 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200952 return display_create_shm_surface(display, rectangle, flags,
953 NULL, NULL);
954}
955
Pekka Paalanena4eda732012-11-19 17:16:02 +0200956struct shm_surface_leaf {
957 cairo_surface_t *cairo_surface;
958 /* 'data' is automatically destroyed, when 'cairo_surface' is */
959 struct shm_surface_data *data;
960
961 struct shm_pool *resize_pool;
962 int busy;
963};
964
965static void
966shm_surface_leaf_release(struct shm_surface_leaf *leaf)
967{
968 if (leaf->cairo_surface)
969 cairo_surface_destroy(leaf->cairo_surface);
970 /* leaf->data already destroyed via cairo private */
971
972 if (leaf->resize_pool)
973 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200974
975 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200976}
977
Pekka Paalanenaef02542013-04-25 13:57:47 +0300978#define MAX_LEAVES 3
979
Pekka Paalanen99436862012-11-19 17:15:59 +0200980struct shm_surface {
981 struct toysurface base;
982 struct display *display;
983 struct wl_surface *surface;
984 uint32_t flags;
985 int dx, dy;
986
Pekka Paalanenaef02542013-04-25 13:57:47 +0300987 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200988 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200989};
990
991static struct shm_surface *
992to_shm_surface(struct toysurface *base)
993{
994 return container_of(base, struct shm_surface, base);
995}
996
Pekka Paalanena4eda732012-11-19 17:16:02 +0200997static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300998shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
999{
1000#ifdef DEBUG
1001 struct shm_surface_leaf *leaf;
1002 char bufs[MAX_LEAVES + 1];
1003 int i;
1004
1005 for (i = 0; i < MAX_LEAVES; i++) {
1006 leaf = &surface->leaf[i];
1007
1008 if (leaf->busy)
1009 bufs[i] = 'b';
1010 else if (leaf->cairo_surface)
1011 bufs[i] = 'a';
1012 else
1013 bufs[i] = ' ';
1014 }
1015
1016 bufs[MAX_LEAVES] = '\0';
1017 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1018#endif
1019}
1020
1021static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001022shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1023{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001024 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 struct shm_surface_leaf *leaf;
1026 int i;
1027 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001028
1029 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001030
Pekka Paalanenaef02542013-04-25 13:57:47 +03001031 for (i = 0; i < MAX_LEAVES; i++) {
1032 leaf = &surface->leaf[i];
1033 if (leaf->data && leaf->data->buffer == buffer) {
1034 leaf->busy = 0;
1035 break;
1036 }
1037 }
1038 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001039
Pekka Paalanenaef02542013-04-25 13:57:47 +03001040 /* Leave one free leaf with storage, release others */
1041 free_found = 0;
1042 for (i = 0; i < MAX_LEAVES; i++) {
1043 leaf = &surface->leaf[i];
1044
1045 if (!leaf->cairo_surface || leaf->busy)
1046 continue;
1047
1048 if (!free_found)
1049 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001050 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001051 shm_surface_leaf_release(leaf);
1052 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001053
Pekka Paalanen97777442013-05-22 10:20:05 +03001054 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055}
1056
1057static const struct wl_buffer_listener shm_surface_buffer_listener = {
1058 shm_surface_buffer_release
1059};
1060
Pekka Paalanen99436862012-11-19 17:15:59 +02001061static cairo_surface_t *
1062shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001063 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001064 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001065{
Pekka Paalanenec076692012-11-30 13:37:27 +02001066 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001067 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001068 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001069 struct shm_surface_leaf *leaf = NULL;
1070 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001071
1072 surface->dx = dx;
1073 surface->dy = dy;
1074
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001075 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001076 for (i = 0; i < MAX_LEAVES; i++) {
1077 if (surface->leaf[i].busy)
1078 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001079
Pekka Paalanenaef02542013-04-25 13:57:47 +03001080 if (!leaf || surface->leaf[i].cairo_surface)
1081 leaf = &surface->leaf[i];
1082 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001083 DBG_OBJ(surface->surface, "pick leaf %d\n",
1084 (int)(leaf - &surface->leaf[0]));
1085
Pekka Paalanenaef02542013-04-25 13:57:47 +03001086 if (!leaf) {
1087 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001089 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001090 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001091 }
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 if (!resize_hint && leaf->resize_pool) {
1094 cairo_surface_destroy(leaf->cairo_surface);
1095 leaf->cairo_surface = NULL;
1096 shm_pool_destroy(leaf->resize_pool);
1097 leaf->resize_pool = NULL;
1098 }
1099
Alexander Larsson1818e312013-05-22 14:41:31 +02001100 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1101
Pekka Paalanena4eda732012-11-19 17:16:02 +02001102 if (leaf->cairo_surface &&
1103 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1104 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001105 goto out;
1106
Pekka Paalanena4eda732012-11-19 17:16:02 +02001107 if (leaf->cairo_surface)
1108 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001109
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001110#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001112 /* Create a big pool to allocate from, while continuously
1113 * resizing. Mmapping a new pool in the server
1114 * is relatively expensive, so reusing a pool performs
1115 * better, but may temporarily reserve unneeded memory.
1116 */
1117 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001118 leaf->resize_pool = shm_pool_create(surface->display,
1119 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001120 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001121#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001122
Alexander Larsson1818e312013-05-22 14:41:31 +02001123 rect.width = width;
1124 rect.height = height;
1125
Pekka Paalanena4eda732012-11-19 17:16:02 +02001126 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001127 display_create_shm_surface(surface->display, &rect,
1128 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001129 leaf->resize_pool,
1130 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001131 if (!leaf->cairo_surface)
1132 return NULL;
1133
Pekka Paalanena4eda732012-11-19 17:16:02 +02001134 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001135 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001136
1137out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001138 surface->current = leaf;
1139
1140 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001141}
1142
1143static void
1144shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001145 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001146 struct rectangle *server_allocation)
1147{
1148 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001149 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001150
1151 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001152 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001153 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001154 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001155
Alexander Larsson1818e312013-05-22 14:41:31 +02001156 buffer_to_surface_size (buffer_transform, buffer_scale,
1157 &server_allocation->width,
1158 &server_allocation->height);
1159
Pekka Paalanena4eda732012-11-19 17:16:02 +02001160 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001161 surface->dx, surface->dy);
1162 wl_surface_damage(surface->surface, 0, 0,
1163 server_allocation->width, server_allocation->height);
1164 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001165
Pekka Paalanen71233882013-04-25 13:57:53 +03001166 DBG_OBJ(surface->surface, "leaf %d busy\n",
1167 (int)(leaf - &surface->leaf[0]));
1168
Pekka Paalanena4eda732012-11-19 17:16:02 +02001169 leaf->busy = 1;
1170 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001171}
1172
1173static int
1174shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1175{
1176 return -1;
1177}
1178
1179static void
1180shm_surface_release(struct toysurface *base)
1181{
1182}
1183
1184static void
1185shm_surface_destroy(struct toysurface *base)
1186{
1187 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001188 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001189
Pekka Paalanenaef02542013-04-25 13:57:47 +03001190 for (i = 0; i < MAX_LEAVES; i++)
1191 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001192
1193 free(surface);
1194}
1195
1196static struct toysurface *
1197shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1198 uint32_t flags, struct rectangle *rectangle)
1199{
1200 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001201 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001202
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001203 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001204 surface->base.prepare = shm_surface_prepare;
1205 surface->base.swap = shm_surface_swap;
1206 surface->base.acquire = shm_surface_acquire;
1207 surface->base.release = shm_surface_release;
1208 surface->base.destroy = shm_surface_destroy;
1209
1210 surface->display = display;
1211 surface->surface = wl_surface;
1212 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001213
1214 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001215}
1216
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001217/*
1218 * The following correspondences between file names and cursors was copied
1219 * from: https://bugs.kde.org/attachment.cgi?id=67313
1220 */
1221
1222static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001223 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001224 "sw-resize",
1225 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001226};
1227
1228static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001229 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001230 "se-resize",
1231 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001232};
1233
1234static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001235 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001236 "s-resize",
1237 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001238};
1239
1240static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001241 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 "closedhand",
1243 "208530c400c041818281048008011002"
1244};
1245
1246static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001247 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248 "default",
1249 "top_left_arrow",
1250 "left-arrow"
1251};
1252
1253static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001254 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001255 "w-resize",
1256 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257};
1258
1259static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001260 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001261 "e-resize",
1262 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001263};
1264
1265static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001266 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001267 "nw-resize",
1268 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001269};
1270
1271static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001272 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001273 "ne-resize",
1274 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001275};
1276
1277static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001278 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001279 "n-resize",
1280 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001281};
1282
1283static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001284 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001285 "ibeam",
1286 "text"
1287};
1288
1289static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001290 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001291 "pointer",
1292 "pointing_hand",
1293 "e29285e634086352946a0e7090d73106"
1294};
1295
1296static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001297 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001298 "wait",
1299 "0426c94ea35c87780ff01dc239897213"
1300};
1301
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001302static const char *move_draggings[] = {
1303 "dnd-move"
1304};
1305
1306static const char *copy_draggings[] = {
1307 "dnd-copy"
1308};
1309
1310static const char *forbidden_draggings[] = {
1311 "dnd-none",
1312 "dnd-no-drop"
1313};
1314
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001315struct cursor_alternatives {
1316 const char **names;
1317 size_t count;
1318};
1319
1320static const struct cursor_alternatives cursors[] = {
1321 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1322 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1323 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1324 {grabbings, ARRAY_LENGTH(grabbings)},
1325 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1326 {left_sides, ARRAY_LENGTH(left_sides)},
1327 {right_sides, ARRAY_LENGTH(right_sides)},
1328 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1329 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1330 {top_sides, ARRAY_LENGTH(top_sides)},
1331 {xterms, ARRAY_LENGTH(xterms)},
1332 {hand1s, ARRAY_LENGTH(hand1s)},
1333 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001334 {move_draggings, ARRAY_LENGTH(move_draggings)},
1335 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1336 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001337};
1338
1339static void
1340create_cursors(struct display *display)
1341{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001342 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001343 struct weston_config *config;
1344 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001345 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001346 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001347 unsigned int i, j;
1348 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001349
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001350 config_file = weston_config_get_name_from_env();
1351 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001352 s = weston_config_get_section(config, "shell", NULL, NULL);
1353 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1354 weston_config_section_get_int(s, "cursor-size", &size, 32);
1355 weston_config_destroy(config);
1356
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001357 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001358 if (!display->cursor_theme) {
1359 fprintf(stderr, "could not load theme '%s'\n", theme);
1360 return;
1361 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001362 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001363 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001364 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001365
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001366 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001367 cursor = NULL;
1368 for (j = 0; !cursor && j < cursors[i].count; ++j)
1369 cursor = wl_cursor_theme_get_cursor(
1370 display->cursor_theme, cursors[i].names[j]);
1371
1372 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001373 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001374 cursors[i].names[0]);
1375
1376 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001377 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001378}
1379
1380static void
1381destroy_cursors(struct display *display)
1382{
1383 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001384 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001385}
1386
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001387struct wl_cursor_image *
1388display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001389{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001390 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001391
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001392 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001393}
1394
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001395static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001396surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001397{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001398 if (!surface->cairo_surface)
1399 return;
1400
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001401 if (surface->opaque_region) {
1402 wl_surface_set_opaque_region(surface->surface,
1403 surface->opaque_region);
1404 wl_region_destroy(surface->opaque_region);
1405 surface->opaque_region = NULL;
1406 }
1407
1408 if (surface->input_region) {
1409 wl_surface_set_input_region(surface->surface,
1410 surface->input_region);
1411 wl_region_destroy(surface->input_region);
1412 surface->input_region = NULL;
1413 }
1414
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001415 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001416 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418
Pekka Paalanen89dee002013-02-13 16:17:20 +02001419 cairo_surface_destroy(surface->cairo_surface);
1420 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001421}
1422
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001423int
1424window_has_focus(struct window *window)
1425{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001426 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001427}
1428
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001429static void
1430window_close(struct window *window)
1431{
1432 if (window->close_handler)
1433 window->close_handler(window->user_data);
1434 else
1435 display_exit(window->display);
1436}
1437
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001438struct display *
1439window_get_display(struct window *window)
1440{
1441 return window->display;
1442}
1443
Pekka Paalanen89dee002013-02-13 16:17:20 +02001444static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001445handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1446 int32_t width, int32_t height)
1447{
1448 struct window *window = data;
1449
1450 window_schedule_resize(window, width, height);
1451}
1452
1453static const struct ivi_surface_listener ivi_surface_listener = {
1454 handle_ivi_surface_configure,
1455};
1456
1457static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001458surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001459{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001460 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001461 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001462
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001463 if (!surface->toysurface && display->dpy &&
1464 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001465 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001466 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001467 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001468 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001469 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001470 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001471
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001472 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001473 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001474 surface->surface,
1475 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001476
Pekka Paalanen89dee002013-02-13 16:17:20 +02001477 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001478 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001479 allocation.width, allocation.height, flags,
1480 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001481}
1482
1483static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001484window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001485{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001486 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001487 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001488
Pekka Paalanenec076692012-11-30 13:37:27 +02001489 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001490 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001491
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001492 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1493 flags |= SURFACE_HINT_RGB565;
1494
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001495 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001496}
1497
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001498int
1499window_get_buffer_transform(struct window *window)
1500{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001501 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001502}
1503
1504void
1505window_set_buffer_transform(struct window *window,
1506 enum wl_output_transform transform)
1507{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001508 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001509 wl_surface_set_buffer_transform(window->main_surface->surface,
1510 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001511}
1512
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001513void
1514window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001515 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001516{
1517 window->main_surface->buffer_scale = scale;
1518 wl_surface_set_buffer_scale(window->main_surface->surface,
1519 scale);
1520}
1521
1522uint32_t
1523window_get_buffer_scale(struct window *window)
1524{
1525 return window->main_surface->buffer_scale;
1526}
1527
Alexander Larssond68f5232013-05-22 14:41:33 +02001528uint32_t
1529window_get_output_scale(struct window *window)
1530{
1531 struct window_output *window_output;
1532 struct window_output *window_output_tmp;
1533 int scale = 1;
1534
1535 wl_list_for_each_safe(window_output, window_output_tmp,
1536 &window->window_output_list, link) {
1537 if (window_output->output->scale > scale)
1538 scale = window_output->output->scale;
1539 }
1540
1541 return scale;
1542}
1543
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001544static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001545
Pekka Paalanen4e373742013-02-13 16:17:13 +02001546static void
1547surface_destroy(struct surface *surface)
1548{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001549 if (surface->frame_cb)
1550 wl_callback_destroy(surface->frame_cb);
1551
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001552 if (surface->input_region)
1553 wl_region_destroy(surface->input_region);
1554
1555 if (surface->opaque_region)
1556 wl_region_destroy(surface->opaque_region);
1557
Pekka Paalanen35e82632013-04-25 13:57:48 +03001558 if (surface->subsurface)
1559 wl_subsurface_destroy(surface->subsurface);
1560
Pekka Paalanen4e373742013-02-13 16:17:13 +02001561 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001562
1563 if (surface->toysurface)
1564 surface->toysurface->destroy(surface->toysurface);
1565
Pekka Paalanen35e82632013-04-25 13:57:48 +03001566 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001567 free(surface);
1568}
1569
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001570void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001571window_destroy(struct window *window)
1572{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001573 struct display *display = window->display;
1574 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001575 struct window_output *window_output;
1576 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001577
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001578 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001579
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001580 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001581 if (input->touch_focus == window)
1582 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001583 if (input->pointer_focus == window)
1584 input->pointer_focus = NULL;
1585 if (input->keyboard_focus == window)
1586 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001587 if (input->focus_widget &&
1588 input->focus_widget->window == window)
1589 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001590 }
1591
Rob Bradford7507b572012-05-15 17:55:34 +01001592 wl_list_for_each_safe(window_output, window_output_tmp,
1593 &window->window_output_list, link) {
1594 free (window_output);
1595 }
1596
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001597 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001598 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001599
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001600 if (window->xdg_toplevel)
1601 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001602 if (window->xdg_popup)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001603 zxdg_popup_v6_destroy(window->xdg_popup);
1604 if (window->xdg_surface)
1605 zxdg_surface_v6_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001606
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001607 if (window->ivi_surface)
1608 ivi_surface_destroy(window->ivi_surface);
1609
Pekka Paalanen4e373742013-02-13 16:17:13 +02001610 surface_destroy(window->main_surface);
1611
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001612 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001613
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001614 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001615 free(window);
1616}
1617
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001618static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001620{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001621 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001623 wl_list_for_each(child, &widget->child_list, link) {
1624 target = widget_find_widget(child, x, y);
1625 if (target)
1626 return target;
1627 }
1628
1629 if (widget->allocation.x <= x &&
1630 x < widget->allocation.x + widget->allocation.width &&
1631 widget->allocation.y <= y &&
1632 y < widget->allocation.y + widget->allocation.height) {
1633 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001634 }
1635
1636 return NULL;
1637}
1638
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001639static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001640window_find_widget(struct window *window, int32_t x, int32_t y)
1641{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001642 struct surface *surface;
1643 struct widget *widget;
1644
1645 wl_list_for_each(surface, &window->subsurface_list, link) {
1646 widget = widget_find_widget(surface->widget, x, y);
1647 if (widget)
1648 return widget;
1649 }
1650
1651 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001652}
1653
1654static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001655widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001656{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001657 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001658
Peter Huttererf3d62272013-08-08 11:57:05 +10001659 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001660 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001661 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001662 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001663 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001664 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001665 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001666 widget->tooltip = NULL;
1667 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001668 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001669 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001670
1671 return widget;
1672}
1673
1674struct widget *
1675window_add_widget(struct window *window, void *data)
1676{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001677 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001678
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001679 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001680 wl_list_init(&widget->link);
1681 window->main_surface->widget = widget;
1682
1683 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001684}
1685
1686struct widget *
1687widget_add_widget(struct widget *parent, void *data)
1688{
1689 struct widget *widget;
1690
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001691 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001692 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001693
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001694 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695}
1696
1697void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001698widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001699{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001700 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001701 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001702 struct input *input;
1703
Pekka Paalanen35e82632013-04-25 13:57:48 +03001704 /* Destroy the sub-surface along with the root widget */
1705 if (surface->widget == widget && surface->subsurface)
1706 surface_destroy(widget->surface);
1707
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001708 if (widget->tooltip)
1709 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001710
Pekka Paalanene156fb62012-01-19 13:51:38 +02001711 wl_list_for_each(input, &display->input_list, link) {
1712 if (input->focus_widget == widget)
1713 input->focus_widget = NULL;
1714 }
1715
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001716 wl_list_remove(&widget->link);
1717 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001718}
1719
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001720void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001721widget_set_default_cursor(struct widget *widget, int cursor)
1722{
1723 widget->default_cursor = cursor;
1724}
1725
1726void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001727widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001728{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001729 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001730}
1731
1732void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001733widget_set_size(struct widget *widget, int32_t width, int32_t height)
1734{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001735 widget->allocation.width = width;
1736 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001737}
1738
1739void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001740widget_set_allocation(struct widget *widget,
1741 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001742{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001743 widget->allocation.x = x;
1744 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001745 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001746}
1747
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001748void
1749widget_set_transparent(struct widget *widget, int transparent)
1750{
1751 widget->opaque = !transparent;
1752}
1753
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001754void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001755widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001756{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001757 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001758}
1759
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001760static cairo_surface_t *
1761widget_get_cairo_surface(struct widget *widget)
1762{
1763 struct surface *surface = widget->surface;
1764 struct window *window = widget->window;
1765
Neil Roberts97b747c2013-12-19 16:17:12 +00001766 assert(widget->use_cairo);
1767
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001768 if (!surface->cairo_surface) {
1769 if (surface == window->main_surface)
1770 window_create_main_surface(window);
1771 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001772 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001773 }
1774
1775 return surface->cairo_surface;
1776}
1777
Alexander Larsson15901f02013-05-22 14:41:25 +02001778static void
1779widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1780{
1781 struct surface *surface = widget->surface;
1782 double angle;
1783 cairo_matrix_t m;
1784 enum wl_output_transform transform;
1785 int surface_width, surface_height;
1786 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001787 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001788
1789 surface_width = surface->allocation.width;
1790 surface_height = surface->allocation.height;
1791
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001792 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001793 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001794
Alexander Larsson15901f02013-05-22 14:41:25 +02001795 switch (transform) {
1796 case WL_OUTPUT_TRANSFORM_FLIPPED:
1797 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1798 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1799 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1800 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1801 break;
1802 default:
1803 cairo_matrix_init_identity(&m);
1804 break;
1805 }
1806
1807 switch (transform) {
1808 case WL_OUTPUT_TRANSFORM_NORMAL:
1809 default:
1810 angle = 0;
1811 translate_x = 0;
1812 translate_y = 0;
1813 break;
1814 case WL_OUTPUT_TRANSFORM_FLIPPED:
1815 angle = 0;
1816 translate_x = surface_width;
1817 translate_y = 0;
1818 break;
1819 case WL_OUTPUT_TRANSFORM_90:
1820 angle = M_PI_2;
1821 translate_x = surface_height;
1822 translate_y = 0;
1823 break;
1824 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1825 angle = M_PI_2;
1826 translate_x = surface_height;
1827 translate_y = surface_width;
1828 break;
1829 case WL_OUTPUT_TRANSFORM_180:
1830 angle = M_PI;
1831 translate_x = surface_width;
1832 translate_y = surface_height;
1833 break;
1834 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1835 angle = M_PI;
1836 translate_x = 0;
1837 translate_y = surface_height;
1838 break;
1839 case WL_OUTPUT_TRANSFORM_270:
1840 angle = M_PI + M_PI_2;
1841 translate_x = 0;
1842 translate_y = surface_width;
1843 break;
1844 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1845 angle = M_PI + M_PI_2;
1846 translate_x = 0;
1847 translate_y = 0;
1848 break;
1849 }
1850
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001851 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001852 cairo_translate(cr, translate_x, translate_y);
1853 cairo_rotate(cr, angle);
1854 cairo_transform(cr, &m);
1855}
1856
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001857cairo_t *
1858widget_cairo_create(struct widget *widget)
1859{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001860 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001861 cairo_surface_t *cairo_surface;
1862 cairo_t *cr;
1863
1864 cairo_surface = widget_get_cairo_surface(widget);
1865 cr = cairo_create(cairo_surface);
1866
Alexander Larsson15901f02013-05-22 14:41:25 +02001867 widget_cairo_update_transform(widget, cr);
1868
Pekka Paalanen35e82632013-04-25 13:57:48 +03001869 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1870
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001871 return cr;
1872}
1873
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001874struct wl_surface *
1875widget_get_wl_surface(struct widget *widget)
1876{
1877 return widget->surface->surface;
1878}
1879
Neil Roberts5e10a042013-09-09 00:40:17 +01001880struct wl_subsurface *
1881widget_get_wl_subsurface(struct widget *widget)
1882{
1883 return widget->surface->subsurface;
1884}
1885
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001886uint32_t
1887widget_get_last_time(struct widget *widget)
1888{
1889 return widget->surface->last_time;
1890}
1891
1892void
1893widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1894{
1895 struct wl_compositor *comp = widget->window->display->compositor;
1896 struct surface *surface = widget->surface;
1897
1898 if (!surface->input_region)
1899 surface->input_region = wl_compositor_create_region(comp);
1900
1901 if (rect) {
1902 wl_region_add(surface->input_region,
1903 rect->x, rect->y, rect->width, rect->height);
1904 }
1905}
1906
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001907void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001908widget_set_resize_handler(struct widget *widget,
1909 widget_resize_handler_t handler)
1910{
1911 widget->resize_handler = handler;
1912}
1913
1914void
1915widget_set_redraw_handler(struct widget *widget,
1916 widget_redraw_handler_t handler)
1917{
1918 widget->redraw_handler = handler;
1919}
1920
1921void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001922widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001923{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001924 widget->enter_handler = handler;
1925}
1926
1927void
1928widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1929{
1930 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001931}
1932
1933void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001934widget_set_motion_handler(struct widget *widget,
1935 widget_motion_handler_t handler)
1936{
1937 widget->motion_handler = handler;
1938}
1939
1940void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001941widget_set_button_handler(struct widget *widget,
1942 widget_button_handler_t handler)
1943{
1944 widget->button_handler = handler;
1945}
1946
1947void
Rusty Lynch041815a2013-08-08 21:20:38 -07001948widget_set_touch_up_handler(struct widget *widget,
1949 widget_touch_up_handler_t handler)
1950{
1951 widget->touch_up_handler = handler;
1952}
1953
1954void
1955widget_set_touch_down_handler(struct widget *widget,
1956 widget_touch_down_handler_t handler)
1957{
1958 widget->touch_down_handler = handler;
1959}
1960
1961void
1962widget_set_touch_motion_handler(struct widget *widget,
1963 widget_touch_motion_handler_t handler)
1964{
1965 widget->touch_motion_handler = handler;
1966}
1967
1968void
1969widget_set_touch_frame_handler(struct widget *widget,
1970 widget_touch_frame_handler_t handler)
1971{
1972 widget->touch_frame_handler = handler;
1973}
1974
1975void
1976widget_set_touch_cancel_handler(struct widget *widget,
1977 widget_touch_cancel_handler_t handler)
1978{
1979 widget->touch_cancel_handler = handler;
1980}
1981
1982void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001983widget_set_axis_handler(struct widget *widget,
1984 widget_axis_handler_t handler)
1985{
1986 widget->axis_handler = handler;
1987}
1988
Peter Hutterer87743e92016-01-18 16:38:22 +10001989void
1990widget_set_pointer_frame_handler(struct widget *widget,
1991 widget_pointer_frame_handler_t handler)
1992{
1993 widget->pointer_frame_handler = handler;
1994}
1995
1996void
1997widget_set_axis_handlers(struct widget *widget,
1998 widget_axis_handler_t axis_handler,
1999 widget_axis_source_handler_t axis_source_handler,
2000 widget_axis_stop_handler_t axis_stop_handler,
2001 widget_axis_discrete_handler_t axis_discrete_handler)
2002{
2003 widget->axis_handler = axis_handler;
2004 widget->axis_source_handler = axis_source_handler;
2005 widget->axis_stop_handler = axis_stop_handler;
2006 widget->axis_discrete_handler = axis_discrete_handler;
2007}
2008
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002009static void
2010window_schedule_redraw_task(struct window *window);
2011
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002012void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002013widget_schedule_redraw(struct widget *widget)
2014{
Pekka Paalanen71233882013-04-25 13:57:53 +03002015 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002016 widget->surface->redraw_needed = 1;
2017 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002018}
2019
Neil Roberts97b747c2013-12-19 16:17:12 +00002020void
2021widget_set_use_cairo(struct widget *widget,
2022 int use_cairo)
2023{
2024 widget->use_cairo = use_cairo;
2025}
2026
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002027cairo_surface_t *
2028window_get_surface(struct window *window)
2029{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002030 cairo_surface_t *cairo_surface;
2031
2032 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2033
2034 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002035}
2036
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002037struct wl_surface *
2038window_get_wl_surface(struct window *window)
2039{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002040 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002041}
2042
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002043static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002044tooltip_redraw_handler(struct widget *widget, void *data)
2045{
2046 cairo_t *cr;
2047 const int32_t r = 3;
2048 struct tooltip *tooltip = data;
2049 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002050
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002051 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002052 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002053 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2054 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2055 cairo_paint(cr);
2056
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002057 width = widget->allocation.width;
2058 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002059 rounded_rect(cr, 0, 0, width, height, r);
2060
2061 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2062 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2063 cairo_fill(cr);
2064
2065 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2066 cairo_move_to(cr, 10, 16);
2067 cairo_show_text(cr, tooltip->entry);
2068 cairo_destroy(cr);
2069}
2070
2071static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002072get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002073{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002074 cairo_t *cr;
2075 cairo_text_extents_t extents;
2076
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002077 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002078 * created yet, and parent does not have a valid surface
2079 * outside repaint, either.
2080 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002081 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002082 cairo_text_extents(cr, tooltip->entry, &extents);
2083 cairo_destroy(cr);
2084
2085 return extents;
2086}
2087
2088static int
2089window_create_tooltip(struct tooltip *tooltip)
2090{
2091 struct widget *parent = tooltip->parent;
2092 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002093 const int offset_y = 27;
2094 const int margin = 3;
2095 cairo_text_extents_t extents;
2096
2097 if (tooltip->widget)
2098 return 0;
2099
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002100 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002101
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002102 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002103 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002104 widget_set_allocation(tooltip->widget,
2105 tooltip->x, tooltip->y + offset_y,
2106 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002107
2108 return 0;
2109}
2110
2111void
2112widget_destroy_tooltip(struct widget *parent)
2113{
2114 struct tooltip *tooltip = parent->tooltip;
2115
2116 parent->tooltip_count = 0;
2117 if (!tooltip)
2118 return;
2119
2120 if (tooltip->widget) {
2121 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002122 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002123 }
2124
2125 close(tooltip->tooltip_fd);
2126 free(tooltip->entry);
2127 free(tooltip);
2128 parent->tooltip = NULL;
2129}
2130
2131static void
2132tooltip_func(struct task *task, uint32_t events)
2133{
2134 struct tooltip *tooltip =
2135 container_of(task, struct tooltip, tooltip_task);
2136 uint64_t exp;
2137
Martin Olsson8df662a2012-07-08 03:03:47 +02002138 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2139 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002140 window_create_tooltip(tooltip);
2141}
2142
2143#define TOOLTIP_TIMEOUT 500
2144static int
2145tooltip_timer_reset(struct tooltip *tooltip)
2146{
2147 struct itimerspec its;
2148
2149 its.it_interval.tv_sec = 0;
2150 its.it_interval.tv_nsec = 0;
2151 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2152 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2153 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2154 fprintf(stderr, "could not set timerfd\n: %m");
2155 return -1;
2156 }
2157
2158 return 0;
2159}
2160
2161int
2162widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2163{
2164 struct tooltip *tooltip = parent->tooltip;
2165
2166 parent->tooltip_count++;
2167 if (tooltip) {
2168 tooltip->x = x;
2169 tooltip->y = y;
2170 tooltip_timer_reset(tooltip);
2171 return 0;
2172 }
2173
2174 /* the handler might be triggered too fast via input device motion, so
2175 * we need this check here to make sure tooltip is fully initialized */
2176 if (parent->tooltip_count > 1)
2177 return 0;
2178
2179 tooltip = malloc(sizeof *tooltip);
2180 if (!tooltip)
2181 return -1;
2182
2183 parent->tooltip = tooltip;
2184 tooltip->parent = parent;
2185 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002186 tooltip->x = x;
2187 tooltip->y = y;
2188 tooltip->entry = strdup(entry);
2189 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2190 if (tooltip->tooltip_fd < 0) {
2191 fprintf(stderr, "could not create timerfd\n: %m");
2192 return -1;
2193 }
2194
2195 tooltip->tooltip_task.run = tooltip_func;
2196 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2197 EPOLLIN, &tooltip->tooltip_task);
2198 tooltip_timer_reset(tooltip);
2199
2200 return 0;
2201}
2202
2203static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002204frame_resize_handler(struct widget *widget,
2205 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002206{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002207 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002208 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002209 struct rectangle interior;
2210 struct rectangle input;
2211 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002213 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002214 interior.x = 0;
2215 interior.y = 0;
2216 interior.width = width;
2217 interior.height = height;
2218 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002219 frame_resize(frame->frame, width, height);
2220 frame_interior(frame->frame, &interior.x, &interior.y,
2221 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002222 }
2223
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002224 widget_set_allocation(child, interior.x, interior.y,
2225 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002227 if (child->resize_handler) {
2228 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002229 child->user_data);
2230
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002231 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002232 width = child->allocation.width;
2233 height = child->allocation.height;
2234 } else {
2235 frame_resize_inside(frame->frame,
2236 child->allocation.width,
2237 child->allocation.height);
2238 width = frame_width(frame->frame);
2239 height = frame_height(frame->frame);
2240 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002241 }
2242
Scott Moreauf7e498c2012-05-14 11:39:29 -06002243 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002244
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002245 widget->surface->input_region =
2246 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002247 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002248 frame_input_rect(frame->frame, &input.x, &input.y,
2249 &input.width, &input.height);
2250 wl_region_add(widget->surface->input_region,
2251 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002252 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002253 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002254 }
2255
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002256 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002257
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002258 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002259 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002260 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2261 &opaque.width, &opaque.height);
2262
2263 wl_region_add(widget->surface->opaque_region,
2264 opaque.x, opaque.y,
2265 opaque.width, opaque.height);
2266 } else {
2267 wl_region_add(widget->surface->opaque_region,
2268 0, 0, width, height);
2269 }
Martin Minarik1998b152012-05-10 02:04:35 +02002270 }
2271
Martin Minarik1998b152012-05-10 02:04:35 +02002272
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002273 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002274}
2275
2276static void
2277frame_redraw_handler(struct widget *widget, void *data)
2278{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002279 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002280 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002281 struct window *window = widget->window;
2282
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002283 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002284 return;
2285
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002286 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002287
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002288 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002289
2290 cairo_destroy(cr);
2291}
2292
2293static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002294frame_get_pointer_image_for_location(struct window_frame *frame,
2295 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002296{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002297 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002298
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002299 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002300 return CURSOR_LEFT_PTR;
2301
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002302 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002303 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002304 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002305 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002306 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002307 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002308 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002309 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002310 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002311 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002312 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002313 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002314 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002315 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002316 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002317 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002318 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002319 case THEME_LOCATION_EXTERIOR:
2320 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002321 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002322 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002323 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002324}
2325
Pekka Paalanen26237862014-09-10 15:10:30 +03002326static void
2327frame_menu_func(void *data, struct input *input, int index)
2328{
2329 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002330
2331 switch (index) {
2332 case 0: /* close */
2333 window_close(window);
2334 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002335 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002336 /* we don't have a way to get out of fullscreen for now */
2337 if (window->fullscreen_handler)
2338 window->fullscreen_handler(window, window->user_data);
2339 break;
2340 }
2341}
2342
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002343void
2344window_show_frame_menu(struct window *window,
2345 struct input *input, uint32_t time)
2346{
2347 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002348 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002349
Pekka Paalanen26237862014-09-10 15:10:30 +03002350 static const char *entries[] = {
2351 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002352 "Fullscreen"
2353 };
2354
2355 if (window->fullscreen_handler)
2356 count = ARRAY_LENGTH(entries);
2357 else
2358 count = ARRAY_LENGTH(entries) - 1;
2359
2360 input_get_position(input, &x, &y);
2361 window_show_menu(window->display, input, time, window,
2362 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002363}
2364
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002365static int
2366frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002367 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002368{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002369 struct window_frame *frame = data;
2370 enum theme_location location;
2371
2372 location = frame_pointer_enter(frame->frame, input, x, y);
2373 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2374 widget_schedule_redraw(frame->widget);
2375
2376 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002377}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002378
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002379static int
2380frame_motion_handler(struct widget *widget,
2381 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002382 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002383{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002384 struct window_frame *frame = data;
2385 enum theme_location location;
2386
2387 location = frame_pointer_motion(frame->frame, input, x, y);
2388 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2389 widget_schedule_redraw(frame->widget);
2390
2391 return frame_get_pointer_image_for_location(data, location);
2392}
2393
2394static void
2395frame_leave_handler(struct widget *widget,
2396 struct input *input, void *data)
2397{
2398 struct window_frame *frame = data;
2399
2400 frame_pointer_leave(frame->frame, input);
2401 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2402 widget_schedule_redraw(frame->widget);
2403}
2404
2405static void
2406frame_handle_status(struct window_frame *frame, struct input *input,
2407 uint32_t time, enum theme_location location)
2408{
2409 struct window *window = frame->widget->window;
2410 uint32_t status;
2411
2412 status = frame_status(frame->frame);
2413 if (status & FRAME_STATUS_REPAINT)
2414 widget_schedule_redraw(frame->widget);
2415
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002416 if (status & FRAME_STATUS_MINIMIZE) {
2417 window_set_minimized(window);
2418 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2419 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002420
2421 if (status & FRAME_STATUS_MENU) {
2422 window_show_frame_menu(window, input, time);
2423 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2424 }
2425
2426 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002427 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002428 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2429 }
2430
2431 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002432 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002433 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002434 }
2435
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002436 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002437 input_ungrab(input);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002438 zxdg_toplevel_v6_move(window->xdg_toplevel,
2439 input_get_seat(input),
2440 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002441
2442 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2443 }
2444
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002445 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002446 input_ungrab(input);
2447
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002448 zxdg_toplevel_v6_resize(window->xdg_toplevel,
2449 input_get_seat(input),
2450 window->display->serial,
2451 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002452
2453 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2454 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002455}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002456
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002457#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002458static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002459frame_button_handler(struct widget *widget,
2460 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002461 uint32_t button, enum wl_pointer_button_state state,
2462 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002463
2464{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002465 struct window_frame *frame = data;
2466 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002467
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002468 frame->double_click = 0;
2469 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2470 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2471 frame->double_click = 1;
2472 frame->did_double = 1;
2473 } else
2474 frame->did_double = 0;
2475
2476 frame->last_time = time;
2477 } else if (frame->did_double == 1) {
2478 frame->double_click = 1;
2479 frame->did_double = 0;
2480 }
2481
2482 if (frame->double_click)
2483 location = frame_double_click(frame->frame, input,
2484 button, state);
2485 else
2486 location = frame_pointer_button(frame->frame, input,
2487 button, state);
2488
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002489 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002490}
2491
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002492static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002493frame_touch_down_handler(struct widget *widget, struct input *input,
2494 uint32_t serial, uint32_t time, int32_t id,
2495 float x, float y, void *data)
2496{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002497 struct window_frame *frame = data;
2498
Xiong Zhang382de462014-06-12 11:06:26 +08002499 frame->double_click = 0;
2500 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2501 frame->last_id == id) {
2502 frame->double_click = 1;
2503 frame->did_double = 1;
2504 frame->double_id = id;
2505 } else
2506 frame->did_double = 0;
2507
2508 frame->last_time = time;
2509 frame->last_id = id;
2510
2511 if (frame->double_click)
2512 frame_double_touch_down(frame->frame, input, id, x, y);
2513 else
2514 frame_touch_down(frame->frame, input, id, x, y);
2515
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002516 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2517}
2518
2519static void
2520frame_touch_up_handler(struct widget *widget,
2521 struct input *input, uint32_t serial, uint32_t time,
2522 int32_t id, void *data)
2523{
2524 struct window_frame *frame = data;
2525
Xiong Zhang382de462014-06-12 11:06:26 +08002526 if (frame->double_id == id && frame->did_double) {
2527 frame->did_double = 0;
2528 frame->double_id = 0;
2529 frame_double_touch_up(frame->frame, input, id);
2530 } else
2531 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002532 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002533}
2534
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002535struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002536window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002537{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002538 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002539 uint32_t buttons;
2540
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002541 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002542 buttons = FRAME_BUTTON_NONE;
2543 } else {
2544 buttons = FRAME_BUTTON_ALL;
2545 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002546
Peter Huttererf3d62272013-08-08 11:57:05 +10002547 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002548 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002549 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002550
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002551 frame->widget = window_add_widget(window, frame);
2552 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002553
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002554 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2555 widget_set_resize_handler(frame->widget, frame_resize_handler);
2556 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002557 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002558 widget_set_motion_handler(frame->widget, frame_motion_handler);
2559 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002560 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002561 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002562
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002563 window->frame = frame;
2564
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002565 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002566}
2567
Kristian Høgsberga1627922012-06-20 17:30:03 -04002568void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002569window_frame_set_child_size(struct widget *widget, int child_width,
2570 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002571{
2572 struct display *display = widget->window->display;
2573 struct theme *t = display->theme;
2574 int decoration_width, decoration_height;
2575 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002576 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002577
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002578 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002579 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002580 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002581 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002582
2583 width = child_width + decoration_width;
2584 height = child_height + decoration_height;
2585 } else {
2586 width = child_width;
2587 height = child_height;
2588 }
2589
2590 window_schedule_resize(widget->window, width, height);
2591}
2592
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002593static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002594window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002595{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002596 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002597
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002598 /* frame->child must be destroyed by the application */
2599 widget_destroy(frame->widget);
2600 free(frame);
2601}
2602
2603static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002604input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002605 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002606{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002607 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002608 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002609
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002610 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002611 return;
2612
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002613 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002614 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002615 widget = old;
2616 if (input->grab)
2617 widget = input->grab;
2618 if (widget->leave_handler)
2619 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002620 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002621 }
2622
2623 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002624 widget = focus;
2625 if (input->grab)
2626 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002627 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002628 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002629 cursor = widget->enter_handler(focus, input, x, y,
2630 widget->user_data);
2631 else
2632 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002633
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002634 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002635 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002636}
2637
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002638void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002639touch_grab(struct input *input, int32_t touch_id)
2640{
2641 input->touch_grab = 1;
2642 input->touch_grab_id = touch_id;
2643}
2644
2645void
2646touch_ungrab(struct input *input)
2647{
2648 struct touch_point *tp, *tmp;
2649
2650 input->touch_grab = 0;
2651
2652 wl_list_for_each_safe(tp, tmp,
2653 &input->touch_point_list, link) {
2654 if (tp->id != input->touch_grab_id)
2655 continue;
2656 wl_list_remove(&tp->link);
2657 free(tp);
2658
2659 return;
2660 }
2661}
2662
2663void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002664input_grab(struct input *input, struct widget *widget, uint32_t button)
2665{
2666 input->grab = widget;
2667 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002668
2669 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002670}
2671
2672void
2673input_ungrab(struct input *input)
2674{
2675 struct widget *widget;
2676
2677 input->grab = NULL;
2678 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002679 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002680 input->sx, input->sy);
2681 input_set_focus_widget(input, widget, input->sx, input->sy);
2682 }
2683}
2684
2685static void
Derek Foreman118a4292015-04-22 17:23:35 -05002686cursor_delay_timer_reset(struct input *input, uint32_t duration)
2687{
2688 struct itimerspec its;
2689
2690 if (!duration)
2691 input->cursor_timer_running = false;
2692 else
2693 input->cursor_timer_running = true;
2694
2695 its.it_interval.tv_sec = 0;
2696 its.it_interval.tv_nsec = 0;
2697 its.it_value.tv_sec = duration / 1000;
2698 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2699 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2700 fprintf(stderr, "could not set cursor timerfd\n: %m");
2701}
2702
2703static void cancel_pointer_image_update(struct input *input)
2704{
2705 if (input->cursor_timer_running)
2706 cursor_delay_timer_reset(input, 0);
2707}
2708
2709static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002710input_remove_pointer_focus(struct input *input)
2711{
2712 struct window *window = input->pointer_focus;
2713
2714 if (!window)
2715 return;
2716
2717 input_set_focus_widget(input, NULL, 0, 0);
2718
2719 input->pointer_focus = NULL;
2720 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002721 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002722}
2723
2724static void
2725pointer_handle_enter(void *data, struct wl_pointer *pointer,
2726 uint32_t serial, struct wl_surface *surface,
2727 wl_fixed_t sx_w, wl_fixed_t sy_w)
2728{
2729 struct input *input = data;
2730 struct window *window;
2731 struct widget *widget;
2732 float sx = wl_fixed_to_double(sx_w);
2733 float sy = wl_fixed_to_double(sy_w);
2734
2735 if (!surface) {
2736 /* enter event for a window we've just destroyed */
2737 return;
2738 }
2739
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002740 window = wl_surface_get_user_data(surface);
2741 if (surface != window->main_surface->surface) {
2742 DBG("Ignoring input event from subsurface %p\n", surface);
2743 return;
2744 }
2745
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002746 input->display->serial = serial;
2747 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002748 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002749
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002750 input->sx = sx;
2751 input->sy = sy;
2752
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002753 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002754 input_set_focus_widget(input, widget, sx, sy);
2755}
2756
2757static void
2758pointer_handle_leave(void *data, struct wl_pointer *pointer,
2759 uint32_t serial, struct wl_surface *surface)
2760{
2761 struct input *input = data;
2762
2763 input->display->serial = serial;
2764 input_remove_pointer_focus(input);
2765}
2766
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002767static void
Daniel Stone37816df2012-05-16 18:45:18 +01002768pointer_handle_motion(void *data, struct wl_pointer *pointer,
2769 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002770{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002771 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002772 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002773 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002774 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002775 float sx = wl_fixed_to_double(sx_w);
2776 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002777
Paul Winwoodb22bf572013-08-29 10:52:54 +01002778 if (!window)
2779 return;
2780
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002781 input->sx = sx;
2782 input->sy = sy;
2783
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002784 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002785 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002786 * based on the old surface dimensions. However, if we have an active
2787 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002788 */
Derek Foreman46812b62015-08-26 17:13:27 -05002789 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002790 sy < window->main_surface->allocation.y ||
2791 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002792 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002793 return;
2794
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002795 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002796 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002797 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002798 }
2799
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002800 if (input->grab)
2801 widget = input->grab;
2802 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002803 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002804 if (widget) {
2805 if (widget->motion_handler)
2806 cursor = widget->motion_handler(input->focus_widget,
2807 input, time, sx, sy,
2808 widget->user_data);
2809 else
2810 cursor = widget->default_cursor;
2811 } else
2812 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002813
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002814 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002815}
2816
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002817static void
Daniel Stone37816df2012-05-16 18:45:18 +01002818pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002819 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002820{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002821 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002822 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002823 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002824
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002825 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002826 if (input->focus_widget && input->grab == NULL &&
2827 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002828 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002829
Neil Roberts6b28aad2012-01-23 19:11:18 +00002830 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002831 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002832 (*widget->button_handler)(widget,
2833 input, time,
2834 button, state,
2835 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002836
Daniel Stone4dbadb12012-05-30 16:31:51 +01002837 if (input->grab && input->grab_button == button &&
2838 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002839 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002840}
2841
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002842static void
Daniel Stone37816df2012-05-16 18:45:18 +01002843pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002844 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002845{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002846 struct input *input = data;
2847 struct widget *widget;
2848
2849 widget = input->focus_widget;
2850 if (input->grab)
2851 widget = input->grab;
2852 if (widget && widget->axis_handler)
2853 (*widget->axis_handler)(widget,
2854 input, time,
2855 axis, value,
2856 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002857}
2858
Peter Hutterer87743e92016-01-18 16:38:22 +10002859static void
2860pointer_handle_frame(void *data, struct wl_pointer *pointer)
2861{
2862 struct input *input = data;
2863 struct widget *widget;
2864
2865 widget = input->focus_widget;
2866 if (input->grab)
2867 widget = input->grab;
2868 if (widget && widget->pointer_frame_handler)
2869 (*widget->pointer_frame_handler)(widget,
2870 input,
2871 widget->user_data);
2872}
2873
2874static void
2875pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2876 uint32_t source)
2877{
2878 struct input *input = data;
2879 struct widget *widget;
2880
2881 widget = input->focus_widget;
2882 if (input->grab)
2883 widget = input->grab;
2884 if (widget && widget->axis_source_handler)
2885 (*widget->axis_source_handler)(widget,
2886 input,
2887 source,
2888 widget->user_data);
2889}
2890
2891static void
2892pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2893 uint32_t time, uint32_t axis)
2894{
2895 struct input *input = data;
2896 struct widget *widget;
2897
2898 widget = input->focus_widget;
2899 if (input->grab)
2900 widget = input->grab;
2901 if (widget && widget->axis_stop_handler)
2902 (*widget->axis_stop_handler)(widget,
2903 input, time,
2904 axis,
2905 widget->user_data);
2906}
2907
2908static void
2909pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2910 uint32_t axis, int32_t discrete)
2911{
2912 struct input *input = data;
2913 struct widget *widget;
2914
2915 widget = input->focus_widget;
2916 if (input->grab)
2917 widget = input->grab;
2918 if (widget && widget->axis_discrete_handler)
2919 (*widget->axis_discrete_handler)(widget,
2920 input,
2921 axis,
2922 discrete,
2923 widget->user_data);
2924}
2925
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002926static const struct wl_pointer_listener pointer_listener = {
2927 pointer_handle_enter,
2928 pointer_handle_leave,
2929 pointer_handle_motion,
2930 pointer_handle_button,
2931 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002932 pointer_handle_frame,
2933 pointer_handle_axis_source,
2934 pointer_handle_axis_stop,
2935 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002936};
2937
2938static void
2939input_remove_keyboard_focus(struct input *input)
2940{
2941 struct window *window = input->keyboard_focus;
2942 struct itimerspec its;
2943
2944 its.it_interval.tv_sec = 0;
2945 its.it_interval.tv_nsec = 0;
2946 its.it_value.tv_sec = 0;
2947 its.it_value.tv_nsec = 0;
2948 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2949
2950 if (!window)
2951 return;
2952
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002953 if (window->keyboard_focus_handler)
2954 (*window->keyboard_focus_handler)(window, NULL,
2955 window->user_data);
2956
2957 input->keyboard_focus = NULL;
2958}
2959
2960static void
2961keyboard_repeat_func(struct task *task, uint32_t events)
2962{
2963 struct input *input =
2964 container_of(task, struct input, repeat_task);
2965 struct window *window = input->keyboard_focus;
2966 uint64_t exp;
2967
2968 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2969 /* If we change the timer between the fd becoming
2970 * readable and getting here, there'll be nothing to
2971 * read and we get EAGAIN. */
2972 return;
2973
2974 if (window && window->key_handler) {
2975 (*window->key_handler)(window, input, input->repeat_time,
2976 input->repeat_key, input->repeat_sym,
2977 WL_KEYBOARD_KEY_STATE_PRESSED,
2978 window->user_data);
2979 }
2980}
2981
2982static void
2983keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2984 uint32_t format, int fd, uint32_t size)
2985{
2986 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002987 struct xkb_keymap *keymap;
2988 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002989#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002990 struct xkb_compose_table *compose_table;
2991 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002992#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002993 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002994 char *map_str;
2995
2996 if (!data) {
2997 close(fd);
2998 return;
2999 }
3000
3001 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3002 close(fd);
3003 return;
3004 }
3005
3006 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3007 if (map_str == MAP_FAILED) {
3008 close(fd);
3009 return;
3010 }
3011
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003012 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03003013 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
3014 map_str,
3015 XKB_KEYMAP_FORMAT_TEXT_V1,
3016 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003017 munmap(map_str, size);
3018 close(fd);
3019
Rui Matos3eccb862013-10-10 19:44:22 +02003020 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003021 fprintf(stderr, "failed to compile keymap\n");
3022 return;
3023 }
3024
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003025 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02003026 state = xkb_state_new(keymap);
3027 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003028 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003029 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003030 return;
3031 }
3032
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003033 /* Look up the preferred locale, falling back to "C" as default */
3034 if (!(locale = getenv("LC_ALL")))
3035 if (!(locale = getenv("LC_CTYPE")))
3036 if (!(locale = getenv("LANG")))
3037 locale = "C";
3038
3039 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003040#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003041 compose_table =
3042 xkb_compose_table_new_from_locale(input->display->xkb_context,
3043 locale,
3044 XKB_COMPOSE_COMPILE_NO_FLAGS);
3045 if (compose_table) {
3046 /* Set up XKB compose state */
3047 compose_state = xkb_compose_state_new(compose_table,
3048 XKB_COMPOSE_STATE_NO_FLAGS);
3049 if (compose_state) {
3050 xkb_compose_state_unref(input->xkb.compose_state);
3051 xkb_compose_table_unref(input->xkb.compose_table);
3052 input->xkb.compose_state = compose_state;
3053 input->xkb.compose_table = compose_table;
3054 } else {
3055 fprintf(stderr, "could not create XKB compose state. "
3056 "Disabiling compose.\n");
3057 xkb_compose_table_unref(compose_table);
3058 compose_table = NULL;
3059 }
3060 } else {
3061 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3062 "Disabiling compose\n", locale);
3063 }
Daniel Stone5b015962016-10-20 14:45:58 +01003064#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003065
Rui Matos3eccb862013-10-10 19:44:22 +02003066 xkb_keymap_unref(input->xkb.keymap);
3067 xkb_state_unref(input->xkb.state);
3068 input->xkb.keymap = keymap;
3069 input->xkb.state = state;
3070
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003071 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003072 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003073 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003074 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003075 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003076 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003077}
3078
3079static void
3080keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3081 uint32_t serial, struct wl_surface *surface,
3082 struct wl_array *keys)
3083{
3084 struct input *input = data;
3085 struct window *window;
3086
3087 input->display->serial = serial;
3088 input->keyboard_focus = wl_surface_get_user_data(surface);
3089
3090 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003091 if (window->keyboard_focus_handler)
3092 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003093 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003094}
3095
3096static void
3097keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3098 uint32_t serial, struct wl_surface *surface)
3099{
3100 struct input *input = data;
3101
3102 input->display->serial = serial;
3103 input_remove_keyboard_focus(input);
3104}
3105
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003106/* Translate symbols appropriately if a compose sequence is being entered */
3107static xkb_keysym_t
3108process_key_press(xkb_keysym_t sym, struct input *input)
3109{
Daniel Stone5b015962016-10-20 14:45:58 +01003110#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003111 if (!input->xkb.compose_state)
3112 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003113 if (sym == XKB_KEY_NoSymbol)
3114 return sym;
3115 if (xkb_compose_state_feed(input->xkb.compose_state,
3116 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3117 return sym;
3118
3119 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3120 case XKB_COMPOSE_COMPOSING:
3121 return XKB_KEY_NoSymbol;
3122 case XKB_COMPOSE_COMPOSED:
3123 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3124 case XKB_COMPOSE_CANCELLED:
3125 return XKB_KEY_NoSymbol;
3126 case XKB_COMPOSE_NOTHING:
3127 return sym;
3128 default:
3129 return sym;
3130 }
Daniel Stone5b015962016-10-20 14:45:58 +01003131#else
3132 return sym;
3133#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003134}
3135
Scott Moreau210d0792012-03-22 10:47:01 -06003136static void
Daniel Stone37816df2012-05-16 18:45:18 +01003137keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003138 uint32_t serial, uint32_t time, uint32_t key,
3139 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003140{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003141 struct input *input = data;
3142 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003143 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003144 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003145 const xkb_keysym_t *syms;
3146 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003147 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003148
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003149 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003150 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003151 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003152 return;
3153
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003154 /* We only use input grabs for pointer events for now, so just
3155 * ignore key presses if a grab is active. We expand the key
3156 * event delivery mechanism to route events to widgets to
3157 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003158 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003159 if (input->grab && input->grab_button == 0)
3160 return;
3161
Ran Benita2e1968f2014-08-19 23:59:51 +03003162 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003163
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003164 sym = XKB_KEY_NoSymbol;
3165 if (num_syms == 1)
3166 sym = syms[0];
3167
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003168
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003169 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003170 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003171 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003172 } else if (sym == XKB_KEY_F11 &&
3173 window->fullscreen_handler &&
3174 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3175 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003176 } else if (sym == XKB_KEY_F4 &&
3177 input->modifiers == MOD_ALT_MASK &&
3178 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003179 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003180 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003181 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3182 sym = process_key_press(sym, input);
3183
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003184 (*window->key_handler)(window, input, time, key,
3185 sym, state, window->user_data);
3186 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003187
3188 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3189 key == input->repeat_key) {
3190 its.it_interval.tv_sec = 0;
3191 its.it_interval.tv_nsec = 0;
3192 its.it_value.tv_sec = 0;
3193 its.it_value.tv_nsec = 0;
3194 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003195 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3196 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003197 input->repeat_sym = sym;
3198 input->repeat_key = key;
3199 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003200 its.it_interval.tv_sec = input->repeat_rate_sec;
3201 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3202 its.it_value.tv_sec = input->repeat_delay_sec;
3203 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003204 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3205 }
3206}
3207
3208static void
Daniel Stone351eb612012-05-31 15:27:47 -04003209keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3210 uint32_t serial, uint32_t mods_depressed,
3211 uint32_t mods_latched, uint32_t mods_locked,
3212 uint32_t group)
3213{
3214 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003215 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003216
Matt Ropere61561f2013-06-24 16:52:43 +01003217 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3218 if (!input->xkb.keymap)
3219 return;
3220
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003221 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3222 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003223 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003224 XKB_STATE_MODS_DEPRESSED |
3225 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003226 input->modifiers = 0;
3227 if (mask & input->xkb.control_mask)
3228 input->modifiers |= MOD_CONTROL_MASK;
3229 if (mask & input->xkb.alt_mask)
3230 input->modifiers |= MOD_ALT_MASK;
3231 if (mask & input->xkb.shift_mask)
3232 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003233}
3234
Jonny Lamb06959082014-08-12 14:58:27 +02003235static void
3236set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3237{
3238 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3239 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3240
3241 /* a rate of zero disables any repeating, regardless of the delay's
3242 * value */
3243 if (rate == 0)
3244 return;
3245
3246 if (rate == 1)
3247 input->repeat_rate_sec = 1;
3248 else
3249 input->repeat_rate_nsec = 1000000000 / rate;
3250
3251 input->repeat_delay_sec = delay / 1000;
3252 delay -= (input->repeat_delay_sec * 1000);
3253 input->repeat_delay_nsec = delay * 1000 * 1000;
3254}
3255
3256static void
3257keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3258 int32_t rate, int32_t delay)
3259{
3260 struct input *input = data;
3261
3262 set_repeat_info(input, rate, delay);
3263}
3264
Daniel Stone37816df2012-05-16 18:45:18 +01003265static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003266 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003267 keyboard_handle_enter,
3268 keyboard_handle_leave,
3269 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003270 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003271 keyboard_handle_repeat_info
3272
Daniel Stone37816df2012-05-16 18:45:18 +01003273};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003274
3275static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003276touch_handle_down(void *data, struct wl_touch *wl_touch,
3277 uint32_t serial, uint32_t time, struct wl_surface *surface,
3278 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3279{
3280 struct input *input = data;
3281 struct widget *widget;
3282 float sx = wl_fixed_to_double(x_w);
3283 float sy = wl_fixed_to_double(y_w);
3284
Rusty Lynch1084da52013-08-15 09:10:08 -07003285 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003286 input->touch_focus = wl_surface_get_user_data(surface);
3287 if (!input->touch_focus) {
3288 DBG("Failed to find to touch focus for surface %p\n", surface);
3289 return;
3290 }
3291
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003292 if (surface != input->touch_focus->main_surface->surface) {
3293 DBG("Ignoring input event from subsurface %p\n", surface);
3294 input->touch_focus = NULL;
3295 return;
3296 }
3297
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003298 if (input->grab)
3299 widget = input->grab;
3300 else
3301 widget = window_find_widget(input->touch_focus,
3302 wl_fixed_to_double(x_w),
3303 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003304 if (widget) {
3305 struct touch_point *tp = xmalloc(sizeof *tp);
3306 if (tp) {
3307 tp->id = id;
3308 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003309 tp->x = sx;
3310 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003311 wl_list_insert(&input->touch_point_list, &tp->link);
3312
3313 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003314 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003315 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003316 sx, sy,
3317 widget->user_data);
3318 }
3319 }
3320}
3321
3322static void
3323touch_handle_up(void *data, struct wl_touch *wl_touch,
3324 uint32_t serial, uint32_t time, int32_t id)
3325{
3326 struct input *input = data;
3327 struct touch_point *tp, *tmp;
3328
Rusty Lynch041815a2013-08-08 21:20:38 -07003329 if (!input->touch_focus) {
3330 DBG("No touch focus found for touch up event!\n");
3331 return;
3332 }
3333
3334 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3335 if (tp->id != id)
3336 continue;
3337
3338 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003339 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003340 time, id,
3341 tp->widget->user_data);
3342
3343 wl_list_remove(&tp->link);
3344 free(tp);
3345
3346 return;
3347 }
3348}
3349
3350static void
3351touch_handle_motion(void *data, struct wl_touch *wl_touch,
3352 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3353{
3354 struct input *input = data;
3355 struct touch_point *tp;
3356 float sx = wl_fixed_to_double(x_w);
3357 float sy = wl_fixed_to_double(y_w);
3358
3359 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3360
3361 if (!input->touch_focus) {
3362 DBG("No touch focus found for touch motion event!\n");
3363 return;
3364 }
3365
3366 wl_list_for_each(tp, &input->touch_point_list, link) {
3367 if (tp->id != id)
3368 continue;
3369
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003370 tp->x = sx;
3371 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003372 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003373 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003374 id, sx, sy,
3375 tp->widget->user_data);
3376 return;
3377 }
3378}
3379
3380static void
3381touch_handle_frame(void *data, struct wl_touch *wl_touch)
3382{
3383 struct input *input = data;
3384 struct touch_point *tp, *tmp;
3385
3386 DBG("touch_handle_frame\n");
3387
3388 if (!input->touch_focus) {
3389 DBG("No touch focus found for touch frame event!\n");
3390 return;
3391 }
3392
3393 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3394 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003395 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003396 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003397 }
3398}
3399
3400static void
3401touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3402{
3403 struct input *input = data;
3404 struct touch_point *tp, *tmp;
3405
3406 DBG("touch_handle_cancel\n");
3407
3408 if (!input->touch_focus) {
3409 DBG("No touch focus found for touch cancel event!\n");
3410 return;
3411 }
3412
3413 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3414 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003415 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3416 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003417
3418 wl_list_remove(&tp->link);
3419 free(tp);
3420 }
3421}
3422
3423static const struct wl_touch_listener touch_listener = {
3424 touch_handle_down,
3425 touch_handle_up,
3426 touch_handle_motion,
3427 touch_handle_frame,
3428 touch_handle_cancel,
3429};
3430
3431static void
Daniel Stone37816df2012-05-16 18:45:18 +01003432seat_handle_capabilities(void *data, struct wl_seat *seat,
3433 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003434{
Daniel Stone37816df2012-05-16 18:45:18 +01003435 struct input *input = data;
3436
3437 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3438 input->pointer = wl_seat_get_pointer(seat);
3439 wl_pointer_set_user_data(input->pointer, input);
3440 wl_pointer_add_listener(input->pointer, &pointer_listener,
3441 input);
3442 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003443 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003444 wl_pointer_release(input->pointer);
3445 else
3446 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003447 input->pointer = NULL;
3448 }
3449
3450 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3451 input->keyboard = wl_seat_get_keyboard(seat);
3452 wl_keyboard_set_user_data(input->keyboard, input);
3453 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3454 input);
3455 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003456 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003457 wl_keyboard_release(input->keyboard);
3458 else
3459 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003460 input->keyboard = NULL;
3461 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003462
3463 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3464 input->touch = wl_seat_get_touch(seat);
3465 wl_touch_set_user_data(input->touch, input);
3466 wl_touch_add_listener(input->touch, &touch_listener, input);
3467 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003468 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003469 wl_touch_release(input->touch);
3470 else
3471 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003472 input->touch = NULL;
3473 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003474}
3475
Rob Bradford08031182013-08-13 20:11:03 +01003476static void
3477seat_handle_name(void *data, struct wl_seat *seat,
3478 const char *name)
3479{
3480
3481}
3482
Daniel Stone37816df2012-05-16 18:45:18 +01003483static const struct wl_seat_listener seat_listener = {
3484 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003485 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003486};
3487
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003488void
3489input_get_position(struct input *input, int32_t *x, int32_t *y)
3490{
3491 *x = input->sx;
3492 *y = input->sy;
3493}
3494
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003495int
3496input_get_touch(struct input *input, int32_t id, float *x, float *y)
3497{
3498 struct touch_point *tp;
3499
3500 wl_list_for_each(tp, &input->touch_point_list, link) {
3501 if (tp->id != id)
3502 continue;
3503
3504 *x = tp->x;
3505 *y = tp->y;
3506 return 0;
3507 }
3508
3509 return -1;
3510}
3511
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003512struct display *
3513input_get_display(struct input *input)
3514{
3515 return input->display;
3516}
3517
Daniel Stone37816df2012-05-16 18:45:18 +01003518struct wl_seat *
3519input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003520{
Daniel Stone37816df2012-05-16 18:45:18 +01003521 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003522}
3523
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003524uint32_t
3525input_get_modifiers(struct input *input)
3526{
3527 return input->modifiers;
3528}
3529
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003530struct widget *
3531input_get_focus_widget(struct input *input)
3532{
3533 return input->focus_widget;
3534}
3535
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003536struct data_offer {
3537 struct wl_data_offer *offer;
3538 struct input *input;
3539 struct wl_array types;
3540 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003541
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003542 struct task io_task;
3543 int fd;
3544 data_func_t func;
3545 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003546 uint32_t dnd_action;
3547 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003548 void *user_data;
3549};
3550
3551static void
3552data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3553{
3554 struct data_offer *offer = data;
3555 char **p;
3556
3557 p = wl_array_add(&offer->types, sizeof *p);
3558 *p = strdup(type);
3559}
3560
Carlos Garnacho9c931792016-01-18 23:52:12 +01003561static void
3562data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3563{
3564 struct data_offer *offer = data;
3565
3566 offer->source_actions = source_actions;
3567}
3568
3569static void
3570data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3571{
3572 struct data_offer *offer = data;
3573
3574 offer->dnd_action = dnd_action;
3575}
3576
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003577static const struct wl_data_offer_listener data_offer_listener = {
3578 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003579 data_offer_source_actions,
3580 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003581};
3582
3583static void
3584data_offer_destroy(struct data_offer *offer)
3585{
3586 char **p;
3587
3588 offer->refcount--;
3589 if (offer->refcount == 0) {
3590 wl_data_offer_destroy(offer->offer);
3591 for (p = offer->types.data; *p; p++)
3592 free(*p);
3593 wl_array_release(&offer->types);
3594 free(offer);
3595 }
3596}
3597
3598static void
3599data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003600 struct wl_data_device *data_device,
3601 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003602{
3603 struct data_offer *offer;
3604
Brian Lovinbc919262013-08-07 15:34:59 -07003605 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003606
3607 wl_array_init(&offer->types);
3608 offer->refcount = 1;
3609 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003610 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003611 wl_data_offer_add_listener(offer->offer,
3612 &data_offer_listener, offer);
3613}
3614
3615static void
3616data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003617 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003618 wl_fixed_t x_w, wl_fixed_t y_w,
3619 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003620{
3621 struct input *input = data;
3622 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003623 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003624 float x = wl_fixed_to_double(x_w);
3625 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003626 char **p;
3627
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003628 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003629 input->drag_enter_serial = serial;
3630 input->drag_focus = window,
3631 input->drag_x = x;
3632 input->drag_y = y;
3633
3634 if (!input->touch_grab)
3635 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003636
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003637 if (offer) {
3638 input->drag_offer = wl_data_offer_get_user_data(offer);
3639
3640 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3641 *p = NULL;
3642
3643 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003644
3645 if (input->display->data_device_manager_version >=
3646 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3647 wl_data_offer_set_actions(offer,
3648 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3649 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3650 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3651 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003652 } else {
3653 input->drag_offer = NULL;
3654 types_data = NULL;
3655 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003656
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003657 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003658 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003659 window->user_data);
3660}
3661
3662static void
3663data_device_leave(void *data, struct wl_data_device *data_device)
3664{
3665 struct input *input = data;
3666
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003667 if (input->drag_offer) {
3668 data_offer_destroy(input->drag_offer);
3669 input->drag_offer = NULL;
3670 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003671}
3672
3673static void
3674data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003675 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003676{
3677 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003678 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003679 float x = wl_fixed_to_double(x_w);
3680 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003681 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003682
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003683 input->drag_x = x;
3684 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003685
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003686 if (input->drag_offer)
3687 types_data = input->drag_offer->types.data;
3688 else
3689 types_data = NULL;
3690
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003691 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003692 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003693 window->user_data);
3694}
3695
3696static void
3697data_device_drop(void *data, struct wl_data_device *data_device)
3698{
3699 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003700 struct window *window = input->drag_focus;
3701 float x, y;
3702
3703 x = input->drag_x;
3704 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003705
3706 if (window->drop_handler)
3707 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003708 x, y, window->user_data);
3709
3710 if (input->touch_grab)
3711 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003712}
3713
3714static void
3715data_device_selection(void *data,
3716 struct wl_data_device *wl_data_device,
3717 struct wl_data_offer *offer)
3718{
3719 struct input *input = data;
3720 char **p;
3721
3722 if (input->selection_offer)
3723 data_offer_destroy(input->selection_offer);
3724
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003725 if (offer) {
3726 input->selection_offer = wl_data_offer_get_user_data(offer);
3727 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3728 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003729 } else {
3730 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003731 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003732}
3733
3734static const struct wl_data_device_listener data_device_listener = {
3735 data_device_data_offer,
3736 data_device_enter,
3737 data_device_leave,
3738 data_device_motion,
3739 data_device_drop,
3740 data_device_selection
3741};
3742
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003743static void
3744input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003745{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003746 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003747 struct wl_cursor *cursor;
3748 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003749
Daniel Stone80972742012-11-07 17:51:39 +11003750 if (!input->pointer)
3751 return;
3752
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003753 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003754 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003755 return;
3756
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003757 if (index >= (int) cursor->image_count) {
3758 fprintf(stderr, "cursor index out of range\n");
3759 return;
3760 }
3761
3762 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003763 buffer = wl_cursor_image_get_buffer(image);
3764 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003765 return;
3766
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003767 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3768 wl_surface_damage(input->pointer_surface, 0, 0,
3769 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003770 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003771 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3772 input->pointer_surface,
3773 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003774}
3775
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003776static const struct wl_callback_listener pointer_surface_listener;
3777
Derek Foreman493d9792015-03-04 16:26:25 -06003778static bool
3779input_set_pointer_special(struct input *input)
3780{
3781 if (input->current_cursor == CURSOR_BLANK) {
3782 wl_pointer_set_cursor(input->pointer,
3783 input->pointer_enter_serial,
3784 NULL, 0, 0);
3785 return true;
3786 }
3787
3788 if (input->current_cursor == CURSOR_UNSET)
3789 return true;
3790
3791 return false;
3792}
3793
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003794static void
Derek Foreman118a4292015-04-22 17:23:35 -05003795schedule_pointer_image_update(struct input *input,
3796 struct wl_cursor *cursor,
3797 uint32_t duration,
3798 bool force_frame)
3799{
3800 /* Some silly cursor sets have enormous pauses in them. In these
3801 * cases it's better to use a timer even if it results in less
3802 * accurate presentation, since it will save us having to set the
3803 * same cursor image over and over again.
3804 *
3805 * This is really not the way we're supposed to time any kind of
3806 * animation, but we're pretending it's OK here because we don't
3807 * want animated cursors with long delays to needlessly hog CPU.
3808 *
3809 * We use force_frame to ensure we don't accumulate large timing
3810 * errors by running off the wrong clock.
3811 */
3812 if (!force_frame && duration > 100) {
3813 struct timespec tp;
3814
3815 clock_gettime(CLOCK_MONOTONIC, &tp);
3816 input->cursor_timer_start = tp.tv_sec * 1000
3817 + tp.tv_nsec / 1000000;
3818 cursor_delay_timer_reset(input, duration);
3819 return;
3820 }
3821
3822 /* for short durations we'll just spin on frame callbacks for
3823 * accurate timing - the way any kind of timing sensitive animation
3824 * should really be done. */
3825 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3826 wl_callback_add_listener(input->cursor_frame_cb,
3827 &pointer_surface_listener, input);
3828
3829}
3830
3831static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003832pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3833 uint32_t time)
3834{
3835 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003836 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003837 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003838 uint32_t duration;
3839 bool force_frame = true;
3840
3841 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003842
3843 if (callback) {
3844 assert(callback == input->cursor_frame_cb);
3845 wl_callback_destroy(callback);
3846 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003847 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003848 }
3849
Daniel Stone80972742012-11-07 17:51:39 +11003850 if (!input->pointer)
3851 return;
3852
Derek Foreman493d9792015-03-04 16:26:25 -06003853 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003854 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003855
Daniel Stonea494f1d2012-06-18 19:31:12 +01003856 cursor = input->display->cursors[input->current_cursor];
3857 if (!cursor)
3858 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003859
3860 /* FIXME We don't have the current time on the first call so we set
3861 * the animation start to the time of the first frame callback. */
3862 if (time == 0)
3863 input->cursor_anim_start = 0;
3864 else if (input->cursor_anim_start == 0)
3865 input->cursor_anim_start = time;
3866
Derek Foreman118a4292015-04-22 17:23:35 -05003867 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003868
Derek Foreman118a4292015-04-22 17:23:35 -05003869 if (time == 0 || input->cursor_anim_start == 0) {
3870 duration = 0;
3871 i = 0;
3872 } else
3873 i = wl_cursor_frame_and_duration(
3874 cursor,
3875 time - input->cursor_anim_start,
3876 &duration);
3877
3878 if (cursor->image_count > 1)
3879 schedule_pointer_image_update(input, cursor, duration,
3880 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003881
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003882 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003883}
3884
Derek Foreman118a4292015-04-22 17:23:35 -05003885static void
3886cursor_timer_func(struct task *task, uint32_t events)
3887{
3888 struct input *input = container_of(task, struct input, cursor_task);
3889 struct timespec tp;
3890 struct wl_cursor *cursor;
3891 uint32_t time;
3892 uint64_t exp;
3893
3894 if (!input->cursor_timer_running)
3895 return;
3896
3897 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3898 return;
3899
3900 cursor = input->display->cursors[input->current_cursor];
3901 if (!cursor)
3902 return;
3903
3904 clock_gettime(CLOCK_MONOTONIC, &tp);
3905 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3906 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3907}
3908
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003909static const struct wl_callback_listener pointer_surface_listener = {
3910 pointer_surface_frame_callback
3911};
3912
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003913void
3914input_set_pointer_image(struct input *input, int pointer)
3915{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003916 int force = 0;
3917
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003918 if (!input->pointer)
3919 return;
3920
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003921 if (input->pointer_enter_serial > input->cursor_serial)
3922 force = 1;
3923
3924 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003925 return;
3926
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003927 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003928 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003929 if (!input->cursor_frame_cb)
3930 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003931 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003932 /* The current frame callback may be stuck if, for instance,
3933 * the set cursor request was processed by the server after
3934 * this client lost the focus. In this case the cursor surface
3935 * might not be mapped and the frame callback wouldn't ever
3936 * complete. Send a set_cursor and attach to try to map the
3937 * cursor surface again so that the callback will finish */
3938 input_set_pointer_image_index(input, 0);
3939 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003940}
3941
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003942struct wl_data_device *
3943input_get_data_device(struct input *input)
3944{
3945 return input->data_device;
3946}
3947
3948void
3949input_set_selection(struct input *input,
3950 struct wl_data_source *source, uint32_t time)
3951{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003952 if (input->data_device)
3953 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003954}
3955
3956void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003957input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003958{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003959 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003960 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003961}
3962
3963static void
3964offer_io_func(struct task *task, uint32_t events)
3965{
3966 struct data_offer *offer =
3967 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003968 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003969 unsigned int len;
3970 char buffer[4096];
3971
3972 len = read(offer->fd, buffer, sizeof buffer);
3973 offer->func(buffer, len,
3974 offer->x, offer->y, offer->user_data);
3975
3976 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003977 if (display->data_device_manager_version >=
3978 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3979 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003980 close(offer->fd);
3981 data_offer_destroy(offer);
3982 }
3983}
3984
3985static void
3986data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3987 data_func_t func, void *user_data)
3988{
3989 int p[2];
3990
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003991 if (pipe2(p, O_CLOEXEC) == -1)
3992 return;
3993
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003994 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3995 close(p[1]);
3996
3997 offer->io_task.run = offer_io_func;
3998 offer->fd = p[0];
3999 offer->func = func;
4000 offer->refcount++;
4001 offer->user_data = user_data;
4002
4003 display_watch_fd(offer->input->display,
4004 offer->fd, EPOLLIN, &offer->io_task);
4005}
4006
4007void
4008input_receive_drag_data(struct input *input, const char *mime_type,
4009 data_func_t func, void *data)
4010{
4011 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004012 input->drag_offer->x = input->drag_x;
4013 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004014}
4015
4016int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004017input_receive_drag_data_to_fd(struct input *input,
4018 const char *mime_type, int fd)
4019{
4020 if (input->drag_offer)
4021 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4022
4023 return 0;
4024}
4025
4026int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004027input_receive_selection_data(struct input *input, const char *mime_type,
4028 data_func_t func, void *data)
4029{
4030 char **p;
4031
4032 if (input->selection_offer == NULL)
4033 return -1;
4034
4035 for (p = input->selection_offer->types.data; *p; p++)
4036 if (strcmp(mime_type, *p) == 0)
4037 break;
4038
4039 if (*p == NULL)
4040 return -1;
4041
4042 data_offer_receive_data(input->selection_offer,
4043 mime_type, func, data);
4044 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004045}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004046
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004047int
4048input_receive_selection_data_to_fd(struct input *input,
4049 const char *mime_type, int fd)
4050{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004051 if (input->selection_offer)
4052 wl_data_offer_receive(input->selection_offer->offer,
4053 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004054
4055 return 0;
4056}
4057
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004058void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004059window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004060{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004061 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004062 return;
4063
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004064 zxdg_toplevel_v6_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004065}
4066
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004067static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004068surface_set_synchronized(struct surface *surface)
4069{
4070 if (!surface->subsurface)
4071 return;
4072
4073 if (surface->synchronized)
4074 return;
4075
4076 wl_subsurface_set_sync(surface->subsurface);
4077 surface->synchronized = 1;
4078}
4079
4080static void
4081surface_set_synchronized_default(struct surface *surface)
4082{
4083 if (!surface->subsurface)
4084 return;
4085
4086 if (surface->synchronized == surface->synchronized_default)
4087 return;
4088
4089 if (surface->synchronized_default)
4090 wl_subsurface_set_sync(surface->subsurface);
4091 else
4092 wl_subsurface_set_desync(surface->subsurface);
4093
4094 surface->synchronized = surface->synchronized_default;
4095}
4096
4097static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004098surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004099{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004100 struct widget *widget = surface->widget;
4101 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004102
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004103 if (surface->input_region) {
4104 wl_region_destroy(surface->input_region);
4105 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004106 }
4107
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004108 if (surface->opaque_region)
4109 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004110
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004111 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004112
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004113 if (widget->resize_handler)
4114 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004115 widget->allocation.width,
4116 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004117 widget->user_data);
4118
Pekka Paalanen35e82632013-04-25 13:57:48 +03004119 if (surface->subsurface &&
4120 (surface->allocation.x != widget->allocation.x ||
4121 surface->allocation.y != widget->allocation.y)) {
4122 wl_subsurface_set_position(surface->subsurface,
4123 widget->allocation.x,
4124 widget->allocation.y);
4125 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004126 if (surface->allocation.width != widget->allocation.width ||
4127 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004128 window_schedule_redraw(widget->window);
4129 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004130 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004131
4132 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004133 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004134 widget->allocation.width,
4135 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004136}
4137
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004138static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004139window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004140{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004141 struct surface *surface;
4142
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004143 widget_set_allocation(window->main_surface->widget,
4144 window->pending_allocation.x,
4145 window->pending_allocation.y,
4146 window->pending_allocation.width,
4147 window->pending_allocation.height);
4148
4149 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004150
4151 /* The main surface is in the list, too. Main surface's
4152 * resize_handler is responsible for calling widget_set_allocation()
4153 * on all sub-surface root widgets, so they will be resized
4154 * properly.
4155 */
4156 wl_list_for_each(surface, &window->subsurface_list, link) {
4157 if (surface == window->main_surface)
4158 continue;
4159
4160 surface_set_synchronized(surface);
4161 surface_resize(surface);
4162 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004163
4164 if (!window->fullscreen && !window->maximized)
4165 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004166
4167 if (window->confined && window->confined_widget) {
4168 struct wl_compositor *compositor = window->display->compositor;
4169 struct wl_region *region;
4170 struct widget *widget = window->confined_widget;
4171
4172 region = wl_compositor_create_region(compositor);
4173 wl_region_add(region,
4174 widget->allocation.x,
4175 widget->allocation.y,
4176 widget->allocation.width,
4177 widget->allocation.height);
4178 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4179 region);
4180 wl_region_destroy(region);
4181 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004182}
4183
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004184static void
4185idle_resize(struct window *window)
4186{
4187 window->resize_needed = 0;
4188 window->redraw_needed = 1;
4189
4190 DBG("from %dx%d to %dx%d\n",
4191 window->main_surface->server_allocation.width,
4192 window->main_surface->server_allocation.height,
4193 window->pending_allocation.width,
4194 window->pending_allocation.height);
4195
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004196 window_do_resize(window);
4197}
4198
4199static void
4200undo_resize(struct window *window)
4201{
4202 window->pending_allocation.width =
4203 window->main_surface->server_allocation.width;
4204 window->pending_allocation.height =
4205 window->main_surface->server_allocation.height;
4206
4207 DBG("back to %dx%d\n",
4208 window->main_surface->server_allocation.width,
4209 window->main_surface->server_allocation.height);
4210
4211 window_do_resize(window);
4212
4213 if (window->pending_allocation.width == 0 &&
4214 window->pending_allocation.height == 0) {
4215 fprintf(stderr, "Error: Could not draw a surface, "
4216 "most likely due to insufficient disk space in "
4217 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4218 exit(EXIT_FAILURE);
4219 }
4220}
4221
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004222void
4223window_schedule_resize(struct window *window, int width, int height)
4224{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004225 /* We should probably get these numbers from the theme. */
4226 const int min_width = 200, min_height = 200;
4227
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004228 window->pending_allocation.x = 0;
4229 window->pending_allocation.y = 0;
4230 window->pending_allocation.width = width;
4231 window->pending_allocation.height = height;
4232
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004233 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004234 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004235 window->min_allocation.width = min_width;
4236 else
4237 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004238 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004239 window->min_allocation.height = min_height;
4240 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004241 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004242 }
4243
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004244 if (window->pending_allocation.width < window->min_allocation.width)
4245 window->pending_allocation.width = window->min_allocation.width;
4246 if (window->pending_allocation.height < window->min_allocation.height)
4247 window->pending_allocation.height = window->min_allocation.height;
4248
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004249 window->resize_needed = 1;
4250 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004251}
4252
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004253void
4254widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4255{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004256 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004257}
4258
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004259static int
4260window_get_shadow_margin(struct window *window)
4261{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004262 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004263 return frame_get_shadow_margin(window->frame->frame);
4264 else
4265 return 0;
4266}
4267
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004268static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004269window_inhibit_redraw(struct window *window)
4270{
4271 window->redraw_inhibited = 1;
4272 wl_list_remove(&window->redraw_task.link);
4273 wl_list_init(&window->redraw_task.link);
4274 window->redraw_task_scheduled = 0;
4275}
4276
4277static void
4278window_uninhibit_redraw(struct window *window)
4279{
4280 window->redraw_inhibited = 0;
4281 if (window->redraw_needed || window->resize_needed)
4282 window_schedule_redraw_task(window);
4283}
4284
4285static void
4286xdg_surface_handle_configure(void *data,
4287 struct zxdg_surface_v6 *zxdg_surface_v6,
4288 uint32_t serial)
4289{
4290 struct window *window = data;
4291
4292 zxdg_surface_v6_ack_configure(window->xdg_surface, serial);
4293
4294 if (window->state_changed_handler)
4295 window->state_changed_handler(window, window->user_data);
4296
4297 window_uninhibit_redraw(window);
4298}
4299
4300static const struct zxdg_surface_v6_listener xdg_surface_listener = {
4301 xdg_surface_handle_configure
4302};
4303
4304static void
4305xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
4306 int32_t width, int32_t height,
4307 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004308{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004309 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004310 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004311
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004312 window->maximized = 0;
4313 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004314 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004315 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004316
4317 wl_array_for_each(p, states) {
4318 uint32_t state = *p;
4319 switch (state) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004320 case ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004321 window->maximized = 1;
4322 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004323 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004324 window->fullscreen = 1;
4325 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004326 case ZXDG_TOPLEVEL_V6_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004327 window->resizing = 1;
4328 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004329 case ZXDG_TOPLEVEL_V6_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004330 window->focused = 1;
4331 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004332 default:
4333 /* Unknown state */
4334 break;
4335 }
4336 }
4337
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004338 if (window->frame) {
4339 if (window->maximized) {
4340 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4341 } else {
4342 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4343 }
4344
4345 if (window->focused) {
4346 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4347 } else {
4348 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4349 }
4350 }
4351
Jasper St. Pierref184c382014-05-06 08:33:27 -04004352 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004353 /* The width / height params are for window geometry,
4354 * but window_schedule_resize takes allocation. Add
4355 * on the shadow margin to get the difference. */
4356 int margin = window_get_shadow_margin(window);
4357
4358 window_schedule_resize(window,
4359 width + margin * 2,
4360 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004361 } else if (window->saved_allocation.width > 0 &&
4362 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004363 window_schedule_resize(window,
4364 window->saved_allocation.width,
4365 window->saved_allocation.height);
4366 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004367}
4368
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004369static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004370xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004371{
4372 struct window *window = data;
4373 window_close(window);
4374}
4375
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004376static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
4377 xdg_toplevel_handle_configure,
4378 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004379};
4380
4381static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004382window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004383{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004384 struct zxdg_toplevel_v6 *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004385
4386 if (!window->xdg_surface)
4387 return;
4388
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004389 if (window->parent == window->last_parent)
4390 return;
4391
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004392 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004393 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004394 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004395 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004396
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004397 zxdg_toplevel_v6_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004398 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004399}
4400
4401static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004402window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004403{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004404 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004405 frame_input_rect(window->frame->frame,
4406 &geometry->x,
4407 &geometry->y,
4408 &geometry->width,
4409 &geometry->height);
4410 else
4411 window_get_allocation(window, geometry);
4412}
4413
4414static void
4415window_sync_geometry(struct window *window)
4416{
4417 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004418
4419 if (!window->xdg_surface)
4420 return;
4421
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004422 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004423 if (geometry.x == window->last_geometry.x &&
4424 geometry.y == window->last_geometry.y &&
4425 geometry.width == window->last_geometry.width &&
4426 geometry.height == window->last_geometry.height)
4427 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004428
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004429 zxdg_surface_v6_set_window_geometry(window->xdg_surface,
4430 geometry.x,
4431 geometry.y,
4432 geometry.width,
4433 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004434 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004435}
4436
4437static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004438window_flush(struct window *window)
4439{
4440 struct surface *surface;
4441
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004442 assert(!window->redraw_inhibited);
4443
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004444 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004445 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004446 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004447 if (window->xdg_toplevel)
4448 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004449 }
4450
4451 wl_list_for_each(surface, &window->subsurface_list, link) {
4452 if (surface == window->main_surface)
4453 continue;
4454
4455 surface_flush(surface);
4456 }
4457
4458 surface_flush(window->main_surface);
4459}
4460
4461static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004462menu_destroy(struct menu *menu)
4463{
4464 widget_destroy(menu->widget);
4465 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004466 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004467 free(menu);
4468}
4469
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004470void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004471window_get_allocation(struct window *window,
4472 struct rectangle *allocation)
4473{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004474 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004475}
4476
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004477static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004478widget_redraw(struct widget *widget)
4479{
4480 struct widget *child;
4481
4482 if (widget->redraw_handler)
4483 widget->redraw_handler(widget, widget->user_data);
4484 wl_list_for_each(child, &widget->child_list, link)
4485 widget_redraw(child);
4486}
4487
4488static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004489frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4490{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004491 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004492
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004493 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004494 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004495 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004496 surface->frame_cb = NULL;
4497
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004498 surface->last_time = time;
4499
Pekka Paalanen71233882013-04-25 13:57:53 +03004500 if (surface->redraw_needed || surface->window->redraw_needed) {
4501 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004502 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004503 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004504}
4505
4506static const struct wl_callback_listener listener = {
4507 frame_callback
4508};
4509
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004510static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004511surface_redraw(struct surface *surface)
4512{
Pekka Paalanen71233882013-04-25 13:57:53 +03004513 DBG_OBJ(surface->surface, "begin\n");
4514
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004515 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004516 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004517
4518 /* Whole-window redraw forces a redraw even if the previous has
4519 * not yet hit the screen.
4520 */
4521 if (surface->frame_cb) {
4522 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004523 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004524
Pekka Paalanen71233882013-04-25 13:57:53 +03004525 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004526 wl_callback_destroy(surface->frame_cb);
4527 }
4528
Neil Roberts97b747c2013-12-19 16:17:12 +00004529 if (surface->widget->use_cairo &&
4530 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004531 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004532 return -1;
4533 }
4534
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004535 surface->frame_cb = wl_surface_frame(surface->surface);
4536 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004537 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004538
4539 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004540 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004541 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004542 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004543 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004544}
4545
4546static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004547idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004548{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004549 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004550 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004551 int failed = 0;
4552 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004553
Pekka Paalanen71233882013-04-25 13:57:53 +03004554 DBG(" --------- \n");
4555
Pekka Paalaneneebff542013-04-25 13:57:52 +03004556 wl_list_init(&window->redraw_task.link);
4557 window->redraw_task_scheduled = 0;
4558
4559 if (window->resize_needed) {
4560 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004561 if (window->main_surface->frame_cb) {
4562 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004563 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004564 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004565
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004566 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004567 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004568 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004569
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004570 if (surface_redraw(window->main_surface) < 0) {
4571 /*
4572 * Only main_surface failure will cause us to undo the resize.
4573 * If sub-surfaces fail, they will just be broken with old
4574 * content.
4575 */
4576 failed = 1;
4577 } else {
4578 wl_list_for_each(surface, &window->subsurface_list, link) {
4579 if (surface == window->main_surface)
4580 continue;
4581
4582 surface_redraw(surface);
4583 }
4584 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004585
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004586 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004587 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004588
4589 wl_list_for_each(surface, &window->subsurface_list, link)
4590 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004591
4592 if (resized && failed) {
4593 /* Restore widget tree to correspond to what is on screen. */
4594 undo_resize(window);
4595 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004596}
4597
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004598static void
4599window_schedule_redraw_task(struct window *window)
4600{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004601 if (window->redraw_inhibited)
4602 return;
4603
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004604 if (!window->redraw_task_scheduled) {
4605 window->redraw_task.run = idle_redraw;
4606 display_defer(window->display, &window->redraw_task);
4607 window->redraw_task_scheduled = 1;
4608 }
4609}
4610
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004611void
4612window_schedule_redraw(struct window *window)
4613{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004614 struct surface *surface;
4615
Pekka Paalanen71233882013-04-25 13:57:53 +03004616 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4617
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004618 wl_list_for_each(surface, &window->subsurface_list, link)
4619 surface->redraw_needed = 1;
4620
4621 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004622}
4623
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004624int
4625window_is_fullscreen(struct window *window)
4626{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004627 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004628}
4629
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004630void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004631window_set_fullscreen(struct window *window, int fullscreen)
4632{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004633 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004634 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004635
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004636 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004637 return;
4638
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004639 if (fullscreen)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004640 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004641 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004642 zxdg_toplevel_v6_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004643}
4644
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004645int
4646window_is_maximized(struct window *window)
4647{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004648 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004649}
4650
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004651void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004652window_set_maximized(struct window *window, int maximized)
4653{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004654 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004655 return;
4656
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004657 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004658 return;
4659
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004660 if (maximized)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004661 zxdg_toplevel_v6_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004662 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004663 zxdg_toplevel_v6_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004664}
4665
Jasper St. Pierrede680992014-04-10 17:23:49 -07004666int
4667window_is_resizing(struct window *window)
4668{
4669 return window->resizing;
4670}
4671
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004672void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004673window_set_minimized(struct window *window)
4674{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004675 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004676 return;
4677
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004678 zxdg_toplevel_v6_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004679}
4680
4681void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004682window_set_user_data(struct window *window, void *data)
4683{
4684 window->user_data = data;
4685}
4686
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004687void *
4688window_get_user_data(struct window *window)
4689{
4690 return window->user_data;
4691}
4692
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004693void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004694window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004695 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004696{
4697 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004698}
4699
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004700void
4701window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004702 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004703{
4704 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004705}
4706
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004707void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004708window_set_data_handler(struct window *window, window_data_handler_t handler)
4709{
4710 window->data_handler = handler;
4711}
4712
4713void
4714window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4715{
4716 window->drop_handler = handler;
4717}
4718
4719void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004720window_set_close_handler(struct window *window,
4721 window_close_handler_t handler)
4722{
4723 window->close_handler = handler;
4724}
4725
4726void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004727window_set_fullscreen_handler(struct window *window,
4728 window_fullscreen_handler_t handler)
4729{
4730 window->fullscreen_handler = handler;
4731}
4732
4733void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004734window_set_output_handler(struct window *window,
4735 window_output_handler_t handler)
4736{
4737 window->output_handler = handler;
4738}
4739
4740void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004741window_set_state_changed_handler(struct window *window,
4742 window_state_changed_handler_t handler)
4743{
4744 window->state_changed_handler = handler;
4745}
4746
4747void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004748window_set_pointer_locked_handler(struct window *window,
4749 locked_pointer_locked_handler_t locked,
4750 locked_pointer_unlocked_handler_t unlocked)
4751{
4752 window->pointer_unlocked_handler = unlocked;
4753 window->pointer_locked_handler = locked;
4754}
4755
4756void
4757window_set_pointer_confined_handler(struct window *window,
4758 confined_pointer_confined_handler_t confined,
4759 confined_pointer_unconfined_handler_t unconfined)
4760{
4761 window->pointer_confined_handler = confined;
4762 window->pointer_unconfined_handler = unconfined;
4763}
4764
4765void
4766window_set_locked_pointer_motion_handler(struct window *window,
4767 window_locked_pointer_motion_handler_t handler)
4768{
4769 window->locked_pointer_motion_handler = handler;
4770}
4771
4772void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004773window_set_title(struct window *window, const char *title)
4774{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004775 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004776 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004777 if (window->frame) {
4778 frame_set_title(window->frame->frame, title);
4779 widget_schedule_redraw(window->frame->widget);
4780 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004781 if (window->xdg_toplevel)
4782 zxdg_toplevel_v6_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004783}
4784
4785const char *
4786window_get_title(struct window *window)
4787{
4788 return window->title;
4789}
4790
4791void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004792window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4793{
4794 struct text_cursor_position *text_cursor_position =
4795 window->display->text_cursor_position;
4796
Scott Moreau9295ce02012-06-01 12:46:10 -06004797 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004798 return;
4799
4800 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004801 window->main_surface->surface,
4802 wl_fixed_from_int(x),
4803 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004804}
4805
Casey Dahlin9074db52012-04-19 22:50:09 -04004806static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004807relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4808 uint32_t utime_hi,
4809 uint32_t utime_lo,
4810 wl_fixed_t dx,
4811 wl_fixed_t dy,
4812 wl_fixed_t dx_unaccel,
4813 wl_fixed_t dy_unaccel)
4814{
4815 struct input *input = data;
4816 struct window *window = input->pointer_focus;
4817 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4818
4819 if (window->locked_pointer_motion_handler &&
4820 window->pointer_locked) {
4821 window->locked_pointer_motion_handler(
4822 window, input, ms,
4823 wl_fixed_to_double(dx),
4824 wl_fixed_to_double(dy),
4825 window->user_data);
4826 }
4827}
4828
4829static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4830 relative_pointer_handle_motion,
4831};
4832
4833static void
4834locked_pointer_locked(void *data,
4835 struct zwp_locked_pointer_v1 *locked_pointer)
4836{
4837 struct input *input = data;
4838 struct window *window = input->pointer_focus;
4839
4840 window->pointer_locked = true;
4841
4842 if (window->pointer_locked_handler) {
4843 window->pointer_locked_handler(window,
4844 input,
4845 window->user_data);
4846 }
4847}
4848
4849static void
4850locked_pointer_unlocked(void *data,
4851 struct zwp_locked_pointer_v1 *locked_pointer)
4852{
4853 struct input *input = data;
4854 struct window *window = input->pointer_focus;
4855
4856 window_unlock_pointer(window);
4857
4858 if (window->pointer_unlocked_handler) {
4859 window->pointer_unlocked_handler(window,
4860 input,
4861 window->user_data);
4862 }
4863}
4864
4865static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4866 locked_pointer_locked,
4867 locked_pointer_unlocked,
4868};
4869
4870int
4871window_lock_pointer(struct window *window, struct input *input)
4872{
4873 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4874 window->display->relative_pointer_manager;
4875 struct zwp_pointer_constraints_v1 *pointer_constraints =
4876 window->display->pointer_constraints;
4877 struct zwp_relative_pointer_v1 *relative_pointer;
4878 struct zwp_locked_pointer_v1 *locked_pointer;
4879
4880 if (!window->display->relative_pointer_manager)
4881 return -1;
4882
4883 if (!window->display->pointer_constraints)
4884 return -1;
4885
4886 if (window->locked_pointer)
4887 return -1;
4888
4889 if (window->confined_pointer)
4890 return -1;
4891
4892 if (!input->pointer)
4893 return -1;
4894
4895 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4896 relative_pointer_manager, input->pointer);
4897 zwp_relative_pointer_v1_add_listener(relative_pointer,
4898 &relative_pointer_listener,
4899 input);
4900
4901 locked_pointer =
4902 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4903 window->main_surface->surface,
4904 input->pointer,
4905 NULL,
4906 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4907 zwp_locked_pointer_v1_add_listener(locked_pointer,
4908 &locked_pointer_listener,
4909 input);
4910
4911 window->locked_input = input;
4912 window->locked_pointer = locked_pointer;
4913 window->relative_pointer = relative_pointer;
4914
4915 return 0;
4916}
4917
4918void
4919window_unlock_pointer(struct window *window)
4920{
4921 if (!window->locked_pointer)
4922 return;
4923
4924 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4925 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4926 window->locked_pointer = NULL;
4927 window->relative_pointer = NULL;
4928 window->pointer_locked = false;
4929 window->locked_input = NULL;
4930}
4931
4932void
4933widget_set_locked_pointer_cursor_hint(struct widget *widget,
4934 float x, float y)
4935{
4936 struct window *window = widget->window;
4937
4938 if (!window->locked_pointer)
4939 return;
4940
4941 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4942 wl_fixed_from_double(x),
4943 wl_fixed_from_double(y));
4944 wl_surface_commit(window->main_surface->surface);
4945}
4946
4947static void
4948confined_pointer_confined(void *data,
4949 struct zwp_confined_pointer_v1 *confined_pointer)
4950{
4951 struct input *input = data;
4952 struct window *window = input->pointer_focus;
4953
4954 window->confined = true;
4955
4956 if (window->pointer_confined_handler) {
4957 window->pointer_confined_handler(window,
4958 input,
4959 window->user_data);
4960 }
4961}
4962
4963static void
4964confined_pointer_unconfined(void *data,
4965 struct zwp_confined_pointer_v1 *confined_pointer)
4966{
4967 struct input *input = data;
4968 struct window *window = input->pointer_focus;
4969
4970 window_unconfine_pointer(window);
4971
4972 window->confined = false;
4973
4974 if (window->pointer_unconfined_handler) {
4975 window->pointer_unconfined_handler(window,
4976 input,
4977 window->user_data);
4978 }
4979}
4980
4981static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4982 confined_pointer_confined,
4983 confined_pointer_unconfined,
4984};
4985
4986int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004987window_confine_pointer_to_rectangles(struct window *window,
4988 struct input *input,
4989 struct rectangle *rectangles,
4990 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004991{
4992 struct zwp_pointer_constraints_v1 *pointer_constraints =
4993 window->display->pointer_constraints;
4994 struct zwp_confined_pointer_v1 *confined_pointer;
4995 struct wl_compositor *compositor = window->display->compositor;
4996 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004997 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004998
4999 if (!window->display->pointer_constraints)
5000 return -1;
5001
5002 if (window->locked_pointer)
5003 return -1;
5004
5005 if (window->confined_pointer)
5006 return -1;
5007
5008 if (!input->pointer)
5009 return -1;
5010
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005011 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005012 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005013 for (i = 0; i < num_rectangles; i++) {
5014 wl_region_add(region,
5015 rectangles[i].x,
5016 rectangles[i].y,
5017 rectangles[i].width,
5018 rectangles[i].height);
5019 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005020 }
5021
5022 confined_pointer =
5023 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5024 window->main_surface->surface,
5025 input->pointer,
5026 region,
5027 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5028 if (region)
5029 wl_region_destroy(region);
5030
5031 zwp_confined_pointer_v1_add_listener(confined_pointer,
5032 &confined_pointer_listener,
5033 input);
5034
5035 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005036 window->confined_widget = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005037
5038 return 0;
5039}
5040
5041void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005042window_update_confine_rectangles(struct window *window,
5043 struct rectangle *rectangles,
5044 int num_rectangles)
5045{
5046 struct wl_compositor *compositor = window->display->compositor;
5047 struct wl_region *region;
5048 int i;
5049
5050 region = wl_compositor_create_region(compositor);
5051 for (i = 0; i < num_rectangles; i++) {
5052 wl_region_add(region,
5053 rectangles[i].x,
5054 rectangles[i].y,
5055 rectangles[i].width,
5056 rectangles[i].height);
5057 }
5058
5059 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5060
5061 wl_region_destroy(region);
5062}
5063
5064int
5065window_confine_pointer_to_widget(struct window *window,
5066 struct widget *widget,
5067 struct input *input)
5068{
5069 int ret;
5070
5071 if (widget) {
5072 ret = window_confine_pointer_to_rectangles(window,
5073 input,
5074 &widget->allocation,
5075 1);
5076 window->confined_widget = widget;
5077 return ret;
5078 } else {
5079 return window_confine_pointer_to_rectangles(window,
5080 input,
5081 NULL,
5082 0);
5083 }
5084}
5085
5086void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005087window_unconfine_pointer(struct window *window)
5088{
5089 if (!window->confined_pointer)
5090 return;
5091
5092 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5093 window->confined_pointer = NULL;
5094 window->confined = false;
5095}
5096
5097static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005098surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005099 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005100{
Rob Bradford7507b572012-05-15 17:55:34 +01005101 struct window *window = data;
5102 struct output *output;
5103 struct output *output_found = NULL;
5104 struct window_output *window_output;
5105
5106 wl_list_for_each(output, &window->display->output_list, link) {
5107 if (output->output == wl_output) {
5108 output_found = output;
5109 break;
5110 }
5111 }
5112
5113 if (!output_found)
5114 return;
5115
Brian Lovinbc919262013-08-07 15:34:59 -07005116 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005117 window_output->output = output_found;
5118
5119 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005120
5121 if (window->output_handler)
5122 window->output_handler(window, output_found, 1,
5123 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005124}
5125
5126static void
5127surface_leave(void *data,
5128 struct wl_surface *wl_surface, struct wl_output *output)
5129{
Rob Bradford7507b572012-05-15 17:55:34 +01005130 struct window *window = data;
5131 struct window_output *window_output;
5132 struct window_output *window_output_found = NULL;
5133
5134 wl_list_for_each(window_output, &window->window_output_list, link) {
5135 if (window_output->output->output == output) {
5136 window_output_found = window_output;
5137 break;
5138 }
5139 }
5140
5141 if (window_output_found) {
5142 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005143
5144 if (window->output_handler)
5145 window->output_handler(window, window_output->output,
5146 0, window->user_data);
5147
Rob Bradford7507b572012-05-15 17:55:34 +01005148 free(window_output_found);
5149 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005150}
5151
5152static const struct wl_surface_listener surface_listener = {
5153 surface_enter,
5154 surface_leave
5155};
5156
Pekka Paalanen4e373742013-02-13 16:17:13 +02005157static struct surface *
5158surface_create(struct window *window)
5159{
5160 struct display *display = window->display;
5161 struct surface *surface;
5162
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005163 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005164 surface->window = window;
5165 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005166 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005167 wl_surface_add_listener(surface->surface, &surface_listener, window);
5168
Pekka Paalanen35e82632013-04-25 13:57:48 +03005169 wl_list_insert(&window->subsurface_list, &surface->link);
5170
Pekka Paalanen4e373742013-02-13 16:17:13 +02005171 return surface;
5172}
5173
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005174static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005175get_preferred_buffer_type(struct display *display)
5176{
5177#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005178 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005179 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5180#endif
5181
5182 return WINDOW_BUFFER_TYPE_SHM;
5183}
5184
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005185static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005186window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005187{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005188 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005189 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005190
Peter Huttererf3d62272013-08-08 11:57:05 +10005191 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005192 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005193 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005194
5195 surface = surface_create(window);
5196 window->main_surface = surface;
5197
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005198 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005199
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005200 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005201 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005202
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005203 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005204
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005205 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005206 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005207 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005208
Rob Bradford7507b572012-05-15 17:55:34 +01005209 wl_list_init (&window->window_output_list);
5210
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005211 return window;
5212}
5213
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005214struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005215window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005216{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005217 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005218 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005219
5220 window = window_create_internal(display, 0);
5221
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005222 if (window->display->xdg_shell) {
5223 window->xdg_surface =
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005224 zxdg_shell_v6_get_xdg_surface(window->display->xdg_shell,
5225 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005226 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005227
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005228 zxdg_surface_v6_add_listener(window->xdg_surface,
5229 &xdg_surface_listener, window);
5230
5231 window->xdg_toplevel =
5232 zxdg_surface_v6_get_toplevel(window->xdg_surface);
5233 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5234
5235 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
5236 &xdg_toplevel_listener, window);
5237
5238 window_inhibit_redraw(window);
5239
5240 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005241 } else if (display->ivi_application) {
5242 /* auto generation of ivi_id based on process id + basement of id */
5243 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5244 window->ivi_surface =
5245 ivi_application_surface_create(display->ivi_application,
5246 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005247 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005248
5249 ivi_surface_add_listener(window->ivi_surface,
5250 &ivi_surface_listener, window);
5251 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005252
5253 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005254}
5255
5256struct window *
5257window_create_custom(struct display *display)
5258{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005259 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005260}
5261
Jasper St. Pierre53686042013-12-09 15:26:25 -05005262void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005263window_set_parent(struct window *window,
5264 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005265{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005266 window->parent = parent_window;
5267 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005268}
5269
5270struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005271window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005272{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005273 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005274}
5275
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005276static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005277menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005278{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005279 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005280 int next;
5281
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005282 frame_interior(menu->frame, &x, &y, &width, &height);
5283 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005284 if (menu->current != next) {
5285 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005286 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005287 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005288}
5289
5290static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005291menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005292 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005293 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005294{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005295 struct menu *menu = data;
5296
5297 if (widget == menu->widget)
5298 menu_set_item(data, y);
5299
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005300 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005301}
5302
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005303static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005304menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005305 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005306{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005307 struct menu *menu = data;
5308
5309 if (widget == menu->widget)
5310 menu_set_item(data, y);
5311
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005312 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005313}
5314
5315static void
5316menu_leave_handler(struct widget *widget, struct input *input, void *data)
5317{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005318 struct menu *menu = data;
5319
5320 if (widget == menu->widget)
5321 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005322}
5323
5324static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005325menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005326 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005327 uint32_t button, enum wl_pointer_button_state state,
5328 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005329
5330{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005331 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005332
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005333 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5334 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005335 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005336 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005337 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005338 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005339 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005340 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005341 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005342 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005343}
5344
5345static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005346menu_touch_up_handler(struct widget *widget,
5347 struct input *input,
5348 uint32_t serial,
5349 uint32_t time,
5350 int32_t id,
5351 void *data)
5352{
5353 struct menu *menu = data;
5354
5355 input_ungrab(input);
5356 menu_destroy(menu);
5357}
5358
5359static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005360menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005361{
5362 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005363 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005364 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005365
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005366 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005367
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005368 frame_repaint(menu->frame, cr);
5369 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005370
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005371 theme_set_background_source(menu->window->display->theme,
5372 cr, THEME_FRAME_ACTIVE);
5373 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005374 cairo_fill(cr);
5375
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005376 cairo_select_font_face(cr, "sans",
5377 CAIRO_FONT_SLANT_NORMAL,
5378 CAIRO_FONT_WEIGHT_NORMAL);
5379 cairo_set_font_size(cr, 12);
5380
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005381 for (i = 0; i < menu->count; i++) {
5382 if (i == menu->current) {
5383 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005384 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005385 cairo_fill(cr);
5386 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005387 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005388 cairo_show_text(cr, menu->entries[i]);
5389 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005390 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5391 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005392 cairo_show_text(cr, menu->entries[i]);
5393 }
5394 }
5395
5396 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005397}
5398
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005399static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005400xdg_popup_handle_configure(void *data,
5401 struct zxdg_popup_v6 *zxdg_popup_v6,
5402 int32_t x,
5403 int32_t y,
5404 int32_t width,
5405 int32_t height)
5406{
5407}
5408
5409static void
5410xdg_popup_handle_popup_done(void *data, struct zxdg_popup_v6 *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005411{
5412 struct window *window = data;
5413 struct menu *menu = window->main_surface->widget->user_data;
5414
5415 input_ungrab(menu->input);
5416 menu_destroy(menu);
5417}
5418
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005419static const struct zxdg_popup_v6_listener xdg_popup_listener = {
5420 xdg_popup_handle_configure,
5421 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005422};
5423
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005424static struct menu *
5425create_menu(struct display *display,
5426 struct input *input, uint32_t time,
5427 menu_func_t func, const char **entries, int count,
5428 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005429{
5430 struct window *window;
5431 struct menu *menu;
5432
5433 menu = malloc(sizeof *menu);
5434 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005435 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005436
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005437 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005438 if (!window) {
5439 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005440 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005441 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005442
5443 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005444 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005445 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005446 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07005447 FRAME_BUTTON_NONE, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005448 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005449 menu->entries = entries;
5450 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005451 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005452 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005453 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005454 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005455 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005456
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005457 input_ungrab(input);
5458
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005459 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005460 widget_set_enter_handler(menu->widget, menu_enter_handler);
5461 widget_set_leave_handler(menu->widget, menu_leave_handler);
5462 widget_set_motion_handler(menu->widget, menu_motion_handler);
5463 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005464 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005465
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005466 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005467 frame_resize_inside(menu->frame, 200, count * 20);
5468 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5469 window_schedule_resize(window, frame_width(menu->frame),
5470 frame_height(menu->frame));
5471
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005472 return menu;
5473}
5474
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005475static struct zxdg_positioner_v6 *
5476create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005477 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005478{
5479 struct zxdg_positioner_v6 *positioner;
5480
5481 positioner = zxdg_shell_v6_create_positioner(display->xdg_shell);
5482 fail_on_null(positioner, 0, __FILE__, __LINE__);
5483 zxdg_positioner_v6_set_anchor_rect(positioner, x, y, 1, 1);
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005484 zxdg_positioner_v6_set_size(positioner, w, h);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005485 zxdg_positioner_v6_set_anchor(positioner,
5486 ZXDG_POSITIONER_V6_ANCHOR_TOP |
5487 ZXDG_POSITIONER_V6_ANCHOR_LEFT);
5488 zxdg_positioner_v6_set_gravity(positioner,
5489 ZXDG_POSITIONER_V6_ANCHOR_BOTTOM |
5490 ZXDG_POSITIONER_V6_ANCHOR_RIGHT);
5491
5492 return positioner;
5493}
5494
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005495void
5496window_show_menu(struct display *display,
5497 struct input *input, uint32_t time, struct window *parent,
5498 int32_t x, int32_t y,
5499 menu_func_t func, const char **entries, int count)
5500{
5501 struct menu *menu;
5502 struct window *window;
5503 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005504 struct rectangle parent_geometry;
5505 struct zxdg_positioner_v6 *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005506
5507 menu = create_menu(display, input, time, func, entries, count, parent);
5508
5509 if (menu == NULL)
5510 return;
5511
5512 window = menu->window;
5513
5514 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5515 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5516
5517 window->x = x;
5518 window->y = y;
5519
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005520 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005521 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005522
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005523 if (!display->xdg_shell)
5524 return;
5525
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005526 window->xdg_surface =
5527 zxdg_shell_v6_get_xdg_surface(display->xdg_shell,
5528 window->main_surface->surface);
5529 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005530
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005531 zxdg_surface_v6_add_listener(window->xdg_surface,
5532 &xdg_surface_listener, window);
5533
5534 positioner = create_simple_positioner(display,
5535 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005536 window->y - (iy + parent_geometry.y),
5537 frame_width(menu->frame),
5538 frame_height(menu->frame));
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005539 window->xdg_popup =
5540 zxdg_surface_v6_get_popup(window->xdg_surface,
5541 parent->xdg_surface,
5542 positioner);
5543 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
5544 zxdg_positioner_v6_destroy(positioner);
5545 zxdg_popup_v6_grab(window->xdg_popup,
5546 input->seat,
5547 display_get_serial(window->display));
5548 zxdg_popup_v6_add_listener(window->xdg_popup,
5549 &xdg_popup_listener, window);
5550
5551 window_inhibit_redraw(window);
5552
5553 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005554}
5555
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005556void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005557window_set_buffer_type(struct window *window, enum window_buffer_type type)
5558{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005559 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005560}
5561
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005562enum window_buffer_type
5563window_get_buffer_type(struct window *window)
5564{
5565 return window->main_surface->buffer_type;
5566}
5567
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005568void
5569window_set_preferred_format(struct window *window,
5570 enum preferred_format format)
5571{
5572 window->preferred_format = format;
5573}
5574
Pekka Paalanen35e82632013-04-25 13:57:48 +03005575struct widget *
5576window_add_subsurface(struct window *window, void *data,
5577 enum subsurface_mode default_mode)
5578{
5579 struct widget *widget;
5580 struct surface *surface;
5581 struct wl_surface *parent;
5582 struct wl_subcompositor *subcompo = window->display->subcompositor;
5583
Pekka Paalanen35e82632013-04-25 13:57:48 +03005584 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005585 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005586 widget = widget_create(window, surface, data);
5587 wl_list_init(&widget->link);
5588 surface->widget = widget;
5589
5590 parent = window->main_surface->surface;
5591 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5592 surface->surface,
5593 parent);
5594 surface->synchronized = 1;
5595
5596 switch (default_mode) {
5597 case SUBSURFACE_SYNCHRONIZED:
5598 surface->synchronized_default = 1;
5599 break;
5600 case SUBSURFACE_DESYNCHRONIZED:
5601 surface->synchronized_default = 0;
5602 break;
5603 default:
5604 assert(!"bad enum subsurface_mode");
5605 }
5606
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005607 window->resize_needed = 1;
5608 window_schedule_redraw(window);
5609
Pekka Paalanen35e82632013-04-25 13:57:48 +03005610 return widget;
5611}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005612
5613static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005614display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005615 struct wl_output *wl_output,
5616 int x, int y,
5617 int physical_width,
5618 int physical_height,
5619 int subpixel,
5620 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005621 const char *model,
5622 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005623{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005624 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005625
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005626 output->allocation.x = x;
5627 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005628 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005629
5630 if (output->make)
5631 free(output->make);
5632 output->make = strdup(make);
5633
5634 if (output->model)
5635 free(output->model);
5636 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005637}
5638
5639static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005640display_handle_done(void *data,
5641 struct wl_output *wl_output)
5642{
5643}
5644
5645static void
5646display_handle_scale(void *data,
5647 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005648 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005649{
5650 struct output *output = data;
5651
5652 output->scale = scale;
5653}
5654
5655static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005656display_handle_mode(void *data,
5657 struct wl_output *wl_output,
5658 uint32_t flags,
5659 int width,
5660 int height,
5661 int refresh)
5662{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005663 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005664 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005665
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005666 if (flags & WL_OUTPUT_MODE_CURRENT) {
5667 output->allocation.width = width;
5668 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005669 if (display->output_configure_handler)
5670 (*display->output_configure_handler)(
5671 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005672 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005673}
5674
5675static const struct wl_output_listener output_listener = {
5676 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005677 display_handle_mode,
5678 display_handle_done,
5679 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005680};
5681
5682static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005683display_add_output(struct display *d, uint32_t id)
5684{
5685 struct output *output;
5686
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005687 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005688 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005689 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005690 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005691 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005692 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005693 wl_list_insert(d->output_list.prev, &output->link);
5694
5695 wl_output_add_listener(output->output, &output_listener, output);
5696}
5697
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005698static void
5699output_destroy(struct output *output)
5700{
5701 if (output->destroy_handler)
5702 (*output->destroy_handler)(output, output->user_data);
5703
5704 wl_output_destroy(output->output);
5705 wl_list_remove(&output->link);
5706 free(output);
5707}
5708
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005709static void
5710display_destroy_output(struct display *d, uint32_t id)
5711{
5712 struct output *output;
5713
5714 wl_list_for_each(output, &d->output_list, link) {
5715 if (output->server_output_id == id) {
5716 output_destroy(output);
5717 break;
5718 }
5719 }
5720}
5721
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005722void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005723display_set_global_handler(struct display *display,
5724 display_global_handler_t handler)
5725{
5726 struct global *global;
5727
5728 display->global_handler = handler;
5729 if (!handler)
5730 return;
5731
5732 wl_list_for_each(global, &display->global_list, link)
5733 display->global_handler(display,
5734 global->name, global->interface,
5735 global->version, display->user_data);
5736}
5737
5738void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005739display_set_global_handler_remove(struct display *display,
5740 display_global_handler_t remove_handler)
5741{
5742 display->global_handler_remove = remove_handler;
5743 if (!remove_handler)
5744 return;
5745}
5746
5747void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005748display_set_output_configure_handler(struct display *display,
5749 display_output_handler_t handler)
5750{
5751 struct output *output;
5752
5753 display->output_configure_handler = handler;
5754 if (!handler)
5755 return;
5756
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005757 wl_list_for_each(output, &display->output_list, link) {
5758 if (output->allocation.width == 0 &&
5759 output->allocation.height == 0)
5760 continue;
5761
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005762 (*display->output_configure_handler)(output,
5763 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005764 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005765}
5766
5767void
5768output_set_user_data(struct output *output, void *data)
5769{
5770 output->user_data = data;
5771}
5772
5773void *
5774output_get_user_data(struct output *output)
5775{
5776 return output->user_data;
5777}
5778
5779void
5780output_set_destroy_handler(struct output *output,
5781 display_output_handler_t handler)
5782{
5783 output->destroy_handler = handler;
5784 /* FIXME: implement this, once we have way to remove outputs */
5785}
5786
5787void
Scott Moreau4e072362012-09-29 02:03:11 -06005788output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005789{
Scott Moreau4e072362012-09-29 02:03:11 -06005790 struct rectangle allocation = output->allocation;
5791
5792 switch (output->transform) {
5793 case WL_OUTPUT_TRANSFORM_90:
5794 case WL_OUTPUT_TRANSFORM_270:
5795 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5796 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5797 /* Swap width and height */
5798 allocation.width = output->allocation.height;
5799 allocation.height = output->allocation.width;
5800 break;
5801 }
5802
5803 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005804}
5805
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005806struct wl_output *
5807output_get_wl_output(struct output *output)
5808{
5809 return output->output;
5810}
5811
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005812enum wl_output_transform
5813output_get_transform(struct output *output)
5814{
5815 return output->transform;
5816}
5817
Alexander Larssonafd319a2013-05-22 14:41:27 +02005818uint32_t
5819output_get_scale(struct output *output)
5820{
5821 return output->scale;
5822}
5823
Jason Ekstrand738715d2014-04-02 19:53:50 -05005824const char *
5825output_get_make(struct output *output)
5826{
5827 return output->make;
5828}
5829
5830const char *
5831output_get_model(struct output *output)
5832{
5833 return output->model;
5834}
5835
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005836static void
Daniel Stone97f68542012-05-30 16:32:01 +01005837fini_xkb(struct input *input)
5838{
5839 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005840 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005841}
5842
5843static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005844display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005845{
5846 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005847 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005848
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005849 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005850 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005851 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005852 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005853 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005854 input->pointer_focus = NULL;
5855 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005856 input->seat_version = seat_version;
5857
Rusty Lynch041815a2013-08-08 21:20:38 -07005858 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005859 wl_list_insert(d->input_list.prev, &input->link);
5860
Daniel Stone37816df2012-05-16 18:45:18 +01005861 wl_seat_add_listener(input->seat, &seat_listener, input);
5862 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005863
Jason Ekstranda669bd52014-04-02 19:53:51 -05005864 if (d->data_device_manager) {
5865 input->data_device =
5866 wl_data_device_manager_get_data_device(d->data_device_manager,
5867 input->seat);
5868 wl_data_device_add_listener(input->data_device,
5869 &data_device_listener,
5870 input);
5871 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005872
5873 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005874 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005875
Derek Foreman118a4292015-04-22 17:23:35 -05005876 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5877 TFD_CLOEXEC | TFD_NONBLOCK);
5878 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5879 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005880 set_repeat_info(input, 40, 400);
5881
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005882 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5883 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005884 input->repeat_task.run = keyboard_repeat_func;
5885 display_watch_fd(d, input->repeat_timer_fd,
5886 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005887}
5888
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005889static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005890input_destroy(struct input *input)
5891{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005892 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005893 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005894
5895 if (input->drag_offer)
5896 data_offer_destroy(input->drag_offer);
5897
5898 if (input->selection_offer)
5899 data_offer_destroy(input->selection_offer);
5900
kabeer khan6ce67ec2014-10-20 11:55:29 +05305901 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005902 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305903 wl_data_device_release(input->data_device);
5904 else
5905 wl_data_device_destroy(input->data_device);
5906 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005907 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005908 if (input->touch)
5909 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005910 if (input->pointer)
5911 wl_pointer_release(input->pointer);
5912 if (input->keyboard)
5913 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005914 } else {
5915 if (input->touch)
5916 wl_touch_destroy(input->touch);
5917 if (input->pointer)
5918 wl_pointer_destroy(input->pointer);
5919 if (input->keyboard)
5920 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005921 }
5922
Daniel Stone97f68542012-05-30 16:32:01 +01005923 fini_xkb(input);
5924
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005925 wl_surface_destroy(input->pointer_surface);
5926
Pekka Paalanene1207c72011-12-16 12:02:09 +02005927 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005928 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005929 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005930 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005931 free(input);
5932}
5933
5934static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005935shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5936{
5937 struct display *d = data;
5938
5939 if (format == WL_SHM_FORMAT_RGB565)
5940 d->has_rgb565 = 1;
5941}
5942
5943struct wl_shm_listener shm_listener = {
5944 shm_format
5945};
5946
5947static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005948xdg_shell_handle_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005949{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005950 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005951}
5952
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005953static const struct zxdg_shell_v6_listener xdg_shell_listener = {
5954 xdg_shell_handle_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005955};
5956
5957static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005958registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5959 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005960{
5961 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005962 struct global *global;
5963
Brian Lovinbc919262013-08-07 15:34:59 -07005964 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005965 global->name = id;
5966 global->interface = strdup(interface);
5967 global->version = version;
5968 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005969
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005970 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005971 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005972 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005973 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005974 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005975 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005976 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005977 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5978 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5979 d->relative_pointer_manager =
5980 wl_registry_bind(registry, id,
5981 &zwp_relative_pointer_manager_v1_interface,
5982 1);
5983 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5984 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5985 d->pointer_constraints =
5986 wl_registry_bind(registry, id,
5987 &zwp_pointer_constraints_v1_interface,
5988 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005989 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005990 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005991 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005992 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005993 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005994 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005995 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305996 &wl_data_device_manager_interface,
5997 d->data_device_manager_version);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005998 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005999 d->xdg_shell = wl_registry_bind(registry, id,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006000 &zxdg_shell_v6_interface, 1);
6001 zxdg_shell_v6_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06006002 } else if (strcmp(interface, "text_cursor_position") == 0) {
6003 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006004 wl_registry_bind(registry, id,
6005 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006006 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6007 d->subcompositor =
6008 wl_registry_bind(registry, id,
6009 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006010 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006011 else if (strcmp(interface, "ivi_application") == 0) {
6012 d->ivi_application =
6013 wl_registry_bind(registry, id,
6014 &ivi_application_interface, 1);
6015 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006016
6017 if (d->global_handler)
6018 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006019}
6020
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006021static void
6022registry_handle_global_remove(void *data, struct wl_registry *registry,
6023 uint32_t name)
6024{
6025 struct display *d = data;
6026 struct global *global;
6027 struct global *tmp;
6028
6029 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6030 if (global->name != name)
6031 continue;
6032
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006033 if (strcmp(global->interface, "wl_output") == 0)
6034 display_destroy_output(d, name);
6035
6036 /* XXX: Should destroy remaining bound globals */
6037
6038 if (d->global_handler_remove)
6039 d->global_handler_remove(d, name, global->interface,
6040 global->version, d->user_data);
6041
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006042 wl_list_remove(&global->link);
6043 free(global->interface);
6044 free(global);
6045 }
6046}
6047
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006048void *
6049display_bind(struct display *display, uint32_t name,
6050 const struct wl_interface *interface, uint32_t version)
6051{
6052 return wl_registry_bind(display->registry, name, interface, version);
6053}
6054
6055static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006056 registry_handle_global,
6057 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006058};
6059
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006060#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006061static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006062init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006063{
6064 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006065 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006066
Rob Clark6396ed32012-03-11 19:48:41 -05006067#ifdef USE_CAIRO_GLESV2
6068# define GL_BIT EGL_OPENGL_ES2_BIT
6069#else
6070# define GL_BIT EGL_OPENGL_BIT
6071#endif
6072
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006073 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006074 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006075 EGL_RED_SIZE, 1,
6076 EGL_GREEN_SIZE, 1,
6077 EGL_BLUE_SIZE, 1,
6078 EGL_ALPHA_SIZE, 1,
6079 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006080 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006081 EGL_NONE
6082 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006083
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006084#ifdef USE_CAIRO_GLESV2
6085 static const EGLint context_attribs[] = {
6086 EGL_CONTEXT_CLIENT_VERSION, 2,
6087 EGL_NONE
6088 };
6089 EGLint api = EGL_OPENGL_ES_API;
6090#else
6091 EGLint *context_attribs = NULL;
6092 EGLint api = EGL_OPENGL_API;
6093#endif
6094
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006095 d->dpy =
6096 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6097 d->display, NULL);
6098
Yuval Fledel45568f62010-12-06 09:18:12 -05006099 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006100 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006101 return -1;
6102 }
6103
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006104 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006105 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006106 return -1;
6107 }
6108
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006109 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6110 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006111 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006112 return -1;
6113 }
6114
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006115 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006116 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006117 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006118 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006119 return -1;
6120 }
6121
Benjamin Franzke0c991632011-09-27 21:57:31 +02006122 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6123 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006124 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006125 return -1;
6126 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006127
6128 return 0;
6129}
6130
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006131static void
6132fini_egl(struct display *display)
6133{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006134 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006135
6136 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6137 EGL_NO_CONTEXT);
6138
6139 eglTerminate(display->dpy);
6140 eglReleaseThread();
6141}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006142#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006143
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006144static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006145init_dummy_surface(struct display *display)
6146{
6147 int len;
6148 void *data;
6149
6150 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006151 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006152 display->dummy_surface =
6153 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6154 1, 1, len);
6155 display->dummy_surface_data = data;
6156}
6157
6158static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006159handle_display_data(struct task *task, uint32_t events)
6160{
6161 struct display *display =
6162 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006163 struct epoll_event ep;
6164 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006165
6166 display->display_fd_events = events;
6167
6168 if (events & EPOLLERR || events & EPOLLHUP) {
6169 display_exit(display);
6170 return;
6171 }
6172
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006173 if (events & EPOLLIN) {
6174 ret = wl_display_dispatch(display->display);
6175 if (ret == -1) {
6176 display_exit(display);
6177 return;
6178 }
6179 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006180
6181 if (events & EPOLLOUT) {
6182 ret = wl_display_flush(display->display);
6183 if (ret == 0) {
6184 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6185 ep.data.ptr = &display->display_task;
6186 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6187 display->display_fd, &ep);
6188 } else if (ret == -1 && errno != EAGAIN) {
6189 display_exit(display);
6190 return;
6191 }
6192 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006193}
6194
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006195static void
6196log_handler(const char *format, va_list args)
6197{
6198 vfprintf(stderr, format, args);
6199}
6200
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006201struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006202display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006203{
6204 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006205
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006206 wl_log_set_handler_client(log_handler);
6207
Peter Huttererf3d62272013-08-08 11:57:05 +10006208 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006209 if (d == NULL)
6210 return NULL;
6211
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006212 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006213 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006214 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006215 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006216 return NULL;
6217 }
6218
Rob Bradford5ab9c752013-07-26 16:29:43 +01006219 d->xkb_context = xkb_context_new(0);
6220 if (d->xkb_context == NULL) {
6221 fprintf(stderr, "Failed to create XKB context\n");
6222 free(d);
6223 return NULL;
6224 }
6225
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006226 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006227 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006228 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006229 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6230 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006231
6232 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006233 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006234 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006235 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006236
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006237 d->registry = wl_display_get_registry(d->display);
6238 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006239
Marek Chalupaa519d062014-12-05 13:49:40 +01006240 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006241 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6242 return NULL;
6243 }
6244
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006245#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006246 if (init_egl(d) < 0)
6247 fprintf(stderr, "EGL does not seem to work, "
6248 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006249#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006250
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006251 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006252
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006253 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006254
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006255 wl_list_init(&d->window_list);
6256
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006257 init_dummy_surface(d);
6258
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006259 return d;
6260}
6261
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006262static void
6263display_destroy_outputs(struct display *display)
6264{
6265 struct output *tmp;
6266 struct output *output;
6267
6268 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6269 output_destroy(output);
6270}
6271
Pekka Paalanene1207c72011-12-16 12:02:09 +02006272static void
6273display_destroy_inputs(struct display *display)
6274{
6275 struct input *tmp;
6276 struct input *input;
6277
6278 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6279 input_destroy(input);
6280}
6281
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006282void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006283display_destroy(struct display *display)
6284{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006285 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006286 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6287 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006288
6289 if (!wl_list_empty(&display->deferred_list))
6290 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6291
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006292 cairo_surface_destroy(display->dummy_surface);
6293 free(display->dummy_surface_data);
6294
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006295 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006296 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006297
Daniel Stone97f68542012-05-30 16:32:01 +01006298 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006299
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006300 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006301 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006302
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006303#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006304 if (display->argb_device)
6305 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006306#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006307
Pekka Paalanen35e82632013-04-25 13:57:48 +03006308 if (display->subcompositor)
6309 wl_subcompositor_destroy(display->subcompositor);
6310
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006311 if (display->xdg_shell)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006312 zxdg_shell_v6_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006313
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006314 if (display->ivi_application)
6315 ivi_application_destroy(display->ivi_application);
6316
Pekka Paalanenc2052982011-12-16 11:41:32 +02006317 if (display->shm)
6318 wl_shm_destroy(display->shm);
6319
6320 if (display->data_device_manager)
6321 wl_data_device_manager_destroy(display->data_device_manager);
6322
6323 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006324 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006325
6326 close(display->epoll_fd);
6327
U. Artie Eoff44874d92012-10-02 21:12:35 -07006328 if (!(display->display_fd_events & EPOLLERR) &&
6329 !(display->display_fd_events & EPOLLHUP))
6330 wl_display_flush(display->display);
6331
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006332 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006333 free(display);
6334}
6335
6336void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006337display_set_user_data(struct display *display, void *data)
6338{
6339 display->user_data = data;
6340}
6341
6342void *
6343display_get_user_data(struct display *display)
6344{
6345 return display->user_data;
6346}
6347
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006348struct wl_display *
6349display_get_display(struct display *display)
6350{
6351 return display->display;
6352}
6353
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006354int
6355display_has_subcompositor(struct display *display)
6356{
6357 if (display->subcompositor)
6358 return 1;
6359
6360 wl_display_roundtrip(display->display);
6361
6362 return display->subcompositor != NULL;
6363}
6364
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006365cairo_device_t *
6366display_get_cairo_device(struct display *display)
6367{
6368 return display->argb_device;
6369}
6370
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006371struct output *
6372display_get_output(struct display *display)
6373{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006374 if (wl_list_empty(&display->output_list))
6375 return NULL;
6376
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006377 return container_of(display->output_list.next, struct output, link);
6378}
6379
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006380struct wl_compositor *
6381display_get_compositor(struct display *display)
6382{
6383 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006384}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006385
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006386uint32_t
6387display_get_serial(struct display *display)
6388{
6389 return display->serial;
6390}
6391
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006392EGLDisplay
6393display_get_egl_display(struct display *d)
6394{
6395 return d->dpy;
6396}
6397
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006398struct wl_data_source *
6399display_create_data_source(struct display *display)
6400{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006401 if (display->data_device_manager)
6402 return wl_data_device_manager_create_data_source(display->data_device_manager);
6403 else
6404 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006405}
6406
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006407EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006408display_get_argb_egl_config(struct display *d)
6409{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006410 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006411}
6412
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006413int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006414display_acquire_window_surface(struct display *display,
6415 struct window *window,
6416 EGLContext ctx)
6417{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006418 struct surface *surface = window->main_surface;
6419
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006420 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006421 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006422
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006423 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006424 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006425}
6426
6427void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006428display_release_window_surface(struct display *display,
6429 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006430{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006431 struct surface *surface = window->main_surface;
6432
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006433 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006434 return;
6435
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006436 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006437}
6438
6439void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006440display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006441{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006442 wl_list_insert(&display->deferred_list, &task->link);
6443}
6444
6445void
6446display_watch_fd(struct display *display,
6447 int fd, uint32_t events, struct task *task)
6448{
6449 struct epoll_event ep;
6450
6451 ep.events = events;
6452 ep.data.ptr = task;
6453 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6454}
6455
6456void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006457display_unwatch_fd(struct display *display, int fd)
6458{
6459 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6460}
6461
6462void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006463display_run(struct display *display)
6464{
6465 struct task *task;
6466 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006467 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006468
Pekka Paalanen826d7952011-12-15 10:14:07 +02006469 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006470 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006471 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006472 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006473 struct task, link);
6474 wl_list_remove(&task->link);
6475 task->run(task, 0);
6476 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006477
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006478 wl_display_dispatch_pending(display->display);
6479
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006480 if (!display->running)
6481 break;
6482
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006483 ret = wl_display_flush(display->display);
6484 if (ret < 0 && errno == EAGAIN) {
6485 ep[0].events =
6486 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6487 ep[0].data.ptr = &display->display_task;
6488
6489 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6490 display->display_fd, &ep[0]);
6491 } else if (ret < 0) {
6492 break;
6493 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006494
6495 count = epoll_wait(display->epoll_fd,
6496 ep, ARRAY_LENGTH(ep), -1);
6497 for (i = 0; i < count; i++) {
6498 task = ep[i].data.ptr;
6499 task->run(task, ep[i].events);
6500 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006501 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006502}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006503
6504void
6505display_exit(struct display *display)
6506{
6507 display->running = 0;
6508}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006509
Carlos Garnacho9c931792016-01-18 23:52:12 +01006510int
6511display_get_data_device_manager_version(struct display *display)
6512{
6513 return display->data_device_manager_version;
6514}
6515
Jan Arne Petersencd997062012-11-18 19:06:44 +01006516void
6517keysym_modifiers_add(struct wl_array *modifiers_map,
6518 const char *name)
6519{
6520 size_t len = strlen(name) + 1;
6521 char *p;
6522
6523 p = wl_array_add(modifiers_map, len);
6524
6525 if (p == NULL)
6526 return;
6527
6528 strncpy(p, name, len);
6529}
6530
6531static xkb_mod_index_t
6532keysym_modifiers_get_index(struct wl_array *modifiers_map,
6533 const char *name)
6534{
6535 xkb_mod_index_t index = 0;
6536 char *p = modifiers_map->data;
6537
6538 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6539 if (strcmp(p, name) == 0)
6540 return index;
6541
6542 index++;
6543 p += strlen(p) + 1;
6544 }
6545
6546 return XKB_MOD_INVALID;
6547}
6548
6549xkb_mod_mask_t
6550keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6551 const char *name)
6552{
6553 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6554
6555 if (index == XKB_MOD_INVALID)
6556 return XKB_MOD_INVALID;
6557
6558 return 1 << index;
6559}