blob: dfde55eea63a535ced947f708f5f1274c118e76f [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"
Pekka Paalanenecbdcfd2019-04-04 14:46:00 +030076#include <libweston/zalloc.h>
ant8mef99fac22018-11-28 22:46:37 +010077#include "xdg-shell-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"
emersionac71ee52018-11-18 21:42:10 +010082#include "shared/string-helpers.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050083
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050084#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050085
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080086#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
87#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
88
emersionac71ee52018-11-18 21:42:10 +010089#define DEFAULT_XCURSOR_SIZE 32
90
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070091struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030092
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040093struct global {
94 uint32_t name;
95 char *interface;
96 uint32_t version;
97 struct wl_list link;
98};
99
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500100struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500101 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400102 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500103 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300104 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400105 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400106 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600107 struct text_cursor_position *text_cursor_position;
ant8mef99fac22018-11-28 22:46:37 +0100108 struct xdg_wm_base *xdg_shell;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800109 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
110 struct zwp_pointer_constraints_v1 *pointer_constraints;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400111 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500112 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200113 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200114 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400115 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400116
117 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700118 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400119 struct task display_task;
120
121 int epoll_fd;
122 struct wl_list deferred_list;
123
Pekka Paalanen826d7952011-12-15 10:14:07 +0200124 int running;
125
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400126 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400127 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500129 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400130
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400131 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400132
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300133 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300134 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400135
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200136 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400137 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800138 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200139
140 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100141
142 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200143
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200144 /* A hack to get text extents for tooltips */
145 cairo_surface_t *dummy_surface;
146 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200147
148 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530149 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500150};
151
Rob Bradford7507b572012-05-15 17:55:34 +0100152struct window_output {
153 struct output *output;
154 struct wl_list link;
155};
156
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157struct toysurface {
158 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500159 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800160 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200162 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200163 * If flags has SURFACE_HINT_RESIZE set, the user is
164 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200165 * Returns the Cairo surface to draw to.
166 */
167 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200168 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200169 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200170
171 /*
172 * Post the surface to the server, returning the server allocation
173 * rectangle. The Cairo surface from prepare() must be destroyed
174 * after calling this.
175 */
176 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200177 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200178 struct rectangle *server_allocation);
179
180 /*
181 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800182 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200183 */
184 int (*acquire)(struct toysurface *base, EGLContext ctx);
185
186 /*
187 * Release the toysurface from the EGL context, returning control
188 * to Cairo.
189 */
190 void (*release)(struct toysurface *base);
191
192 /*
193 * Destroy the toysurface, including the Cairo surface, any
194 * backing storage, and the Wayland protocol objects.
195 */
196 void (*destroy)(struct toysurface *base);
197};
198
Pekka Paalanen4e373742013-02-13 16:17:13 +0200199struct surface {
200 struct window *window;
201
202 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300203 struct wl_subsurface *subsurface;
204 int synchronized;
205 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200206 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200207 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300208 int redraw_needed;
209 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300210 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200211
212 struct rectangle allocation;
213 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200214
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200215 struct wl_region *input_region;
216 struct wl_region *opaque_region;
217
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200218 enum window_buffer_type buffer_type;
219 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200220 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200221
222 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300223
224 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200225};
226
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500227struct window {
228 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100229 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500230 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200231 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400232 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500233 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200234 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500235 int x, y;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800236 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400237 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300238 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400239 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400240 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500241 int custom;
242 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400243
Pekka Paalanen99436862012-11-19 17:15:59 +0200244 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400245
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500246 int fullscreen;
247 int maximized;
248
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200249 enum preferred_format preferred_format;
250
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500251 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500252 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400253 window_data_handler_t data_handler;
254 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500255 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400256 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200257 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700258 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400259
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800260 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
261
Pekka Paalanen4e373742013-02-13 16:17:13 +0200262 struct surface *main_surface;
ant8mef99fac22018-11-28 22:46:37 +0100263 struct xdg_surface *xdg_surface;
264 struct xdg_toplevel *xdg_toplevel;
265 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300266
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700267 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800268 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500269
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500270 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500271
Pekka Paalanen35e82632013-04-25 13:57:48 +0300272 /* struct surface::link, contains also main_surface */
273 struct wl_list subsurface_list;
274
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800275 struct zwp_relative_pointer_v1 *relative_pointer;
276 struct zwp_locked_pointer_v1 *locked_pointer;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800277 bool pointer_locked;
278 locked_pointer_locked_handler_t pointer_locked_handler;
279 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
280 confined_pointer_confined_handler_t pointer_confined_handler;
281 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
282
283 struct zwp_confined_pointer_v1 *confined_pointer;
284 struct widget *confined_widget;
285 bool confined;
286
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500287 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400288 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500289};
290
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500291struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500292 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200293 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300294 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500295 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400296 struct wl_list link;
297 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500298 widget_resize_handler_t resize_handler;
299 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500300 widget_enter_handler_t enter_handler;
301 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500302 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500303 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 widget_touch_down_handler_t touch_down_handler;
305 widget_touch_up_handler_t touch_up_handler;
306 widget_touch_motion_handler_t touch_motion_handler;
307 widget_touch_frame_handler_t touch_frame_handler;
308 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200309 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000310 widget_pointer_frame_handler_t pointer_frame_handler;
311 widget_axis_source_handler_t axis_source_handler;
312 widget_axis_stop_handler_t axis_stop_handler;
313 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400314 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500315 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300316 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500317 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000318 /* If this is set to false then no cairo surface will be
319 * created before redrawing the surface. This is useful if the
320 * redraw handler is going to do completely custom rendering
321 * such as using EGL directly */
322 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400323};
324
Rusty Lynch041815a2013-08-08 21:20:38 -0700325struct touch_point {
326 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800327 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700328 struct widget *widget;
329 struct wl_list link;
330};
331
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400332struct input {
333 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100334 struct wl_seat *seat;
335 struct wl_pointer *pointer;
336 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700337 struct wl_touch *touch;
338 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400339 struct window *pointer_focus;
340 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700341 struct window *touch_focus;
Dima Ryazanov13bdf252018-11-14 22:17:42 -0800342 struct window *locked_window;
343 struct window *confined_window;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300344 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300345 uint32_t cursor_anim_start;
346 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500347 uint32_t cursor_timer_start;
348 uint32_t cursor_anim_current;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200349 struct toytimer cursor_timer;
Derek Foreman118a4292015-04-22 17:23:35 -0500350 bool cursor_timer_running;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300351 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400352 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400353 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400354 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400355 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400356 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400357
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500358 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500359 struct widget *grab;
360 uint32_t grab_button;
361
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400362 struct wl_data_device *data_device;
363 struct data_offer *drag_offer;
364 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800365 uint32_t touch_grab;
366 int32_t touch_grab_id;
367 float drag_x, drag_y;
368 struct window *drag_focus;
369 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100370
371 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100372 struct xkb_keymap *keymap;
373 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +0100374#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700375 struct xkb_compose_table *compose_table;
376 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +0100377#endif
Daniel Stone97f68542012-05-30 16:32:01 +0100378 xkb_mod_mask_t control_mask;
379 xkb_mod_mask_t alt_mask;
380 xkb_mod_mask_t shift_mask;
381 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400382
Jonny Lamb06959082014-08-12 14:58:27 +0200383 int32_t repeat_rate_sec;
384 int32_t repeat_rate_nsec;
385 int32_t repeat_delay_sec;
386 int32_t repeat_delay_nsec;
387
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200388 struct toytimer repeat_timer;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400389 uint32_t repeat_sym;
390 uint32_t repeat_key;
391 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500392 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400393};
394
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500395struct output {
396 struct display *display;
397 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800398 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500399 struct rectangle allocation;
400 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600401 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200402 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500403 char *make;
404 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200405
406 display_output_handler_t destroy_handler;
407 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500408};
409
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500410struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500411 struct widget *widget;
412 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500413 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800414
415 uint32_t last_time;
416 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800417 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500418};
419
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500420struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400421 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500422 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500423 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500424 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700425 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500426 const char **entries;
427 uint32_t time;
428 int current;
429 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400430 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500431 menu_func_t func;
432};
433
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300434struct tooltip {
435 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300436 struct widget *widget;
437 char *entry;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200438 struct toytimer timer;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300439 float x, y;
440};
441
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700442struct shm_pool {
443 struct wl_shm_pool *pool;
444 size_t size;
445 size_t used;
446 void *data;
447};
448
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400449enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300450 CURSOR_DEFAULT = 100,
451 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400452};
453
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200454static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400455
Pekka Paalanen97777442013-05-22 10:20:05 +0300456/* #define DEBUG */
457
458#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300459
460static void
461debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
462__attribute__ ((format (printf, 4, 5)));
463
464static void
465debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
466{
467 va_list ap;
468 struct timeval tv;
469
470 gettimeofday(&tv, NULL);
471 fprintf(stderr, "%8ld.%03ld ",
472 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
473
474 if (proxy)
475 fprintf(stderr, "%s@%d ",
476 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
477
478 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
479 fprintf(stderr, "%s ", func);
480
481 va_start(ap, fmt);
482 vfprintf(stderr, fmt, ap);
483 va_end(ap);
484}
485
486#define DBG(fmt, ...) \
487 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
488
489#define DBG_OBJ(obj, fmt, ...) \
490 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
491
492#else
493
494#define DBG(...) do {} while (0)
495#define DBG_OBJ(...) do {} while (0)
496
497#endif
498
Alexander Larsson1818e312013-05-22 14:41:31 +0200499static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200500surface_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 +0200501{
502 int32_t tmp;
503
504 switch (buffer_transform) {
505 case WL_OUTPUT_TRANSFORM_90:
506 case WL_OUTPUT_TRANSFORM_270:
507 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
508 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
509 tmp = *width;
510 *width = *height;
511 *height = tmp;
512 break;
513 default:
514 break;
515 }
516
517 *width *= buffer_scale;
518 *height *= buffer_scale;
519}
520
521static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200522buffer_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 +0200523{
524 int32_t tmp;
525
526 switch (buffer_transform) {
527 case WL_OUTPUT_TRANSFORM_90:
528 case WL_OUTPUT_TRANSFORM_270:
529 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
530 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
531 tmp = *width;
532 *width = *height;
533 *height = tmp;
534 break;
535 default:
536 break;
537 }
538
539 *width /= buffer_scale;
540 *height /= buffer_scale;
541}
542
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500543#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200545struct egl_window_surface {
546 struct toysurface base;
547 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100548 struct display *display;
549 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200550 struct wl_egl_window *egl_window;
551 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100552};
553
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200554static struct egl_window_surface *
555to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100556{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200557 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558}
559
560static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200561egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200562 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200563 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100564{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200565 struct egl_window_surface *surface = to_egl_window_surface(base);
566
Alexander Larsson1818e312013-05-22 14:41:31 +0200567 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
568
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200569 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
570 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
571
572 return cairo_surface_reference(surface->cairo_surface);
573}
574
575static void
576egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200577 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200578 struct rectangle *server_allocation)
579{
580 struct egl_window_surface *surface = to_egl_window_surface(base);
581
582 cairo_gl_surface_swapbuffers(surface->cairo_surface);
583 wl_egl_window_get_attached_size(surface->egl_window,
584 &server_allocation->width,
585 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200586
587 buffer_to_surface_size (buffer_transform, buffer_scale,
588 &server_allocation->width,
589 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200590}
591
592static int
593egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
594{
595 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200596 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100597
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200598 device = cairo_surface_get_device(surface->cairo_surface);
599 if (!device)
600 return -1;
601
602 if (!ctx) {
603 if (device == surface->display->argb_device)
604 ctx = surface->display->argb_ctx;
605 else
606 assert(0);
607 }
608
609 cairo_device_flush(device);
610 cairo_device_acquire(device);
611 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
612 surface->egl_surface, ctx))
613 fprintf(stderr, "failed to make surface current\n");
614
615 return 0;
616}
617
618static void
619egl_window_surface_release(struct toysurface *base)
620{
621 struct egl_window_surface *surface = to_egl_window_surface(base);
622 cairo_device_t *device;
623
624 device = cairo_surface_get_device(surface->cairo_surface);
625 if (!device)
626 return;
627
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200628 if (!eglMakeCurrent(surface->display->dpy,
629 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200630 fprintf(stderr, "failed to make context current\n");
631
632 cairo_device_release(device);
633}
634
635static void
636egl_window_surface_destroy(struct toysurface *base)
637{
638 struct egl_window_surface *surface = to_egl_window_surface(base);
639 struct display *d = surface->display;
640
641 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000642 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200643 wl_egl_window_destroy(surface->egl_window);
644 surface->surface = NULL;
645
646 free(surface);
647}
648
649static struct toysurface *
650egl_window_surface_create(struct display *display,
651 struct wl_surface *wl_surface,
652 uint32_t flags,
653 struct rectangle *rectangle)
654{
655 struct egl_window_surface *surface;
656
Pekka Paalanenb3627362012-11-19 17:16:00 +0200657 if (display->dpy == EGL_NO_DISPLAY)
658 return NULL;
659
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800660 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200661 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100662 return NULL;
663
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200664 surface->base.prepare = egl_window_surface_prepare;
665 surface->base.swap = egl_window_surface_swap;
666 surface->base.acquire = egl_window_surface_acquire;
667 surface->base.release = egl_window_surface_release;
668 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100669
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200670 surface->display = display;
671 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400672
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200673 surface->egl_window = wl_egl_window_create(surface->surface,
674 rectangle->width,
675 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100676
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100677 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100678 weston_platform_create_egl_surface(display->dpy,
679 display->argb_config,
680 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100681
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200682 surface->cairo_surface =
683 cairo_gl_surface_create_for_egl(display->argb_device,
684 surface->egl_surface,
685 rectangle->width,
686 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100687
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200688 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100689}
690
Pekka Paalanenb3627362012-11-19 17:16:00 +0200691#else
692
693static struct toysurface *
694egl_window_surface_create(struct display *display,
695 struct wl_surface *wl_surface,
696 uint32_t flags,
697 struct rectangle *rectangle)
698{
699 return NULL;
700}
701
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702#endif
703
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200704struct shm_surface_data {
705 struct wl_buffer *buffer;
706 struct shm_pool *pool;
707};
708
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400709struct wl_buffer *
710display_get_buffer_for_surface(struct display *display,
711 cairo_surface_t *surface)
712{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200713 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400714
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200715 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400716
717 return data->buffer;
718}
719
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500720static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700721shm_pool_destroy(struct shm_pool *pool);
722
723static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400724shm_surface_data_destroy(void *p)
725{
726 struct shm_surface_data *data = p;
727
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200728 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700729 if (data->pool)
730 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300731
732 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400733}
734
Kristian Høgsberg16626282012-04-03 11:21:27 -0400735static struct wl_shm_pool *
736make_shm_pool(struct display *display, int size, void **data)
737{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400738 struct wl_shm_pool *pool;
739 int fd;
740
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300741 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400742 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300743 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
744 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400745 return NULL;
746 }
747
748 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400749 if (*data == MAP_FAILED) {
750 fprintf(stderr, "mmap failed: %m\n");
751 close(fd);
752 return NULL;
753 }
754
755 pool = wl_shm_create_pool(display->shm, fd, size);
756
757 close(fd);
758
759 return pool;
760}
761
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700762static struct shm_pool *
763shm_pool_create(struct display *display, size_t size)
764{
765 struct shm_pool *pool = malloc(sizeof *pool);
766
767 if (!pool)
768 return NULL;
769
770 pool->pool = make_shm_pool(display, size, &pool->data);
771 if (!pool->pool) {
772 free(pool);
773 return NULL;
774 }
775
776 pool->size = size;
777 pool->used = 0;
778
779 return pool;
780}
781
782static void *
783shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
784{
785 if (pool->used + size > pool->size)
786 return NULL;
787
788 *offset = pool->used;
789 pool->used += size;
790
791 return (char *) pool->data + *offset;
792}
793
794/* destroy the pool. this does not unmap the memory though */
795static void
796shm_pool_destroy(struct shm_pool *pool)
797{
798 munmap(pool->data, pool->size);
799 wl_shm_pool_destroy(pool->pool);
800 free(pool);
801}
802
803/* Start allocating from the beginning of the pool again */
804static void
805shm_pool_reset(struct shm_pool *pool)
806{
807 pool->used = 0;
808}
809
810static int
811data_length_for_shm_surface(struct rectangle *rect)
812{
813 int stride;
814
815 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
816 rect->width);
817 return stride * rect->height;
818}
819
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500820static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700821display_create_shm_surface_from_pool(struct display *display,
822 struct rectangle *rectangle,
823 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824{
825 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400826 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400827 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200828 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700829 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400830 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831
832 data = malloc(sizeof *data);
833 if (data == NULL)
834 return NULL;
835
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200836 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
837 cairo_format = CAIRO_FORMAT_RGB16_565;
838 else
839 cairo_format = CAIRO_FORMAT_ARGB32;
840
841 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842 length = stride * rectangle->height;
843 data->pool = NULL;
844 map = shm_pool_allocate(pool, length, &offset);
845
846 if (!map) {
847 free(data);
848 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400849 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400851 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200852 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400853 rectangle->width,
854 rectangle->height,
855 stride);
856
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200857 cairo_surface_set_user_data(surface, &shm_surface_data_key,
858 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400859
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200860 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
861 format = WL_SHM_FORMAT_RGB565;
862 else {
863 if (flags & SURFACE_OPAQUE)
864 format = WL_SHM_FORMAT_XRGB8888;
865 else
866 format = WL_SHM_FORMAT_ARGB8888;
867 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400868
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200869 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
870 rectangle->width,
871 rectangle->height,
872 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400873
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700874 return surface;
875}
876
877static cairo_surface_t *
878display_create_shm_surface(struct display *display,
879 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200880 struct shm_pool *alternate_pool,
881 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700882{
883 struct shm_surface_data *data;
884 struct shm_pool *pool;
885 cairo_surface_t *surface;
886
Pekka Paalanen99436862012-11-19 17:15:59 +0200887 if (alternate_pool) {
888 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700889 surface = display_create_shm_surface_from_pool(display,
890 rectangle,
891 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200892 alternate_pool);
893 if (surface) {
894 data = cairo_surface_get_user_data(surface,
895 &shm_surface_data_key);
896 goto out;
897 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700898 }
899
900 pool = shm_pool_create(display,
901 data_length_for_shm_surface(rectangle));
902 if (!pool)
903 return NULL;
904
905 surface =
906 display_create_shm_surface_from_pool(display, rectangle,
907 flags, pool);
908
909 if (!surface) {
910 shm_pool_destroy(pool);
911 return NULL;
912 }
913
914 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200915 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700916 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400917
Pekka Paalanen99436862012-11-19 17:15:59 +0200918out:
919 if (data_ret)
920 *data_ret = data;
921
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400922 return surface;
923}
924
nobled7b87cb02011-02-01 18:51:47 +0000925static int
926check_size(struct rectangle *rect)
927{
928 if (rect->width && rect->height)
929 return 0;
930
931 fprintf(stderr, "tried to create surface of "
932 "width: %d, height: %d\n", rect->width, rect->height);
933 return -1;
934}
935
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936cairo_surface_t *
937display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100938 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400939 struct rectangle *rectangle,
940 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400941{
nobled7b87cb02011-02-01 18:51:47 +0000942 if (check_size(rectangle) < 0)
943 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200944
945 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200946 return display_create_shm_surface(display, rectangle, flags,
947 NULL, NULL);
948}
949
Pekka Paalanena4eda732012-11-19 17:16:02 +0200950struct shm_surface_leaf {
951 cairo_surface_t *cairo_surface;
952 /* 'data' is automatically destroyed, when 'cairo_surface' is */
953 struct shm_surface_data *data;
954
955 struct shm_pool *resize_pool;
956 int busy;
957};
958
959static void
960shm_surface_leaf_release(struct shm_surface_leaf *leaf)
961{
962 if (leaf->cairo_surface)
963 cairo_surface_destroy(leaf->cairo_surface);
964 /* leaf->data already destroyed via cairo private */
965
966 if (leaf->resize_pool)
967 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200968
969 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200970}
971
Pekka Paalanenaef02542013-04-25 13:57:47 +0300972#define MAX_LEAVES 3
973
Pekka Paalanen99436862012-11-19 17:15:59 +0200974struct shm_surface {
975 struct toysurface base;
976 struct display *display;
977 struct wl_surface *surface;
978 uint32_t flags;
979 int dx, dy;
980
Pekka Paalanenaef02542013-04-25 13:57:47 +0300981 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200982 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200983};
984
985static struct shm_surface *
986to_shm_surface(struct toysurface *base)
987{
988 return container_of(base, struct shm_surface, base);
989}
990
Pekka Paalanena4eda732012-11-19 17:16:02 +0200991static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300992shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
993{
994#ifdef DEBUG
995 struct shm_surface_leaf *leaf;
996 char bufs[MAX_LEAVES + 1];
997 int i;
998
999 for (i = 0; i < MAX_LEAVES; i++) {
1000 leaf = &surface->leaf[i];
1001
1002 if (leaf->busy)
1003 bufs[i] = 'b';
1004 else if (leaf->cairo_surface)
1005 bufs[i] = 'a';
1006 else
1007 bufs[i] = ' ';
1008 }
1009
1010 bufs[MAX_LEAVES] = '\0';
1011 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1012#endif
1013}
1014
1015static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001016shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1017{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001018 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001019 struct shm_surface_leaf *leaf;
1020 int i;
1021 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001022
1023 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001024
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 for (i = 0; i < MAX_LEAVES; i++) {
1026 leaf = &surface->leaf[i];
1027 if (leaf->data && leaf->data->buffer == buffer) {
1028 leaf->busy = 0;
1029 break;
1030 }
1031 }
1032 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001033
Pekka Paalanenaef02542013-04-25 13:57:47 +03001034 /* Leave one free leaf with storage, release others */
1035 free_found = 0;
1036 for (i = 0; i < MAX_LEAVES; i++) {
1037 leaf = &surface->leaf[i];
1038
1039 if (!leaf->cairo_surface || leaf->busy)
1040 continue;
1041
1042 if (!free_found)
1043 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001044 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001045 shm_surface_leaf_release(leaf);
1046 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001047
Pekka Paalanen97777442013-05-22 10:20:05 +03001048 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049}
1050
1051static const struct wl_buffer_listener shm_surface_buffer_listener = {
1052 shm_surface_buffer_release
1053};
1054
Pekka Paalanen99436862012-11-19 17:15:59 +02001055static cairo_surface_t *
1056shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001057 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001058 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001059{
Pekka Paalanenec076692012-11-30 13:37:27 +02001060 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001061 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001062 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001063 struct shm_surface_leaf *leaf = NULL;
1064 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001065
1066 surface->dx = dx;
1067 surface->dy = dy;
1068
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001069 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001070 for (i = 0; i < MAX_LEAVES; i++) {
1071 if (surface->leaf[i].busy)
1072 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001073
Pekka Paalanenaef02542013-04-25 13:57:47 +03001074 if (!leaf || surface->leaf[i].cairo_surface)
1075 leaf = &surface->leaf[i];
1076 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001077 DBG_OBJ(surface->surface, "pick leaf %d\n",
1078 (int)(leaf - &surface->leaf[0]));
1079
Pekka Paalanenaef02542013-04-25 13:57:47 +03001080 if (!leaf) {
1081 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001082 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001083 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001085 }
1086
Pekka Paalanena4eda732012-11-19 17:16:02 +02001087 if (!resize_hint && leaf->resize_pool) {
1088 cairo_surface_destroy(leaf->cairo_surface);
1089 leaf->cairo_surface = NULL;
1090 shm_pool_destroy(leaf->resize_pool);
1091 leaf->resize_pool = NULL;
1092 }
1093
Alexander Larsson1818e312013-05-22 14:41:31 +02001094 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1095
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 if (leaf->cairo_surface &&
1097 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1098 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001099 goto out;
1100
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 if (leaf->cairo_surface)
1102 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001104#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001106 /* Create a big pool to allocate from, while continuously
1107 * resizing. Mmapping a new pool in the server
1108 * is relatively expensive, so reusing a pool performs
1109 * better, but may temporarily reserve unneeded memory.
1110 */
1111 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001112 leaf->resize_pool = shm_pool_create(surface->display,
1113 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001114 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001115#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001116
Alexander Larsson1818e312013-05-22 14:41:31 +02001117 rect.width = width;
1118 rect.height = height;
1119
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001121 display_create_shm_surface(surface->display, &rect,
1122 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001123 leaf->resize_pool,
1124 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001125 if (!leaf->cairo_surface)
1126 return NULL;
1127
Pekka Paalanena4eda732012-11-19 17:16:02 +02001128 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001129 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001130
1131out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001132 surface->current = leaf;
1133
1134 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001135}
1136
1137static void
1138shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001139 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001140 struct rectangle *server_allocation)
1141{
1142 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001143 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001144
1145 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001146 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001147 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001148 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001149
Alexander Larsson1818e312013-05-22 14:41:31 +02001150 buffer_to_surface_size (buffer_transform, buffer_scale,
1151 &server_allocation->width,
1152 &server_allocation->height);
1153
Pekka Paalanena4eda732012-11-19 17:16:02 +02001154 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001155 surface->dx, surface->dy);
1156 wl_surface_damage(surface->surface, 0, 0,
1157 server_allocation->width, server_allocation->height);
1158 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001159
Pekka Paalanen71233882013-04-25 13:57:53 +03001160 DBG_OBJ(surface->surface, "leaf %d busy\n",
1161 (int)(leaf - &surface->leaf[0]));
1162
Pekka Paalanena4eda732012-11-19 17:16:02 +02001163 leaf->busy = 1;
1164 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001165}
1166
1167static int
1168shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1169{
1170 return -1;
1171}
1172
1173static void
1174shm_surface_release(struct toysurface *base)
1175{
1176}
1177
1178static void
1179shm_surface_destroy(struct toysurface *base)
1180{
1181 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001182 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001183
Pekka Paalanenaef02542013-04-25 13:57:47 +03001184 for (i = 0; i < MAX_LEAVES; i++)
1185 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001186
1187 free(surface);
1188}
1189
1190static struct toysurface *
1191shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1192 uint32_t flags, struct rectangle *rectangle)
1193{
1194 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001195 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001196
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001197 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001198 surface->base.prepare = shm_surface_prepare;
1199 surface->base.swap = shm_surface_swap;
1200 surface->base.acquire = shm_surface_acquire;
1201 surface->base.release = shm_surface_release;
1202 surface->base.destroy = shm_surface_destroy;
1203
1204 surface->display = display;
1205 surface->surface = wl_surface;
1206 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001207
1208 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001209}
1210
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001211/*
1212 * The following correspondences between file names and cursors was copied
1213 * from: https://bugs.kde.org/attachment.cgi?id=67313
1214 */
1215
1216static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001217 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001218 "sw-resize",
1219 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001220};
1221
1222static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001223 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001224 "se-resize",
1225 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001226};
1227
1228static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001229 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001230 "s-resize",
1231 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001232};
1233
1234static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001235 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236 "closedhand",
1237 "208530c400c041818281048008011002"
1238};
1239
1240static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001241 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 "default",
1243 "top_left_arrow",
1244 "left-arrow"
1245};
1246
1247static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001248 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001249 "w-resize",
1250 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001251};
1252
1253static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001254 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001255 "e-resize",
1256 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257};
1258
1259static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001260 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001261 "nw-resize",
1262 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001263};
1264
1265static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001266 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001267 "ne-resize",
1268 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001269};
1270
1271static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001272 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001273 "n-resize",
1274 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001275};
1276
1277static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001278 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001279 "ibeam",
1280 "text"
1281};
1282
1283static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001284 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001285 "pointer",
1286 "pointing_hand",
1287 "e29285e634086352946a0e7090d73106"
1288};
1289
1290static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001291 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001292 "wait",
1293 "0426c94ea35c87780ff01dc239897213"
1294};
1295
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001296static const char *move_draggings[] = {
1297 "dnd-move"
1298};
1299
1300static const char *copy_draggings[] = {
1301 "dnd-copy"
1302};
1303
1304static const char *forbidden_draggings[] = {
1305 "dnd-none",
1306 "dnd-no-drop"
1307};
1308
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001309struct cursor_alternatives {
1310 const char **names;
1311 size_t count;
1312};
1313
1314static const struct cursor_alternatives cursors[] = {
1315 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1316 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1317 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1318 {grabbings, ARRAY_LENGTH(grabbings)},
1319 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1320 {left_sides, ARRAY_LENGTH(left_sides)},
1321 {right_sides, ARRAY_LENGTH(right_sides)},
1322 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1323 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1324 {top_sides, ARRAY_LENGTH(top_sides)},
1325 {xterms, ARRAY_LENGTH(xterms)},
1326 {hand1s, ARRAY_LENGTH(hand1s)},
1327 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001328 {move_draggings, ARRAY_LENGTH(move_draggings)},
1329 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1330 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001331};
1332
1333static void
1334create_cursors(struct display *display)
1335{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001336 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001337 struct weston_config *config;
1338 struct weston_config_section *s;
emersionac71ee52018-11-18 21:42:10 +01001339 int size = DEFAULT_XCURSOR_SIZE;
1340 char *theme = NULL, *size_str;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001341 unsigned int i, j;
1342 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001343
emersion807cd2e2018-10-10 09:46:12 +02001344 theme = getenv("XCURSOR_THEME");
emersionac71ee52018-11-18 21:42:10 +01001345
1346 size_str = getenv("XCURSOR_SIZE");
1347 if (size_str) {
1348 safe_strtoint(size_str, &size);
1349 if (size <= 0)
1350 size = DEFAULT_XCURSOR_SIZE;
1351 }
emersion807cd2e2018-10-10 09:46:12 +02001352
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001353 config_file = weston_config_get_name_from_env();
1354 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001355 s = weston_config_get_section(config, "shell", NULL, NULL);
emersion807cd2e2018-10-10 09:46:12 +02001356 weston_config_section_get_string(s, "cursor-theme", &theme, theme);
1357 weston_config_section_get_int(s, "cursor-size", &size, size);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001358 weston_config_destroy(config);
1359
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001360 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001361 if (!display->cursor_theme) {
1362 fprintf(stderr, "could not load theme '%s'\n", theme);
1363 return;
1364 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001365 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001366 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001367 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001368
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001369 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001370 cursor = NULL;
1371 for (j = 0; !cursor && j < cursors[i].count; ++j)
1372 cursor = wl_cursor_theme_get_cursor(
1373 display->cursor_theme, cursors[i].names[j]);
1374
1375 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001376 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001377 cursors[i].names[0]);
1378
1379 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001380 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001381}
1382
1383static void
1384destroy_cursors(struct display *display)
1385{
1386 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001387 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001388}
1389
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001390struct wl_cursor_image *
1391display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001392{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001393 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001394
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001395 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001396}
1397
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001398static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001399surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001400{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001401 if (!surface->cairo_surface)
1402 return;
1403
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001404 if (surface->opaque_region) {
1405 wl_surface_set_opaque_region(surface->surface,
1406 surface->opaque_region);
1407 wl_region_destroy(surface->opaque_region);
1408 surface->opaque_region = NULL;
1409 }
1410
1411 if (surface->input_region) {
1412 wl_surface_set_input_region(surface->surface,
1413 surface->input_region);
1414 wl_region_destroy(surface->input_region);
1415 surface->input_region = NULL;
1416 }
1417
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001419 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001420 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001421
Pekka Paalanen89dee002013-02-13 16:17:20 +02001422 cairo_surface_destroy(surface->cairo_surface);
1423 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001424}
1425
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001426int
1427window_has_focus(struct window *window)
1428{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001429 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001430}
1431
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001432static void
1433window_close(struct window *window)
1434{
1435 if (window->close_handler)
1436 window->close_handler(window->user_data);
1437 else
1438 display_exit(window->display);
1439}
1440
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001441struct display *
1442window_get_display(struct window *window)
1443{
1444 return window->display;
1445}
1446
Pekka Paalanen89dee002013-02-13 16:17:20 +02001447static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001448surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001449{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001450 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001451 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001452
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001453 if (!surface->toysurface && display->dpy &&
1454 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001455 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001457 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001458 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001459 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001460 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001461
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001462 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001463 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001464 surface->surface,
1465 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001466
Pekka Paalanen89dee002013-02-13 16:17:20 +02001467 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001468 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001469 allocation.width, allocation.height, flags,
1470 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001471}
1472
1473static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001474window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001475{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001476 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001477 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001478
Pekka Paalanenec076692012-11-30 13:37:27 +02001479 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001480 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001481
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001482 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1483 flags |= SURFACE_HINT_RGB565;
1484
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001485 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001486}
1487
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001488int
1489window_get_buffer_transform(struct window *window)
1490{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001491 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001492}
1493
1494void
1495window_set_buffer_transform(struct window *window,
1496 enum wl_output_transform transform)
1497{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001498 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001499 wl_surface_set_buffer_transform(window->main_surface->surface,
1500 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001501}
1502
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001503void
1504window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001505 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001506{
1507 window->main_surface->buffer_scale = scale;
1508 wl_surface_set_buffer_scale(window->main_surface->surface,
1509 scale);
1510}
1511
1512uint32_t
1513window_get_buffer_scale(struct window *window)
1514{
1515 return window->main_surface->buffer_scale;
1516}
1517
Alexander Larssond68f5232013-05-22 14:41:33 +02001518uint32_t
1519window_get_output_scale(struct window *window)
1520{
1521 struct window_output *window_output;
1522 struct window_output *window_output_tmp;
1523 int scale = 1;
1524
1525 wl_list_for_each_safe(window_output, window_output_tmp,
1526 &window->window_output_list, link) {
1527 if (window_output->output->scale > scale)
1528 scale = window_output->output->scale;
1529 }
1530
1531 return scale;
1532}
1533
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001534static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001535
Pekka Paalanen4e373742013-02-13 16:17:13 +02001536static void
1537surface_destroy(struct surface *surface)
1538{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001539 if (surface->frame_cb)
1540 wl_callback_destroy(surface->frame_cb);
1541
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001542 if (surface->input_region)
1543 wl_region_destroy(surface->input_region);
1544
1545 if (surface->opaque_region)
1546 wl_region_destroy(surface->opaque_region);
1547
Pekka Paalanen35e82632013-04-25 13:57:48 +03001548 if (surface->subsurface)
1549 wl_subsurface_destroy(surface->subsurface);
1550
Pekka Paalanen4e373742013-02-13 16:17:13 +02001551 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001552
1553 if (surface->toysurface)
1554 surface->toysurface->destroy(surface->toysurface);
1555
Pekka Paalanen35e82632013-04-25 13:57:48 +03001556 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001557 free(surface);
1558}
1559
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001560void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001561window_destroy(struct window *window)
1562{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001563 struct display *display = window->display;
1564 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001565 struct window_output *window_output;
1566 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001567
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001568 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001569
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001570 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001571 if (input->touch_focus == window)
1572 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001573 if (input->pointer_focus == window)
1574 input->pointer_focus = NULL;
1575 if (input->keyboard_focus == window)
1576 input->keyboard_focus = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08001577 if (input->locked_window == window)
1578 input->locked_window = NULL;
1579 if (input->confined_window == window)
1580 input->confined_window = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001581 if (input->focus_widget &&
1582 input->focus_widget->window == window)
1583 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001584 }
1585
Rob Bradford7507b572012-05-15 17:55:34 +01001586 wl_list_for_each_safe(window_output, window_output_tmp,
1587 &window->window_output_list, link) {
1588 free (window_output);
1589 }
1590
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001591 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001592 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001593
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001594 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01001595 xdg_toplevel_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001596 if (window->xdg_popup)
ant8mef99fac22018-11-28 22:46:37 +01001597 xdg_popup_destroy(window->xdg_popup);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001598 if (window->xdg_surface)
ant8mef99fac22018-11-28 22:46:37 +01001599 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001600
1601 surface_destroy(window->main_surface);
1602
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001603 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001604
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001605 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001606 free(window);
1607}
1608
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001609static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001610widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001611{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001613
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001614 wl_list_for_each(child, &widget->child_list, link) {
1615 target = widget_find_widget(child, x, y);
1616 if (target)
1617 return target;
1618 }
1619
1620 if (widget->allocation.x <= x &&
1621 x < widget->allocation.x + widget->allocation.width &&
1622 widget->allocation.y <= y &&
1623 y < widget->allocation.y + widget->allocation.height) {
1624 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001625 }
1626
1627 return NULL;
1628}
1629
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001630static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001631window_find_widget(struct window *window, int32_t x, int32_t y)
1632{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001633 struct surface *surface;
1634 struct widget *widget;
1635
1636 wl_list_for_each(surface, &window->subsurface_list, link) {
1637 widget = widget_find_widget(surface->widget, x, y);
1638 if (widget)
1639 return widget;
1640 }
1641
1642 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001643}
1644
1645static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001646widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001648 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001649
Peter Huttererf3d62272013-08-08 11:57:05 +10001650 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001651 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001652 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001653 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001654 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001655 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001656 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001657 widget->tooltip = NULL;
1658 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001659 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001660 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001661
1662 return widget;
1663}
1664
1665struct widget *
1666window_add_widget(struct window *window, void *data)
1667{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001668 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001669
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001670 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001671 wl_list_init(&widget->link);
1672 window->main_surface->widget = widget;
1673
1674 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001675}
1676
1677struct widget *
1678widget_add_widget(struct widget *parent, void *data)
1679{
1680 struct widget *widget;
1681
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001682 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001683 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001684
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001685 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686}
1687
1688void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001689widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001691 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001692 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001693 struct input *input;
1694
Pekka Paalanen35e82632013-04-25 13:57:48 +03001695 /* Destroy the sub-surface along with the root widget */
1696 if (surface->widget == widget && surface->subsurface)
1697 surface_destroy(widget->surface);
1698
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001699 if (widget->tooltip)
1700 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001701
Pekka Paalanene156fb62012-01-19 13:51:38 +02001702 wl_list_for_each(input, &display->input_list, link) {
1703 if (input->focus_widget == widget)
1704 input->focus_widget = NULL;
1705 }
1706
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001707 wl_list_remove(&widget->link);
1708 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001709}
1710
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001711void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001712widget_set_default_cursor(struct widget *widget, int cursor)
1713{
1714 widget->default_cursor = cursor;
1715}
1716
1717void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001718widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001719{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001720 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001721}
1722
1723void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001724widget_set_size(struct widget *widget, int32_t width, int32_t height)
1725{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001726 widget->allocation.width = width;
1727 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001728}
1729
1730void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001731widget_set_allocation(struct widget *widget,
1732 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001733{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001734 widget->allocation.x = x;
1735 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001736 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001737}
1738
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001739void
1740widget_set_transparent(struct widget *widget, int transparent)
1741{
1742 widget->opaque = !transparent;
1743}
1744
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001745void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001746widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001748 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001749}
1750
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001751static cairo_surface_t *
1752widget_get_cairo_surface(struct widget *widget)
1753{
1754 struct surface *surface = widget->surface;
1755 struct window *window = widget->window;
1756
Neil Roberts97b747c2013-12-19 16:17:12 +00001757 assert(widget->use_cairo);
1758
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001759 if (!surface->cairo_surface) {
1760 if (surface == window->main_surface)
1761 window_create_main_surface(window);
1762 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001763 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001764 }
1765
1766 return surface->cairo_surface;
1767}
1768
Alexander Larsson15901f02013-05-22 14:41:25 +02001769static void
1770widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1771{
1772 struct surface *surface = widget->surface;
1773 double angle;
1774 cairo_matrix_t m;
1775 enum wl_output_transform transform;
1776 int surface_width, surface_height;
1777 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001778 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001779
1780 surface_width = surface->allocation.width;
1781 surface_height = surface->allocation.height;
1782
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001783 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001784 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001785
Alexander Larsson15901f02013-05-22 14:41:25 +02001786 switch (transform) {
1787 case WL_OUTPUT_TRANSFORM_FLIPPED:
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1789 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1790 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1791 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1792 break;
1793 default:
1794 cairo_matrix_init_identity(&m);
1795 break;
1796 }
1797
1798 switch (transform) {
1799 case WL_OUTPUT_TRANSFORM_NORMAL:
1800 default:
1801 angle = 0;
1802 translate_x = 0;
1803 translate_y = 0;
1804 break;
1805 case WL_OUTPUT_TRANSFORM_FLIPPED:
1806 angle = 0;
1807 translate_x = surface_width;
1808 translate_y = 0;
1809 break;
1810 case WL_OUTPUT_TRANSFORM_90:
1811 angle = M_PI_2;
1812 translate_x = surface_height;
1813 translate_y = 0;
1814 break;
1815 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1816 angle = M_PI_2;
1817 translate_x = surface_height;
1818 translate_y = surface_width;
1819 break;
1820 case WL_OUTPUT_TRANSFORM_180:
1821 angle = M_PI;
1822 translate_x = surface_width;
1823 translate_y = surface_height;
1824 break;
1825 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1826 angle = M_PI;
1827 translate_x = 0;
1828 translate_y = surface_height;
1829 break;
1830 case WL_OUTPUT_TRANSFORM_270:
1831 angle = M_PI + M_PI_2;
1832 translate_x = 0;
1833 translate_y = surface_width;
1834 break;
1835 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1836 angle = M_PI + M_PI_2;
1837 translate_x = 0;
1838 translate_y = 0;
1839 break;
1840 }
1841
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001842 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001843 cairo_translate(cr, translate_x, translate_y);
1844 cairo_rotate(cr, angle);
1845 cairo_transform(cr, &m);
1846}
1847
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001848cairo_t *
1849widget_cairo_create(struct widget *widget)
1850{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001851 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001852 cairo_surface_t *cairo_surface;
1853 cairo_t *cr;
1854
1855 cairo_surface = widget_get_cairo_surface(widget);
1856 cr = cairo_create(cairo_surface);
1857
Alexander Larsson15901f02013-05-22 14:41:25 +02001858 widget_cairo_update_transform(widget, cr);
1859
Pekka Paalanen35e82632013-04-25 13:57:48 +03001860 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1861
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001862 return cr;
1863}
1864
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001865struct wl_surface *
1866widget_get_wl_surface(struct widget *widget)
1867{
1868 return widget->surface->surface;
1869}
1870
Neil Roberts5e10a042013-09-09 00:40:17 +01001871struct wl_subsurface *
1872widget_get_wl_subsurface(struct widget *widget)
1873{
1874 return widget->surface->subsurface;
1875}
1876
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001877uint32_t
1878widget_get_last_time(struct widget *widget)
1879{
1880 return widget->surface->last_time;
1881}
1882
1883void
1884widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1885{
1886 struct wl_compositor *comp = widget->window->display->compositor;
1887 struct surface *surface = widget->surface;
1888
1889 if (!surface->input_region)
1890 surface->input_region = wl_compositor_create_region(comp);
1891
1892 if (rect) {
1893 wl_region_add(surface->input_region,
1894 rect->x, rect->y, rect->width, rect->height);
1895 }
1896}
1897
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001898void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001899widget_set_resize_handler(struct widget *widget,
1900 widget_resize_handler_t handler)
1901{
1902 widget->resize_handler = handler;
1903}
1904
1905void
1906widget_set_redraw_handler(struct widget *widget,
1907 widget_redraw_handler_t handler)
1908{
1909 widget->redraw_handler = handler;
1910}
1911
1912void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001913widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001914{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001915 widget->enter_handler = handler;
1916}
1917
1918void
1919widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1920{
1921 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001922}
1923
1924void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001925widget_set_motion_handler(struct widget *widget,
1926 widget_motion_handler_t handler)
1927{
1928 widget->motion_handler = handler;
1929}
1930
1931void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001932widget_set_button_handler(struct widget *widget,
1933 widget_button_handler_t handler)
1934{
1935 widget->button_handler = handler;
1936}
1937
1938void
Rusty Lynch041815a2013-08-08 21:20:38 -07001939widget_set_touch_up_handler(struct widget *widget,
1940 widget_touch_up_handler_t handler)
1941{
1942 widget->touch_up_handler = handler;
1943}
1944
1945void
1946widget_set_touch_down_handler(struct widget *widget,
1947 widget_touch_down_handler_t handler)
1948{
1949 widget->touch_down_handler = handler;
1950}
1951
1952void
1953widget_set_touch_motion_handler(struct widget *widget,
1954 widget_touch_motion_handler_t handler)
1955{
1956 widget->touch_motion_handler = handler;
1957}
1958
1959void
1960widget_set_touch_frame_handler(struct widget *widget,
1961 widget_touch_frame_handler_t handler)
1962{
1963 widget->touch_frame_handler = handler;
1964}
1965
1966void
1967widget_set_touch_cancel_handler(struct widget *widget,
1968 widget_touch_cancel_handler_t handler)
1969{
1970 widget->touch_cancel_handler = handler;
1971}
1972
1973void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001974widget_set_axis_handler(struct widget *widget,
1975 widget_axis_handler_t handler)
1976{
1977 widget->axis_handler = handler;
1978}
1979
Peter Hutterer87743e92016-01-18 16:38:22 +10001980void
1981widget_set_pointer_frame_handler(struct widget *widget,
1982 widget_pointer_frame_handler_t handler)
1983{
1984 widget->pointer_frame_handler = handler;
1985}
1986
1987void
1988widget_set_axis_handlers(struct widget *widget,
1989 widget_axis_handler_t axis_handler,
1990 widget_axis_source_handler_t axis_source_handler,
1991 widget_axis_stop_handler_t axis_stop_handler,
1992 widget_axis_discrete_handler_t axis_discrete_handler)
1993{
1994 widget->axis_handler = axis_handler;
1995 widget->axis_source_handler = axis_source_handler;
1996 widget->axis_stop_handler = axis_stop_handler;
1997 widget->axis_discrete_handler = axis_discrete_handler;
1998}
1999
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002000static void
2001window_schedule_redraw_task(struct window *window);
2002
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002003void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002004widget_schedule_redraw(struct widget *widget)
2005{
Pekka Paalanen71233882013-04-25 13:57:53 +03002006 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002007 widget->surface->redraw_needed = 1;
2008 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002009}
2010
Neil Roberts97b747c2013-12-19 16:17:12 +00002011void
2012widget_set_use_cairo(struct widget *widget,
2013 int use_cairo)
2014{
2015 widget->use_cairo = use_cairo;
2016}
2017
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002018cairo_surface_t *
2019window_get_surface(struct window *window)
2020{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002021 cairo_surface_t *cairo_surface;
2022
2023 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2024
2025 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002026}
2027
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002028struct wl_surface *
2029window_get_wl_surface(struct window *window)
2030{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002031 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002032}
2033
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002034static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002035tooltip_redraw_handler(struct widget *widget, void *data)
2036{
2037 cairo_t *cr;
2038 const int32_t r = 3;
2039 struct tooltip *tooltip = data;
2040 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002041
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002042 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002043 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002044 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2045 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2046 cairo_paint(cr);
2047
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002048 width = widget->allocation.width;
2049 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002050 rounded_rect(cr, 0, 0, width, height, r);
2051
2052 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2053 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2054 cairo_fill(cr);
2055
2056 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2057 cairo_move_to(cr, 10, 16);
2058 cairo_show_text(cr, tooltip->entry);
2059 cairo_destroy(cr);
2060}
2061
2062static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002063get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002064{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002065 cairo_t *cr;
2066 cairo_text_extents_t extents;
2067
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002068 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002069 * created yet, and parent does not have a valid surface
2070 * outside repaint, either.
2071 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002072 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002073 cairo_text_extents(cr, tooltip->entry, &extents);
2074 cairo_destroy(cr);
2075
2076 return extents;
2077}
2078
2079static int
2080window_create_tooltip(struct tooltip *tooltip)
2081{
2082 struct widget *parent = tooltip->parent;
2083 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002084 const int offset_y = 27;
2085 const int margin = 3;
2086 cairo_text_extents_t extents;
2087
2088 if (tooltip->widget)
2089 return 0;
2090
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002091 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002092
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002093 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002094 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002095 widget_set_allocation(tooltip->widget,
2096 tooltip->x, tooltip->y + offset_y,
2097 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002098
2099 return 0;
2100}
2101
2102void
2103widget_destroy_tooltip(struct widget *parent)
2104{
2105 struct tooltip *tooltip = parent->tooltip;
2106
2107 parent->tooltip_count = 0;
2108 if (!tooltip)
2109 return;
2110
2111 if (tooltip->widget) {
2112 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002113 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002114 }
2115
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002116 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002117 free(tooltip->entry);
2118 free(tooltip);
2119 parent->tooltip = NULL;
2120}
2121
2122static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002123tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002124{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002125 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002126
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002127 window_create_tooltip(tooltip);
2128}
2129
2130#define TOOLTIP_TIMEOUT 500
2131static int
2132tooltip_timer_reset(struct tooltip *tooltip)
2133{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002134 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002135
2136 return 0;
2137}
2138
2139int
2140widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2141{
2142 struct tooltip *tooltip = parent->tooltip;
2143
2144 parent->tooltip_count++;
2145 if (tooltip) {
2146 tooltip->x = x;
2147 tooltip->y = y;
2148 tooltip_timer_reset(tooltip);
2149 return 0;
2150 }
2151
2152 /* the handler might be triggered too fast via input device motion, so
2153 * we need this check here to make sure tooltip is fully initialized */
2154 if (parent->tooltip_count > 1)
2155 return 0;
2156
2157 tooltip = malloc(sizeof *tooltip);
2158 if (!tooltip)
2159 return -1;
2160
2161 parent->tooltip = tooltip;
2162 tooltip->parent = parent;
2163 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002164 tooltip->x = x;
2165 tooltip->y = y;
2166 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002167 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2168 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002169 tooltip_timer_reset(tooltip);
2170
2171 return 0;
2172}
2173
2174static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002175frame_resize_handler(struct widget *widget,
2176 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002177{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002178 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002179 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002180 struct rectangle interior;
2181 struct rectangle input;
2182 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002183
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002184 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002185 interior.x = 0;
2186 interior.y = 0;
2187 interior.width = width;
2188 interior.height = height;
2189 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 frame_resize(frame->frame, width, height);
2191 frame_interior(frame->frame, &interior.x, &interior.y,
2192 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002193 }
2194
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002195 widget_set_allocation(child, interior.x, interior.y,
2196 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 if (child->resize_handler) {
2199 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200 child->user_data);
2201
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002202 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 width = child->allocation.width;
2204 height = child->allocation.height;
2205 } else {
2206 frame_resize_inside(frame->frame,
2207 child->allocation.width,
2208 child->allocation.height);
2209 width = frame_width(frame->frame);
2210 height = frame_height(frame->frame);
2211 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002212 }
2213
Scott Moreauf7e498c2012-05-14 11:39:29 -06002214 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002215
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002216 widget->surface->input_region =
2217 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002218 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002219 frame_input_rect(frame->frame, &input.x, &input.y,
2220 &input.width, &input.height);
2221 wl_region_add(widget->surface->input_region,
2222 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002223 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002224 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002225 }
2226
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002227 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002228
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002229 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002230 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002231 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2232 &opaque.width, &opaque.height);
2233
2234 wl_region_add(widget->surface->opaque_region,
2235 opaque.x, opaque.y,
2236 opaque.width, opaque.height);
2237 } else {
2238 wl_region_add(widget->surface->opaque_region,
2239 0, 0, width, height);
2240 }
Martin Minarik1998b152012-05-10 02:04:35 +02002241 }
2242
Martin Minarik1998b152012-05-10 02:04:35 +02002243
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002244 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002245}
2246
2247static void
2248frame_redraw_handler(struct widget *widget, void *data)
2249{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002250 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002251 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002252 struct window *window = widget->window;
2253
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002254 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002255 return;
2256
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002257 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002258
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002259 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002260
2261 cairo_destroy(cr);
2262}
2263
2264static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002265frame_get_pointer_image_for_location(struct window_frame *frame,
2266 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002267{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002268 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002269
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002270 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002271 return CURSOR_LEFT_PTR;
2272
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002273 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002274 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002275 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002276 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002277 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002278 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002279 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002280 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002281 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002282 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002283 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002284 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002285 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002286 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002287 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002288 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002289 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002290 case THEME_LOCATION_EXTERIOR:
2291 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002292 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002293 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002294 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002295}
2296
Pekka Paalanen26237862014-09-10 15:10:30 +03002297static void
2298frame_menu_func(void *data, struct input *input, int index)
2299{
2300 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002301
2302 switch (index) {
2303 case 0: /* close */
2304 window_close(window);
2305 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002306 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002307 /* we don't have a way to get out of fullscreen for now */
2308 if (window->fullscreen_handler)
2309 window->fullscreen_handler(window, window->user_data);
2310 break;
2311 }
2312}
2313
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002314void
2315window_show_frame_menu(struct window *window,
2316 struct input *input, uint32_t time)
2317{
2318 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002319 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002320
Pekka Paalanen26237862014-09-10 15:10:30 +03002321 static const char *entries[] = {
2322 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002323 "Fullscreen"
2324 };
2325
2326 if (window->fullscreen_handler)
2327 count = ARRAY_LENGTH(entries);
2328 else
2329 count = ARRAY_LENGTH(entries) - 1;
2330
2331 input_get_position(input, &x, &y);
2332 window_show_menu(window->display, input, time, window,
2333 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002334}
2335
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002336static int
2337frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002338 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002339{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002340 struct window_frame *frame = data;
2341 enum theme_location location;
2342
2343 location = frame_pointer_enter(frame->frame, input, x, y);
2344 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2345 widget_schedule_redraw(frame->widget);
2346
2347 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002348}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002349
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002350static int
2351frame_motion_handler(struct widget *widget,
2352 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002353 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_motion(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);
2363}
2364
2365static void
2366frame_leave_handler(struct widget *widget,
2367 struct input *input, void *data)
2368{
2369 struct window_frame *frame = data;
2370
2371 frame_pointer_leave(frame->frame, input);
2372 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2373 widget_schedule_redraw(frame->widget);
2374}
2375
2376static void
2377frame_handle_status(struct window_frame *frame, struct input *input,
2378 uint32_t time, enum theme_location location)
2379{
2380 struct window *window = frame->widget->window;
2381 uint32_t status;
2382
2383 status = frame_status(frame->frame);
2384 if (status & FRAME_STATUS_REPAINT)
2385 widget_schedule_redraw(frame->widget);
2386
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002387 if (status & FRAME_STATUS_MINIMIZE) {
2388 window_set_minimized(window);
2389 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2390 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002391
2392 if (status & FRAME_STATUS_MENU) {
2393 window_show_frame_menu(window, input, time);
2394 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2395 }
2396
2397 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002398 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002399 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2400 }
2401
2402 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002403 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002404 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002405 }
2406
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002407 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002408 input_ungrab(input);
ant8mef99fac22018-11-28 22:46:37 +01002409 xdg_toplevel_move(window->xdg_toplevel,
2410 input_get_seat(input),
2411 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002412
2413 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2414 }
2415
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002416 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002417 input_ungrab(input);
2418
ant8mef99fac22018-11-28 22:46:37 +01002419 xdg_toplevel_resize(window->xdg_toplevel,
2420 input_get_seat(input),
2421 window->display->serial,
2422 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002423
2424 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2425 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002427
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002428#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002429static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002430frame_button_handler(struct widget *widget,
2431 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002432 uint32_t button, enum wl_pointer_button_state state,
2433 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002434
2435{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002436 struct window_frame *frame = data;
2437 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002438
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002439 frame->double_click = 0;
2440 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2441 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2442 frame->double_click = 1;
2443 frame->did_double = 1;
2444 } else
2445 frame->did_double = 0;
2446
2447 frame->last_time = time;
2448 } else if (frame->did_double == 1) {
2449 frame->double_click = 1;
2450 frame->did_double = 0;
2451 }
2452
2453 if (frame->double_click)
2454 location = frame_double_click(frame->frame, input,
2455 button, state);
2456 else
2457 location = frame_pointer_button(frame->frame, input,
2458 button, state);
2459
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461}
2462
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002463static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002464frame_touch_down_handler(struct widget *widget, struct input *input,
2465 uint32_t serial, uint32_t time, int32_t id,
2466 float x, float y, void *data)
2467{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002468 struct window_frame *frame = data;
2469
Xiong Zhang382de462014-06-12 11:06:26 +08002470 frame->double_click = 0;
2471 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2472 frame->last_id == id) {
2473 frame->double_click = 1;
2474 frame->did_double = 1;
2475 frame->double_id = id;
2476 } else
2477 frame->did_double = 0;
2478
2479 frame->last_time = time;
2480 frame->last_id = id;
2481
2482 if (frame->double_click)
2483 frame_double_touch_down(frame->frame, input, id, x, y);
2484 else
2485 frame_touch_down(frame->frame, input, id, x, y);
2486
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002487 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2488}
2489
2490static void
2491frame_touch_up_handler(struct widget *widget,
2492 struct input *input, uint32_t serial, uint32_t time,
2493 int32_t id, void *data)
2494{
2495 struct window_frame *frame = data;
2496
Xiong Zhang382de462014-06-12 11:06:26 +08002497 if (frame->double_id == id && frame->did_double) {
2498 frame->did_double = 0;
2499 frame->double_id = 0;
2500 frame_double_touch_up(frame->frame, input, id);
2501 } else
2502 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002503 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002504}
2505
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002506struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002507window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002508{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002509 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002510 uint32_t buttons;
2511
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002512 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002513 buttons = FRAME_BUTTON_NONE;
2514 } else {
2515 buttons = FRAME_BUTTON_ALL;
2516 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002517
Peter Huttererf3d62272013-08-08 11:57:05 +10002518 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002519 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002520 buttons, window->title, NULL);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002521
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002522 frame->widget = window_add_widget(window, frame);
2523 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002524
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002525 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2526 widget_set_resize_handler(frame->widget, frame_resize_handler);
2527 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002528 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002529 widget_set_motion_handler(frame->widget, frame_motion_handler);
2530 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002531 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002532 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002533
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002534 window->frame = frame;
2535
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002536 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002537}
2538
Kristian Høgsberga1627922012-06-20 17:30:03 -04002539void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002540window_frame_set_child_size(struct widget *widget, int child_width,
2541 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002542{
2543 struct display *display = widget->window->display;
2544 struct theme *t = display->theme;
2545 int decoration_width, decoration_height;
2546 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002547 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002548
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002549 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002550 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002551 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002552 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002553
2554 width = child_width + decoration_width;
2555 height = child_height + decoration_height;
2556 } else {
2557 width = child_width;
2558 height = child_height;
2559 }
2560
2561 window_schedule_resize(widget->window, width, height);
2562}
2563
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002564static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002565window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002566{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002567 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002568
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002569 /* frame->child must be destroyed by the application */
2570 widget_destroy(frame->widget);
2571 free(frame);
2572}
2573
2574static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002575input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002576 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002577{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002578 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002579 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002580
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002581 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002582 return;
2583
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002584 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002585 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002586 widget = old;
2587 if (input->grab)
2588 widget = input->grab;
2589 if (widget->leave_handler)
2590 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002591 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002592 }
2593
2594 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002595 widget = focus;
2596 if (input->grab)
2597 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002598 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002599 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002600 cursor = widget->enter_handler(focus, input, x, y,
2601 widget->user_data);
2602 else
2603 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002604
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002605 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002606 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002607}
2608
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002609void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002610touch_grab(struct input *input, int32_t touch_id)
2611{
2612 input->touch_grab = 1;
2613 input->touch_grab_id = touch_id;
2614}
2615
2616void
2617touch_ungrab(struct input *input)
2618{
2619 struct touch_point *tp, *tmp;
2620
2621 input->touch_grab = 0;
2622
2623 wl_list_for_each_safe(tp, tmp,
2624 &input->touch_point_list, link) {
2625 if (tp->id != input->touch_grab_id)
2626 continue;
2627 wl_list_remove(&tp->link);
2628 free(tp);
2629
2630 return;
2631 }
2632}
2633
2634void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002635input_grab(struct input *input, struct widget *widget, uint32_t button)
2636{
2637 input->grab = widget;
2638 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002639
2640 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002641}
2642
2643void
2644input_ungrab(struct input *input)
2645{
2646 struct widget *widget;
2647
2648 input->grab = NULL;
2649 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002650 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002651 input->sx, input->sy);
2652 input_set_focus_widget(input, widget, input->sx, input->sy);
2653 }
2654}
2655
2656static void
Derek Foreman118a4292015-04-22 17:23:35 -05002657cursor_delay_timer_reset(struct input *input, uint32_t duration)
2658{
Derek Foreman118a4292015-04-22 17:23:35 -05002659 if (!duration)
2660 input->cursor_timer_running = false;
2661 else
2662 input->cursor_timer_running = true;
2663
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002664 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002665}
2666
2667static void cancel_pointer_image_update(struct input *input)
2668{
2669 if (input->cursor_timer_running)
2670 cursor_delay_timer_reset(input, 0);
2671}
2672
2673static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002674input_remove_pointer_focus(struct input *input)
2675{
2676 struct window *window = input->pointer_focus;
2677
2678 if (!window)
2679 return;
2680
2681 input_set_focus_widget(input, NULL, 0, 0);
2682
2683 input->pointer_focus = NULL;
2684 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002685 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002686}
2687
2688static void
2689pointer_handle_enter(void *data, struct wl_pointer *pointer,
2690 uint32_t serial, struct wl_surface *surface,
2691 wl_fixed_t sx_w, wl_fixed_t sy_w)
2692{
2693 struct input *input = data;
2694 struct window *window;
2695 struct widget *widget;
2696 float sx = wl_fixed_to_double(sx_w);
2697 float sy = wl_fixed_to_double(sy_w);
2698
2699 if (!surface) {
2700 /* enter event for a window we've just destroyed */
2701 return;
2702 }
2703
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002704 window = wl_surface_get_user_data(surface);
2705 if (surface != window->main_surface->surface) {
2706 DBG("Ignoring input event from subsurface %p\n", surface);
2707 return;
2708 }
2709
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002710 input->display->serial = serial;
2711 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002712 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002713
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002714 input->sx = sx;
2715 input->sy = sy;
2716
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002717 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002718 input_set_focus_widget(input, widget, sx, sy);
2719}
2720
2721static void
2722pointer_handle_leave(void *data, struct wl_pointer *pointer,
2723 uint32_t serial, struct wl_surface *surface)
2724{
2725 struct input *input = data;
2726
2727 input->display->serial = serial;
2728 input_remove_pointer_focus(input);
2729}
2730
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002731static void
Daniel Stone37816df2012-05-16 18:45:18 +01002732pointer_handle_motion(void *data, struct wl_pointer *pointer,
2733 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002734{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002735 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002736 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002737 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002738 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002739 float sx = wl_fixed_to_double(sx_w);
2740 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002741
Paul Winwoodb22bf572013-08-29 10:52:54 +01002742 if (!window)
2743 return;
2744
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002745 input->sx = sx;
2746 input->sy = sy;
2747
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002748 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002749 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002750 * based on the old surface dimensions. However, if we have an active
2751 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002752 */
Derek Foreman46812b62015-08-26 17:13:27 -05002753 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002754 sy < window->main_surface->allocation.y ||
2755 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002756 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002757 return;
2758
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002759 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002760 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002761 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002762 }
2763
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002764 if (input->grab)
2765 widget = input->grab;
2766 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002767 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002768 if (widget) {
2769 if (widget->motion_handler)
2770 cursor = widget->motion_handler(input->focus_widget,
2771 input, time, sx, sy,
2772 widget->user_data);
2773 else
2774 cursor = widget->default_cursor;
2775 } else
2776 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002777
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002778 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002779}
2780
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002781static void
Daniel Stone37816df2012-05-16 18:45:18 +01002782pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002783 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002784{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002785 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002786 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002787 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002788
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002789 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002790 if (input->focus_widget && input->grab == NULL &&
2791 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002792 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002793
Neil Roberts6b28aad2012-01-23 19:11:18 +00002794 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002795 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002796 (*widget->button_handler)(widget,
2797 input, time,
2798 button, state,
2799 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002800
Daniel Stone4dbadb12012-05-30 16:31:51 +01002801 if (input->grab && input->grab_button == button &&
2802 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002803 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002804}
2805
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002806static void
Daniel Stone37816df2012-05-16 18:45:18 +01002807pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002808 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002809{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002810 struct input *input = data;
2811 struct widget *widget;
2812
2813 widget = input->focus_widget;
2814 if (input->grab)
2815 widget = input->grab;
2816 if (widget && widget->axis_handler)
2817 (*widget->axis_handler)(widget,
2818 input, time,
2819 axis, value,
2820 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002821}
2822
Peter Hutterer87743e92016-01-18 16:38:22 +10002823static void
2824pointer_handle_frame(void *data, struct wl_pointer *pointer)
2825{
2826 struct input *input = data;
2827 struct widget *widget;
2828
2829 widget = input->focus_widget;
2830 if (input->grab)
2831 widget = input->grab;
2832 if (widget && widget->pointer_frame_handler)
2833 (*widget->pointer_frame_handler)(widget,
2834 input,
2835 widget->user_data);
2836}
2837
2838static void
2839pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2840 uint32_t source)
2841{
2842 struct input *input = data;
2843 struct widget *widget;
2844
2845 widget = input->focus_widget;
2846 if (input->grab)
2847 widget = input->grab;
2848 if (widget && widget->axis_source_handler)
2849 (*widget->axis_source_handler)(widget,
2850 input,
2851 source,
2852 widget->user_data);
2853}
2854
2855static void
2856pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2857 uint32_t time, uint32_t axis)
2858{
2859 struct input *input = data;
2860 struct widget *widget;
2861
2862 widget = input->focus_widget;
2863 if (input->grab)
2864 widget = input->grab;
2865 if (widget && widget->axis_stop_handler)
2866 (*widget->axis_stop_handler)(widget,
2867 input, time,
2868 axis,
2869 widget->user_data);
2870}
2871
2872static void
2873pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2874 uint32_t axis, int32_t discrete)
2875{
2876 struct input *input = data;
2877 struct widget *widget;
2878
2879 widget = input->focus_widget;
2880 if (input->grab)
2881 widget = input->grab;
2882 if (widget && widget->axis_discrete_handler)
2883 (*widget->axis_discrete_handler)(widget,
2884 input,
2885 axis,
2886 discrete,
2887 widget->user_data);
2888}
2889
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002890static const struct wl_pointer_listener pointer_listener = {
2891 pointer_handle_enter,
2892 pointer_handle_leave,
2893 pointer_handle_motion,
2894 pointer_handle_button,
2895 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002896 pointer_handle_frame,
2897 pointer_handle_axis_source,
2898 pointer_handle_axis_stop,
2899 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002900};
2901
2902static void
2903input_remove_keyboard_focus(struct input *input)
2904{
2905 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002906
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002907 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002908
2909 if (!window)
2910 return;
2911
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002912 if (window->keyboard_focus_handler)
2913 (*window->keyboard_focus_handler)(window, NULL,
2914 window->user_data);
2915
2916 input->keyboard_focus = NULL;
2917}
2918
2919static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002920keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002921{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002922 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002923 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002924
2925 if (window && window->key_handler) {
2926 (*window->key_handler)(window, input, input->repeat_time,
2927 input->repeat_key, input->repeat_sym,
2928 WL_KEYBOARD_KEY_STATE_PRESSED,
2929 window->user_data);
2930 }
2931}
2932
2933static void
2934keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2935 uint32_t format, int fd, uint32_t size)
2936{
2937 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002938 struct xkb_keymap *keymap;
2939 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002940#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002941 struct xkb_compose_table *compose_table;
2942 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002943#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002944 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002945 char *map_str;
2946
2947 if (!data) {
2948 close(fd);
2949 return;
2950 }
2951
2952 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2953 close(fd);
2954 return;
2955 }
2956
2957 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2958 if (map_str == MAP_FAILED) {
2959 close(fd);
2960 return;
2961 }
2962
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002963 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03002964 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2965 map_str,
2966 XKB_KEYMAP_FORMAT_TEXT_V1,
2967 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002968 munmap(map_str, size);
2969 close(fd);
2970
Rui Matos3eccb862013-10-10 19:44:22 +02002971 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002972 fprintf(stderr, "failed to compile keymap\n");
2973 return;
2974 }
2975
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002976 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02002977 state = xkb_state_new(keymap);
2978 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002979 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002980 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002981 return;
2982 }
2983
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002984 /* Look up the preferred locale, falling back to "C" as default */
2985 if (!(locale = getenv("LC_ALL")))
2986 if (!(locale = getenv("LC_CTYPE")))
2987 if (!(locale = getenv("LANG")))
2988 locale = "C";
2989
2990 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01002991#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002992 compose_table =
2993 xkb_compose_table_new_from_locale(input->display->xkb_context,
2994 locale,
2995 XKB_COMPOSE_COMPILE_NO_FLAGS);
2996 if (compose_table) {
2997 /* Set up XKB compose state */
2998 compose_state = xkb_compose_state_new(compose_table,
2999 XKB_COMPOSE_STATE_NO_FLAGS);
3000 if (compose_state) {
3001 xkb_compose_state_unref(input->xkb.compose_state);
3002 xkb_compose_table_unref(input->xkb.compose_table);
3003 input->xkb.compose_state = compose_state;
3004 input->xkb.compose_table = compose_table;
3005 } else {
3006 fprintf(stderr, "could not create XKB compose state. "
3007 "Disabiling compose.\n");
3008 xkb_compose_table_unref(compose_table);
3009 compose_table = NULL;
3010 }
3011 } else {
3012 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3013 "Disabiling compose\n", locale);
3014 }
Daniel Stone5b015962016-10-20 14:45:58 +01003015#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003016
Rui Matos3eccb862013-10-10 19:44:22 +02003017 xkb_keymap_unref(input->xkb.keymap);
3018 xkb_state_unref(input->xkb.state);
3019 input->xkb.keymap = keymap;
3020 input->xkb.state = state;
3021
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003022 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003023 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003024 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003025 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003026 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003027 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003028}
3029
3030static void
3031keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3032 uint32_t serial, struct wl_surface *surface,
3033 struct wl_array *keys)
3034{
3035 struct input *input = data;
3036 struct window *window;
3037
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003038 if (!surface) {
3039 /* enter event for a window we've just destroyed */
3040 return;
3041 }
3042
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003043 input->display->serial = serial;
3044 input->keyboard_focus = wl_surface_get_user_data(surface);
3045
3046 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003047 if (window->keyboard_focus_handler)
3048 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003049 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003050}
3051
3052static void
3053keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3054 uint32_t serial, struct wl_surface *surface)
3055{
3056 struct input *input = data;
3057
3058 input->display->serial = serial;
3059 input_remove_keyboard_focus(input);
3060}
3061
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003062/* Translate symbols appropriately if a compose sequence is being entered */
3063static xkb_keysym_t
3064process_key_press(xkb_keysym_t sym, struct input *input)
3065{
Daniel Stone5b015962016-10-20 14:45:58 +01003066#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003067 if (!input->xkb.compose_state)
3068 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003069 if (sym == XKB_KEY_NoSymbol)
3070 return sym;
3071 if (xkb_compose_state_feed(input->xkb.compose_state,
3072 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3073 return sym;
3074
3075 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3076 case XKB_COMPOSE_COMPOSING:
3077 return XKB_KEY_NoSymbol;
3078 case XKB_COMPOSE_COMPOSED:
3079 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3080 case XKB_COMPOSE_CANCELLED:
3081 return XKB_KEY_NoSymbol;
3082 case XKB_COMPOSE_NOTHING:
3083 return sym;
3084 default:
3085 return sym;
3086 }
Daniel Stone5b015962016-10-20 14:45:58 +01003087#else
3088 return sym;
3089#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003090}
3091
Scott Moreau210d0792012-03-22 10:47:01 -06003092static void
Daniel Stone37816df2012-05-16 18:45:18 +01003093keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003094 uint32_t serial, uint32_t time, uint32_t key,
3095 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003096{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003097 struct input *input = data;
3098 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003099 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003100 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003101 const xkb_keysym_t *syms;
3102 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003103 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003104
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003105 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003106 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003107 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003108 return;
3109
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003110 /* We only use input grabs for pointer events for now, so just
3111 * ignore key presses if a grab is active. We expand the key
3112 * event delivery mechanism to route events to widgets to
3113 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003114 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003115 if (input->grab && input->grab_button == 0)
3116 return;
3117
Ran Benita2e1968f2014-08-19 23:59:51 +03003118 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003119
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003120 sym = XKB_KEY_NoSymbol;
3121 if (num_syms == 1)
3122 sym = syms[0];
3123
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003124
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003125 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003126 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003127 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003128 } else if (sym == XKB_KEY_F11 &&
3129 window->fullscreen_handler &&
3130 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3131 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003132 } else if (sym == XKB_KEY_F4 &&
3133 input->modifiers == MOD_ALT_MASK &&
3134 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003135 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003136 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003137 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3138 sym = process_key_press(sym, input);
3139
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003140 (*window->key_handler)(window, input, time, key,
3141 sym, state, window->user_data);
3142 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003143
3144 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3145 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003146 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003147 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3148 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003149 input->repeat_sym = sym;
3150 input->repeat_key = key;
3151 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003152 its.it_interval.tv_sec = input->repeat_rate_sec;
3153 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3154 its.it_value.tv_sec = input->repeat_delay_sec;
3155 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003156 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003157 }
3158}
3159
3160static void
Daniel Stone351eb612012-05-31 15:27:47 -04003161keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3162 uint32_t serial, uint32_t mods_depressed,
3163 uint32_t mods_latched, uint32_t mods_locked,
3164 uint32_t group)
3165{
3166 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003167 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003168
Matt Ropere61561f2013-06-24 16:52:43 +01003169 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3170 if (!input->xkb.keymap)
3171 return;
3172
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003173 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3174 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003175 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003176 XKB_STATE_MODS_DEPRESSED |
3177 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003178 input->modifiers = 0;
3179 if (mask & input->xkb.control_mask)
3180 input->modifiers |= MOD_CONTROL_MASK;
3181 if (mask & input->xkb.alt_mask)
3182 input->modifiers |= MOD_ALT_MASK;
3183 if (mask & input->xkb.shift_mask)
3184 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003185}
3186
Jonny Lamb06959082014-08-12 14:58:27 +02003187static void
3188set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3189{
3190 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3191 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3192
3193 /* a rate of zero disables any repeating, regardless of the delay's
3194 * value */
3195 if (rate == 0)
3196 return;
3197
3198 if (rate == 1)
3199 input->repeat_rate_sec = 1;
3200 else
3201 input->repeat_rate_nsec = 1000000000 / rate;
3202
3203 input->repeat_delay_sec = delay / 1000;
3204 delay -= (input->repeat_delay_sec * 1000);
3205 input->repeat_delay_nsec = delay * 1000 * 1000;
3206}
3207
3208static void
3209keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3210 int32_t rate, int32_t delay)
3211{
3212 struct input *input = data;
3213
3214 set_repeat_info(input, rate, delay);
3215}
3216
Daniel Stone37816df2012-05-16 18:45:18 +01003217static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003218 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003219 keyboard_handle_enter,
3220 keyboard_handle_leave,
3221 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003222 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003223 keyboard_handle_repeat_info
3224
Daniel Stone37816df2012-05-16 18:45:18 +01003225};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003226
3227static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003228touch_handle_down(void *data, struct wl_touch *wl_touch,
3229 uint32_t serial, uint32_t time, struct wl_surface *surface,
3230 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3231{
3232 struct input *input = data;
3233 struct widget *widget;
3234 float sx = wl_fixed_to_double(x_w);
3235 float sy = wl_fixed_to_double(y_w);
3236
Rusty Lynch1084da52013-08-15 09:10:08 -07003237 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003238 input->touch_focus = wl_surface_get_user_data(surface);
3239 if (!input->touch_focus) {
3240 DBG("Failed to find to touch focus for surface %p\n", surface);
3241 return;
3242 }
3243
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003244 if (surface != input->touch_focus->main_surface->surface) {
3245 DBG("Ignoring input event from subsurface %p\n", surface);
3246 input->touch_focus = NULL;
3247 return;
3248 }
3249
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003250 if (input->grab)
3251 widget = input->grab;
3252 else
3253 widget = window_find_widget(input->touch_focus,
3254 wl_fixed_to_double(x_w),
3255 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003256 if (widget) {
3257 struct touch_point *tp = xmalloc(sizeof *tp);
3258 if (tp) {
3259 tp->id = id;
3260 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003261 tp->x = sx;
3262 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003263 wl_list_insert(&input->touch_point_list, &tp->link);
3264
3265 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003266 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003267 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003268 sx, sy,
3269 widget->user_data);
3270 }
3271 }
3272}
3273
3274static void
3275touch_handle_up(void *data, struct wl_touch *wl_touch,
3276 uint32_t serial, uint32_t time, int32_t id)
3277{
3278 struct input *input = data;
3279 struct touch_point *tp, *tmp;
3280
Rusty Lynch041815a2013-08-08 21:20:38 -07003281 if (!input->touch_focus) {
3282 DBG("No touch focus found for touch up event!\n");
3283 return;
3284 }
3285
3286 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3287 if (tp->id != id)
3288 continue;
3289
3290 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003291 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003292 time, id,
3293 tp->widget->user_data);
3294
3295 wl_list_remove(&tp->link);
3296 free(tp);
3297
3298 return;
3299 }
3300}
3301
3302static void
3303touch_handle_motion(void *data, struct wl_touch *wl_touch,
3304 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3305{
3306 struct input *input = data;
3307 struct touch_point *tp;
3308 float sx = wl_fixed_to_double(x_w);
3309 float sy = wl_fixed_to_double(y_w);
3310
3311 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3312
3313 if (!input->touch_focus) {
3314 DBG("No touch focus found for touch motion event!\n");
3315 return;
3316 }
3317
3318 wl_list_for_each(tp, &input->touch_point_list, link) {
3319 if (tp->id != id)
3320 continue;
3321
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003322 tp->x = sx;
3323 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003324 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003325 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003326 id, sx, sy,
3327 tp->widget->user_data);
3328 return;
3329 }
3330}
3331
3332static void
3333touch_handle_frame(void *data, struct wl_touch *wl_touch)
3334{
3335 struct input *input = data;
3336 struct touch_point *tp, *tmp;
3337
3338 DBG("touch_handle_frame\n");
3339
3340 if (!input->touch_focus) {
3341 DBG("No touch focus found for touch frame event!\n");
3342 return;
3343 }
3344
3345 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3346 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003347 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003348 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003349 }
3350}
3351
3352static void
3353touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3354{
3355 struct input *input = data;
3356 struct touch_point *tp, *tmp;
3357
3358 DBG("touch_handle_cancel\n");
3359
3360 if (!input->touch_focus) {
3361 DBG("No touch focus found for touch cancel event!\n");
3362 return;
3363 }
3364
3365 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3366 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003367 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3368 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003369
3370 wl_list_remove(&tp->link);
3371 free(tp);
3372 }
3373}
3374
3375static const struct wl_touch_listener touch_listener = {
3376 touch_handle_down,
3377 touch_handle_up,
3378 touch_handle_motion,
3379 touch_handle_frame,
3380 touch_handle_cancel,
3381};
3382
3383static void
Daniel Stone37816df2012-05-16 18:45:18 +01003384seat_handle_capabilities(void *data, struct wl_seat *seat,
3385 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003386{
Daniel Stone37816df2012-05-16 18:45:18 +01003387 struct input *input = data;
3388
3389 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3390 input->pointer = wl_seat_get_pointer(seat);
3391 wl_pointer_set_user_data(input->pointer, input);
3392 wl_pointer_add_listener(input->pointer, &pointer_listener,
3393 input);
3394 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003395 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003396 wl_pointer_release(input->pointer);
3397 else
3398 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003399 input->pointer = NULL;
3400 }
3401
3402 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3403 input->keyboard = wl_seat_get_keyboard(seat);
3404 wl_keyboard_set_user_data(input->keyboard, input);
3405 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3406 input);
3407 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003408 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003409 wl_keyboard_release(input->keyboard);
3410 else
3411 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003412 input->keyboard = NULL;
3413 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003414
3415 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3416 input->touch = wl_seat_get_touch(seat);
3417 wl_touch_set_user_data(input->touch, input);
3418 wl_touch_add_listener(input->touch, &touch_listener, input);
3419 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003420 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003421 wl_touch_release(input->touch);
3422 else
3423 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003424 input->touch = NULL;
3425 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003426}
3427
Rob Bradford08031182013-08-13 20:11:03 +01003428static void
3429seat_handle_name(void *data, struct wl_seat *seat,
3430 const char *name)
3431{
3432
3433}
3434
Daniel Stone37816df2012-05-16 18:45:18 +01003435static const struct wl_seat_listener seat_listener = {
3436 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003437 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003438};
3439
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003440void
3441input_get_position(struct input *input, int32_t *x, int32_t *y)
3442{
3443 *x = input->sx;
3444 *y = input->sy;
3445}
3446
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003447int
3448input_get_touch(struct input *input, int32_t id, float *x, float *y)
3449{
3450 struct touch_point *tp;
3451
3452 wl_list_for_each(tp, &input->touch_point_list, link) {
3453 if (tp->id != id)
3454 continue;
3455
3456 *x = tp->x;
3457 *y = tp->y;
3458 return 0;
3459 }
3460
3461 return -1;
3462}
3463
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003464struct display *
3465input_get_display(struct input *input)
3466{
3467 return input->display;
3468}
3469
Daniel Stone37816df2012-05-16 18:45:18 +01003470struct wl_seat *
3471input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003472{
Daniel Stone37816df2012-05-16 18:45:18 +01003473 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003474}
3475
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003476uint32_t
3477input_get_modifiers(struct input *input)
3478{
3479 return input->modifiers;
3480}
3481
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003482struct widget *
3483input_get_focus_widget(struct input *input)
3484{
3485 return input->focus_widget;
3486}
3487
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003488struct data_offer {
3489 struct wl_data_offer *offer;
3490 struct input *input;
3491 struct wl_array types;
3492 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003493
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003494 struct task io_task;
3495 int fd;
3496 data_func_t func;
3497 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003498 uint32_t dnd_action;
3499 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003500 void *user_data;
3501};
3502
3503static void
3504data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3505{
3506 struct data_offer *offer = data;
3507 char **p;
3508
3509 p = wl_array_add(&offer->types, sizeof *p);
3510 *p = strdup(type);
3511}
3512
Carlos Garnacho9c931792016-01-18 23:52:12 +01003513static void
3514data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3515{
3516 struct data_offer *offer = data;
3517
3518 offer->source_actions = source_actions;
3519}
3520
3521static void
3522data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3523{
3524 struct data_offer *offer = data;
3525
3526 offer->dnd_action = dnd_action;
3527}
3528
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003529static const struct wl_data_offer_listener data_offer_listener = {
3530 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003531 data_offer_source_actions,
3532 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003533};
3534
3535static void
3536data_offer_destroy(struct data_offer *offer)
3537{
3538 char **p;
3539
3540 offer->refcount--;
3541 if (offer->refcount == 0) {
3542 wl_data_offer_destroy(offer->offer);
3543 for (p = offer->types.data; *p; p++)
3544 free(*p);
3545 wl_array_release(&offer->types);
3546 free(offer);
3547 }
3548}
3549
3550static void
3551data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003552 struct wl_data_device *data_device,
3553 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003554{
3555 struct data_offer *offer;
3556
Brian Lovinbc919262013-08-07 15:34:59 -07003557 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003558
3559 wl_array_init(&offer->types);
3560 offer->refcount = 1;
3561 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003562 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003563 wl_data_offer_add_listener(offer->offer,
3564 &data_offer_listener, offer);
3565}
3566
3567static void
3568data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003569 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003570 wl_fixed_t x_w, wl_fixed_t y_w,
3571 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003572{
3573 struct input *input = data;
3574 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003575 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003576 float x = wl_fixed_to_double(x_w);
3577 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003578 char **p;
3579
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003580 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003581 input->drag_enter_serial = serial;
3582 input->drag_focus = window,
3583 input->drag_x = x;
3584 input->drag_y = y;
3585
3586 if (!input->touch_grab)
3587 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003588
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003589 if (offer) {
3590 input->drag_offer = wl_data_offer_get_user_data(offer);
3591
3592 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3593 *p = NULL;
3594
3595 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003596
3597 if (input->display->data_device_manager_version >=
3598 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3599 wl_data_offer_set_actions(offer,
3600 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3601 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3602 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3603 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003604 } else {
3605 input->drag_offer = NULL;
3606 types_data = NULL;
3607 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003608
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003609 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003610 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003611 window->user_data);
3612}
3613
3614static void
3615data_device_leave(void *data, struct wl_data_device *data_device)
3616{
3617 struct input *input = data;
3618
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003619 if (input->drag_offer) {
3620 data_offer_destroy(input->drag_offer);
3621 input->drag_offer = NULL;
3622 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003623}
3624
3625static void
3626data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003627 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003628{
3629 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003630 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003631 float x = wl_fixed_to_double(x_w);
3632 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003633 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003634
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003635 input->drag_x = x;
3636 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003637
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003638 if (input->drag_offer)
3639 types_data = input->drag_offer->types.data;
3640 else
3641 types_data = NULL;
3642
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003643 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003644 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003645 window->user_data);
3646}
3647
3648static void
3649data_device_drop(void *data, struct wl_data_device *data_device)
3650{
3651 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003652 struct window *window = input->drag_focus;
3653 float x, y;
3654
3655 x = input->drag_x;
3656 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003657
3658 if (window->drop_handler)
3659 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003660 x, y, window->user_data);
3661
3662 if (input->touch_grab)
3663 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003664}
3665
3666static void
3667data_device_selection(void *data,
3668 struct wl_data_device *wl_data_device,
3669 struct wl_data_offer *offer)
3670{
3671 struct input *input = data;
3672 char **p;
3673
3674 if (input->selection_offer)
3675 data_offer_destroy(input->selection_offer);
3676
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003677 if (offer) {
3678 input->selection_offer = wl_data_offer_get_user_data(offer);
3679 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3680 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003681 } else {
3682 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003683 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003684}
3685
3686static const struct wl_data_device_listener data_device_listener = {
3687 data_device_data_offer,
3688 data_device_enter,
3689 data_device_leave,
3690 data_device_motion,
3691 data_device_drop,
3692 data_device_selection
3693};
3694
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003695static void
3696input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003697{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003698 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003699 struct wl_cursor *cursor;
3700 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003701
Daniel Stone80972742012-11-07 17:51:39 +11003702 if (!input->pointer)
3703 return;
3704
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003705 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003706 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003707 return;
3708
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003709 if (index >= (int) cursor->image_count) {
3710 fprintf(stderr, "cursor index out of range\n");
3711 return;
3712 }
3713
3714 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003715 buffer = wl_cursor_image_get_buffer(image);
3716 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003717 return;
3718
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003719 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3720 wl_surface_damage(input->pointer_surface, 0, 0,
3721 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003722 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003723 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3724 input->pointer_surface,
3725 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003726}
3727
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003728static const struct wl_callback_listener pointer_surface_listener;
3729
Derek Foreman493d9792015-03-04 16:26:25 -06003730static bool
3731input_set_pointer_special(struct input *input)
3732{
3733 if (input->current_cursor == CURSOR_BLANK) {
3734 wl_pointer_set_cursor(input->pointer,
3735 input->pointer_enter_serial,
3736 NULL, 0, 0);
3737 return true;
3738 }
3739
3740 if (input->current_cursor == CURSOR_UNSET)
3741 return true;
3742
3743 return false;
3744}
3745
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003746static void
Derek Foreman118a4292015-04-22 17:23:35 -05003747schedule_pointer_image_update(struct input *input,
3748 struct wl_cursor *cursor,
3749 uint32_t duration,
3750 bool force_frame)
3751{
3752 /* Some silly cursor sets have enormous pauses in them. In these
3753 * cases it's better to use a timer even if it results in less
3754 * accurate presentation, since it will save us having to set the
3755 * same cursor image over and over again.
3756 *
3757 * This is really not the way we're supposed to time any kind of
3758 * animation, but we're pretending it's OK here because we don't
3759 * want animated cursors with long delays to needlessly hog CPU.
3760 *
3761 * We use force_frame to ensure we don't accumulate large timing
3762 * errors by running off the wrong clock.
3763 */
3764 if (!force_frame && duration > 100) {
3765 struct timespec tp;
3766
3767 clock_gettime(CLOCK_MONOTONIC, &tp);
3768 input->cursor_timer_start = tp.tv_sec * 1000
3769 + tp.tv_nsec / 1000000;
3770 cursor_delay_timer_reset(input, duration);
3771 return;
3772 }
3773
3774 /* for short durations we'll just spin on frame callbacks for
3775 * accurate timing - the way any kind of timing sensitive animation
3776 * should really be done. */
3777 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3778 wl_callback_add_listener(input->cursor_frame_cb,
3779 &pointer_surface_listener, input);
3780
3781}
3782
3783static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003784pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3785 uint32_t time)
3786{
3787 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003788 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003789 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003790 uint32_t duration;
3791 bool force_frame = true;
3792
3793 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003794
3795 if (callback) {
3796 assert(callback == input->cursor_frame_cb);
3797 wl_callback_destroy(callback);
3798 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003799 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003800 }
3801
Daniel Stone80972742012-11-07 17:51:39 +11003802 if (!input->pointer)
3803 return;
3804
Derek Foreman493d9792015-03-04 16:26:25 -06003805 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003806 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003807
Daniel Stonea494f1d2012-06-18 19:31:12 +01003808 cursor = input->display->cursors[input->current_cursor];
3809 if (!cursor)
3810 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003811
3812 /* FIXME We don't have the current time on the first call so we set
3813 * the animation start to the time of the first frame callback. */
3814 if (time == 0)
3815 input->cursor_anim_start = 0;
3816 else if (input->cursor_anim_start == 0)
3817 input->cursor_anim_start = time;
3818
Derek Foreman118a4292015-04-22 17:23:35 -05003819 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003820
Derek Foreman118a4292015-04-22 17:23:35 -05003821 if (time == 0 || input->cursor_anim_start == 0) {
3822 duration = 0;
3823 i = 0;
3824 } else
3825 i = wl_cursor_frame_and_duration(
3826 cursor,
3827 time - input->cursor_anim_start,
3828 &duration);
3829
3830 if (cursor->image_count > 1)
3831 schedule_pointer_image_update(input, cursor, duration,
3832 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003833
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003834 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003835}
3836
Derek Foreman118a4292015-04-22 17:23:35 -05003837static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003838cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003839{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003840 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003841 struct timespec tp;
3842 struct wl_cursor *cursor;
3843 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003844
3845 if (!input->cursor_timer_running)
3846 return;
3847
Derek Foreman118a4292015-04-22 17:23:35 -05003848 cursor = input->display->cursors[input->current_cursor];
3849 if (!cursor)
3850 return;
3851
3852 clock_gettime(CLOCK_MONOTONIC, &tp);
3853 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3854 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3855}
3856
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003857static const struct wl_callback_listener pointer_surface_listener = {
3858 pointer_surface_frame_callback
3859};
3860
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003861void
3862input_set_pointer_image(struct input *input, int pointer)
3863{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003864 int force = 0;
3865
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003866 if (!input->pointer)
3867 return;
3868
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003869 if (input->pointer_enter_serial > input->cursor_serial)
3870 force = 1;
3871
3872 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003873 return;
3874
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003875 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003876 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003877 if (!input->cursor_frame_cb)
3878 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003879 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003880 /* The current frame callback may be stuck if, for instance,
3881 * the set cursor request was processed by the server after
3882 * this client lost the focus. In this case the cursor surface
3883 * might not be mapped and the frame callback wouldn't ever
3884 * complete. Send a set_cursor and attach to try to map the
3885 * cursor surface again so that the callback will finish */
3886 input_set_pointer_image_index(input, 0);
3887 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003888}
3889
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003890struct wl_data_device *
3891input_get_data_device(struct input *input)
3892{
3893 return input->data_device;
3894}
3895
3896void
3897input_set_selection(struct input *input,
3898 struct wl_data_source *source, uint32_t time)
3899{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003900 if (input->data_device)
3901 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003902}
3903
3904void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003905input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003906{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003907 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003908 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003909}
3910
3911static void
3912offer_io_func(struct task *task, uint32_t events)
3913{
3914 struct data_offer *offer =
3915 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003916 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003917 unsigned int len;
3918 char buffer[4096];
3919
3920 len = read(offer->fd, buffer, sizeof buffer);
3921 offer->func(buffer, len,
3922 offer->x, offer->y, offer->user_data);
3923
3924 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003925 if (display->data_device_manager_version >=
3926 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3927 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003928 close(offer->fd);
3929 data_offer_destroy(offer);
3930 }
3931}
3932
3933static void
3934data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3935 data_func_t func, void *user_data)
3936{
3937 int p[2];
3938
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003939 if (pipe2(p, O_CLOEXEC) == -1)
3940 return;
3941
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003942 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3943 close(p[1]);
3944
3945 offer->io_task.run = offer_io_func;
3946 offer->fd = p[0];
3947 offer->func = func;
3948 offer->refcount++;
3949 offer->user_data = user_data;
3950
3951 display_watch_fd(offer->input->display,
3952 offer->fd, EPOLLIN, &offer->io_task);
3953}
3954
3955void
3956input_receive_drag_data(struct input *input, const char *mime_type,
3957 data_func_t func, void *data)
3958{
3959 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003960 input->drag_offer->x = input->drag_x;
3961 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003962}
3963
3964int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003965input_receive_drag_data_to_fd(struct input *input,
3966 const char *mime_type, int fd)
3967{
3968 if (input->drag_offer)
3969 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3970
3971 return 0;
3972}
3973
3974int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003975input_receive_selection_data(struct input *input, const char *mime_type,
3976 data_func_t func, void *data)
3977{
3978 char **p;
3979
3980 if (input->selection_offer == NULL)
3981 return -1;
3982
3983 for (p = input->selection_offer->types.data; *p; p++)
3984 if (strcmp(mime_type, *p) == 0)
3985 break;
3986
3987 if (*p == NULL)
3988 return -1;
3989
3990 data_offer_receive_data(input->selection_offer,
3991 mime_type, func, data);
3992 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003993}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003994
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003995int
3996input_receive_selection_data_to_fd(struct input *input,
3997 const char *mime_type, int fd)
3998{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003999 if (input->selection_offer)
4000 wl_data_offer_receive(input->selection_offer->offer,
4001 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004002
4003 return 0;
4004}
4005
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004006void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004007window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004008{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004009 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004010 return;
4011
ant8mef99fac22018-11-28 22:46:37 +01004012 xdg_toplevel_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004013}
4014
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004015static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004016surface_set_synchronized(struct surface *surface)
4017{
4018 if (!surface->subsurface)
4019 return;
4020
4021 if (surface->synchronized)
4022 return;
4023
4024 wl_subsurface_set_sync(surface->subsurface);
4025 surface->synchronized = 1;
4026}
4027
4028static void
4029surface_set_synchronized_default(struct surface *surface)
4030{
4031 if (!surface->subsurface)
4032 return;
4033
4034 if (surface->synchronized == surface->synchronized_default)
4035 return;
4036
4037 if (surface->synchronized_default)
4038 wl_subsurface_set_sync(surface->subsurface);
4039 else
4040 wl_subsurface_set_desync(surface->subsurface);
4041
4042 surface->synchronized = surface->synchronized_default;
4043}
4044
4045static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004046surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004047{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004048 struct widget *widget = surface->widget;
4049 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004050
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004051 if (surface->input_region) {
4052 wl_region_destroy(surface->input_region);
4053 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004054 }
4055
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004056 if (surface->opaque_region)
4057 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004058
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004059 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004060
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004061 if (widget->resize_handler)
4062 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004063 widget->allocation.width,
4064 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004065 widget->user_data);
4066
Pekka Paalanen35e82632013-04-25 13:57:48 +03004067 if (surface->subsurface &&
4068 (surface->allocation.x != widget->allocation.x ||
4069 surface->allocation.y != widget->allocation.y)) {
4070 wl_subsurface_set_position(surface->subsurface,
4071 widget->allocation.x,
4072 widget->allocation.y);
4073 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004074 if (surface->allocation.width != widget->allocation.width ||
4075 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004076 window_schedule_redraw(widget->window);
4077 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004078 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004079
4080 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004081 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004082 widget->allocation.width,
4083 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004084}
4085
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004086static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004087window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004088{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004089 struct surface *surface;
4090
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004091 widget_set_allocation(window->main_surface->widget,
4092 window->pending_allocation.x,
4093 window->pending_allocation.y,
4094 window->pending_allocation.width,
4095 window->pending_allocation.height);
4096
4097 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004098
4099 /* The main surface is in the list, too. Main surface's
4100 * resize_handler is responsible for calling widget_set_allocation()
4101 * on all sub-surface root widgets, so they will be resized
4102 * properly.
4103 */
4104 wl_list_for_each(surface, &window->subsurface_list, link) {
4105 if (surface == window->main_surface)
4106 continue;
4107
4108 surface_set_synchronized(surface);
4109 surface_resize(surface);
4110 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004111
4112 if (!window->fullscreen && !window->maximized)
4113 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004114
4115 if (window->confined && window->confined_widget) {
4116 struct wl_compositor *compositor = window->display->compositor;
4117 struct wl_region *region;
4118 struct widget *widget = window->confined_widget;
4119
4120 region = wl_compositor_create_region(compositor);
4121 wl_region_add(region,
4122 widget->allocation.x,
4123 widget->allocation.y,
4124 widget->allocation.width,
4125 widget->allocation.height);
4126 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4127 region);
4128 wl_region_destroy(region);
4129 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004130}
4131
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004132static void
4133idle_resize(struct window *window)
4134{
4135 window->resize_needed = 0;
4136 window->redraw_needed = 1;
4137
4138 DBG("from %dx%d to %dx%d\n",
4139 window->main_surface->server_allocation.width,
4140 window->main_surface->server_allocation.height,
4141 window->pending_allocation.width,
4142 window->pending_allocation.height);
4143
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004144 window_do_resize(window);
4145}
4146
4147static void
4148undo_resize(struct window *window)
4149{
4150 window->pending_allocation.width =
4151 window->main_surface->server_allocation.width;
4152 window->pending_allocation.height =
4153 window->main_surface->server_allocation.height;
4154
4155 DBG("back to %dx%d\n",
4156 window->main_surface->server_allocation.width,
4157 window->main_surface->server_allocation.height);
4158
4159 window_do_resize(window);
4160
4161 if (window->pending_allocation.width == 0 &&
4162 window->pending_allocation.height == 0) {
4163 fprintf(stderr, "Error: Could not draw a surface, "
4164 "most likely due to insufficient disk space in "
4165 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4166 exit(EXIT_FAILURE);
4167 }
4168}
4169
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004170void
4171window_schedule_resize(struct window *window, int width, int height)
4172{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004173 /* We should probably get these numbers from the theme. */
4174 const int min_width = 200, min_height = 200;
4175
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004176 window->pending_allocation.x = 0;
4177 window->pending_allocation.y = 0;
4178 window->pending_allocation.width = width;
4179 window->pending_allocation.height = height;
4180
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004181 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004182 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004183 window->min_allocation.width = min_width;
4184 else
4185 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004186 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004187 window->min_allocation.height = min_height;
4188 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004189 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004190 }
4191
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004192 if (window->pending_allocation.width < window->min_allocation.width)
4193 window->pending_allocation.width = window->min_allocation.width;
4194 if (window->pending_allocation.height < window->min_allocation.height)
4195 window->pending_allocation.height = window->min_allocation.height;
4196
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004197 window->resize_needed = 1;
4198 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004199}
4200
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004201void
4202widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4203{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004204 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004205}
4206
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004207static int
4208window_get_shadow_margin(struct window *window)
4209{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004210 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004211 return frame_get_shadow_margin(window->frame->frame);
4212 else
4213 return 0;
4214}
4215
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004216void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004217window_inhibit_redraw(struct window *window)
4218{
4219 window->redraw_inhibited = 1;
4220 wl_list_remove(&window->redraw_task.link);
4221 wl_list_init(&window->redraw_task.link);
4222 window->redraw_task_scheduled = 0;
4223}
4224
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004225void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004226window_uninhibit_redraw(struct window *window)
4227{
4228 window->redraw_inhibited = 0;
4229 if (window->redraw_needed || window->resize_needed)
4230 window_schedule_redraw_task(window);
4231}
4232
4233static void
4234xdg_surface_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01004235 struct xdg_surface *xdg_surface,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004236 uint32_t serial)
4237{
4238 struct window *window = data;
4239
ant8mef99fac22018-11-28 22:46:37 +01004240 xdg_surface_ack_configure(window->xdg_surface, serial);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004241
4242 if (window->state_changed_handler)
4243 window->state_changed_handler(window, window->user_data);
4244
4245 window_uninhibit_redraw(window);
4246}
4247
ant8mef99fac22018-11-28 22:46:37 +01004248static const struct xdg_surface_listener xdg_surface_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004249 xdg_surface_handle_configure
4250};
4251
4252static void
ant8mef99fac22018-11-28 22:46:37 +01004253xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004254 int32_t width, int32_t height,
4255 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004256{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004257 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004258 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004259
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004260 window->maximized = 0;
4261 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004262 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004263 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004264
4265 wl_array_for_each(p, states) {
4266 uint32_t state = *p;
4267 switch (state) {
ant8mef99fac22018-11-28 22:46:37 +01004268 case XDG_TOPLEVEL_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004269 window->maximized = 1;
4270 break;
ant8mef99fac22018-11-28 22:46:37 +01004271 case XDG_TOPLEVEL_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004272 window->fullscreen = 1;
4273 break;
ant8mef99fac22018-11-28 22:46:37 +01004274 case XDG_TOPLEVEL_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004275 window->resizing = 1;
4276 break;
ant8mef99fac22018-11-28 22:46:37 +01004277 case XDG_TOPLEVEL_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004278 window->focused = 1;
4279 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004280 default:
4281 /* Unknown state */
4282 break;
4283 }
4284 }
4285
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004286 if (window->frame) {
4287 if (window->maximized) {
4288 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4289 } else {
4290 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4291 }
4292
4293 if (window->focused) {
4294 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4295 } else {
4296 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4297 }
4298 }
4299
Jasper St. Pierref184c382014-05-06 08:33:27 -04004300 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004301 /* The width / height params are for window geometry,
4302 * but window_schedule_resize takes allocation. Add
4303 * on the shadow margin to get the difference. */
4304 int margin = window_get_shadow_margin(window);
4305
4306 window_schedule_resize(window,
4307 width + margin * 2,
4308 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004309 } else if (window->saved_allocation.width > 0 &&
4310 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004311 window_schedule_resize(window,
4312 window->saved_allocation.width,
4313 window->saved_allocation.height);
4314 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004315}
4316
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004317static void
ant8mef99fac22018-11-28 22:46:37 +01004318xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004319{
4320 struct window *window = data;
4321 window_close(window);
4322}
4323
ant8mef99fac22018-11-28 22:46:37 +01004324static const struct xdg_toplevel_listener xdg_toplevel_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004325 xdg_toplevel_handle_configure,
4326 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004327};
4328
4329static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004330window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004331{
ant8mef99fac22018-11-28 22:46:37 +01004332 struct xdg_toplevel *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004333
4334 if (!window->xdg_surface)
4335 return;
4336
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004337 if (window->parent == window->last_parent)
4338 return;
4339
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004340 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004341 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004342 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004343 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004344
ant8mef99fac22018-11-28 22:46:37 +01004345 xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004346 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004347}
4348
4349static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004350window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004351{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004352 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004353 frame_input_rect(window->frame->frame,
4354 &geometry->x,
4355 &geometry->y,
4356 &geometry->width,
4357 &geometry->height);
4358 else
4359 window_get_allocation(window, geometry);
4360}
4361
4362static void
4363window_sync_geometry(struct window *window)
4364{
4365 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004366
4367 if (!window->xdg_surface)
4368 return;
4369
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004370 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004371 if (geometry.x == window->last_geometry.x &&
4372 geometry.y == window->last_geometry.y &&
4373 geometry.width == window->last_geometry.width &&
4374 geometry.height == window->last_geometry.height)
4375 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004376
ant8mef99fac22018-11-28 22:46:37 +01004377 xdg_surface_set_window_geometry(window->xdg_surface,
4378 geometry.x,
4379 geometry.y,
4380 geometry.width,
4381 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004382 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004383}
4384
4385static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004386window_flush(struct window *window)
4387{
4388 struct surface *surface;
4389
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004390 assert(!window->redraw_inhibited);
4391
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004392 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004393 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004394 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004395 if (window->xdg_toplevel)
4396 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004397 }
4398
4399 wl_list_for_each(surface, &window->subsurface_list, link) {
4400 if (surface == window->main_surface)
4401 continue;
4402
4403 surface_flush(surface);
4404 }
4405
4406 surface_flush(window->main_surface);
4407}
4408
4409static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004410menu_destroy(struct menu *menu)
4411{
4412 widget_destroy(menu->widget);
4413 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004414 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004415 free(menu);
4416}
4417
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004418void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004419window_get_allocation(struct window *window,
4420 struct rectangle *allocation)
4421{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004422 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004423}
4424
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004425static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004426widget_redraw(struct widget *widget)
4427{
4428 struct widget *child;
4429
4430 if (widget->redraw_handler)
4431 widget->redraw_handler(widget, widget->user_data);
4432 wl_list_for_each(child, &widget->child_list, link)
4433 widget_redraw(child);
4434}
4435
4436static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004437frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4438{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004439 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004440
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004441 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004442 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004443 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004444 surface->frame_cb = NULL;
4445
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004446 surface->last_time = time;
4447
Pekka Paalanen71233882013-04-25 13:57:53 +03004448 if (surface->redraw_needed || surface->window->redraw_needed) {
4449 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004450 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004451 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004452}
4453
4454static const struct wl_callback_listener listener = {
4455 frame_callback
4456};
4457
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004458static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004459surface_redraw(struct surface *surface)
4460{
Pekka Paalanen71233882013-04-25 13:57:53 +03004461 DBG_OBJ(surface->surface, "begin\n");
4462
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004463 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004464 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004465
4466 /* Whole-window redraw forces a redraw even if the previous has
4467 * not yet hit the screen.
4468 */
4469 if (surface->frame_cb) {
4470 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004471 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004472
Pekka Paalanen71233882013-04-25 13:57:53 +03004473 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004474 wl_callback_destroy(surface->frame_cb);
4475 }
4476
Neil Roberts97b747c2013-12-19 16:17:12 +00004477 if (surface->widget->use_cairo &&
4478 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004479 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004480 return -1;
4481 }
4482
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004483 surface->frame_cb = wl_surface_frame(surface->surface);
4484 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004485 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004486
4487 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004488 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004489 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004490 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004491 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004492}
4493
4494static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004495idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004496{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004497 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004498 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004499 int failed = 0;
4500 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004501
Pekka Paalanen71233882013-04-25 13:57:53 +03004502 DBG(" --------- \n");
4503
Pekka Paalaneneebff542013-04-25 13:57:52 +03004504 wl_list_init(&window->redraw_task.link);
4505 window->redraw_task_scheduled = 0;
4506
4507 if (window->resize_needed) {
4508 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004509 if (window->main_surface->frame_cb) {
4510 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004511 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004512 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004513
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004514 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004515 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004516 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004517
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004518 if (surface_redraw(window->main_surface) < 0) {
4519 /*
4520 * Only main_surface failure will cause us to undo the resize.
4521 * If sub-surfaces fail, they will just be broken with old
4522 * content.
4523 */
4524 failed = 1;
4525 } else {
4526 wl_list_for_each(surface, &window->subsurface_list, link) {
4527 if (surface == window->main_surface)
4528 continue;
4529
4530 surface_redraw(surface);
4531 }
4532 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004533
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004534 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004535 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004536
4537 wl_list_for_each(surface, &window->subsurface_list, link)
4538 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004539
4540 if (resized && failed) {
4541 /* Restore widget tree to correspond to what is on screen. */
4542 undo_resize(window);
4543 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004544}
4545
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004546static void
4547window_schedule_redraw_task(struct window *window)
4548{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004549 if (window->redraw_inhibited)
4550 return;
4551
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004552 if (!window->redraw_task_scheduled) {
4553 window->redraw_task.run = idle_redraw;
4554 display_defer(window->display, &window->redraw_task);
4555 window->redraw_task_scheduled = 1;
4556 }
4557}
4558
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004559void
4560window_schedule_redraw(struct window *window)
4561{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004562 struct surface *surface;
4563
Pekka Paalanen71233882013-04-25 13:57:53 +03004564 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4565
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004566 wl_list_for_each(surface, &window->subsurface_list, link)
4567 surface->redraw_needed = 1;
4568
4569 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004570}
4571
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004572int
4573window_is_fullscreen(struct window *window)
4574{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004575 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004576}
4577
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004578void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004579window_set_fullscreen(struct window *window, int fullscreen)
4580{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004581 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004582 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004583
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004584 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004585 return;
4586
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004587 if (fullscreen)
ant8mef99fac22018-11-28 22:46:37 +01004588 xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004589 else
ant8mef99fac22018-11-28 22:46:37 +01004590 xdg_toplevel_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004591}
4592
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004593int
4594window_is_maximized(struct window *window)
4595{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004596 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004597}
4598
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004599void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004600window_set_maximized(struct window *window, int maximized)
4601{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004602 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004603 return;
4604
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004605 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004606 return;
4607
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004608 if (maximized)
ant8mef99fac22018-11-28 22:46:37 +01004609 xdg_toplevel_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004610 else
ant8mef99fac22018-11-28 22:46:37 +01004611 xdg_toplevel_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004612}
4613
Jasper St. Pierrede680992014-04-10 17:23:49 -07004614int
4615window_is_resizing(struct window *window)
4616{
4617 return window->resizing;
4618}
4619
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004620void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004621window_set_minimized(struct window *window)
4622{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004623 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004624 return;
4625
ant8mef99fac22018-11-28 22:46:37 +01004626 xdg_toplevel_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004627}
4628
4629void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004630window_set_user_data(struct window *window, void *data)
4631{
4632 window->user_data = data;
4633}
4634
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004635void *
4636window_get_user_data(struct window *window)
4637{
4638 return window->user_data;
4639}
4640
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004641void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004642window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004643 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004644{
4645 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004646}
4647
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004648void
4649window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004650 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004651{
4652 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004653}
4654
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004655void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004656window_set_data_handler(struct window *window, window_data_handler_t handler)
4657{
4658 window->data_handler = handler;
4659}
4660
4661void
4662window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4663{
4664 window->drop_handler = handler;
4665}
4666
4667void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004668window_set_close_handler(struct window *window,
4669 window_close_handler_t handler)
4670{
4671 window->close_handler = handler;
4672}
4673
4674void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004675window_set_fullscreen_handler(struct window *window,
4676 window_fullscreen_handler_t handler)
4677{
4678 window->fullscreen_handler = handler;
4679}
4680
4681void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004682window_set_output_handler(struct window *window,
4683 window_output_handler_t handler)
4684{
4685 window->output_handler = handler;
4686}
4687
4688void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004689window_set_state_changed_handler(struct window *window,
4690 window_state_changed_handler_t handler)
4691{
4692 window->state_changed_handler = handler;
4693}
4694
4695void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004696window_set_pointer_locked_handler(struct window *window,
4697 locked_pointer_locked_handler_t locked,
4698 locked_pointer_unlocked_handler_t unlocked)
4699{
4700 window->pointer_unlocked_handler = unlocked;
4701 window->pointer_locked_handler = locked;
4702}
4703
4704void
4705window_set_pointer_confined_handler(struct window *window,
4706 confined_pointer_confined_handler_t confined,
4707 confined_pointer_unconfined_handler_t unconfined)
4708{
4709 window->pointer_confined_handler = confined;
4710 window->pointer_unconfined_handler = unconfined;
4711}
4712
4713void
4714window_set_locked_pointer_motion_handler(struct window *window,
4715 window_locked_pointer_motion_handler_t handler)
4716{
4717 window->locked_pointer_motion_handler = handler;
4718}
4719
4720void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004721window_set_title(struct window *window, const char *title)
4722{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004723 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004724 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004725 if (window->frame) {
4726 frame_set_title(window->frame->frame, title);
4727 widget_schedule_redraw(window->frame->widget);
4728 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004729 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01004730 xdg_toplevel_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004731}
4732
4733const char *
4734window_get_title(struct window *window)
4735{
4736 return window->title;
4737}
4738
4739void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004740window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4741{
4742 struct text_cursor_position *text_cursor_position =
4743 window->display->text_cursor_position;
4744
Scott Moreau9295ce02012-06-01 12:46:10 -06004745 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004746 return;
4747
4748 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004749 window->main_surface->surface,
4750 wl_fixed_from_int(x),
4751 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004752}
4753
Casey Dahlin9074db52012-04-19 22:50:09 -04004754static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004755relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4756 uint32_t utime_hi,
4757 uint32_t utime_lo,
4758 wl_fixed_t dx,
4759 wl_fixed_t dy,
4760 wl_fixed_t dx_unaccel,
4761 wl_fixed_t dy_unaccel)
4762{
4763 struct input *input = data;
4764 struct window *window = input->pointer_focus;
4765 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4766
4767 if (window->locked_pointer_motion_handler &&
4768 window->pointer_locked) {
4769 window->locked_pointer_motion_handler(
4770 window, input, ms,
4771 wl_fixed_to_double(dx),
4772 wl_fixed_to_double(dy),
4773 window->user_data);
4774 }
4775}
4776
4777static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4778 relative_pointer_handle_motion,
4779};
4780
4781static void
4782locked_pointer_locked(void *data,
4783 struct zwp_locked_pointer_v1 *locked_pointer)
4784{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004785 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004786 struct window *window = input->locked_window;
4787
4788 if (!window)
4789 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004790
4791 window->pointer_locked = true;
4792
4793 if (window->pointer_locked_handler) {
4794 window->pointer_locked_handler(window,
4795 input,
4796 window->user_data);
4797 }
4798}
4799
4800static void
4801locked_pointer_unlocked(void *data,
4802 struct zwp_locked_pointer_v1 *locked_pointer)
4803{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004804 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004805 struct window *window = input->locked_window;
4806
4807 if (!window)
4808 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004809
4810 window_unlock_pointer(window);
4811
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004812 input->locked_window = NULL;
4813
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004814 if (window->pointer_unlocked_handler) {
4815 window->pointer_unlocked_handler(window,
4816 input,
4817 window->user_data);
4818 }
4819}
4820
4821static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4822 locked_pointer_locked,
4823 locked_pointer_unlocked,
4824};
4825
4826int
4827window_lock_pointer(struct window *window, struct input *input)
4828{
4829 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4830 window->display->relative_pointer_manager;
4831 struct zwp_pointer_constraints_v1 *pointer_constraints =
4832 window->display->pointer_constraints;
4833 struct zwp_relative_pointer_v1 *relative_pointer;
4834 struct zwp_locked_pointer_v1 *locked_pointer;
4835
4836 if (!window->display->relative_pointer_manager)
4837 return -1;
4838
4839 if (!window->display->pointer_constraints)
4840 return -1;
4841
4842 if (window->locked_pointer)
4843 return -1;
4844
4845 if (window->confined_pointer)
4846 return -1;
4847
4848 if (!input->pointer)
4849 return -1;
4850
4851 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4852 relative_pointer_manager, input->pointer);
4853 zwp_relative_pointer_v1_add_listener(relative_pointer,
4854 &relative_pointer_listener,
4855 input);
4856
4857 locked_pointer =
4858 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4859 window->main_surface->surface,
4860 input->pointer,
4861 NULL,
4862 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4863 zwp_locked_pointer_v1_add_listener(locked_pointer,
4864 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004865 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004866
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004867 window->locked_pointer = locked_pointer;
4868 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004869 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004870
4871 return 0;
4872}
4873
4874void
4875window_unlock_pointer(struct window *window)
4876{
4877 if (!window->locked_pointer)
4878 return;
4879
4880 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4881 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4882 window->locked_pointer = NULL;
4883 window->relative_pointer = NULL;
4884 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004885}
4886
4887void
4888widget_set_locked_pointer_cursor_hint(struct widget *widget,
4889 float x, float y)
4890{
4891 struct window *window = widget->window;
4892
4893 if (!window->locked_pointer)
4894 return;
4895
4896 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4897 wl_fixed_from_double(x),
4898 wl_fixed_from_double(y));
4899 wl_surface_commit(window->main_surface->surface);
4900}
4901
4902static void
4903confined_pointer_confined(void *data,
4904 struct zwp_confined_pointer_v1 *confined_pointer)
4905{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004906 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004907 struct window *window = input->confined_window;
4908
4909 if (!window)
4910 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004911
4912 window->confined = true;
4913
4914 if (window->pointer_confined_handler) {
4915 window->pointer_confined_handler(window,
4916 input,
4917 window->user_data);
4918 }
4919}
4920
4921static void
4922confined_pointer_unconfined(void *data,
4923 struct zwp_confined_pointer_v1 *confined_pointer)
4924{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004925 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004926 struct window *window = input->confined_window;
4927
4928 if (!window)
4929 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004930
4931 window_unconfine_pointer(window);
4932
4933 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004934 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004935
4936 if (window->pointer_unconfined_handler) {
4937 window->pointer_unconfined_handler(window,
4938 input,
4939 window->user_data);
4940 }
4941}
4942
4943static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4944 confined_pointer_confined,
4945 confined_pointer_unconfined,
4946};
4947
4948int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004949window_confine_pointer_to_rectangles(struct window *window,
4950 struct input *input,
4951 struct rectangle *rectangles,
4952 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004953{
4954 struct zwp_pointer_constraints_v1 *pointer_constraints =
4955 window->display->pointer_constraints;
4956 struct zwp_confined_pointer_v1 *confined_pointer;
4957 struct wl_compositor *compositor = window->display->compositor;
4958 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004959 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004960
4961 if (!window->display->pointer_constraints)
4962 return -1;
4963
4964 if (window->locked_pointer)
4965 return -1;
4966
4967 if (window->confined_pointer)
4968 return -1;
4969
4970 if (!input->pointer)
4971 return -1;
4972
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004973 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004974 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004975 for (i = 0; i < num_rectangles; i++) {
4976 wl_region_add(region,
4977 rectangles[i].x,
4978 rectangles[i].y,
4979 rectangles[i].width,
4980 rectangles[i].height);
4981 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004982 }
4983
4984 confined_pointer =
4985 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
4986 window->main_surface->surface,
4987 input->pointer,
4988 region,
4989 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4990 if (region)
4991 wl_region_destroy(region);
4992
4993 zwp_confined_pointer_v1_add_listener(confined_pointer,
4994 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004995 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004996
4997 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004998 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004999 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005000
5001 return 0;
5002}
5003
5004void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005005window_update_confine_rectangles(struct window *window,
5006 struct rectangle *rectangles,
5007 int num_rectangles)
5008{
5009 struct wl_compositor *compositor = window->display->compositor;
5010 struct wl_region *region;
5011 int i;
5012
5013 region = wl_compositor_create_region(compositor);
5014 for (i = 0; i < num_rectangles; i++) {
5015 wl_region_add(region,
5016 rectangles[i].x,
5017 rectangles[i].y,
5018 rectangles[i].width,
5019 rectangles[i].height);
5020 }
5021
5022 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5023
5024 wl_region_destroy(region);
5025}
5026
5027int
5028window_confine_pointer_to_widget(struct window *window,
5029 struct widget *widget,
5030 struct input *input)
5031{
5032 int ret;
5033
5034 if (widget) {
5035 ret = window_confine_pointer_to_rectangles(window,
5036 input,
5037 &widget->allocation,
5038 1);
5039 window->confined_widget = widget;
5040 return ret;
5041 } else {
5042 return window_confine_pointer_to_rectangles(window,
5043 input,
5044 NULL,
5045 0);
5046 }
5047}
5048
5049void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005050window_unconfine_pointer(struct window *window)
5051{
5052 if (!window->confined_pointer)
5053 return;
5054
5055 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5056 window->confined_pointer = NULL;
5057 window->confined = false;
5058}
5059
5060static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005061surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005062 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005063{
Rob Bradford7507b572012-05-15 17:55:34 +01005064 struct window *window = data;
5065 struct output *output;
5066 struct output *output_found = NULL;
5067 struct window_output *window_output;
5068
5069 wl_list_for_each(output, &window->display->output_list, link) {
5070 if (output->output == wl_output) {
5071 output_found = output;
5072 break;
5073 }
5074 }
5075
5076 if (!output_found)
5077 return;
5078
Brian Lovinbc919262013-08-07 15:34:59 -07005079 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005080 window_output->output = output_found;
5081
5082 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005083
5084 if (window->output_handler)
5085 window->output_handler(window, output_found, 1,
5086 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005087}
5088
5089static void
5090surface_leave(void *data,
5091 struct wl_surface *wl_surface, struct wl_output *output)
5092{
Rob Bradford7507b572012-05-15 17:55:34 +01005093 struct window *window = data;
5094 struct window_output *window_output;
5095 struct window_output *window_output_found = NULL;
5096
5097 wl_list_for_each(window_output, &window->window_output_list, link) {
5098 if (window_output->output->output == output) {
5099 window_output_found = window_output;
5100 break;
5101 }
5102 }
5103
5104 if (window_output_found) {
5105 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005106
5107 if (window->output_handler)
5108 window->output_handler(window, window_output->output,
5109 0, window->user_data);
5110
Rob Bradford7507b572012-05-15 17:55:34 +01005111 free(window_output_found);
5112 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005113}
5114
5115static const struct wl_surface_listener surface_listener = {
5116 surface_enter,
5117 surface_leave
5118};
5119
Pekka Paalanen4e373742013-02-13 16:17:13 +02005120static struct surface *
5121surface_create(struct window *window)
5122{
5123 struct display *display = window->display;
5124 struct surface *surface;
5125
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005126 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005127 surface->window = window;
5128 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005129 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005130 wl_surface_add_listener(surface->surface, &surface_listener, window);
5131
Pekka Paalanen35e82632013-04-25 13:57:48 +03005132 wl_list_insert(&window->subsurface_list, &surface->link);
5133
Pekka Paalanen4e373742013-02-13 16:17:13 +02005134 return surface;
5135}
5136
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005137static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005138get_preferred_buffer_type(struct display *display)
5139{
5140#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005141 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005142 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5143#endif
5144
5145 return WINDOW_BUFFER_TYPE_SHM;
5146}
5147
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005148static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005149window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005150{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005151 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005152 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005153
Peter Huttererf3d62272013-08-08 11:57:05 +10005154 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005155 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005156 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005157
5158 surface = surface_create(window);
5159 window->main_surface = surface;
5160
Michael Teyfeld6371b52017-10-11 17:17:29 +02005161 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005162
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005163 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005164 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005165
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005166 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005167
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005168 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005169 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005170 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005171
Rob Bradford7507b572012-05-15 17:55:34 +01005172 wl_list_init (&window->window_output_list);
5173
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005174 return window;
5175}
5176
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005177struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005178window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005179{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005180 struct window *window;
5181
5182 window = window_create_internal(display, 0);
5183
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005184 if (window->display->xdg_shell) {
5185 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005186 xdg_wm_base_get_xdg_surface(window->display->xdg_shell,
5187 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005188 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005189
ant8mef99fac22018-11-28 22:46:37 +01005190 xdg_surface_add_listener(window->xdg_surface,
5191 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005192
5193 window->xdg_toplevel =
ant8mef99fac22018-11-28 22:46:37 +01005194 xdg_surface_get_toplevel(window->xdg_surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005195 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5196
ant8mef99fac22018-11-28 22:46:37 +01005197 xdg_toplevel_add_listener(window->xdg_toplevel,
5198 &xdg_toplevel_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005199
5200 window_inhibit_redraw(window);
5201
5202 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005203 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005204
5205 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005206}
5207
5208struct window *
5209window_create_custom(struct display *display)
5210{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005211 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005212}
5213
Jasper St. Pierre53686042013-12-09 15:26:25 -05005214void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005215window_set_parent(struct window *window,
5216 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005217{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005218 window->parent = parent_window;
5219 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005220}
5221
5222struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005223window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005224{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005225 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005226}
5227
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005228static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005229menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005230{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005231 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005232 int next;
5233
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005234 frame_interior(menu->frame, &x, &y, &width, &height);
5235 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005236 if (menu->current != next) {
5237 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005238 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005239 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005240}
5241
5242static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005243menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005244 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005245 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005246{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005247 struct menu *menu = data;
5248
5249 if (widget == menu->widget)
5250 menu_set_item(data, y);
5251
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005252 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005253}
5254
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005255static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005256menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005257 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005258{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005259 struct menu *menu = data;
5260
5261 if (widget == menu->widget)
5262 menu_set_item(data, y);
5263
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005264 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005265}
5266
5267static void
5268menu_leave_handler(struct widget *widget, struct input *input, void *data)
5269{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005270 struct menu *menu = data;
5271
5272 if (widget == menu->widget)
5273 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005274}
5275
5276static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005277menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005278 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005279 uint32_t button, enum wl_pointer_button_state state,
5280 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005281
5282{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005283 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005284
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005285 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5286 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005287 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005288 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005289 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005290 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005291 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005292 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005293 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005294 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005295}
5296
5297static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005298menu_touch_up_handler(struct widget *widget,
5299 struct input *input,
5300 uint32_t serial,
5301 uint32_t time,
5302 int32_t id,
5303 void *data)
5304{
5305 struct menu *menu = data;
5306
5307 input_ungrab(input);
5308 menu_destroy(menu);
5309}
5310
5311static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005312menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005313{
5314 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005315 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005316 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005317
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005318 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005319
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005320 frame_repaint(menu->frame, cr);
5321 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005322
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005323 theme_set_background_source(menu->window->display->theme,
5324 cr, THEME_FRAME_ACTIVE);
5325 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005326 cairo_fill(cr);
5327
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005328 cairo_select_font_face(cr, "sans",
5329 CAIRO_FONT_SLANT_NORMAL,
5330 CAIRO_FONT_WEIGHT_NORMAL);
5331 cairo_set_font_size(cr, 12);
5332
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005333 for (i = 0; i < menu->count; i++) {
5334 if (i == menu->current) {
5335 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005336 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005337 cairo_fill(cr);
5338 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005339 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005340 cairo_show_text(cr, menu->entries[i]);
5341 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005342 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5343 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005344 cairo_show_text(cr, menu->entries[i]);
5345 }
5346 }
5347
5348 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005349}
5350
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005351static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005352xdg_popup_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01005353 struct xdg_popup *xdg_popup,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005354 int32_t x,
5355 int32_t y,
5356 int32_t width,
5357 int32_t height)
5358{
5359}
5360
5361static void
ant8mef99fac22018-11-28 22:46:37 +01005362xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005363{
5364 struct window *window = data;
5365 struct menu *menu = window->main_surface->widget->user_data;
5366
5367 input_ungrab(menu->input);
5368 menu_destroy(menu);
5369}
5370
ant8mef99fac22018-11-28 22:46:37 +01005371static const struct xdg_popup_listener xdg_popup_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005372 xdg_popup_handle_configure,
5373 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005374};
5375
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005376static struct menu *
5377create_menu(struct display *display,
5378 struct input *input, uint32_t time,
5379 menu_func_t func, const char **entries, int count,
5380 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005381{
5382 struct window *window;
5383 struct menu *menu;
5384
5385 menu = malloc(sizeof *menu);
5386 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005387 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005388
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005389 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005390 if (!window) {
5391 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005392 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005393 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005394
5395 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005396 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005397 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005398 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005399 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005400 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005401 menu->entries = entries;
5402 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005403 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005404 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005405 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005406 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005407 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005408
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005409 input_ungrab(input);
5410
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005411 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005412 widget_set_enter_handler(menu->widget, menu_enter_handler);
5413 widget_set_leave_handler(menu->widget, menu_leave_handler);
5414 widget_set_motion_handler(menu->widget, menu_motion_handler);
5415 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005416 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005417
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005418 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005419 frame_resize_inside(menu->frame, 200, count * 20);
5420 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5421 window_schedule_resize(window, frame_width(menu->frame),
5422 frame_height(menu->frame));
5423
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005424 return menu;
5425}
5426
ant8mef99fac22018-11-28 22:46:37 +01005427static struct xdg_positioner *
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005428create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005429 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005430{
ant8mef99fac22018-11-28 22:46:37 +01005431 struct xdg_positioner *positioner;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005432
ant8mef99fac22018-11-28 22:46:37 +01005433 positioner = xdg_wm_base_create_positioner(display->xdg_shell);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005434 fail_on_null(positioner, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005435 xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1);
5436 xdg_positioner_set_size(positioner, w, h);
5437 xdg_positioner_set_anchor(positioner,
5438 XDG_POSITIONER_ANCHOR_TOP_LEFT);
5439 xdg_positioner_set_gravity(positioner,
5440 XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005441
5442 return positioner;
5443}
5444
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005445void
5446window_show_menu(struct display *display,
5447 struct input *input, uint32_t time, struct window *parent,
5448 int32_t x, int32_t y,
5449 menu_func_t func, const char **entries, int count)
5450{
5451 struct menu *menu;
5452 struct window *window;
5453 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005454 struct rectangle parent_geometry;
ant8mef99fac22018-11-28 22:46:37 +01005455 struct xdg_positioner *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005456
5457 menu = create_menu(display, input, time, func, entries, count, parent);
5458
5459 if (menu == NULL)
5460 return;
5461
5462 window = menu->window;
5463
5464 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5465 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5466
5467 window->x = x;
5468 window->y = y;
5469
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005470 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005471 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005472
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005473 if (!display->xdg_shell)
5474 return;
5475
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005476 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005477 xdg_wm_base_get_xdg_surface(display->xdg_shell,
5478 window->main_surface->surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005479 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005480
ant8mef99fac22018-11-28 22:46:37 +01005481 xdg_surface_add_listener(window->xdg_surface,
5482 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005483
5484 positioner = create_simple_positioner(display,
5485 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005486 window->y - (iy + parent_geometry.y),
5487 frame_width(menu->frame),
5488 frame_height(menu->frame));
ant8mef99fac22018-11-28 22:46:37 +01005489 window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
5490 parent->xdg_surface,
5491 positioner);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005492 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005493 xdg_positioner_destroy(positioner);
5494 xdg_popup_grab(window->xdg_popup, input->seat,
5495 display_get_serial(window->display));
5496 xdg_popup_add_listener(window->xdg_popup,
5497 &xdg_popup_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005498
5499 window_inhibit_redraw(window);
5500
5501 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005502}
5503
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005504void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005505window_set_buffer_type(struct window *window, enum window_buffer_type type)
5506{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005507 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005508}
5509
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005510enum window_buffer_type
5511window_get_buffer_type(struct window *window)
5512{
5513 return window->main_surface->buffer_type;
5514}
5515
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005516void
5517window_set_preferred_format(struct window *window,
5518 enum preferred_format format)
5519{
5520 window->preferred_format = format;
5521}
5522
Pekka Paalanen35e82632013-04-25 13:57:48 +03005523struct widget *
5524window_add_subsurface(struct window *window, void *data,
5525 enum subsurface_mode default_mode)
5526{
5527 struct widget *widget;
5528 struct surface *surface;
5529 struct wl_surface *parent;
5530 struct wl_subcompositor *subcompo = window->display->subcompositor;
5531
Pekka Paalanen35e82632013-04-25 13:57:48 +03005532 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005533 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005534 widget = widget_create(window, surface, data);
5535 wl_list_init(&widget->link);
5536 surface->widget = widget;
5537
5538 parent = window->main_surface->surface;
5539 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5540 surface->surface,
5541 parent);
5542 surface->synchronized = 1;
5543
5544 switch (default_mode) {
5545 case SUBSURFACE_SYNCHRONIZED:
5546 surface->synchronized_default = 1;
5547 break;
5548 case SUBSURFACE_DESYNCHRONIZED:
5549 surface->synchronized_default = 0;
5550 break;
5551 default:
5552 assert(!"bad enum subsurface_mode");
5553 }
5554
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005555 window->resize_needed = 1;
5556 window_schedule_redraw(window);
5557
Pekka Paalanen35e82632013-04-25 13:57:48 +03005558 return widget;
5559}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005560
5561static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005562display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005563 struct wl_output *wl_output,
5564 int x, int y,
5565 int physical_width,
5566 int physical_height,
5567 int subpixel,
5568 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005569 const char *model,
5570 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005571{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005572 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005573
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005574 output->allocation.x = x;
5575 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005576 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005577
5578 if (output->make)
5579 free(output->make);
5580 output->make = strdup(make);
5581
5582 if (output->model)
5583 free(output->model);
5584 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005585}
5586
5587static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005588display_handle_done(void *data,
5589 struct wl_output *wl_output)
5590{
5591}
5592
5593static void
5594display_handle_scale(void *data,
5595 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005596 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005597{
5598 struct output *output = data;
5599
5600 output->scale = scale;
5601}
5602
5603static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005604display_handle_mode(void *data,
5605 struct wl_output *wl_output,
5606 uint32_t flags,
5607 int width,
5608 int height,
5609 int refresh)
5610{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005611 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005612 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005613
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005614 if (flags & WL_OUTPUT_MODE_CURRENT) {
5615 output->allocation.width = width;
5616 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005617 if (display->output_configure_handler)
5618 (*display->output_configure_handler)(
5619 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005620 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005621}
5622
5623static const struct wl_output_listener output_listener = {
5624 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005625 display_handle_mode,
5626 display_handle_done,
5627 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005628};
5629
5630static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005631display_add_output(struct display *d, uint32_t id)
5632{
5633 struct output *output;
5634
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005635 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005636 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005637 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005638 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005639 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005640 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005641 wl_list_insert(d->output_list.prev, &output->link);
5642
5643 wl_output_add_listener(output->output, &output_listener, output);
5644}
5645
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005646static void
5647output_destroy(struct output *output)
5648{
5649 if (output->destroy_handler)
5650 (*output->destroy_handler)(output, output->user_data);
5651
5652 wl_output_destroy(output->output);
5653 wl_list_remove(&output->link);
5654 free(output);
5655}
5656
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005657static void
5658display_destroy_output(struct display *d, uint32_t id)
5659{
5660 struct output *output;
5661
5662 wl_list_for_each(output, &d->output_list, link) {
5663 if (output->server_output_id == id) {
5664 output_destroy(output);
5665 break;
5666 }
5667 }
5668}
5669
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005670void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005671display_set_global_handler(struct display *display,
5672 display_global_handler_t handler)
5673{
5674 struct global *global;
5675
5676 display->global_handler = handler;
5677 if (!handler)
5678 return;
5679
5680 wl_list_for_each(global, &display->global_list, link)
5681 display->global_handler(display,
5682 global->name, global->interface,
5683 global->version, display->user_data);
5684}
5685
5686void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005687display_set_global_handler_remove(struct display *display,
5688 display_global_handler_t remove_handler)
5689{
5690 display->global_handler_remove = remove_handler;
5691 if (!remove_handler)
5692 return;
5693}
5694
5695void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005696display_set_output_configure_handler(struct display *display,
5697 display_output_handler_t handler)
5698{
5699 struct output *output;
5700
5701 display->output_configure_handler = handler;
5702 if (!handler)
5703 return;
5704
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005705 wl_list_for_each(output, &display->output_list, link) {
5706 if (output->allocation.width == 0 &&
5707 output->allocation.height == 0)
5708 continue;
5709
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005710 (*display->output_configure_handler)(output,
5711 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005712 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005713}
5714
5715void
5716output_set_user_data(struct output *output, void *data)
5717{
5718 output->user_data = data;
5719}
5720
5721void *
5722output_get_user_data(struct output *output)
5723{
5724 return output->user_data;
5725}
5726
5727void
5728output_set_destroy_handler(struct output *output,
5729 display_output_handler_t handler)
5730{
5731 output->destroy_handler = handler;
5732 /* FIXME: implement this, once we have way to remove outputs */
5733}
5734
5735void
Scott Moreau4e072362012-09-29 02:03:11 -06005736output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005737{
Scott Moreau4e072362012-09-29 02:03:11 -06005738 struct rectangle allocation = output->allocation;
5739
5740 switch (output->transform) {
5741 case WL_OUTPUT_TRANSFORM_90:
5742 case WL_OUTPUT_TRANSFORM_270:
5743 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5744 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5745 /* Swap width and height */
5746 allocation.width = output->allocation.height;
5747 allocation.height = output->allocation.width;
5748 break;
5749 }
5750
5751 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005752}
5753
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005754struct wl_output *
5755output_get_wl_output(struct output *output)
5756{
5757 return output->output;
5758}
5759
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005760enum wl_output_transform
5761output_get_transform(struct output *output)
5762{
5763 return output->transform;
5764}
5765
Alexander Larssonafd319a2013-05-22 14:41:27 +02005766uint32_t
5767output_get_scale(struct output *output)
5768{
5769 return output->scale;
5770}
5771
Jason Ekstrand738715d2014-04-02 19:53:50 -05005772const char *
5773output_get_make(struct output *output)
5774{
5775 return output->make;
5776}
5777
5778const char *
5779output_get_model(struct output *output)
5780{
5781 return output->model;
5782}
5783
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005784static void
Daniel Stone97f68542012-05-30 16:32:01 +01005785fini_xkb(struct input *input)
5786{
5787 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005788 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005789}
5790
5791static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005792display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005793{
5794 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005795 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005796
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005797 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005798 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005799 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005800 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005801 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005802 input->pointer_focus = NULL;
5803 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005804 input->seat_version = seat_version;
5805
Rusty Lynch041815a2013-08-08 21:20:38 -07005806 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005807 wl_list_insert(d->input_list.prev, &input->link);
5808
Daniel Stone37816df2012-05-16 18:45:18 +01005809 wl_seat_add_listener(input->seat, &seat_listener, input);
5810 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005811
Jason Ekstranda669bd52014-04-02 19:53:51 -05005812 if (d->data_device_manager) {
5813 input->data_device =
5814 wl_data_device_manager_get_data_device(d->data_device_manager,
5815 input->seat);
5816 wl_data_device_add_listener(input->data_device,
5817 &data_device_listener,
5818 input);
5819 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005820
5821 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005822
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005823 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5824 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005825
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005826 set_repeat_info(input, 40, 400);
5827 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5828 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005829}
5830
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005831static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005832input_destroy(struct input *input)
5833{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005834 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005835 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005836
5837 if (input->drag_offer)
5838 data_offer_destroy(input->drag_offer);
5839
5840 if (input->selection_offer)
5841 data_offer_destroy(input->selection_offer);
5842
kabeer khan6ce67ec2014-10-20 11:55:29 +05305843 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005844 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305845 wl_data_device_release(input->data_device);
5846 else
5847 wl_data_device_destroy(input->data_device);
5848 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005849 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005850 if (input->touch)
5851 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005852 if (input->pointer)
5853 wl_pointer_release(input->pointer);
5854 if (input->keyboard)
5855 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005856 } else {
5857 if (input->touch)
5858 wl_touch_destroy(input->touch);
5859 if (input->pointer)
5860 wl_pointer_destroy(input->pointer);
5861 if (input->keyboard)
5862 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005863 }
5864
Daniel Stone97f68542012-05-30 16:32:01 +01005865 fini_xkb(input);
5866
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005867 wl_surface_destroy(input->pointer_surface);
5868
Pekka Paalanene1207c72011-12-16 12:02:09 +02005869 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005870 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005871 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005872 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005873 free(input);
5874}
5875
5876static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005877shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5878{
5879 struct display *d = data;
5880
5881 if (format == WL_SHM_FORMAT_RGB565)
5882 d->has_rgb565 = 1;
5883}
5884
5885struct wl_shm_listener shm_listener = {
5886 shm_format
5887};
5888
5889static void
ant8mef99fac22018-11-28 22:46:37 +01005890xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005891{
ant8mef99fac22018-11-28 22:46:37 +01005892 xdg_wm_base_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005893}
5894
ant8mef99fac22018-11-28 22:46:37 +01005895static const struct xdg_wm_base_listener wm_base_listener = {
5896 xdg_wm_base_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005897};
5898
5899static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005900registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5901 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005902{
5903 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005904 struct global *global;
5905
Brian Lovinbc919262013-08-07 15:34:59 -07005906 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005907 global->name = id;
5908 global->interface = strdup(interface);
5909 global->version = version;
5910 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005911
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005912 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005913 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005914 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005915 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005916 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005917 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005918 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005919 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5920 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5921 d->relative_pointer_manager =
5922 wl_registry_bind(registry, id,
5923 &zwp_relative_pointer_manager_v1_interface,
5924 1);
5925 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5926 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5927 d->pointer_constraints =
5928 wl_registry_bind(registry, id,
5929 &zwp_pointer_constraints_v1_interface,
5930 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005931 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005932 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005933 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005934 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005935 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005936 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005937 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305938 &wl_data_device_manager_interface,
5939 d->data_device_manager_version);
ant8mef99fac22018-11-28 22:46:37 +01005940 } else if (strcmp(interface, "xdg_wm_base") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005941 d->xdg_shell = wl_registry_bind(registry, id,
ant8mef99fac22018-11-28 22:46:37 +01005942 &xdg_wm_base_interface, 1);
5943 xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005944 } else if (strcmp(interface, "text_cursor_position") == 0) {
5945 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005946 wl_registry_bind(registry, id,
5947 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005948 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5949 d->subcompositor =
5950 wl_registry_bind(registry, id,
5951 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005952 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005953
5954 if (d->global_handler)
5955 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005956}
5957
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005958static void
5959registry_handle_global_remove(void *data, struct wl_registry *registry,
5960 uint32_t name)
5961{
5962 struct display *d = data;
5963 struct global *global;
5964 struct global *tmp;
5965
5966 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5967 if (global->name != name)
5968 continue;
5969
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005970 if (strcmp(global->interface, "wl_output") == 0)
5971 display_destroy_output(d, name);
5972
5973 /* XXX: Should destroy remaining bound globals */
5974
5975 if (d->global_handler_remove)
5976 d->global_handler_remove(d, name, global->interface,
5977 global->version, d->user_data);
5978
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005979 wl_list_remove(&global->link);
5980 free(global->interface);
5981 free(global);
5982 }
5983}
5984
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005985void *
5986display_bind(struct display *display, uint32_t name,
5987 const struct wl_interface *interface, uint32_t version)
5988{
5989 return wl_registry_bind(display->registry, name, interface, version);
5990}
5991
5992static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005993 registry_handle_global,
5994 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005995};
5996
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005997#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005998static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005999init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006000{
6001 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006002 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006003
Rob Clark6396ed32012-03-11 19:48:41 -05006004#ifdef USE_CAIRO_GLESV2
6005# define GL_BIT EGL_OPENGL_ES2_BIT
6006#else
6007# define GL_BIT EGL_OPENGL_BIT
6008#endif
6009
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006010 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006011 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006012 EGL_RED_SIZE, 1,
6013 EGL_GREEN_SIZE, 1,
6014 EGL_BLUE_SIZE, 1,
6015 EGL_ALPHA_SIZE, 1,
6016 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006017 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006018 EGL_NONE
6019 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006020
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006021#ifdef USE_CAIRO_GLESV2
6022 static const EGLint context_attribs[] = {
6023 EGL_CONTEXT_CLIENT_VERSION, 2,
6024 EGL_NONE
6025 };
6026 EGLint api = EGL_OPENGL_ES_API;
6027#else
6028 EGLint *context_attribs = NULL;
6029 EGLint api = EGL_OPENGL_API;
6030#endif
6031
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006032 d->dpy =
6033 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6034 d->display, NULL);
6035
Yuval Fledel45568f62010-12-06 09:18:12 -05006036 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006037 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006038 return -1;
6039 }
6040
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006041 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006042 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006043 return -1;
6044 }
6045
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006046 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6047 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006048 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006049 return -1;
6050 }
6051
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006052 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006053 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006054 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006055 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006056 return -1;
6057 }
6058
Benjamin Franzke0c991632011-09-27 21:57:31 +02006059 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6060 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006061 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006062 return -1;
6063 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006064
6065 return 0;
6066}
6067
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006068static void
6069fini_egl(struct display *display)
6070{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006071 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006072
6073 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6074 EGL_NO_CONTEXT);
6075
6076 eglTerminate(display->dpy);
6077 eglReleaseThread();
6078}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006079#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006080
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006081static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006082init_dummy_surface(struct display *display)
6083{
6084 int len;
6085 void *data;
6086
6087 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006088 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006089 display->dummy_surface =
6090 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6091 1, 1, len);
6092 display->dummy_surface_data = data;
6093}
6094
6095static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006096handle_display_data(struct task *task, uint32_t events)
6097{
6098 struct display *display =
6099 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006100 struct epoll_event ep;
6101 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006102
6103 display->display_fd_events = events;
6104
6105 if (events & EPOLLERR || events & EPOLLHUP) {
6106 display_exit(display);
6107 return;
6108 }
6109
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006110 if (events & EPOLLIN) {
6111 ret = wl_display_dispatch(display->display);
6112 if (ret == -1) {
6113 display_exit(display);
6114 return;
6115 }
6116 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006117
6118 if (events & EPOLLOUT) {
6119 ret = wl_display_flush(display->display);
6120 if (ret == 0) {
6121 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6122 ep.data.ptr = &display->display_task;
6123 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6124 display->display_fd, &ep);
6125 } else if (ret == -1 && errno != EAGAIN) {
6126 display_exit(display);
6127 return;
6128 }
6129 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006130}
6131
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006132static void
6133log_handler(const char *format, va_list args)
6134{
6135 vfprintf(stderr, format, args);
6136}
6137
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006138struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006139display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006140{
6141 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006142
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006143 wl_log_set_handler_client(log_handler);
6144
Peter Huttererf3d62272013-08-08 11:57:05 +10006145 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006146 if (d == NULL)
6147 return NULL;
6148
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006149 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006150 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006151 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006152 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006153 return NULL;
6154 }
6155
Rob Bradford5ab9c752013-07-26 16:29:43 +01006156 d->xkb_context = xkb_context_new(0);
6157 if (d->xkb_context == NULL) {
6158 fprintf(stderr, "Failed to create XKB context\n");
6159 free(d);
6160 return NULL;
6161 }
6162
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006163 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006164 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006165 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006166 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6167 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006168
6169 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006170 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006171 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006172 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006173
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006174 d->registry = wl_display_get_registry(d->display);
6175 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006176
Marek Chalupaa519d062014-12-05 13:49:40 +01006177 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006178 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6179 return NULL;
6180 }
6181
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006182#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006183 if (init_egl(d) < 0)
6184 fprintf(stderr, "EGL does not seem to work, "
6185 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006186#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006187
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006188 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006189
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006190 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006191
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006192 wl_list_init(&d->window_list);
6193
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006194 init_dummy_surface(d);
6195
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006196 return d;
6197}
6198
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006199static void
6200display_destroy_outputs(struct display *display)
6201{
6202 struct output *tmp;
6203 struct output *output;
6204
6205 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6206 output_destroy(output);
6207}
6208
Pekka Paalanene1207c72011-12-16 12:02:09 +02006209static void
6210display_destroy_inputs(struct display *display)
6211{
6212 struct input *tmp;
6213 struct input *input;
6214
6215 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6216 input_destroy(input);
6217}
6218
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006219void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006220display_destroy(struct display *display)
6221{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006222 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006223 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6224 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006225
6226 if (!wl_list_empty(&display->deferred_list))
6227 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6228
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006229 cairo_surface_destroy(display->dummy_surface);
6230 free(display->dummy_surface_data);
6231
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006232 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006233 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006234
Daniel Stone97f68542012-05-30 16:32:01 +01006235 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006236
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006237 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006238 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006239
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006240#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006241 if (display->argb_device)
6242 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006243#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006244
Pekka Paalanen35e82632013-04-25 13:57:48 +03006245 if (display->subcompositor)
6246 wl_subcompositor_destroy(display->subcompositor);
6247
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006248 if (display->xdg_shell)
ant8mef99fac22018-11-28 22:46:37 +01006249 xdg_wm_base_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006250
6251 if (display->shm)
6252 wl_shm_destroy(display->shm);
6253
6254 if (display->data_device_manager)
6255 wl_data_device_manager_destroy(display->data_device_manager);
6256
6257 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006258 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006259
6260 close(display->epoll_fd);
6261
U. Artie Eoff44874d92012-10-02 21:12:35 -07006262 if (!(display->display_fd_events & EPOLLERR) &&
6263 !(display->display_fd_events & EPOLLHUP))
6264 wl_display_flush(display->display);
6265
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006266 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006267 free(display);
6268}
6269
6270void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006271display_set_user_data(struct display *display, void *data)
6272{
6273 display->user_data = data;
6274}
6275
6276void *
6277display_get_user_data(struct display *display)
6278{
6279 return display->user_data;
6280}
6281
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006282struct wl_display *
6283display_get_display(struct display *display)
6284{
6285 return display->display;
6286}
6287
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006288int
6289display_has_subcompositor(struct display *display)
6290{
6291 if (display->subcompositor)
6292 return 1;
6293
6294 wl_display_roundtrip(display->display);
6295
6296 return display->subcompositor != NULL;
6297}
6298
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006299cairo_device_t *
6300display_get_cairo_device(struct display *display)
6301{
6302 return display->argb_device;
6303}
6304
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006305struct output *
6306display_get_output(struct display *display)
6307{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006308 if (wl_list_empty(&display->output_list))
6309 return NULL;
6310
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006311 return container_of(display->output_list.next, struct output, link);
6312}
6313
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006314struct wl_compositor *
6315display_get_compositor(struct display *display)
6316{
6317 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006318}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006319
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006320uint32_t
6321display_get_serial(struct display *display)
6322{
6323 return display->serial;
6324}
6325
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006326EGLDisplay
6327display_get_egl_display(struct display *d)
6328{
6329 return d->dpy;
6330}
6331
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006332struct wl_data_source *
6333display_create_data_source(struct display *display)
6334{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006335 if (display->data_device_manager)
6336 return wl_data_device_manager_create_data_source(display->data_device_manager);
6337 else
6338 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006339}
6340
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006341EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006342display_get_argb_egl_config(struct display *d)
6343{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006344 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006345}
6346
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006347int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006348display_acquire_window_surface(struct display *display,
6349 struct window *window,
6350 EGLContext ctx)
6351{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006352 struct surface *surface = window->main_surface;
6353
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006354 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006355 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006356
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006357 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006358 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006359}
6360
6361void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006362display_release_window_surface(struct display *display,
6363 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006364{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006365 struct surface *surface = window->main_surface;
6366
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006367 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006368 return;
6369
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006370 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006371}
6372
6373void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006374display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006375{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006376 wl_list_insert(&display->deferred_list, &task->link);
6377}
6378
6379void
6380display_watch_fd(struct display *display,
6381 int fd, uint32_t events, struct task *task)
6382{
6383 struct epoll_event ep;
6384
6385 ep.events = events;
6386 ep.data.ptr = task;
6387 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6388}
6389
6390void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006391display_unwatch_fd(struct display *display, int fd)
6392{
6393 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6394}
6395
6396void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006397display_run(struct display *display)
6398{
6399 struct task *task;
6400 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006401 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006402
Pekka Paalanen826d7952011-12-15 10:14:07 +02006403 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006404 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006405 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006406 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006407 struct task, link);
6408 wl_list_remove(&task->link);
6409 task->run(task, 0);
6410 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006411
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006412 wl_display_dispatch_pending(display->display);
6413
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006414 if (!display->running)
6415 break;
6416
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006417 ret = wl_display_flush(display->display);
6418 if (ret < 0 && errno == EAGAIN) {
6419 ep[0].events =
6420 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6421 ep[0].data.ptr = &display->display_task;
6422
6423 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6424 display->display_fd, &ep[0]);
6425 } else if (ret < 0) {
6426 break;
6427 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006428
6429 count = epoll_wait(display->epoll_fd,
6430 ep, ARRAY_LENGTH(ep), -1);
6431 for (i = 0; i < count; i++) {
6432 task = ep[i].data.ptr;
6433 task->run(task, ep[i].events);
6434 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006435 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006436}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006437
6438void
6439display_exit(struct display *display)
6440{
6441 display->running = 0;
6442}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006443
Carlos Garnacho9c931792016-01-18 23:52:12 +01006444int
6445display_get_data_device_manager_version(struct display *display)
6446{
6447 return display->data_device_manager_version;
6448}
6449
Jan Arne Petersencd997062012-11-18 19:06:44 +01006450void
6451keysym_modifiers_add(struct wl_array *modifiers_map,
6452 const char *name)
6453{
6454 size_t len = strlen(name) + 1;
6455 char *p;
6456
6457 p = wl_array_add(modifiers_map, len);
6458
6459 if (p == NULL)
6460 return;
6461
6462 strncpy(p, name, len);
6463}
6464
6465static xkb_mod_index_t
6466keysym_modifiers_get_index(struct wl_array *modifiers_map,
6467 const char *name)
6468{
6469 xkb_mod_index_t index = 0;
6470 char *p = modifiers_map->data;
6471
6472 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6473 if (strcmp(p, name) == 0)
6474 return index;
6475
6476 index++;
6477 p += strlen(p) + 1;
6478 }
6479
6480 return XKB_MOD_INVALID;
6481}
6482
6483xkb_mod_mask_t
6484keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6485 const char *name)
6486{
6487 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6488
6489 if (index == XKB_MOD_INVALID)
6490 return XKB_MOD_INVALID;
6491
6492 return 1 << index;
6493}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006494
6495static void
6496toytimer_fire(struct task *tsk, uint32_t events)
6497{
6498 uint64_t e;
6499 struct toytimer *tt;
6500
6501 tt = container_of(tsk, struct toytimer, tsk);
6502
6503 if (events != EPOLLIN)
6504 fprintf(stderr, "unexpected timerfd events %x\n", events);
6505
6506 if (!(events & EPOLLIN))
6507 return;
6508
6509 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6510 /* If we change the timer between the fd becoming
6511 * readable and getting here, there'll be nothing to
6512 * read and we get EAGAIN. */
6513 if (errno != EAGAIN)
6514 fprintf(stderr, "timer read failed: %m\n");
6515 return;
6516 }
6517
6518 tt->callback(tt);
6519}
6520
6521void
6522toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6523 toytimer_cb callback)
6524{
6525 memset(tt, 0, sizeof *tt);
6526
6527 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6528 if (tt->fd == -1) {
6529 fprintf(stderr, "creating timer failed: %m\n");
6530 abort();
6531 }
6532
6533 tt->display = display;
6534 tt->callback = callback;
6535 tt->tsk.run = toytimer_fire;
6536 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6537}
6538
6539void
6540toytimer_fini(struct toytimer *tt)
6541{
6542 display_unwatch_fd(tt->display, tt->fd);
6543 close(tt->fd);
6544 tt->fd = -1;
6545}
6546
6547void
6548toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6549{
6550 int ret;
6551
6552 ret = timerfd_settime(tt->fd, 0, its, NULL);
6553 if (ret < 0) {
6554 fprintf(stderr, "timer setup failed: %m\n");
6555 abort();
6556 }
6557}
6558
6559#define USEC_PER_SEC 1000000
6560
6561void
6562toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6563{
6564 struct itimerspec its;
6565
6566 its.it_interval.tv_sec = 0;
6567 its.it_interval.tv_nsec = 0;
6568 its.it_value.tv_sec = usec / USEC_PER_SEC;
6569 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6570 toytimer_arm(tt, &its);
6571}
6572
6573void
6574toytimer_disarm(struct toytimer *tt)
6575{
6576 struct itimerspec its = {};
6577
6578 toytimer_arm(tt, &its);
6579}