blob: d55d27eb3d5b98862079331286e610e84b12d7f9 [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>
Daniel Stone4b341a82017-02-16 21:52:23 +000058#elif !defined(ENABLE_EGL) /* platform.h defines these if EGL is enabled */
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030059typedef 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;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800281 bool pointer_locked;
282 locked_pointer_locked_handler_t pointer_locked_handler;
283 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
284 confined_pointer_confined_handler_t pointer_confined_handler;
285 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
286
287 struct zwp_confined_pointer_v1 *confined_pointer;
288 struct widget *confined_widget;
289 bool confined;
290
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500291 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400292 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500293};
294
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500295struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500296 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200297 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300298 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500299 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400300 struct wl_list link;
301 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500302 widget_resize_handler_t resize_handler;
303 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500304 widget_enter_handler_t enter_handler;
305 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500306 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500307 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700308 widget_touch_down_handler_t touch_down_handler;
309 widget_touch_up_handler_t touch_up_handler;
310 widget_touch_motion_handler_t touch_motion_handler;
311 widget_touch_frame_handler_t touch_frame_handler;
312 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200313 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000314 widget_pointer_frame_handler_t pointer_frame_handler;
315 widget_axis_source_handler_t axis_source_handler;
316 widget_axis_stop_handler_t axis_stop_handler;
317 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400318 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500319 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300320 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500321 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000322 /* If this is set to false then no cairo surface will be
323 * created before redrawing the surface. This is useful if the
324 * redraw handler is going to do completely custom rendering
325 * such as using EGL directly */
326 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400327};
328
Rusty Lynch041815a2013-08-08 21:20:38 -0700329struct touch_point {
330 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800331 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700332 struct widget *widget;
333 struct wl_list link;
334};
335
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400336struct input {
337 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100338 struct wl_seat *seat;
339 struct wl_pointer *pointer;
340 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700341 struct wl_touch *touch;
342 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400343 struct window *pointer_focus;
344 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700345 struct window *touch_focus;
Dima Ryazanov13bdf252018-11-14 22:17:42 -0800346 struct window *locked_window;
347 struct window *confined_window;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300348 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300349 uint32_t cursor_anim_start;
350 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500351 uint32_t cursor_timer_start;
352 uint32_t cursor_anim_current;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200353 struct toytimer cursor_timer;
Derek Foreman118a4292015-04-22 17:23:35 -0500354 bool cursor_timer_running;
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
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200392 struct toytimer repeat_timer;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400393 uint32_t repeat_sym;
394 uint32_t repeat_key;
395 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500396 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400397};
398
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500399struct output {
400 struct display *display;
401 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800402 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500403 struct rectangle allocation;
404 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600405 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200406 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500407 char *make;
408 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200409
410 display_output_handler_t destroy_handler;
411 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500412};
413
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500414struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500415 struct widget *widget;
416 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500417 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800418
419 uint32_t last_time;
420 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800421 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500422};
423
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500424struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400425 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500426 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500427 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500428 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700429 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500430 const char **entries;
431 uint32_t time;
432 int current;
433 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400434 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500435 menu_func_t func;
436};
437
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300438struct tooltip {
439 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300440 struct widget *widget;
441 char *entry;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200442 struct toytimer timer;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300443 float x, y;
444};
445
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700446struct shm_pool {
447 struct wl_shm_pool *pool;
448 size_t size;
449 size_t used;
450 void *data;
451};
452
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400453enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300454 CURSOR_DEFAULT = 100,
455 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400456};
457
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200458static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400459
Pekka Paalanen97777442013-05-22 10:20:05 +0300460/* #define DEBUG */
461
462#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300463
464static void
465debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
466__attribute__ ((format (printf, 4, 5)));
467
468static void
469debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
470{
471 va_list ap;
472 struct timeval tv;
473
474 gettimeofday(&tv, NULL);
475 fprintf(stderr, "%8ld.%03ld ",
476 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
477
478 if (proxy)
479 fprintf(stderr, "%s@%d ",
480 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
481
482 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
483 fprintf(stderr, "%s ", func);
484
485 va_start(ap, fmt);
486 vfprintf(stderr, fmt, ap);
487 va_end(ap);
488}
489
490#define DBG(fmt, ...) \
491 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
492
493#define DBG_OBJ(obj, fmt, ...) \
494 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
495
496#else
497
498#define DBG(...) do {} while (0)
499#define DBG_OBJ(...) do {} while (0)
500
501#endif
502
Alexander Larsson1818e312013-05-22 14:41:31 +0200503static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200504surface_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 +0200505{
506 int32_t tmp;
507
508 switch (buffer_transform) {
509 case WL_OUTPUT_TRANSFORM_90:
510 case WL_OUTPUT_TRANSFORM_270:
511 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
512 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
513 tmp = *width;
514 *width = *height;
515 *height = tmp;
516 break;
517 default:
518 break;
519 }
520
521 *width *= buffer_scale;
522 *height *= buffer_scale;
523}
524
525static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200526buffer_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 +0200527{
528 int32_t tmp;
529
530 switch (buffer_transform) {
531 case WL_OUTPUT_TRANSFORM_90:
532 case WL_OUTPUT_TRANSFORM_270:
533 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
534 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
535 tmp = *width;
536 *width = *height;
537 *height = tmp;
538 break;
539 default:
540 break;
541 }
542
543 *width /= buffer_scale;
544 *height /= buffer_scale;
545}
546
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500547#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200549struct egl_window_surface {
550 struct toysurface base;
551 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100552 struct display *display;
553 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200554 struct wl_egl_window *egl_window;
555 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100556};
557
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200558static struct egl_window_surface *
559to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100560{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200561 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100562}
563
564static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200565egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200566 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200567 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100568{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200569 struct egl_window_surface *surface = to_egl_window_surface(base);
570
Alexander Larsson1818e312013-05-22 14:41:31 +0200571 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
572
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200573 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
574 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
575
576 return cairo_surface_reference(surface->cairo_surface);
577}
578
579static void
580egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200581 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200582 struct rectangle *server_allocation)
583{
584 struct egl_window_surface *surface = to_egl_window_surface(base);
585
586 cairo_gl_surface_swapbuffers(surface->cairo_surface);
587 wl_egl_window_get_attached_size(surface->egl_window,
588 &server_allocation->width,
589 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200590
591 buffer_to_surface_size (buffer_transform, buffer_scale,
592 &server_allocation->width,
593 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200594}
595
596static int
597egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
598{
599 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200600 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100601
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200602 device = cairo_surface_get_device(surface->cairo_surface);
603 if (!device)
604 return -1;
605
606 if (!ctx) {
607 if (device == surface->display->argb_device)
608 ctx = surface->display->argb_ctx;
609 else
610 assert(0);
611 }
612
613 cairo_device_flush(device);
614 cairo_device_acquire(device);
615 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
616 surface->egl_surface, ctx))
617 fprintf(stderr, "failed to make surface current\n");
618
619 return 0;
620}
621
622static void
623egl_window_surface_release(struct toysurface *base)
624{
625 struct egl_window_surface *surface = to_egl_window_surface(base);
626 cairo_device_t *device;
627
628 device = cairo_surface_get_device(surface->cairo_surface);
629 if (!device)
630 return;
631
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200632 if (!eglMakeCurrent(surface->display->dpy,
633 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200634 fprintf(stderr, "failed to make context current\n");
635
636 cairo_device_release(device);
637}
638
639static void
640egl_window_surface_destroy(struct toysurface *base)
641{
642 struct egl_window_surface *surface = to_egl_window_surface(base);
643 struct display *d = surface->display;
644
645 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000646 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200647 wl_egl_window_destroy(surface->egl_window);
648 surface->surface = NULL;
649
650 free(surface);
651}
652
653static struct toysurface *
654egl_window_surface_create(struct display *display,
655 struct wl_surface *wl_surface,
656 uint32_t flags,
657 struct rectangle *rectangle)
658{
659 struct egl_window_surface *surface;
660
Pekka Paalanenb3627362012-11-19 17:16:00 +0200661 if (display->dpy == EGL_NO_DISPLAY)
662 return NULL;
663
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800664 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200665 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100666 return NULL;
667
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200668 surface->base.prepare = egl_window_surface_prepare;
669 surface->base.swap = egl_window_surface_swap;
670 surface->base.acquire = egl_window_surface_acquire;
671 surface->base.release = egl_window_surface_release;
672 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100673
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200674 surface->display = display;
675 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400676
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200677 surface->egl_window = wl_egl_window_create(surface->surface,
678 rectangle->width,
679 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100680
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100681 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100682 weston_platform_create_egl_surface(display->dpy,
683 display->argb_config,
684 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100685
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200686 surface->cairo_surface =
687 cairo_gl_surface_create_for_egl(display->argb_device,
688 surface->egl_surface,
689 rectangle->width,
690 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100691
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200692 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100693}
694
Pekka Paalanenb3627362012-11-19 17:16:00 +0200695#else
696
697static struct toysurface *
698egl_window_surface_create(struct display *display,
699 struct wl_surface *wl_surface,
700 uint32_t flags,
701 struct rectangle *rectangle)
702{
703 return NULL;
704}
705
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706#endif
707
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200708struct shm_surface_data {
709 struct wl_buffer *buffer;
710 struct shm_pool *pool;
711};
712
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400713struct wl_buffer *
714display_get_buffer_for_surface(struct display *display,
715 cairo_surface_t *surface)
716{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200717 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400718
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200719 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400720
721 return data->buffer;
722}
723
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500724static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700725shm_pool_destroy(struct shm_pool *pool);
726
727static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400728shm_surface_data_destroy(void *p)
729{
730 struct shm_surface_data *data = p;
731
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200732 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700733 if (data->pool)
734 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300735
736 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400737}
738
Kristian Høgsberg16626282012-04-03 11:21:27 -0400739static struct wl_shm_pool *
740make_shm_pool(struct display *display, int size, void **data)
741{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400742 struct wl_shm_pool *pool;
743 int fd;
744
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300745 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400746 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300747 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
748 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400749 return NULL;
750 }
751
752 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400753 if (*data == MAP_FAILED) {
754 fprintf(stderr, "mmap failed: %m\n");
755 close(fd);
756 return NULL;
757 }
758
759 pool = wl_shm_create_pool(display->shm, fd, size);
760
761 close(fd);
762
763 return pool;
764}
765
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700766static struct shm_pool *
767shm_pool_create(struct display *display, size_t size)
768{
769 struct shm_pool *pool = malloc(sizeof *pool);
770
771 if (!pool)
772 return NULL;
773
774 pool->pool = make_shm_pool(display, size, &pool->data);
775 if (!pool->pool) {
776 free(pool);
777 return NULL;
778 }
779
780 pool->size = size;
781 pool->used = 0;
782
783 return pool;
784}
785
786static void *
787shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
788{
789 if (pool->used + size > pool->size)
790 return NULL;
791
792 *offset = pool->used;
793 pool->used += size;
794
795 return (char *) pool->data + *offset;
796}
797
798/* destroy the pool. this does not unmap the memory though */
799static void
800shm_pool_destroy(struct shm_pool *pool)
801{
802 munmap(pool->data, pool->size);
803 wl_shm_pool_destroy(pool->pool);
804 free(pool);
805}
806
807/* Start allocating from the beginning of the pool again */
808static void
809shm_pool_reset(struct shm_pool *pool)
810{
811 pool->used = 0;
812}
813
814static int
815data_length_for_shm_surface(struct rectangle *rect)
816{
817 int stride;
818
819 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
820 rect->width);
821 return stride * rect->height;
822}
823
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500824static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700825display_create_shm_surface_from_pool(struct display *display,
826 struct rectangle *rectangle,
827 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400828{
829 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400830 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400831 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200832 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700833 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400834 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835
836 data = malloc(sizeof *data);
837 if (data == NULL)
838 return NULL;
839
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200840 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
841 cairo_format = CAIRO_FORMAT_RGB16_565;
842 else
843 cairo_format = CAIRO_FORMAT_ARGB32;
844
845 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700846 length = stride * rectangle->height;
847 data->pool = NULL;
848 map = shm_pool_allocate(pool, length, &offset);
849
850 if (!map) {
851 free(data);
852 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400853 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400854
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400855 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200856 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400857 rectangle->width,
858 rectangle->height,
859 stride);
860
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200861 cairo_surface_set_user_data(surface, &shm_surface_data_key,
862 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400863
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200864 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
865 format = WL_SHM_FORMAT_RGB565;
866 else {
867 if (flags & SURFACE_OPAQUE)
868 format = WL_SHM_FORMAT_XRGB8888;
869 else
870 format = WL_SHM_FORMAT_ARGB8888;
871 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400872
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200873 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
874 rectangle->width,
875 rectangle->height,
876 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400877
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700878 return surface;
879}
880
881static cairo_surface_t *
882display_create_shm_surface(struct display *display,
883 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200884 struct shm_pool *alternate_pool,
885 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700886{
887 struct shm_surface_data *data;
888 struct shm_pool *pool;
889 cairo_surface_t *surface;
890
Pekka Paalanen99436862012-11-19 17:15:59 +0200891 if (alternate_pool) {
892 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700893 surface = display_create_shm_surface_from_pool(display,
894 rectangle,
895 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200896 alternate_pool);
897 if (surface) {
898 data = cairo_surface_get_user_data(surface,
899 &shm_surface_data_key);
900 goto out;
901 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700902 }
903
904 pool = shm_pool_create(display,
905 data_length_for_shm_surface(rectangle));
906 if (!pool)
907 return NULL;
908
909 surface =
910 display_create_shm_surface_from_pool(display, rectangle,
911 flags, pool);
912
913 if (!surface) {
914 shm_pool_destroy(pool);
915 return NULL;
916 }
917
918 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200919 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700920 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400921
Pekka Paalanen99436862012-11-19 17:15:59 +0200922out:
923 if (data_ret)
924 *data_ret = data;
925
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400926 return surface;
927}
928
nobled7b87cb02011-02-01 18:51:47 +0000929static int
930check_size(struct rectangle *rect)
931{
932 if (rect->width && rect->height)
933 return 0;
934
935 fprintf(stderr, "tried to create surface of "
936 "width: %d, height: %d\n", rect->width, rect->height);
937 return -1;
938}
939
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940cairo_surface_t *
941display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400943 struct rectangle *rectangle,
944 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945{
nobled7b87cb02011-02-01 18:51:47 +0000946 if (check_size(rectangle) < 0)
947 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200948
949 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200950 return display_create_shm_surface(display, rectangle, flags,
951 NULL, NULL);
952}
953
Pekka Paalanena4eda732012-11-19 17:16:02 +0200954struct shm_surface_leaf {
955 cairo_surface_t *cairo_surface;
956 /* 'data' is automatically destroyed, when 'cairo_surface' is */
957 struct shm_surface_data *data;
958
959 struct shm_pool *resize_pool;
960 int busy;
961};
962
963static void
964shm_surface_leaf_release(struct shm_surface_leaf *leaf)
965{
966 if (leaf->cairo_surface)
967 cairo_surface_destroy(leaf->cairo_surface);
968 /* leaf->data already destroyed via cairo private */
969
970 if (leaf->resize_pool)
971 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200972
973 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200974}
975
Pekka Paalanenaef02542013-04-25 13:57:47 +0300976#define MAX_LEAVES 3
977
Pekka Paalanen99436862012-11-19 17:15:59 +0200978struct shm_surface {
979 struct toysurface base;
980 struct display *display;
981 struct wl_surface *surface;
982 uint32_t flags;
983 int dx, dy;
984
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200986 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200987};
988
989static struct shm_surface *
990to_shm_surface(struct toysurface *base)
991{
992 return container_of(base, struct shm_surface, base);
993}
994
Pekka Paalanena4eda732012-11-19 17:16:02 +0200995static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300996shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
997{
998#ifdef DEBUG
999 struct shm_surface_leaf *leaf;
1000 char bufs[MAX_LEAVES + 1];
1001 int i;
1002
1003 for (i = 0; i < MAX_LEAVES; i++) {
1004 leaf = &surface->leaf[i];
1005
1006 if (leaf->busy)
1007 bufs[i] = 'b';
1008 else if (leaf->cairo_surface)
1009 bufs[i] = 'a';
1010 else
1011 bufs[i] = ' ';
1012 }
1013
1014 bufs[MAX_LEAVES] = '\0';
1015 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1016#endif
1017}
1018
1019static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001020shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1021{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001022 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001023 struct shm_surface_leaf *leaf;
1024 int i;
1025 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001026
1027 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001028
Pekka Paalanenaef02542013-04-25 13:57:47 +03001029 for (i = 0; i < MAX_LEAVES; i++) {
1030 leaf = &surface->leaf[i];
1031 if (leaf->data && leaf->data->buffer == buffer) {
1032 leaf->busy = 0;
1033 break;
1034 }
1035 }
1036 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001037
Pekka Paalanenaef02542013-04-25 13:57:47 +03001038 /* Leave one free leaf with storage, release others */
1039 free_found = 0;
1040 for (i = 0; i < MAX_LEAVES; i++) {
1041 leaf = &surface->leaf[i];
1042
1043 if (!leaf->cairo_surface || leaf->busy)
1044 continue;
1045
1046 if (!free_found)
1047 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001048 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001049 shm_surface_leaf_release(leaf);
1050 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001051
Pekka Paalanen97777442013-05-22 10:20:05 +03001052 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001053}
1054
1055static const struct wl_buffer_listener shm_surface_buffer_listener = {
1056 shm_surface_buffer_release
1057};
1058
Pekka Paalanen99436862012-11-19 17:15:59 +02001059static cairo_surface_t *
1060shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001061 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001062 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001063{
Pekka Paalanenec076692012-11-30 13:37:27 +02001064 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001065 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001066 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001067 struct shm_surface_leaf *leaf = NULL;
1068 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001069
1070 surface->dx = dx;
1071 surface->dy = dy;
1072
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001073 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001074 for (i = 0; i < MAX_LEAVES; i++) {
1075 if (surface->leaf[i].busy)
1076 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001077
Pekka Paalanenaef02542013-04-25 13:57:47 +03001078 if (!leaf || surface->leaf[i].cairo_surface)
1079 leaf = &surface->leaf[i];
1080 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001081 DBG_OBJ(surface->surface, "pick leaf %d\n",
1082 (int)(leaf - &surface->leaf[0]));
1083
Pekka Paalanenaef02542013-04-25 13:57:47 +03001084 if (!leaf) {
1085 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001086 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001087 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001089 }
1090
Pekka Paalanena4eda732012-11-19 17:16:02 +02001091 if (!resize_hint && leaf->resize_pool) {
1092 cairo_surface_destroy(leaf->cairo_surface);
1093 leaf->cairo_surface = NULL;
1094 shm_pool_destroy(leaf->resize_pool);
1095 leaf->resize_pool = NULL;
1096 }
1097
Alexander Larsson1818e312013-05-22 14:41:31 +02001098 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1099
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 if (leaf->cairo_surface &&
1101 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1102 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001103 goto out;
1104
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 if (leaf->cairo_surface)
1106 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001107
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001108#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001109 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001110 /* Create a big pool to allocate from, while continuously
1111 * resizing. Mmapping a new pool in the server
1112 * is relatively expensive, so reusing a pool performs
1113 * better, but may temporarily reserve unneeded memory.
1114 */
1115 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 leaf->resize_pool = shm_pool_create(surface->display,
1117 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001118 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001119#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001120
Alexander Larsson1818e312013-05-22 14:41:31 +02001121 rect.width = width;
1122 rect.height = height;
1123
Pekka Paalanena4eda732012-11-19 17:16:02 +02001124 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001125 display_create_shm_surface(surface->display, &rect,
1126 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127 leaf->resize_pool,
1128 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001129 if (!leaf->cairo_surface)
1130 return NULL;
1131
Pekka Paalanena4eda732012-11-19 17:16:02 +02001132 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001133 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001134
1135out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001136 surface->current = leaf;
1137
1138 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001139}
1140
1141static void
1142shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001143 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001144 struct rectangle *server_allocation)
1145{
1146 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001147 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001148
1149 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001150 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001151 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001152 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001153
Alexander Larsson1818e312013-05-22 14:41:31 +02001154 buffer_to_surface_size (buffer_transform, buffer_scale,
1155 &server_allocation->width,
1156 &server_allocation->height);
1157
Pekka Paalanena4eda732012-11-19 17:16:02 +02001158 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001159 surface->dx, surface->dy);
1160 wl_surface_damage(surface->surface, 0, 0,
1161 server_allocation->width, server_allocation->height);
1162 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001163
Pekka Paalanen71233882013-04-25 13:57:53 +03001164 DBG_OBJ(surface->surface, "leaf %d busy\n",
1165 (int)(leaf - &surface->leaf[0]));
1166
Pekka Paalanena4eda732012-11-19 17:16:02 +02001167 leaf->busy = 1;
1168 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001169}
1170
1171static int
1172shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1173{
1174 return -1;
1175}
1176
1177static void
1178shm_surface_release(struct toysurface *base)
1179{
1180}
1181
1182static void
1183shm_surface_destroy(struct toysurface *base)
1184{
1185 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001186 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001187
Pekka Paalanenaef02542013-04-25 13:57:47 +03001188 for (i = 0; i < MAX_LEAVES; i++)
1189 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001190
1191 free(surface);
1192}
1193
1194static struct toysurface *
1195shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1196 uint32_t flags, struct rectangle *rectangle)
1197{
1198 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001199 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001200
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001201 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001202 surface->base.prepare = shm_surface_prepare;
1203 surface->base.swap = shm_surface_swap;
1204 surface->base.acquire = shm_surface_acquire;
1205 surface->base.release = shm_surface_release;
1206 surface->base.destroy = shm_surface_destroy;
1207
1208 surface->display = display;
1209 surface->surface = wl_surface;
1210 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001211
1212 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001213}
1214
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215/*
1216 * The following correspondences between file names and cursors was copied
1217 * from: https://bugs.kde.org/attachment.cgi?id=67313
1218 */
1219
1220static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "sw-resize",
1223 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "se-resize",
1229 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "s-resize",
1235 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001240 "closedhand",
1241 "208530c400c041818281048008011002"
1242};
1243
1244static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001246 "default",
1247 "top_left_arrow",
1248 "left-arrow"
1249};
1250
1251static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001252 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001253 "w-resize",
1254 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001255};
1256
1257static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001258 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001259 "e-resize",
1260 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001261};
1262
1263static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001264 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001265 "nw-resize",
1266 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001267};
1268
1269static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001270 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001271 "ne-resize",
1272 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001273};
1274
1275static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001276 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001277 "n-resize",
1278 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001279};
1280
1281static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001282 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001283 "ibeam",
1284 "text"
1285};
1286
1287static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001288 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001289 "pointer",
1290 "pointing_hand",
1291 "e29285e634086352946a0e7090d73106"
1292};
1293
1294static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001295 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001296 "wait",
1297 "0426c94ea35c87780ff01dc239897213"
1298};
1299
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001300static const char *move_draggings[] = {
1301 "dnd-move"
1302};
1303
1304static const char *copy_draggings[] = {
1305 "dnd-copy"
1306};
1307
1308static const char *forbidden_draggings[] = {
1309 "dnd-none",
1310 "dnd-no-drop"
1311};
1312
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001313struct cursor_alternatives {
1314 const char **names;
1315 size_t count;
1316};
1317
1318static const struct cursor_alternatives cursors[] = {
1319 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1320 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1321 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1322 {grabbings, ARRAY_LENGTH(grabbings)},
1323 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1324 {left_sides, ARRAY_LENGTH(left_sides)},
1325 {right_sides, ARRAY_LENGTH(right_sides)},
1326 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1327 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1328 {top_sides, ARRAY_LENGTH(top_sides)},
1329 {xterms, ARRAY_LENGTH(xterms)},
1330 {hand1s, ARRAY_LENGTH(hand1s)},
1331 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001332 {move_draggings, ARRAY_LENGTH(move_draggings)},
1333 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1334 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001335};
1336
1337static void
1338create_cursors(struct display *display)
1339{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001340 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001341 struct weston_config *config;
1342 struct weston_config_section *s;
emersion807cd2e2018-10-10 09:46:12 +02001343 int size = 32;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001344 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001345 unsigned int i, j;
1346 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001347
emersion807cd2e2018-10-10 09:46:12 +02001348 theme = getenv("XCURSOR_THEME");
1349 if (getenv("XCURSOR_SIZE"))
1350 size = atoi(getenv("XCURSOR_SIZE"));
1351
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001352 config_file = weston_config_get_name_from_env();
1353 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001354 s = weston_config_get_section(config, "shell", NULL, NULL);
emersion807cd2e2018-10-10 09:46:12 +02001355 weston_config_section_get_string(s, "cursor-theme", &theme, theme);
1356 weston_config_section_get_int(s, "cursor-size", &size, size);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001357 weston_config_destroy(config);
1358
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001359 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001360 if (!display->cursor_theme) {
1361 fprintf(stderr, "could not load theme '%s'\n", theme);
1362 return;
1363 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001364 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001365 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001366 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001367
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001368 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001369 cursor = NULL;
1370 for (j = 0; !cursor && j < cursors[i].count; ++j)
1371 cursor = wl_cursor_theme_get_cursor(
1372 display->cursor_theme, cursors[i].names[j]);
1373
1374 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001375 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001376 cursors[i].names[0]);
1377
1378 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001379 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001380}
1381
1382static void
1383destroy_cursors(struct display *display)
1384{
1385 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001386 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001387}
1388
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001389struct wl_cursor_image *
1390display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001391{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001392 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001393
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001394 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001395}
1396
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001397static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001398surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001399{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001400 if (!surface->cairo_surface)
1401 return;
1402
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001403 if (surface->opaque_region) {
1404 wl_surface_set_opaque_region(surface->surface,
1405 surface->opaque_region);
1406 wl_region_destroy(surface->opaque_region);
1407 surface->opaque_region = NULL;
1408 }
1409
1410 if (surface->input_region) {
1411 wl_surface_set_input_region(surface->surface,
1412 surface->input_region);
1413 wl_region_destroy(surface->input_region);
1414 surface->input_region = NULL;
1415 }
1416
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001418 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001419 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001420
Pekka Paalanen89dee002013-02-13 16:17:20 +02001421 cairo_surface_destroy(surface->cairo_surface);
1422 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001423}
1424
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001425int
1426window_has_focus(struct window *window)
1427{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001428 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001429}
1430
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001431static void
1432window_close(struct window *window)
1433{
1434 if (window->close_handler)
1435 window->close_handler(window->user_data);
1436 else
1437 display_exit(window->display);
1438}
1439
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001440struct display *
1441window_get_display(struct window *window)
1442{
1443 return window->display;
1444}
1445
Pekka Paalanen89dee002013-02-13 16:17:20 +02001446static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001447handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1448 int32_t width, int32_t height)
1449{
1450 struct window *window = data;
1451
1452 window_schedule_resize(window, width, height);
1453}
1454
1455static const struct ivi_surface_listener ivi_surface_listener = {
1456 handle_ivi_surface_configure,
1457};
1458
1459static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001460surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001461{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001462 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001463 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001464
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001465 if (!surface->toysurface && display->dpy &&
1466 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001467 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001468 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001469 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001470 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001471 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001472 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001473
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001474 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001475 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001476 surface->surface,
1477 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001478
Pekka Paalanen89dee002013-02-13 16:17:20 +02001479 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001480 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001481 allocation.width, allocation.height, flags,
1482 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001483}
1484
1485static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001486window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001487{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001488 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001489 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001490
Pekka Paalanenec076692012-11-30 13:37:27 +02001491 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001492 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001493
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001494 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1495 flags |= SURFACE_HINT_RGB565;
1496
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001497 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001498}
1499
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001500int
1501window_get_buffer_transform(struct window *window)
1502{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001503 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001504}
1505
1506void
1507window_set_buffer_transform(struct window *window,
1508 enum wl_output_transform transform)
1509{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001510 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001511 wl_surface_set_buffer_transform(window->main_surface->surface,
1512 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001513}
1514
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001515void
1516window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001517 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001518{
1519 window->main_surface->buffer_scale = scale;
1520 wl_surface_set_buffer_scale(window->main_surface->surface,
1521 scale);
1522}
1523
1524uint32_t
1525window_get_buffer_scale(struct window *window)
1526{
1527 return window->main_surface->buffer_scale;
1528}
1529
Alexander Larssond68f5232013-05-22 14:41:33 +02001530uint32_t
1531window_get_output_scale(struct window *window)
1532{
1533 struct window_output *window_output;
1534 struct window_output *window_output_tmp;
1535 int scale = 1;
1536
1537 wl_list_for_each_safe(window_output, window_output_tmp,
1538 &window->window_output_list, link) {
1539 if (window_output->output->scale > scale)
1540 scale = window_output->output->scale;
1541 }
1542
1543 return scale;
1544}
1545
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001546static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001547
Pekka Paalanen4e373742013-02-13 16:17:13 +02001548static void
1549surface_destroy(struct surface *surface)
1550{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001551 if (surface->frame_cb)
1552 wl_callback_destroy(surface->frame_cb);
1553
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001554 if (surface->input_region)
1555 wl_region_destroy(surface->input_region);
1556
1557 if (surface->opaque_region)
1558 wl_region_destroy(surface->opaque_region);
1559
Pekka Paalanen35e82632013-04-25 13:57:48 +03001560 if (surface->subsurface)
1561 wl_subsurface_destroy(surface->subsurface);
1562
Pekka Paalanen4e373742013-02-13 16:17:13 +02001563 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001564
1565 if (surface->toysurface)
1566 surface->toysurface->destroy(surface->toysurface);
1567
Pekka Paalanen35e82632013-04-25 13:57:48 +03001568 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001569 free(surface);
1570}
1571
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001572void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001573window_destroy(struct window *window)
1574{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001575 struct display *display = window->display;
1576 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001577 struct window_output *window_output;
1578 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001579
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001580 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001581
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001582 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001583 if (input->touch_focus == window)
1584 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001585 if (input->pointer_focus == window)
1586 input->pointer_focus = NULL;
1587 if (input->keyboard_focus == window)
1588 input->keyboard_focus = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08001589 if (input->locked_window == window)
1590 input->locked_window = NULL;
1591 if (input->confined_window == window)
1592 input->confined_window = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001593 if (input->focus_widget &&
1594 input->focus_widget->window == window)
1595 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001596 }
1597
Rob Bradford7507b572012-05-15 17:55:34 +01001598 wl_list_for_each_safe(window_output, window_output_tmp,
1599 &window->window_output_list, link) {
1600 free (window_output);
1601 }
1602
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001603 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001604 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001605
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001606 if (window->xdg_toplevel)
1607 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001608 if (window->xdg_popup)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001609 zxdg_popup_v6_destroy(window->xdg_popup);
1610 if (window->xdg_surface)
1611 zxdg_surface_v6_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001612
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001613 if (window->ivi_surface)
1614 ivi_surface_destroy(window->ivi_surface);
1615
Pekka Paalanen4e373742013-02-13 16:17:13 +02001616 surface_destroy(window->main_surface);
1617
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001618 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001619
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001620 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001621 free(window);
1622}
1623
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001624static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001625widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001626{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001627 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001628
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001629 wl_list_for_each(child, &widget->child_list, link) {
1630 target = widget_find_widget(child, x, y);
1631 if (target)
1632 return target;
1633 }
1634
1635 if (widget->allocation.x <= x &&
1636 x < widget->allocation.x + widget->allocation.width &&
1637 widget->allocation.y <= y &&
1638 y < widget->allocation.y + widget->allocation.height) {
1639 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001640 }
1641
1642 return NULL;
1643}
1644
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001645static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001646window_find_widget(struct window *window, int32_t x, int32_t y)
1647{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001648 struct surface *surface;
1649 struct widget *widget;
1650
1651 wl_list_for_each(surface, &window->subsurface_list, link) {
1652 widget = widget_find_widget(surface->widget, x, y);
1653 if (widget)
1654 return widget;
1655 }
1656
1657 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001658}
1659
1660static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001661widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001663 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664
Peter Huttererf3d62272013-08-08 11:57:05 +10001665 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001666 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001667 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001668 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001669 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001670 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001671 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001672 widget->tooltip = NULL;
1673 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001674 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001675 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001676
1677 return widget;
1678}
1679
1680struct widget *
1681window_add_widget(struct window *window, void *data)
1682{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001683 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001684
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001685 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001686 wl_list_init(&widget->link);
1687 window->main_surface->widget = widget;
1688
1689 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001690}
1691
1692struct widget *
1693widget_add_widget(struct widget *parent, void *data)
1694{
1695 struct widget *widget;
1696
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001697 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001698 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001699
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001700 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001701}
1702
1703void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001704widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001705{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001706 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001707 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001708 struct input *input;
1709
Pekka Paalanen35e82632013-04-25 13:57:48 +03001710 /* Destroy the sub-surface along with the root widget */
1711 if (surface->widget == widget && surface->subsurface)
1712 surface_destroy(widget->surface);
1713
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001714 if (widget->tooltip)
1715 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001716
Pekka Paalanene156fb62012-01-19 13:51:38 +02001717 wl_list_for_each(input, &display->input_list, link) {
1718 if (input->focus_widget == widget)
1719 input->focus_widget = NULL;
1720 }
1721
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001722 wl_list_remove(&widget->link);
1723 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001724}
1725
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001726void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001727widget_set_default_cursor(struct widget *widget, int cursor)
1728{
1729 widget->default_cursor = cursor;
1730}
1731
1732void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001733widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001734{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001735 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001736}
1737
1738void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001739widget_set_size(struct widget *widget, int32_t width, int32_t height)
1740{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001741 widget->allocation.width = width;
1742 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001743}
1744
1745void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001746widget_set_allocation(struct widget *widget,
1747 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001748{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001749 widget->allocation.x = x;
1750 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001751 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001752}
1753
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001754void
1755widget_set_transparent(struct widget *widget, int transparent)
1756{
1757 widget->opaque = !transparent;
1758}
1759
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001760void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001761widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001762{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001763 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001764}
1765
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001766static cairo_surface_t *
1767widget_get_cairo_surface(struct widget *widget)
1768{
1769 struct surface *surface = widget->surface;
1770 struct window *window = widget->window;
1771
Neil Roberts97b747c2013-12-19 16:17:12 +00001772 assert(widget->use_cairo);
1773
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001774 if (!surface->cairo_surface) {
1775 if (surface == window->main_surface)
1776 window_create_main_surface(window);
1777 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001778 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001779 }
1780
1781 return surface->cairo_surface;
1782}
1783
Alexander Larsson15901f02013-05-22 14:41:25 +02001784static void
1785widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1786{
1787 struct surface *surface = widget->surface;
1788 double angle;
1789 cairo_matrix_t m;
1790 enum wl_output_transform transform;
1791 int surface_width, surface_height;
1792 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001793 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001794
1795 surface_width = surface->allocation.width;
1796 surface_height = surface->allocation.height;
1797
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001798 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001799 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001800
Alexander Larsson15901f02013-05-22 14:41:25 +02001801 switch (transform) {
1802 case WL_OUTPUT_TRANSFORM_FLIPPED:
1803 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1804 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1805 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1806 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1807 break;
1808 default:
1809 cairo_matrix_init_identity(&m);
1810 break;
1811 }
1812
1813 switch (transform) {
1814 case WL_OUTPUT_TRANSFORM_NORMAL:
1815 default:
1816 angle = 0;
1817 translate_x = 0;
1818 translate_y = 0;
1819 break;
1820 case WL_OUTPUT_TRANSFORM_FLIPPED:
1821 angle = 0;
1822 translate_x = surface_width;
1823 translate_y = 0;
1824 break;
1825 case WL_OUTPUT_TRANSFORM_90:
1826 angle = M_PI_2;
1827 translate_x = surface_height;
1828 translate_y = 0;
1829 break;
1830 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1831 angle = M_PI_2;
1832 translate_x = surface_height;
1833 translate_y = surface_width;
1834 break;
1835 case WL_OUTPUT_TRANSFORM_180:
1836 angle = M_PI;
1837 translate_x = surface_width;
1838 translate_y = surface_height;
1839 break;
1840 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1841 angle = M_PI;
1842 translate_x = 0;
1843 translate_y = surface_height;
1844 break;
1845 case WL_OUTPUT_TRANSFORM_270:
1846 angle = M_PI + M_PI_2;
1847 translate_x = 0;
1848 translate_y = surface_width;
1849 break;
1850 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1851 angle = M_PI + M_PI_2;
1852 translate_x = 0;
1853 translate_y = 0;
1854 break;
1855 }
1856
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001857 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001858 cairo_translate(cr, translate_x, translate_y);
1859 cairo_rotate(cr, angle);
1860 cairo_transform(cr, &m);
1861}
1862
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001863cairo_t *
1864widget_cairo_create(struct widget *widget)
1865{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001866 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001867 cairo_surface_t *cairo_surface;
1868 cairo_t *cr;
1869
1870 cairo_surface = widget_get_cairo_surface(widget);
1871 cr = cairo_create(cairo_surface);
1872
Alexander Larsson15901f02013-05-22 14:41:25 +02001873 widget_cairo_update_transform(widget, cr);
1874
Pekka Paalanen35e82632013-04-25 13:57:48 +03001875 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1876
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001877 return cr;
1878}
1879
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001880struct wl_surface *
1881widget_get_wl_surface(struct widget *widget)
1882{
1883 return widget->surface->surface;
1884}
1885
Neil Roberts5e10a042013-09-09 00:40:17 +01001886struct wl_subsurface *
1887widget_get_wl_subsurface(struct widget *widget)
1888{
1889 return widget->surface->subsurface;
1890}
1891
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001892uint32_t
1893widget_get_last_time(struct widget *widget)
1894{
1895 return widget->surface->last_time;
1896}
1897
1898void
1899widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1900{
1901 struct wl_compositor *comp = widget->window->display->compositor;
1902 struct surface *surface = widget->surface;
1903
1904 if (!surface->input_region)
1905 surface->input_region = wl_compositor_create_region(comp);
1906
1907 if (rect) {
1908 wl_region_add(surface->input_region,
1909 rect->x, rect->y, rect->width, rect->height);
1910 }
1911}
1912
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001913void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001914widget_set_resize_handler(struct widget *widget,
1915 widget_resize_handler_t handler)
1916{
1917 widget->resize_handler = handler;
1918}
1919
1920void
1921widget_set_redraw_handler(struct widget *widget,
1922 widget_redraw_handler_t handler)
1923{
1924 widget->redraw_handler = handler;
1925}
1926
1927void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001928widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001929{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001930 widget->enter_handler = handler;
1931}
1932
1933void
1934widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1935{
1936 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001937}
1938
1939void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001940widget_set_motion_handler(struct widget *widget,
1941 widget_motion_handler_t handler)
1942{
1943 widget->motion_handler = handler;
1944}
1945
1946void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001947widget_set_button_handler(struct widget *widget,
1948 widget_button_handler_t handler)
1949{
1950 widget->button_handler = handler;
1951}
1952
1953void
Rusty Lynch041815a2013-08-08 21:20:38 -07001954widget_set_touch_up_handler(struct widget *widget,
1955 widget_touch_up_handler_t handler)
1956{
1957 widget->touch_up_handler = handler;
1958}
1959
1960void
1961widget_set_touch_down_handler(struct widget *widget,
1962 widget_touch_down_handler_t handler)
1963{
1964 widget->touch_down_handler = handler;
1965}
1966
1967void
1968widget_set_touch_motion_handler(struct widget *widget,
1969 widget_touch_motion_handler_t handler)
1970{
1971 widget->touch_motion_handler = handler;
1972}
1973
1974void
1975widget_set_touch_frame_handler(struct widget *widget,
1976 widget_touch_frame_handler_t handler)
1977{
1978 widget->touch_frame_handler = handler;
1979}
1980
1981void
1982widget_set_touch_cancel_handler(struct widget *widget,
1983 widget_touch_cancel_handler_t handler)
1984{
1985 widget->touch_cancel_handler = handler;
1986}
1987
1988void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001989widget_set_axis_handler(struct widget *widget,
1990 widget_axis_handler_t handler)
1991{
1992 widget->axis_handler = handler;
1993}
1994
Peter Hutterer87743e92016-01-18 16:38:22 +10001995void
1996widget_set_pointer_frame_handler(struct widget *widget,
1997 widget_pointer_frame_handler_t handler)
1998{
1999 widget->pointer_frame_handler = handler;
2000}
2001
2002void
2003widget_set_axis_handlers(struct widget *widget,
2004 widget_axis_handler_t axis_handler,
2005 widget_axis_source_handler_t axis_source_handler,
2006 widget_axis_stop_handler_t axis_stop_handler,
2007 widget_axis_discrete_handler_t axis_discrete_handler)
2008{
2009 widget->axis_handler = axis_handler;
2010 widget->axis_source_handler = axis_source_handler;
2011 widget->axis_stop_handler = axis_stop_handler;
2012 widget->axis_discrete_handler = axis_discrete_handler;
2013}
2014
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002015static void
2016window_schedule_redraw_task(struct window *window);
2017
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002018void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002019widget_schedule_redraw(struct widget *widget)
2020{
Pekka Paalanen71233882013-04-25 13:57:53 +03002021 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002022 widget->surface->redraw_needed = 1;
2023 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002024}
2025
Neil Roberts97b747c2013-12-19 16:17:12 +00002026void
2027widget_set_use_cairo(struct widget *widget,
2028 int use_cairo)
2029{
2030 widget->use_cairo = use_cairo;
2031}
2032
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002033cairo_surface_t *
2034window_get_surface(struct window *window)
2035{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002036 cairo_surface_t *cairo_surface;
2037
2038 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2039
2040 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002041}
2042
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002043struct wl_surface *
2044window_get_wl_surface(struct window *window)
2045{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002046 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002047}
2048
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002049static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002050tooltip_redraw_handler(struct widget *widget, void *data)
2051{
2052 cairo_t *cr;
2053 const int32_t r = 3;
2054 struct tooltip *tooltip = data;
2055 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002056
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002057 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002058 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002059 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2060 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2061 cairo_paint(cr);
2062
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002063 width = widget->allocation.width;
2064 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002065 rounded_rect(cr, 0, 0, width, height, r);
2066
2067 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2068 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2069 cairo_fill(cr);
2070
2071 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2072 cairo_move_to(cr, 10, 16);
2073 cairo_show_text(cr, tooltip->entry);
2074 cairo_destroy(cr);
2075}
2076
2077static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002078get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002079{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002080 cairo_t *cr;
2081 cairo_text_extents_t extents;
2082
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002083 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002084 * created yet, and parent does not have a valid surface
2085 * outside repaint, either.
2086 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002087 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002088 cairo_text_extents(cr, tooltip->entry, &extents);
2089 cairo_destroy(cr);
2090
2091 return extents;
2092}
2093
2094static int
2095window_create_tooltip(struct tooltip *tooltip)
2096{
2097 struct widget *parent = tooltip->parent;
2098 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002099 const int offset_y = 27;
2100 const int margin = 3;
2101 cairo_text_extents_t extents;
2102
2103 if (tooltip->widget)
2104 return 0;
2105
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002106 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002107
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002108 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002109 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002110 widget_set_allocation(tooltip->widget,
2111 tooltip->x, tooltip->y + offset_y,
2112 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002113
2114 return 0;
2115}
2116
2117void
2118widget_destroy_tooltip(struct widget *parent)
2119{
2120 struct tooltip *tooltip = parent->tooltip;
2121
2122 parent->tooltip_count = 0;
2123 if (!tooltip)
2124 return;
2125
2126 if (tooltip->widget) {
2127 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002128 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002129 }
2130
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002131 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002132 free(tooltip->entry);
2133 free(tooltip);
2134 parent->tooltip = NULL;
2135}
2136
2137static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002138tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002139{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002140 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002141
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002142 window_create_tooltip(tooltip);
2143}
2144
2145#define TOOLTIP_TIMEOUT 500
2146static int
2147tooltip_timer_reset(struct tooltip *tooltip)
2148{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002149 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002150
2151 return 0;
2152}
2153
2154int
2155widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2156{
2157 struct tooltip *tooltip = parent->tooltip;
2158
2159 parent->tooltip_count++;
2160 if (tooltip) {
2161 tooltip->x = x;
2162 tooltip->y = y;
2163 tooltip_timer_reset(tooltip);
2164 return 0;
2165 }
2166
2167 /* the handler might be triggered too fast via input device motion, so
2168 * we need this check here to make sure tooltip is fully initialized */
2169 if (parent->tooltip_count > 1)
2170 return 0;
2171
2172 tooltip = malloc(sizeof *tooltip);
2173 if (!tooltip)
2174 return -1;
2175
2176 parent->tooltip = tooltip;
2177 tooltip->parent = parent;
2178 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002179 tooltip->x = x;
2180 tooltip->y = y;
2181 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002182 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2183 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002184 tooltip_timer_reset(tooltip);
2185
2186 return 0;
2187}
2188
2189static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002190frame_resize_handler(struct widget *widget,
2191 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002192{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002195 struct rectangle interior;
2196 struct rectangle input;
2197 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002198
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002199 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002200 interior.x = 0;
2201 interior.y = 0;
2202 interior.width = width;
2203 interior.height = height;
2204 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002205 frame_resize(frame->frame, width, height);
2206 frame_interior(frame->frame, &interior.x, &interior.y,
2207 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002208 }
2209
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002210 widget_set_allocation(child, interior.x, interior.y,
2211 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002213 if (child->resize_handler) {
2214 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002215 child->user_data);
2216
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002217 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 width = child->allocation.width;
2219 height = child->allocation.height;
2220 } else {
2221 frame_resize_inside(frame->frame,
2222 child->allocation.width,
2223 child->allocation.height);
2224 width = frame_width(frame->frame);
2225 height = frame_height(frame->frame);
2226 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002227 }
2228
Scott Moreauf7e498c2012-05-14 11:39:29 -06002229 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002230
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002231 widget->surface->input_region =
2232 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002233 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002234 frame_input_rect(frame->frame, &input.x, &input.y,
2235 &input.width, &input.height);
2236 wl_region_add(widget->surface->input_region,
2237 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002238 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002239 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002240 }
2241
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002242 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002243
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002244 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002245 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002246 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2247 &opaque.width, &opaque.height);
2248
2249 wl_region_add(widget->surface->opaque_region,
2250 opaque.x, opaque.y,
2251 opaque.width, opaque.height);
2252 } else {
2253 wl_region_add(widget->surface->opaque_region,
2254 0, 0, width, height);
2255 }
Martin Minarik1998b152012-05-10 02:04:35 +02002256 }
2257
Martin Minarik1998b152012-05-10 02:04:35 +02002258
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002259 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002260}
2261
2262static void
2263frame_redraw_handler(struct widget *widget, void *data)
2264{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002265 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002266 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002267 struct window *window = widget->window;
2268
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002269 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002270 return;
2271
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002272 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002273
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002274 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002275
2276 cairo_destroy(cr);
2277}
2278
2279static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002280frame_get_pointer_image_for_location(struct window_frame *frame,
2281 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002282{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002283 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002284
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002285 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002286 return CURSOR_LEFT_PTR;
2287
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002288 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002289 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002290 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002291 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002292 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002293 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002294 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002295 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002296 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002297 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002298 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002299 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002300 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002301 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002302 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002303 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002304 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002305 case THEME_LOCATION_EXTERIOR:
2306 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002307 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002308 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002309 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002310}
2311
Pekka Paalanen26237862014-09-10 15:10:30 +03002312static void
2313frame_menu_func(void *data, struct input *input, int index)
2314{
2315 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002316
2317 switch (index) {
2318 case 0: /* close */
2319 window_close(window);
2320 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002321 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002322 /* we don't have a way to get out of fullscreen for now */
2323 if (window->fullscreen_handler)
2324 window->fullscreen_handler(window, window->user_data);
2325 break;
2326 }
2327}
2328
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002329void
2330window_show_frame_menu(struct window *window,
2331 struct input *input, uint32_t time)
2332{
2333 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002334 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002335
Pekka Paalanen26237862014-09-10 15:10:30 +03002336 static const char *entries[] = {
2337 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002338 "Fullscreen"
2339 };
2340
2341 if (window->fullscreen_handler)
2342 count = ARRAY_LENGTH(entries);
2343 else
2344 count = ARRAY_LENGTH(entries) - 1;
2345
2346 input_get_position(input, &x, &y);
2347 window_show_menu(window->display, input, time, window,
2348 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002349}
2350
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002351static int
2352frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002353 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002354{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002355 struct window_frame *frame = data;
2356 enum theme_location location;
2357
2358 location = frame_pointer_enter(frame->frame, input, x, y);
2359 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2360 widget_schedule_redraw(frame->widget);
2361
2362 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002363}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002364
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002365static int
2366frame_motion_handler(struct widget *widget,
2367 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002368 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002369{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002370 struct window_frame *frame = data;
2371 enum theme_location location;
2372
2373 location = frame_pointer_motion(frame->frame, input, x, y);
2374 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2375 widget_schedule_redraw(frame->widget);
2376
2377 return frame_get_pointer_image_for_location(data, location);
2378}
2379
2380static void
2381frame_leave_handler(struct widget *widget,
2382 struct input *input, void *data)
2383{
2384 struct window_frame *frame = data;
2385
2386 frame_pointer_leave(frame->frame, input);
2387 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2388 widget_schedule_redraw(frame->widget);
2389}
2390
2391static void
2392frame_handle_status(struct window_frame *frame, struct input *input,
2393 uint32_t time, enum theme_location location)
2394{
2395 struct window *window = frame->widget->window;
2396 uint32_t status;
2397
2398 status = frame_status(frame->frame);
2399 if (status & FRAME_STATUS_REPAINT)
2400 widget_schedule_redraw(frame->widget);
2401
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002402 if (status & FRAME_STATUS_MINIMIZE) {
2403 window_set_minimized(window);
2404 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2405 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002406
2407 if (status & FRAME_STATUS_MENU) {
2408 window_show_frame_menu(window, input, time);
2409 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2410 }
2411
2412 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002413 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002414 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2415 }
2416
2417 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002418 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002419 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002420 }
2421
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002422 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002423 input_ungrab(input);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002424 zxdg_toplevel_v6_move(window->xdg_toplevel,
2425 input_get_seat(input),
2426 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002427
2428 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2429 }
2430
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002431 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002432 input_ungrab(input);
2433
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002434 zxdg_toplevel_v6_resize(window->xdg_toplevel,
2435 input_get_seat(input),
2436 window->display->serial,
2437 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002438
2439 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2440 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002441}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002442
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002443#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002444static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002445frame_button_handler(struct widget *widget,
2446 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002447 uint32_t button, enum wl_pointer_button_state state,
2448 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002449
2450{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002451 struct window_frame *frame = data;
2452 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002453
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002454 frame->double_click = 0;
2455 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2456 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2457 frame->double_click = 1;
2458 frame->did_double = 1;
2459 } else
2460 frame->did_double = 0;
2461
2462 frame->last_time = time;
2463 } else if (frame->did_double == 1) {
2464 frame->double_click = 1;
2465 frame->did_double = 0;
2466 }
2467
2468 if (frame->double_click)
2469 location = frame_double_click(frame->frame, input,
2470 button, state);
2471 else
2472 location = frame_pointer_button(frame->frame, input,
2473 button, state);
2474
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002475 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002476}
2477
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002478static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002479frame_touch_down_handler(struct widget *widget, struct input *input,
2480 uint32_t serial, uint32_t time, int32_t id,
2481 float x, float y, void *data)
2482{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002483 struct window_frame *frame = data;
2484
Xiong Zhang382de462014-06-12 11:06:26 +08002485 frame->double_click = 0;
2486 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2487 frame->last_id == id) {
2488 frame->double_click = 1;
2489 frame->did_double = 1;
2490 frame->double_id = id;
2491 } else
2492 frame->did_double = 0;
2493
2494 frame->last_time = time;
2495 frame->last_id = id;
2496
2497 if (frame->double_click)
2498 frame_double_touch_down(frame->frame, input, id, x, y);
2499 else
2500 frame_touch_down(frame->frame, input, id, x, y);
2501
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002502 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2503}
2504
2505static void
2506frame_touch_up_handler(struct widget *widget,
2507 struct input *input, uint32_t serial, uint32_t time,
2508 int32_t id, void *data)
2509{
2510 struct window_frame *frame = data;
2511
Xiong Zhang382de462014-06-12 11:06:26 +08002512 if (frame->double_id == id && frame->did_double) {
2513 frame->did_double = 0;
2514 frame->double_id = 0;
2515 frame_double_touch_up(frame->frame, input, id);
2516 } else
2517 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002518 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002519}
2520
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002521struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002522window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002523{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002524 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002525 uint32_t buttons;
2526
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002527 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002528 buttons = FRAME_BUTTON_NONE;
2529 } else {
2530 buttons = FRAME_BUTTON_ALL;
2531 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002532
Peter Huttererf3d62272013-08-08 11:57:05 +10002533 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002534 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002535 buttons, window->title, NULL);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002536
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002537 frame->widget = window_add_widget(window, frame);
2538 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002539
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002540 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2541 widget_set_resize_handler(frame->widget, frame_resize_handler);
2542 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002543 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002544 widget_set_motion_handler(frame->widget, frame_motion_handler);
2545 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002546 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002547 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002548
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002549 window->frame = frame;
2550
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002551 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002552}
2553
Kristian Høgsberga1627922012-06-20 17:30:03 -04002554void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002555window_frame_set_child_size(struct widget *widget, int child_width,
2556 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002557{
2558 struct display *display = widget->window->display;
2559 struct theme *t = display->theme;
2560 int decoration_width, decoration_height;
2561 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002562 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002563
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002564 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002565 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002566 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002567 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002568
2569 width = child_width + decoration_width;
2570 height = child_height + decoration_height;
2571 } else {
2572 width = child_width;
2573 height = child_height;
2574 }
2575
2576 window_schedule_resize(widget->window, width, height);
2577}
2578
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002579static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002580window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002581{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002582 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002583
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002584 /* frame->child must be destroyed by the application */
2585 widget_destroy(frame->widget);
2586 free(frame);
2587}
2588
2589static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002590input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002591 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002592{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002593 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002594 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002595
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002596 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002597 return;
2598
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002599 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002600 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002601 widget = old;
2602 if (input->grab)
2603 widget = input->grab;
2604 if (widget->leave_handler)
2605 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002606 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002607 }
2608
2609 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002610 widget = focus;
2611 if (input->grab)
2612 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002613 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002614 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002615 cursor = widget->enter_handler(focus, input, x, y,
2616 widget->user_data);
2617 else
2618 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002619
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002620 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002621 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002622}
2623
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002624void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002625touch_grab(struct input *input, int32_t touch_id)
2626{
2627 input->touch_grab = 1;
2628 input->touch_grab_id = touch_id;
2629}
2630
2631void
2632touch_ungrab(struct input *input)
2633{
2634 struct touch_point *tp, *tmp;
2635
2636 input->touch_grab = 0;
2637
2638 wl_list_for_each_safe(tp, tmp,
2639 &input->touch_point_list, link) {
2640 if (tp->id != input->touch_grab_id)
2641 continue;
2642 wl_list_remove(&tp->link);
2643 free(tp);
2644
2645 return;
2646 }
2647}
2648
2649void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002650input_grab(struct input *input, struct widget *widget, uint32_t button)
2651{
2652 input->grab = widget;
2653 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002654
2655 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002656}
2657
2658void
2659input_ungrab(struct input *input)
2660{
2661 struct widget *widget;
2662
2663 input->grab = NULL;
2664 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002665 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002666 input->sx, input->sy);
2667 input_set_focus_widget(input, widget, input->sx, input->sy);
2668 }
2669}
2670
2671static void
Derek Foreman118a4292015-04-22 17:23:35 -05002672cursor_delay_timer_reset(struct input *input, uint32_t duration)
2673{
Derek Foreman118a4292015-04-22 17:23:35 -05002674 if (!duration)
2675 input->cursor_timer_running = false;
2676 else
2677 input->cursor_timer_running = true;
2678
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002679 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002680}
2681
2682static void cancel_pointer_image_update(struct input *input)
2683{
2684 if (input->cursor_timer_running)
2685 cursor_delay_timer_reset(input, 0);
2686}
2687
2688static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002689input_remove_pointer_focus(struct input *input)
2690{
2691 struct window *window = input->pointer_focus;
2692
2693 if (!window)
2694 return;
2695
2696 input_set_focus_widget(input, NULL, 0, 0);
2697
2698 input->pointer_focus = NULL;
2699 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002700 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002701}
2702
2703static void
2704pointer_handle_enter(void *data, struct wl_pointer *pointer,
2705 uint32_t serial, struct wl_surface *surface,
2706 wl_fixed_t sx_w, wl_fixed_t sy_w)
2707{
2708 struct input *input = data;
2709 struct window *window;
2710 struct widget *widget;
2711 float sx = wl_fixed_to_double(sx_w);
2712 float sy = wl_fixed_to_double(sy_w);
2713
2714 if (!surface) {
2715 /* enter event for a window we've just destroyed */
2716 return;
2717 }
2718
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002719 window = wl_surface_get_user_data(surface);
2720 if (surface != window->main_surface->surface) {
2721 DBG("Ignoring input event from subsurface %p\n", surface);
2722 return;
2723 }
2724
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002725 input->display->serial = serial;
2726 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002727 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002728
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002729 input->sx = sx;
2730 input->sy = sy;
2731
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002732 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002733 input_set_focus_widget(input, widget, sx, sy);
2734}
2735
2736static void
2737pointer_handle_leave(void *data, struct wl_pointer *pointer,
2738 uint32_t serial, struct wl_surface *surface)
2739{
2740 struct input *input = data;
2741
2742 input->display->serial = serial;
2743 input_remove_pointer_focus(input);
2744}
2745
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002746static void
Daniel Stone37816df2012-05-16 18:45:18 +01002747pointer_handle_motion(void *data, struct wl_pointer *pointer,
2748 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002749{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002750 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002751 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002752 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002753 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002754 float sx = wl_fixed_to_double(sx_w);
2755 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002756
Paul Winwoodb22bf572013-08-29 10:52:54 +01002757 if (!window)
2758 return;
2759
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002760 input->sx = sx;
2761 input->sy = sy;
2762
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002763 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002764 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002765 * based on the old surface dimensions. However, if we have an active
2766 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002767 */
Derek Foreman46812b62015-08-26 17:13:27 -05002768 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002769 sy < window->main_surface->allocation.y ||
2770 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002771 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002772 return;
2773
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002774 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002775 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002776 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002777 }
2778
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002779 if (input->grab)
2780 widget = input->grab;
2781 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002782 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002783 if (widget) {
2784 if (widget->motion_handler)
2785 cursor = widget->motion_handler(input->focus_widget,
2786 input, time, sx, sy,
2787 widget->user_data);
2788 else
2789 cursor = widget->default_cursor;
2790 } else
2791 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002792
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002793 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002794}
2795
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002796static void
Daniel Stone37816df2012-05-16 18:45:18 +01002797pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002798 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002799{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002800 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002801 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002802 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002803
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002804 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002805 if (input->focus_widget && input->grab == NULL &&
2806 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002807 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002808
Neil Roberts6b28aad2012-01-23 19:11:18 +00002809 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002810 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002811 (*widget->button_handler)(widget,
2812 input, time,
2813 button, state,
2814 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002815
Daniel Stone4dbadb12012-05-30 16:31:51 +01002816 if (input->grab && input->grab_button == button &&
2817 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002818 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002819}
2820
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002821static void
Daniel Stone37816df2012-05-16 18:45:18 +01002822pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002823 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002824{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002825 struct input *input = data;
2826 struct widget *widget;
2827
2828 widget = input->focus_widget;
2829 if (input->grab)
2830 widget = input->grab;
2831 if (widget && widget->axis_handler)
2832 (*widget->axis_handler)(widget,
2833 input, time,
2834 axis, value,
2835 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002836}
2837
Peter Hutterer87743e92016-01-18 16:38:22 +10002838static void
2839pointer_handle_frame(void *data, struct wl_pointer *pointer)
2840{
2841 struct input *input = data;
2842 struct widget *widget;
2843
2844 widget = input->focus_widget;
2845 if (input->grab)
2846 widget = input->grab;
2847 if (widget && widget->pointer_frame_handler)
2848 (*widget->pointer_frame_handler)(widget,
2849 input,
2850 widget->user_data);
2851}
2852
2853static void
2854pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2855 uint32_t source)
2856{
2857 struct input *input = data;
2858 struct widget *widget;
2859
2860 widget = input->focus_widget;
2861 if (input->grab)
2862 widget = input->grab;
2863 if (widget && widget->axis_source_handler)
2864 (*widget->axis_source_handler)(widget,
2865 input,
2866 source,
2867 widget->user_data);
2868}
2869
2870static void
2871pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2872 uint32_t time, uint32_t axis)
2873{
2874 struct input *input = data;
2875 struct widget *widget;
2876
2877 widget = input->focus_widget;
2878 if (input->grab)
2879 widget = input->grab;
2880 if (widget && widget->axis_stop_handler)
2881 (*widget->axis_stop_handler)(widget,
2882 input, time,
2883 axis,
2884 widget->user_data);
2885}
2886
2887static void
2888pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2889 uint32_t axis, int32_t discrete)
2890{
2891 struct input *input = data;
2892 struct widget *widget;
2893
2894 widget = input->focus_widget;
2895 if (input->grab)
2896 widget = input->grab;
2897 if (widget && widget->axis_discrete_handler)
2898 (*widget->axis_discrete_handler)(widget,
2899 input,
2900 axis,
2901 discrete,
2902 widget->user_data);
2903}
2904
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002905static const struct wl_pointer_listener pointer_listener = {
2906 pointer_handle_enter,
2907 pointer_handle_leave,
2908 pointer_handle_motion,
2909 pointer_handle_button,
2910 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002911 pointer_handle_frame,
2912 pointer_handle_axis_source,
2913 pointer_handle_axis_stop,
2914 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002915};
2916
2917static void
2918input_remove_keyboard_focus(struct input *input)
2919{
2920 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002921
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002922 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002923
2924 if (!window)
2925 return;
2926
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002927 if (window->keyboard_focus_handler)
2928 (*window->keyboard_focus_handler)(window, NULL,
2929 window->user_data);
2930
2931 input->keyboard_focus = NULL;
2932}
2933
2934static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002935keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002936{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002937 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002938 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002939
2940 if (window && window->key_handler) {
2941 (*window->key_handler)(window, input, input->repeat_time,
2942 input->repeat_key, input->repeat_sym,
2943 WL_KEYBOARD_KEY_STATE_PRESSED,
2944 window->user_data);
2945 }
2946}
2947
2948static void
2949keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2950 uint32_t format, int fd, uint32_t size)
2951{
2952 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002953 struct xkb_keymap *keymap;
2954 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002955#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002956 struct xkb_compose_table *compose_table;
2957 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002958#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002959 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002960 char *map_str;
2961
2962 if (!data) {
2963 close(fd);
2964 return;
2965 }
2966
2967 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2968 close(fd);
2969 return;
2970 }
2971
2972 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2973 if (map_str == MAP_FAILED) {
2974 close(fd);
2975 return;
2976 }
2977
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002978 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03002979 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2980 map_str,
2981 XKB_KEYMAP_FORMAT_TEXT_V1,
2982 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002983 munmap(map_str, size);
2984 close(fd);
2985
Rui Matos3eccb862013-10-10 19:44:22 +02002986 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002987 fprintf(stderr, "failed to compile keymap\n");
2988 return;
2989 }
2990
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002991 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02002992 state = xkb_state_new(keymap);
2993 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002994 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002995 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002996 return;
2997 }
2998
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002999 /* Look up the preferred locale, falling back to "C" as default */
3000 if (!(locale = getenv("LC_ALL")))
3001 if (!(locale = getenv("LC_CTYPE")))
3002 if (!(locale = getenv("LANG")))
3003 locale = "C";
3004
3005 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003006#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003007 compose_table =
3008 xkb_compose_table_new_from_locale(input->display->xkb_context,
3009 locale,
3010 XKB_COMPOSE_COMPILE_NO_FLAGS);
3011 if (compose_table) {
3012 /* Set up XKB compose state */
3013 compose_state = xkb_compose_state_new(compose_table,
3014 XKB_COMPOSE_STATE_NO_FLAGS);
3015 if (compose_state) {
3016 xkb_compose_state_unref(input->xkb.compose_state);
3017 xkb_compose_table_unref(input->xkb.compose_table);
3018 input->xkb.compose_state = compose_state;
3019 input->xkb.compose_table = compose_table;
3020 } else {
3021 fprintf(stderr, "could not create XKB compose state. "
3022 "Disabiling compose.\n");
3023 xkb_compose_table_unref(compose_table);
3024 compose_table = NULL;
3025 }
3026 } else {
3027 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3028 "Disabiling compose\n", locale);
3029 }
Daniel Stone5b015962016-10-20 14:45:58 +01003030#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003031
Rui Matos3eccb862013-10-10 19:44:22 +02003032 xkb_keymap_unref(input->xkb.keymap);
3033 xkb_state_unref(input->xkb.state);
3034 input->xkb.keymap = keymap;
3035 input->xkb.state = state;
3036
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003037 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003038 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003039 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003040 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003041 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003042 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003043}
3044
3045static void
3046keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3047 uint32_t serial, struct wl_surface *surface,
3048 struct wl_array *keys)
3049{
3050 struct input *input = data;
3051 struct window *window;
3052
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003053 if (!surface) {
3054 /* enter event for a window we've just destroyed */
3055 return;
3056 }
3057
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003058 input->display->serial = serial;
3059 input->keyboard_focus = wl_surface_get_user_data(surface);
3060
3061 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003062 if (window->keyboard_focus_handler)
3063 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003064 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003065}
3066
3067static void
3068keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3069 uint32_t serial, struct wl_surface *surface)
3070{
3071 struct input *input = data;
3072
3073 input->display->serial = serial;
3074 input_remove_keyboard_focus(input);
3075}
3076
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003077/* Translate symbols appropriately if a compose sequence is being entered */
3078static xkb_keysym_t
3079process_key_press(xkb_keysym_t sym, struct input *input)
3080{
Daniel Stone5b015962016-10-20 14:45:58 +01003081#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003082 if (!input->xkb.compose_state)
3083 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003084 if (sym == XKB_KEY_NoSymbol)
3085 return sym;
3086 if (xkb_compose_state_feed(input->xkb.compose_state,
3087 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3088 return sym;
3089
3090 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3091 case XKB_COMPOSE_COMPOSING:
3092 return XKB_KEY_NoSymbol;
3093 case XKB_COMPOSE_COMPOSED:
3094 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3095 case XKB_COMPOSE_CANCELLED:
3096 return XKB_KEY_NoSymbol;
3097 case XKB_COMPOSE_NOTHING:
3098 return sym;
3099 default:
3100 return sym;
3101 }
Daniel Stone5b015962016-10-20 14:45:58 +01003102#else
3103 return sym;
3104#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003105}
3106
Scott Moreau210d0792012-03-22 10:47:01 -06003107static void
Daniel Stone37816df2012-05-16 18:45:18 +01003108keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003109 uint32_t serial, uint32_t time, uint32_t key,
3110 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003111{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003112 struct input *input = data;
3113 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003114 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003115 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003116 const xkb_keysym_t *syms;
3117 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003118 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003119
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003120 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003121 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003122 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003123 return;
3124
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003125 /* We only use input grabs for pointer events for now, so just
3126 * ignore key presses if a grab is active. We expand the key
3127 * event delivery mechanism to route events to widgets to
3128 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003129 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003130 if (input->grab && input->grab_button == 0)
3131 return;
3132
Ran Benita2e1968f2014-08-19 23:59:51 +03003133 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003134
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003135 sym = XKB_KEY_NoSymbol;
3136 if (num_syms == 1)
3137 sym = syms[0];
3138
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003139
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003140 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003141 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003142 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003143 } else if (sym == XKB_KEY_F11 &&
3144 window->fullscreen_handler &&
3145 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3146 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003147 } else if (sym == XKB_KEY_F4 &&
3148 input->modifiers == MOD_ALT_MASK &&
3149 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003150 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003151 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003152 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3153 sym = process_key_press(sym, input);
3154
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003155 (*window->key_handler)(window, input, time, key,
3156 sym, state, window->user_data);
3157 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003158
3159 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3160 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003161 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003162 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3163 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003164 input->repeat_sym = sym;
3165 input->repeat_key = key;
3166 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003167 its.it_interval.tv_sec = input->repeat_rate_sec;
3168 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3169 its.it_value.tv_sec = input->repeat_delay_sec;
3170 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003171 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003172 }
3173}
3174
3175static void
Daniel Stone351eb612012-05-31 15:27:47 -04003176keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3177 uint32_t serial, uint32_t mods_depressed,
3178 uint32_t mods_latched, uint32_t mods_locked,
3179 uint32_t group)
3180{
3181 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003182 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003183
Matt Ropere61561f2013-06-24 16:52:43 +01003184 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3185 if (!input->xkb.keymap)
3186 return;
3187
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003188 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3189 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003190 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003191 XKB_STATE_MODS_DEPRESSED |
3192 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003193 input->modifiers = 0;
3194 if (mask & input->xkb.control_mask)
3195 input->modifiers |= MOD_CONTROL_MASK;
3196 if (mask & input->xkb.alt_mask)
3197 input->modifiers |= MOD_ALT_MASK;
3198 if (mask & input->xkb.shift_mask)
3199 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003200}
3201
Jonny Lamb06959082014-08-12 14:58:27 +02003202static void
3203set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3204{
3205 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3206 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3207
3208 /* a rate of zero disables any repeating, regardless of the delay's
3209 * value */
3210 if (rate == 0)
3211 return;
3212
3213 if (rate == 1)
3214 input->repeat_rate_sec = 1;
3215 else
3216 input->repeat_rate_nsec = 1000000000 / rate;
3217
3218 input->repeat_delay_sec = delay / 1000;
3219 delay -= (input->repeat_delay_sec * 1000);
3220 input->repeat_delay_nsec = delay * 1000 * 1000;
3221}
3222
3223static void
3224keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3225 int32_t rate, int32_t delay)
3226{
3227 struct input *input = data;
3228
3229 set_repeat_info(input, rate, delay);
3230}
3231
Daniel Stone37816df2012-05-16 18:45:18 +01003232static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003233 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003234 keyboard_handle_enter,
3235 keyboard_handle_leave,
3236 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003237 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003238 keyboard_handle_repeat_info
3239
Daniel Stone37816df2012-05-16 18:45:18 +01003240};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003241
3242static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003243touch_handle_down(void *data, struct wl_touch *wl_touch,
3244 uint32_t serial, uint32_t time, struct wl_surface *surface,
3245 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3246{
3247 struct input *input = data;
3248 struct widget *widget;
3249 float sx = wl_fixed_to_double(x_w);
3250 float sy = wl_fixed_to_double(y_w);
3251
Rusty Lynch1084da52013-08-15 09:10:08 -07003252 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003253 input->touch_focus = wl_surface_get_user_data(surface);
3254 if (!input->touch_focus) {
3255 DBG("Failed to find to touch focus for surface %p\n", surface);
3256 return;
3257 }
3258
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003259 if (surface != input->touch_focus->main_surface->surface) {
3260 DBG("Ignoring input event from subsurface %p\n", surface);
3261 input->touch_focus = NULL;
3262 return;
3263 }
3264
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003265 if (input->grab)
3266 widget = input->grab;
3267 else
3268 widget = window_find_widget(input->touch_focus,
3269 wl_fixed_to_double(x_w),
3270 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003271 if (widget) {
3272 struct touch_point *tp = xmalloc(sizeof *tp);
3273 if (tp) {
3274 tp->id = id;
3275 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003276 tp->x = sx;
3277 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003278 wl_list_insert(&input->touch_point_list, &tp->link);
3279
3280 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003281 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003282 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003283 sx, sy,
3284 widget->user_data);
3285 }
3286 }
3287}
3288
3289static void
3290touch_handle_up(void *data, struct wl_touch *wl_touch,
3291 uint32_t serial, uint32_t time, int32_t id)
3292{
3293 struct input *input = data;
3294 struct touch_point *tp, *tmp;
3295
Rusty Lynch041815a2013-08-08 21:20:38 -07003296 if (!input->touch_focus) {
3297 DBG("No touch focus found for touch up event!\n");
3298 return;
3299 }
3300
3301 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3302 if (tp->id != id)
3303 continue;
3304
3305 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003306 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003307 time, id,
3308 tp->widget->user_data);
3309
3310 wl_list_remove(&tp->link);
3311 free(tp);
3312
3313 return;
3314 }
3315}
3316
3317static void
3318touch_handle_motion(void *data, struct wl_touch *wl_touch,
3319 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3320{
3321 struct input *input = data;
3322 struct touch_point *tp;
3323 float sx = wl_fixed_to_double(x_w);
3324 float sy = wl_fixed_to_double(y_w);
3325
3326 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3327
3328 if (!input->touch_focus) {
3329 DBG("No touch focus found for touch motion event!\n");
3330 return;
3331 }
3332
3333 wl_list_for_each(tp, &input->touch_point_list, link) {
3334 if (tp->id != id)
3335 continue;
3336
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003337 tp->x = sx;
3338 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003339 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003340 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003341 id, sx, sy,
3342 tp->widget->user_data);
3343 return;
3344 }
3345}
3346
3347static void
3348touch_handle_frame(void *data, struct wl_touch *wl_touch)
3349{
3350 struct input *input = data;
3351 struct touch_point *tp, *tmp;
3352
3353 DBG("touch_handle_frame\n");
3354
3355 if (!input->touch_focus) {
3356 DBG("No touch focus found for touch frame event!\n");
3357 return;
3358 }
3359
3360 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3361 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003362 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003363 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003364 }
3365}
3366
3367static void
3368touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3369{
3370 struct input *input = data;
3371 struct touch_point *tp, *tmp;
3372
3373 DBG("touch_handle_cancel\n");
3374
3375 if (!input->touch_focus) {
3376 DBG("No touch focus found for touch cancel event!\n");
3377 return;
3378 }
3379
3380 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3381 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003382 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3383 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003384
3385 wl_list_remove(&tp->link);
3386 free(tp);
3387 }
3388}
3389
3390static const struct wl_touch_listener touch_listener = {
3391 touch_handle_down,
3392 touch_handle_up,
3393 touch_handle_motion,
3394 touch_handle_frame,
3395 touch_handle_cancel,
3396};
3397
3398static void
Daniel Stone37816df2012-05-16 18:45:18 +01003399seat_handle_capabilities(void *data, struct wl_seat *seat,
3400 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003401{
Daniel Stone37816df2012-05-16 18:45:18 +01003402 struct input *input = data;
3403
3404 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3405 input->pointer = wl_seat_get_pointer(seat);
3406 wl_pointer_set_user_data(input->pointer, input);
3407 wl_pointer_add_listener(input->pointer, &pointer_listener,
3408 input);
3409 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003410 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003411 wl_pointer_release(input->pointer);
3412 else
3413 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003414 input->pointer = NULL;
3415 }
3416
3417 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3418 input->keyboard = wl_seat_get_keyboard(seat);
3419 wl_keyboard_set_user_data(input->keyboard, input);
3420 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3421 input);
3422 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003423 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003424 wl_keyboard_release(input->keyboard);
3425 else
3426 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003427 input->keyboard = NULL;
3428 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003429
3430 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3431 input->touch = wl_seat_get_touch(seat);
3432 wl_touch_set_user_data(input->touch, input);
3433 wl_touch_add_listener(input->touch, &touch_listener, input);
3434 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003435 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003436 wl_touch_release(input->touch);
3437 else
3438 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003439 input->touch = NULL;
3440 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003441}
3442
Rob Bradford08031182013-08-13 20:11:03 +01003443static void
3444seat_handle_name(void *data, struct wl_seat *seat,
3445 const char *name)
3446{
3447
3448}
3449
Daniel Stone37816df2012-05-16 18:45:18 +01003450static const struct wl_seat_listener seat_listener = {
3451 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003452 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003453};
3454
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003455void
3456input_get_position(struct input *input, int32_t *x, int32_t *y)
3457{
3458 *x = input->sx;
3459 *y = input->sy;
3460}
3461
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003462int
3463input_get_touch(struct input *input, int32_t id, float *x, float *y)
3464{
3465 struct touch_point *tp;
3466
3467 wl_list_for_each(tp, &input->touch_point_list, link) {
3468 if (tp->id != id)
3469 continue;
3470
3471 *x = tp->x;
3472 *y = tp->y;
3473 return 0;
3474 }
3475
3476 return -1;
3477}
3478
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003479struct display *
3480input_get_display(struct input *input)
3481{
3482 return input->display;
3483}
3484
Daniel Stone37816df2012-05-16 18:45:18 +01003485struct wl_seat *
3486input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003487{
Daniel Stone37816df2012-05-16 18:45:18 +01003488 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003489}
3490
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003491uint32_t
3492input_get_modifiers(struct input *input)
3493{
3494 return input->modifiers;
3495}
3496
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003497struct widget *
3498input_get_focus_widget(struct input *input)
3499{
3500 return input->focus_widget;
3501}
3502
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003503struct data_offer {
3504 struct wl_data_offer *offer;
3505 struct input *input;
3506 struct wl_array types;
3507 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003508
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003509 struct task io_task;
3510 int fd;
3511 data_func_t func;
3512 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003513 uint32_t dnd_action;
3514 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003515 void *user_data;
3516};
3517
3518static void
3519data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3520{
3521 struct data_offer *offer = data;
3522 char **p;
3523
3524 p = wl_array_add(&offer->types, sizeof *p);
3525 *p = strdup(type);
3526}
3527
Carlos Garnacho9c931792016-01-18 23:52:12 +01003528static void
3529data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3530{
3531 struct data_offer *offer = data;
3532
3533 offer->source_actions = source_actions;
3534}
3535
3536static void
3537data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3538{
3539 struct data_offer *offer = data;
3540
3541 offer->dnd_action = dnd_action;
3542}
3543
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003544static const struct wl_data_offer_listener data_offer_listener = {
3545 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003546 data_offer_source_actions,
3547 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003548};
3549
3550static void
3551data_offer_destroy(struct data_offer *offer)
3552{
3553 char **p;
3554
3555 offer->refcount--;
3556 if (offer->refcount == 0) {
3557 wl_data_offer_destroy(offer->offer);
3558 for (p = offer->types.data; *p; p++)
3559 free(*p);
3560 wl_array_release(&offer->types);
3561 free(offer);
3562 }
3563}
3564
3565static void
3566data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003567 struct wl_data_device *data_device,
3568 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003569{
3570 struct data_offer *offer;
3571
Brian Lovinbc919262013-08-07 15:34:59 -07003572 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003573
3574 wl_array_init(&offer->types);
3575 offer->refcount = 1;
3576 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003577 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003578 wl_data_offer_add_listener(offer->offer,
3579 &data_offer_listener, offer);
3580}
3581
3582static void
3583data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003584 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003585 wl_fixed_t x_w, wl_fixed_t y_w,
3586 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003587{
3588 struct input *input = data;
3589 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003590 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003591 float x = wl_fixed_to_double(x_w);
3592 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003593 char **p;
3594
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003595 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003596 input->drag_enter_serial = serial;
3597 input->drag_focus = window,
3598 input->drag_x = x;
3599 input->drag_y = y;
3600
3601 if (!input->touch_grab)
3602 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003603
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003604 if (offer) {
3605 input->drag_offer = wl_data_offer_get_user_data(offer);
3606
3607 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3608 *p = NULL;
3609
3610 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003611
3612 if (input->display->data_device_manager_version >=
3613 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3614 wl_data_offer_set_actions(offer,
3615 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3616 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3617 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3618 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003619 } else {
3620 input->drag_offer = NULL;
3621 types_data = NULL;
3622 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003623
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003624 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003625 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003626 window->user_data);
3627}
3628
3629static void
3630data_device_leave(void *data, struct wl_data_device *data_device)
3631{
3632 struct input *input = data;
3633
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003634 if (input->drag_offer) {
3635 data_offer_destroy(input->drag_offer);
3636 input->drag_offer = NULL;
3637 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003638}
3639
3640static void
3641data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003642 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003643{
3644 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003645 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003646 float x = wl_fixed_to_double(x_w);
3647 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003648 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003649
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003650 input->drag_x = x;
3651 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003652
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003653 if (input->drag_offer)
3654 types_data = input->drag_offer->types.data;
3655 else
3656 types_data = NULL;
3657
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003658 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003659 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003660 window->user_data);
3661}
3662
3663static void
3664data_device_drop(void *data, struct wl_data_device *data_device)
3665{
3666 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003667 struct window *window = input->drag_focus;
3668 float x, y;
3669
3670 x = input->drag_x;
3671 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003672
3673 if (window->drop_handler)
3674 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003675 x, y, window->user_data);
3676
3677 if (input->touch_grab)
3678 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003679}
3680
3681static void
3682data_device_selection(void *data,
3683 struct wl_data_device *wl_data_device,
3684 struct wl_data_offer *offer)
3685{
3686 struct input *input = data;
3687 char **p;
3688
3689 if (input->selection_offer)
3690 data_offer_destroy(input->selection_offer);
3691
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003692 if (offer) {
3693 input->selection_offer = wl_data_offer_get_user_data(offer);
3694 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3695 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003696 } else {
3697 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003698 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003699}
3700
3701static const struct wl_data_device_listener data_device_listener = {
3702 data_device_data_offer,
3703 data_device_enter,
3704 data_device_leave,
3705 data_device_motion,
3706 data_device_drop,
3707 data_device_selection
3708};
3709
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003710static void
3711input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003712{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003713 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003714 struct wl_cursor *cursor;
3715 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003716
Daniel Stone80972742012-11-07 17:51:39 +11003717 if (!input->pointer)
3718 return;
3719
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003720 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003721 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003722 return;
3723
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003724 if (index >= (int) cursor->image_count) {
3725 fprintf(stderr, "cursor index out of range\n");
3726 return;
3727 }
3728
3729 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003730 buffer = wl_cursor_image_get_buffer(image);
3731 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003732 return;
3733
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003734 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3735 wl_surface_damage(input->pointer_surface, 0, 0,
3736 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003737 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003738 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3739 input->pointer_surface,
3740 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003741}
3742
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003743static const struct wl_callback_listener pointer_surface_listener;
3744
Derek Foreman493d9792015-03-04 16:26:25 -06003745static bool
3746input_set_pointer_special(struct input *input)
3747{
3748 if (input->current_cursor == CURSOR_BLANK) {
3749 wl_pointer_set_cursor(input->pointer,
3750 input->pointer_enter_serial,
3751 NULL, 0, 0);
3752 return true;
3753 }
3754
3755 if (input->current_cursor == CURSOR_UNSET)
3756 return true;
3757
3758 return false;
3759}
3760
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003761static void
Derek Foreman118a4292015-04-22 17:23:35 -05003762schedule_pointer_image_update(struct input *input,
3763 struct wl_cursor *cursor,
3764 uint32_t duration,
3765 bool force_frame)
3766{
3767 /* Some silly cursor sets have enormous pauses in them. In these
3768 * cases it's better to use a timer even if it results in less
3769 * accurate presentation, since it will save us having to set the
3770 * same cursor image over and over again.
3771 *
3772 * This is really not the way we're supposed to time any kind of
3773 * animation, but we're pretending it's OK here because we don't
3774 * want animated cursors with long delays to needlessly hog CPU.
3775 *
3776 * We use force_frame to ensure we don't accumulate large timing
3777 * errors by running off the wrong clock.
3778 */
3779 if (!force_frame && duration > 100) {
3780 struct timespec tp;
3781
3782 clock_gettime(CLOCK_MONOTONIC, &tp);
3783 input->cursor_timer_start = tp.tv_sec * 1000
3784 + tp.tv_nsec / 1000000;
3785 cursor_delay_timer_reset(input, duration);
3786 return;
3787 }
3788
3789 /* for short durations we'll just spin on frame callbacks for
3790 * accurate timing - the way any kind of timing sensitive animation
3791 * should really be done. */
3792 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3793 wl_callback_add_listener(input->cursor_frame_cb,
3794 &pointer_surface_listener, input);
3795
3796}
3797
3798static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003799pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3800 uint32_t time)
3801{
3802 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003803 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003804 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003805 uint32_t duration;
3806 bool force_frame = true;
3807
3808 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003809
3810 if (callback) {
3811 assert(callback == input->cursor_frame_cb);
3812 wl_callback_destroy(callback);
3813 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003814 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003815 }
3816
Daniel Stone80972742012-11-07 17:51:39 +11003817 if (!input->pointer)
3818 return;
3819
Derek Foreman493d9792015-03-04 16:26:25 -06003820 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003821 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003822
Daniel Stonea494f1d2012-06-18 19:31:12 +01003823 cursor = input->display->cursors[input->current_cursor];
3824 if (!cursor)
3825 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003826
3827 /* FIXME We don't have the current time on the first call so we set
3828 * the animation start to the time of the first frame callback. */
3829 if (time == 0)
3830 input->cursor_anim_start = 0;
3831 else if (input->cursor_anim_start == 0)
3832 input->cursor_anim_start = time;
3833
Derek Foreman118a4292015-04-22 17:23:35 -05003834 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003835
Derek Foreman118a4292015-04-22 17:23:35 -05003836 if (time == 0 || input->cursor_anim_start == 0) {
3837 duration = 0;
3838 i = 0;
3839 } else
3840 i = wl_cursor_frame_and_duration(
3841 cursor,
3842 time - input->cursor_anim_start,
3843 &duration);
3844
3845 if (cursor->image_count > 1)
3846 schedule_pointer_image_update(input, cursor, duration,
3847 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003848
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003849 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003850}
3851
Derek Foreman118a4292015-04-22 17:23:35 -05003852static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003853cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003854{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003855 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003856 struct timespec tp;
3857 struct wl_cursor *cursor;
3858 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003859
3860 if (!input->cursor_timer_running)
3861 return;
3862
Derek Foreman118a4292015-04-22 17:23:35 -05003863 cursor = input->display->cursors[input->current_cursor];
3864 if (!cursor)
3865 return;
3866
3867 clock_gettime(CLOCK_MONOTONIC, &tp);
3868 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3869 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3870}
3871
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003872static const struct wl_callback_listener pointer_surface_listener = {
3873 pointer_surface_frame_callback
3874};
3875
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003876void
3877input_set_pointer_image(struct input *input, int pointer)
3878{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003879 int force = 0;
3880
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003881 if (!input->pointer)
3882 return;
3883
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003884 if (input->pointer_enter_serial > input->cursor_serial)
3885 force = 1;
3886
3887 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003888 return;
3889
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003890 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003891 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003892 if (!input->cursor_frame_cb)
3893 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003894 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003895 /* The current frame callback may be stuck if, for instance,
3896 * the set cursor request was processed by the server after
3897 * this client lost the focus. In this case the cursor surface
3898 * might not be mapped and the frame callback wouldn't ever
3899 * complete. Send a set_cursor and attach to try to map the
3900 * cursor surface again so that the callback will finish */
3901 input_set_pointer_image_index(input, 0);
3902 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003903}
3904
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003905struct wl_data_device *
3906input_get_data_device(struct input *input)
3907{
3908 return input->data_device;
3909}
3910
3911void
3912input_set_selection(struct input *input,
3913 struct wl_data_source *source, uint32_t time)
3914{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003915 if (input->data_device)
3916 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003917}
3918
3919void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003920input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003921{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003922 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003923 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003924}
3925
3926static void
3927offer_io_func(struct task *task, uint32_t events)
3928{
3929 struct data_offer *offer =
3930 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003931 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003932 unsigned int len;
3933 char buffer[4096];
3934
3935 len = read(offer->fd, buffer, sizeof buffer);
3936 offer->func(buffer, len,
3937 offer->x, offer->y, offer->user_data);
3938
3939 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003940 if (display->data_device_manager_version >=
3941 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3942 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003943 close(offer->fd);
3944 data_offer_destroy(offer);
3945 }
3946}
3947
3948static void
3949data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3950 data_func_t func, void *user_data)
3951{
3952 int p[2];
3953
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003954 if (pipe2(p, O_CLOEXEC) == -1)
3955 return;
3956
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003957 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3958 close(p[1]);
3959
3960 offer->io_task.run = offer_io_func;
3961 offer->fd = p[0];
3962 offer->func = func;
3963 offer->refcount++;
3964 offer->user_data = user_data;
3965
3966 display_watch_fd(offer->input->display,
3967 offer->fd, EPOLLIN, &offer->io_task);
3968}
3969
3970void
3971input_receive_drag_data(struct input *input, const char *mime_type,
3972 data_func_t func, void *data)
3973{
3974 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003975 input->drag_offer->x = input->drag_x;
3976 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003977}
3978
3979int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003980input_receive_drag_data_to_fd(struct input *input,
3981 const char *mime_type, int fd)
3982{
3983 if (input->drag_offer)
3984 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3985
3986 return 0;
3987}
3988
3989int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003990input_receive_selection_data(struct input *input, const char *mime_type,
3991 data_func_t func, void *data)
3992{
3993 char **p;
3994
3995 if (input->selection_offer == NULL)
3996 return -1;
3997
3998 for (p = input->selection_offer->types.data; *p; p++)
3999 if (strcmp(mime_type, *p) == 0)
4000 break;
4001
4002 if (*p == NULL)
4003 return -1;
4004
4005 data_offer_receive_data(input->selection_offer,
4006 mime_type, func, data);
4007 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004008}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004009
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004010int
4011input_receive_selection_data_to_fd(struct input *input,
4012 const char *mime_type, int fd)
4013{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004014 if (input->selection_offer)
4015 wl_data_offer_receive(input->selection_offer->offer,
4016 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004017
4018 return 0;
4019}
4020
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004021void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004022window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004023{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004024 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004025 return;
4026
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004027 zxdg_toplevel_v6_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004028}
4029
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004030static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004031surface_set_synchronized(struct surface *surface)
4032{
4033 if (!surface->subsurface)
4034 return;
4035
4036 if (surface->synchronized)
4037 return;
4038
4039 wl_subsurface_set_sync(surface->subsurface);
4040 surface->synchronized = 1;
4041}
4042
4043static void
4044surface_set_synchronized_default(struct surface *surface)
4045{
4046 if (!surface->subsurface)
4047 return;
4048
4049 if (surface->synchronized == surface->synchronized_default)
4050 return;
4051
4052 if (surface->synchronized_default)
4053 wl_subsurface_set_sync(surface->subsurface);
4054 else
4055 wl_subsurface_set_desync(surface->subsurface);
4056
4057 surface->synchronized = surface->synchronized_default;
4058}
4059
4060static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004061surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004062{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004063 struct widget *widget = surface->widget;
4064 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004065
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004066 if (surface->input_region) {
4067 wl_region_destroy(surface->input_region);
4068 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004069 }
4070
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004071 if (surface->opaque_region)
4072 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004073
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004074 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004075
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004076 if (widget->resize_handler)
4077 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004078 widget->allocation.width,
4079 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004080 widget->user_data);
4081
Pekka Paalanen35e82632013-04-25 13:57:48 +03004082 if (surface->subsurface &&
4083 (surface->allocation.x != widget->allocation.x ||
4084 surface->allocation.y != widget->allocation.y)) {
4085 wl_subsurface_set_position(surface->subsurface,
4086 widget->allocation.x,
4087 widget->allocation.y);
4088 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004089 if (surface->allocation.width != widget->allocation.width ||
4090 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004091 window_schedule_redraw(widget->window);
4092 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004093 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004094
4095 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004096 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004097 widget->allocation.width,
4098 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004099}
4100
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004101static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004102window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004103{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004104 struct surface *surface;
4105
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004106 widget_set_allocation(window->main_surface->widget,
4107 window->pending_allocation.x,
4108 window->pending_allocation.y,
4109 window->pending_allocation.width,
4110 window->pending_allocation.height);
4111
4112 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004113
4114 /* The main surface is in the list, too. Main surface's
4115 * resize_handler is responsible for calling widget_set_allocation()
4116 * on all sub-surface root widgets, so they will be resized
4117 * properly.
4118 */
4119 wl_list_for_each(surface, &window->subsurface_list, link) {
4120 if (surface == window->main_surface)
4121 continue;
4122
4123 surface_set_synchronized(surface);
4124 surface_resize(surface);
4125 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004126
4127 if (!window->fullscreen && !window->maximized)
4128 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004129
4130 if (window->confined && window->confined_widget) {
4131 struct wl_compositor *compositor = window->display->compositor;
4132 struct wl_region *region;
4133 struct widget *widget = window->confined_widget;
4134
4135 region = wl_compositor_create_region(compositor);
4136 wl_region_add(region,
4137 widget->allocation.x,
4138 widget->allocation.y,
4139 widget->allocation.width,
4140 widget->allocation.height);
4141 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4142 region);
4143 wl_region_destroy(region);
4144 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004145}
4146
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004147static void
4148idle_resize(struct window *window)
4149{
4150 window->resize_needed = 0;
4151 window->redraw_needed = 1;
4152
4153 DBG("from %dx%d to %dx%d\n",
4154 window->main_surface->server_allocation.width,
4155 window->main_surface->server_allocation.height,
4156 window->pending_allocation.width,
4157 window->pending_allocation.height);
4158
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004159 window_do_resize(window);
4160}
4161
4162static void
4163undo_resize(struct window *window)
4164{
4165 window->pending_allocation.width =
4166 window->main_surface->server_allocation.width;
4167 window->pending_allocation.height =
4168 window->main_surface->server_allocation.height;
4169
4170 DBG("back to %dx%d\n",
4171 window->main_surface->server_allocation.width,
4172 window->main_surface->server_allocation.height);
4173
4174 window_do_resize(window);
4175
4176 if (window->pending_allocation.width == 0 &&
4177 window->pending_allocation.height == 0) {
4178 fprintf(stderr, "Error: Could not draw a surface, "
4179 "most likely due to insufficient disk space in "
4180 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4181 exit(EXIT_FAILURE);
4182 }
4183}
4184
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004185void
4186window_schedule_resize(struct window *window, int width, int height)
4187{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004188 /* We should probably get these numbers from the theme. */
4189 const int min_width = 200, min_height = 200;
4190
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004191 window->pending_allocation.x = 0;
4192 window->pending_allocation.y = 0;
4193 window->pending_allocation.width = width;
4194 window->pending_allocation.height = height;
4195
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004196 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004197 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004198 window->min_allocation.width = min_width;
4199 else
4200 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004201 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004202 window->min_allocation.height = min_height;
4203 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004204 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004205 }
4206
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004207 if (window->pending_allocation.width < window->min_allocation.width)
4208 window->pending_allocation.width = window->min_allocation.width;
4209 if (window->pending_allocation.height < window->min_allocation.height)
4210 window->pending_allocation.height = window->min_allocation.height;
4211
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004212 window->resize_needed = 1;
4213 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004214}
4215
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004216void
4217widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4218{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004219 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004220}
4221
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004222static int
4223window_get_shadow_margin(struct window *window)
4224{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004225 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004226 return frame_get_shadow_margin(window->frame->frame);
4227 else
4228 return 0;
4229}
4230
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004231void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004232window_inhibit_redraw(struct window *window)
4233{
4234 window->redraw_inhibited = 1;
4235 wl_list_remove(&window->redraw_task.link);
4236 wl_list_init(&window->redraw_task.link);
4237 window->redraw_task_scheduled = 0;
4238}
4239
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004240void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004241window_uninhibit_redraw(struct window *window)
4242{
4243 window->redraw_inhibited = 0;
4244 if (window->redraw_needed || window->resize_needed)
4245 window_schedule_redraw_task(window);
4246}
4247
4248static void
4249xdg_surface_handle_configure(void *data,
4250 struct zxdg_surface_v6 *zxdg_surface_v6,
4251 uint32_t serial)
4252{
4253 struct window *window = data;
4254
4255 zxdg_surface_v6_ack_configure(window->xdg_surface, serial);
4256
4257 if (window->state_changed_handler)
4258 window->state_changed_handler(window, window->user_data);
4259
4260 window_uninhibit_redraw(window);
4261}
4262
4263static const struct zxdg_surface_v6_listener xdg_surface_listener = {
4264 xdg_surface_handle_configure
4265};
4266
4267static void
4268xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
4269 int32_t width, int32_t height,
4270 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004271{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004272 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004273 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004274
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004275 window->maximized = 0;
4276 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004277 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004278 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004279
4280 wl_array_for_each(p, states) {
4281 uint32_t state = *p;
4282 switch (state) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004283 case ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004284 window->maximized = 1;
4285 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004286 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004287 window->fullscreen = 1;
4288 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004289 case ZXDG_TOPLEVEL_V6_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004290 window->resizing = 1;
4291 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004292 case ZXDG_TOPLEVEL_V6_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004293 window->focused = 1;
4294 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004295 default:
4296 /* Unknown state */
4297 break;
4298 }
4299 }
4300
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004301 if (window->frame) {
4302 if (window->maximized) {
4303 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4304 } else {
4305 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4306 }
4307
4308 if (window->focused) {
4309 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4310 } else {
4311 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4312 }
4313 }
4314
Jasper St. Pierref184c382014-05-06 08:33:27 -04004315 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004316 /* The width / height params are for window geometry,
4317 * but window_schedule_resize takes allocation. Add
4318 * on the shadow margin to get the difference. */
4319 int margin = window_get_shadow_margin(window);
4320
4321 window_schedule_resize(window,
4322 width + margin * 2,
4323 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004324 } else if (window->saved_allocation.width > 0 &&
4325 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004326 window_schedule_resize(window,
4327 window->saved_allocation.width,
4328 window->saved_allocation.height);
4329 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004330}
4331
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004332static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004333xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004334{
4335 struct window *window = data;
4336 window_close(window);
4337}
4338
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004339static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
4340 xdg_toplevel_handle_configure,
4341 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004342};
4343
4344static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004345window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004346{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004347 struct zxdg_toplevel_v6 *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004348
4349 if (!window->xdg_surface)
4350 return;
4351
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004352 if (window->parent == window->last_parent)
4353 return;
4354
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004355 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004356 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004357 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004358 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004359
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004360 zxdg_toplevel_v6_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004361 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004362}
4363
4364static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004365window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004366{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004367 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004368 frame_input_rect(window->frame->frame,
4369 &geometry->x,
4370 &geometry->y,
4371 &geometry->width,
4372 &geometry->height);
4373 else
4374 window_get_allocation(window, geometry);
4375}
4376
4377static void
4378window_sync_geometry(struct window *window)
4379{
4380 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004381
4382 if (!window->xdg_surface)
4383 return;
4384
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004385 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004386 if (geometry.x == window->last_geometry.x &&
4387 geometry.y == window->last_geometry.y &&
4388 geometry.width == window->last_geometry.width &&
4389 geometry.height == window->last_geometry.height)
4390 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004391
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004392 zxdg_surface_v6_set_window_geometry(window->xdg_surface,
4393 geometry.x,
4394 geometry.y,
4395 geometry.width,
4396 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004397 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004398}
4399
4400static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004401window_flush(struct window *window)
4402{
4403 struct surface *surface;
4404
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004405 assert(!window->redraw_inhibited);
4406
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004407 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004408 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004409 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004410 if (window->xdg_toplevel)
4411 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004412 }
4413
4414 wl_list_for_each(surface, &window->subsurface_list, link) {
4415 if (surface == window->main_surface)
4416 continue;
4417
4418 surface_flush(surface);
4419 }
4420
4421 surface_flush(window->main_surface);
4422}
4423
4424static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004425menu_destroy(struct menu *menu)
4426{
4427 widget_destroy(menu->widget);
4428 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004429 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004430 free(menu);
4431}
4432
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004433void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004434window_get_allocation(struct window *window,
4435 struct rectangle *allocation)
4436{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004437 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004438}
4439
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004440static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004441widget_redraw(struct widget *widget)
4442{
4443 struct widget *child;
4444
4445 if (widget->redraw_handler)
4446 widget->redraw_handler(widget, widget->user_data);
4447 wl_list_for_each(child, &widget->child_list, link)
4448 widget_redraw(child);
4449}
4450
4451static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004452frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4453{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004454 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004455
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004456 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004457 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004458 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004459 surface->frame_cb = NULL;
4460
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004461 surface->last_time = time;
4462
Pekka Paalanen71233882013-04-25 13:57:53 +03004463 if (surface->redraw_needed || surface->window->redraw_needed) {
4464 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004465 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004466 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004467}
4468
4469static const struct wl_callback_listener listener = {
4470 frame_callback
4471};
4472
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004473static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004474surface_redraw(struct surface *surface)
4475{
Pekka Paalanen71233882013-04-25 13:57:53 +03004476 DBG_OBJ(surface->surface, "begin\n");
4477
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004478 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004479 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004480
4481 /* Whole-window redraw forces a redraw even if the previous has
4482 * not yet hit the screen.
4483 */
4484 if (surface->frame_cb) {
4485 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004486 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004487
Pekka Paalanen71233882013-04-25 13:57:53 +03004488 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004489 wl_callback_destroy(surface->frame_cb);
4490 }
4491
Neil Roberts97b747c2013-12-19 16:17:12 +00004492 if (surface->widget->use_cairo &&
4493 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004494 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004495 return -1;
4496 }
4497
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004498 surface->frame_cb = wl_surface_frame(surface->surface);
4499 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004500 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004501
4502 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004503 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004504 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004505 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004506 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004507}
4508
4509static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004510idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004511{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004512 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004513 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004514 int failed = 0;
4515 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004516
Pekka Paalanen71233882013-04-25 13:57:53 +03004517 DBG(" --------- \n");
4518
Pekka Paalaneneebff542013-04-25 13:57:52 +03004519 wl_list_init(&window->redraw_task.link);
4520 window->redraw_task_scheduled = 0;
4521
4522 if (window->resize_needed) {
4523 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004524 if (window->main_surface->frame_cb) {
4525 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004526 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004527 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004528
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004529 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004530 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004531 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004532
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004533 if (surface_redraw(window->main_surface) < 0) {
4534 /*
4535 * Only main_surface failure will cause us to undo the resize.
4536 * If sub-surfaces fail, they will just be broken with old
4537 * content.
4538 */
4539 failed = 1;
4540 } else {
4541 wl_list_for_each(surface, &window->subsurface_list, link) {
4542 if (surface == window->main_surface)
4543 continue;
4544
4545 surface_redraw(surface);
4546 }
4547 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004548
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004549 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004550 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004551
4552 wl_list_for_each(surface, &window->subsurface_list, link)
4553 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004554
4555 if (resized && failed) {
4556 /* Restore widget tree to correspond to what is on screen. */
4557 undo_resize(window);
4558 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004559}
4560
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004561static void
4562window_schedule_redraw_task(struct window *window)
4563{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004564 if (window->redraw_inhibited)
4565 return;
4566
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004567 if (!window->redraw_task_scheduled) {
4568 window->redraw_task.run = idle_redraw;
4569 display_defer(window->display, &window->redraw_task);
4570 window->redraw_task_scheduled = 1;
4571 }
4572}
4573
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004574void
4575window_schedule_redraw(struct window *window)
4576{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004577 struct surface *surface;
4578
Pekka Paalanen71233882013-04-25 13:57:53 +03004579 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4580
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004581 wl_list_for_each(surface, &window->subsurface_list, link)
4582 surface->redraw_needed = 1;
4583
4584 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004585}
4586
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004587int
4588window_is_fullscreen(struct window *window)
4589{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004590 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004591}
4592
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004593void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004594window_set_fullscreen(struct window *window, int fullscreen)
4595{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004596 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004597 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004598
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004599 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004600 return;
4601
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004602 if (fullscreen)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004603 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004604 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004605 zxdg_toplevel_v6_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004606}
4607
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004608int
4609window_is_maximized(struct window *window)
4610{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004611 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004612}
4613
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004614void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004615window_set_maximized(struct window *window, int maximized)
4616{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004617 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004618 return;
4619
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004620 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004621 return;
4622
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004623 if (maximized)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004624 zxdg_toplevel_v6_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004625 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004626 zxdg_toplevel_v6_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004627}
4628
Jasper St. Pierrede680992014-04-10 17:23:49 -07004629int
4630window_is_resizing(struct window *window)
4631{
4632 return window->resizing;
4633}
4634
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004635void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004636window_set_minimized(struct window *window)
4637{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004638 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004639 return;
4640
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004641 zxdg_toplevel_v6_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004642}
4643
4644void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004645window_set_user_data(struct window *window, void *data)
4646{
4647 window->user_data = data;
4648}
4649
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004650void *
4651window_get_user_data(struct window *window)
4652{
4653 return window->user_data;
4654}
4655
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004656void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004657window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004658 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004659{
4660 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004661}
4662
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004663void
4664window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004665 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004666{
4667 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004668}
4669
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004670void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004671window_set_data_handler(struct window *window, window_data_handler_t handler)
4672{
4673 window->data_handler = handler;
4674}
4675
4676void
4677window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4678{
4679 window->drop_handler = handler;
4680}
4681
4682void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004683window_set_close_handler(struct window *window,
4684 window_close_handler_t handler)
4685{
4686 window->close_handler = handler;
4687}
4688
4689void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004690window_set_fullscreen_handler(struct window *window,
4691 window_fullscreen_handler_t handler)
4692{
4693 window->fullscreen_handler = handler;
4694}
4695
4696void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004697window_set_output_handler(struct window *window,
4698 window_output_handler_t handler)
4699{
4700 window->output_handler = handler;
4701}
4702
4703void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004704window_set_state_changed_handler(struct window *window,
4705 window_state_changed_handler_t handler)
4706{
4707 window->state_changed_handler = handler;
4708}
4709
4710void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004711window_set_pointer_locked_handler(struct window *window,
4712 locked_pointer_locked_handler_t locked,
4713 locked_pointer_unlocked_handler_t unlocked)
4714{
4715 window->pointer_unlocked_handler = unlocked;
4716 window->pointer_locked_handler = locked;
4717}
4718
4719void
4720window_set_pointer_confined_handler(struct window *window,
4721 confined_pointer_confined_handler_t confined,
4722 confined_pointer_unconfined_handler_t unconfined)
4723{
4724 window->pointer_confined_handler = confined;
4725 window->pointer_unconfined_handler = unconfined;
4726}
4727
4728void
4729window_set_locked_pointer_motion_handler(struct window *window,
4730 window_locked_pointer_motion_handler_t handler)
4731{
4732 window->locked_pointer_motion_handler = handler;
4733}
4734
4735void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004736window_set_title(struct window *window, const char *title)
4737{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004738 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004739 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004740 if (window->frame) {
4741 frame_set_title(window->frame->frame, title);
4742 widget_schedule_redraw(window->frame->widget);
4743 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004744 if (window->xdg_toplevel)
4745 zxdg_toplevel_v6_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004746}
4747
4748const char *
4749window_get_title(struct window *window)
4750{
4751 return window->title;
4752}
4753
4754void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004755window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4756{
4757 struct text_cursor_position *text_cursor_position =
4758 window->display->text_cursor_position;
4759
Scott Moreau9295ce02012-06-01 12:46:10 -06004760 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004761 return;
4762
4763 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004764 window->main_surface->surface,
4765 wl_fixed_from_int(x),
4766 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004767}
4768
Casey Dahlin9074db52012-04-19 22:50:09 -04004769static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004770relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4771 uint32_t utime_hi,
4772 uint32_t utime_lo,
4773 wl_fixed_t dx,
4774 wl_fixed_t dy,
4775 wl_fixed_t dx_unaccel,
4776 wl_fixed_t dy_unaccel)
4777{
4778 struct input *input = data;
4779 struct window *window = input->pointer_focus;
4780 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4781
4782 if (window->locked_pointer_motion_handler &&
4783 window->pointer_locked) {
4784 window->locked_pointer_motion_handler(
4785 window, input, ms,
4786 wl_fixed_to_double(dx),
4787 wl_fixed_to_double(dy),
4788 window->user_data);
4789 }
4790}
4791
4792static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4793 relative_pointer_handle_motion,
4794};
4795
4796static void
4797locked_pointer_locked(void *data,
4798 struct zwp_locked_pointer_v1 *locked_pointer)
4799{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004800 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004801 struct window *window = input->locked_window;
4802
4803 if (!window)
4804 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004805
4806 window->pointer_locked = true;
4807
4808 if (window->pointer_locked_handler) {
4809 window->pointer_locked_handler(window,
4810 input,
4811 window->user_data);
4812 }
4813}
4814
4815static void
4816locked_pointer_unlocked(void *data,
4817 struct zwp_locked_pointer_v1 *locked_pointer)
4818{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004819 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004820 struct window *window = input->locked_window;
4821
4822 if (!window)
4823 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004824
4825 window_unlock_pointer(window);
4826
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004827 input->locked_window = NULL;
4828
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004829 if (window->pointer_unlocked_handler) {
4830 window->pointer_unlocked_handler(window,
4831 input,
4832 window->user_data);
4833 }
4834}
4835
4836static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4837 locked_pointer_locked,
4838 locked_pointer_unlocked,
4839};
4840
4841int
4842window_lock_pointer(struct window *window, struct input *input)
4843{
4844 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4845 window->display->relative_pointer_manager;
4846 struct zwp_pointer_constraints_v1 *pointer_constraints =
4847 window->display->pointer_constraints;
4848 struct zwp_relative_pointer_v1 *relative_pointer;
4849 struct zwp_locked_pointer_v1 *locked_pointer;
4850
4851 if (!window->display->relative_pointer_manager)
4852 return -1;
4853
4854 if (!window->display->pointer_constraints)
4855 return -1;
4856
4857 if (window->locked_pointer)
4858 return -1;
4859
4860 if (window->confined_pointer)
4861 return -1;
4862
4863 if (!input->pointer)
4864 return -1;
4865
4866 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4867 relative_pointer_manager, input->pointer);
4868 zwp_relative_pointer_v1_add_listener(relative_pointer,
4869 &relative_pointer_listener,
4870 input);
4871
4872 locked_pointer =
4873 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4874 window->main_surface->surface,
4875 input->pointer,
4876 NULL,
4877 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4878 zwp_locked_pointer_v1_add_listener(locked_pointer,
4879 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004880 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004881
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004882 window->locked_pointer = locked_pointer;
4883 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004884 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004885
4886 return 0;
4887}
4888
4889void
4890window_unlock_pointer(struct window *window)
4891{
4892 if (!window->locked_pointer)
4893 return;
4894
4895 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4896 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4897 window->locked_pointer = NULL;
4898 window->relative_pointer = NULL;
4899 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004900}
4901
4902void
4903widget_set_locked_pointer_cursor_hint(struct widget *widget,
4904 float x, float y)
4905{
4906 struct window *window = widget->window;
4907
4908 if (!window->locked_pointer)
4909 return;
4910
4911 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4912 wl_fixed_from_double(x),
4913 wl_fixed_from_double(y));
4914 wl_surface_commit(window->main_surface->surface);
4915}
4916
4917static void
4918confined_pointer_confined(void *data,
4919 struct zwp_confined_pointer_v1 *confined_pointer)
4920{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004921 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004922 struct window *window = input->confined_window;
4923
4924 if (!window)
4925 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004926
4927 window->confined = true;
4928
4929 if (window->pointer_confined_handler) {
4930 window->pointer_confined_handler(window,
4931 input,
4932 window->user_data);
4933 }
4934}
4935
4936static void
4937confined_pointer_unconfined(void *data,
4938 struct zwp_confined_pointer_v1 *confined_pointer)
4939{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004940 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004941 struct window *window = input->confined_window;
4942
4943 if (!window)
4944 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004945
4946 window_unconfine_pointer(window);
4947
4948 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004949 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004950
4951 if (window->pointer_unconfined_handler) {
4952 window->pointer_unconfined_handler(window,
4953 input,
4954 window->user_data);
4955 }
4956}
4957
4958static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4959 confined_pointer_confined,
4960 confined_pointer_unconfined,
4961};
4962
4963int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004964window_confine_pointer_to_rectangles(struct window *window,
4965 struct input *input,
4966 struct rectangle *rectangles,
4967 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004968{
4969 struct zwp_pointer_constraints_v1 *pointer_constraints =
4970 window->display->pointer_constraints;
4971 struct zwp_confined_pointer_v1 *confined_pointer;
4972 struct wl_compositor *compositor = window->display->compositor;
4973 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004974 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004975
4976 if (!window->display->pointer_constraints)
4977 return -1;
4978
4979 if (window->locked_pointer)
4980 return -1;
4981
4982 if (window->confined_pointer)
4983 return -1;
4984
4985 if (!input->pointer)
4986 return -1;
4987
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004988 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004989 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004990 for (i = 0; i < num_rectangles; i++) {
4991 wl_region_add(region,
4992 rectangles[i].x,
4993 rectangles[i].y,
4994 rectangles[i].width,
4995 rectangles[i].height);
4996 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004997 }
4998
4999 confined_pointer =
5000 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5001 window->main_surface->surface,
5002 input->pointer,
5003 region,
5004 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5005 if (region)
5006 wl_region_destroy(region);
5007
5008 zwp_confined_pointer_v1_add_listener(confined_pointer,
5009 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08005010 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005011
5012 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005013 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005014 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005015
5016 return 0;
5017}
5018
5019void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005020window_update_confine_rectangles(struct window *window,
5021 struct rectangle *rectangles,
5022 int num_rectangles)
5023{
5024 struct wl_compositor *compositor = window->display->compositor;
5025 struct wl_region *region;
5026 int i;
5027
5028 region = wl_compositor_create_region(compositor);
5029 for (i = 0; i < num_rectangles; i++) {
5030 wl_region_add(region,
5031 rectangles[i].x,
5032 rectangles[i].y,
5033 rectangles[i].width,
5034 rectangles[i].height);
5035 }
5036
5037 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5038
5039 wl_region_destroy(region);
5040}
5041
5042int
5043window_confine_pointer_to_widget(struct window *window,
5044 struct widget *widget,
5045 struct input *input)
5046{
5047 int ret;
5048
5049 if (widget) {
5050 ret = window_confine_pointer_to_rectangles(window,
5051 input,
5052 &widget->allocation,
5053 1);
5054 window->confined_widget = widget;
5055 return ret;
5056 } else {
5057 return window_confine_pointer_to_rectangles(window,
5058 input,
5059 NULL,
5060 0);
5061 }
5062}
5063
5064void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005065window_unconfine_pointer(struct window *window)
5066{
5067 if (!window->confined_pointer)
5068 return;
5069
5070 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5071 window->confined_pointer = NULL;
5072 window->confined = false;
5073}
5074
5075static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005076surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005077 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005078{
Rob Bradford7507b572012-05-15 17:55:34 +01005079 struct window *window = data;
5080 struct output *output;
5081 struct output *output_found = NULL;
5082 struct window_output *window_output;
5083
5084 wl_list_for_each(output, &window->display->output_list, link) {
5085 if (output->output == wl_output) {
5086 output_found = output;
5087 break;
5088 }
5089 }
5090
5091 if (!output_found)
5092 return;
5093
Brian Lovinbc919262013-08-07 15:34:59 -07005094 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005095 window_output->output = output_found;
5096
5097 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005098
5099 if (window->output_handler)
5100 window->output_handler(window, output_found, 1,
5101 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005102}
5103
5104static void
5105surface_leave(void *data,
5106 struct wl_surface *wl_surface, struct wl_output *output)
5107{
Rob Bradford7507b572012-05-15 17:55:34 +01005108 struct window *window = data;
5109 struct window_output *window_output;
5110 struct window_output *window_output_found = NULL;
5111
5112 wl_list_for_each(window_output, &window->window_output_list, link) {
5113 if (window_output->output->output == output) {
5114 window_output_found = window_output;
5115 break;
5116 }
5117 }
5118
5119 if (window_output_found) {
5120 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005121
5122 if (window->output_handler)
5123 window->output_handler(window, window_output->output,
5124 0, window->user_data);
5125
Rob Bradford7507b572012-05-15 17:55:34 +01005126 free(window_output_found);
5127 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005128}
5129
5130static const struct wl_surface_listener surface_listener = {
5131 surface_enter,
5132 surface_leave
5133};
5134
Pekka Paalanen4e373742013-02-13 16:17:13 +02005135static struct surface *
5136surface_create(struct window *window)
5137{
5138 struct display *display = window->display;
5139 struct surface *surface;
5140
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005141 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005142 surface->window = window;
5143 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005144 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005145 wl_surface_add_listener(surface->surface, &surface_listener, window);
5146
Pekka Paalanen35e82632013-04-25 13:57:48 +03005147 wl_list_insert(&window->subsurface_list, &surface->link);
5148
Pekka Paalanen4e373742013-02-13 16:17:13 +02005149 return surface;
5150}
5151
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005152static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005153get_preferred_buffer_type(struct display *display)
5154{
5155#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005156 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005157 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5158#endif
5159
5160 return WINDOW_BUFFER_TYPE_SHM;
5161}
5162
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005163static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005164window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005165{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005166 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005167 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005168
Peter Huttererf3d62272013-08-08 11:57:05 +10005169 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005170 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005171 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005172
5173 surface = surface_create(window);
5174 window->main_surface = surface;
5175
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005176 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005177
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005178 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005179 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005180
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005181 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005182
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005183 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005184 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005185 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005186
Rob Bradford7507b572012-05-15 17:55:34 +01005187 wl_list_init (&window->window_output_list);
5188
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005189 return window;
5190}
5191
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005192struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005193window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005194{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005195 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005196 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005197
5198 window = window_create_internal(display, 0);
5199
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005200 if (window->display->xdg_shell) {
5201 window->xdg_surface =
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005202 zxdg_shell_v6_get_xdg_surface(window->display->xdg_shell,
5203 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005204 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005205
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005206 zxdg_surface_v6_add_listener(window->xdg_surface,
5207 &xdg_surface_listener, window);
5208
5209 window->xdg_toplevel =
5210 zxdg_surface_v6_get_toplevel(window->xdg_surface);
5211 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5212
5213 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
5214 &xdg_toplevel_listener, window);
5215
5216 window_inhibit_redraw(window);
5217
5218 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005219 } else if (display->ivi_application) {
5220 /* auto generation of ivi_id based on process id + basement of id */
5221 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5222 window->ivi_surface =
5223 ivi_application_surface_create(display->ivi_application,
5224 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005225 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005226
5227 ivi_surface_add_listener(window->ivi_surface,
5228 &ivi_surface_listener, window);
5229 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005230
5231 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005232}
5233
5234struct window *
5235window_create_custom(struct display *display)
5236{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005237 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005238}
5239
Jasper St. Pierre53686042013-12-09 15:26:25 -05005240void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005241window_set_parent(struct window *window,
5242 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005243{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005244 window->parent = parent_window;
5245 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005246}
5247
5248struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005249window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005250{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005251 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005252}
5253
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005254static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005255menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005256{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005257 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005258 int next;
5259
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005260 frame_interior(menu->frame, &x, &y, &width, &height);
5261 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005262 if (menu->current != next) {
5263 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005264 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005265 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005266}
5267
5268static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005269menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005270 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005271 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005272{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005273 struct menu *menu = data;
5274
5275 if (widget == menu->widget)
5276 menu_set_item(data, y);
5277
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005278 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005279}
5280
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005281static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005282menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005283 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005284{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005285 struct menu *menu = data;
5286
5287 if (widget == menu->widget)
5288 menu_set_item(data, y);
5289
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005290 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005291}
5292
5293static void
5294menu_leave_handler(struct widget *widget, struct input *input, void *data)
5295{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005296 struct menu *menu = data;
5297
5298 if (widget == menu->widget)
5299 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005300}
5301
5302static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005303menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005304 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005305 uint32_t button, enum wl_pointer_button_state state,
5306 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005307
5308{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005309 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005310
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005311 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5312 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005313 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005314 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005315 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005316 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005317 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005318 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005319 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005320 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005321}
5322
5323static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005324menu_touch_up_handler(struct widget *widget,
5325 struct input *input,
5326 uint32_t serial,
5327 uint32_t time,
5328 int32_t id,
5329 void *data)
5330{
5331 struct menu *menu = data;
5332
5333 input_ungrab(input);
5334 menu_destroy(menu);
5335}
5336
5337static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005338menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005339{
5340 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005341 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005342 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005343
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005344 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005345
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005346 frame_repaint(menu->frame, cr);
5347 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005348
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005349 theme_set_background_source(menu->window->display->theme,
5350 cr, THEME_FRAME_ACTIVE);
5351 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005352 cairo_fill(cr);
5353
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005354 cairo_select_font_face(cr, "sans",
5355 CAIRO_FONT_SLANT_NORMAL,
5356 CAIRO_FONT_WEIGHT_NORMAL);
5357 cairo_set_font_size(cr, 12);
5358
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005359 for (i = 0; i < menu->count; i++) {
5360 if (i == menu->current) {
5361 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005362 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005363 cairo_fill(cr);
5364 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005365 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005366 cairo_show_text(cr, menu->entries[i]);
5367 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005368 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5369 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005370 cairo_show_text(cr, menu->entries[i]);
5371 }
5372 }
5373
5374 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005375}
5376
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005377static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005378xdg_popup_handle_configure(void *data,
5379 struct zxdg_popup_v6 *zxdg_popup_v6,
5380 int32_t x,
5381 int32_t y,
5382 int32_t width,
5383 int32_t height)
5384{
5385}
5386
5387static void
5388xdg_popup_handle_popup_done(void *data, struct zxdg_popup_v6 *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005389{
5390 struct window *window = data;
5391 struct menu *menu = window->main_surface->widget->user_data;
5392
5393 input_ungrab(menu->input);
5394 menu_destroy(menu);
5395}
5396
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005397static const struct zxdg_popup_v6_listener xdg_popup_listener = {
5398 xdg_popup_handle_configure,
5399 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005400};
5401
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005402static struct menu *
5403create_menu(struct display *display,
5404 struct input *input, uint32_t time,
5405 menu_func_t func, const char **entries, int count,
5406 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005407{
5408 struct window *window;
5409 struct menu *menu;
5410
5411 menu = malloc(sizeof *menu);
5412 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005413 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005414
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005415 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005416 if (!window) {
5417 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005418 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005419 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005420
5421 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005422 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005423 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005424 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005425 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005426 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005427 menu->entries = entries;
5428 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005429 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005430 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005431 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005432 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005433 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005434
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005435 input_ungrab(input);
5436
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005437 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005438 widget_set_enter_handler(menu->widget, menu_enter_handler);
5439 widget_set_leave_handler(menu->widget, menu_leave_handler);
5440 widget_set_motion_handler(menu->widget, menu_motion_handler);
5441 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005442 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005443
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005444 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005445 frame_resize_inside(menu->frame, 200, count * 20);
5446 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5447 window_schedule_resize(window, frame_width(menu->frame),
5448 frame_height(menu->frame));
5449
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005450 return menu;
5451}
5452
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005453static struct zxdg_positioner_v6 *
5454create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005455 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005456{
5457 struct zxdg_positioner_v6 *positioner;
5458
5459 positioner = zxdg_shell_v6_create_positioner(display->xdg_shell);
5460 fail_on_null(positioner, 0, __FILE__, __LINE__);
5461 zxdg_positioner_v6_set_anchor_rect(positioner, x, y, 1, 1);
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005462 zxdg_positioner_v6_set_size(positioner, w, h);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005463 zxdg_positioner_v6_set_anchor(positioner,
5464 ZXDG_POSITIONER_V6_ANCHOR_TOP |
5465 ZXDG_POSITIONER_V6_ANCHOR_LEFT);
5466 zxdg_positioner_v6_set_gravity(positioner,
5467 ZXDG_POSITIONER_V6_ANCHOR_BOTTOM |
5468 ZXDG_POSITIONER_V6_ANCHOR_RIGHT);
5469
5470 return positioner;
5471}
5472
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005473void
5474window_show_menu(struct display *display,
5475 struct input *input, uint32_t time, struct window *parent,
5476 int32_t x, int32_t y,
5477 menu_func_t func, const char **entries, int count)
5478{
5479 struct menu *menu;
5480 struct window *window;
5481 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005482 struct rectangle parent_geometry;
5483 struct zxdg_positioner_v6 *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005484
5485 menu = create_menu(display, input, time, func, entries, count, parent);
5486
5487 if (menu == NULL)
5488 return;
5489
5490 window = menu->window;
5491
5492 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5493 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5494
5495 window->x = x;
5496 window->y = y;
5497
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005498 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005499 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005500
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005501 if (!display->xdg_shell)
5502 return;
5503
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005504 window->xdg_surface =
5505 zxdg_shell_v6_get_xdg_surface(display->xdg_shell,
5506 window->main_surface->surface);
5507 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005508
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005509 zxdg_surface_v6_add_listener(window->xdg_surface,
5510 &xdg_surface_listener, window);
5511
5512 positioner = create_simple_positioner(display,
5513 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005514 window->y - (iy + parent_geometry.y),
5515 frame_width(menu->frame),
5516 frame_height(menu->frame));
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005517 window->xdg_popup =
5518 zxdg_surface_v6_get_popup(window->xdg_surface,
5519 parent->xdg_surface,
5520 positioner);
5521 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
5522 zxdg_positioner_v6_destroy(positioner);
5523 zxdg_popup_v6_grab(window->xdg_popup,
5524 input->seat,
5525 display_get_serial(window->display));
5526 zxdg_popup_v6_add_listener(window->xdg_popup,
5527 &xdg_popup_listener, window);
5528
5529 window_inhibit_redraw(window);
5530
5531 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005532}
5533
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005534void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005535window_set_buffer_type(struct window *window, enum window_buffer_type type)
5536{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005537 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005538}
5539
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005540enum window_buffer_type
5541window_get_buffer_type(struct window *window)
5542{
5543 return window->main_surface->buffer_type;
5544}
5545
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005546void
5547window_set_preferred_format(struct window *window,
5548 enum preferred_format format)
5549{
5550 window->preferred_format = format;
5551}
5552
Pekka Paalanen35e82632013-04-25 13:57:48 +03005553struct widget *
5554window_add_subsurface(struct window *window, void *data,
5555 enum subsurface_mode default_mode)
5556{
5557 struct widget *widget;
5558 struct surface *surface;
5559 struct wl_surface *parent;
5560 struct wl_subcompositor *subcompo = window->display->subcompositor;
5561
Pekka Paalanen35e82632013-04-25 13:57:48 +03005562 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005563 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005564 widget = widget_create(window, surface, data);
5565 wl_list_init(&widget->link);
5566 surface->widget = widget;
5567
5568 parent = window->main_surface->surface;
5569 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5570 surface->surface,
5571 parent);
5572 surface->synchronized = 1;
5573
5574 switch (default_mode) {
5575 case SUBSURFACE_SYNCHRONIZED:
5576 surface->synchronized_default = 1;
5577 break;
5578 case SUBSURFACE_DESYNCHRONIZED:
5579 surface->synchronized_default = 0;
5580 break;
5581 default:
5582 assert(!"bad enum subsurface_mode");
5583 }
5584
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005585 window->resize_needed = 1;
5586 window_schedule_redraw(window);
5587
Pekka Paalanen35e82632013-04-25 13:57:48 +03005588 return widget;
5589}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005590
5591static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005592display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005593 struct wl_output *wl_output,
5594 int x, int y,
5595 int physical_width,
5596 int physical_height,
5597 int subpixel,
5598 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005599 const char *model,
5600 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005601{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005602 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005603
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005604 output->allocation.x = x;
5605 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005606 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005607
5608 if (output->make)
5609 free(output->make);
5610 output->make = strdup(make);
5611
5612 if (output->model)
5613 free(output->model);
5614 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005615}
5616
5617static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005618display_handle_done(void *data,
5619 struct wl_output *wl_output)
5620{
5621}
5622
5623static void
5624display_handle_scale(void *data,
5625 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005626 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005627{
5628 struct output *output = data;
5629
5630 output->scale = scale;
5631}
5632
5633static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005634display_handle_mode(void *data,
5635 struct wl_output *wl_output,
5636 uint32_t flags,
5637 int width,
5638 int height,
5639 int refresh)
5640{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005641 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005642 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005643
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005644 if (flags & WL_OUTPUT_MODE_CURRENT) {
5645 output->allocation.width = width;
5646 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005647 if (display->output_configure_handler)
5648 (*display->output_configure_handler)(
5649 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005650 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005651}
5652
5653static const struct wl_output_listener output_listener = {
5654 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005655 display_handle_mode,
5656 display_handle_done,
5657 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005658};
5659
5660static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005661display_add_output(struct display *d, uint32_t id)
5662{
5663 struct output *output;
5664
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005665 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005666 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005667 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005668 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005669 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005670 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005671 wl_list_insert(d->output_list.prev, &output->link);
5672
5673 wl_output_add_listener(output->output, &output_listener, output);
5674}
5675
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005676static void
5677output_destroy(struct output *output)
5678{
5679 if (output->destroy_handler)
5680 (*output->destroy_handler)(output, output->user_data);
5681
5682 wl_output_destroy(output->output);
5683 wl_list_remove(&output->link);
5684 free(output);
5685}
5686
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005687static void
5688display_destroy_output(struct display *d, uint32_t id)
5689{
5690 struct output *output;
5691
5692 wl_list_for_each(output, &d->output_list, link) {
5693 if (output->server_output_id == id) {
5694 output_destroy(output);
5695 break;
5696 }
5697 }
5698}
5699
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005700void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005701display_set_global_handler(struct display *display,
5702 display_global_handler_t handler)
5703{
5704 struct global *global;
5705
5706 display->global_handler = handler;
5707 if (!handler)
5708 return;
5709
5710 wl_list_for_each(global, &display->global_list, link)
5711 display->global_handler(display,
5712 global->name, global->interface,
5713 global->version, display->user_data);
5714}
5715
5716void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005717display_set_global_handler_remove(struct display *display,
5718 display_global_handler_t remove_handler)
5719{
5720 display->global_handler_remove = remove_handler;
5721 if (!remove_handler)
5722 return;
5723}
5724
5725void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005726display_set_output_configure_handler(struct display *display,
5727 display_output_handler_t handler)
5728{
5729 struct output *output;
5730
5731 display->output_configure_handler = handler;
5732 if (!handler)
5733 return;
5734
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005735 wl_list_for_each(output, &display->output_list, link) {
5736 if (output->allocation.width == 0 &&
5737 output->allocation.height == 0)
5738 continue;
5739
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005740 (*display->output_configure_handler)(output,
5741 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005742 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005743}
5744
5745void
5746output_set_user_data(struct output *output, void *data)
5747{
5748 output->user_data = data;
5749}
5750
5751void *
5752output_get_user_data(struct output *output)
5753{
5754 return output->user_data;
5755}
5756
5757void
5758output_set_destroy_handler(struct output *output,
5759 display_output_handler_t handler)
5760{
5761 output->destroy_handler = handler;
5762 /* FIXME: implement this, once we have way to remove outputs */
5763}
5764
5765void
Scott Moreau4e072362012-09-29 02:03:11 -06005766output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005767{
Scott Moreau4e072362012-09-29 02:03:11 -06005768 struct rectangle allocation = output->allocation;
5769
5770 switch (output->transform) {
5771 case WL_OUTPUT_TRANSFORM_90:
5772 case WL_OUTPUT_TRANSFORM_270:
5773 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5774 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5775 /* Swap width and height */
5776 allocation.width = output->allocation.height;
5777 allocation.height = output->allocation.width;
5778 break;
5779 }
5780
5781 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005782}
5783
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005784struct wl_output *
5785output_get_wl_output(struct output *output)
5786{
5787 return output->output;
5788}
5789
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005790enum wl_output_transform
5791output_get_transform(struct output *output)
5792{
5793 return output->transform;
5794}
5795
Alexander Larssonafd319a2013-05-22 14:41:27 +02005796uint32_t
5797output_get_scale(struct output *output)
5798{
5799 return output->scale;
5800}
5801
Jason Ekstrand738715d2014-04-02 19:53:50 -05005802const char *
5803output_get_make(struct output *output)
5804{
5805 return output->make;
5806}
5807
5808const char *
5809output_get_model(struct output *output)
5810{
5811 return output->model;
5812}
5813
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005814static void
Daniel Stone97f68542012-05-30 16:32:01 +01005815fini_xkb(struct input *input)
5816{
5817 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005818 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005819}
5820
5821static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005822display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005823{
5824 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005825 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005826
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005827 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005828 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005829 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005830 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005831 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005832 input->pointer_focus = NULL;
5833 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005834 input->seat_version = seat_version;
5835
Rusty Lynch041815a2013-08-08 21:20:38 -07005836 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005837 wl_list_insert(d->input_list.prev, &input->link);
5838
Daniel Stone37816df2012-05-16 18:45:18 +01005839 wl_seat_add_listener(input->seat, &seat_listener, input);
5840 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005841
Jason Ekstranda669bd52014-04-02 19:53:51 -05005842 if (d->data_device_manager) {
5843 input->data_device =
5844 wl_data_device_manager_get_data_device(d->data_device_manager,
5845 input->seat);
5846 wl_data_device_add_listener(input->data_device,
5847 &data_device_listener,
5848 input);
5849 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005850
5851 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005852
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005853 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5854 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005855
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005856 set_repeat_info(input, 40, 400);
5857 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5858 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005859}
5860
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005861static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005862input_destroy(struct input *input)
5863{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005864 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005865 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005866
5867 if (input->drag_offer)
5868 data_offer_destroy(input->drag_offer);
5869
5870 if (input->selection_offer)
5871 data_offer_destroy(input->selection_offer);
5872
kabeer khan6ce67ec2014-10-20 11:55:29 +05305873 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005874 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305875 wl_data_device_release(input->data_device);
5876 else
5877 wl_data_device_destroy(input->data_device);
5878 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005879 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005880 if (input->touch)
5881 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005882 if (input->pointer)
5883 wl_pointer_release(input->pointer);
5884 if (input->keyboard)
5885 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005886 } else {
5887 if (input->touch)
5888 wl_touch_destroy(input->touch);
5889 if (input->pointer)
5890 wl_pointer_destroy(input->pointer);
5891 if (input->keyboard)
5892 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005893 }
5894
Daniel Stone97f68542012-05-30 16:32:01 +01005895 fini_xkb(input);
5896
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005897 wl_surface_destroy(input->pointer_surface);
5898
Pekka Paalanene1207c72011-12-16 12:02:09 +02005899 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005900 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005901 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005902 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005903 free(input);
5904}
5905
5906static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005907shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5908{
5909 struct display *d = data;
5910
5911 if (format == WL_SHM_FORMAT_RGB565)
5912 d->has_rgb565 = 1;
5913}
5914
5915struct wl_shm_listener shm_listener = {
5916 shm_format
5917};
5918
5919static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005920xdg_shell_handle_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005921{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005922 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005923}
5924
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005925static const struct zxdg_shell_v6_listener xdg_shell_listener = {
5926 xdg_shell_handle_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005927};
5928
5929static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005930registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5931 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005932{
5933 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005934 struct global *global;
5935
Brian Lovinbc919262013-08-07 15:34:59 -07005936 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005937 global->name = id;
5938 global->interface = strdup(interface);
5939 global->version = version;
5940 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005941
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005942 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005943 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005944 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005945 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005946 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005947 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005948 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005949 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5950 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5951 d->relative_pointer_manager =
5952 wl_registry_bind(registry, id,
5953 &zwp_relative_pointer_manager_v1_interface,
5954 1);
5955 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5956 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5957 d->pointer_constraints =
5958 wl_registry_bind(registry, id,
5959 &zwp_pointer_constraints_v1_interface,
5960 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005961 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005962 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005963 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005964 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005965 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005966 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005967 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305968 &wl_data_device_manager_interface,
5969 d->data_device_manager_version);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005970 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005971 d->xdg_shell = wl_registry_bind(registry, id,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005972 &zxdg_shell_v6_interface, 1);
5973 zxdg_shell_v6_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005974 } else if (strcmp(interface, "text_cursor_position") == 0) {
5975 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005976 wl_registry_bind(registry, id,
5977 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005978 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5979 d->subcompositor =
5980 wl_registry_bind(registry, id,
5981 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005982 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005983 else if (strcmp(interface, "ivi_application") == 0) {
5984 d->ivi_application =
5985 wl_registry_bind(registry, id,
5986 &ivi_application_interface, 1);
5987 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005988
5989 if (d->global_handler)
5990 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005991}
5992
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005993static void
5994registry_handle_global_remove(void *data, struct wl_registry *registry,
5995 uint32_t name)
5996{
5997 struct display *d = data;
5998 struct global *global;
5999 struct global *tmp;
6000
6001 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6002 if (global->name != name)
6003 continue;
6004
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006005 if (strcmp(global->interface, "wl_output") == 0)
6006 display_destroy_output(d, name);
6007
6008 /* XXX: Should destroy remaining bound globals */
6009
6010 if (d->global_handler_remove)
6011 d->global_handler_remove(d, name, global->interface,
6012 global->version, d->user_data);
6013
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006014 wl_list_remove(&global->link);
6015 free(global->interface);
6016 free(global);
6017 }
6018}
6019
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006020void *
6021display_bind(struct display *display, uint32_t name,
6022 const struct wl_interface *interface, uint32_t version)
6023{
6024 return wl_registry_bind(display->registry, name, interface, version);
6025}
6026
6027static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006028 registry_handle_global,
6029 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006030};
6031
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006032#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006033static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006034init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006035{
6036 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006037 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006038
Rob Clark6396ed32012-03-11 19:48:41 -05006039#ifdef USE_CAIRO_GLESV2
6040# define GL_BIT EGL_OPENGL_ES2_BIT
6041#else
6042# define GL_BIT EGL_OPENGL_BIT
6043#endif
6044
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006045 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006046 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006047 EGL_RED_SIZE, 1,
6048 EGL_GREEN_SIZE, 1,
6049 EGL_BLUE_SIZE, 1,
6050 EGL_ALPHA_SIZE, 1,
6051 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006052 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006053 EGL_NONE
6054 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006055
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006056#ifdef USE_CAIRO_GLESV2
6057 static const EGLint context_attribs[] = {
6058 EGL_CONTEXT_CLIENT_VERSION, 2,
6059 EGL_NONE
6060 };
6061 EGLint api = EGL_OPENGL_ES_API;
6062#else
6063 EGLint *context_attribs = NULL;
6064 EGLint api = EGL_OPENGL_API;
6065#endif
6066
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006067 d->dpy =
6068 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6069 d->display, NULL);
6070
Yuval Fledel45568f62010-12-06 09:18:12 -05006071 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006072 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006073 return -1;
6074 }
6075
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006076 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006077 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006078 return -1;
6079 }
6080
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006081 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6082 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006083 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006084 return -1;
6085 }
6086
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006087 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006088 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006089 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006090 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006091 return -1;
6092 }
6093
Benjamin Franzke0c991632011-09-27 21:57:31 +02006094 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6095 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006096 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006097 return -1;
6098 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006099
6100 return 0;
6101}
6102
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006103static void
6104fini_egl(struct display *display)
6105{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006106 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006107
6108 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6109 EGL_NO_CONTEXT);
6110
6111 eglTerminate(display->dpy);
6112 eglReleaseThread();
6113}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006114#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006115
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006116static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006117init_dummy_surface(struct display *display)
6118{
6119 int len;
6120 void *data;
6121
6122 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006123 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006124 display->dummy_surface =
6125 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6126 1, 1, len);
6127 display->dummy_surface_data = data;
6128}
6129
6130static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006131handle_display_data(struct task *task, uint32_t events)
6132{
6133 struct display *display =
6134 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006135 struct epoll_event ep;
6136 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006137
6138 display->display_fd_events = events;
6139
6140 if (events & EPOLLERR || events & EPOLLHUP) {
6141 display_exit(display);
6142 return;
6143 }
6144
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006145 if (events & EPOLLIN) {
6146 ret = wl_display_dispatch(display->display);
6147 if (ret == -1) {
6148 display_exit(display);
6149 return;
6150 }
6151 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006152
6153 if (events & EPOLLOUT) {
6154 ret = wl_display_flush(display->display);
6155 if (ret == 0) {
6156 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6157 ep.data.ptr = &display->display_task;
6158 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6159 display->display_fd, &ep);
6160 } else if (ret == -1 && errno != EAGAIN) {
6161 display_exit(display);
6162 return;
6163 }
6164 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006165}
6166
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006167static void
6168log_handler(const char *format, va_list args)
6169{
6170 vfprintf(stderr, format, args);
6171}
6172
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006173struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006174display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006175{
6176 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006177
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006178 wl_log_set_handler_client(log_handler);
6179
Peter Huttererf3d62272013-08-08 11:57:05 +10006180 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006181 if (d == NULL)
6182 return NULL;
6183
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006184 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006185 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006186 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006187 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006188 return NULL;
6189 }
6190
Rob Bradford5ab9c752013-07-26 16:29:43 +01006191 d->xkb_context = xkb_context_new(0);
6192 if (d->xkb_context == NULL) {
6193 fprintf(stderr, "Failed to create XKB context\n");
6194 free(d);
6195 return NULL;
6196 }
6197
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006198 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006199 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006200 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006201 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6202 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006203
6204 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006205 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006206 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006207 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006208
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006209 d->registry = wl_display_get_registry(d->display);
6210 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006211
Marek Chalupaa519d062014-12-05 13:49:40 +01006212 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006213 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6214 return NULL;
6215 }
6216
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006217#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006218 if (init_egl(d) < 0)
6219 fprintf(stderr, "EGL does not seem to work, "
6220 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006221#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006222
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006223 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006224
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006225 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006226
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006227 wl_list_init(&d->window_list);
6228
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006229 init_dummy_surface(d);
6230
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006231 return d;
6232}
6233
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006234static void
6235display_destroy_outputs(struct display *display)
6236{
6237 struct output *tmp;
6238 struct output *output;
6239
6240 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6241 output_destroy(output);
6242}
6243
Pekka Paalanene1207c72011-12-16 12:02:09 +02006244static void
6245display_destroy_inputs(struct display *display)
6246{
6247 struct input *tmp;
6248 struct input *input;
6249
6250 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6251 input_destroy(input);
6252}
6253
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006254void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006255display_destroy(struct display *display)
6256{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006257 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006258 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6259 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006260
6261 if (!wl_list_empty(&display->deferred_list))
6262 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6263
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006264 cairo_surface_destroy(display->dummy_surface);
6265 free(display->dummy_surface_data);
6266
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006267 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006268 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006269
Daniel Stone97f68542012-05-30 16:32:01 +01006270 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006271
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006272 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006273 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006274
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006275#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006276 if (display->argb_device)
6277 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006278#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006279
Pekka Paalanen35e82632013-04-25 13:57:48 +03006280 if (display->subcompositor)
6281 wl_subcompositor_destroy(display->subcompositor);
6282
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006283 if (display->xdg_shell)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006284 zxdg_shell_v6_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006285
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006286 if (display->ivi_application)
6287 ivi_application_destroy(display->ivi_application);
6288
Pekka Paalanenc2052982011-12-16 11:41:32 +02006289 if (display->shm)
6290 wl_shm_destroy(display->shm);
6291
6292 if (display->data_device_manager)
6293 wl_data_device_manager_destroy(display->data_device_manager);
6294
6295 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006296 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006297
6298 close(display->epoll_fd);
6299
U. Artie Eoff44874d92012-10-02 21:12:35 -07006300 if (!(display->display_fd_events & EPOLLERR) &&
6301 !(display->display_fd_events & EPOLLHUP))
6302 wl_display_flush(display->display);
6303
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006304 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006305 free(display);
6306}
6307
6308void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006309display_set_user_data(struct display *display, void *data)
6310{
6311 display->user_data = data;
6312}
6313
6314void *
6315display_get_user_data(struct display *display)
6316{
6317 return display->user_data;
6318}
6319
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006320struct wl_display *
6321display_get_display(struct display *display)
6322{
6323 return display->display;
6324}
6325
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006326int
6327display_has_subcompositor(struct display *display)
6328{
6329 if (display->subcompositor)
6330 return 1;
6331
6332 wl_display_roundtrip(display->display);
6333
6334 return display->subcompositor != NULL;
6335}
6336
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006337cairo_device_t *
6338display_get_cairo_device(struct display *display)
6339{
6340 return display->argb_device;
6341}
6342
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006343struct output *
6344display_get_output(struct display *display)
6345{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006346 if (wl_list_empty(&display->output_list))
6347 return NULL;
6348
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006349 return container_of(display->output_list.next, struct output, link);
6350}
6351
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006352struct wl_compositor *
6353display_get_compositor(struct display *display)
6354{
6355 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006356}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006357
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006358uint32_t
6359display_get_serial(struct display *display)
6360{
6361 return display->serial;
6362}
6363
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006364EGLDisplay
6365display_get_egl_display(struct display *d)
6366{
6367 return d->dpy;
6368}
6369
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006370struct wl_data_source *
6371display_create_data_source(struct display *display)
6372{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006373 if (display->data_device_manager)
6374 return wl_data_device_manager_create_data_source(display->data_device_manager);
6375 else
6376 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006377}
6378
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006379EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006380display_get_argb_egl_config(struct display *d)
6381{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006382 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006383}
6384
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006385int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006386display_acquire_window_surface(struct display *display,
6387 struct window *window,
6388 EGLContext ctx)
6389{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006390 struct surface *surface = window->main_surface;
6391
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006392 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006393 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006394
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006395 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006396 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006397}
6398
6399void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006400display_release_window_surface(struct display *display,
6401 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006402{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006403 struct surface *surface = window->main_surface;
6404
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006405 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006406 return;
6407
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006408 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006409}
6410
6411void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006412display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006413{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006414 wl_list_insert(&display->deferred_list, &task->link);
6415}
6416
6417void
6418display_watch_fd(struct display *display,
6419 int fd, uint32_t events, struct task *task)
6420{
6421 struct epoll_event ep;
6422
6423 ep.events = events;
6424 ep.data.ptr = task;
6425 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6426}
6427
6428void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006429display_unwatch_fd(struct display *display, int fd)
6430{
6431 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6432}
6433
6434void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006435display_run(struct display *display)
6436{
6437 struct task *task;
6438 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006439 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006440
Pekka Paalanen826d7952011-12-15 10:14:07 +02006441 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006442 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006443 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006444 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006445 struct task, link);
6446 wl_list_remove(&task->link);
6447 task->run(task, 0);
6448 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006449
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006450 wl_display_dispatch_pending(display->display);
6451
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006452 if (!display->running)
6453 break;
6454
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006455 ret = wl_display_flush(display->display);
6456 if (ret < 0 && errno == EAGAIN) {
6457 ep[0].events =
6458 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6459 ep[0].data.ptr = &display->display_task;
6460
6461 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6462 display->display_fd, &ep[0]);
6463 } else if (ret < 0) {
6464 break;
6465 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006466
6467 count = epoll_wait(display->epoll_fd,
6468 ep, ARRAY_LENGTH(ep), -1);
6469 for (i = 0; i < count; i++) {
6470 task = ep[i].data.ptr;
6471 task->run(task, ep[i].events);
6472 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006473 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006474}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006475
6476void
6477display_exit(struct display *display)
6478{
6479 display->running = 0;
6480}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006481
Carlos Garnacho9c931792016-01-18 23:52:12 +01006482int
6483display_get_data_device_manager_version(struct display *display)
6484{
6485 return display->data_device_manager_version;
6486}
6487
Jan Arne Petersencd997062012-11-18 19:06:44 +01006488void
6489keysym_modifiers_add(struct wl_array *modifiers_map,
6490 const char *name)
6491{
6492 size_t len = strlen(name) + 1;
6493 char *p;
6494
6495 p = wl_array_add(modifiers_map, len);
6496
6497 if (p == NULL)
6498 return;
6499
6500 strncpy(p, name, len);
6501}
6502
6503static xkb_mod_index_t
6504keysym_modifiers_get_index(struct wl_array *modifiers_map,
6505 const char *name)
6506{
6507 xkb_mod_index_t index = 0;
6508 char *p = modifiers_map->data;
6509
6510 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6511 if (strcmp(p, name) == 0)
6512 return index;
6513
6514 index++;
6515 p += strlen(p) + 1;
6516 }
6517
6518 return XKB_MOD_INVALID;
6519}
6520
6521xkb_mod_mask_t
6522keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6523 const char *name)
6524{
6525 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6526
6527 if (index == XKB_MOD_INVALID)
6528 return XKB_MOD_INVALID;
6529
6530 return 1 << index;
6531}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006532
6533static void
6534toytimer_fire(struct task *tsk, uint32_t events)
6535{
6536 uint64_t e;
6537 struct toytimer *tt;
6538
6539 tt = container_of(tsk, struct toytimer, tsk);
6540
6541 if (events != EPOLLIN)
6542 fprintf(stderr, "unexpected timerfd events %x\n", events);
6543
6544 if (!(events & EPOLLIN))
6545 return;
6546
6547 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6548 /* If we change the timer between the fd becoming
6549 * readable and getting here, there'll be nothing to
6550 * read and we get EAGAIN. */
6551 if (errno != EAGAIN)
6552 fprintf(stderr, "timer read failed: %m\n");
6553 return;
6554 }
6555
6556 tt->callback(tt);
6557}
6558
6559void
6560toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6561 toytimer_cb callback)
6562{
6563 memset(tt, 0, sizeof *tt);
6564
6565 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6566 if (tt->fd == -1) {
6567 fprintf(stderr, "creating timer failed: %m\n");
6568 abort();
6569 }
6570
6571 tt->display = display;
6572 tt->callback = callback;
6573 tt->tsk.run = toytimer_fire;
6574 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6575}
6576
6577void
6578toytimer_fini(struct toytimer *tt)
6579{
6580 display_unwatch_fd(tt->display, tt->fd);
6581 close(tt->fd);
6582 tt->fd = -1;
6583}
6584
6585void
6586toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6587{
6588 int ret;
6589
6590 ret = timerfd_settime(tt->fd, 0, its, NULL);
6591 if (ret < 0) {
6592 fprintf(stderr, "timer setup failed: %m\n");
6593 abort();
6594 }
6595}
6596
6597#define USEC_PER_SEC 1000000
6598
6599void
6600toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6601{
6602 struct itimerspec its;
6603
6604 its.it_interval.tv_sec = 0;
6605 its.it_interval.tv_nsec = 0;
6606 its.it_value.tv_sec = usec / USEC_PER_SEC;
6607 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6608 toytimer_arm(tt, &its);
6609}
6610
6611void
6612toytimer_disarm(struct toytimer *tt)
6613{
6614 struct itimerspec its = {};
6615
6616 toytimer_arm(tt, &its);
6617}