blob: 84d585e53c2ae97d5213ec1b0728129ad4ab8962 [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);
648 eglDestroySurface(d->dpy, surface->egl_surface);
649 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
Rob Bradford5f087742013-07-11 19:41:27 +01002784 /* when making the window smaller - e.g. after a unmaximise we might
2785 * 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
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003111 if (sym == XKB_KEY_NoSymbol)
3112 return sym;
3113 if (xkb_compose_state_feed(input->xkb.compose_state,
3114 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3115 return sym;
3116
3117 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3118 case XKB_COMPOSE_COMPOSING:
3119 return XKB_KEY_NoSymbol;
3120 case XKB_COMPOSE_COMPOSED:
3121 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3122 case XKB_COMPOSE_CANCELLED:
3123 return XKB_KEY_NoSymbol;
3124 case XKB_COMPOSE_NOTHING:
3125 return sym;
3126 default:
3127 return sym;
3128 }
Daniel Stone5b015962016-10-20 14:45:58 +01003129#else
3130 return sym;
3131#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003132}
3133
Scott Moreau210d0792012-03-22 10:47:01 -06003134static void
Daniel Stone37816df2012-05-16 18:45:18 +01003135keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003136 uint32_t serial, uint32_t time, uint32_t key,
3137 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003138{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003139 struct input *input = data;
3140 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003141 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003142 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003143 const xkb_keysym_t *syms;
3144 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003145 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003146
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003147 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003148 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003149 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003150 return;
3151
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003152 /* We only use input grabs for pointer events for now, so just
3153 * ignore key presses if a grab is active. We expand the key
3154 * event delivery mechanism to route events to widgets to
3155 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003156 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003157 if (input->grab && input->grab_button == 0)
3158 return;
3159
Ran Benita2e1968f2014-08-19 23:59:51 +03003160 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003161
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003162 sym = XKB_KEY_NoSymbol;
3163 if (num_syms == 1)
3164 sym = syms[0];
3165
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003166
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003167 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003168 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003169 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003170 } else if (sym == XKB_KEY_F11 &&
3171 window->fullscreen_handler &&
3172 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3173 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003174 } else if (sym == XKB_KEY_F4 &&
3175 input->modifiers == MOD_ALT_MASK &&
3176 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003177 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003178 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003179 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3180 sym = process_key_press(sym, input);
3181
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003182 (*window->key_handler)(window, input, time, key,
3183 sym, state, window->user_data);
3184 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003185
3186 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3187 key == input->repeat_key) {
3188 its.it_interval.tv_sec = 0;
3189 its.it_interval.tv_nsec = 0;
3190 its.it_value.tv_sec = 0;
3191 its.it_value.tv_nsec = 0;
3192 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003193 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3194 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003195 input->repeat_sym = sym;
3196 input->repeat_key = key;
3197 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003198 its.it_interval.tv_sec = input->repeat_rate_sec;
3199 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3200 its.it_value.tv_sec = input->repeat_delay_sec;
3201 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003202 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3203 }
3204}
3205
3206static void
Daniel Stone351eb612012-05-31 15:27:47 -04003207keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3208 uint32_t serial, uint32_t mods_depressed,
3209 uint32_t mods_latched, uint32_t mods_locked,
3210 uint32_t group)
3211{
3212 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003213 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003214
Matt Ropere61561f2013-06-24 16:52:43 +01003215 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3216 if (!input->xkb.keymap)
3217 return;
3218
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003219 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3220 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003221 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003222 XKB_STATE_MODS_DEPRESSED |
3223 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003224 input->modifiers = 0;
3225 if (mask & input->xkb.control_mask)
3226 input->modifiers |= MOD_CONTROL_MASK;
3227 if (mask & input->xkb.alt_mask)
3228 input->modifiers |= MOD_ALT_MASK;
3229 if (mask & input->xkb.shift_mask)
3230 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003231}
3232
Jonny Lamb06959082014-08-12 14:58:27 +02003233static void
3234set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3235{
3236 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3237 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3238
3239 /* a rate of zero disables any repeating, regardless of the delay's
3240 * value */
3241 if (rate == 0)
3242 return;
3243
3244 if (rate == 1)
3245 input->repeat_rate_sec = 1;
3246 else
3247 input->repeat_rate_nsec = 1000000000 / rate;
3248
3249 input->repeat_delay_sec = delay / 1000;
3250 delay -= (input->repeat_delay_sec * 1000);
3251 input->repeat_delay_nsec = delay * 1000 * 1000;
3252}
3253
3254static void
3255keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3256 int32_t rate, int32_t delay)
3257{
3258 struct input *input = data;
3259
3260 set_repeat_info(input, rate, delay);
3261}
3262
Daniel Stone37816df2012-05-16 18:45:18 +01003263static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003264 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003265 keyboard_handle_enter,
3266 keyboard_handle_leave,
3267 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003268 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003269 keyboard_handle_repeat_info
3270
Daniel Stone37816df2012-05-16 18:45:18 +01003271};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003272
3273static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003274touch_handle_down(void *data, struct wl_touch *wl_touch,
3275 uint32_t serial, uint32_t time, struct wl_surface *surface,
3276 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3277{
3278 struct input *input = data;
3279 struct widget *widget;
3280 float sx = wl_fixed_to_double(x_w);
3281 float sy = wl_fixed_to_double(y_w);
3282
Rusty Lynch1084da52013-08-15 09:10:08 -07003283 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003284 input->touch_focus = wl_surface_get_user_data(surface);
3285 if (!input->touch_focus) {
3286 DBG("Failed to find to touch focus for surface %p\n", surface);
3287 return;
3288 }
3289
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003290 if (surface != input->touch_focus->main_surface->surface) {
3291 DBG("Ignoring input event from subsurface %p\n", surface);
3292 input->touch_focus = NULL;
3293 return;
3294 }
3295
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003296 if (input->grab)
3297 widget = input->grab;
3298 else
3299 widget = window_find_widget(input->touch_focus,
3300 wl_fixed_to_double(x_w),
3301 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003302 if (widget) {
3303 struct touch_point *tp = xmalloc(sizeof *tp);
3304 if (tp) {
3305 tp->id = id;
3306 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003307 tp->x = sx;
3308 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003309 wl_list_insert(&input->touch_point_list, &tp->link);
3310
3311 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003312 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003313 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003314 sx, sy,
3315 widget->user_data);
3316 }
3317 }
3318}
3319
3320static void
3321touch_handle_up(void *data, struct wl_touch *wl_touch,
3322 uint32_t serial, uint32_t time, int32_t id)
3323{
3324 struct input *input = data;
3325 struct touch_point *tp, *tmp;
3326
Rusty Lynch041815a2013-08-08 21:20:38 -07003327 if (!input->touch_focus) {
3328 DBG("No touch focus found for touch up event!\n");
3329 return;
3330 }
3331
3332 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3333 if (tp->id != id)
3334 continue;
3335
3336 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003337 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003338 time, id,
3339 tp->widget->user_data);
3340
3341 wl_list_remove(&tp->link);
3342 free(tp);
3343
3344 return;
3345 }
3346}
3347
3348static void
3349touch_handle_motion(void *data, struct wl_touch *wl_touch,
3350 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3351{
3352 struct input *input = data;
3353 struct touch_point *tp;
3354 float sx = wl_fixed_to_double(x_w);
3355 float sy = wl_fixed_to_double(y_w);
3356
3357 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3358
3359 if (!input->touch_focus) {
3360 DBG("No touch focus found for touch motion event!\n");
3361 return;
3362 }
3363
3364 wl_list_for_each(tp, &input->touch_point_list, link) {
3365 if (tp->id != id)
3366 continue;
3367
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003368 tp->x = sx;
3369 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003370 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003371 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003372 id, sx, sy,
3373 tp->widget->user_data);
3374 return;
3375 }
3376}
3377
3378static void
3379touch_handle_frame(void *data, struct wl_touch *wl_touch)
3380{
3381 struct input *input = data;
3382 struct touch_point *tp, *tmp;
3383
3384 DBG("touch_handle_frame\n");
3385
3386 if (!input->touch_focus) {
3387 DBG("No touch focus found for touch frame event!\n");
3388 return;
3389 }
3390
3391 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3392 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003393 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003394 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003395 }
3396}
3397
3398static void
3399touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3400{
3401 struct input *input = data;
3402 struct touch_point *tp, *tmp;
3403
3404 DBG("touch_handle_cancel\n");
3405
3406 if (!input->touch_focus) {
3407 DBG("No touch focus found for touch cancel event!\n");
3408 return;
3409 }
3410
3411 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3412 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003413 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3414 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003415
3416 wl_list_remove(&tp->link);
3417 free(tp);
3418 }
3419}
3420
3421static const struct wl_touch_listener touch_listener = {
3422 touch_handle_down,
3423 touch_handle_up,
3424 touch_handle_motion,
3425 touch_handle_frame,
3426 touch_handle_cancel,
3427};
3428
3429static void
Daniel Stone37816df2012-05-16 18:45:18 +01003430seat_handle_capabilities(void *data, struct wl_seat *seat,
3431 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003432{
Daniel Stone37816df2012-05-16 18:45:18 +01003433 struct input *input = data;
3434
3435 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3436 input->pointer = wl_seat_get_pointer(seat);
3437 wl_pointer_set_user_data(input->pointer, input);
3438 wl_pointer_add_listener(input->pointer, &pointer_listener,
3439 input);
3440 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003441 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003442 wl_pointer_release(input->pointer);
3443 else
3444 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003445 input->pointer = NULL;
3446 }
3447
3448 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3449 input->keyboard = wl_seat_get_keyboard(seat);
3450 wl_keyboard_set_user_data(input->keyboard, input);
3451 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3452 input);
3453 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003454 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003455 wl_keyboard_release(input->keyboard);
3456 else
3457 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003458 input->keyboard = NULL;
3459 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003460
3461 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3462 input->touch = wl_seat_get_touch(seat);
3463 wl_touch_set_user_data(input->touch, input);
3464 wl_touch_add_listener(input->touch, &touch_listener, input);
3465 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003466 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003467 wl_touch_release(input->touch);
3468 else
3469 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003470 input->touch = NULL;
3471 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003472}
3473
Rob Bradford08031182013-08-13 20:11:03 +01003474static void
3475seat_handle_name(void *data, struct wl_seat *seat,
3476 const char *name)
3477{
3478
3479}
3480
Daniel Stone37816df2012-05-16 18:45:18 +01003481static const struct wl_seat_listener seat_listener = {
3482 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003483 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003484};
3485
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003486void
3487input_get_position(struct input *input, int32_t *x, int32_t *y)
3488{
3489 *x = input->sx;
3490 *y = input->sy;
3491}
3492
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003493int
3494input_get_touch(struct input *input, int32_t id, float *x, float *y)
3495{
3496 struct touch_point *tp;
3497
3498 wl_list_for_each(tp, &input->touch_point_list, link) {
3499 if (tp->id != id)
3500 continue;
3501
3502 *x = tp->x;
3503 *y = tp->y;
3504 return 0;
3505 }
3506
3507 return -1;
3508}
3509
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003510struct display *
3511input_get_display(struct input *input)
3512{
3513 return input->display;
3514}
3515
Daniel Stone37816df2012-05-16 18:45:18 +01003516struct wl_seat *
3517input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003518{
Daniel Stone37816df2012-05-16 18:45:18 +01003519 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003520}
3521
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003522uint32_t
3523input_get_modifiers(struct input *input)
3524{
3525 return input->modifiers;
3526}
3527
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003528struct widget *
3529input_get_focus_widget(struct input *input)
3530{
3531 return input->focus_widget;
3532}
3533
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003534struct data_offer {
3535 struct wl_data_offer *offer;
3536 struct input *input;
3537 struct wl_array types;
3538 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003539
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003540 struct task io_task;
3541 int fd;
3542 data_func_t func;
3543 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003544 uint32_t dnd_action;
3545 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003546 void *user_data;
3547};
3548
3549static void
3550data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3551{
3552 struct data_offer *offer = data;
3553 char **p;
3554
3555 p = wl_array_add(&offer->types, sizeof *p);
3556 *p = strdup(type);
3557}
3558
Carlos Garnacho9c931792016-01-18 23:52:12 +01003559static void
3560data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3561{
3562 struct data_offer *offer = data;
3563
3564 offer->source_actions = source_actions;
3565}
3566
3567static void
3568data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3569{
3570 struct data_offer *offer = data;
3571
3572 offer->dnd_action = dnd_action;
3573}
3574
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003575static const struct wl_data_offer_listener data_offer_listener = {
3576 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003577 data_offer_source_actions,
3578 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003579};
3580
3581static void
3582data_offer_destroy(struct data_offer *offer)
3583{
3584 char **p;
3585
3586 offer->refcount--;
3587 if (offer->refcount == 0) {
3588 wl_data_offer_destroy(offer->offer);
3589 for (p = offer->types.data; *p; p++)
3590 free(*p);
3591 wl_array_release(&offer->types);
3592 free(offer);
3593 }
3594}
3595
3596static void
3597data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003598 struct wl_data_device *data_device,
3599 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003600{
3601 struct data_offer *offer;
3602
Brian Lovinbc919262013-08-07 15:34:59 -07003603 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003604
3605 wl_array_init(&offer->types);
3606 offer->refcount = 1;
3607 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003608 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003609 wl_data_offer_add_listener(offer->offer,
3610 &data_offer_listener, offer);
3611}
3612
3613static void
3614data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003615 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003616 wl_fixed_t x_w, wl_fixed_t y_w,
3617 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003618{
3619 struct input *input = data;
3620 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003621 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003622 float x = wl_fixed_to_double(x_w);
3623 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003624 char **p;
3625
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003626 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003627 input->drag_enter_serial = serial;
3628 input->drag_focus = window,
3629 input->drag_x = x;
3630 input->drag_y = y;
3631
3632 if (!input->touch_grab)
3633 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003634
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003635 if (offer) {
3636 input->drag_offer = wl_data_offer_get_user_data(offer);
3637
3638 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3639 *p = NULL;
3640
3641 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003642
3643 if (input->display->data_device_manager_version >=
3644 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3645 wl_data_offer_set_actions(offer,
3646 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3647 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3648 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3649 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003650 } else {
3651 input->drag_offer = NULL;
3652 types_data = NULL;
3653 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003654
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003655 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003656 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003657 window->user_data);
3658}
3659
3660static void
3661data_device_leave(void *data, struct wl_data_device *data_device)
3662{
3663 struct input *input = data;
3664
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003665 if (input->drag_offer) {
3666 data_offer_destroy(input->drag_offer);
3667 input->drag_offer = NULL;
3668 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003669}
3670
3671static void
3672data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003673 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003674{
3675 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003676 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003677 float x = wl_fixed_to_double(x_w);
3678 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003679 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003680
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003681 input->drag_x = x;
3682 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003683
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003684 if (input->drag_offer)
3685 types_data = input->drag_offer->types.data;
3686 else
3687 types_data = NULL;
3688
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003689 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003690 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003691 window->user_data);
3692}
3693
3694static void
3695data_device_drop(void *data, struct wl_data_device *data_device)
3696{
3697 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003698 struct window *window = input->drag_focus;
3699 float x, y;
3700
3701 x = input->drag_x;
3702 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003703
3704 if (window->drop_handler)
3705 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003706 x, y, window->user_data);
3707
3708 if (input->touch_grab)
3709 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003710}
3711
3712static void
3713data_device_selection(void *data,
3714 struct wl_data_device *wl_data_device,
3715 struct wl_data_offer *offer)
3716{
3717 struct input *input = data;
3718 char **p;
3719
3720 if (input->selection_offer)
3721 data_offer_destroy(input->selection_offer);
3722
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003723 if (offer) {
3724 input->selection_offer = wl_data_offer_get_user_data(offer);
3725 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3726 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003727 } else {
3728 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003729 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003730}
3731
3732static const struct wl_data_device_listener data_device_listener = {
3733 data_device_data_offer,
3734 data_device_enter,
3735 data_device_leave,
3736 data_device_motion,
3737 data_device_drop,
3738 data_device_selection
3739};
3740
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003741static void
3742input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003743{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003744 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003745 struct wl_cursor *cursor;
3746 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003747
Daniel Stone80972742012-11-07 17:51:39 +11003748 if (!input->pointer)
3749 return;
3750
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003751 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003752 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003753 return;
3754
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003755 if (index >= (int) cursor->image_count) {
3756 fprintf(stderr, "cursor index out of range\n");
3757 return;
3758 }
3759
3760 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003761 buffer = wl_cursor_image_get_buffer(image);
3762 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003763 return;
3764
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003765 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3766 wl_surface_damage(input->pointer_surface, 0, 0,
3767 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003768 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003769 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3770 input->pointer_surface,
3771 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003772}
3773
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003774static const struct wl_callback_listener pointer_surface_listener;
3775
Derek Foreman493d9792015-03-04 16:26:25 -06003776static bool
3777input_set_pointer_special(struct input *input)
3778{
3779 if (input->current_cursor == CURSOR_BLANK) {
3780 wl_pointer_set_cursor(input->pointer,
3781 input->pointer_enter_serial,
3782 NULL, 0, 0);
3783 return true;
3784 }
3785
3786 if (input->current_cursor == CURSOR_UNSET)
3787 return true;
3788
3789 return false;
3790}
3791
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003792static void
Derek Foreman118a4292015-04-22 17:23:35 -05003793schedule_pointer_image_update(struct input *input,
3794 struct wl_cursor *cursor,
3795 uint32_t duration,
3796 bool force_frame)
3797{
3798 /* Some silly cursor sets have enormous pauses in them. In these
3799 * cases it's better to use a timer even if it results in less
3800 * accurate presentation, since it will save us having to set the
3801 * same cursor image over and over again.
3802 *
3803 * This is really not the way we're supposed to time any kind of
3804 * animation, but we're pretending it's OK here because we don't
3805 * want animated cursors with long delays to needlessly hog CPU.
3806 *
3807 * We use force_frame to ensure we don't accumulate large timing
3808 * errors by running off the wrong clock.
3809 */
3810 if (!force_frame && duration > 100) {
3811 struct timespec tp;
3812
3813 clock_gettime(CLOCK_MONOTONIC, &tp);
3814 input->cursor_timer_start = tp.tv_sec * 1000
3815 + tp.tv_nsec / 1000000;
3816 cursor_delay_timer_reset(input, duration);
3817 return;
3818 }
3819
3820 /* for short durations we'll just spin on frame callbacks for
3821 * accurate timing - the way any kind of timing sensitive animation
3822 * should really be done. */
3823 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3824 wl_callback_add_listener(input->cursor_frame_cb,
3825 &pointer_surface_listener, input);
3826
3827}
3828
3829static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003830pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3831 uint32_t time)
3832{
3833 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003834 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003835 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003836 uint32_t duration;
3837 bool force_frame = true;
3838
3839 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003840
3841 if (callback) {
3842 assert(callback == input->cursor_frame_cb);
3843 wl_callback_destroy(callback);
3844 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003845 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003846 }
3847
Daniel Stone80972742012-11-07 17:51:39 +11003848 if (!input->pointer)
3849 return;
3850
Derek Foreman493d9792015-03-04 16:26:25 -06003851 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003852 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003853
Daniel Stonea494f1d2012-06-18 19:31:12 +01003854 cursor = input->display->cursors[input->current_cursor];
3855 if (!cursor)
3856 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003857
3858 /* FIXME We don't have the current time on the first call so we set
3859 * the animation start to the time of the first frame callback. */
3860 if (time == 0)
3861 input->cursor_anim_start = 0;
3862 else if (input->cursor_anim_start == 0)
3863 input->cursor_anim_start = time;
3864
Derek Foreman118a4292015-04-22 17:23:35 -05003865 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003866
Derek Foreman118a4292015-04-22 17:23:35 -05003867 if (time == 0 || input->cursor_anim_start == 0) {
3868 duration = 0;
3869 i = 0;
3870 } else
3871 i = wl_cursor_frame_and_duration(
3872 cursor,
3873 time - input->cursor_anim_start,
3874 &duration);
3875
3876 if (cursor->image_count > 1)
3877 schedule_pointer_image_update(input, cursor, duration,
3878 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003879
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003880 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003881}
3882
Derek Foreman118a4292015-04-22 17:23:35 -05003883static void
3884cursor_timer_func(struct task *task, uint32_t events)
3885{
3886 struct input *input = container_of(task, struct input, cursor_task);
3887 struct timespec tp;
3888 struct wl_cursor *cursor;
3889 uint32_t time;
3890 uint64_t exp;
3891
3892 if (!input->cursor_timer_running)
3893 return;
3894
3895 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3896 return;
3897
3898 cursor = input->display->cursors[input->current_cursor];
3899 if (!cursor)
3900 return;
3901
3902 clock_gettime(CLOCK_MONOTONIC, &tp);
3903 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3904 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3905}
3906
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003907static const struct wl_callback_listener pointer_surface_listener = {
3908 pointer_surface_frame_callback
3909};
3910
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003911void
3912input_set_pointer_image(struct input *input, int pointer)
3913{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003914 int force = 0;
3915
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003916 if (!input->pointer)
3917 return;
3918
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003919 if (input->pointer_enter_serial > input->cursor_serial)
3920 force = 1;
3921
3922 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003923 return;
3924
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003925 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003926 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003927 if (!input->cursor_frame_cb)
3928 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003929 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003930 /* The current frame callback may be stuck if, for instance,
3931 * the set cursor request was processed by the server after
3932 * this client lost the focus. In this case the cursor surface
3933 * might not be mapped and the frame callback wouldn't ever
3934 * complete. Send a set_cursor and attach to try to map the
3935 * cursor surface again so that the callback will finish */
3936 input_set_pointer_image_index(input, 0);
3937 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003938}
3939
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003940struct wl_data_device *
3941input_get_data_device(struct input *input)
3942{
3943 return input->data_device;
3944}
3945
3946void
3947input_set_selection(struct input *input,
3948 struct wl_data_source *source, uint32_t time)
3949{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003950 if (input->data_device)
3951 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003952}
3953
3954void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003955input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003956{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003957 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003958 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003959}
3960
3961static void
3962offer_io_func(struct task *task, uint32_t events)
3963{
3964 struct data_offer *offer =
3965 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003966 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003967 unsigned int len;
3968 char buffer[4096];
3969
3970 len = read(offer->fd, buffer, sizeof buffer);
3971 offer->func(buffer, len,
3972 offer->x, offer->y, offer->user_data);
3973
3974 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003975 if (display->data_device_manager_version >=
3976 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3977 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003978 close(offer->fd);
3979 data_offer_destroy(offer);
3980 }
3981}
3982
3983static void
3984data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3985 data_func_t func, void *user_data)
3986{
3987 int p[2];
3988
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003989 if (pipe2(p, O_CLOEXEC) == -1)
3990 return;
3991
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003992 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3993 close(p[1]);
3994
3995 offer->io_task.run = offer_io_func;
3996 offer->fd = p[0];
3997 offer->func = func;
3998 offer->refcount++;
3999 offer->user_data = user_data;
4000
4001 display_watch_fd(offer->input->display,
4002 offer->fd, EPOLLIN, &offer->io_task);
4003}
4004
4005void
4006input_receive_drag_data(struct input *input, const char *mime_type,
4007 data_func_t func, void *data)
4008{
4009 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004010 input->drag_offer->x = input->drag_x;
4011 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004012}
4013
4014int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004015input_receive_drag_data_to_fd(struct input *input,
4016 const char *mime_type, int fd)
4017{
4018 if (input->drag_offer)
4019 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4020
4021 return 0;
4022}
4023
4024int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004025input_receive_selection_data(struct input *input, const char *mime_type,
4026 data_func_t func, void *data)
4027{
4028 char **p;
4029
4030 if (input->selection_offer == NULL)
4031 return -1;
4032
4033 for (p = input->selection_offer->types.data; *p; p++)
4034 if (strcmp(mime_type, *p) == 0)
4035 break;
4036
4037 if (*p == NULL)
4038 return -1;
4039
4040 data_offer_receive_data(input->selection_offer,
4041 mime_type, func, data);
4042 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004043}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004044
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004045int
4046input_receive_selection_data_to_fd(struct input *input,
4047 const char *mime_type, int fd)
4048{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004049 if (input->selection_offer)
4050 wl_data_offer_receive(input->selection_offer->offer,
4051 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004052
4053 return 0;
4054}
4055
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004056void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004057window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004058{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004059 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004060 return;
4061
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004062 zxdg_toplevel_v6_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004063}
4064
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004065static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004066surface_set_synchronized(struct surface *surface)
4067{
4068 if (!surface->subsurface)
4069 return;
4070
4071 if (surface->synchronized)
4072 return;
4073
4074 wl_subsurface_set_sync(surface->subsurface);
4075 surface->synchronized = 1;
4076}
4077
4078static void
4079surface_set_synchronized_default(struct surface *surface)
4080{
4081 if (!surface->subsurface)
4082 return;
4083
4084 if (surface->synchronized == surface->synchronized_default)
4085 return;
4086
4087 if (surface->synchronized_default)
4088 wl_subsurface_set_sync(surface->subsurface);
4089 else
4090 wl_subsurface_set_desync(surface->subsurface);
4091
4092 surface->synchronized = surface->synchronized_default;
4093}
4094
4095static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004096surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004097{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004098 struct widget *widget = surface->widget;
4099 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004100
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004101 if (surface->input_region) {
4102 wl_region_destroy(surface->input_region);
4103 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004104 }
4105
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004106 if (surface->opaque_region)
4107 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004108
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004109 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004110
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004111 if (widget->resize_handler)
4112 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004113 widget->allocation.width,
4114 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004115 widget->user_data);
4116
Pekka Paalanen35e82632013-04-25 13:57:48 +03004117 if (surface->subsurface &&
4118 (surface->allocation.x != widget->allocation.x ||
4119 surface->allocation.y != widget->allocation.y)) {
4120 wl_subsurface_set_position(surface->subsurface,
4121 widget->allocation.x,
4122 widget->allocation.y);
4123 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004124 if (surface->allocation.width != widget->allocation.width ||
4125 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004126 window_schedule_redraw(widget->window);
4127 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004128 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004129
4130 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004131 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004132 widget->allocation.width,
4133 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004134}
4135
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004136static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004137window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004138{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004139 struct surface *surface;
4140
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004141 widget_set_allocation(window->main_surface->widget,
4142 window->pending_allocation.x,
4143 window->pending_allocation.y,
4144 window->pending_allocation.width,
4145 window->pending_allocation.height);
4146
4147 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004148
4149 /* The main surface is in the list, too. Main surface's
4150 * resize_handler is responsible for calling widget_set_allocation()
4151 * on all sub-surface root widgets, so they will be resized
4152 * properly.
4153 */
4154 wl_list_for_each(surface, &window->subsurface_list, link) {
4155 if (surface == window->main_surface)
4156 continue;
4157
4158 surface_set_synchronized(surface);
4159 surface_resize(surface);
4160 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004161
4162 if (!window->fullscreen && !window->maximized)
4163 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004164
4165 if (window->confined && window->confined_widget) {
4166 struct wl_compositor *compositor = window->display->compositor;
4167 struct wl_region *region;
4168 struct widget *widget = window->confined_widget;
4169
4170 region = wl_compositor_create_region(compositor);
4171 wl_region_add(region,
4172 widget->allocation.x,
4173 widget->allocation.y,
4174 widget->allocation.width,
4175 widget->allocation.height);
4176 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4177 region);
4178 wl_region_destroy(region);
4179 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004180}
4181
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004182static void
4183idle_resize(struct window *window)
4184{
4185 window->resize_needed = 0;
4186 window->redraw_needed = 1;
4187
4188 DBG("from %dx%d to %dx%d\n",
4189 window->main_surface->server_allocation.width,
4190 window->main_surface->server_allocation.height,
4191 window->pending_allocation.width,
4192 window->pending_allocation.height);
4193
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004194 window_do_resize(window);
4195}
4196
4197static void
4198undo_resize(struct window *window)
4199{
4200 window->pending_allocation.width =
4201 window->main_surface->server_allocation.width;
4202 window->pending_allocation.height =
4203 window->main_surface->server_allocation.height;
4204
4205 DBG("back to %dx%d\n",
4206 window->main_surface->server_allocation.width,
4207 window->main_surface->server_allocation.height);
4208
4209 window_do_resize(window);
4210
4211 if (window->pending_allocation.width == 0 &&
4212 window->pending_allocation.height == 0) {
4213 fprintf(stderr, "Error: Could not draw a surface, "
4214 "most likely due to insufficient disk space in "
4215 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4216 exit(EXIT_FAILURE);
4217 }
4218}
4219
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004220void
4221window_schedule_resize(struct window *window, int width, int height)
4222{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004223 /* We should probably get these numbers from the theme. */
4224 const int min_width = 200, min_height = 200;
4225
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004226 window->pending_allocation.x = 0;
4227 window->pending_allocation.y = 0;
4228 window->pending_allocation.width = width;
4229 window->pending_allocation.height = height;
4230
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004231 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004232 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004233 window->min_allocation.width = min_width;
4234 else
4235 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004236 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004237 window->min_allocation.height = min_height;
4238 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004239 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004240 }
4241
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004242 if (window->pending_allocation.width < window->min_allocation.width)
4243 window->pending_allocation.width = window->min_allocation.width;
4244 if (window->pending_allocation.height < window->min_allocation.height)
4245 window->pending_allocation.height = window->min_allocation.height;
4246
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004247 window->resize_needed = 1;
4248 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004249}
4250
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004251void
4252widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4253{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004254 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004255}
4256
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004257static int
4258window_get_shadow_margin(struct window *window)
4259{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004260 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004261 return frame_get_shadow_margin(window->frame->frame);
4262 else
4263 return 0;
4264}
4265
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004266static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004267window_inhibit_redraw(struct window *window)
4268{
4269 window->redraw_inhibited = 1;
4270 wl_list_remove(&window->redraw_task.link);
4271 wl_list_init(&window->redraw_task.link);
4272 window->redraw_task_scheduled = 0;
4273}
4274
4275static void
4276window_uninhibit_redraw(struct window *window)
4277{
4278 window->redraw_inhibited = 0;
4279 if (window->redraw_needed || window->resize_needed)
4280 window_schedule_redraw_task(window);
4281}
4282
4283static void
4284xdg_surface_handle_configure(void *data,
4285 struct zxdg_surface_v6 *zxdg_surface_v6,
4286 uint32_t serial)
4287{
4288 struct window *window = data;
4289
4290 zxdg_surface_v6_ack_configure(window->xdg_surface, serial);
4291
4292 if (window->state_changed_handler)
4293 window->state_changed_handler(window, window->user_data);
4294
4295 window_uninhibit_redraw(window);
4296}
4297
4298static const struct zxdg_surface_v6_listener xdg_surface_listener = {
4299 xdg_surface_handle_configure
4300};
4301
4302static void
4303xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
4304 int32_t width, int32_t height,
4305 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004306{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004307 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004308 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004309
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004310 window->maximized = 0;
4311 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004312 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004313 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004314
4315 wl_array_for_each(p, states) {
4316 uint32_t state = *p;
4317 switch (state) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004318 case ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004319 window->maximized = 1;
4320 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004321 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004322 window->fullscreen = 1;
4323 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004324 case ZXDG_TOPLEVEL_V6_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004325 window->resizing = 1;
4326 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004327 case ZXDG_TOPLEVEL_V6_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004328 window->focused = 1;
4329 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004330 default:
4331 /* Unknown state */
4332 break;
4333 }
4334 }
4335
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004336 if (window->frame) {
4337 if (window->maximized) {
4338 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4339 } else {
4340 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4341 }
4342
4343 if (window->focused) {
4344 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4345 } else {
4346 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4347 }
4348 }
4349
Jasper St. Pierref184c382014-05-06 08:33:27 -04004350 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004351 /* The width / height params are for window geometry,
4352 * but window_schedule_resize takes allocation. Add
4353 * on the shadow margin to get the difference. */
4354 int margin = window_get_shadow_margin(window);
4355
4356 window_schedule_resize(window,
4357 width + margin * 2,
4358 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004359 } else if (window->saved_allocation.width > 0 &&
4360 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004361 window_schedule_resize(window,
4362 window->saved_allocation.width,
4363 window->saved_allocation.height);
4364 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004365}
4366
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004367static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004368xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004369{
4370 struct window *window = data;
4371 window_close(window);
4372}
4373
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004374static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
4375 xdg_toplevel_handle_configure,
4376 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004377};
4378
4379static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004380window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004381{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004382 struct zxdg_toplevel_v6 *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004383
4384 if (!window->xdg_surface)
4385 return;
4386
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004387 if (window->parent == window->last_parent)
4388 return;
4389
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004390 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004391 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004392 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004393 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004394
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004395 zxdg_toplevel_v6_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004396 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004397}
4398
4399static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004400window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004401{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004402 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004403 frame_input_rect(window->frame->frame,
4404 &geometry->x,
4405 &geometry->y,
4406 &geometry->width,
4407 &geometry->height);
4408 else
4409 window_get_allocation(window, geometry);
4410}
4411
4412static void
4413window_sync_geometry(struct window *window)
4414{
4415 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004416
4417 if (!window->xdg_surface)
4418 return;
4419
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004420 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004421 if (geometry.x == window->last_geometry.x &&
4422 geometry.y == window->last_geometry.y &&
4423 geometry.width == window->last_geometry.width &&
4424 geometry.height == window->last_geometry.height)
4425 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004426
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004427 zxdg_surface_v6_set_window_geometry(window->xdg_surface,
4428 geometry.x,
4429 geometry.y,
4430 geometry.width,
4431 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004432 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004433}
4434
4435static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004436window_flush(struct window *window)
4437{
4438 struct surface *surface;
4439
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004440 assert(!window->redraw_inhibited);
4441
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004442 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004443 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004444 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004445 if (window->xdg_toplevel)
4446 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004447 }
4448
4449 wl_list_for_each(surface, &window->subsurface_list, link) {
4450 if (surface == window->main_surface)
4451 continue;
4452
4453 surface_flush(surface);
4454 }
4455
4456 surface_flush(window->main_surface);
4457}
4458
4459static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004460menu_destroy(struct menu *menu)
4461{
4462 widget_destroy(menu->widget);
4463 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004464 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004465 free(menu);
4466}
4467
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004468void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004469window_get_allocation(struct window *window,
4470 struct rectangle *allocation)
4471{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004472 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004473}
4474
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004475static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004476widget_redraw(struct widget *widget)
4477{
4478 struct widget *child;
4479
4480 if (widget->redraw_handler)
4481 widget->redraw_handler(widget, widget->user_data);
4482 wl_list_for_each(child, &widget->child_list, link)
4483 widget_redraw(child);
4484}
4485
4486static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004487frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4488{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004489 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004490
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004491 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004492 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004493 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004494 surface->frame_cb = NULL;
4495
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004496 surface->last_time = time;
4497
Pekka Paalanen71233882013-04-25 13:57:53 +03004498 if (surface->redraw_needed || surface->window->redraw_needed) {
4499 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004500 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004501 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004502}
4503
4504static const struct wl_callback_listener listener = {
4505 frame_callback
4506};
4507
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004508static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004509surface_redraw(struct surface *surface)
4510{
Pekka Paalanen71233882013-04-25 13:57:53 +03004511 DBG_OBJ(surface->surface, "begin\n");
4512
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004513 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004514 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004515
4516 /* Whole-window redraw forces a redraw even if the previous has
4517 * not yet hit the screen.
4518 */
4519 if (surface->frame_cb) {
4520 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004521 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004522
Pekka Paalanen71233882013-04-25 13:57:53 +03004523 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004524 wl_callback_destroy(surface->frame_cb);
4525 }
4526
Neil Roberts97b747c2013-12-19 16:17:12 +00004527 if (surface->widget->use_cairo &&
4528 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004529 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004530 return -1;
4531 }
4532
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004533 surface->frame_cb = wl_surface_frame(surface->surface);
4534 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004535 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004536
4537 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004538 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004539 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004540 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004541 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004542}
4543
4544static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004545idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004546{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004547 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004548 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004549 int failed = 0;
4550 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004551
Pekka Paalanen71233882013-04-25 13:57:53 +03004552 DBG(" --------- \n");
4553
Pekka Paalaneneebff542013-04-25 13:57:52 +03004554 wl_list_init(&window->redraw_task.link);
4555 window->redraw_task_scheduled = 0;
4556
4557 if (window->resize_needed) {
4558 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004559 if (window->main_surface->frame_cb) {
4560 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004561 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004562 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004563
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004564 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004565 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004566 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004567
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004568 if (surface_redraw(window->main_surface) < 0) {
4569 /*
4570 * Only main_surface failure will cause us to undo the resize.
4571 * If sub-surfaces fail, they will just be broken with old
4572 * content.
4573 */
4574 failed = 1;
4575 } else {
4576 wl_list_for_each(surface, &window->subsurface_list, link) {
4577 if (surface == window->main_surface)
4578 continue;
4579
4580 surface_redraw(surface);
4581 }
4582 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004583
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004584 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004585 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004586
4587 wl_list_for_each(surface, &window->subsurface_list, link)
4588 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004589
4590 if (resized && failed) {
4591 /* Restore widget tree to correspond to what is on screen. */
4592 undo_resize(window);
4593 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004594}
4595
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004596static void
4597window_schedule_redraw_task(struct window *window)
4598{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004599 if (window->redraw_inhibited)
4600 return;
4601
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004602 if (!window->redraw_task_scheduled) {
4603 window->redraw_task.run = idle_redraw;
4604 display_defer(window->display, &window->redraw_task);
4605 window->redraw_task_scheduled = 1;
4606 }
4607}
4608
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004609void
4610window_schedule_redraw(struct window *window)
4611{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004612 struct surface *surface;
4613
Pekka Paalanen71233882013-04-25 13:57:53 +03004614 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4615
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004616 wl_list_for_each(surface, &window->subsurface_list, link)
4617 surface->redraw_needed = 1;
4618
4619 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004620}
4621
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004622int
4623window_is_fullscreen(struct window *window)
4624{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004625 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004626}
4627
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004628void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004629window_set_fullscreen(struct window *window, int fullscreen)
4630{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004631 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004632 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004633
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004634 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004635 return;
4636
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004637 if (fullscreen)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004638 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004639 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004640 zxdg_toplevel_v6_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004641}
4642
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004643int
4644window_is_maximized(struct window *window)
4645{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004646 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004647}
4648
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004649void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004650window_set_maximized(struct window *window, int maximized)
4651{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004652 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004653 return;
4654
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004655 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004656 return;
4657
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004658 if (maximized)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004659 zxdg_toplevel_v6_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004660 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004661 zxdg_toplevel_v6_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004662}
4663
Jasper St. Pierrede680992014-04-10 17:23:49 -07004664int
4665window_is_resizing(struct window *window)
4666{
4667 return window->resizing;
4668}
4669
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004670void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004671window_set_minimized(struct window *window)
4672{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004673 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004674 return;
4675
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004676 zxdg_toplevel_v6_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004677}
4678
4679void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004680window_set_user_data(struct window *window, void *data)
4681{
4682 window->user_data = data;
4683}
4684
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004685void *
4686window_get_user_data(struct window *window)
4687{
4688 return window->user_data;
4689}
4690
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004691void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004692window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004693 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004694{
4695 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004696}
4697
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004698void
4699window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004700 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004701{
4702 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004703}
4704
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004705void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004706window_set_data_handler(struct window *window, window_data_handler_t handler)
4707{
4708 window->data_handler = handler;
4709}
4710
4711void
4712window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4713{
4714 window->drop_handler = handler;
4715}
4716
4717void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004718window_set_close_handler(struct window *window,
4719 window_close_handler_t handler)
4720{
4721 window->close_handler = handler;
4722}
4723
4724void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004725window_set_fullscreen_handler(struct window *window,
4726 window_fullscreen_handler_t handler)
4727{
4728 window->fullscreen_handler = handler;
4729}
4730
4731void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004732window_set_output_handler(struct window *window,
4733 window_output_handler_t handler)
4734{
4735 window->output_handler = handler;
4736}
4737
4738void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004739window_set_state_changed_handler(struct window *window,
4740 window_state_changed_handler_t handler)
4741{
4742 window->state_changed_handler = handler;
4743}
4744
4745void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004746window_set_pointer_locked_handler(struct window *window,
4747 locked_pointer_locked_handler_t locked,
4748 locked_pointer_unlocked_handler_t unlocked)
4749{
4750 window->pointer_unlocked_handler = unlocked;
4751 window->pointer_locked_handler = locked;
4752}
4753
4754void
4755window_set_pointer_confined_handler(struct window *window,
4756 confined_pointer_confined_handler_t confined,
4757 confined_pointer_unconfined_handler_t unconfined)
4758{
4759 window->pointer_confined_handler = confined;
4760 window->pointer_unconfined_handler = unconfined;
4761}
4762
4763void
4764window_set_locked_pointer_motion_handler(struct window *window,
4765 window_locked_pointer_motion_handler_t handler)
4766{
4767 window->locked_pointer_motion_handler = handler;
4768}
4769
4770void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004771window_set_title(struct window *window, const char *title)
4772{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004773 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004774 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004775 if (window->frame) {
4776 frame_set_title(window->frame->frame, title);
4777 widget_schedule_redraw(window->frame->widget);
4778 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004779 if (window->xdg_toplevel)
4780 zxdg_toplevel_v6_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004781}
4782
4783const char *
4784window_get_title(struct window *window)
4785{
4786 return window->title;
4787}
4788
4789void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004790window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4791{
4792 struct text_cursor_position *text_cursor_position =
4793 window->display->text_cursor_position;
4794
Scott Moreau9295ce02012-06-01 12:46:10 -06004795 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004796 return;
4797
4798 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004799 window->main_surface->surface,
4800 wl_fixed_from_int(x),
4801 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004802}
4803
Casey Dahlin9074db52012-04-19 22:50:09 -04004804static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004805relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4806 uint32_t utime_hi,
4807 uint32_t utime_lo,
4808 wl_fixed_t dx,
4809 wl_fixed_t dy,
4810 wl_fixed_t dx_unaccel,
4811 wl_fixed_t dy_unaccel)
4812{
4813 struct input *input = data;
4814 struct window *window = input->pointer_focus;
4815 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4816
4817 if (window->locked_pointer_motion_handler &&
4818 window->pointer_locked) {
4819 window->locked_pointer_motion_handler(
4820 window, input, ms,
4821 wl_fixed_to_double(dx),
4822 wl_fixed_to_double(dy),
4823 window->user_data);
4824 }
4825}
4826
4827static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4828 relative_pointer_handle_motion,
4829};
4830
4831static void
4832locked_pointer_locked(void *data,
4833 struct zwp_locked_pointer_v1 *locked_pointer)
4834{
4835 struct input *input = data;
4836 struct window *window = input->pointer_focus;
4837
4838 window->pointer_locked = true;
4839
4840 if (window->pointer_locked_handler) {
4841 window->pointer_locked_handler(window,
4842 input,
4843 window->user_data);
4844 }
4845}
4846
4847static void
4848locked_pointer_unlocked(void *data,
4849 struct zwp_locked_pointer_v1 *locked_pointer)
4850{
4851 struct input *input = data;
4852 struct window *window = input->pointer_focus;
4853
4854 window_unlock_pointer(window);
4855
4856 if (window->pointer_unlocked_handler) {
4857 window->pointer_unlocked_handler(window,
4858 input,
4859 window->user_data);
4860 }
4861}
4862
4863static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4864 locked_pointer_locked,
4865 locked_pointer_unlocked,
4866};
4867
4868int
4869window_lock_pointer(struct window *window, struct input *input)
4870{
4871 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4872 window->display->relative_pointer_manager;
4873 struct zwp_pointer_constraints_v1 *pointer_constraints =
4874 window->display->pointer_constraints;
4875 struct zwp_relative_pointer_v1 *relative_pointer;
4876 struct zwp_locked_pointer_v1 *locked_pointer;
4877
4878 if (!window->display->relative_pointer_manager)
4879 return -1;
4880
4881 if (!window->display->pointer_constraints)
4882 return -1;
4883
4884 if (window->locked_pointer)
4885 return -1;
4886
4887 if (window->confined_pointer)
4888 return -1;
4889
4890 if (!input->pointer)
4891 return -1;
4892
4893 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4894 relative_pointer_manager, input->pointer);
4895 zwp_relative_pointer_v1_add_listener(relative_pointer,
4896 &relative_pointer_listener,
4897 input);
4898
4899 locked_pointer =
4900 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4901 window->main_surface->surface,
4902 input->pointer,
4903 NULL,
4904 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4905 zwp_locked_pointer_v1_add_listener(locked_pointer,
4906 &locked_pointer_listener,
4907 input);
4908
4909 window->locked_input = input;
4910 window->locked_pointer = locked_pointer;
4911 window->relative_pointer = relative_pointer;
4912
4913 return 0;
4914}
4915
4916void
4917window_unlock_pointer(struct window *window)
4918{
4919 if (!window->locked_pointer)
4920 return;
4921
4922 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4923 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4924 window->locked_pointer = NULL;
4925 window->relative_pointer = NULL;
4926 window->pointer_locked = false;
4927 window->locked_input = NULL;
4928}
4929
4930void
4931widget_set_locked_pointer_cursor_hint(struct widget *widget,
4932 float x, float y)
4933{
4934 struct window *window = widget->window;
4935
4936 if (!window->locked_pointer)
4937 return;
4938
4939 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4940 wl_fixed_from_double(x),
4941 wl_fixed_from_double(y));
4942 wl_surface_commit(window->main_surface->surface);
4943}
4944
4945static void
4946confined_pointer_confined(void *data,
4947 struct zwp_confined_pointer_v1 *confined_pointer)
4948{
4949 struct input *input = data;
4950 struct window *window = input->pointer_focus;
4951
4952 window->confined = true;
4953
4954 if (window->pointer_confined_handler) {
4955 window->pointer_confined_handler(window,
4956 input,
4957 window->user_data);
4958 }
4959}
4960
4961static void
4962confined_pointer_unconfined(void *data,
4963 struct zwp_confined_pointer_v1 *confined_pointer)
4964{
4965 struct input *input = data;
4966 struct window *window = input->pointer_focus;
4967
4968 window_unconfine_pointer(window);
4969
4970 window->confined = false;
4971
4972 if (window->pointer_unconfined_handler) {
4973 window->pointer_unconfined_handler(window,
4974 input,
4975 window->user_data);
4976 }
4977}
4978
4979static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4980 confined_pointer_confined,
4981 confined_pointer_unconfined,
4982};
4983
4984int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004985window_confine_pointer_to_rectangles(struct window *window,
4986 struct input *input,
4987 struct rectangle *rectangles,
4988 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004989{
4990 struct zwp_pointer_constraints_v1 *pointer_constraints =
4991 window->display->pointer_constraints;
4992 struct zwp_confined_pointer_v1 *confined_pointer;
4993 struct wl_compositor *compositor = window->display->compositor;
4994 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004995 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004996
4997 if (!window->display->pointer_constraints)
4998 return -1;
4999
5000 if (window->locked_pointer)
5001 return -1;
5002
5003 if (window->confined_pointer)
5004 return -1;
5005
5006 if (!input->pointer)
5007 return -1;
5008
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005009 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005010 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005011 for (i = 0; i < num_rectangles; i++) {
5012 wl_region_add(region,
5013 rectangles[i].x,
5014 rectangles[i].y,
5015 rectangles[i].width,
5016 rectangles[i].height);
5017 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005018 }
5019
5020 confined_pointer =
5021 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5022 window->main_surface->surface,
5023 input->pointer,
5024 region,
5025 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5026 if (region)
5027 wl_region_destroy(region);
5028
5029 zwp_confined_pointer_v1_add_listener(confined_pointer,
5030 &confined_pointer_listener,
5031 input);
5032
5033 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005034 window->confined_widget = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005035
5036 return 0;
5037}
5038
5039void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005040window_update_confine_rectangles(struct window *window,
5041 struct rectangle *rectangles,
5042 int num_rectangles)
5043{
5044 struct wl_compositor *compositor = window->display->compositor;
5045 struct wl_region *region;
5046 int i;
5047
5048 region = wl_compositor_create_region(compositor);
5049 for (i = 0; i < num_rectangles; i++) {
5050 wl_region_add(region,
5051 rectangles[i].x,
5052 rectangles[i].y,
5053 rectangles[i].width,
5054 rectangles[i].height);
5055 }
5056
5057 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5058
5059 wl_region_destroy(region);
5060}
5061
5062int
5063window_confine_pointer_to_widget(struct window *window,
5064 struct widget *widget,
5065 struct input *input)
5066{
5067 int ret;
5068
5069 if (widget) {
5070 ret = window_confine_pointer_to_rectangles(window,
5071 input,
5072 &widget->allocation,
5073 1);
5074 window->confined_widget = widget;
5075 return ret;
5076 } else {
5077 return window_confine_pointer_to_rectangles(window,
5078 input,
5079 NULL,
5080 0);
5081 }
5082}
5083
5084void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005085window_unconfine_pointer(struct window *window)
5086{
5087 if (!window->confined_pointer)
5088 return;
5089
5090 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5091 window->confined_pointer = NULL;
5092 window->confined = false;
5093}
5094
5095static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005096surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005097 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005098{
Rob Bradford7507b572012-05-15 17:55:34 +01005099 struct window *window = data;
5100 struct output *output;
5101 struct output *output_found = NULL;
5102 struct window_output *window_output;
5103
5104 wl_list_for_each(output, &window->display->output_list, link) {
5105 if (output->output == wl_output) {
5106 output_found = output;
5107 break;
5108 }
5109 }
5110
5111 if (!output_found)
5112 return;
5113
Brian Lovinbc919262013-08-07 15:34:59 -07005114 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005115 window_output->output = output_found;
5116
5117 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005118
5119 if (window->output_handler)
5120 window->output_handler(window, output_found, 1,
5121 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005122}
5123
5124static void
5125surface_leave(void *data,
5126 struct wl_surface *wl_surface, struct wl_output *output)
5127{
Rob Bradford7507b572012-05-15 17:55:34 +01005128 struct window *window = data;
5129 struct window_output *window_output;
5130 struct window_output *window_output_found = NULL;
5131
5132 wl_list_for_each(window_output, &window->window_output_list, link) {
5133 if (window_output->output->output == output) {
5134 window_output_found = window_output;
5135 break;
5136 }
5137 }
5138
5139 if (window_output_found) {
5140 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005141
5142 if (window->output_handler)
5143 window->output_handler(window, window_output->output,
5144 0, window->user_data);
5145
Rob Bradford7507b572012-05-15 17:55:34 +01005146 free(window_output_found);
5147 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005148}
5149
5150static const struct wl_surface_listener surface_listener = {
5151 surface_enter,
5152 surface_leave
5153};
5154
Pekka Paalanen4e373742013-02-13 16:17:13 +02005155static struct surface *
5156surface_create(struct window *window)
5157{
5158 struct display *display = window->display;
5159 struct surface *surface;
5160
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005161 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005162 surface->window = window;
5163 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005164 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005165 wl_surface_add_listener(surface->surface, &surface_listener, window);
5166
Pekka Paalanen35e82632013-04-25 13:57:48 +03005167 wl_list_insert(&window->subsurface_list, &surface->link);
5168
Pekka Paalanen4e373742013-02-13 16:17:13 +02005169 return surface;
5170}
5171
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005172static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005173get_preferred_buffer_type(struct display *display)
5174{
5175#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005176 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005177 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5178#endif
5179
5180 return WINDOW_BUFFER_TYPE_SHM;
5181}
5182
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005183static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005184window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005185{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005186 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005187 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005188
Peter Huttererf3d62272013-08-08 11:57:05 +10005189 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005190 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005191 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005192
5193 surface = surface_create(window);
5194 window->main_surface = surface;
5195
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005196 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005197
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005198 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005199 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005200
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005201 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005202
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005203 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005204 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005205 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005206
Rob Bradford7507b572012-05-15 17:55:34 +01005207 wl_list_init (&window->window_output_list);
5208
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005209 return window;
5210}
5211
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005212struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005213window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005214{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005215 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005216 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005217
5218 window = window_create_internal(display, 0);
5219
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005220 if (window->display->xdg_shell) {
5221 window->xdg_surface =
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005222 zxdg_shell_v6_get_xdg_surface(window->display->xdg_shell,
5223 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005224 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005225
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005226 zxdg_surface_v6_add_listener(window->xdg_surface,
5227 &xdg_surface_listener, window);
5228
5229 window->xdg_toplevel =
5230 zxdg_surface_v6_get_toplevel(window->xdg_surface);
5231 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5232
5233 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
5234 &xdg_toplevel_listener, window);
5235
5236 window_inhibit_redraw(window);
5237
5238 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005239 } else if (display->ivi_application) {
5240 /* auto generation of ivi_id based on process id + basement of id */
5241 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5242 window->ivi_surface =
5243 ivi_application_surface_create(display->ivi_application,
5244 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005245 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005246
5247 ivi_surface_add_listener(window->ivi_surface,
5248 &ivi_surface_listener, window);
5249 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005250
5251 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005252}
5253
5254struct window *
5255window_create_custom(struct display *display)
5256{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005257 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005258}
5259
Jasper St. Pierre53686042013-12-09 15:26:25 -05005260void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005261window_set_parent(struct window *window,
5262 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005263{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005264 window->parent = parent_window;
5265 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005266}
5267
5268struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005269window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005270{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005271 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005272}
5273
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005274static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005275menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005276{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005277 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005278 int next;
5279
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005280 frame_interior(menu->frame, &x, &y, &width, &height);
5281 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005282 if (menu->current != next) {
5283 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005284 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005285 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005286}
5287
5288static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005289menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005290 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005291 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005292{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005293 struct menu *menu = data;
5294
5295 if (widget == menu->widget)
5296 menu_set_item(data, y);
5297
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005298 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005299}
5300
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005301static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005302menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005303 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005304{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005305 struct menu *menu = data;
5306
5307 if (widget == menu->widget)
5308 menu_set_item(data, y);
5309
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005310 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005311}
5312
5313static void
5314menu_leave_handler(struct widget *widget, struct input *input, void *data)
5315{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005316 struct menu *menu = data;
5317
5318 if (widget == menu->widget)
5319 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005320}
5321
5322static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005323menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005324 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005325 uint32_t button, enum wl_pointer_button_state state,
5326 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005327
5328{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005329 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005330
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005331 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5332 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005333 /* Either relase after press-drag-release or
5334 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005335 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005336 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005337 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005338 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005339 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005340 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005341}
5342
5343static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005344menu_touch_up_handler(struct widget *widget,
5345 struct input *input,
5346 uint32_t serial,
5347 uint32_t time,
5348 int32_t id,
5349 void *data)
5350{
5351 struct menu *menu = data;
5352
5353 input_ungrab(input);
5354 menu_destroy(menu);
5355}
5356
5357static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005358menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005359{
5360 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005361 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005362 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005363
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005364 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005365
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005366 frame_repaint(menu->frame, cr);
5367 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005368
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005369 theme_set_background_source(menu->window->display->theme,
5370 cr, THEME_FRAME_ACTIVE);
5371 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005372 cairo_fill(cr);
5373
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005374 cairo_select_font_face(cr, "sans",
5375 CAIRO_FONT_SLANT_NORMAL,
5376 CAIRO_FONT_WEIGHT_NORMAL);
5377 cairo_set_font_size(cr, 12);
5378
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005379 for (i = 0; i < menu->count; i++) {
5380 if (i == menu->current) {
5381 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005382 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005383 cairo_fill(cr);
5384 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005385 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005386 cairo_show_text(cr, menu->entries[i]);
5387 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005388 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5389 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005390 cairo_show_text(cr, menu->entries[i]);
5391 }
5392 }
5393
5394 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005395}
5396
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005397static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005398xdg_popup_handle_configure(void *data,
5399 struct zxdg_popup_v6 *zxdg_popup_v6,
5400 int32_t x,
5401 int32_t y,
5402 int32_t width,
5403 int32_t height)
5404{
5405}
5406
5407static void
5408xdg_popup_handle_popup_done(void *data, struct zxdg_popup_v6 *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005409{
5410 struct window *window = data;
5411 struct menu *menu = window->main_surface->widget->user_data;
5412
5413 input_ungrab(menu->input);
5414 menu_destroy(menu);
5415}
5416
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005417static const struct zxdg_popup_v6_listener xdg_popup_listener = {
5418 xdg_popup_handle_configure,
5419 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005420};
5421
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005422static struct menu *
5423create_menu(struct display *display,
5424 struct input *input, uint32_t time,
5425 menu_func_t func, const char **entries, int count,
5426 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005427{
5428 struct window *window;
5429 struct menu *menu;
5430
5431 menu = malloc(sizeof *menu);
5432 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005433 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005434
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005435 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005436 if (!window) {
5437 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005438 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005439 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005440
5441 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005442 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005443 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005444 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07005445 FRAME_BUTTON_NONE, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005446 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005447 menu->entries = entries;
5448 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005449 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005450 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005451 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005452 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005453 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005454
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005455 input_ungrab(input);
5456
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005457 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005458 widget_set_enter_handler(menu->widget, menu_enter_handler);
5459 widget_set_leave_handler(menu->widget, menu_leave_handler);
5460 widget_set_motion_handler(menu->widget, menu_motion_handler);
5461 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005462 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005463
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005464 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005465 frame_resize_inside(menu->frame, 200, count * 20);
5466 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5467 window_schedule_resize(window, frame_width(menu->frame),
5468 frame_height(menu->frame));
5469
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005470 return menu;
5471}
5472
5473struct window *
5474window_create_menu(struct display *display,
5475 struct input *input, uint32_t time,
5476 menu_func_t func, const char **entries, int count,
5477 void *user_data)
5478{
5479 struct menu *menu;
5480 menu = create_menu(display, input, time, func, entries, count, user_data);
5481
5482 if (menu == NULL)
5483 return NULL;
5484
5485 return menu->window;
5486}
5487
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005488static struct zxdg_positioner_v6 *
5489create_simple_positioner(struct display *display,
5490 int x, int y)
5491{
5492 struct zxdg_positioner_v6 *positioner;
5493
5494 positioner = zxdg_shell_v6_create_positioner(display->xdg_shell);
5495 fail_on_null(positioner, 0, __FILE__, __LINE__);
5496 zxdg_positioner_v6_set_anchor_rect(positioner, x, y, 1, 1);
5497 zxdg_positioner_v6_set_anchor(positioner,
5498 ZXDG_POSITIONER_V6_ANCHOR_TOP |
5499 ZXDG_POSITIONER_V6_ANCHOR_LEFT);
5500 zxdg_positioner_v6_set_gravity(positioner,
5501 ZXDG_POSITIONER_V6_ANCHOR_BOTTOM |
5502 ZXDG_POSITIONER_V6_ANCHOR_RIGHT);
5503
5504 return positioner;
5505}
5506
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005507void
5508window_show_menu(struct display *display,
5509 struct input *input, uint32_t time, struct window *parent,
5510 int32_t x, int32_t y,
5511 menu_func_t func, const char **entries, int count)
5512{
5513 struct menu *menu;
5514 struct window *window;
5515 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005516 struct rectangle parent_geometry;
5517 struct zxdg_positioner_v6 *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005518
5519 menu = create_menu(display, input, time, func, entries, count, parent);
5520
5521 if (menu == NULL)
5522 return;
5523
5524 window = menu->window;
5525
5526 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5527 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5528
5529 window->x = x;
5530 window->y = y;
5531
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005532 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005533 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005534
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005535 if (!display->xdg_shell)
5536 return;
5537
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005538 window->xdg_surface =
5539 zxdg_shell_v6_get_xdg_surface(display->xdg_shell,
5540 window->main_surface->surface);
5541 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005542
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005543 zxdg_surface_v6_add_listener(window->xdg_surface,
5544 &xdg_surface_listener, window);
5545
5546 positioner = create_simple_positioner(display,
5547 window->x - (ix + parent_geometry.x),
5548 window->y - (iy + parent_geometry.y));
5549 window->xdg_popup =
5550 zxdg_surface_v6_get_popup(window->xdg_surface,
5551 parent->xdg_surface,
5552 positioner);
5553 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
5554 zxdg_positioner_v6_destroy(positioner);
5555 zxdg_popup_v6_grab(window->xdg_popup,
5556 input->seat,
5557 display_get_serial(window->display));
5558 zxdg_popup_v6_add_listener(window->xdg_popup,
5559 &xdg_popup_listener, window);
5560
5561 window_inhibit_redraw(window);
5562
5563 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005564}
5565
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005566void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005567window_set_buffer_type(struct window *window, enum window_buffer_type type)
5568{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005569 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005570}
5571
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005572enum window_buffer_type
5573window_get_buffer_type(struct window *window)
5574{
5575 return window->main_surface->buffer_type;
5576}
5577
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005578void
5579window_set_preferred_format(struct window *window,
5580 enum preferred_format format)
5581{
5582 window->preferred_format = format;
5583}
5584
Pekka Paalanen35e82632013-04-25 13:57:48 +03005585struct widget *
5586window_add_subsurface(struct window *window, void *data,
5587 enum subsurface_mode default_mode)
5588{
5589 struct widget *widget;
5590 struct surface *surface;
5591 struct wl_surface *parent;
5592 struct wl_subcompositor *subcompo = window->display->subcompositor;
5593
Pekka Paalanen35e82632013-04-25 13:57:48 +03005594 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005595 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005596 widget = widget_create(window, surface, data);
5597 wl_list_init(&widget->link);
5598 surface->widget = widget;
5599
5600 parent = window->main_surface->surface;
5601 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5602 surface->surface,
5603 parent);
5604 surface->synchronized = 1;
5605
5606 switch (default_mode) {
5607 case SUBSURFACE_SYNCHRONIZED:
5608 surface->synchronized_default = 1;
5609 break;
5610 case SUBSURFACE_DESYNCHRONIZED:
5611 surface->synchronized_default = 0;
5612 break;
5613 default:
5614 assert(!"bad enum subsurface_mode");
5615 }
5616
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005617 window->resize_needed = 1;
5618 window_schedule_redraw(window);
5619
Pekka Paalanen35e82632013-04-25 13:57:48 +03005620 return widget;
5621}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005622
5623static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005624display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005625 struct wl_output *wl_output,
5626 int x, int y,
5627 int physical_width,
5628 int physical_height,
5629 int subpixel,
5630 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005631 const char *model,
5632 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005633{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005634 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005635
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005636 output->allocation.x = x;
5637 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005638 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005639
5640 if (output->make)
5641 free(output->make);
5642 output->make = strdup(make);
5643
5644 if (output->model)
5645 free(output->model);
5646 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005647}
5648
5649static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005650display_handle_done(void *data,
5651 struct wl_output *wl_output)
5652{
5653}
5654
5655static void
5656display_handle_scale(void *data,
5657 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005658 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005659{
5660 struct output *output = data;
5661
5662 output->scale = scale;
5663}
5664
5665static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005666display_handle_mode(void *data,
5667 struct wl_output *wl_output,
5668 uint32_t flags,
5669 int width,
5670 int height,
5671 int refresh)
5672{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005673 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005674 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005675
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005676 if (flags & WL_OUTPUT_MODE_CURRENT) {
5677 output->allocation.width = width;
5678 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005679 if (display->output_configure_handler)
5680 (*display->output_configure_handler)(
5681 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005682 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005683}
5684
5685static const struct wl_output_listener output_listener = {
5686 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005687 display_handle_mode,
5688 display_handle_done,
5689 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005690};
5691
5692static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005693display_add_output(struct display *d, uint32_t id)
5694{
5695 struct output *output;
5696
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005697 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005698 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005699 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005700 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005701 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005702 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005703 wl_list_insert(d->output_list.prev, &output->link);
5704
5705 wl_output_add_listener(output->output, &output_listener, output);
5706}
5707
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005708static void
5709output_destroy(struct output *output)
5710{
5711 if (output->destroy_handler)
5712 (*output->destroy_handler)(output, output->user_data);
5713
5714 wl_output_destroy(output->output);
5715 wl_list_remove(&output->link);
5716 free(output);
5717}
5718
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005719static void
5720display_destroy_output(struct display *d, uint32_t id)
5721{
5722 struct output *output;
5723
5724 wl_list_for_each(output, &d->output_list, link) {
5725 if (output->server_output_id == id) {
5726 output_destroy(output);
5727 break;
5728 }
5729 }
5730}
5731
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005732void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005733display_set_global_handler(struct display *display,
5734 display_global_handler_t handler)
5735{
5736 struct global *global;
5737
5738 display->global_handler = handler;
5739 if (!handler)
5740 return;
5741
5742 wl_list_for_each(global, &display->global_list, link)
5743 display->global_handler(display,
5744 global->name, global->interface,
5745 global->version, display->user_data);
5746}
5747
5748void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005749display_set_global_handler_remove(struct display *display,
5750 display_global_handler_t remove_handler)
5751{
5752 display->global_handler_remove = remove_handler;
5753 if (!remove_handler)
5754 return;
5755}
5756
5757void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005758display_set_output_configure_handler(struct display *display,
5759 display_output_handler_t handler)
5760{
5761 struct output *output;
5762
5763 display->output_configure_handler = handler;
5764 if (!handler)
5765 return;
5766
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005767 wl_list_for_each(output, &display->output_list, link) {
5768 if (output->allocation.width == 0 &&
5769 output->allocation.height == 0)
5770 continue;
5771
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005772 (*display->output_configure_handler)(output,
5773 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005774 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005775}
5776
5777void
5778output_set_user_data(struct output *output, void *data)
5779{
5780 output->user_data = data;
5781}
5782
5783void *
5784output_get_user_data(struct output *output)
5785{
5786 return output->user_data;
5787}
5788
5789void
5790output_set_destroy_handler(struct output *output,
5791 display_output_handler_t handler)
5792{
5793 output->destroy_handler = handler;
5794 /* FIXME: implement this, once we have way to remove outputs */
5795}
5796
5797void
Scott Moreau4e072362012-09-29 02:03:11 -06005798output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005799{
Scott Moreau4e072362012-09-29 02:03:11 -06005800 struct rectangle allocation = output->allocation;
5801
5802 switch (output->transform) {
5803 case WL_OUTPUT_TRANSFORM_90:
5804 case WL_OUTPUT_TRANSFORM_270:
5805 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5806 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5807 /* Swap width and height */
5808 allocation.width = output->allocation.height;
5809 allocation.height = output->allocation.width;
5810 break;
5811 }
5812
5813 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005814}
5815
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005816struct wl_output *
5817output_get_wl_output(struct output *output)
5818{
5819 return output->output;
5820}
5821
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005822enum wl_output_transform
5823output_get_transform(struct output *output)
5824{
5825 return output->transform;
5826}
5827
Alexander Larssonafd319a2013-05-22 14:41:27 +02005828uint32_t
5829output_get_scale(struct output *output)
5830{
5831 return output->scale;
5832}
5833
Jason Ekstrand738715d2014-04-02 19:53:50 -05005834const char *
5835output_get_make(struct output *output)
5836{
5837 return output->make;
5838}
5839
5840const char *
5841output_get_model(struct output *output)
5842{
5843 return output->model;
5844}
5845
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005846static void
Daniel Stone97f68542012-05-30 16:32:01 +01005847fini_xkb(struct input *input)
5848{
5849 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005850 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005851}
5852
5853static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005854display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005855{
5856 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005857 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005858
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005859 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005860 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005861 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005862 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005863 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005864 input->pointer_focus = NULL;
5865 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005866 input->seat_version = seat_version;
5867
Rusty Lynch041815a2013-08-08 21:20:38 -07005868 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005869 wl_list_insert(d->input_list.prev, &input->link);
5870
Daniel Stone37816df2012-05-16 18:45:18 +01005871 wl_seat_add_listener(input->seat, &seat_listener, input);
5872 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005873
Jason Ekstranda669bd52014-04-02 19:53:51 -05005874 if (d->data_device_manager) {
5875 input->data_device =
5876 wl_data_device_manager_get_data_device(d->data_device_manager,
5877 input->seat);
5878 wl_data_device_add_listener(input->data_device,
5879 &data_device_listener,
5880 input);
5881 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005882
5883 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005884 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005885
Derek Foreman118a4292015-04-22 17:23:35 -05005886 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5887 TFD_CLOEXEC | TFD_NONBLOCK);
5888 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5889 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005890 set_repeat_info(input, 40, 400);
5891
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005892 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5893 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005894 input->repeat_task.run = keyboard_repeat_func;
5895 display_watch_fd(d, input->repeat_timer_fd,
5896 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005897}
5898
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005899static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005900input_destroy(struct input *input)
5901{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005902 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005903 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005904
5905 if (input->drag_offer)
5906 data_offer_destroy(input->drag_offer);
5907
5908 if (input->selection_offer)
5909 data_offer_destroy(input->selection_offer);
5910
kabeer khan6ce67ec2014-10-20 11:55:29 +05305911 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005912 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305913 wl_data_device_release(input->data_device);
5914 else
5915 wl_data_device_destroy(input->data_device);
5916 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005917 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005918 if (input->touch)
5919 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005920 if (input->pointer)
5921 wl_pointer_release(input->pointer);
5922 if (input->keyboard)
5923 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005924 } else {
5925 if (input->touch)
5926 wl_touch_destroy(input->touch);
5927 if (input->pointer)
5928 wl_pointer_destroy(input->pointer);
5929 if (input->keyboard)
5930 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005931 }
5932
Daniel Stone97f68542012-05-30 16:32:01 +01005933 fini_xkb(input);
5934
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005935 wl_surface_destroy(input->pointer_surface);
5936
Pekka Paalanene1207c72011-12-16 12:02:09 +02005937 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005938 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005939 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005940 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005941 free(input);
5942}
5943
5944static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005945shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5946{
5947 struct display *d = data;
5948
5949 if (format == WL_SHM_FORMAT_RGB565)
5950 d->has_rgb565 = 1;
5951}
5952
5953struct wl_shm_listener shm_listener = {
5954 shm_format
5955};
5956
5957static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005958xdg_shell_handle_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005959{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005960 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005961}
5962
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005963static const struct zxdg_shell_v6_listener xdg_shell_listener = {
5964 xdg_shell_handle_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005965};
5966
5967static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005968registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5969 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005970{
5971 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005972 struct global *global;
5973
Brian Lovinbc919262013-08-07 15:34:59 -07005974 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005975 global->name = id;
5976 global->interface = strdup(interface);
5977 global->version = version;
5978 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005979
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005980 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005981 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005982 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005983 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005984 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005985 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005986 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005987 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5988 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5989 d->relative_pointer_manager =
5990 wl_registry_bind(registry, id,
5991 &zwp_relative_pointer_manager_v1_interface,
5992 1);
5993 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5994 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5995 d->pointer_constraints =
5996 wl_registry_bind(registry, id,
5997 &zwp_pointer_constraints_v1_interface,
5998 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005999 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006000 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02006001 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006002 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01006003 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006004 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006005 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05306006 &wl_data_device_manager_interface,
6007 d->data_device_manager_version);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006008 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006009 d->xdg_shell = wl_registry_bind(registry, id,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006010 &zxdg_shell_v6_interface, 1);
6011 zxdg_shell_v6_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06006012 } else if (strcmp(interface, "text_cursor_position") == 0) {
6013 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006014 wl_registry_bind(registry, id,
6015 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006016 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6017 d->subcompositor =
6018 wl_registry_bind(registry, id,
6019 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006020 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006021 else if (strcmp(interface, "ivi_application") == 0) {
6022 d->ivi_application =
6023 wl_registry_bind(registry, id,
6024 &ivi_application_interface, 1);
6025 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006026
6027 if (d->global_handler)
6028 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006029}
6030
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006031static void
6032registry_handle_global_remove(void *data, struct wl_registry *registry,
6033 uint32_t name)
6034{
6035 struct display *d = data;
6036 struct global *global;
6037 struct global *tmp;
6038
6039 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6040 if (global->name != name)
6041 continue;
6042
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006043 if (strcmp(global->interface, "wl_output") == 0)
6044 display_destroy_output(d, name);
6045
6046 /* XXX: Should destroy remaining bound globals */
6047
6048 if (d->global_handler_remove)
6049 d->global_handler_remove(d, name, global->interface,
6050 global->version, d->user_data);
6051
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006052 wl_list_remove(&global->link);
6053 free(global->interface);
6054 free(global);
6055 }
6056}
6057
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006058void *
6059display_bind(struct display *display, uint32_t name,
6060 const struct wl_interface *interface, uint32_t version)
6061{
6062 return wl_registry_bind(display->registry, name, interface, version);
6063}
6064
6065static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006066 registry_handle_global,
6067 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006068};
6069
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006070#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006071static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006072init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006073{
6074 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006075 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006076
Rob Clark6396ed32012-03-11 19:48:41 -05006077#ifdef USE_CAIRO_GLESV2
6078# define GL_BIT EGL_OPENGL_ES2_BIT
6079#else
6080# define GL_BIT EGL_OPENGL_BIT
6081#endif
6082
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006083 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006084 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006085 EGL_RED_SIZE, 1,
6086 EGL_GREEN_SIZE, 1,
6087 EGL_BLUE_SIZE, 1,
6088 EGL_ALPHA_SIZE, 1,
6089 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006090 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006091 EGL_NONE
6092 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006093
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006094#ifdef USE_CAIRO_GLESV2
6095 static const EGLint context_attribs[] = {
6096 EGL_CONTEXT_CLIENT_VERSION, 2,
6097 EGL_NONE
6098 };
6099 EGLint api = EGL_OPENGL_ES_API;
6100#else
6101 EGLint *context_attribs = NULL;
6102 EGLint api = EGL_OPENGL_API;
6103#endif
6104
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006105 d->dpy =
6106 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6107 d->display, NULL);
6108
Yuval Fledel45568f62010-12-06 09:18:12 -05006109 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006110 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006111 return -1;
6112 }
6113
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006114 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006115 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006116 return -1;
6117 }
6118
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006119 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6120 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006121 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006122 return -1;
6123 }
6124
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006125 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006126 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006127 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006128 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006129 return -1;
6130 }
6131
Benjamin Franzke0c991632011-09-27 21:57:31 +02006132 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6133 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006134 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006135 return -1;
6136 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006137
6138 return 0;
6139}
6140
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006141static void
6142fini_egl(struct display *display)
6143{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006144 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006145
6146 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6147 EGL_NO_CONTEXT);
6148
6149 eglTerminate(display->dpy);
6150 eglReleaseThread();
6151}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006152#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006153
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006154static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006155init_dummy_surface(struct display *display)
6156{
6157 int len;
6158 void *data;
6159
6160 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006161 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006162 display->dummy_surface =
6163 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6164 1, 1, len);
6165 display->dummy_surface_data = data;
6166}
6167
6168static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006169handle_display_data(struct task *task, uint32_t events)
6170{
6171 struct display *display =
6172 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006173 struct epoll_event ep;
6174 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006175
6176 display->display_fd_events = events;
6177
6178 if (events & EPOLLERR || events & EPOLLHUP) {
6179 display_exit(display);
6180 return;
6181 }
6182
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006183 if (events & EPOLLIN) {
6184 ret = wl_display_dispatch(display->display);
6185 if (ret == -1) {
6186 display_exit(display);
6187 return;
6188 }
6189 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006190
6191 if (events & EPOLLOUT) {
6192 ret = wl_display_flush(display->display);
6193 if (ret == 0) {
6194 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6195 ep.data.ptr = &display->display_task;
6196 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6197 display->display_fd, &ep);
6198 } else if (ret == -1 && errno != EAGAIN) {
6199 display_exit(display);
6200 return;
6201 }
6202 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006203}
6204
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006205static void
6206log_handler(const char *format, va_list args)
6207{
6208 vfprintf(stderr, format, args);
6209}
6210
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006211struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006212display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006213{
6214 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006215
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006216 wl_log_set_handler_client(log_handler);
6217
Peter Huttererf3d62272013-08-08 11:57:05 +10006218 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006219 if (d == NULL)
6220 return NULL;
6221
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006222 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006223 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006224 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006225 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006226 return NULL;
6227 }
6228
Rob Bradford5ab9c752013-07-26 16:29:43 +01006229 d->xkb_context = xkb_context_new(0);
6230 if (d->xkb_context == NULL) {
6231 fprintf(stderr, "Failed to create XKB context\n");
6232 free(d);
6233 return NULL;
6234 }
6235
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006236 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006237 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006238 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006239 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6240 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006241
6242 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006243 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006244 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006245 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006246
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006247 d->registry = wl_display_get_registry(d->display);
6248 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006249
Marek Chalupaa519d062014-12-05 13:49:40 +01006250 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006251 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6252 return NULL;
6253 }
6254
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006255#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006256 if (init_egl(d) < 0)
6257 fprintf(stderr, "EGL does not seem to work, "
6258 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006259#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006260
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006261 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006262
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006263 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006264
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006265 wl_list_init(&d->window_list);
6266
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006267 init_dummy_surface(d);
6268
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006269 return d;
6270}
6271
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006272static void
6273display_destroy_outputs(struct display *display)
6274{
6275 struct output *tmp;
6276 struct output *output;
6277
6278 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6279 output_destroy(output);
6280}
6281
Pekka Paalanene1207c72011-12-16 12:02:09 +02006282static void
6283display_destroy_inputs(struct display *display)
6284{
6285 struct input *tmp;
6286 struct input *input;
6287
6288 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6289 input_destroy(input);
6290}
6291
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006292void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006293display_destroy(struct display *display)
6294{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006295 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006296 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6297 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006298
6299 if (!wl_list_empty(&display->deferred_list))
6300 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6301
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006302 cairo_surface_destroy(display->dummy_surface);
6303 free(display->dummy_surface_data);
6304
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006305 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006306 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006307
Daniel Stone97f68542012-05-30 16:32:01 +01006308 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006309
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006310 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006311 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006312
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006313#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006314 if (display->argb_device)
6315 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006316#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006317
Pekka Paalanen35e82632013-04-25 13:57:48 +03006318 if (display->subcompositor)
6319 wl_subcompositor_destroy(display->subcompositor);
6320
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006321 if (display->xdg_shell)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006322 zxdg_shell_v6_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006323
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006324 if (display->ivi_application)
6325 ivi_application_destroy(display->ivi_application);
6326
Pekka Paalanenc2052982011-12-16 11:41:32 +02006327 if (display->shm)
6328 wl_shm_destroy(display->shm);
6329
6330 if (display->data_device_manager)
6331 wl_data_device_manager_destroy(display->data_device_manager);
6332
6333 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006334 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006335
6336 close(display->epoll_fd);
6337
U. Artie Eoff44874d92012-10-02 21:12:35 -07006338 if (!(display->display_fd_events & EPOLLERR) &&
6339 !(display->display_fd_events & EPOLLHUP))
6340 wl_display_flush(display->display);
6341
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006342 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006343 free(display);
6344}
6345
6346void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006347display_set_user_data(struct display *display, void *data)
6348{
6349 display->user_data = data;
6350}
6351
6352void *
6353display_get_user_data(struct display *display)
6354{
6355 return display->user_data;
6356}
6357
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006358struct wl_display *
6359display_get_display(struct display *display)
6360{
6361 return display->display;
6362}
6363
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006364int
6365display_has_subcompositor(struct display *display)
6366{
6367 if (display->subcompositor)
6368 return 1;
6369
6370 wl_display_roundtrip(display->display);
6371
6372 return display->subcompositor != NULL;
6373}
6374
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006375cairo_device_t *
6376display_get_cairo_device(struct display *display)
6377{
6378 return display->argb_device;
6379}
6380
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006381struct output *
6382display_get_output(struct display *display)
6383{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006384 if (wl_list_empty(&display->output_list))
6385 return NULL;
6386
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006387 return container_of(display->output_list.next, struct output, link);
6388}
6389
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006390struct wl_compositor *
6391display_get_compositor(struct display *display)
6392{
6393 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006394}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006395
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006396uint32_t
6397display_get_serial(struct display *display)
6398{
6399 return display->serial;
6400}
6401
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006402EGLDisplay
6403display_get_egl_display(struct display *d)
6404{
6405 return d->dpy;
6406}
6407
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006408struct wl_data_source *
6409display_create_data_source(struct display *display)
6410{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006411 if (display->data_device_manager)
6412 return wl_data_device_manager_create_data_source(display->data_device_manager);
6413 else
6414 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006415}
6416
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006417EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006418display_get_argb_egl_config(struct display *d)
6419{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006420 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006421}
6422
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006423int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006424display_acquire_window_surface(struct display *display,
6425 struct window *window,
6426 EGLContext ctx)
6427{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006428 struct surface *surface = window->main_surface;
6429
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006430 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006431 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006432
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006433 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006434 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006435}
6436
6437void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006438display_release_window_surface(struct display *display,
6439 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006440{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006441 struct surface *surface = window->main_surface;
6442
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006443 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006444 return;
6445
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006446 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006447}
6448
6449void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006450display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006451{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006452 wl_list_insert(&display->deferred_list, &task->link);
6453}
6454
6455void
6456display_watch_fd(struct display *display,
6457 int fd, uint32_t events, struct task *task)
6458{
6459 struct epoll_event ep;
6460
6461 ep.events = events;
6462 ep.data.ptr = task;
6463 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6464}
6465
6466void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006467display_unwatch_fd(struct display *display, int fd)
6468{
6469 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6470}
6471
6472void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006473display_run(struct display *display)
6474{
6475 struct task *task;
6476 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006477 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006478
Pekka Paalanen826d7952011-12-15 10:14:07 +02006479 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006480 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006481 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006482 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006483 struct task, link);
6484 wl_list_remove(&task->link);
6485 task->run(task, 0);
6486 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006487
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006488 wl_display_dispatch_pending(display->display);
6489
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006490 if (!display->running)
6491 break;
6492
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006493 ret = wl_display_flush(display->display);
6494 if (ret < 0 && errno == EAGAIN) {
6495 ep[0].events =
6496 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6497 ep[0].data.ptr = &display->display_task;
6498
6499 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6500 display->display_fd, &ep[0]);
6501 } else if (ret < 0) {
6502 break;
6503 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006504
6505 count = epoll_wait(display->epoll_fd,
6506 ep, ARRAY_LENGTH(ep), -1);
6507 for (i = 0; i < count; i++) {
6508 task = ep[i].data.ptr;
6509 task->run(task, ep[i].events);
6510 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006511 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006512}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006513
6514void
6515display_exit(struct display *display)
6516{
6517 display->running = 0;
6518}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006519
Carlos Garnacho9c931792016-01-18 23:52:12 +01006520int
6521display_get_data_device_manager_version(struct display *display)
6522{
6523 return display->data_device_manager_version;
6524}
6525
Jan Arne Petersencd997062012-11-18 19:06:44 +01006526void
6527keysym_modifiers_add(struct wl_array *modifiers_map,
6528 const char *name)
6529{
6530 size_t len = strlen(name) + 1;
6531 char *p;
6532
6533 p = wl_array_add(modifiers_map, len);
6534
6535 if (p == NULL)
6536 return;
6537
6538 strncpy(p, name, len);
6539}
6540
6541static xkb_mod_index_t
6542keysym_modifiers_get_index(struct wl_array *modifiers_map,
6543 const char *name)
6544{
6545 xkb_mod_index_t index = 0;
6546 char *p = modifiers_map->data;
6547
6548 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6549 if (strcmp(p, name) == 0)
6550 return index;
6551
6552 index++;
6553 p += strlen(p) + 1;
6554 }
6555
6556 return XKB_MOD_INVALID;
6557}
6558
6559xkb_mod_mask_t
6560keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6561 const char *name)
6562{
6563 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6564
6565 if (index == XKB_MOD_INVALID)
6566 return XKB_MOD_INVALID;
6567
6568 return 1 << index;
6569}