blob: 1baab32e85b33f6ee1354aae08d7607f1f4ba4d4 [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) {
Antonio Borneo39578632019-04-26 23:57:31 +0200743 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
744 size, strerror(errno));
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) {
Antonio Borneo39578632019-04-26 23:57:31 +0200750 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400751 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
-c505af82019-06-26 21:00:43 +00002056 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.85);
2057 cairo_move_to(cr, 10, 17);
2058 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002059 cairo_show_text(cr, tooltip->entry);
2060 cairo_destroy(cr);
2061}
2062
2063static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002064get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002065{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002066 cairo_t *cr;
2067 cairo_text_extents_t extents;
2068
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002069 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002070 * created yet, and parent does not have a valid surface
2071 * outside repaint, either.
2072 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002073 cr = cairo_create(display->dummy_surface);
-c505af82019-06-26 21:00:43 +00002074 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002075 cairo_text_extents(cr, tooltip->entry, &extents);
2076 cairo_destroy(cr);
2077
2078 return extents;
2079}
2080
2081static int
2082window_create_tooltip(struct tooltip *tooltip)
2083{
2084 struct widget *parent = tooltip->parent;
2085 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002086 const int offset_y = 27;
2087 const int margin = 3;
2088 cairo_text_extents_t extents;
2089
2090 if (tooltip->widget)
2091 return 0;
2092
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002093 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002094
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002095 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002096 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002097 widget_set_allocation(tooltip->widget,
2098 tooltip->x, tooltip->y + offset_y,
2099 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002100
2101 return 0;
2102}
2103
2104void
2105widget_destroy_tooltip(struct widget *parent)
2106{
2107 struct tooltip *tooltip = parent->tooltip;
2108
2109 parent->tooltip_count = 0;
2110 if (!tooltip)
2111 return;
2112
2113 if (tooltip->widget) {
2114 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002115 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002116 }
2117
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002118 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002119 free(tooltip->entry);
2120 free(tooltip);
2121 parent->tooltip = NULL;
2122}
2123
2124static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002125tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002126{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002127 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002128
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002129 window_create_tooltip(tooltip);
2130}
2131
2132#define TOOLTIP_TIMEOUT 500
2133static int
2134tooltip_timer_reset(struct tooltip *tooltip)
2135{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002136 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002137
2138 return 0;
2139}
2140
2141int
2142widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2143{
2144 struct tooltip *tooltip = parent->tooltip;
2145
2146 parent->tooltip_count++;
2147 if (tooltip) {
2148 tooltip->x = x;
2149 tooltip->y = y;
2150 tooltip_timer_reset(tooltip);
2151 return 0;
2152 }
2153
2154 /* the handler might be triggered too fast via input device motion, so
2155 * we need this check here to make sure tooltip is fully initialized */
2156 if (parent->tooltip_count > 1)
2157 return 0;
2158
2159 tooltip = malloc(sizeof *tooltip);
2160 if (!tooltip)
2161 return -1;
2162
2163 parent->tooltip = tooltip;
2164 tooltip->parent = parent;
2165 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002166 tooltip->x = x;
2167 tooltip->y = y;
2168 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002169 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2170 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002171 tooltip_timer_reset(tooltip);
2172
2173 return 0;
2174}
2175
2176static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177frame_resize_handler(struct widget *widget,
2178 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002179{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002180 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002181 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002182 struct rectangle interior;
2183 struct rectangle input;
2184 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002185
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002186 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002187 interior.x = 0;
2188 interior.y = 0;
2189 interior.width = width;
2190 interior.height = height;
2191 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002192 frame_resize(frame->frame, width, height);
2193 frame_interior(frame->frame, &interior.x, &interior.y,
2194 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002195 }
2196
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 widget_set_allocation(child, interior.x, interior.y,
2198 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002199
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002200 if (child->resize_handler) {
2201 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002202 child->user_data);
2203
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002204 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002205 width = child->allocation.width;
2206 height = child->allocation.height;
2207 } else {
2208 frame_resize_inside(frame->frame,
2209 child->allocation.width,
2210 child->allocation.height);
2211 width = frame_width(frame->frame);
2212 height = frame_height(frame->frame);
2213 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002214 }
2215
Scott Moreauf7e498c2012-05-14 11:39:29 -06002216 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002217
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 widget->surface->input_region =
2219 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002220 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002221 frame_input_rect(frame->frame, &input.x, &input.y,
2222 &input.width, &input.height);
2223 wl_region_add(widget->surface->input_region,
2224 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002225 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002226 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002227 }
2228
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002229 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002230
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002231 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002232 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002233 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2234 &opaque.width, &opaque.height);
2235
2236 wl_region_add(widget->surface->opaque_region,
2237 opaque.x, opaque.y,
2238 opaque.width, opaque.height);
2239 } else {
2240 wl_region_add(widget->surface->opaque_region,
2241 0, 0, width, height);
2242 }
Martin Minarik1998b152012-05-10 02:04:35 +02002243 }
2244
Martin Minarik1998b152012-05-10 02:04:35 +02002245
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002246 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002247}
2248
2249static void
2250frame_redraw_handler(struct widget *widget, void *data)
2251{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002252 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002253 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002254 struct window *window = widget->window;
2255
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002256 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002257 return;
2258
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002259 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002260
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002261 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002262
2263 cairo_destroy(cr);
2264}
2265
2266static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002267frame_get_pointer_image_for_location(struct window_frame *frame,
2268 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002269{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002270 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002271
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002272 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002273 return CURSOR_LEFT_PTR;
2274
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002275 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002276 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002277 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002278 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002279 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002280 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002281 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002282 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002283 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002284 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002285 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002286 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002287 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002288 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002289 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002290 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002291 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002292 case THEME_LOCATION_EXTERIOR:
2293 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002294 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002295 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002296 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002297}
2298
Pekka Paalanen26237862014-09-10 15:10:30 +03002299static void
2300frame_menu_func(void *data, struct input *input, int index)
2301{
2302 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002303
2304 switch (index) {
2305 case 0: /* close */
2306 window_close(window);
2307 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002308 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002309 /* we don't have a way to get out of fullscreen for now */
2310 if (window->fullscreen_handler)
2311 window->fullscreen_handler(window, window->user_data);
2312 break;
2313 }
2314}
2315
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002316void
2317window_show_frame_menu(struct window *window,
2318 struct input *input, uint32_t time)
2319{
2320 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002321 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002322
Pekka Paalanen26237862014-09-10 15:10:30 +03002323 static const char *entries[] = {
2324 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002325 "Fullscreen"
2326 };
2327
2328 if (window->fullscreen_handler)
2329 count = ARRAY_LENGTH(entries);
2330 else
2331 count = ARRAY_LENGTH(entries) - 1;
2332
2333 input_get_position(input, &x, &y);
2334 window_show_menu(window->display, input, time, window,
2335 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002336}
2337
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002338static int
2339frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002340 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002342 struct window_frame *frame = data;
2343 enum theme_location location;
2344
2345 location = frame_pointer_enter(frame->frame, input, x, y);
2346 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2347 widget_schedule_redraw(frame->widget);
2348
2349 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002350}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002352static int
2353frame_motion_handler(struct widget *widget,
2354 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002355 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002356{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002357 struct window_frame *frame = data;
2358 enum theme_location location;
2359
2360 location = frame_pointer_motion(frame->frame, input, x, y);
2361 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2362 widget_schedule_redraw(frame->widget);
2363
2364 return frame_get_pointer_image_for_location(data, location);
2365}
2366
2367static void
2368frame_leave_handler(struct widget *widget,
2369 struct input *input, void *data)
2370{
2371 struct window_frame *frame = data;
2372
2373 frame_pointer_leave(frame->frame, input);
2374 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2375 widget_schedule_redraw(frame->widget);
2376}
2377
2378static void
2379frame_handle_status(struct window_frame *frame, struct input *input,
2380 uint32_t time, enum theme_location location)
2381{
2382 struct window *window = frame->widget->window;
2383 uint32_t status;
2384
2385 status = frame_status(frame->frame);
2386 if (status & FRAME_STATUS_REPAINT)
2387 widget_schedule_redraw(frame->widget);
2388
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002389 if (status & FRAME_STATUS_MINIMIZE) {
2390 window_set_minimized(window);
2391 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2392 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002393
2394 if (status & FRAME_STATUS_MENU) {
2395 window_show_frame_menu(window, input, time);
2396 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2397 }
2398
2399 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002400 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002401 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2402 }
2403
2404 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002405 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002406 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002407 }
2408
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002409 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002410 input_ungrab(input);
ant8mef99fac22018-11-28 22:46:37 +01002411 xdg_toplevel_move(window->xdg_toplevel,
2412 input_get_seat(input),
2413 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002414
2415 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2416 }
2417
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002418 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002419 input_ungrab(input);
2420
ant8mef99fac22018-11-28 22:46:37 +01002421 xdg_toplevel_resize(window->xdg_toplevel,
2422 input_get_seat(input),
2423 window->display->serial,
2424 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002425
2426 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2427 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002428}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002429
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002430#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002431static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002432frame_button_handler(struct widget *widget,
2433 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002434 uint32_t button, enum wl_pointer_button_state state,
2435 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002436
2437{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002438 struct window_frame *frame = data;
2439 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002440
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002441 frame->double_click = 0;
2442 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2443 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2444 frame->double_click = 1;
2445 frame->did_double = 1;
2446 } else
2447 frame->did_double = 0;
2448
2449 frame->last_time = time;
2450 } else if (frame->did_double == 1) {
2451 frame->double_click = 1;
2452 frame->did_double = 0;
2453 }
2454
2455 if (frame->double_click)
2456 location = frame_double_click(frame->frame, input,
2457 button, state);
2458 else
2459 location = frame_pointer_button(frame->frame, input,
2460 button, state);
2461
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002462 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002463}
2464
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002465static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002466frame_touch_down_handler(struct widget *widget, struct input *input,
2467 uint32_t serial, uint32_t time, int32_t id,
2468 float x, float y, void *data)
2469{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002470 struct window_frame *frame = data;
2471
Xiong Zhang382de462014-06-12 11:06:26 +08002472 frame->double_click = 0;
2473 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2474 frame->last_id == id) {
2475 frame->double_click = 1;
2476 frame->did_double = 1;
2477 frame->double_id = id;
2478 } else
2479 frame->did_double = 0;
2480
2481 frame->last_time = time;
2482 frame->last_id = id;
2483
2484 if (frame->double_click)
2485 frame_double_touch_down(frame->frame, input, id, x, y);
2486 else
2487 frame_touch_down(frame->frame, input, id, x, y);
2488
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002489 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2490}
2491
2492static void
2493frame_touch_up_handler(struct widget *widget,
2494 struct input *input, uint32_t serial, uint32_t time,
2495 int32_t id, void *data)
2496{
2497 struct window_frame *frame = data;
2498
Xiong Zhang382de462014-06-12 11:06:26 +08002499 if (frame->double_id == id && frame->did_double) {
2500 frame->did_double = 0;
2501 frame->double_id = 0;
2502 frame_double_touch_up(frame->frame, input, id);
2503 } else
2504 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002505 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002506}
2507
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002508struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002509window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002510{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002511 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002512 uint32_t buttons;
2513
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002514 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002515 buttons = FRAME_BUTTON_NONE;
2516 } else {
2517 buttons = FRAME_BUTTON_ALL;
2518 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002519
Peter Huttererf3d62272013-08-08 11:57:05 +10002520 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002521 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002522 buttons, window->title, NULL);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002523
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524 frame->widget = window_add_widget(window, frame);
2525 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002526
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002527 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2528 widget_set_resize_handler(frame->widget, frame_resize_handler);
2529 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002530 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002531 widget_set_motion_handler(frame->widget, frame_motion_handler);
2532 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002533 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002534 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002535
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002536 window->frame = frame;
2537
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002538 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002539}
2540
Kristian Høgsberga1627922012-06-20 17:30:03 -04002541void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002542window_frame_set_child_size(struct widget *widget, int child_width,
2543 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002544{
2545 struct display *display = widget->window->display;
2546 struct theme *t = display->theme;
2547 int decoration_width, decoration_height;
2548 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002549 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002550
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002551 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002552 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002553 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002554 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002555
2556 width = child_width + decoration_width;
2557 height = child_height + decoration_height;
2558 } else {
2559 width = child_width;
2560 height = child_height;
2561 }
2562
2563 window_schedule_resize(widget->window, width, height);
2564}
2565
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002566static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002567window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002568{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002569 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002570
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002571 /* frame->child must be destroyed by the application */
2572 widget_destroy(frame->widget);
2573 free(frame);
2574}
2575
2576static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002577input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002578 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002579{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002580 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002581 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002582
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002583 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002584 return;
2585
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002586 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002587 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002588 widget = old;
2589 if (input->grab)
2590 widget = input->grab;
2591 if (widget->leave_handler)
2592 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002593 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002594 }
2595
2596 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002597 widget = focus;
2598 if (input->grab)
2599 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002600 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002601 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002602 cursor = widget->enter_handler(focus, input, x, y,
2603 widget->user_data);
2604 else
2605 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002606
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002607 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002608 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002609}
2610
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002611void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002612touch_grab(struct input *input, int32_t touch_id)
2613{
2614 input->touch_grab = 1;
2615 input->touch_grab_id = touch_id;
2616}
2617
2618void
2619touch_ungrab(struct input *input)
2620{
2621 struct touch_point *tp, *tmp;
2622
2623 input->touch_grab = 0;
2624
2625 wl_list_for_each_safe(tp, tmp,
2626 &input->touch_point_list, link) {
2627 if (tp->id != input->touch_grab_id)
2628 continue;
2629 wl_list_remove(&tp->link);
2630 free(tp);
2631
2632 return;
2633 }
2634}
2635
2636void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002637input_grab(struct input *input, struct widget *widget, uint32_t button)
2638{
2639 input->grab = widget;
2640 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002641
2642 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002643}
2644
2645void
2646input_ungrab(struct input *input)
2647{
2648 struct widget *widget;
2649
2650 input->grab = NULL;
2651 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002652 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002653 input->sx, input->sy);
2654 input_set_focus_widget(input, widget, input->sx, input->sy);
2655 }
2656}
2657
2658static void
Derek Foreman118a4292015-04-22 17:23:35 -05002659cursor_delay_timer_reset(struct input *input, uint32_t duration)
2660{
Derek Foreman118a4292015-04-22 17:23:35 -05002661 if (!duration)
2662 input->cursor_timer_running = false;
2663 else
2664 input->cursor_timer_running = true;
2665
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002666 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002667}
2668
2669static void cancel_pointer_image_update(struct input *input)
2670{
2671 if (input->cursor_timer_running)
2672 cursor_delay_timer_reset(input, 0);
2673}
2674
2675static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002676input_remove_pointer_focus(struct input *input)
2677{
2678 struct window *window = input->pointer_focus;
2679
2680 if (!window)
2681 return;
2682
2683 input_set_focus_widget(input, NULL, 0, 0);
2684
2685 input->pointer_focus = NULL;
2686 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002687 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002688}
2689
2690static void
2691pointer_handle_enter(void *data, struct wl_pointer *pointer,
2692 uint32_t serial, struct wl_surface *surface,
2693 wl_fixed_t sx_w, wl_fixed_t sy_w)
2694{
2695 struct input *input = data;
2696 struct window *window;
2697 struct widget *widget;
2698 float sx = wl_fixed_to_double(sx_w);
2699 float sy = wl_fixed_to_double(sy_w);
2700
2701 if (!surface) {
2702 /* enter event for a window we've just destroyed */
2703 return;
2704 }
2705
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002706 window = wl_surface_get_user_data(surface);
2707 if (surface != window->main_surface->surface) {
2708 DBG("Ignoring input event from subsurface %p\n", surface);
2709 return;
2710 }
2711
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002712 input->display->serial = serial;
2713 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002714 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002715
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002716 input->sx = sx;
2717 input->sy = sy;
2718
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002719 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002720 input_set_focus_widget(input, widget, sx, sy);
2721}
2722
2723static void
2724pointer_handle_leave(void *data, struct wl_pointer *pointer,
2725 uint32_t serial, struct wl_surface *surface)
2726{
2727 struct input *input = data;
2728
2729 input->display->serial = serial;
2730 input_remove_pointer_focus(input);
2731}
2732
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002733static void
Daniel Stone37816df2012-05-16 18:45:18 +01002734pointer_handle_motion(void *data, struct wl_pointer *pointer,
2735 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002736{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002737 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002738 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002739 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002740 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002741 float sx = wl_fixed_to_double(sx_w);
2742 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002743
Paul Winwoodb22bf572013-08-29 10:52:54 +01002744 if (!window)
2745 return;
2746
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002747 input->sx = sx;
2748 input->sy = sy;
2749
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002750 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002751 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002752 * based on the old surface dimensions. However, if we have an active
2753 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002754 */
Derek Foreman46812b62015-08-26 17:13:27 -05002755 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002756 sy < window->main_surface->allocation.y ||
2757 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002758 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002759 return;
2760
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002761 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002762 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002763 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002764 }
2765
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002766 if (input->grab)
2767 widget = input->grab;
2768 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002769 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002770 if (widget) {
2771 if (widget->motion_handler)
2772 cursor = widget->motion_handler(input->focus_widget,
2773 input, time, sx, sy,
2774 widget->user_data);
2775 else
2776 cursor = widget->default_cursor;
2777 } else
2778 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002779
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002780 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002781}
2782
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002783static void
Daniel Stone37816df2012-05-16 18:45:18 +01002784pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002785 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002786{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002787 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002788 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002789 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002790
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002791 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002792 if (input->focus_widget && input->grab == NULL &&
2793 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002794 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002795
Neil Roberts6b28aad2012-01-23 19:11:18 +00002796 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002797 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002798 (*widget->button_handler)(widget,
2799 input, time,
2800 button, state,
2801 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002802
Daniel Stone4dbadb12012-05-30 16:31:51 +01002803 if (input->grab && input->grab_button == button &&
2804 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002805 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002806}
2807
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002808static void
Daniel Stone37816df2012-05-16 18:45:18 +01002809pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002810 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002811{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002812 struct input *input = data;
2813 struct widget *widget;
2814
2815 widget = input->focus_widget;
2816 if (input->grab)
2817 widget = input->grab;
2818 if (widget && widget->axis_handler)
2819 (*widget->axis_handler)(widget,
2820 input, time,
2821 axis, value,
2822 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002823}
2824
Peter Hutterer87743e92016-01-18 16:38:22 +10002825static void
2826pointer_handle_frame(void *data, struct wl_pointer *pointer)
2827{
2828 struct input *input = data;
2829 struct widget *widget;
2830
2831 widget = input->focus_widget;
2832 if (input->grab)
2833 widget = input->grab;
2834 if (widget && widget->pointer_frame_handler)
2835 (*widget->pointer_frame_handler)(widget,
2836 input,
2837 widget->user_data);
2838}
2839
2840static void
2841pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2842 uint32_t source)
2843{
2844 struct input *input = data;
2845 struct widget *widget;
2846
2847 widget = input->focus_widget;
2848 if (input->grab)
2849 widget = input->grab;
2850 if (widget && widget->axis_source_handler)
2851 (*widget->axis_source_handler)(widget,
2852 input,
2853 source,
2854 widget->user_data);
2855}
2856
2857static void
2858pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2859 uint32_t time, uint32_t axis)
2860{
2861 struct input *input = data;
2862 struct widget *widget;
2863
2864 widget = input->focus_widget;
2865 if (input->grab)
2866 widget = input->grab;
2867 if (widget && widget->axis_stop_handler)
2868 (*widget->axis_stop_handler)(widget,
2869 input, time,
2870 axis,
2871 widget->user_data);
2872}
2873
2874static void
2875pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2876 uint32_t axis, int32_t discrete)
2877{
2878 struct input *input = data;
2879 struct widget *widget;
2880
2881 widget = input->focus_widget;
2882 if (input->grab)
2883 widget = input->grab;
2884 if (widget && widget->axis_discrete_handler)
2885 (*widget->axis_discrete_handler)(widget,
2886 input,
2887 axis,
2888 discrete,
2889 widget->user_data);
2890}
2891
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002892static const struct wl_pointer_listener pointer_listener = {
2893 pointer_handle_enter,
2894 pointer_handle_leave,
2895 pointer_handle_motion,
2896 pointer_handle_button,
2897 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002898 pointer_handle_frame,
2899 pointer_handle_axis_source,
2900 pointer_handle_axis_stop,
2901 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002902};
2903
2904static void
2905input_remove_keyboard_focus(struct input *input)
2906{
2907 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002908
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002909 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002910
2911 if (!window)
2912 return;
2913
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002914 if (window->keyboard_focus_handler)
2915 (*window->keyboard_focus_handler)(window, NULL,
2916 window->user_data);
2917
2918 input->keyboard_focus = NULL;
2919}
2920
2921static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002922keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002923{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002924 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002925 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002926
2927 if (window && window->key_handler) {
2928 (*window->key_handler)(window, input, input->repeat_time,
2929 input->repeat_key, input->repeat_sym,
2930 WL_KEYBOARD_KEY_STATE_PRESSED,
2931 window->user_data);
2932 }
2933}
2934
2935static void
2936keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2937 uint32_t format, int fd, uint32_t size)
2938{
2939 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002940 struct xkb_keymap *keymap;
2941 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002942#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002943 struct xkb_compose_table *compose_table;
2944 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002945#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002946 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002947 char *map_str;
2948
2949 if (!data) {
2950 close(fd);
2951 return;
2952 }
2953
2954 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2955 close(fd);
2956 return;
2957 }
2958
2959 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2960 if (map_str == MAP_FAILED) {
2961 close(fd);
2962 return;
2963 }
2964
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002965 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03002966 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2967 map_str,
2968 XKB_KEYMAP_FORMAT_TEXT_V1,
2969 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002970 munmap(map_str, size);
2971 close(fd);
2972
Rui Matos3eccb862013-10-10 19:44:22 +02002973 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002974 fprintf(stderr, "failed to compile keymap\n");
2975 return;
2976 }
2977
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002978 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02002979 state = xkb_state_new(keymap);
2980 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002981 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002982 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002983 return;
2984 }
2985
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002986 /* Look up the preferred locale, falling back to "C" as default */
2987 if (!(locale = getenv("LC_ALL")))
2988 if (!(locale = getenv("LC_CTYPE")))
2989 if (!(locale = getenv("LANG")))
2990 locale = "C";
2991
2992 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01002993#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002994 compose_table =
2995 xkb_compose_table_new_from_locale(input->display->xkb_context,
2996 locale,
2997 XKB_COMPOSE_COMPILE_NO_FLAGS);
2998 if (compose_table) {
2999 /* Set up XKB compose state */
3000 compose_state = xkb_compose_state_new(compose_table,
3001 XKB_COMPOSE_STATE_NO_FLAGS);
3002 if (compose_state) {
3003 xkb_compose_state_unref(input->xkb.compose_state);
3004 xkb_compose_table_unref(input->xkb.compose_table);
3005 input->xkb.compose_state = compose_state;
3006 input->xkb.compose_table = compose_table;
3007 } else {
3008 fprintf(stderr, "could not create XKB compose state. "
3009 "Disabiling compose.\n");
3010 xkb_compose_table_unref(compose_table);
3011 compose_table = NULL;
3012 }
3013 } else {
3014 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3015 "Disabiling compose\n", locale);
3016 }
Daniel Stone5b015962016-10-20 14:45:58 +01003017#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003018
Rui Matos3eccb862013-10-10 19:44:22 +02003019 xkb_keymap_unref(input->xkb.keymap);
3020 xkb_state_unref(input->xkb.state);
3021 input->xkb.keymap = keymap;
3022 input->xkb.state = state;
3023
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003024 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003025 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003026 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003027 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003028 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003029 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003030}
3031
3032static void
3033keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3034 uint32_t serial, struct wl_surface *surface,
3035 struct wl_array *keys)
3036{
3037 struct input *input = data;
3038 struct window *window;
3039
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003040 if (!surface) {
3041 /* enter event for a window we've just destroyed */
3042 return;
3043 }
3044
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003045 input->display->serial = serial;
3046 input->keyboard_focus = wl_surface_get_user_data(surface);
3047
3048 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003049 if (window->keyboard_focus_handler)
3050 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003051 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003052}
3053
3054static void
3055keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3056 uint32_t serial, struct wl_surface *surface)
3057{
3058 struct input *input = data;
3059
3060 input->display->serial = serial;
3061 input_remove_keyboard_focus(input);
3062}
3063
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003064/* Translate symbols appropriately if a compose sequence is being entered */
3065static xkb_keysym_t
3066process_key_press(xkb_keysym_t sym, struct input *input)
3067{
Daniel Stone5b015962016-10-20 14:45:58 +01003068#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003069 if (!input->xkb.compose_state)
3070 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003071 if (sym == XKB_KEY_NoSymbol)
3072 return sym;
3073 if (xkb_compose_state_feed(input->xkb.compose_state,
3074 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3075 return sym;
3076
3077 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3078 case XKB_COMPOSE_COMPOSING:
3079 return XKB_KEY_NoSymbol;
3080 case XKB_COMPOSE_COMPOSED:
3081 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3082 case XKB_COMPOSE_CANCELLED:
3083 return XKB_KEY_NoSymbol;
3084 case XKB_COMPOSE_NOTHING:
3085 return sym;
3086 default:
3087 return sym;
3088 }
Daniel Stone5b015962016-10-20 14:45:58 +01003089#else
3090 return sym;
3091#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003092}
3093
Scott Moreau210d0792012-03-22 10:47:01 -06003094static void
Daniel Stone37816df2012-05-16 18:45:18 +01003095keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003096 uint32_t serial, uint32_t time, uint32_t key,
3097 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003098{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003099 struct input *input = data;
3100 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003101 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003102 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003103 const xkb_keysym_t *syms;
3104 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003105 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003106
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003107 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003108 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003109 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003110 return;
3111
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003112 /* We only use input grabs for pointer events for now, so just
3113 * ignore key presses if a grab is active. We expand the key
3114 * event delivery mechanism to route events to widgets to
3115 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003116 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003117 if (input->grab && input->grab_button == 0)
3118 return;
3119
Ran Benita2e1968f2014-08-19 23:59:51 +03003120 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003121
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003122 sym = XKB_KEY_NoSymbol;
3123 if (num_syms == 1)
3124 sym = syms[0];
3125
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003126
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003127 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003128 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003129 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003130 } else if (sym == XKB_KEY_F11 &&
3131 window->fullscreen_handler &&
3132 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3133 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003134 } else if (sym == XKB_KEY_F4 &&
3135 input->modifiers == MOD_ALT_MASK &&
3136 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003137 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003138 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003139 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3140 sym = process_key_press(sym, input);
3141
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003142 (*window->key_handler)(window, input, time, key,
3143 sym, state, window->user_data);
3144 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003145
3146 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3147 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003148 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003149 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3150 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003151 input->repeat_sym = sym;
3152 input->repeat_key = key;
3153 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003154 its.it_interval.tv_sec = input->repeat_rate_sec;
3155 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3156 its.it_value.tv_sec = input->repeat_delay_sec;
3157 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003158 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003159 }
3160}
3161
3162static void
Daniel Stone351eb612012-05-31 15:27:47 -04003163keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3164 uint32_t serial, uint32_t mods_depressed,
3165 uint32_t mods_latched, uint32_t mods_locked,
3166 uint32_t group)
3167{
3168 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003169 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003170
Matt Ropere61561f2013-06-24 16:52:43 +01003171 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3172 if (!input->xkb.keymap)
3173 return;
3174
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003175 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3176 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003177 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003178 XKB_STATE_MODS_DEPRESSED |
3179 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003180 input->modifiers = 0;
3181 if (mask & input->xkb.control_mask)
3182 input->modifiers |= MOD_CONTROL_MASK;
3183 if (mask & input->xkb.alt_mask)
3184 input->modifiers |= MOD_ALT_MASK;
3185 if (mask & input->xkb.shift_mask)
3186 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003187}
3188
Jonny Lamb06959082014-08-12 14:58:27 +02003189static void
3190set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3191{
3192 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3193 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3194
3195 /* a rate of zero disables any repeating, regardless of the delay's
3196 * value */
3197 if (rate == 0)
3198 return;
3199
3200 if (rate == 1)
3201 input->repeat_rate_sec = 1;
3202 else
3203 input->repeat_rate_nsec = 1000000000 / rate;
3204
3205 input->repeat_delay_sec = delay / 1000;
3206 delay -= (input->repeat_delay_sec * 1000);
3207 input->repeat_delay_nsec = delay * 1000 * 1000;
3208}
3209
3210static void
3211keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3212 int32_t rate, int32_t delay)
3213{
3214 struct input *input = data;
3215
3216 set_repeat_info(input, rate, delay);
3217}
3218
Daniel Stone37816df2012-05-16 18:45:18 +01003219static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003220 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003221 keyboard_handle_enter,
3222 keyboard_handle_leave,
3223 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003224 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003225 keyboard_handle_repeat_info
3226
Daniel Stone37816df2012-05-16 18:45:18 +01003227};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003228
3229static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003230touch_handle_down(void *data, struct wl_touch *wl_touch,
3231 uint32_t serial, uint32_t time, struct wl_surface *surface,
3232 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3233{
3234 struct input *input = data;
3235 struct widget *widget;
3236 float sx = wl_fixed_to_double(x_w);
3237 float sy = wl_fixed_to_double(y_w);
3238
Rusty Lynch1084da52013-08-15 09:10:08 -07003239 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003240 input->touch_focus = wl_surface_get_user_data(surface);
3241 if (!input->touch_focus) {
3242 DBG("Failed to find to touch focus for surface %p\n", surface);
3243 return;
3244 }
3245
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003246 if (surface != input->touch_focus->main_surface->surface) {
3247 DBG("Ignoring input event from subsurface %p\n", surface);
3248 input->touch_focus = NULL;
3249 return;
3250 }
3251
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003252 if (input->grab)
3253 widget = input->grab;
3254 else
3255 widget = window_find_widget(input->touch_focus,
3256 wl_fixed_to_double(x_w),
3257 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003258 if (widget) {
3259 struct touch_point *tp = xmalloc(sizeof *tp);
3260 if (tp) {
3261 tp->id = id;
3262 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003263 tp->x = sx;
3264 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003265 wl_list_insert(&input->touch_point_list, &tp->link);
3266
3267 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003268 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003269 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003270 sx, sy,
3271 widget->user_data);
3272 }
3273 }
3274}
3275
3276static void
3277touch_handle_up(void *data, struct wl_touch *wl_touch,
3278 uint32_t serial, uint32_t time, int32_t id)
3279{
3280 struct input *input = data;
3281 struct touch_point *tp, *tmp;
3282
Rusty Lynch041815a2013-08-08 21:20:38 -07003283 if (!input->touch_focus) {
3284 DBG("No touch focus found for touch up event!\n");
3285 return;
3286 }
3287
3288 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3289 if (tp->id != id)
3290 continue;
3291
3292 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003293 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003294 time, id,
3295 tp->widget->user_data);
3296
3297 wl_list_remove(&tp->link);
3298 free(tp);
3299
3300 return;
3301 }
3302}
3303
3304static void
3305touch_handle_motion(void *data, struct wl_touch *wl_touch,
3306 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3307{
3308 struct input *input = data;
3309 struct touch_point *tp;
3310 float sx = wl_fixed_to_double(x_w);
3311 float sy = wl_fixed_to_double(y_w);
3312
3313 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3314
3315 if (!input->touch_focus) {
3316 DBG("No touch focus found for touch motion event!\n");
3317 return;
3318 }
3319
3320 wl_list_for_each(tp, &input->touch_point_list, link) {
3321 if (tp->id != id)
3322 continue;
3323
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003324 tp->x = sx;
3325 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003326 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003327 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003328 id, sx, sy,
3329 tp->widget->user_data);
3330 return;
3331 }
3332}
3333
3334static void
3335touch_handle_frame(void *data, struct wl_touch *wl_touch)
3336{
3337 struct input *input = data;
3338 struct touch_point *tp, *tmp;
3339
3340 DBG("touch_handle_frame\n");
3341
3342 if (!input->touch_focus) {
3343 DBG("No touch focus found for touch frame event!\n");
3344 return;
3345 }
3346
3347 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3348 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003349 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003350 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003351 }
3352}
3353
3354static void
3355touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3356{
3357 struct input *input = data;
3358 struct touch_point *tp, *tmp;
3359
3360 DBG("touch_handle_cancel\n");
3361
3362 if (!input->touch_focus) {
3363 DBG("No touch focus found for touch cancel event!\n");
3364 return;
3365 }
3366
3367 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3368 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003369 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3370 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003371
3372 wl_list_remove(&tp->link);
3373 free(tp);
3374 }
3375}
3376
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003377void touch_handle_shape(void *data, struct wl_touch *wl_touch, int32_t id,
3378 wl_fixed_t major, wl_fixed_t minor)
3379{
3380}
3381
3382void touch_handle_orientation(void *data, struct wl_touch *wl_touch, int32_t id,
3383 wl_fixed_t orientation)
3384{
3385}
3386
Rusty Lynch041815a2013-08-08 21:20:38 -07003387static const struct wl_touch_listener touch_listener = {
3388 touch_handle_down,
3389 touch_handle_up,
3390 touch_handle_motion,
3391 touch_handle_frame,
3392 touch_handle_cancel,
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003393 touch_handle_shape,
3394 touch_handle_orientation,
Rusty Lynch041815a2013-08-08 21:20:38 -07003395};
3396
3397static void
Daniel Stone37816df2012-05-16 18:45:18 +01003398seat_handle_capabilities(void *data, struct wl_seat *seat,
3399 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003400{
Daniel Stone37816df2012-05-16 18:45:18 +01003401 struct input *input = data;
3402
3403 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3404 input->pointer = wl_seat_get_pointer(seat);
3405 wl_pointer_set_user_data(input->pointer, input);
3406 wl_pointer_add_listener(input->pointer, &pointer_listener,
3407 input);
3408 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003409 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003410 wl_pointer_release(input->pointer);
3411 else
3412 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003413 input->pointer = NULL;
3414 }
3415
3416 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3417 input->keyboard = wl_seat_get_keyboard(seat);
3418 wl_keyboard_set_user_data(input->keyboard, input);
3419 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3420 input);
3421 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003422 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003423 wl_keyboard_release(input->keyboard);
3424 else
3425 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003426 input->keyboard = NULL;
3427 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003428
3429 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3430 input->touch = wl_seat_get_touch(seat);
3431 wl_touch_set_user_data(input->touch, input);
3432 wl_touch_add_listener(input->touch, &touch_listener, input);
3433 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003434 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003435 wl_touch_release(input->touch);
3436 else
3437 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003438 input->touch = NULL;
3439 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003440}
3441
Rob Bradford08031182013-08-13 20:11:03 +01003442static void
3443seat_handle_name(void *data, struct wl_seat *seat,
3444 const char *name)
3445{
3446
3447}
3448
Daniel Stone37816df2012-05-16 18:45:18 +01003449static const struct wl_seat_listener seat_listener = {
3450 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003451 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003452};
3453
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003454void
3455input_get_position(struct input *input, int32_t *x, int32_t *y)
3456{
3457 *x = input->sx;
3458 *y = input->sy;
3459}
3460
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003461int
3462input_get_touch(struct input *input, int32_t id, float *x, float *y)
3463{
3464 struct touch_point *tp;
3465
3466 wl_list_for_each(tp, &input->touch_point_list, link) {
3467 if (tp->id != id)
3468 continue;
3469
3470 *x = tp->x;
3471 *y = tp->y;
3472 return 0;
3473 }
3474
3475 return -1;
3476}
3477
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003478struct display *
3479input_get_display(struct input *input)
3480{
3481 return input->display;
3482}
3483
Daniel Stone37816df2012-05-16 18:45:18 +01003484struct wl_seat *
3485input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003486{
Daniel Stone37816df2012-05-16 18:45:18 +01003487 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003488}
3489
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003490uint32_t
3491input_get_modifiers(struct input *input)
3492{
3493 return input->modifiers;
3494}
3495
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003496struct widget *
3497input_get_focus_widget(struct input *input)
3498{
3499 return input->focus_widget;
3500}
3501
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003502struct data_offer {
3503 struct wl_data_offer *offer;
3504 struct input *input;
3505 struct wl_array types;
3506 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003507
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003508 struct task io_task;
3509 int fd;
3510 data_func_t func;
3511 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003512 uint32_t dnd_action;
3513 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003514 void *user_data;
3515};
3516
3517static void
3518data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3519{
3520 struct data_offer *offer = data;
3521 char **p;
3522
3523 p = wl_array_add(&offer->types, sizeof *p);
3524 *p = strdup(type);
3525}
3526
Carlos Garnacho9c931792016-01-18 23:52:12 +01003527static void
3528data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3529{
3530 struct data_offer *offer = data;
3531
3532 offer->source_actions = source_actions;
3533}
3534
3535static void
3536data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3537{
3538 struct data_offer *offer = data;
3539
3540 offer->dnd_action = dnd_action;
3541}
3542
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003543static const struct wl_data_offer_listener data_offer_listener = {
3544 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003545 data_offer_source_actions,
3546 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003547};
3548
3549static void
3550data_offer_destroy(struct data_offer *offer)
3551{
3552 char **p;
3553
3554 offer->refcount--;
3555 if (offer->refcount == 0) {
3556 wl_data_offer_destroy(offer->offer);
3557 for (p = offer->types.data; *p; p++)
3558 free(*p);
3559 wl_array_release(&offer->types);
3560 free(offer);
3561 }
3562}
3563
3564static void
3565data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003566 struct wl_data_device *data_device,
3567 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003568{
3569 struct data_offer *offer;
3570
Brian Lovinbc919262013-08-07 15:34:59 -07003571 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003572
3573 wl_array_init(&offer->types);
3574 offer->refcount = 1;
3575 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003576 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003577 wl_data_offer_add_listener(offer->offer,
3578 &data_offer_listener, offer);
3579}
3580
3581static void
3582data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003583 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003584 wl_fixed_t x_w, wl_fixed_t y_w,
3585 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003586{
3587 struct input *input = data;
3588 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003589 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003590 float x = wl_fixed_to_double(x_w);
3591 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003592 char **p;
3593
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003594 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003595 input->drag_enter_serial = serial;
3596 input->drag_focus = window,
3597 input->drag_x = x;
3598 input->drag_y = y;
3599
3600 if (!input->touch_grab)
3601 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003602
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003603 if (offer) {
3604 input->drag_offer = wl_data_offer_get_user_data(offer);
3605
3606 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3607 *p = NULL;
3608
3609 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003610
3611 if (input->display->data_device_manager_version >=
3612 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3613 wl_data_offer_set_actions(offer,
3614 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3615 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3616 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3617 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003618 } else {
3619 input->drag_offer = NULL;
3620 types_data = NULL;
3621 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003622
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003623 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003624 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003625 window->user_data);
3626}
3627
3628static void
3629data_device_leave(void *data, struct wl_data_device *data_device)
3630{
3631 struct input *input = data;
3632
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003633 if (input->drag_offer) {
3634 data_offer_destroy(input->drag_offer);
3635 input->drag_offer = NULL;
3636 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003637}
3638
3639static void
3640data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003641 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003642{
3643 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003644 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003645 float x = wl_fixed_to_double(x_w);
3646 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003647 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003648
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003649 input->drag_x = x;
3650 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003651
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003652 if (input->drag_offer)
3653 types_data = input->drag_offer->types.data;
3654 else
3655 types_data = NULL;
3656
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003657 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003658 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003659 window->user_data);
3660}
3661
3662static void
3663data_device_drop(void *data, struct wl_data_device *data_device)
3664{
3665 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003666 struct window *window = input->drag_focus;
3667 float x, y;
3668
3669 x = input->drag_x;
3670 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003671
3672 if (window->drop_handler)
3673 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003674 x, y, window->user_data);
3675
3676 if (input->touch_grab)
3677 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003678}
3679
3680static void
3681data_device_selection(void *data,
3682 struct wl_data_device *wl_data_device,
3683 struct wl_data_offer *offer)
3684{
3685 struct input *input = data;
3686 char **p;
3687
3688 if (input->selection_offer)
3689 data_offer_destroy(input->selection_offer);
3690
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003691 if (offer) {
3692 input->selection_offer = wl_data_offer_get_user_data(offer);
3693 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3694 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003695 } else {
3696 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003697 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003698}
3699
3700static const struct wl_data_device_listener data_device_listener = {
3701 data_device_data_offer,
3702 data_device_enter,
3703 data_device_leave,
3704 data_device_motion,
3705 data_device_drop,
3706 data_device_selection
3707};
3708
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003709static void
3710input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003711{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003712 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003713 struct wl_cursor *cursor;
3714 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003715
Daniel Stone80972742012-11-07 17:51:39 +11003716 if (!input->pointer)
3717 return;
3718
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003719 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003720 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003721 return;
3722
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003723 if (index >= (int) cursor->image_count) {
3724 fprintf(stderr, "cursor index out of range\n");
3725 return;
3726 }
3727
3728 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003729 buffer = wl_cursor_image_get_buffer(image);
3730 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003731 return;
3732
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003733 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3734 wl_surface_damage(input->pointer_surface, 0, 0,
3735 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003736 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003737 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3738 input->pointer_surface,
3739 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003740}
3741
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003742static const struct wl_callback_listener pointer_surface_listener;
3743
Derek Foreman493d9792015-03-04 16:26:25 -06003744static bool
3745input_set_pointer_special(struct input *input)
3746{
3747 if (input->current_cursor == CURSOR_BLANK) {
3748 wl_pointer_set_cursor(input->pointer,
3749 input->pointer_enter_serial,
3750 NULL, 0, 0);
3751 return true;
3752 }
3753
3754 if (input->current_cursor == CURSOR_UNSET)
3755 return true;
3756
3757 return false;
3758}
3759
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003760static void
Derek Foreman118a4292015-04-22 17:23:35 -05003761schedule_pointer_image_update(struct input *input,
3762 struct wl_cursor *cursor,
3763 uint32_t duration,
3764 bool force_frame)
3765{
3766 /* Some silly cursor sets have enormous pauses in them. In these
3767 * cases it's better to use a timer even if it results in less
3768 * accurate presentation, since it will save us having to set the
3769 * same cursor image over and over again.
3770 *
3771 * This is really not the way we're supposed to time any kind of
3772 * animation, but we're pretending it's OK here because we don't
3773 * want animated cursors with long delays to needlessly hog CPU.
3774 *
3775 * We use force_frame to ensure we don't accumulate large timing
3776 * errors by running off the wrong clock.
3777 */
3778 if (!force_frame && duration > 100) {
3779 struct timespec tp;
3780
3781 clock_gettime(CLOCK_MONOTONIC, &tp);
3782 input->cursor_timer_start = tp.tv_sec * 1000
3783 + tp.tv_nsec / 1000000;
3784 cursor_delay_timer_reset(input, duration);
3785 return;
3786 }
3787
3788 /* for short durations we'll just spin on frame callbacks for
3789 * accurate timing - the way any kind of timing sensitive animation
3790 * should really be done. */
3791 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3792 wl_callback_add_listener(input->cursor_frame_cb,
3793 &pointer_surface_listener, input);
3794
3795}
3796
3797static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003798pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3799 uint32_t time)
3800{
3801 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003802 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003803 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003804 uint32_t duration;
3805 bool force_frame = true;
3806
3807 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003808
3809 if (callback) {
3810 assert(callback == input->cursor_frame_cb);
3811 wl_callback_destroy(callback);
3812 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003813 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003814 }
3815
Daniel Stone80972742012-11-07 17:51:39 +11003816 if (!input->pointer)
3817 return;
3818
Derek Foreman493d9792015-03-04 16:26:25 -06003819 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003820 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003821
Daniel Stonea494f1d2012-06-18 19:31:12 +01003822 cursor = input->display->cursors[input->current_cursor];
3823 if (!cursor)
3824 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003825
3826 /* FIXME We don't have the current time on the first call so we set
3827 * the animation start to the time of the first frame callback. */
3828 if (time == 0)
3829 input->cursor_anim_start = 0;
3830 else if (input->cursor_anim_start == 0)
3831 input->cursor_anim_start = time;
3832
Derek Foreman118a4292015-04-22 17:23:35 -05003833 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003834
Derek Foreman118a4292015-04-22 17:23:35 -05003835 if (time == 0 || input->cursor_anim_start == 0) {
3836 duration = 0;
3837 i = 0;
3838 } else
3839 i = wl_cursor_frame_and_duration(
3840 cursor,
3841 time - input->cursor_anim_start,
3842 &duration);
3843
3844 if (cursor->image_count > 1)
3845 schedule_pointer_image_update(input, cursor, duration,
3846 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003847
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003848 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003849}
3850
Derek Foreman118a4292015-04-22 17:23:35 -05003851static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003852cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003853{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003854 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003855 struct timespec tp;
3856 struct wl_cursor *cursor;
3857 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003858
3859 if (!input->cursor_timer_running)
3860 return;
3861
Derek Foreman118a4292015-04-22 17:23:35 -05003862 cursor = input->display->cursors[input->current_cursor];
3863 if (!cursor)
3864 return;
3865
3866 clock_gettime(CLOCK_MONOTONIC, &tp);
3867 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3868 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3869}
3870
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003871static const struct wl_callback_listener pointer_surface_listener = {
3872 pointer_surface_frame_callback
3873};
3874
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003875void
3876input_set_pointer_image(struct input *input, int pointer)
3877{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003878 int force = 0;
3879
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003880 if (!input->pointer)
3881 return;
3882
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003883 if (input->pointer_enter_serial > input->cursor_serial)
3884 force = 1;
3885
3886 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003887 return;
3888
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003889 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003890 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003891 if (!input->cursor_frame_cb)
3892 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003893 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003894 /* The current frame callback may be stuck if, for instance,
3895 * the set cursor request was processed by the server after
3896 * this client lost the focus. In this case the cursor surface
3897 * might not be mapped and the frame callback wouldn't ever
3898 * complete. Send a set_cursor and attach to try to map the
3899 * cursor surface again so that the callback will finish */
3900 input_set_pointer_image_index(input, 0);
3901 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003902}
3903
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003904struct wl_data_device *
3905input_get_data_device(struct input *input)
3906{
3907 return input->data_device;
3908}
3909
3910void
3911input_set_selection(struct input *input,
3912 struct wl_data_source *source, uint32_t time)
3913{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003914 if (input->data_device)
3915 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003916}
3917
3918void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003919input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003920{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003921 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003922 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003923}
3924
3925static void
3926offer_io_func(struct task *task, uint32_t events)
3927{
3928 struct data_offer *offer =
3929 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003930 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003931 unsigned int len;
3932 char buffer[4096];
3933
3934 len = read(offer->fd, buffer, sizeof buffer);
3935 offer->func(buffer, len,
3936 offer->x, offer->y, offer->user_data);
3937
3938 if (len == 0) {
Harish Krupo43152a32019-04-19 22:06:44 +05303939 if ((offer != offer->input->selection_offer) &&
3940 (display->data_device_manager_version >=
3941 WL_DATA_OFFER_FINISH_SINCE_VERSION))
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003942 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003943 close(offer->fd);
3944 data_offer_destroy(offer);
3945 }
3946}
3947
3948static void
3949data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3950 data_func_t func, void *user_data)
3951{
3952 int p[2];
3953
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003954 if (pipe2(p, O_CLOEXEC) == -1)
3955 return;
3956
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003957 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3958 close(p[1]);
3959
3960 offer->io_task.run = offer_io_func;
3961 offer->fd = p[0];
3962 offer->func = func;
3963 offer->refcount++;
3964 offer->user_data = user_data;
3965
3966 display_watch_fd(offer->input->display,
3967 offer->fd, EPOLLIN, &offer->io_task);
3968}
3969
3970void
3971input_receive_drag_data(struct input *input, const char *mime_type,
3972 data_func_t func, void *data)
3973{
3974 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003975 input->drag_offer->x = input->drag_x;
3976 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003977}
3978
3979int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003980input_receive_drag_data_to_fd(struct input *input,
3981 const char *mime_type, int fd)
3982{
3983 if (input->drag_offer)
3984 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3985
3986 return 0;
3987}
3988
3989int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003990input_receive_selection_data(struct input *input, const char *mime_type,
3991 data_func_t func, void *data)
3992{
3993 char **p;
3994
3995 if (input->selection_offer == NULL)
3996 return -1;
3997
3998 for (p = input->selection_offer->types.data; *p; p++)
3999 if (strcmp(mime_type, *p) == 0)
4000 break;
4001
4002 if (*p == NULL)
4003 return -1;
4004
4005 data_offer_receive_data(input->selection_offer,
4006 mime_type, func, data);
4007 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004008}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004009
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004010int
4011input_receive_selection_data_to_fd(struct input *input,
4012 const char *mime_type, int fd)
4013{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004014 if (input->selection_offer)
4015 wl_data_offer_receive(input->selection_offer->offer,
4016 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004017
4018 return 0;
4019}
4020
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004021void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004022window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004023{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004024 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004025 return;
4026
ant8mef99fac22018-11-28 22:46:37 +01004027 xdg_toplevel_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004028}
4029
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004030static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004031surface_set_synchronized(struct surface *surface)
4032{
4033 if (!surface->subsurface)
4034 return;
4035
4036 if (surface->synchronized)
4037 return;
4038
4039 wl_subsurface_set_sync(surface->subsurface);
4040 surface->synchronized = 1;
4041}
4042
4043static void
4044surface_set_synchronized_default(struct surface *surface)
4045{
4046 if (!surface->subsurface)
4047 return;
4048
4049 if (surface->synchronized == surface->synchronized_default)
4050 return;
4051
4052 if (surface->synchronized_default)
4053 wl_subsurface_set_sync(surface->subsurface);
4054 else
4055 wl_subsurface_set_desync(surface->subsurface);
4056
4057 surface->synchronized = surface->synchronized_default;
4058}
4059
4060static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004061surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004062{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004063 struct widget *widget = surface->widget;
4064 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004065
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004066 if (surface->input_region) {
4067 wl_region_destroy(surface->input_region);
4068 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004069 }
4070
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004071 if (surface->opaque_region)
4072 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004073
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004074 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004075
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004076 if (widget->resize_handler)
4077 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004078 widget->allocation.width,
4079 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004080 widget->user_data);
4081
Pekka Paalanen35e82632013-04-25 13:57:48 +03004082 if (surface->subsurface &&
4083 (surface->allocation.x != widget->allocation.x ||
4084 surface->allocation.y != widget->allocation.y)) {
4085 wl_subsurface_set_position(surface->subsurface,
4086 widget->allocation.x,
4087 widget->allocation.y);
4088 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004089 if (surface->allocation.width != widget->allocation.width ||
4090 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004091 window_schedule_redraw(widget->window);
4092 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004093 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004094
4095 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004096 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004097 widget->allocation.width,
4098 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004099}
4100
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004101static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004102window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004103{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004104 struct surface *surface;
4105
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004106 widget_set_allocation(window->main_surface->widget,
4107 window->pending_allocation.x,
4108 window->pending_allocation.y,
4109 window->pending_allocation.width,
4110 window->pending_allocation.height);
4111
4112 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004113
4114 /* The main surface is in the list, too. Main surface's
4115 * resize_handler is responsible for calling widget_set_allocation()
4116 * on all sub-surface root widgets, so they will be resized
4117 * properly.
4118 */
4119 wl_list_for_each(surface, &window->subsurface_list, link) {
4120 if (surface == window->main_surface)
4121 continue;
4122
4123 surface_set_synchronized(surface);
4124 surface_resize(surface);
4125 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004126
4127 if (!window->fullscreen && !window->maximized)
4128 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004129
4130 if (window->confined && window->confined_widget) {
4131 struct wl_compositor *compositor = window->display->compositor;
4132 struct wl_region *region;
4133 struct widget *widget = window->confined_widget;
4134
4135 region = wl_compositor_create_region(compositor);
4136 wl_region_add(region,
4137 widget->allocation.x,
4138 widget->allocation.y,
4139 widget->allocation.width,
4140 widget->allocation.height);
4141 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4142 region);
4143 wl_region_destroy(region);
4144 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004145}
4146
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004147static void
4148idle_resize(struct window *window)
4149{
4150 window->resize_needed = 0;
4151 window->redraw_needed = 1;
4152
4153 DBG("from %dx%d to %dx%d\n",
4154 window->main_surface->server_allocation.width,
4155 window->main_surface->server_allocation.height,
4156 window->pending_allocation.width,
4157 window->pending_allocation.height);
4158
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004159 window_do_resize(window);
4160}
4161
4162static void
4163undo_resize(struct window *window)
4164{
4165 window->pending_allocation.width =
4166 window->main_surface->server_allocation.width;
4167 window->pending_allocation.height =
4168 window->main_surface->server_allocation.height;
4169
4170 DBG("back to %dx%d\n",
4171 window->main_surface->server_allocation.width,
4172 window->main_surface->server_allocation.height);
4173
4174 window_do_resize(window);
4175
4176 if (window->pending_allocation.width == 0 &&
4177 window->pending_allocation.height == 0) {
4178 fprintf(stderr, "Error: Could not draw a surface, "
4179 "most likely due to insufficient disk space in "
4180 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4181 exit(EXIT_FAILURE);
4182 }
4183}
4184
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004185void
4186window_schedule_resize(struct window *window, int width, int height)
4187{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004188 /* We should probably get these numbers from the theme. */
4189 const int min_width = 200, min_height = 200;
4190
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004191 window->pending_allocation.x = 0;
4192 window->pending_allocation.y = 0;
4193 window->pending_allocation.width = width;
4194 window->pending_allocation.height = height;
4195
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004196 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004197 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004198 window->min_allocation.width = min_width;
4199 else
4200 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004201 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004202 window->min_allocation.height = min_height;
4203 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004204 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004205 }
4206
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004207 if (window->pending_allocation.width < window->min_allocation.width)
4208 window->pending_allocation.width = window->min_allocation.width;
4209 if (window->pending_allocation.height < window->min_allocation.height)
4210 window->pending_allocation.height = window->min_allocation.height;
4211
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004212 window->resize_needed = 1;
4213 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004214}
4215
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004216void
4217widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4218{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004219 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004220}
4221
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004222static int
4223window_get_shadow_margin(struct window *window)
4224{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004225 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004226 return frame_get_shadow_margin(window->frame->frame);
4227 else
4228 return 0;
4229}
4230
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004231void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004232window_inhibit_redraw(struct window *window)
4233{
4234 window->redraw_inhibited = 1;
4235 wl_list_remove(&window->redraw_task.link);
4236 wl_list_init(&window->redraw_task.link);
4237 window->redraw_task_scheduled = 0;
4238}
4239
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004240void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004241window_uninhibit_redraw(struct window *window)
4242{
4243 window->redraw_inhibited = 0;
4244 if (window->redraw_needed || window->resize_needed)
4245 window_schedule_redraw_task(window);
4246}
4247
4248static void
4249xdg_surface_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01004250 struct xdg_surface *xdg_surface,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004251 uint32_t serial)
4252{
4253 struct window *window = data;
4254
ant8mef99fac22018-11-28 22:46:37 +01004255 xdg_surface_ack_configure(window->xdg_surface, serial);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004256
4257 if (window->state_changed_handler)
4258 window->state_changed_handler(window, window->user_data);
4259
4260 window_uninhibit_redraw(window);
4261}
4262
ant8mef99fac22018-11-28 22:46:37 +01004263static const struct xdg_surface_listener xdg_surface_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004264 xdg_surface_handle_configure
4265};
4266
4267static void
ant8mef99fac22018-11-28 22:46:37 +01004268xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004269 int32_t width, int32_t height,
4270 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004271{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004272 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004273 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004274
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004275 window->maximized = 0;
4276 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004277 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004278 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004279
4280 wl_array_for_each(p, states) {
4281 uint32_t state = *p;
4282 switch (state) {
ant8mef99fac22018-11-28 22:46:37 +01004283 case XDG_TOPLEVEL_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004284 window->maximized = 1;
4285 break;
ant8mef99fac22018-11-28 22:46:37 +01004286 case XDG_TOPLEVEL_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004287 window->fullscreen = 1;
4288 break;
ant8mef99fac22018-11-28 22:46:37 +01004289 case XDG_TOPLEVEL_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004290 window->resizing = 1;
4291 break;
ant8mef99fac22018-11-28 22:46:37 +01004292 case XDG_TOPLEVEL_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004293 window->focused = 1;
4294 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004295 default:
4296 /* Unknown state */
4297 break;
4298 }
4299 }
4300
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004301 if (window->frame) {
4302 if (window->maximized) {
4303 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4304 } else {
4305 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4306 }
4307
4308 if (window->focused) {
4309 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4310 } else {
4311 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4312 }
4313 }
4314
Jasper St. Pierref184c382014-05-06 08:33:27 -04004315 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004316 /* The width / height params are for window geometry,
4317 * but window_schedule_resize takes allocation. Add
4318 * on the shadow margin to get the difference. */
4319 int margin = window_get_shadow_margin(window);
4320
4321 window_schedule_resize(window,
4322 width + margin * 2,
4323 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004324 } else if (window->saved_allocation.width > 0 &&
4325 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004326 window_schedule_resize(window,
4327 window->saved_allocation.width,
4328 window->saved_allocation.height);
4329 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004330}
4331
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004332static void
ant8mef99fac22018-11-28 22:46:37 +01004333xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004334{
4335 struct window *window = data;
4336 window_close(window);
4337}
4338
ant8mef99fac22018-11-28 22:46:37 +01004339static const struct xdg_toplevel_listener xdg_toplevel_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004340 xdg_toplevel_handle_configure,
4341 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004342};
4343
4344static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004345window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004346{
ant8mef99fac22018-11-28 22:46:37 +01004347 struct xdg_toplevel *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004348
4349 if (!window->xdg_surface)
4350 return;
4351
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004352 if (window->parent == window->last_parent)
4353 return;
4354
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004355 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004356 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004357 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004358 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004359
ant8mef99fac22018-11-28 22:46:37 +01004360 xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004361 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004362}
4363
4364static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004365window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004366{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004367 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004368 frame_input_rect(window->frame->frame,
4369 &geometry->x,
4370 &geometry->y,
4371 &geometry->width,
4372 &geometry->height);
4373 else
4374 window_get_allocation(window, geometry);
4375}
4376
4377static void
4378window_sync_geometry(struct window *window)
4379{
4380 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004381
4382 if (!window->xdg_surface)
4383 return;
4384
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004385 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004386 if (geometry.x == window->last_geometry.x &&
4387 geometry.y == window->last_geometry.y &&
4388 geometry.width == window->last_geometry.width &&
4389 geometry.height == window->last_geometry.height)
4390 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004391
ant8mef99fac22018-11-28 22:46:37 +01004392 xdg_surface_set_window_geometry(window->xdg_surface,
4393 geometry.x,
4394 geometry.y,
4395 geometry.width,
4396 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004397 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004398}
4399
4400static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004401window_flush(struct window *window)
4402{
4403 struct surface *surface;
4404
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004405 assert(!window->redraw_inhibited);
4406
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004407 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004408 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004409 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004410 if (window->xdg_toplevel)
4411 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004412 }
4413
4414 wl_list_for_each(surface, &window->subsurface_list, link) {
4415 if (surface == window->main_surface)
4416 continue;
4417
4418 surface_flush(surface);
4419 }
4420
4421 surface_flush(window->main_surface);
4422}
4423
4424static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004425menu_destroy(struct menu *menu)
4426{
4427 widget_destroy(menu->widget);
4428 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004429 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004430 free(menu);
4431}
4432
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004433void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004434window_get_allocation(struct window *window,
4435 struct rectangle *allocation)
4436{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004437 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004438}
4439
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004440static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004441widget_redraw(struct widget *widget)
4442{
4443 struct widget *child;
4444
4445 if (widget->redraw_handler)
4446 widget->redraw_handler(widget, widget->user_data);
4447 wl_list_for_each(child, &widget->child_list, link)
4448 widget_redraw(child);
4449}
4450
4451static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004452frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4453{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004454 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004455
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004456 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004457 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004458 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004459 surface->frame_cb = NULL;
4460
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004461 surface->last_time = time;
4462
Pekka Paalanen71233882013-04-25 13:57:53 +03004463 if (surface->redraw_needed || surface->window->redraw_needed) {
4464 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004465 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004466 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004467}
4468
4469static const struct wl_callback_listener listener = {
4470 frame_callback
4471};
4472
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004473static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004474surface_redraw(struct surface *surface)
4475{
Pekka Paalanen71233882013-04-25 13:57:53 +03004476 DBG_OBJ(surface->surface, "begin\n");
4477
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004478 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004479 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004480
4481 /* Whole-window redraw forces a redraw even if the previous has
4482 * not yet hit the screen.
4483 */
4484 if (surface->frame_cb) {
4485 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004486 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004487
Pekka Paalanen71233882013-04-25 13:57:53 +03004488 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004489 wl_callback_destroy(surface->frame_cb);
4490 }
4491
Neil Roberts97b747c2013-12-19 16:17:12 +00004492 if (surface->widget->use_cairo &&
4493 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004494 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004495 return -1;
4496 }
4497
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004498 surface->frame_cb = wl_surface_frame(surface->surface);
4499 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004500 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004501
4502 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004503 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004504 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004505 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004506 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004507}
4508
4509static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004510idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004511{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004512 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004513 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004514 int failed = 0;
4515 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004516
Pekka Paalanen71233882013-04-25 13:57:53 +03004517 DBG(" --------- \n");
4518
Pekka Paalaneneebff542013-04-25 13:57:52 +03004519 wl_list_init(&window->redraw_task.link);
4520 window->redraw_task_scheduled = 0;
4521
4522 if (window->resize_needed) {
4523 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004524 if (window->main_surface->frame_cb) {
4525 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004526 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004527 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004528
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004529 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004530 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004531 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004532
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004533 if (surface_redraw(window->main_surface) < 0) {
4534 /*
4535 * Only main_surface failure will cause us to undo the resize.
4536 * If sub-surfaces fail, they will just be broken with old
4537 * content.
4538 */
4539 failed = 1;
4540 } else {
4541 wl_list_for_each(surface, &window->subsurface_list, link) {
4542 if (surface == window->main_surface)
4543 continue;
4544
4545 surface_redraw(surface);
4546 }
4547 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004548
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004549 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004550 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004551
4552 wl_list_for_each(surface, &window->subsurface_list, link)
4553 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004554
4555 if (resized && failed) {
4556 /* Restore widget tree to correspond to what is on screen. */
4557 undo_resize(window);
4558 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004559}
4560
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004561static void
4562window_schedule_redraw_task(struct window *window)
4563{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004564 if (window->redraw_inhibited)
4565 return;
4566
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004567 if (!window->redraw_task_scheduled) {
4568 window->redraw_task.run = idle_redraw;
4569 display_defer(window->display, &window->redraw_task);
4570 window->redraw_task_scheduled = 1;
4571 }
4572}
4573
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004574void
4575window_schedule_redraw(struct window *window)
4576{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004577 struct surface *surface;
4578
Pekka Paalanen71233882013-04-25 13:57:53 +03004579 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4580
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004581 wl_list_for_each(surface, &window->subsurface_list, link)
4582 surface->redraw_needed = 1;
4583
4584 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004585}
4586
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004587int
4588window_is_fullscreen(struct window *window)
4589{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004590 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004591}
4592
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004593void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004594window_set_fullscreen(struct window *window, int fullscreen)
4595{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004596 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004597 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004598
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004599 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004600 return;
4601
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004602 if (fullscreen)
ant8mef99fac22018-11-28 22:46:37 +01004603 xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004604 else
ant8mef99fac22018-11-28 22:46:37 +01004605 xdg_toplevel_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004606}
4607
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004608int
4609window_is_maximized(struct window *window)
4610{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004611 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004612}
4613
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004614void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004615window_set_maximized(struct window *window, int maximized)
4616{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004617 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004618 return;
4619
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004620 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004621 return;
4622
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004623 if (maximized)
ant8mef99fac22018-11-28 22:46:37 +01004624 xdg_toplevel_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004625 else
ant8mef99fac22018-11-28 22:46:37 +01004626 xdg_toplevel_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004627}
4628
Jasper St. Pierrede680992014-04-10 17:23:49 -07004629int
4630window_is_resizing(struct window *window)
4631{
4632 return window->resizing;
4633}
4634
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004635void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004636window_set_minimized(struct window *window)
4637{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004638 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004639 return;
4640
ant8mef99fac22018-11-28 22:46:37 +01004641 xdg_toplevel_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004642}
4643
4644void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004645window_set_user_data(struct window *window, void *data)
4646{
4647 window->user_data = data;
4648}
4649
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004650void *
4651window_get_user_data(struct window *window)
4652{
4653 return window->user_data;
4654}
4655
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004656void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004657window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004658 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004659{
4660 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004661}
4662
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004663void
4664window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004665 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004666{
4667 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004668}
4669
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004670void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004671window_set_data_handler(struct window *window, window_data_handler_t handler)
4672{
4673 window->data_handler = handler;
4674}
4675
4676void
4677window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4678{
4679 window->drop_handler = handler;
4680}
4681
4682void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004683window_set_close_handler(struct window *window,
4684 window_close_handler_t handler)
4685{
4686 window->close_handler = handler;
4687}
4688
4689void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004690window_set_fullscreen_handler(struct window *window,
4691 window_fullscreen_handler_t handler)
4692{
4693 window->fullscreen_handler = handler;
4694}
4695
4696void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004697window_set_output_handler(struct window *window,
4698 window_output_handler_t handler)
4699{
4700 window->output_handler = handler;
4701}
4702
4703void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004704window_set_state_changed_handler(struct window *window,
4705 window_state_changed_handler_t handler)
4706{
4707 window->state_changed_handler = handler;
4708}
4709
4710void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004711window_set_pointer_locked_handler(struct window *window,
4712 locked_pointer_locked_handler_t locked,
4713 locked_pointer_unlocked_handler_t unlocked)
4714{
4715 window->pointer_unlocked_handler = unlocked;
4716 window->pointer_locked_handler = locked;
4717}
4718
4719void
4720window_set_pointer_confined_handler(struct window *window,
4721 confined_pointer_confined_handler_t confined,
4722 confined_pointer_unconfined_handler_t unconfined)
4723{
4724 window->pointer_confined_handler = confined;
4725 window->pointer_unconfined_handler = unconfined;
4726}
4727
4728void
4729window_set_locked_pointer_motion_handler(struct window *window,
4730 window_locked_pointer_motion_handler_t handler)
4731{
4732 window->locked_pointer_motion_handler = handler;
4733}
4734
4735void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004736window_set_title(struct window *window, const char *title)
4737{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004738 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004739 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004740 if (window->frame) {
4741 frame_set_title(window->frame->frame, title);
4742 widget_schedule_redraw(window->frame->widget);
4743 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004744 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01004745 xdg_toplevel_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004746}
4747
4748const char *
4749window_get_title(struct window *window)
4750{
4751 return window->title;
4752}
4753
4754void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004755window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4756{
4757 struct text_cursor_position *text_cursor_position =
4758 window->display->text_cursor_position;
4759
Scott Moreau9295ce02012-06-01 12:46:10 -06004760 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004761 return;
4762
4763 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004764 window->main_surface->surface,
4765 wl_fixed_from_int(x),
4766 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004767}
4768
Casey Dahlin9074db52012-04-19 22:50:09 -04004769static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004770relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4771 uint32_t utime_hi,
4772 uint32_t utime_lo,
4773 wl_fixed_t dx,
4774 wl_fixed_t dy,
4775 wl_fixed_t dx_unaccel,
4776 wl_fixed_t dy_unaccel)
4777{
4778 struct input *input = data;
4779 struct window *window = input->pointer_focus;
4780 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4781
4782 if (window->locked_pointer_motion_handler &&
4783 window->pointer_locked) {
4784 window->locked_pointer_motion_handler(
4785 window, input, ms,
4786 wl_fixed_to_double(dx),
4787 wl_fixed_to_double(dy),
4788 window->user_data);
4789 }
4790}
4791
4792static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4793 relative_pointer_handle_motion,
4794};
4795
4796static void
4797locked_pointer_locked(void *data,
4798 struct zwp_locked_pointer_v1 *locked_pointer)
4799{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004800 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004801 struct window *window = input->locked_window;
4802
4803 if (!window)
4804 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004805
4806 window->pointer_locked = true;
4807
4808 if (window->pointer_locked_handler) {
4809 window->pointer_locked_handler(window,
4810 input,
4811 window->user_data);
4812 }
4813}
4814
4815static void
4816locked_pointer_unlocked(void *data,
4817 struct zwp_locked_pointer_v1 *locked_pointer)
4818{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004819 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004820 struct window *window = input->locked_window;
4821
4822 if (!window)
4823 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004824
4825 window_unlock_pointer(window);
4826
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004827 input->locked_window = NULL;
4828
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004829 if (window->pointer_unlocked_handler) {
4830 window->pointer_unlocked_handler(window,
4831 input,
4832 window->user_data);
4833 }
4834}
4835
4836static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4837 locked_pointer_locked,
4838 locked_pointer_unlocked,
4839};
4840
4841int
4842window_lock_pointer(struct window *window, struct input *input)
4843{
4844 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4845 window->display->relative_pointer_manager;
4846 struct zwp_pointer_constraints_v1 *pointer_constraints =
4847 window->display->pointer_constraints;
4848 struct zwp_relative_pointer_v1 *relative_pointer;
4849 struct zwp_locked_pointer_v1 *locked_pointer;
4850
4851 if (!window->display->relative_pointer_manager)
4852 return -1;
4853
4854 if (!window->display->pointer_constraints)
4855 return -1;
4856
4857 if (window->locked_pointer)
4858 return -1;
4859
4860 if (window->confined_pointer)
4861 return -1;
4862
4863 if (!input->pointer)
4864 return -1;
4865
4866 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4867 relative_pointer_manager, input->pointer);
4868 zwp_relative_pointer_v1_add_listener(relative_pointer,
4869 &relative_pointer_listener,
4870 input);
4871
4872 locked_pointer =
4873 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4874 window->main_surface->surface,
4875 input->pointer,
4876 NULL,
4877 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4878 zwp_locked_pointer_v1_add_listener(locked_pointer,
4879 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004880 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004881
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004882 window->locked_pointer = locked_pointer;
4883 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004884 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004885
4886 return 0;
4887}
4888
4889void
4890window_unlock_pointer(struct window *window)
4891{
4892 if (!window->locked_pointer)
4893 return;
4894
4895 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4896 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4897 window->locked_pointer = NULL;
4898 window->relative_pointer = NULL;
4899 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004900}
4901
4902void
4903widget_set_locked_pointer_cursor_hint(struct widget *widget,
4904 float x, float y)
4905{
4906 struct window *window = widget->window;
4907
4908 if (!window->locked_pointer)
4909 return;
4910
4911 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4912 wl_fixed_from_double(x),
4913 wl_fixed_from_double(y));
4914 wl_surface_commit(window->main_surface->surface);
4915}
4916
4917static void
4918confined_pointer_confined(void *data,
4919 struct zwp_confined_pointer_v1 *confined_pointer)
4920{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004921 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004922 struct window *window = input->confined_window;
4923
4924 if (!window)
4925 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004926
4927 window->confined = true;
4928
4929 if (window->pointer_confined_handler) {
4930 window->pointer_confined_handler(window,
4931 input,
4932 window->user_data);
4933 }
4934}
4935
4936static void
4937confined_pointer_unconfined(void *data,
4938 struct zwp_confined_pointer_v1 *confined_pointer)
4939{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004940 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004941 struct window *window = input->confined_window;
4942
4943 if (!window)
4944 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004945
4946 window_unconfine_pointer(window);
4947
4948 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004949 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004950
4951 if (window->pointer_unconfined_handler) {
4952 window->pointer_unconfined_handler(window,
4953 input,
4954 window->user_data);
4955 }
4956}
4957
4958static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4959 confined_pointer_confined,
4960 confined_pointer_unconfined,
4961};
4962
4963int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004964window_confine_pointer_to_rectangles(struct window *window,
4965 struct input *input,
4966 struct rectangle *rectangles,
4967 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004968{
4969 struct zwp_pointer_constraints_v1 *pointer_constraints =
4970 window->display->pointer_constraints;
4971 struct zwp_confined_pointer_v1 *confined_pointer;
4972 struct wl_compositor *compositor = window->display->compositor;
4973 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004974 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004975
4976 if (!window->display->pointer_constraints)
4977 return -1;
4978
4979 if (window->locked_pointer)
4980 return -1;
4981
4982 if (window->confined_pointer)
4983 return -1;
4984
4985 if (!input->pointer)
4986 return -1;
4987
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004988 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004989 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004990 for (i = 0; i < num_rectangles; i++) {
4991 wl_region_add(region,
4992 rectangles[i].x,
4993 rectangles[i].y,
4994 rectangles[i].width,
4995 rectangles[i].height);
4996 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004997 }
4998
4999 confined_pointer =
5000 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5001 window->main_surface->surface,
5002 input->pointer,
5003 region,
5004 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5005 if (region)
5006 wl_region_destroy(region);
5007
5008 zwp_confined_pointer_v1_add_listener(confined_pointer,
5009 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08005010 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005011
5012 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005013 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005014 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005015
5016 return 0;
5017}
5018
5019void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005020window_update_confine_rectangles(struct window *window,
5021 struct rectangle *rectangles,
5022 int num_rectangles)
5023{
5024 struct wl_compositor *compositor = window->display->compositor;
5025 struct wl_region *region;
5026 int i;
5027
5028 region = wl_compositor_create_region(compositor);
5029 for (i = 0; i < num_rectangles; i++) {
5030 wl_region_add(region,
5031 rectangles[i].x,
5032 rectangles[i].y,
5033 rectangles[i].width,
5034 rectangles[i].height);
5035 }
5036
5037 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5038
5039 wl_region_destroy(region);
5040}
5041
5042int
5043window_confine_pointer_to_widget(struct window *window,
5044 struct widget *widget,
5045 struct input *input)
5046{
5047 int ret;
5048
5049 if (widget) {
5050 ret = window_confine_pointer_to_rectangles(window,
5051 input,
5052 &widget->allocation,
5053 1);
5054 window->confined_widget = widget;
5055 return ret;
5056 } else {
5057 return window_confine_pointer_to_rectangles(window,
5058 input,
5059 NULL,
5060 0);
5061 }
5062}
5063
5064void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005065window_unconfine_pointer(struct window *window)
5066{
5067 if (!window->confined_pointer)
5068 return;
5069
5070 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5071 window->confined_pointer = NULL;
5072 window->confined = false;
5073}
5074
5075static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005076surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005077 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005078{
Rob Bradford7507b572012-05-15 17:55:34 +01005079 struct window *window = data;
5080 struct output *output;
5081 struct output *output_found = NULL;
5082 struct window_output *window_output;
5083
5084 wl_list_for_each(output, &window->display->output_list, link) {
5085 if (output->output == wl_output) {
5086 output_found = output;
5087 break;
5088 }
5089 }
5090
5091 if (!output_found)
5092 return;
5093
Brian Lovinbc919262013-08-07 15:34:59 -07005094 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005095 window_output->output = output_found;
5096
5097 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005098
5099 if (window->output_handler)
5100 window->output_handler(window, output_found, 1,
5101 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005102}
5103
5104static void
5105surface_leave(void *data,
5106 struct wl_surface *wl_surface, struct wl_output *output)
5107{
Rob Bradford7507b572012-05-15 17:55:34 +01005108 struct window *window = data;
5109 struct window_output *window_output;
5110 struct window_output *window_output_found = NULL;
5111
5112 wl_list_for_each(window_output, &window->window_output_list, link) {
5113 if (window_output->output->output == output) {
5114 window_output_found = window_output;
5115 break;
5116 }
5117 }
5118
5119 if (window_output_found) {
5120 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005121
5122 if (window->output_handler)
5123 window->output_handler(window, window_output->output,
5124 0, window->user_data);
5125
Rob Bradford7507b572012-05-15 17:55:34 +01005126 free(window_output_found);
5127 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005128}
5129
5130static const struct wl_surface_listener surface_listener = {
5131 surface_enter,
5132 surface_leave
5133};
5134
Pekka Paalanen4e373742013-02-13 16:17:13 +02005135static struct surface *
5136surface_create(struct window *window)
5137{
5138 struct display *display = window->display;
5139 struct surface *surface;
5140
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005141 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005142 surface->window = window;
5143 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005144 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005145 wl_surface_add_listener(surface->surface, &surface_listener, window);
5146
Pekka Paalanen35e82632013-04-25 13:57:48 +03005147 wl_list_insert(&window->subsurface_list, &surface->link);
5148
Pekka Paalanen4e373742013-02-13 16:17:13 +02005149 return surface;
5150}
5151
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005152static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005153get_preferred_buffer_type(struct display *display)
5154{
5155#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005156 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005157 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5158#endif
5159
5160 return WINDOW_BUFFER_TYPE_SHM;
5161}
5162
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005163static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005164window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005165{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005166 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005167 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005168
Peter Huttererf3d62272013-08-08 11:57:05 +10005169 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005170 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005171 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005172
5173 surface = surface_create(window);
5174 window->main_surface = surface;
5175
Michael Teyfeld6371b52017-10-11 17:17:29 +02005176 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005177
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005178 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005179 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005180
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005181 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005182
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005183 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005184 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005185 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005186
Rob Bradford7507b572012-05-15 17:55:34 +01005187 wl_list_init (&window->window_output_list);
5188
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005189 return window;
5190}
5191
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005192struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005193window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005194{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005195 struct window *window;
5196
5197 window = window_create_internal(display, 0);
5198
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005199 if (window->display->xdg_shell) {
5200 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005201 xdg_wm_base_get_xdg_surface(window->display->xdg_shell,
5202 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005203 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005204
ant8mef99fac22018-11-28 22:46:37 +01005205 xdg_surface_add_listener(window->xdg_surface,
5206 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005207
5208 window->xdg_toplevel =
ant8mef99fac22018-11-28 22:46:37 +01005209 xdg_surface_get_toplevel(window->xdg_surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005210 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5211
ant8mef99fac22018-11-28 22:46:37 +01005212 xdg_toplevel_add_listener(window->xdg_toplevel,
5213 &xdg_toplevel_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005214
5215 window_inhibit_redraw(window);
5216
5217 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005218 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005219
5220 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005221}
5222
5223struct window *
5224window_create_custom(struct display *display)
5225{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005226 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005227}
5228
Jasper St. Pierre53686042013-12-09 15:26:25 -05005229void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005230window_set_parent(struct window *window,
5231 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005232{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005233 window->parent = parent_window;
5234 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005235}
5236
5237struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005238window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005239{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005240 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005241}
5242
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005243static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005244menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005245{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005246 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005247 int next;
5248
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005249 frame_interior(menu->frame, &x, &y, &width, &height);
5250 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005251 if (menu->current != next) {
5252 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005253 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005254 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005255}
5256
5257static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005258menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005259 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005260 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005261{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005262 struct menu *menu = data;
5263
5264 if (widget == menu->widget)
5265 menu_set_item(data, y);
5266
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005267 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005268}
5269
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005270static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005271menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005272 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005273{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005274 struct menu *menu = data;
5275
5276 if (widget == menu->widget)
5277 menu_set_item(data, y);
5278
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005279 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005280}
5281
5282static void
5283menu_leave_handler(struct widget *widget, struct input *input, void *data)
5284{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005285 struct menu *menu = data;
5286
5287 if (widget == menu->widget)
5288 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005289}
5290
5291static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005292menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005293 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005294 uint32_t button, enum wl_pointer_button_state state,
5295 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005296
5297{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005298 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005299
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005300 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5301 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005302 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005303 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005304 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005305 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005306 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005307 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005308 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005309 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005310}
5311
5312static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005313menu_touch_up_handler(struct widget *widget,
5314 struct input *input,
5315 uint32_t serial,
5316 uint32_t time,
5317 int32_t id,
5318 void *data)
5319{
5320 struct menu *menu = data;
5321
5322 input_ungrab(input);
5323 menu_destroy(menu);
5324}
5325
5326static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005327menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005328{
5329 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005330 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005331 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005332
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005333 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005334
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005335 frame_repaint(menu->frame, cr);
5336 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005337
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005338 theme_set_background_source(menu->window->display->theme,
5339 cr, THEME_FRAME_ACTIVE);
5340 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005341 cairo_fill(cr);
5342
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005343 cairo_select_font_face(cr, "sans",
5344 CAIRO_FONT_SLANT_NORMAL,
5345 CAIRO_FONT_WEIGHT_NORMAL);
5346 cairo_set_font_size(cr, 12);
5347
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005348 for (i = 0; i < menu->count; i++) {
5349 if (i == menu->current) {
5350 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005351 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005352 cairo_fill(cr);
5353 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005354 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005355 cairo_show_text(cr, menu->entries[i]);
5356 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005357 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5358 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005359 cairo_show_text(cr, menu->entries[i]);
5360 }
5361 }
5362
5363 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005364}
5365
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005366static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005367xdg_popup_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01005368 struct xdg_popup *xdg_popup,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005369 int32_t x,
5370 int32_t y,
5371 int32_t width,
5372 int32_t height)
5373{
5374}
5375
5376static void
ant8mef99fac22018-11-28 22:46:37 +01005377xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005378{
5379 struct window *window = data;
5380 struct menu *menu = window->main_surface->widget->user_data;
5381
5382 input_ungrab(menu->input);
5383 menu_destroy(menu);
5384}
5385
ant8mef99fac22018-11-28 22:46:37 +01005386static const struct xdg_popup_listener xdg_popup_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005387 xdg_popup_handle_configure,
5388 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005389};
5390
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005391static struct menu *
5392create_menu(struct display *display,
5393 struct input *input, uint32_t time,
5394 menu_func_t func, const char **entries, int count,
5395 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005396{
5397 struct window *window;
5398 struct menu *menu;
5399
5400 menu = malloc(sizeof *menu);
5401 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005402 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005403
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005404 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005405 if (!window) {
5406 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005407 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005408 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005409
5410 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005411 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005412 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005413 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005414 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005415 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005416 menu->entries = entries;
5417 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005418 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005419 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005420 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005421 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005422 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005423
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005424 input_ungrab(input);
5425
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005426 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005427 widget_set_enter_handler(menu->widget, menu_enter_handler);
5428 widget_set_leave_handler(menu->widget, menu_leave_handler);
5429 widget_set_motion_handler(menu->widget, menu_motion_handler);
5430 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005431 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005432
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005433 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005434 frame_resize_inside(menu->frame, 200, count * 20);
5435 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5436 window_schedule_resize(window, frame_width(menu->frame),
5437 frame_height(menu->frame));
5438
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005439 return menu;
5440}
5441
ant8mef99fac22018-11-28 22:46:37 +01005442static struct xdg_positioner *
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005443create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005444 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005445{
ant8mef99fac22018-11-28 22:46:37 +01005446 struct xdg_positioner *positioner;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005447
ant8mef99fac22018-11-28 22:46:37 +01005448 positioner = xdg_wm_base_create_positioner(display->xdg_shell);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005449 fail_on_null(positioner, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005450 xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1);
5451 xdg_positioner_set_size(positioner, w, h);
5452 xdg_positioner_set_anchor(positioner,
5453 XDG_POSITIONER_ANCHOR_TOP_LEFT);
5454 xdg_positioner_set_gravity(positioner,
5455 XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005456
5457 return positioner;
5458}
5459
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005460void
5461window_show_menu(struct display *display,
5462 struct input *input, uint32_t time, struct window *parent,
5463 int32_t x, int32_t y,
5464 menu_func_t func, const char **entries, int count)
5465{
5466 struct menu *menu;
5467 struct window *window;
5468 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005469 struct rectangle parent_geometry;
ant8mef99fac22018-11-28 22:46:37 +01005470 struct xdg_positioner *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005471
5472 menu = create_menu(display, input, time, func, entries, count, parent);
5473
5474 if (menu == NULL)
5475 return;
5476
5477 window = menu->window;
5478
5479 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5480 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5481
5482 window->x = x;
5483 window->y = y;
5484
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005485 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005486 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005487
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005488 if (!display->xdg_shell)
5489 return;
5490
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005491 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005492 xdg_wm_base_get_xdg_surface(display->xdg_shell,
5493 window->main_surface->surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005494 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005495
ant8mef99fac22018-11-28 22:46:37 +01005496 xdg_surface_add_listener(window->xdg_surface,
5497 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005498
5499 positioner = create_simple_positioner(display,
5500 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005501 window->y - (iy + parent_geometry.y),
5502 frame_width(menu->frame),
5503 frame_height(menu->frame));
ant8mef99fac22018-11-28 22:46:37 +01005504 window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
5505 parent->xdg_surface,
5506 positioner);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005507 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005508 xdg_positioner_destroy(positioner);
5509 xdg_popup_grab(window->xdg_popup, input->seat,
5510 display_get_serial(window->display));
5511 xdg_popup_add_listener(window->xdg_popup,
5512 &xdg_popup_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005513
5514 window_inhibit_redraw(window);
5515
5516 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005517}
5518
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005519void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005520window_set_buffer_type(struct window *window, enum window_buffer_type type)
5521{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005522 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005523}
5524
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005525enum window_buffer_type
5526window_get_buffer_type(struct window *window)
5527{
5528 return window->main_surface->buffer_type;
5529}
5530
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005531void
5532window_set_preferred_format(struct window *window,
5533 enum preferred_format format)
5534{
5535 window->preferred_format = format;
5536}
5537
Pekka Paalanen35e82632013-04-25 13:57:48 +03005538struct widget *
5539window_add_subsurface(struct window *window, void *data,
5540 enum subsurface_mode default_mode)
5541{
5542 struct widget *widget;
5543 struct surface *surface;
5544 struct wl_surface *parent;
5545 struct wl_subcompositor *subcompo = window->display->subcompositor;
5546
Pekka Paalanen35e82632013-04-25 13:57:48 +03005547 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005548 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005549 widget = widget_create(window, surface, data);
5550 wl_list_init(&widget->link);
5551 surface->widget = widget;
5552
5553 parent = window->main_surface->surface;
5554 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5555 surface->surface,
5556 parent);
5557 surface->synchronized = 1;
5558
5559 switch (default_mode) {
5560 case SUBSURFACE_SYNCHRONIZED:
5561 surface->synchronized_default = 1;
5562 break;
5563 case SUBSURFACE_DESYNCHRONIZED:
5564 surface->synchronized_default = 0;
5565 break;
5566 default:
5567 assert(!"bad enum subsurface_mode");
5568 }
5569
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005570 window->resize_needed = 1;
5571 window_schedule_redraw(window);
5572
Pekka Paalanen35e82632013-04-25 13:57:48 +03005573 return widget;
5574}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005575
5576static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005577display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005578 struct wl_output *wl_output,
5579 int x, int y,
5580 int physical_width,
5581 int physical_height,
5582 int subpixel,
5583 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005584 const char *model,
5585 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005586{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005587 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005588
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005589 output->allocation.x = x;
5590 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005591 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005592
5593 if (output->make)
5594 free(output->make);
5595 output->make = strdup(make);
5596
5597 if (output->model)
5598 free(output->model);
5599 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005600}
5601
5602static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005603display_handle_done(void *data,
5604 struct wl_output *wl_output)
5605{
5606}
5607
5608static void
5609display_handle_scale(void *data,
5610 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005611 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005612{
5613 struct output *output = data;
5614
5615 output->scale = scale;
5616}
5617
5618static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005619display_handle_mode(void *data,
5620 struct wl_output *wl_output,
5621 uint32_t flags,
5622 int width,
5623 int height,
5624 int refresh)
5625{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005626 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005627 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005628
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005629 if (flags & WL_OUTPUT_MODE_CURRENT) {
5630 output->allocation.width = width;
5631 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005632 if (display->output_configure_handler)
5633 (*display->output_configure_handler)(
5634 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005635 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005636}
5637
5638static const struct wl_output_listener output_listener = {
5639 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005640 display_handle_mode,
5641 display_handle_done,
5642 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005643};
5644
5645static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005646display_add_output(struct display *d, uint32_t id)
5647{
5648 struct output *output;
5649
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005650 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005651 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005652 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005653 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005654 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005655 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005656 wl_list_insert(d->output_list.prev, &output->link);
5657
5658 wl_output_add_listener(output->output, &output_listener, output);
5659}
5660
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005661static void
5662output_destroy(struct output *output)
5663{
5664 if (output->destroy_handler)
5665 (*output->destroy_handler)(output, output->user_data);
5666
5667 wl_output_destroy(output->output);
5668 wl_list_remove(&output->link);
5669 free(output);
5670}
5671
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005672static void
5673display_destroy_output(struct display *d, uint32_t id)
5674{
5675 struct output *output;
5676
5677 wl_list_for_each(output, &d->output_list, link) {
5678 if (output->server_output_id == id) {
5679 output_destroy(output);
5680 break;
5681 }
5682 }
5683}
5684
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005685void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005686display_set_global_handler(struct display *display,
5687 display_global_handler_t handler)
5688{
5689 struct global *global;
5690
5691 display->global_handler = handler;
5692 if (!handler)
5693 return;
5694
5695 wl_list_for_each(global, &display->global_list, link)
5696 display->global_handler(display,
5697 global->name, global->interface,
5698 global->version, display->user_data);
5699}
5700
5701void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005702display_set_global_handler_remove(struct display *display,
5703 display_global_handler_t remove_handler)
5704{
5705 display->global_handler_remove = remove_handler;
5706 if (!remove_handler)
5707 return;
5708}
5709
5710void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005711display_set_output_configure_handler(struct display *display,
5712 display_output_handler_t handler)
5713{
5714 struct output *output;
5715
5716 display->output_configure_handler = handler;
5717 if (!handler)
5718 return;
5719
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005720 wl_list_for_each(output, &display->output_list, link) {
5721 if (output->allocation.width == 0 &&
5722 output->allocation.height == 0)
5723 continue;
5724
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005725 (*display->output_configure_handler)(output,
5726 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005727 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005728}
5729
5730void
5731output_set_user_data(struct output *output, void *data)
5732{
5733 output->user_data = data;
5734}
5735
5736void *
5737output_get_user_data(struct output *output)
5738{
5739 return output->user_data;
5740}
5741
5742void
5743output_set_destroy_handler(struct output *output,
5744 display_output_handler_t handler)
5745{
5746 output->destroy_handler = handler;
5747 /* FIXME: implement this, once we have way to remove outputs */
5748}
5749
5750void
Scott Moreau4e072362012-09-29 02:03:11 -06005751output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005752{
Scott Moreau4e072362012-09-29 02:03:11 -06005753 struct rectangle allocation = output->allocation;
5754
5755 switch (output->transform) {
5756 case WL_OUTPUT_TRANSFORM_90:
5757 case WL_OUTPUT_TRANSFORM_270:
5758 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5759 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5760 /* Swap width and height */
5761 allocation.width = output->allocation.height;
5762 allocation.height = output->allocation.width;
5763 break;
5764 }
5765
5766 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005767}
5768
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005769struct wl_output *
5770output_get_wl_output(struct output *output)
5771{
5772 return output->output;
5773}
5774
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005775enum wl_output_transform
5776output_get_transform(struct output *output)
5777{
5778 return output->transform;
5779}
5780
Alexander Larssonafd319a2013-05-22 14:41:27 +02005781uint32_t
5782output_get_scale(struct output *output)
5783{
5784 return output->scale;
5785}
5786
Jason Ekstrand738715d2014-04-02 19:53:50 -05005787const char *
5788output_get_make(struct output *output)
5789{
5790 return output->make;
5791}
5792
5793const char *
5794output_get_model(struct output *output)
5795{
5796 return output->model;
5797}
5798
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005799static void
Daniel Stone97f68542012-05-30 16:32:01 +01005800fini_xkb(struct input *input)
5801{
5802 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005803 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005804}
5805
5806static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005807display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005808{
5809 struct input *input;
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01005810 int seat_version = MIN(display_seat_version, 6);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005811
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005812 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005813 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005814 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005815 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005816 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005817 input->pointer_focus = NULL;
5818 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005819 input->seat_version = seat_version;
5820
Rusty Lynch041815a2013-08-08 21:20:38 -07005821 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005822 wl_list_insert(d->input_list.prev, &input->link);
5823
Daniel Stone37816df2012-05-16 18:45:18 +01005824 wl_seat_add_listener(input->seat, &seat_listener, input);
5825 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005826
Jason Ekstranda669bd52014-04-02 19:53:51 -05005827 if (d->data_device_manager) {
5828 input->data_device =
5829 wl_data_device_manager_get_data_device(d->data_device_manager,
5830 input->seat);
5831 wl_data_device_add_listener(input->data_device,
5832 &data_device_listener,
5833 input);
5834 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005835
5836 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005837
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005838 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5839 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005840
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005841 set_repeat_info(input, 40, 400);
5842 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5843 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005844}
5845
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005846static void
Harish Krupoee4c7a22019-04-19 01:53:27 +05305847display_add_data_device(struct display *d, uint32_t id, int ddm_version)
5848{
5849 struct input *input;
5850
5851 d->data_device_manager_version = MIN(ddm_version, 3);
5852 d->data_device_manager =
5853 wl_registry_bind(d->registry, id,
5854 &wl_data_device_manager_interface,
5855 d->data_device_manager_version);
5856
5857 wl_list_for_each(input, &d->input_list, link) {
5858 if (!input->data_device) {
5859 input->data_device =
5860 wl_data_device_manager_get_data_device(d->data_device_manager,
5861 input->seat);
5862 wl_data_device_add_listener(input->data_device,
5863 &data_device_listener,
5864 input);
5865 }
5866 }
5867}
5868
5869static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005870input_destroy(struct input *input)
5871{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005872 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005873 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005874
5875 if (input->drag_offer)
5876 data_offer_destroy(input->drag_offer);
5877
5878 if (input->selection_offer)
5879 data_offer_destroy(input->selection_offer);
5880
kabeer khan6ce67ec2014-10-20 11:55:29 +05305881 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005882 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305883 wl_data_device_release(input->data_device);
5884 else
5885 wl_data_device_destroy(input->data_device);
5886 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005887 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005888 if (input->touch)
5889 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005890 if (input->pointer)
5891 wl_pointer_release(input->pointer);
5892 if (input->keyboard)
5893 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005894 } else {
5895 if (input->touch)
5896 wl_touch_destroy(input->touch);
5897 if (input->pointer)
5898 wl_pointer_destroy(input->pointer);
5899 if (input->keyboard)
5900 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005901 }
5902
Daniel Stone97f68542012-05-30 16:32:01 +01005903 fini_xkb(input);
5904
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005905 wl_surface_destroy(input->pointer_surface);
5906
Pekka Paalanene1207c72011-12-16 12:02:09 +02005907 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005908 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005909 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005910 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005911 free(input);
5912}
5913
5914static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005915shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5916{
5917 struct display *d = data;
5918
5919 if (format == WL_SHM_FORMAT_RGB565)
5920 d->has_rgb565 = 1;
5921}
5922
5923struct wl_shm_listener shm_listener = {
5924 shm_format
5925};
5926
5927static void
ant8mef99fac22018-11-28 22:46:37 +01005928xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005929{
ant8mef99fac22018-11-28 22:46:37 +01005930 xdg_wm_base_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005931}
5932
ant8mef99fac22018-11-28 22:46:37 +01005933static const struct xdg_wm_base_listener wm_base_listener = {
5934 xdg_wm_base_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005935};
5936
5937static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005938registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5939 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005940{
5941 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005942 struct global *global;
5943
Brian Lovinbc919262013-08-07 15:34:59 -07005944 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005945 global->name = id;
5946 global->interface = strdup(interface);
5947 global->version = version;
5948 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005949
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005950 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005951 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005952 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005953 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005954 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005955 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005956 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005957 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5958 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5959 d->relative_pointer_manager =
5960 wl_registry_bind(registry, id,
5961 &zwp_relative_pointer_manager_v1_interface,
5962 1);
5963 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5964 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5965 d->pointer_constraints =
5966 wl_registry_bind(registry, id,
5967 &zwp_pointer_constraints_v1_interface,
5968 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005969 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005970 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005971 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005972 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Harish Krupoee4c7a22019-04-19 01:53:27 +05305973 display_add_data_device(d, id, version);
ant8mef99fac22018-11-28 22:46:37 +01005974 } else if (strcmp(interface, "xdg_wm_base") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005975 d->xdg_shell = wl_registry_bind(registry, id,
ant8mef99fac22018-11-28 22:46:37 +01005976 &xdg_wm_base_interface, 1);
5977 xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005978 } else if (strcmp(interface, "text_cursor_position") == 0) {
5979 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005980 wl_registry_bind(registry, id,
5981 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005982 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5983 d->subcompositor =
5984 wl_registry_bind(registry, id,
5985 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005986 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005987
5988 if (d->global_handler)
5989 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005990}
5991
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005992static void
5993registry_handle_global_remove(void *data, struct wl_registry *registry,
5994 uint32_t name)
5995{
5996 struct display *d = data;
5997 struct global *global;
5998 struct global *tmp;
5999
6000 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6001 if (global->name != name)
6002 continue;
6003
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006004 if (strcmp(global->interface, "wl_output") == 0)
6005 display_destroy_output(d, name);
6006
6007 /* XXX: Should destroy remaining bound globals */
6008
6009 if (d->global_handler_remove)
6010 d->global_handler_remove(d, name, global->interface,
6011 global->version, d->user_data);
6012
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006013 wl_list_remove(&global->link);
6014 free(global->interface);
6015 free(global);
6016 }
6017}
6018
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006019void *
6020display_bind(struct display *display, uint32_t name,
6021 const struct wl_interface *interface, uint32_t version)
6022{
6023 return wl_registry_bind(display->registry, name, interface, version);
6024}
6025
6026static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006027 registry_handle_global,
6028 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006029};
6030
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006031#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006032static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006033init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006034{
6035 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006036 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006037
Rob Clark6396ed32012-03-11 19:48:41 -05006038#ifdef USE_CAIRO_GLESV2
6039# define GL_BIT EGL_OPENGL_ES2_BIT
6040#else
6041# define GL_BIT EGL_OPENGL_BIT
6042#endif
6043
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006044 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006045 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006046 EGL_RED_SIZE, 1,
6047 EGL_GREEN_SIZE, 1,
6048 EGL_BLUE_SIZE, 1,
6049 EGL_ALPHA_SIZE, 1,
6050 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006051 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006052 EGL_NONE
6053 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006054
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006055#ifdef USE_CAIRO_GLESV2
6056 static const EGLint context_attribs[] = {
6057 EGL_CONTEXT_CLIENT_VERSION, 2,
6058 EGL_NONE
6059 };
6060 EGLint api = EGL_OPENGL_ES_API;
6061#else
6062 EGLint *context_attribs = NULL;
6063 EGLint api = EGL_OPENGL_API;
6064#endif
6065
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006066 d->dpy =
6067 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6068 d->display, NULL);
6069
Yuval Fledel45568f62010-12-06 09:18:12 -05006070 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006071 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006072 return -1;
6073 }
6074
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006075 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006076 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006077 return -1;
6078 }
6079
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006080 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6081 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006082 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006083 return -1;
6084 }
6085
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006086 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006087 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006088 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006089 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006090 return -1;
6091 }
6092
Benjamin Franzke0c991632011-09-27 21:57:31 +02006093 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6094 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006095 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006096 return -1;
6097 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006098
6099 return 0;
6100}
6101
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006102static void
6103fini_egl(struct display *display)
6104{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006105 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006106
6107 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6108 EGL_NO_CONTEXT);
6109
6110 eglTerminate(display->dpy);
6111 eglReleaseThread();
6112}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006113#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006114
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006115static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006116init_dummy_surface(struct display *display)
6117{
6118 int len;
6119 void *data;
6120
6121 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006122 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006123 display->dummy_surface =
6124 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6125 1, 1, len);
6126 display->dummy_surface_data = data;
6127}
6128
6129static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006130handle_display_data(struct task *task, uint32_t events)
6131{
6132 struct display *display =
6133 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006134 struct epoll_event ep;
6135 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006136
6137 display->display_fd_events = events;
6138
6139 if (events & EPOLLERR || events & EPOLLHUP) {
6140 display_exit(display);
6141 return;
6142 }
6143
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006144 if (events & EPOLLIN) {
6145 ret = wl_display_dispatch(display->display);
6146 if (ret == -1) {
6147 display_exit(display);
6148 return;
6149 }
6150 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006151
6152 if (events & EPOLLOUT) {
6153 ret = wl_display_flush(display->display);
6154 if (ret == 0) {
6155 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6156 ep.data.ptr = &display->display_task;
6157 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6158 display->display_fd, &ep);
6159 } else if (ret == -1 && errno != EAGAIN) {
6160 display_exit(display);
6161 return;
6162 }
6163 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006164}
6165
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006166static void
6167log_handler(const char *format, va_list args)
6168{
6169 vfprintf(stderr, format, args);
6170}
6171
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006172struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006173display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006174{
6175 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006176
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006177 wl_log_set_handler_client(log_handler);
6178
Peter Huttererf3d62272013-08-08 11:57:05 +10006179 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006180 if (d == NULL)
6181 return NULL;
6182
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006183 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006184 if (d->display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02006185 fprintf(stderr, "failed to connect to Wayland display: %s\n",
6186 strerror(errno));
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006187 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006188 return NULL;
6189 }
6190
Rob Bradford5ab9c752013-07-26 16:29:43 +01006191 d->xkb_context = xkb_context_new(0);
6192 if (d->xkb_context == NULL) {
6193 fprintf(stderr, "Failed to create XKB context\n");
6194 free(d);
6195 return NULL;
6196 }
6197
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006198 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006199 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006200 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006201 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6202 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006203
6204 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006205 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006206 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006207 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006208
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006209 d->registry = wl_display_get_registry(d->display);
6210 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006211
Marek Chalupaa519d062014-12-05 13:49:40 +01006212 if (wl_display_roundtrip(d->display) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006213 fprintf(stderr, "Failed to process Wayland connection: %s\n",
6214 strerror(errno));
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006215 return NULL;
6216 }
6217
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006218#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006219 if (init_egl(d) < 0)
6220 fprintf(stderr, "EGL does not seem to work, "
6221 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006222#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006223
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006224 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006225
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006226 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006227
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006228 wl_list_init(&d->window_list);
6229
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006230 init_dummy_surface(d);
6231
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006232 return d;
6233}
6234
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006235static void
6236display_destroy_outputs(struct display *display)
6237{
6238 struct output *tmp;
6239 struct output *output;
6240
6241 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6242 output_destroy(output);
6243}
6244
Pekka Paalanene1207c72011-12-16 12:02:09 +02006245static void
6246display_destroy_inputs(struct display *display)
6247{
6248 struct input *tmp;
6249 struct input *input;
6250
6251 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6252 input_destroy(input);
6253}
6254
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006255void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006256display_destroy(struct display *display)
6257{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006258 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006259 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6260 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006261
6262 if (!wl_list_empty(&display->deferred_list))
6263 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6264
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006265 cairo_surface_destroy(display->dummy_surface);
6266 free(display->dummy_surface_data);
6267
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006268 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006269 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006270
Daniel Stone97f68542012-05-30 16:32:01 +01006271 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006272
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006273 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006274 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006275
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006276#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006277 if (display->argb_device)
6278 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006279#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006280
Pekka Paalanen35e82632013-04-25 13:57:48 +03006281 if (display->subcompositor)
6282 wl_subcompositor_destroy(display->subcompositor);
6283
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006284 if (display->xdg_shell)
ant8mef99fac22018-11-28 22:46:37 +01006285 xdg_wm_base_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006286
6287 if (display->shm)
6288 wl_shm_destroy(display->shm);
6289
6290 if (display->data_device_manager)
6291 wl_data_device_manager_destroy(display->data_device_manager);
6292
6293 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006294 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006295
6296 close(display->epoll_fd);
6297
U. Artie Eoff44874d92012-10-02 21:12:35 -07006298 if (!(display->display_fd_events & EPOLLERR) &&
6299 !(display->display_fd_events & EPOLLHUP))
6300 wl_display_flush(display->display);
6301
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006302 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006303 free(display);
6304}
6305
6306void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006307display_set_user_data(struct display *display, void *data)
6308{
6309 display->user_data = data;
6310}
6311
6312void *
6313display_get_user_data(struct display *display)
6314{
6315 return display->user_data;
6316}
6317
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006318struct wl_display *
6319display_get_display(struct display *display)
6320{
6321 return display->display;
6322}
6323
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006324int
6325display_has_subcompositor(struct display *display)
6326{
6327 if (display->subcompositor)
6328 return 1;
6329
6330 wl_display_roundtrip(display->display);
6331
6332 return display->subcompositor != NULL;
6333}
6334
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006335cairo_device_t *
6336display_get_cairo_device(struct display *display)
6337{
6338 return display->argb_device;
6339}
6340
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006341struct output *
6342display_get_output(struct display *display)
6343{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006344 if (wl_list_empty(&display->output_list))
6345 return NULL;
6346
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006347 return container_of(display->output_list.next, struct output, link);
6348}
6349
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006350struct wl_compositor *
6351display_get_compositor(struct display *display)
6352{
6353 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006354}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006355
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006356uint32_t
6357display_get_serial(struct display *display)
6358{
6359 return display->serial;
6360}
6361
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006362EGLDisplay
6363display_get_egl_display(struct display *d)
6364{
6365 return d->dpy;
6366}
6367
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006368struct wl_data_source *
6369display_create_data_source(struct display *display)
6370{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006371 if (display->data_device_manager)
6372 return wl_data_device_manager_create_data_source(display->data_device_manager);
6373 else
6374 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006375}
6376
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006377EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006378display_get_argb_egl_config(struct display *d)
6379{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006380 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006381}
6382
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006383int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006384display_acquire_window_surface(struct display *display,
6385 struct window *window,
6386 EGLContext ctx)
6387{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006388 struct surface *surface = window->main_surface;
6389
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006390 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006391 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006392
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006393 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006394 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006395}
6396
6397void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006398display_release_window_surface(struct display *display,
6399 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006400{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006401 struct surface *surface = window->main_surface;
6402
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006403 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006404 return;
6405
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006406 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006407}
6408
6409void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006410display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006411{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006412 wl_list_insert(&display->deferred_list, &task->link);
6413}
6414
6415void
6416display_watch_fd(struct display *display,
6417 int fd, uint32_t events, struct task *task)
6418{
6419 struct epoll_event ep;
6420
6421 ep.events = events;
6422 ep.data.ptr = task;
6423 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6424}
6425
6426void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006427display_unwatch_fd(struct display *display, int fd)
6428{
6429 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6430}
6431
6432void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006433display_run(struct display *display)
6434{
6435 struct task *task;
6436 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006437 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006438
Pekka Paalanen826d7952011-12-15 10:14:07 +02006439 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006440 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006441 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006442 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006443 struct task, link);
6444 wl_list_remove(&task->link);
6445 task->run(task, 0);
6446 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006447
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006448 wl_display_dispatch_pending(display->display);
6449
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006450 if (!display->running)
6451 break;
6452
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006453 ret = wl_display_flush(display->display);
6454 if (ret < 0 && errno == EAGAIN) {
6455 ep[0].events =
6456 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6457 ep[0].data.ptr = &display->display_task;
6458
6459 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6460 display->display_fd, &ep[0]);
6461 } else if (ret < 0) {
6462 break;
6463 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006464
6465 count = epoll_wait(display->epoll_fd,
6466 ep, ARRAY_LENGTH(ep), -1);
6467 for (i = 0; i < count; i++) {
6468 task = ep[i].data.ptr;
6469 task->run(task, ep[i].events);
6470 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006471 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006472}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006473
6474void
6475display_exit(struct display *display)
6476{
6477 display->running = 0;
6478}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006479
Carlos Garnacho9c931792016-01-18 23:52:12 +01006480int
6481display_get_data_device_manager_version(struct display *display)
6482{
6483 return display->data_device_manager_version;
6484}
6485
Jan Arne Petersencd997062012-11-18 19:06:44 +01006486void
6487keysym_modifiers_add(struct wl_array *modifiers_map,
6488 const char *name)
6489{
6490 size_t len = strlen(name) + 1;
6491 char *p;
6492
6493 p = wl_array_add(modifiers_map, len);
6494
6495 if (p == NULL)
6496 return;
6497
6498 strncpy(p, name, len);
6499}
6500
6501static xkb_mod_index_t
6502keysym_modifiers_get_index(struct wl_array *modifiers_map,
6503 const char *name)
6504{
6505 xkb_mod_index_t index = 0;
6506 char *p = modifiers_map->data;
6507
6508 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6509 if (strcmp(p, name) == 0)
6510 return index;
6511
6512 index++;
6513 p += strlen(p) + 1;
6514 }
6515
6516 return XKB_MOD_INVALID;
6517}
6518
6519xkb_mod_mask_t
6520keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6521 const char *name)
6522{
6523 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6524
6525 if (index == XKB_MOD_INVALID)
6526 return XKB_MOD_INVALID;
6527
6528 return 1 << index;
6529}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006530
6531static void
6532toytimer_fire(struct task *tsk, uint32_t events)
6533{
6534 uint64_t e;
6535 struct toytimer *tt;
6536
6537 tt = container_of(tsk, struct toytimer, tsk);
6538
6539 if (events != EPOLLIN)
6540 fprintf(stderr, "unexpected timerfd events %x\n", events);
6541
6542 if (!(events & EPOLLIN))
6543 return;
6544
6545 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6546 /* If we change the timer between the fd becoming
6547 * readable and getting here, there'll be nothing to
6548 * read and we get EAGAIN. */
6549 if (errno != EAGAIN)
Antonio Borneo39578632019-04-26 23:57:31 +02006550 fprintf(stderr, "timer read failed: %s\n",
6551 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006552 return;
6553 }
6554
6555 tt->callback(tt);
6556}
6557
6558void
6559toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6560 toytimer_cb callback)
6561{
6562 memset(tt, 0, sizeof *tt);
6563
6564 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6565 if (tt->fd == -1) {
Antonio Borneo39578632019-04-26 23:57:31 +02006566 fprintf(stderr, "creating timer failed: %s\n",
6567 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006568 abort();
6569 }
6570
6571 tt->display = display;
6572 tt->callback = callback;
6573 tt->tsk.run = toytimer_fire;
6574 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6575}
6576
6577void
6578toytimer_fini(struct toytimer *tt)
6579{
6580 display_unwatch_fd(tt->display, tt->fd);
6581 close(tt->fd);
6582 tt->fd = -1;
6583}
6584
6585void
6586toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6587{
6588 int ret;
6589
6590 ret = timerfd_settime(tt->fd, 0, its, NULL);
6591 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006592 fprintf(stderr, "timer setup failed: %s\n", strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006593 abort();
6594 }
6595}
6596
6597#define USEC_PER_SEC 1000000
6598
6599void
6600toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6601{
6602 struct itimerspec its;
6603
6604 its.it_interval.tv_sec = 0;
6605 its.it_interval.tv_nsec = 0;
6606 its.it_value.tv_sec = usec / USEC_PER_SEC;
6607 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6608 toytimer_arm(tt, &its);
6609}
6610
6611void
6612toytimer_disarm(struct toytimer *tt)
6613{
6614 struct itimerspec its = {};
6615
6616 toytimer_arm(tt, &its);
6617}