blob: 985d3958854ab283fbf0a6215aa136b8b1c5d9c9 [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"
Harish Krupo7bcbab12018-12-11 13:45:43 +053085#include "viewporter-client-protocol.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050086
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080087#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
88#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
89
emersionac71ee52018-11-18 21:42:10 +010090#define DEFAULT_XCURSOR_SIZE 32
91
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070092struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030093
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040094struct global {
95 uint32_t name;
96 char *interface;
97 uint32_t version;
98 struct wl_list link;
99};
100
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500101struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500102 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400103 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500104 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300105 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400106 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400107 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600108 struct text_cursor_position *text_cursor_position;
ant8mef99fac22018-11-28 22:46:37 +0100109 struct xdg_wm_base *xdg_shell;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800110 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
111 struct zwp_pointer_constraints_v1 *pointer_constraints;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400112 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500113 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200114 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200115 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400116 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117
118 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700119 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400120 struct task display_task;
121
122 int epoll_fd;
123 struct wl_list deferred_list;
124
Pekka Paalanen826d7952011-12-15 10:14:07 +0200125 int running;
126
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400127 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400128 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400129 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500130 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400131
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400132 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400133
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300134 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300135 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400136
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200137 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400138 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800139 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200140
141 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100142
143 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200144
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200145 /* A hack to get text extents for tooltips */
146 cairo_surface_t *dummy_surface;
147 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200148
kabeer khan6ce67ec2014-10-20 11:55:29 +0530149 int data_device_manager_version;
Harish Krupo7bcbab12018-12-11 13:45:43 +0530150 struct wp_viewporter *viewporter;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500151};
152
Rob Bradford7507b572012-05-15 17:55:34 +0100153struct window_output {
154 struct output *output;
155 struct wl_list link;
156};
157
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200158struct toysurface {
159 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500160 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800161 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200162 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200163 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200164 * If flags has SURFACE_HINT_RESIZE set, the user is
165 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200166 * Returns the Cairo surface to draw to.
167 */
168 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200169 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200170 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200171
172 /*
173 * Post the surface to the server, returning the server allocation
174 * rectangle. The Cairo surface from prepare() must be destroyed
175 * after calling this.
176 */
177 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200178 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200179 struct rectangle *server_allocation);
180
181 /*
182 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800183 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200184 */
185 int (*acquire)(struct toysurface *base, EGLContext ctx);
186
187 /*
188 * Release the toysurface from the EGL context, returning control
189 * to Cairo.
190 */
191 void (*release)(struct toysurface *base);
192
193 /*
194 * Destroy the toysurface, including the Cairo surface, any
195 * backing storage, and the Wayland protocol objects.
196 */
197 void (*destroy)(struct toysurface *base);
198};
199
Pekka Paalanen4e373742013-02-13 16:17:13 +0200200struct surface {
201 struct window *window;
202
203 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300204 struct wl_subsurface *subsurface;
205 int synchronized;
206 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200207 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200208 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300209 int redraw_needed;
210 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300211 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200212
213 struct rectangle allocation;
214 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200215
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200216 struct wl_region *input_region;
217 struct wl_region *opaque_region;
218
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200219 enum window_buffer_type buffer_type;
220 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200221 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200222
223 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300224
225 struct wl_list link;
Harish Krupo7bcbab12018-12-11 13:45:43 +0530226 struct wp_viewport *viewport;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200227};
228
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500229struct window {
230 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100231 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500232 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200233 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400234 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500235 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200236 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500237 int x, y;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800238 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400239 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300240 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400241 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400242 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500243 int custom;
244 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400245
Pekka Paalanen99436862012-11-19 17:15:59 +0200246 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400247
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500248 int fullscreen;
249 int maximized;
250
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;
Harish Krupo7bcbab12018-12-11 13:45:43 +0530323 int viewport_dest_width;
324 int viewport_dest_height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400325};
326
Rusty Lynch041815a2013-08-08 21:20:38 -0700327struct touch_point {
328 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800329 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700330 struct widget *widget;
331 struct wl_list link;
332};
333
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400334struct input {
335 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100336 struct wl_seat *seat;
337 struct wl_pointer *pointer;
338 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700339 struct wl_touch *touch;
340 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400341 struct window *pointer_focus;
342 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700343 struct window *touch_focus;
Dima Ryazanov13bdf252018-11-14 22:17:42 -0800344 struct window *locked_window;
345 struct window *confined_window;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300346 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300347 uint32_t cursor_anim_start;
348 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500349 uint32_t cursor_timer_start;
350 uint32_t cursor_anim_current;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200351 struct toytimer cursor_timer;
Derek Foreman118a4292015-04-22 17:23:35 -0500352 bool cursor_timer_running;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300353 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400354 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400355 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400356 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400357 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400358 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400359
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500360 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500361 struct widget *grab;
362 uint32_t grab_button;
363
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400364 struct wl_data_device *data_device;
365 struct data_offer *drag_offer;
366 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800367 uint32_t touch_grab;
368 int32_t touch_grab_id;
369 float drag_x, drag_y;
370 struct window *drag_focus;
371 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100372
373 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100374 struct xkb_keymap *keymap;
375 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +0100376#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700377 struct xkb_compose_table *compose_table;
378 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +0100379#endif
Daniel Stone97f68542012-05-30 16:32:01 +0100380 xkb_mod_mask_t control_mask;
381 xkb_mod_mask_t alt_mask;
382 xkb_mod_mask_t shift_mask;
383 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400384
Jonny Lamb06959082014-08-12 14:58:27 +0200385 int32_t repeat_rate_sec;
386 int32_t repeat_rate_nsec;
387 int32_t repeat_delay_sec;
388 int32_t repeat_delay_nsec;
389
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200390 struct toytimer repeat_timer;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400391 uint32_t repeat_sym;
392 uint32_t repeat_key;
393 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500394 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400395};
396
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500397struct output {
398 struct display *display;
399 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800400 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500401 struct rectangle allocation;
402 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600403 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200404 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500405 char *make;
406 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200407
408 display_output_handler_t destroy_handler;
409 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500410};
411
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500412struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500413 struct widget *widget;
414 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500415 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800416
417 uint32_t last_time;
418 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800419 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500420};
421
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500422struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400423 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500424 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500425 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500426 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700427 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500428 const char **entries;
429 uint32_t time;
430 int current;
431 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400432 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500433 menu_func_t func;
434};
435
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300436struct tooltip {
437 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300438 struct widget *widget;
439 char *entry;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200440 struct toytimer timer;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300441 float x, y;
442};
443
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700444struct shm_pool {
445 struct wl_shm_pool *pool;
446 size_t size;
447 size_t used;
448 void *data;
449};
450
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400451enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300452 CURSOR_DEFAULT = 100,
453 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400454};
455
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200456static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457
Pekka Paalanen97777442013-05-22 10:20:05 +0300458/* #define DEBUG */
459
460#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300461
462static void
463debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
464__attribute__ ((format (printf, 4, 5)));
465
466static void
467debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
468{
469 va_list ap;
470 struct timeval tv;
471
472 gettimeofday(&tv, NULL);
473 fprintf(stderr, "%8ld.%03ld ",
474 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
475
476 if (proxy)
477 fprintf(stderr, "%s@%d ",
478 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
479
480 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
481 fprintf(stderr, "%s ", func);
482
483 va_start(ap, fmt);
484 vfprintf(stderr, fmt, ap);
485 va_end(ap);
486}
487
488#define DBG(fmt, ...) \
489 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
490
491#define DBG_OBJ(obj, fmt, ...) \
492 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
493
494#else
495
496#define DBG(...) do {} while (0)
497#define DBG_OBJ(...) do {} while (0)
498
499#endif
500
Alexander Larsson1818e312013-05-22 14:41:31 +0200501static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200502surface_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 +0200503{
504 int32_t tmp;
505
506 switch (buffer_transform) {
507 case WL_OUTPUT_TRANSFORM_90:
508 case WL_OUTPUT_TRANSFORM_270:
509 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
510 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
511 tmp = *width;
512 *width = *height;
513 *height = tmp;
514 break;
515 default:
516 break;
517 }
518
519 *width *= buffer_scale;
520 *height *= buffer_scale;
521}
522
523static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200524buffer_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 +0200525{
526 int32_t tmp;
527
528 switch (buffer_transform) {
529 case WL_OUTPUT_TRANSFORM_90:
530 case WL_OUTPUT_TRANSFORM_270:
531 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
532 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
533 tmp = *width;
534 *width = *height;
535 *height = tmp;
536 break;
537 default:
538 break;
539 }
540
541 *width /= buffer_scale;
542 *height /= buffer_scale;
543}
544
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500545#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547struct egl_window_surface {
548 struct toysurface base;
549 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100550 struct display *display;
551 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200552 struct wl_egl_window *egl_window;
553 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100554};
555
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556static struct egl_window_surface *
557to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100560}
561
562static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200564 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200565 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100566{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200567 struct egl_window_surface *surface = to_egl_window_surface(base);
568
Alexander Larsson1818e312013-05-22 14:41:31 +0200569 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
570
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
572 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
573
574 return cairo_surface_reference(surface->cairo_surface);
575}
576
577static void
578egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200579 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200580 struct rectangle *server_allocation)
581{
582 struct egl_window_surface *surface = to_egl_window_surface(base);
583
584 cairo_gl_surface_swapbuffers(surface->cairo_surface);
585 wl_egl_window_get_attached_size(surface->egl_window,
586 &server_allocation->width,
587 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200588
589 buffer_to_surface_size (buffer_transform, buffer_scale,
590 &server_allocation->width,
591 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200592}
593
594static int
595egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
596{
597 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200598 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100599
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200600 device = cairo_surface_get_device(surface->cairo_surface);
601 if (!device)
602 return -1;
603
604 if (!ctx) {
605 if (device == surface->display->argb_device)
606 ctx = surface->display->argb_ctx;
607 else
608 assert(0);
609 }
610
611 cairo_device_flush(device);
612 cairo_device_acquire(device);
613 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
614 surface->egl_surface, ctx))
615 fprintf(stderr, "failed to make surface current\n");
616
617 return 0;
618}
619
620static void
621egl_window_surface_release(struct toysurface *base)
622{
623 struct egl_window_surface *surface = to_egl_window_surface(base);
624 cairo_device_t *device;
625
626 device = cairo_surface_get_device(surface->cairo_surface);
627 if (!device)
628 return;
629
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200630 if (!eglMakeCurrent(surface->display->dpy,
631 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200632 fprintf(stderr, "failed to make context current\n");
633
634 cairo_device_release(device);
635}
636
637static void
638egl_window_surface_destroy(struct toysurface *base)
639{
640 struct egl_window_surface *surface = to_egl_window_surface(base);
641 struct display *d = surface->display;
642
643 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000644 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200645 wl_egl_window_destroy(surface->egl_window);
646 surface->surface = NULL;
647
648 free(surface);
649}
650
651static struct toysurface *
652egl_window_surface_create(struct display *display,
653 struct wl_surface *wl_surface,
654 uint32_t flags,
655 struct rectangle *rectangle)
656{
657 struct egl_window_surface *surface;
658
Pekka Paalanenb3627362012-11-19 17:16:00 +0200659 if (display->dpy == EGL_NO_DISPLAY)
660 return NULL;
661
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800662 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200663 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100664 return NULL;
665
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200666 surface->base.prepare = egl_window_surface_prepare;
667 surface->base.swap = egl_window_surface_swap;
668 surface->base.acquire = egl_window_surface_acquire;
669 surface->base.release = egl_window_surface_release;
670 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100671
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200672 surface->display = display;
673 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400674
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200675 surface->egl_window = wl_egl_window_create(surface->surface,
676 rectangle->width,
677 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100678
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100679 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100680 weston_platform_create_egl_surface(display->dpy,
681 display->argb_config,
682 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100683
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200684 surface->cairo_surface =
685 cairo_gl_surface_create_for_egl(display->argb_device,
686 surface->egl_surface,
687 rectangle->width,
688 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100689
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200690 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100691}
692
Pekka Paalanenb3627362012-11-19 17:16:00 +0200693#else
694
695static struct toysurface *
696egl_window_surface_create(struct display *display,
697 struct wl_surface *wl_surface,
698 uint32_t flags,
699 struct rectangle *rectangle)
700{
701 return NULL;
702}
703
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704#endif
705
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200706struct shm_surface_data {
707 struct wl_buffer *buffer;
708 struct shm_pool *pool;
709};
710
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400711struct wl_buffer *
712display_get_buffer_for_surface(struct display *display,
713 cairo_surface_t *surface)
714{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200715 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400716
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200717 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400718
719 return data->buffer;
720}
721
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500722static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723shm_pool_destroy(struct shm_pool *pool);
724
725static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400726shm_surface_data_destroy(void *p)
727{
728 struct shm_surface_data *data = p;
729
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200730 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731 if (data->pool)
732 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300733
734 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400735}
736
Kristian Høgsberg16626282012-04-03 11:21:27 -0400737static struct wl_shm_pool *
738make_shm_pool(struct display *display, int size, void **data)
739{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400740 struct wl_shm_pool *pool;
741 int fd;
742
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300743 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400744 if (fd < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200745 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
746 size, strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400747 return NULL;
748 }
749
750 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400751 if (*data == MAP_FAILED) {
Antonio Borneo39578632019-04-26 23:57:31 +0200752 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400753 close(fd);
754 return NULL;
755 }
756
757 pool = wl_shm_create_pool(display->shm, fd, size);
758
759 close(fd);
760
761 return pool;
762}
763
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700764static struct shm_pool *
765shm_pool_create(struct display *display, size_t size)
766{
767 struct shm_pool *pool = malloc(sizeof *pool);
768
769 if (!pool)
770 return NULL;
771
772 pool->pool = make_shm_pool(display, size, &pool->data);
773 if (!pool->pool) {
774 free(pool);
775 return NULL;
776 }
777
778 pool->size = size;
779 pool->used = 0;
780
781 return pool;
782}
783
784static void *
785shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
786{
787 if (pool->used + size > pool->size)
788 return NULL;
789
790 *offset = pool->used;
791 pool->used += size;
792
793 return (char *) pool->data + *offset;
794}
795
796/* destroy the pool. this does not unmap the memory though */
797static void
798shm_pool_destroy(struct shm_pool *pool)
799{
800 munmap(pool->data, pool->size);
801 wl_shm_pool_destroy(pool->pool);
802 free(pool);
803}
804
805/* Start allocating from the beginning of the pool again */
806static void
807shm_pool_reset(struct shm_pool *pool)
808{
809 pool->used = 0;
810}
811
812static int
813data_length_for_shm_surface(struct rectangle *rect)
814{
815 int stride;
816
817 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
818 rect->width);
819 return stride * rect->height;
820}
821
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500822static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700823display_create_shm_surface_from_pool(struct display *display,
824 struct rectangle *rectangle,
825 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826{
827 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400828 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400829 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700830 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400831 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832
833 data = malloc(sizeof *data);
834 if (data == NULL)
835 return NULL;
836
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300837 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
838 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700839 length = stride * rectangle->height;
840 data->pool = NULL;
841 map = shm_pool_allocate(pool, length, &offset);
842
843 if (!map) {
844 free(data);
845 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400846 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400848 surface = cairo_image_surface_create_for_data (map,
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300849 CAIRO_FORMAT_ARGB32,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850 rectangle->width,
851 rectangle->height,
852 stride);
853
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200854 cairo_surface_set_user_data(surface, &shm_surface_data_key,
855 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300857 if (flags & SURFACE_OPAQUE)
858 format = WL_SHM_FORMAT_XRGB8888;
859 else
860 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400861
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200862 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
863 rectangle->width,
864 rectangle->height,
865 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400866
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700867 return surface;
868}
869
870static cairo_surface_t *
871display_create_shm_surface(struct display *display,
872 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200873 struct shm_pool *alternate_pool,
874 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700875{
876 struct shm_surface_data *data;
877 struct shm_pool *pool;
878 cairo_surface_t *surface;
879
Pekka Paalanen99436862012-11-19 17:15:59 +0200880 if (alternate_pool) {
881 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700882 surface = display_create_shm_surface_from_pool(display,
883 rectangle,
884 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200885 alternate_pool);
886 if (surface) {
887 data = cairo_surface_get_user_data(surface,
888 &shm_surface_data_key);
889 goto out;
890 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700891 }
892
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300893 pool = shm_pool_create(display, data_length_for_shm_surface(rectangle));
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700894 if (!pool)
895 return NULL;
896
897 surface =
898 display_create_shm_surface_from_pool(display, rectangle,
899 flags, pool);
900
901 if (!surface) {
902 shm_pool_destroy(pool);
903 return NULL;
904 }
905
906 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200907 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700908 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909
Pekka Paalanen99436862012-11-19 17:15:59 +0200910out:
911 if (data_ret)
912 *data_ret = data;
913
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400914 return surface;
915}
916
nobled7b87cb02011-02-01 18:51:47 +0000917static int
918check_size(struct rectangle *rect)
919{
920 if (rect->width && rect->height)
921 return 0;
922
923 fprintf(stderr, "tried to create surface of "
924 "width: %d, height: %d\n", rect->width, rect->height);
925 return -1;
926}
927
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928cairo_surface_t *
929display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100930 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400931 struct rectangle *rectangle,
932 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400933{
nobled7b87cb02011-02-01 18:51:47 +0000934 if (check_size(rectangle) < 0)
935 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200936
937 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200938 return display_create_shm_surface(display, rectangle, flags,
939 NULL, NULL);
940}
941
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942struct shm_surface_leaf {
943 cairo_surface_t *cairo_surface;
944 /* 'data' is automatically destroyed, when 'cairo_surface' is */
945 struct shm_surface_data *data;
946
947 struct shm_pool *resize_pool;
948 int busy;
949};
950
951static void
952shm_surface_leaf_release(struct shm_surface_leaf *leaf)
953{
954 if (leaf->cairo_surface)
955 cairo_surface_destroy(leaf->cairo_surface);
956 /* leaf->data already destroyed via cairo private */
957
958 if (leaf->resize_pool)
959 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200960
961 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200962}
963
Pekka Paalanenaef02542013-04-25 13:57:47 +0300964#define MAX_LEAVES 3
965
Pekka Paalanen99436862012-11-19 17:15:59 +0200966struct shm_surface {
967 struct toysurface base;
968 struct display *display;
969 struct wl_surface *surface;
970 uint32_t flags;
971 int dx, dy;
972
Pekka Paalanenaef02542013-04-25 13:57:47 +0300973 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200974 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200975};
976
977static struct shm_surface *
978to_shm_surface(struct toysurface *base)
979{
980 return container_of(base, struct shm_surface, base);
981}
982
Pekka Paalanena4eda732012-11-19 17:16:02 +0200983static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300984shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
985{
986#ifdef DEBUG
987 struct shm_surface_leaf *leaf;
988 char bufs[MAX_LEAVES + 1];
989 int i;
990
991 for (i = 0; i < MAX_LEAVES; i++) {
992 leaf = &surface->leaf[i];
993
994 if (leaf->busy)
995 bufs[i] = 'b';
996 else if (leaf->cairo_surface)
997 bufs[i] = 'a';
998 else
999 bufs[i] = ' ';
1000 }
1001
1002 bufs[MAX_LEAVES] = '\0';
1003 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1004#endif
1005}
1006
1007static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001008shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1009{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001010 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001011 struct shm_surface_leaf *leaf;
1012 int i;
1013 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001014
1015 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001016
Pekka Paalanenaef02542013-04-25 13:57:47 +03001017 for (i = 0; i < MAX_LEAVES; i++) {
1018 leaf = &surface->leaf[i];
1019 if (leaf->data && leaf->data->buffer == buffer) {
1020 leaf->busy = 0;
1021 break;
1022 }
1023 }
1024 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001025
Pekka Paalanenaef02542013-04-25 13:57:47 +03001026 /* Leave one free leaf with storage, release others */
1027 free_found = 0;
1028 for (i = 0; i < MAX_LEAVES; i++) {
1029 leaf = &surface->leaf[i];
1030
1031 if (!leaf->cairo_surface || leaf->busy)
1032 continue;
1033
1034 if (!free_found)
1035 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001036 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001037 shm_surface_leaf_release(leaf);
1038 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001039
Pekka Paalanen97777442013-05-22 10:20:05 +03001040 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001041}
1042
1043static const struct wl_buffer_listener shm_surface_buffer_listener = {
1044 shm_surface_buffer_release
1045};
1046
Pekka Paalanen99436862012-11-19 17:15:59 +02001047static cairo_surface_t *
1048shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001049 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001050 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001051{
Pekka Paalanenec076692012-11-30 13:37:27 +02001052 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001053 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001054 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001055 struct shm_surface_leaf *leaf = NULL;
1056 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001057
1058 surface->dx = dx;
1059 surface->dy = dy;
1060
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001061 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001062 for (i = 0; i < MAX_LEAVES; i++) {
1063 if (surface->leaf[i].busy)
1064 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001065
Pekka Paalanenaef02542013-04-25 13:57:47 +03001066 if (!leaf || surface->leaf[i].cairo_surface)
1067 leaf = &surface->leaf[i];
1068 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001069 DBG_OBJ(surface->surface, "pick leaf %d\n",
1070 (int)(leaf - &surface->leaf[0]));
1071
Pekka Paalanenaef02542013-04-25 13:57:47 +03001072 if (!leaf) {
1073 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001075 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001076 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001077 }
1078
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 if (!resize_hint && leaf->resize_pool) {
1080 cairo_surface_destroy(leaf->cairo_surface);
1081 leaf->cairo_surface = NULL;
1082 shm_pool_destroy(leaf->resize_pool);
1083 leaf->resize_pool = NULL;
1084 }
1085
Alexander Larsson1818e312013-05-22 14:41:31 +02001086 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1087
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 if (leaf->cairo_surface &&
1089 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1090 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001091 goto out;
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 if (leaf->cairo_surface)
1094 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001095
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001096#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001097 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001098 /* Create a big pool to allocate from, while continuously
1099 * resizing. Mmapping a new pool in the server
1100 * is relatively expensive, so reusing a pool performs
1101 * better, but may temporarily reserve unneeded memory.
1102 */
1103 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 leaf->resize_pool = shm_pool_create(surface->display,
1105 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001106 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001107#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001108
Alexander Larsson1818e312013-05-22 14:41:31 +02001109 rect.width = width;
1110 rect.height = height;
1111
Pekka Paalanena4eda732012-11-19 17:16:02 +02001112 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 display_create_shm_surface(surface->display, &rect,
1114 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115 leaf->resize_pool,
1116 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001117 if (!leaf->cairo_surface)
1118 return NULL;
1119
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001121 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001122
1123out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001124 surface->current = leaf;
1125
1126 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001127}
1128
1129static void
1130shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001131 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001132 struct rectangle *server_allocation)
1133{
1134 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001135 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001136
1137 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001138 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001139 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001140 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001141
Alexander Larsson1818e312013-05-22 14:41:31 +02001142 buffer_to_surface_size (buffer_transform, buffer_scale,
1143 &server_allocation->width,
1144 &server_allocation->height);
1145
Pekka Paalanena4eda732012-11-19 17:16:02 +02001146 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001147 surface->dx, surface->dy);
1148 wl_surface_damage(surface->surface, 0, 0,
1149 server_allocation->width, server_allocation->height);
1150 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001151
Pekka Paalanen71233882013-04-25 13:57:53 +03001152 DBG_OBJ(surface->surface, "leaf %d busy\n",
1153 (int)(leaf - &surface->leaf[0]));
1154
Pekka Paalanena4eda732012-11-19 17:16:02 +02001155 leaf->busy = 1;
1156 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001157}
1158
1159static int
1160shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1161{
1162 return -1;
1163}
1164
1165static void
1166shm_surface_release(struct toysurface *base)
1167{
1168}
1169
1170static void
1171shm_surface_destroy(struct toysurface *base)
1172{
1173 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001174 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001175
Pekka Paalanenaef02542013-04-25 13:57:47 +03001176 for (i = 0; i < MAX_LEAVES; i++)
1177 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001178
1179 free(surface);
1180}
1181
1182static struct toysurface *
1183shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1184 uint32_t flags, struct rectangle *rectangle)
1185{
1186 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001187 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001188
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001189 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001190 surface->base.prepare = shm_surface_prepare;
1191 surface->base.swap = shm_surface_swap;
1192 surface->base.acquire = shm_surface_acquire;
1193 surface->base.release = shm_surface_release;
1194 surface->base.destroy = shm_surface_destroy;
1195
1196 surface->display = display;
1197 surface->surface = wl_surface;
1198 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001199
1200 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001201}
1202
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001203/*
1204 * The following correspondences between file names and cursors was copied
1205 * from: https://bugs.kde.org/attachment.cgi?id=67313
1206 */
1207
1208static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001209 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001210 "sw-resize",
1211 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001212};
1213
1214static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001215 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001216 "se-resize",
1217 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001218};
1219
1220static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "s-resize",
1223 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001228 "closedhand",
1229 "208530c400c041818281048008011002"
1230};
1231
1232static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001234 "default",
1235 "top_left_arrow",
1236 "left-arrow"
1237};
1238
1239static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001240 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001241 "w-resize",
1242 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001243};
1244
1245static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001246 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001247 "e-resize",
1248 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001249};
1250
1251static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001252 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001253 "nw-resize",
1254 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001255};
1256
1257static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001258 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001259 "ne-resize",
1260 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001261};
1262
1263static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001264 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001265 "n-resize",
1266 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001267};
1268
1269static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001270 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001271 "ibeam",
1272 "text"
1273};
1274
1275static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001276 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001277 "pointer",
1278 "pointing_hand",
1279 "e29285e634086352946a0e7090d73106"
1280};
1281
1282static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001283 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001284 "wait",
1285 "0426c94ea35c87780ff01dc239897213"
1286};
1287
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001288static const char *move_draggings[] = {
1289 "dnd-move"
1290};
1291
1292static const char *copy_draggings[] = {
1293 "dnd-copy"
1294};
1295
1296static const char *forbidden_draggings[] = {
1297 "dnd-none",
1298 "dnd-no-drop"
1299};
1300
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001301struct cursor_alternatives {
1302 const char **names;
1303 size_t count;
1304};
1305
1306static const struct cursor_alternatives cursors[] = {
1307 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1308 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1309 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1310 {grabbings, ARRAY_LENGTH(grabbings)},
1311 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1312 {left_sides, ARRAY_LENGTH(left_sides)},
1313 {right_sides, ARRAY_LENGTH(right_sides)},
1314 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1315 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1316 {top_sides, ARRAY_LENGTH(top_sides)},
1317 {xterms, ARRAY_LENGTH(xterms)},
1318 {hand1s, ARRAY_LENGTH(hand1s)},
1319 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001320 {move_draggings, ARRAY_LENGTH(move_draggings)},
1321 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1322 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001323};
1324
1325static void
1326create_cursors(struct display *display)
1327{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001328 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001329 struct weston_config *config;
1330 struct weston_config_section *s;
emersionac71ee52018-11-18 21:42:10 +01001331 int size = DEFAULT_XCURSOR_SIZE;
1332 char *theme = NULL, *size_str;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001333 unsigned int i, j;
1334 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001335
emersion807cd2e2018-10-10 09:46:12 +02001336 theme = getenv("XCURSOR_THEME");
emersionac71ee52018-11-18 21:42:10 +01001337
1338 size_str = getenv("XCURSOR_SIZE");
1339 if (size_str) {
1340 safe_strtoint(size_str, &size);
1341 if (size <= 0)
1342 size = DEFAULT_XCURSOR_SIZE;
1343 }
emersion807cd2e2018-10-10 09:46:12 +02001344
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001345 config_file = weston_config_get_name_from_env();
1346 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001347 s = weston_config_get_section(config, "shell", NULL, NULL);
emersion807cd2e2018-10-10 09:46:12 +02001348 weston_config_section_get_string(s, "cursor-theme", &theme, theme);
1349 weston_config_section_get_int(s, "cursor-size", &size, size);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001350 weston_config_destroy(config);
1351
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001352 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001353 if (!display->cursor_theme) {
1354 fprintf(stderr, "could not load theme '%s'\n", theme);
1355 return;
1356 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001357 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001358 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001359 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001360
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001361 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001362 cursor = NULL;
1363 for (j = 0; !cursor && j < cursors[i].count; ++j)
1364 cursor = wl_cursor_theme_get_cursor(
1365 display->cursor_theme, cursors[i].names[j]);
1366
1367 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001368 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001369 cursors[i].names[0]);
1370
1371 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001372 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001373}
1374
1375static void
1376destroy_cursors(struct display *display)
1377{
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03001378 if (display->cursor_theme)
1379 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001380 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001381}
1382
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001383struct wl_cursor_image *
1384display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001385{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001386 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001387
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001388 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001389}
1390
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001391static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001392surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001393{
Harish Krupo7bcbab12018-12-11 13:45:43 +05301394 struct widget *widget = surface->widget;
Pekka Paalanen89dee002013-02-13 16:17:20 +02001395 if (!surface->cairo_surface)
1396 return;
1397
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001398 if (surface->opaque_region) {
1399 wl_surface_set_opaque_region(surface->surface,
1400 surface->opaque_region);
1401 wl_region_destroy(surface->opaque_region);
1402 surface->opaque_region = NULL;
1403 }
1404
1405 if (surface->input_region) {
1406 wl_surface_set_input_region(surface->surface,
1407 surface->input_region);
1408 wl_region_destroy(surface->input_region);
1409 surface->input_region = NULL;
1410 }
1411
Harish Krupo7bcbab12018-12-11 13:45:43 +05301412 if (surface->viewport) {
1413 wp_viewport_set_destination(surface->viewport,
1414 widget->viewport_dest_width,
1415 widget->viewport_dest_height);
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
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001482 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001483}
1484
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001485int
1486window_get_buffer_transform(struct window *window)
1487{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001488 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001489}
1490
1491void
1492window_set_buffer_transform(struct window *window,
1493 enum wl_output_transform transform)
1494{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001495 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001496 wl_surface_set_buffer_transform(window->main_surface->surface,
1497 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001498}
1499
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001500void
1501window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001502 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001503{
1504 window->main_surface->buffer_scale = scale;
1505 wl_surface_set_buffer_scale(window->main_surface->surface,
1506 scale);
1507}
1508
1509uint32_t
1510window_get_buffer_scale(struct window *window)
1511{
1512 return window->main_surface->buffer_scale;
1513}
1514
Alexander Larssond68f5232013-05-22 14:41:33 +02001515uint32_t
1516window_get_output_scale(struct window *window)
1517{
1518 struct window_output *window_output;
1519 struct window_output *window_output_tmp;
1520 int scale = 1;
1521
1522 wl_list_for_each_safe(window_output, window_output_tmp,
1523 &window->window_output_list, link) {
1524 if (window_output->output->scale > scale)
1525 scale = window_output->output->scale;
1526 }
1527
1528 return scale;
1529}
1530
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001531static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001532
Pekka Paalanen4e373742013-02-13 16:17:13 +02001533static void
1534surface_destroy(struct surface *surface)
1535{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001536 if (surface->frame_cb)
1537 wl_callback_destroy(surface->frame_cb);
1538
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001539 if (surface->input_region)
1540 wl_region_destroy(surface->input_region);
1541
1542 if (surface->opaque_region)
1543 wl_region_destroy(surface->opaque_region);
1544
Pekka Paalanen35e82632013-04-25 13:57:48 +03001545 if (surface->subsurface)
1546 wl_subsurface_destroy(surface->subsurface);
1547
Pekka Paalanen4e373742013-02-13 16:17:13 +02001548 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001549
1550 if (surface->toysurface)
1551 surface->toysurface->destroy(surface->toysurface);
1552
Pekka Paalanen35e82632013-04-25 13:57:48 +03001553 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001554 free(surface);
1555}
1556
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001557void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001558window_destroy(struct window *window)
1559{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001560 struct display *display = window->display;
1561 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001562 struct window_output *window_output;
1563 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001564
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001565 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001566
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001567 wl_list_for_each(input, &display->input_list, link) {
Jeffy Chen179458a2019-09-02 10:15:26 +08001568 if (input->touch_focus == window) {
1569 struct touch_point *tp, *tmp;
1570
1571 wl_list_for_each_safe(tp, tmp,
1572 &input->touch_point_list,
1573 link) {
1574 wl_list_remove(&tp->link);
1575 free(tp);
1576 }
1577
Rusty Lynch1084da52013-08-15 09:10:08 -07001578 input->touch_focus = NULL;
Jeffy Chen179458a2019-09-02 10:15:26 +08001579 }
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001580 if (input->pointer_focus == window)
1581 input->pointer_focus = NULL;
1582 if (input->keyboard_focus == window)
1583 input->keyboard_focus = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08001584 if (input->locked_window == window)
1585 input->locked_window = NULL;
1586 if (input->confined_window == window)
1587 input->confined_window = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001588 if (input->focus_widget &&
1589 input->focus_widget->window == window)
1590 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001591 }
1592
Rob Bradford7507b572012-05-15 17:55:34 +01001593 wl_list_for_each_safe(window_output, window_output_tmp,
1594 &window->window_output_list, link) {
1595 free (window_output);
1596 }
1597
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001598 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001599 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001600
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001601 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01001602 xdg_toplevel_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001603 if (window->xdg_popup)
ant8mef99fac22018-11-28 22:46:37 +01001604 xdg_popup_destroy(window->xdg_popup);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001605 if (window->xdg_surface)
ant8mef99fac22018-11-28 22:46:37 +01001606 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001607
1608 surface_destroy(window->main_surface);
1609
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001610 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001611
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001612 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001613 free(window);
1614}
1615
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001616static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001617widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001618{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619 struct widget *child, *target;
Harish Krupo7bcbab12018-12-11 13:45:43 +05301620 int alloc_x, alloc_y, width, height;
1621 double scale;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001623 wl_list_for_each(child, &widget->child_list, link) {
1624 target = widget_find_widget(child, x, y);
1625 if (target)
1626 return target;
1627 }
1628
Harish Krupo7bcbab12018-12-11 13:45:43 +05301629 alloc_x = widget->allocation.x;
1630 alloc_y = widget->allocation.y;
1631 width = widget->allocation.width;
1632 height = widget->allocation.height;
1633
1634 if (widget->viewport_dest_width != -1 &&
1635 widget->viewport_dest_height != -1) {
1636 scale = widget->viewport_dest_width / (double) width;
1637 alloc_x = alloc_x * scale;
1638 width = widget->viewport_dest_width;
1639
1640 scale = widget->viewport_dest_height / (double) height;
1641 alloc_y = alloc_y * scale;
1642 height = widget->viewport_dest_height;
1643 }
1644
1645 if (alloc_x <= x && x < alloc_x + width &&
1646 alloc_y <= y && y < alloc_y + height) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001647 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001648 }
1649
1650 return NULL;
1651}
1652
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001653static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001654window_find_widget(struct window *window, int32_t x, int32_t y)
1655{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001656 struct surface *surface;
1657 struct widget *widget;
1658
1659 wl_list_for_each(surface, &window->subsurface_list, link) {
1660 widget = widget_find_widget(surface->widget, x, y);
1661 if (widget)
1662 return widget;
1663 }
1664
1665 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001666}
1667
1668static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001669widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001670{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001671 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001672
Peter Huttererf3d62272013-08-08 11:57:05 +10001673 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001674 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001675 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001677 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001678 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001679 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001680 widget->tooltip = NULL;
1681 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001682 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001683 widget->use_cairo = 1;
Harish Krupo7bcbab12018-12-11 13:45:43 +05301684 widget->viewport_dest_width = -1;
1685 widget->viewport_dest_height = -1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001686
1687 return widget;
1688}
1689
1690struct widget *
1691window_add_widget(struct window *window, void *data)
1692{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001693 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001694
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001695 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001696 wl_list_init(&widget->link);
1697 window->main_surface->widget = widget;
1698
1699 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001700}
1701
1702struct widget *
1703widget_add_widget(struct widget *parent, void *data)
1704{
1705 struct widget *widget;
1706
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001707 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001708 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001709
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001710 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001711}
1712
1713void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001714widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001715{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001716 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001717 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001718 struct input *input;
1719
Pekka Paalanen35e82632013-04-25 13:57:48 +03001720 /* Destroy the sub-surface along with the root widget */
1721 if (surface->widget == widget && surface->subsurface)
1722 surface_destroy(widget->surface);
1723
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001724 if (widget->tooltip)
1725 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001726
Pekka Paalanene156fb62012-01-19 13:51:38 +02001727 wl_list_for_each(input, &display->input_list, link) {
1728 if (input->focus_widget == widget)
1729 input->focus_widget = NULL;
1730 }
1731
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001732 wl_list_remove(&widget->link);
1733 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001734}
1735
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001736void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001737widget_set_default_cursor(struct widget *widget, int cursor)
1738{
1739 widget->default_cursor = cursor;
1740}
1741
1742void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001743widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001744{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001745 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001746}
1747
1748void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001749widget_set_size(struct widget *widget, int32_t width, int32_t height)
1750{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001751 widget->allocation.width = width;
1752 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001753}
1754
1755void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001756widget_set_allocation(struct widget *widget,
1757 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001758{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001759 widget->allocation.x = x;
1760 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001761 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001762}
1763
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001764void
1765widget_set_transparent(struct widget *widget, int transparent)
1766{
1767 widget->opaque = !transparent;
1768}
1769
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001770void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001771widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001772{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001773 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001774}
1775
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001776static cairo_surface_t *
1777widget_get_cairo_surface(struct widget *widget)
1778{
1779 struct surface *surface = widget->surface;
1780 struct window *window = widget->window;
1781
Neil Roberts97b747c2013-12-19 16:17:12 +00001782 assert(widget->use_cairo);
1783
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001784 if (!surface->cairo_surface) {
1785 if (surface == window->main_surface)
1786 window_create_main_surface(window);
1787 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001788 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001789 }
1790
1791 return surface->cairo_surface;
1792}
1793
Alexander Larsson15901f02013-05-22 14:41:25 +02001794static void
1795widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1796{
1797 struct surface *surface = widget->surface;
1798 double angle;
1799 cairo_matrix_t m;
1800 enum wl_output_transform transform;
1801 int surface_width, surface_height;
1802 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001803 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001804
1805 surface_width = surface->allocation.width;
1806 surface_height = surface->allocation.height;
1807
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001808 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001809 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001810
Alexander Larsson15901f02013-05-22 14:41:25 +02001811 switch (transform) {
1812 case WL_OUTPUT_TRANSFORM_FLIPPED:
1813 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1814 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1815 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1816 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1817 break;
1818 default:
1819 cairo_matrix_init_identity(&m);
1820 break;
1821 }
1822
1823 switch (transform) {
1824 case WL_OUTPUT_TRANSFORM_NORMAL:
1825 default:
1826 angle = 0;
1827 translate_x = 0;
1828 translate_y = 0;
1829 break;
1830 case WL_OUTPUT_TRANSFORM_FLIPPED:
1831 angle = 0;
1832 translate_x = surface_width;
1833 translate_y = 0;
1834 break;
1835 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001836 angle = M_PI + M_PI_2;
1837 translate_x = 0;
1838 translate_y = surface_width;
Alexander Larsson15901f02013-05-22 14:41:25 +02001839 break;
1840 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001841 angle = M_PI + M_PI_2;
1842 translate_x = 0;
1843 translate_y = 0;
Alexander Larsson15901f02013-05-22 14:41:25 +02001844 break;
1845 case WL_OUTPUT_TRANSFORM_180:
1846 angle = M_PI;
1847 translate_x = surface_width;
1848 translate_y = surface_height;
1849 break;
1850 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1851 angle = M_PI;
1852 translate_x = 0;
1853 translate_y = surface_height;
1854 break;
1855 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001856 angle = M_PI_2;
1857 translate_x = surface_height;
1858 translate_y = 0;
Alexander Larsson15901f02013-05-22 14:41:25 +02001859 break;
1860 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001861 angle = M_PI_2;
1862 translate_x = surface_height;
1863 translate_y = surface_width;
Alexander Larsson15901f02013-05-22 14:41:25 +02001864 break;
1865 }
1866
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001867 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001868 cairo_translate(cr, translate_x, translate_y);
1869 cairo_rotate(cr, angle);
1870 cairo_transform(cr, &m);
1871}
1872
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001873cairo_t *
1874widget_cairo_create(struct widget *widget)
1875{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001876 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001877 cairo_surface_t *cairo_surface;
1878 cairo_t *cr;
1879
1880 cairo_surface = widget_get_cairo_surface(widget);
1881 cr = cairo_create(cairo_surface);
1882
Alexander Larsson15901f02013-05-22 14:41:25 +02001883 widget_cairo_update_transform(widget, cr);
1884
Pekka Paalanen35e82632013-04-25 13:57:48 +03001885 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1886
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001887 return cr;
1888}
1889
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001890struct wl_surface *
1891widget_get_wl_surface(struct widget *widget)
1892{
1893 return widget->surface->surface;
1894}
1895
Neil Roberts5e10a042013-09-09 00:40:17 +01001896struct wl_subsurface *
1897widget_get_wl_subsurface(struct widget *widget)
1898{
1899 return widget->surface->subsurface;
1900}
1901
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001902uint32_t
1903widget_get_last_time(struct widget *widget)
1904{
1905 return widget->surface->last_time;
1906}
1907
1908void
1909widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1910{
1911 struct wl_compositor *comp = widget->window->display->compositor;
1912 struct surface *surface = widget->surface;
1913
1914 if (!surface->input_region)
1915 surface->input_region = wl_compositor_create_region(comp);
1916
1917 if (rect) {
1918 wl_region_add(surface->input_region,
1919 rect->x, rect->y, rect->width, rect->height);
1920 }
1921}
1922
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001923void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001924widget_set_resize_handler(struct widget *widget,
1925 widget_resize_handler_t handler)
1926{
1927 widget->resize_handler = handler;
1928}
1929
1930void
1931widget_set_redraw_handler(struct widget *widget,
1932 widget_redraw_handler_t handler)
1933{
1934 widget->redraw_handler = handler;
1935}
1936
1937void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001938widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001939{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001940 widget->enter_handler = handler;
1941}
1942
1943void
1944widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1945{
1946 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001947}
1948
1949void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001950widget_set_motion_handler(struct widget *widget,
1951 widget_motion_handler_t handler)
1952{
1953 widget->motion_handler = handler;
1954}
1955
1956void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001957widget_set_button_handler(struct widget *widget,
1958 widget_button_handler_t handler)
1959{
1960 widget->button_handler = handler;
1961}
1962
1963void
Rusty Lynch041815a2013-08-08 21:20:38 -07001964widget_set_touch_up_handler(struct widget *widget,
1965 widget_touch_up_handler_t handler)
1966{
1967 widget->touch_up_handler = handler;
1968}
1969
1970void
1971widget_set_touch_down_handler(struct widget *widget,
1972 widget_touch_down_handler_t handler)
1973{
1974 widget->touch_down_handler = handler;
1975}
1976
1977void
1978widget_set_touch_motion_handler(struct widget *widget,
1979 widget_touch_motion_handler_t handler)
1980{
1981 widget->touch_motion_handler = handler;
1982}
1983
1984void
1985widget_set_touch_frame_handler(struct widget *widget,
1986 widget_touch_frame_handler_t handler)
1987{
1988 widget->touch_frame_handler = handler;
1989}
1990
1991void
1992widget_set_touch_cancel_handler(struct widget *widget,
1993 widget_touch_cancel_handler_t handler)
1994{
1995 widget->touch_cancel_handler = handler;
1996}
1997
1998void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001999widget_set_axis_handler(struct widget *widget,
2000 widget_axis_handler_t handler)
2001{
2002 widget->axis_handler = handler;
2003}
2004
Peter Hutterer87743e92016-01-18 16:38:22 +10002005void
2006widget_set_pointer_frame_handler(struct widget *widget,
2007 widget_pointer_frame_handler_t handler)
2008{
2009 widget->pointer_frame_handler = handler;
2010}
2011
2012void
2013widget_set_axis_handlers(struct widget *widget,
2014 widget_axis_handler_t axis_handler,
2015 widget_axis_source_handler_t axis_source_handler,
2016 widget_axis_stop_handler_t axis_stop_handler,
2017 widget_axis_discrete_handler_t axis_discrete_handler)
2018{
2019 widget->axis_handler = axis_handler;
2020 widget->axis_source_handler = axis_source_handler;
2021 widget->axis_stop_handler = axis_stop_handler;
2022 widget->axis_discrete_handler = axis_discrete_handler;
2023}
2024
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002025static void
2026window_schedule_redraw_task(struct window *window);
2027
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002028void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002029widget_schedule_redraw(struct widget *widget)
2030{
Pekka Paalanen71233882013-04-25 13:57:53 +03002031 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002032 widget->surface->redraw_needed = 1;
2033 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002034}
2035
Neil Roberts97b747c2013-12-19 16:17:12 +00002036void
2037widget_set_use_cairo(struct widget *widget,
2038 int use_cairo)
2039{
2040 widget->use_cairo = use_cairo;
2041}
2042
Harish Krupo7bcbab12018-12-11 13:45:43 +05302043int
2044widget_set_viewport_destination(struct widget *widget, int width, int height)
2045{
2046 struct window *window = widget->window;
2047 struct display *display = window->display;
2048 struct surface *surface = widget->surface;
2049 if (!display->viewporter)
2050 return -1;
2051
2052 if (width == -1 && height == -1) {
2053 if (surface->viewport) {
2054 wp_viewport_destroy(surface->viewport);
2055 surface->viewport = NULL;
2056 }
2057
2058 widget->viewport_dest_width = -1;
2059 widget->viewport_dest_height = -1;
2060 return 0;
2061 }
2062
2063 if (!surface->viewport) {
2064 surface->viewport = wp_viewporter_get_viewport(display->viewporter,
2065 surface->surface);
2066 if (!surface->viewport)
2067 return -1;
2068 }
2069
2070 widget->viewport_dest_width = width;
2071 widget->viewport_dest_height = height;
2072
2073 return 0;
2074}
2075
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002076cairo_surface_t *
2077window_get_surface(struct window *window)
2078{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002079 cairo_surface_t *cairo_surface;
2080
2081 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2082
2083 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002084}
2085
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002086struct wl_surface *
2087window_get_wl_surface(struct window *window)
2088{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002089 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002090}
2091
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002092static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002093tooltip_redraw_handler(struct widget *widget, void *data)
2094{
2095 cairo_t *cr;
2096 const int32_t r = 3;
2097 struct tooltip *tooltip = data;
2098 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002099
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002100 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002101 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002102 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2103 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2104 cairo_paint(cr);
2105
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002106 width = widget->allocation.width;
2107 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002108 rounded_rect(cr, 0, 0, width, height, r);
2109
2110 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2111 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2112 cairo_fill(cr);
2113
-c505af82019-06-26 21:00:43 +00002114 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.85);
2115 cairo_move_to(cr, 10, 17);
2116 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002117 cairo_show_text(cr, tooltip->entry);
2118 cairo_destroy(cr);
2119}
2120
2121static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002122get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002123{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002124 cairo_t *cr;
2125 cairo_text_extents_t extents;
2126
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002127 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002128 * created yet, and parent does not have a valid surface
2129 * outside repaint, either.
2130 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002131 cr = cairo_create(display->dummy_surface);
-c505af82019-06-26 21:00:43 +00002132 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002133 cairo_text_extents(cr, tooltip->entry, &extents);
2134 cairo_destroy(cr);
2135
2136 return extents;
2137}
2138
2139static int
2140window_create_tooltip(struct tooltip *tooltip)
2141{
2142 struct widget *parent = tooltip->parent;
2143 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002144 const int offset_y = 27;
2145 const int margin = 3;
2146 cairo_text_extents_t extents;
2147
2148 if (tooltip->widget)
2149 return 0;
2150
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002151 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002152
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002153 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002154 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002155 widget_set_allocation(tooltip->widget,
2156 tooltip->x, tooltip->y + offset_y,
2157 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002158
2159 return 0;
2160}
2161
2162void
2163widget_destroy_tooltip(struct widget *parent)
2164{
2165 struct tooltip *tooltip = parent->tooltip;
2166
2167 parent->tooltip_count = 0;
2168 if (!tooltip)
2169 return;
2170
2171 if (tooltip->widget) {
2172 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002173 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002174 }
2175
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002176 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002177 free(tooltip->entry);
2178 free(tooltip);
2179 parent->tooltip = NULL;
2180}
2181
2182static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002183tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002184{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002185 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002186
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002187 window_create_tooltip(tooltip);
2188}
2189
2190#define TOOLTIP_TIMEOUT 500
2191static int
2192tooltip_timer_reset(struct tooltip *tooltip)
2193{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002194 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002195
2196 return 0;
2197}
2198
2199int
2200widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2201{
2202 struct tooltip *tooltip = parent->tooltip;
2203
2204 parent->tooltip_count++;
2205 if (tooltip) {
2206 tooltip->x = x;
2207 tooltip->y = y;
2208 tooltip_timer_reset(tooltip);
2209 return 0;
2210 }
2211
2212 /* the handler might be triggered too fast via input device motion, so
2213 * we need this check here to make sure tooltip is fully initialized */
2214 if (parent->tooltip_count > 1)
2215 return 0;
2216
2217 tooltip = malloc(sizeof *tooltip);
2218 if (!tooltip)
2219 return -1;
2220
2221 parent->tooltip = tooltip;
2222 tooltip->parent = parent;
2223 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002224 tooltip->x = x;
2225 tooltip->y = y;
2226 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002227 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2228 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002229 tooltip_timer_reset(tooltip);
2230
2231 return 0;
2232}
2233
2234static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002235frame_resize_handler(struct widget *widget,
2236 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002237{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002238 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002239 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002240 struct rectangle interior;
2241 struct rectangle input;
2242 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002243
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002244 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002245 interior.x = 0;
2246 interior.y = 0;
2247 interior.width = width;
2248 interior.height = height;
2249 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002250 frame_resize(frame->frame, width, height);
2251 frame_interior(frame->frame, &interior.x, &interior.y,
2252 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002253 }
2254
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002255 widget_set_allocation(child, interior.x, interior.y,
2256 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002257
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002258 if (child->resize_handler) {
2259 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002260 child->user_data);
2261
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002262 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002263 width = child->allocation.width;
2264 height = child->allocation.height;
2265 } else {
2266 frame_resize_inside(frame->frame,
2267 child->allocation.width,
2268 child->allocation.height);
2269 width = frame_width(frame->frame);
2270 height = frame_height(frame->frame);
2271 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002272 }
2273
Scott Moreauf7e498c2012-05-14 11:39:29 -06002274 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002275
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002276 widget->surface->input_region =
2277 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002278 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002279 frame_input_rect(frame->frame, &input.x, &input.y,
2280 &input.width, &input.height);
2281 wl_region_add(widget->surface->input_region,
2282 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002283 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002284 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002285 }
2286
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002287 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002288
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002289 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002290 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002291 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2292 &opaque.width, &opaque.height);
2293
2294 wl_region_add(widget->surface->opaque_region,
2295 opaque.x, opaque.y,
2296 opaque.width, opaque.height);
2297 } else {
2298 wl_region_add(widget->surface->opaque_region,
2299 0, 0, width, height);
2300 }
Martin Minarik1998b152012-05-10 02:04:35 +02002301 }
2302
Martin Minarik1998b152012-05-10 02:04:35 +02002303
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002304 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002305}
2306
2307static void
2308frame_redraw_handler(struct widget *widget, void *data)
2309{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002310 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002311 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002312 struct window *window = widget->window;
2313
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002314 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002315 return;
2316
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002317 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002318
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002319 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002320
2321 cairo_destroy(cr);
2322}
2323
2324static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002325frame_get_pointer_image_for_location(struct window_frame *frame,
2326 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002327{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002328 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002329
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002330 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002331 return CURSOR_LEFT_PTR;
2332
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002333 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002334 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002335 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002336 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002337 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002338 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002339 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002340 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002341 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002342 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002343 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002344 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002345 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002346 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002347 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002348 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002349 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002350 case THEME_LOCATION_EXTERIOR:
2351 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002352 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002353 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002354 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002355}
2356
Pekka Paalanen26237862014-09-10 15:10:30 +03002357static void
2358frame_menu_func(void *data, struct input *input, int index)
2359{
2360 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002361
2362 switch (index) {
2363 case 0: /* close */
2364 window_close(window);
2365 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002366 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002367 /* we don't have a way to get out of fullscreen for now */
2368 if (window->fullscreen_handler)
2369 window->fullscreen_handler(window, window->user_data);
2370 break;
2371 }
2372}
2373
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002374void
2375window_show_frame_menu(struct window *window,
2376 struct input *input, uint32_t time)
2377{
2378 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002379 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002380
Pekka Paalanen26237862014-09-10 15:10:30 +03002381 static const char *entries[] = {
2382 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002383 "Fullscreen"
2384 };
2385
2386 if (window->fullscreen_handler)
2387 count = ARRAY_LENGTH(entries);
2388 else
2389 count = ARRAY_LENGTH(entries) - 1;
2390
2391 input_get_position(input, &x, &y);
2392 window_show_menu(window->display, input, time, window,
2393 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002394}
2395
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002396static int
2397frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002398 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002399{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400 struct window_frame *frame = data;
2401 enum theme_location location;
2402
2403 location = frame_pointer_enter(frame->frame, input, x, y);
2404 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2405 widget_schedule_redraw(frame->widget);
2406
2407 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002408}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002409
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002410static int
2411frame_motion_handler(struct widget *widget,
2412 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002413 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002414{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002415 struct window_frame *frame = data;
2416 enum theme_location location;
2417
2418 location = frame_pointer_motion(frame->frame, input, x, y);
2419 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2420 widget_schedule_redraw(frame->widget);
2421
2422 return frame_get_pointer_image_for_location(data, location);
2423}
2424
2425static void
2426frame_leave_handler(struct widget *widget,
2427 struct input *input, void *data)
2428{
2429 struct window_frame *frame = data;
2430
2431 frame_pointer_leave(frame->frame, input);
2432 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2433 widget_schedule_redraw(frame->widget);
2434}
2435
2436static void
2437frame_handle_status(struct window_frame *frame, struct input *input,
2438 uint32_t time, enum theme_location location)
2439{
2440 struct window *window = frame->widget->window;
2441 uint32_t status;
2442
2443 status = frame_status(frame->frame);
2444 if (status & FRAME_STATUS_REPAINT)
2445 widget_schedule_redraw(frame->widget);
2446
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002447 if (status & FRAME_STATUS_MINIMIZE) {
2448 window_set_minimized(window);
2449 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2450 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002451
2452 if (status & FRAME_STATUS_MENU) {
2453 window_show_frame_menu(window, input, time);
2454 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2455 }
2456
2457 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002458 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002459 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2460 }
2461
2462 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002463 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002464 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002465 }
2466
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002467 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002468 input_ungrab(input);
ant8mef99fac22018-11-28 22:46:37 +01002469 xdg_toplevel_move(window->xdg_toplevel,
2470 input_get_seat(input),
2471 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002472
2473 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2474 }
2475
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002476 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002477 input_ungrab(input);
2478
ant8mef99fac22018-11-28 22:46:37 +01002479 xdg_toplevel_resize(window->xdg_toplevel,
2480 input_get_seat(input),
2481 window->display->serial,
2482 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002483
2484 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2485 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002486}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002487
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002488#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002489static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002490frame_button_handler(struct widget *widget,
2491 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002492 uint32_t button, enum wl_pointer_button_state state,
2493 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002494
2495{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002496 struct window_frame *frame = data;
2497 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002498
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002499 frame->double_click = 0;
2500 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2501 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2502 frame->double_click = 1;
2503 frame->did_double = 1;
2504 } else
2505 frame->did_double = 0;
2506
2507 frame->last_time = time;
2508 } else if (frame->did_double == 1) {
2509 frame->double_click = 1;
2510 frame->did_double = 0;
2511 }
2512
2513 if (frame->double_click)
2514 location = frame_double_click(frame->frame, input,
2515 button, state);
2516 else
2517 location = frame_pointer_button(frame->frame, input,
2518 button, state);
2519
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002520 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002521}
2522
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002523static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002524frame_touch_down_handler(struct widget *widget, struct input *input,
2525 uint32_t serial, uint32_t time, int32_t id,
2526 float x, float y, void *data)
2527{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002528 struct window_frame *frame = data;
2529
Xiong Zhang382de462014-06-12 11:06:26 +08002530 frame->double_click = 0;
2531 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2532 frame->last_id == id) {
2533 frame->double_click = 1;
2534 frame->did_double = 1;
2535 frame->double_id = id;
2536 } else
2537 frame->did_double = 0;
2538
2539 frame->last_time = time;
2540 frame->last_id = id;
2541
2542 if (frame->double_click)
2543 frame_double_touch_down(frame->frame, input, id, x, y);
2544 else
2545 frame_touch_down(frame->frame, input, id, x, y);
2546
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002547 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2548}
2549
2550static void
2551frame_touch_up_handler(struct widget *widget,
2552 struct input *input, uint32_t serial, uint32_t time,
2553 int32_t id, void *data)
2554{
2555 struct window_frame *frame = data;
2556
Xiong Zhang382de462014-06-12 11:06:26 +08002557 if (frame->double_id == id && frame->did_double) {
2558 frame->did_double = 0;
2559 frame->double_id = 0;
2560 frame_double_touch_up(frame->frame, input, id);
2561 } else
2562 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002563 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002564}
2565
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002566struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002567window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002568{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002569 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002570 uint32_t buttons;
2571
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002572 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002573 buttons = FRAME_BUTTON_NONE;
2574 } else {
2575 buttons = FRAME_BUTTON_ALL;
2576 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002577
Peter Huttererf3d62272013-08-08 11:57:05 +10002578 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002579 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002580 buttons, window->title, NULL);
Simon Ser598d3a12020-02-10 15:49:20 +01002581 if (!frame->frame) {
2582 free(frame);
2583 return NULL;
2584 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002585
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002586 frame->widget = window_add_widget(window, frame);
2587 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002588
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002589 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2590 widget_set_resize_handler(frame->widget, frame_resize_handler);
2591 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002592 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002593 widget_set_motion_handler(frame->widget, frame_motion_handler);
2594 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002595 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002596 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002597
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002598 window->frame = frame;
2599
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002600 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002601}
2602
Kristian Høgsberga1627922012-06-20 17:30:03 -04002603void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002604window_frame_set_child_size(struct widget *widget, int child_width,
2605 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002606{
2607 struct display *display = widget->window->display;
2608 struct theme *t = display->theme;
2609 int decoration_width, decoration_height;
2610 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002611 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002612
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002613 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002614 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002615 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002616 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002617
2618 width = child_width + decoration_width;
2619 height = child_height + decoration_height;
2620 } else {
2621 width = child_width;
2622 height = child_height;
2623 }
2624
2625 window_schedule_resize(widget->window, width, height);
2626}
2627
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002628static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002629window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002630{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002631 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002632
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002633 /* frame->child must be destroyed by the application */
2634 widget_destroy(frame->widget);
2635 free(frame);
2636}
2637
2638static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002639input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002640 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002641{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002642 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002643 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002644
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002645 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002646 return;
2647
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002648 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002649 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002650 widget = old;
2651 if (input->grab)
2652 widget = input->grab;
2653 if (widget->leave_handler)
2654 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002655 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002656 }
2657
2658 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002659 widget = focus;
2660 if (input->grab)
2661 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002662 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002663 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002664 cursor = widget->enter_handler(focus, input, x, y,
2665 widget->user_data);
2666 else
2667 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002668
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002669 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002670 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002671}
2672
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002673void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002674touch_grab(struct input *input, int32_t touch_id)
2675{
2676 input->touch_grab = 1;
2677 input->touch_grab_id = touch_id;
2678}
2679
2680void
2681touch_ungrab(struct input *input)
2682{
2683 struct touch_point *tp, *tmp;
2684
2685 input->touch_grab = 0;
2686
2687 wl_list_for_each_safe(tp, tmp,
2688 &input->touch_point_list, link) {
2689 if (tp->id != input->touch_grab_id)
2690 continue;
2691 wl_list_remove(&tp->link);
2692 free(tp);
2693
2694 return;
2695 }
2696}
2697
2698void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002699input_grab(struct input *input, struct widget *widget, uint32_t button)
2700{
2701 input->grab = widget;
2702 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002703
2704 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002705}
2706
2707void
2708input_ungrab(struct input *input)
2709{
2710 struct widget *widget;
2711
2712 input->grab = NULL;
2713 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002714 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002715 input->sx, input->sy);
2716 input_set_focus_widget(input, widget, input->sx, input->sy);
2717 }
2718}
2719
2720static void
Derek Foreman118a4292015-04-22 17:23:35 -05002721cursor_delay_timer_reset(struct input *input, uint32_t duration)
2722{
Derek Foreman118a4292015-04-22 17:23:35 -05002723 if (!duration)
2724 input->cursor_timer_running = false;
2725 else
2726 input->cursor_timer_running = true;
2727
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002728 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002729}
2730
2731static void cancel_pointer_image_update(struct input *input)
2732{
2733 if (input->cursor_timer_running)
2734 cursor_delay_timer_reset(input, 0);
2735}
2736
2737static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002738input_remove_pointer_focus(struct input *input)
2739{
2740 struct window *window = input->pointer_focus;
2741
2742 if (!window)
2743 return;
2744
2745 input_set_focus_widget(input, NULL, 0, 0);
2746
2747 input->pointer_focus = NULL;
2748 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002749 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002750}
2751
2752static void
2753pointer_handle_enter(void *data, struct wl_pointer *pointer,
2754 uint32_t serial, struct wl_surface *surface,
2755 wl_fixed_t sx_w, wl_fixed_t sy_w)
2756{
2757 struct input *input = data;
2758 struct window *window;
2759 struct widget *widget;
2760 float sx = wl_fixed_to_double(sx_w);
2761 float sy = wl_fixed_to_double(sy_w);
2762
2763 if (!surface) {
2764 /* enter event for a window we've just destroyed */
2765 return;
2766 }
2767
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002768 window = wl_surface_get_user_data(surface);
2769 if (surface != window->main_surface->surface) {
2770 DBG("Ignoring input event from subsurface %p\n", surface);
2771 return;
2772 }
2773
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002774 input->display->serial = serial;
2775 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002776 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002777
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002778 input->sx = sx;
2779 input->sy = sy;
2780
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002781 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002782 input_set_focus_widget(input, widget, sx, sy);
2783}
2784
2785static void
2786pointer_handle_leave(void *data, struct wl_pointer *pointer,
2787 uint32_t serial, struct wl_surface *surface)
2788{
2789 struct input *input = data;
2790
2791 input->display->serial = serial;
2792 input_remove_pointer_focus(input);
2793}
2794
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002795static void
Daniel Stone37816df2012-05-16 18:45:18 +01002796pointer_handle_motion(void *data, struct wl_pointer *pointer,
2797 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002798{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002799 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002800 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002801 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002802 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002803 float sx = wl_fixed_to_double(sx_w);
2804 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002805
Paul Winwoodb22bf572013-08-29 10:52:54 +01002806 if (!window)
2807 return;
2808
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002809 input->sx = sx;
2810 input->sy = sy;
2811
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002812 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002813 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002814 * based on the old surface dimensions. However, if we have an active
2815 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002816 */
Derek Foreman46812b62015-08-26 17:13:27 -05002817 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002818 sy < window->main_surface->allocation.y ||
2819 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002820 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002821 return;
2822
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002823 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002824 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002825 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002826 }
2827
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002828 if (input->grab)
2829 widget = input->grab;
2830 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002831 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002832 if (widget) {
2833 if (widget->motion_handler)
2834 cursor = widget->motion_handler(input->focus_widget,
2835 input, time, sx, sy,
2836 widget->user_data);
2837 else
2838 cursor = widget->default_cursor;
2839 } else
2840 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002841
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002842 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002843}
2844
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002845static void
Daniel Stone37816df2012-05-16 18:45:18 +01002846pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002847 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002848{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002849 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002850 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002851 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002852
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002853 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002854 if (input->focus_widget && input->grab == NULL &&
2855 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002856 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002857
Neil Roberts6b28aad2012-01-23 19:11:18 +00002858 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002859 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002860 (*widget->button_handler)(widget,
2861 input, time,
2862 button, state,
2863 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002864
Daniel Stone4dbadb12012-05-30 16:31:51 +01002865 if (input->grab && input->grab_button == button &&
2866 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002867 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002868}
2869
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002870static void
Daniel Stone37816df2012-05-16 18:45:18 +01002871pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002872 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002873{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002874 struct input *input = data;
2875 struct widget *widget;
2876
2877 widget = input->focus_widget;
2878 if (input->grab)
2879 widget = input->grab;
2880 if (widget && widget->axis_handler)
2881 (*widget->axis_handler)(widget,
2882 input, time,
2883 axis, value,
2884 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002885}
2886
Peter Hutterer87743e92016-01-18 16:38:22 +10002887static void
2888pointer_handle_frame(void *data, struct wl_pointer *pointer)
2889{
2890 struct input *input = data;
2891 struct widget *widget;
2892
2893 widget = input->focus_widget;
2894 if (input->grab)
2895 widget = input->grab;
2896 if (widget && widget->pointer_frame_handler)
2897 (*widget->pointer_frame_handler)(widget,
2898 input,
2899 widget->user_data);
2900}
2901
2902static void
2903pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2904 uint32_t source)
2905{
2906 struct input *input = data;
2907 struct widget *widget;
2908
2909 widget = input->focus_widget;
2910 if (input->grab)
2911 widget = input->grab;
2912 if (widget && widget->axis_source_handler)
2913 (*widget->axis_source_handler)(widget,
2914 input,
2915 source,
2916 widget->user_data);
2917}
2918
2919static void
2920pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2921 uint32_t time, uint32_t axis)
2922{
2923 struct input *input = data;
2924 struct widget *widget;
2925
2926 widget = input->focus_widget;
2927 if (input->grab)
2928 widget = input->grab;
2929 if (widget && widget->axis_stop_handler)
2930 (*widget->axis_stop_handler)(widget,
2931 input, time,
2932 axis,
2933 widget->user_data);
2934}
2935
2936static void
2937pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2938 uint32_t axis, int32_t discrete)
2939{
2940 struct input *input = data;
2941 struct widget *widget;
2942
2943 widget = input->focus_widget;
2944 if (input->grab)
2945 widget = input->grab;
2946 if (widget && widget->axis_discrete_handler)
2947 (*widget->axis_discrete_handler)(widget,
2948 input,
2949 axis,
2950 discrete,
2951 widget->user_data);
2952}
2953
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002954static const struct wl_pointer_listener pointer_listener = {
2955 pointer_handle_enter,
2956 pointer_handle_leave,
2957 pointer_handle_motion,
2958 pointer_handle_button,
2959 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002960 pointer_handle_frame,
2961 pointer_handle_axis_source,
2962 pointer_handle_axis_stop,
2963 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002964};
2965
2966static void
2967input_remove_keyboard_focus(struct input *input)
2968{
2969 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002970
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002971 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002972
2973 if (!window)
2974 return;
2975
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002976 if (window->keyboard_focus_handler)
2977 (*window->keyboard_focus_handler)(window, NULL,
2978 window->user_data);
2979
2980 input->keyboard_focus = NULL;
2981}
2982
2983static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002984keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002985{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002986 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002987 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002988
2989 if (window && window->key_handler) {
2990 (*window->key_handler)(window, input, input->repeat_time,
2991 input->repeat_key, input->repeat_sym,
2992 WL_KEYBOARD_KEY_STATE_PRESSED,
2993 window->user_data);
2994 }
2995}
2996
2997static void
2998keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2999 uint32_t format, int fd, uint32_t size)
3000{
3001 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02003002 struct xkb_keymap *keymap;
3003 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01003004#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003005 struct xkb_compose_table *compose_table;
3006 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01003007#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003008 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003009 char *map_str;
3010
3011 if (!data) {
3012 close(fd);
3013 return;
3014 }
3015
3016 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3017 close(fd);
3018 return;
3019 }
3020
Sebastian Wickac1b92d2019-11-01 02:27:55 +01003021 map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003022 if (map_str == MAP_FAILED) {
3023 close(fd);
3024 return;
3025 }
3026
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003027 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03003028 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
3029 map_str,
3030 XKB_KEYMAP_FORMAT_TEXT_V1,
3031 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003032 munmap(map_str, size);
3033 close(fd);
3034
Rui Matos3eccb862013-10-10 19:44:22 +02003035 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003036 fprintf(stderr, "failed to compile keymap\n");
3037 return;
3038 }
3039
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003040 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02003041 state = xkb_state_new(keymap);
3042 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003043 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003044 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003045 return;
3046 }
3047
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003048 /* Look up the preferred locale, falling back to "C" as default */
3049 if (!(locale = getenv("LC_ALL")))
3050 if (!(locale = getenv("LC_CTYPE")))
3051 if (!(locale = getenv("LANG")))
3052 locale = "C";
3053
3054 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003055#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003056 compose_table =
3057 xkb_compose_table_new_from_locale(input->display->xkb_context,
3058 locale,
3059 XKB_COMPOSE_COMPILE_NO_FLAGS);
3060 if (compose_table) {
3061 /* Set up XKB compose state */
3062 compose_state = xkb_compose_state_new(compose_table,
3063 XKB_COMPOSE_STATE_NO_FLAGS);
3064 if (compose_state) {
3065 xkb_compose_state_unref(input->xkb.compose_state);
3066 xkb_compose_table_unref(input->xkb.compose_table);
3067 input->xkb.compose_state = compose_state;
3068 input->xkb.compose_table = compose_table;
3069 } else {
3070 fprintf(stderr, "could not create XKB compose state. "
3071 "Disabiling compose.\n");
3072 xkb_compose_table_unref(compose_table);
3073 compose_table = NULL;
3074 }
3075 } else {
3076 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3077 "Disabiling compose\n", locale);
3078 }
Daniel Stone5b015962016-10-20 14:45:58 +01003079#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003080
Rui Matos3eccb862013-10-10 19:44:22 +02003081 xkb_keymap_unref(input->xkb.keymap);
3082 xkb_state_unref(input->xkb.state);
3083 input->xkb.keymap = keymap;
3084 input->xkb.state = state;
3085
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003086 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003087 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003088 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003089 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003090 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003091 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003092}
3093
3094static void
3095keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3096 uint32_t serial, struct wl_surface *surface,
3097 struct wl_array *keys)
3098{
3099 struct input *input = data;
3100 struct window *window;
3101
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003102 if (!surface) {
3103 /* enter event for a window we've just destroyed */
3104 return;
3105 }
3106
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003107 input->display->serial = serial;
3108 input->keyboard_focus = wl_surface_get_user_data(surface);
3109
3110 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003111 if (window->keyboard_focus_handler)
3112 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003113 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003114}
3115
3116static void
3117keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3118 uint32_t serial, struct wl_surface *surface)
3119{
3120 struct input *input = data;
3121
3122 input->display->serial = serial;
3123 input_remove_keyboard_focus(input);
3124}
3125
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003126/* Translate symbols appropriately if a compose sequence is being entered */
3127static xkb_keysym_t
3128process_key_press(xkb_keysym_t sym, struct input *input)
3129{
Daniel Stone5b015962016-10-20 14:45:58 +01003130#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003131 if (!input->xkb.compose_state)
3132 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003133 if (sym == XKB_KEY_NoSymbol)
3134 return sym;
3135 if (xkb_compose_state_feed(input->xkb.compose_state,
3136 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3137 return sym;
3138
3139 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3140 case XKB_COMPOSE_COMPOSING:
3141 return XKB_KEY_NoSymbol;
3142 case XKB_COMPOSE_COMPOSED:
3143 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3144 case XKB_COMPOSE_CANCELLED:
3145 return XKB_KEY_NoSymbol;
3146 case XKB_COMPOSE_NOTHING:
3147 return sym;
3148 default:
3149 return sym;
3150 }
Daniel Stone5b015962016-10-20 14:45:58 +01003151#else
3152 return sym;
3153#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003154}
3155
Scott Moreau210d0792012-03-22 10:47:01 -06003156static void
Daniel Stone37816df2012-05-16 18:45:18 +01003157keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003158 uint32_t serial, uint32_t time, uint32_t key,
3159 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003160{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003161 struct input *input = data;
3162 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003163 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003164 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003165 const xkb_keysym_t *syms;
3166 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003167 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003168
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003169 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003170 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003171 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003172 return;
3173
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003174 /* We only use input grabs for pointer events for now, so just
3175 * ignore key presses if a grab is active. We expand the key
3176 * event delivery mechanism to route events to widgets to
3177 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003178 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003179 if (input->grab && input->grab_button == 0)
3180 return;
3181
Ran Benita2e1968f2014-08-19 23:59:51 +03003182 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003183
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003184 sym = XKB_KEY_NoSymbol;
3185 if (num_syms == 1)
3186 sym = syms[0];
3187
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003188
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003189 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003190 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003191 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003192 } else if (sym == XKB_KEY_F11 &&
3193 window->fullscreen_handler &&
3194 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3195 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003196 } else if (sym == XKB_KEY_F4 &&
3197 input->modifiers == MOD_ALT_MASK &&
3198 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003199 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003200 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003201 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3202 sym = process_key_press(sym, input);
3203
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003204 (*window->key_handler)(window, input, time, key,
3205 sym, state, window->user_data);
3206 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003207
3208 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3209 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003210 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003211 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3212 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003213 input->repeat_sym = sym;
3214 input->repeat_key = key;
3215 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003216 its.it_interval.tv_sec = input->repeat_rate_sec;
3217 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3218 its.it_value.tv_sec = input->repeat_delay_sec;
3219 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003220 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003221 }
3222}
3223
3224static void
Daniel Stone351eb612012-05-31 15:27:47 -04003225keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3226 uint32_t serial, uint32_t mods_depressed,
3227 uint32_t mods_latched, uint32_t mods_locked,
3228 uint32_t group)
3229{
3230 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003231 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003232
Matt Ropere61561f2013-06-24 16:52:43 +01003233 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3234 if (!input->xkb.keymap)
3235 return;
3236
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003237 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3238 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003239 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003240 XKB_STATE_MODS_DEPRESSED |
3241 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003242 input->modifiers = 0;
3243 if (mask & input->xkb.control_mask)
3244 input->modifiers |= MOD_CONTROL_MASK;
3245 if (mask & input->xkb.alt_mask)
3246 input->modifiers |= MOD_ALT_MASK;
3247 if (mask & input->xkb.shift_mask)
3248 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003249}
3250
Jonny Lamb06959082014-08-12 14:58:27 +02003251static void
3252set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3253{
3254 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3255 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3256
3257 /* a rate of zero disables any repeating, regardless of the delay's
3258 * value */
3259 if (rate == 0)
3260 return;
3261
3262 if (rate == 1)
3263 input->repeat_rate_sec = 1;
3264 else
3265 input->repeat_rate_nsec = 1000000000 / rate;
3266
3267 input->repeat_delay_sec = delay / 1000;
3268 delay -= (input->repeat_delay_sec * 1000);
3269 input->repeat_delay_nsec = delay * 1000 * 1000;
3270}
3271
3272static void
3273keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3274 int32_t rate, int32_t delay)
3275{
3276 struct input *input = data;
3277
3278 set_repeat_info(input, rate, delay);
3279}
3280
Daniel Stone37816df2012-05-16 18:45:18 +01003281static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003282 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003283 keyboard_handle_enter,
3284 keyboard_handle_leave,
3285 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003286 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003287 keyboard_handle_repeat_info
3288
Daniel Stone37816df2012-05-16 18:45:18 +01003289};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003290
3291static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003292touch_handle_down(void *data, struct wl_touch *wl_touch,
3293 uint32_t serial, uint32_t time, struct wl_surface *surface,
3294 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3295{
3296 struct input *input = data;
3297 struct widget *widget;
3298 float sx = wl_fixed_to_double(x_w);
3299 float sy = wl_fixed_to_double(y_w);
3300
Jeffy Chen97749172019-09-02 10:26:09 +08003301 if (!surface) {
3302 /* down event for a window we've just destroyed */
3303 return;
3304 }
3305
Rusty Lynch1084da52013-08-15 09:10:08 -07003306 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003307 input->touch_focus = wl_surface_get_user_data(surface);
3308 if (!input->touch_focus) {
3309 DBG("Failed to find to touch focus for surface %p\n", surface);
3310 return;
3311 }
3312
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003313 if (surface != input->touch_focus->main_surface->surface) {
3314 DBG("Ignoring input event from subsurface %p\n", surface);
3315 input->touch_focus = NULL;
3316 return;
3317 }
3318
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003319 if (input->grab)
3320 widget = input->grab;
3321 else
3322 widget = window_find_widget(input->touch_focus,
3323 wl_fixed_to_double(x_w),
3324 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003325 if (widget) {
3326 struct touch_point *tp = xmalloc(sizeof *tp);
3327 if (tp) {
3328 tp->id = id;
3329 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003330 tp->x = sx;
3331 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003332 wl_list_insert(&input->touch_point_list, &tp->link);
3333
3334 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003335 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003336 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003337 sx, sy,
3338 widget->user_data);
3339 }
3340 }
3341}
3342
3343static void
3344touch_handle_up(void *data, struct wl_touch *wl_touch,
3345 uint32_t serial, uint32_t time, int32_t id)
3346{
3347 struct input *input = data;
3348 struct touch_point *tp, *tmp;
3349
Rusty Lynch041815a2013-08-08 21:20:38 -07003350 if (!input->touch_focus) {
3351 DBG("No touch focus found for touch up event!\n");
3352 return;
3353 }
3354
3355 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3356 if (tp->id != id)
3357 continue;
3358
3359 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003360 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003361 time, id,
3362 tp->widget->user_data);
3363
3364 wl_list_remove(&tp->link);
3365 free(tp);
3366
3367 return;
3368 }
3369}
3370
3371static void
3372touch_handle_motion(void *data, struct wl_touch *wl_touch,
3373 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3374{
3375 struct input *input = data;
3376 struct touch_point *tp;
3377 float sx = wl_fixed_to_double(x_w);
3378 float sy = wl_fixed_to_double(y_w);
3379
3380 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3381
3382 if (!input->touch_focus) {
3383 DBG("No touch focus found for touch motion event!\n");
3384 return;
3385 }
3386
3387 wl_list_for_each(tp, &input->touch_point_list, link) {
3388 if (tp->id != id)
3389 continue;
3390
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003391 tp->x = sx;
3392 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003393 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003394 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003395 id, sx, sy,
3396 tp->widget->user_data);
3397 return;
3398 }
3399}
3400
3401static void
3402touch_handle_frame(void *data, struct wl_touch *wl_touch)
3403{
3404 struct input *input = data;
3405 struct touch_point *tp, *tmp;
3406
3407 DBG("touch_handle_frame\n");
3408
3409 if (!input->touch_focus) {
3410 DBG("No touch focus found for touch frame event!\n");
3411 return;
3412 }
3413
3414 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3415 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003416 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003417 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003418 }
3419}
3420
3421static void
3422touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3423{
3424 struct input *input = data;
3425 struct touch_point *tp, *tmp;
3426
3427 DBG("touch_handle_cancel\n");
3428
3429 if (!input->touch_focus) {
3430 DBG("No touch focus found for touch cancel event!\n");
3431 return;
3432 }
3433
3434 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3435 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003436 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3437 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003438
3439 wl_list_remove(&tp->link);
3440 free(tp);
3441 }
3442}
3443
Guillaume Champagne1cb09482020-01-27 20:12:16 -05003444static void
3445touch_handle_shape(void *data, struct wl_touch *wl_touch, int32_t id,
3446 wl_fixed_t major, wl_fixed_t minor)
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003447{
3448}
3449
Guillaume Champagne1cb09482020-01-27 20:12:16 -05003450static void
3451touch_handle_orientation(void *data, struct wl_touch *wl_touch, int32_t id,
3452 wl_fixed_t orientation)
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003453{
3454}
3455
Rusty Lynch041815a2013-08-08 21:20:38 -07003456static const struct wl_touch_listener touch_listener = {
3457 touch_handle_down,
3458 touch_handle_up,
3459 touch_handle_motion,
3460 touch_handle_frame,
3461 touch_handle_cancel,
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003462 touch_handle_shape,
3463 touch_handle_orientation,
Rusty Lynch041815a2013-08-08 21:20:38 -07003464};
3465
3466static void
Daniel Stone37816df2012-05-16 18:45:18 +01003467seat_handle_capabilities(void *data, struct wl_seat *seat,
3468 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003469{
Daniel Stone37816df2012-05-16 18:45:18 +01003470 struct input *input = data;
3471
3472 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3473 input->pointer = wl_seat_get_pointer(seat);
3474 wl_pointer_set_user_data(input->pointer, input);
3475 wl_pointer_add_listener(input->pointer, &pointer_listener,
3476 input);
3477 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003478 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003479 wl_pointer_release(input->pointer);
3480 else
3481 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003482 input->pointer = NULL;
3483 }
3484
3485 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3486 input->keyboard = wl_seat_get_keyboard(seat);
3487 wl_keyboard_set_user_data(input->keyboard, input);
3488 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3489 input);
3490 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003491 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003492 wl_keyboard_release(input->keyboard);
3493 else
3494 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003495 input->keyboard = NULL;
3496 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003497
3498 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3499 input->touch = wl_seat_get_touch(seat);
3500 wl_touch_set_user_data(input->touch, input);
3501 wl_touch_add_listener(input->touch, &touch_listener, input);
3502 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003503 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003504 wl_touch_release(input->touch);
3505 else
3506 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003507 input->touch = NULL;
3508 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003509}
3510
Rob Bradford08031182013-08-13 20:11:03 +01003511static void
3512seat_handle_name(void *data, struct wl_seat *seat,
3513 const char *name)
3514{
3515
3516}
3517
Daniel Stone37816df2012-05-16 18:45:18 +01003518static const struct wl_seat_listener seat_listener = {
3519 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003520 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003521};
3522
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003523void
3524input_get_position(struct input *input, int32_t *x, int32_t *y)
3525{
3526 *x = input->sx;
3527 *y = input->sy;
3528}
3529
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003530int
3531input_get_touch(struct input *input, int32_t id, float *x, float *y)
3532{
3533 struct touch_point *tp;
3534
3535 wl_list_for_each(tp, &input->touch_point_list, link) {
3536 if (tp->id != id)
3537 continue;
3538
3539 *x = tp->x;
3540 *y = tp->y;
3541 return 0;
3542 }
3543
3544 return -1;
3545}
3546
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003547struct display *
3548input_get_display(struct input *input)
3549{
3550 return input->display;
3551}
3552
Daniel Stone37816df2012-05-16 18:45:18 +01003553struct wl_seat *
3554input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003555{
Daniel Stone37816df2012-05-16 18:45:18 +01003556 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003557}
3558
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003559uint32_t
3560input_get_modifiers(struct input *input)
3561{
3562 return input->modifiers;
3563}
3564
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003565struct widget *
3566input_get_focus_widget(struct input *input)
3567{
3568 return input->focus_widget;
3569}
3570
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003571struct data_offer {
3572 struct wl_data_offer *offer;
3573 struct input *input;
3574 struct wl_array types;
3575 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003576
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003577 struct task io_task;
3578 int fd;
3579 data_func_t func;
3580 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003581 uint32_t dnd_action;
3582 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003583 void *user_data;
3584};
3585
3586static void
3587data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3588{
3589 struct data_offer *offer = data;
3590 char **p;
3591
3592 p = wl_array_add(&offer->types, sizeof *p);
3593 *p = strdup(type);
3594}
3595
Carlos Garnacho9c931792016-01-18 23:52:12 +01003596static void
3597data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3598{
3599 struct data_offer *offer = data;
3600
3601 offer->source_actions = source_actions;
3602}
3603
3604static void
3605data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3606{
3607 struct data_offer *offer = data;
3608
3609 offer->dnd_action = dnd_action;
3610}
3611
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003612static const struct wl_data_offer_listener data_offer_listener = {
3613 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003614 data_offer_source_actions,
3615 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003616};
3617
3618static void
3619data_offer_destroy(struct data_offer *offer)
3620{
3621 char **p;
3622
3623 offer->refcount--;
3624 if (offer->refcount == 0) {
3625 wl_data_offer_destroy(offer->offer);
3626 for (p = offer->types.data; *p; p++)
3627 free(*p);
3628 wl_array_release(&offer->types);
3629 free(offer);
3630 }
3631}
3632
3633static void
3634data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003635 struct wl_data_device *data_device,
3636 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003637{
3638 struct data_offer *offer;
3639
Brian Lovinbc919262013-08-07 15:34:59 -07003640 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003641
3642 wl_array_init(&offer->types);
3643 offer->refcount = 1;
3644 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003645 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003646 wl_data_offer_add_listener(offer->offer,
3647 &data_offer_listener, offer);
3648}
3649
3650static void
3651data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003652 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003653 wl_fixed_t x_w, wl_fixed_t y_w,
3654 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003655{
3656 struct input *input = data;
3657 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003658 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003659 float x = wl_fixed_to_double(x_w);
3660 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003661 char **p;
3662
Jeffy Chen97749172019-09-02 10:26:09 +08003663 if (!surface) {
3664 /* enter event for a window we've just destroyed */
3665 return;
3666 }
3667
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003668 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003669 input->drag_enter_serial = serial;
3670 input->drag_focus = window,
3671 input->drag_x = x;
3672 input->drag_y = y;
3673
3674 if (!input->touch_grab)
3675 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003676
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003677 if (offer) {
3678 input->drag_offer = wl_data_offer_get_user_data(offer);
3679
3680 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3681 *p = NULL;
3682
3683 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003684
3685 if (input->display->data_device_manager_version >=
3686 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3687 wl_data_offer_set_actions(offer,
3688 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3689 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3690 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3691 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003692 } else {
3693 input->drag_offer = NULL;
3694 types_data = NULL;
3695 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003696
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003697 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003698 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003699 window->user_data);
3700}
3701
3702static void
3703data_device_leave(void *data, struct wl_data_device *data_device)
3704{
3705 struct input *input = data;
3706
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003707 if (input->drag_offer) {
3708 data_offer_destroy(input->drag_offer);
3709 input->drag_offer = NULL;
3710 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003711}
3712
3713static void
3714data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003715 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003716{
3717 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003718 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003719 float x = wl_fixed_to_double(x_w);
3720 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003721 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003722
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003723 input->drag_x = x;
3724 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003725
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003726 if (input->drag_offer)
3727 types_data = input->drag_offer->types.data;
3728 else
3729 types_data = NULL;
3730
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003731 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003732 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003733 window->user_data);
3734}
3735
3736static void
3737data_device_drop(void *data, struct wl_data_device *data_device)
3738{
3739 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003740 struct window *window = input->drag_focus;
3741 float x, y;
3742
3743 x = input->drag_x;
3744 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003745
3746 if (window->drop_handler)
3747 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003748 x, y, window->user_data);
3749
3750 if (input->touch_grab)
3751 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003752}
3753
3754static void
3755data_device_selection(void *data,
3756 struct wl_data_device *wl_data_device,
3757 struct wl_data_offer *offer)
3758{
3759 struct input *input = data;
3760 char **p;
3761
3762 if (input->selection_offer)
3763 data_offer_destroy(input->selection_offer);
3764
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003765 if (offer) {
3766 input->selection_offer = wl_data_offer_get_user_data(offer);
3767 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3768 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003769 } else {
3770 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003771 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003772}
3773
3774static const struct wl_data_device_listener data_device_listener = {
3775 data_device_data_offer,
3776 data_device_enter,
3777 data_device_leave,
3778 data_device_motion,
3779 data_device_drop,
3780 data_device_selection
3781};
3782
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003783static void
3784input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003785{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003786 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003787 struct wl_cursor *cursor;
3788 struct wl_cursor_image *image;
Simon Ser992ee042021-02-07 11:32:46 +01003789 struct wl_surface *prev_surface;
3790 struct display *d = input->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003791
Daniel Stone80972742012-11-07 17:51:39 +11003792 if (!input->pointer)
3793 return;
3794
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003795 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003796 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003797 return;
3798
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003799 if (index >= (int) cursor->image_count) {
3800 fprintf(stderr, "cursor index out of range\n");
3801 return;
3802 }
3803
3804 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003805 buffer = wl_cursor_image_get_buffer(image);
3806 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003807 return;
3808
Simon Ser992ee042021-02-07 11:32:46 +01003809 /* Don't re-use the previous surface, otherwise the new buffer and the
3810 * new hotspot aren't applied atomically. */
3811 prev_surface = input->pointer_surface;
3812 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3813
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003814 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3815 wl_surface_damage(input->pointer_surface, 0, 0,
3816 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003817 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003818 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3819 input->pointer_surface,
3820 image->hotspot_x, image->hotspot_y);
Simon Ser992ee042021-02-07 11:32:46 +01003821
3822 if (prev_surface)
3823 wl_surface_destroy(prev_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003824}
3825
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003826static const struct wl_callback_listener pointer_surface_listener;
3827
Derek Foreman493d9792015-03-04 16:26:25 -06003828static bool
3829input_set_pointer_special(struct input *input)
3830{
3831 if (input->current_cursor == CURSOR_BLANK) {
3832 wl_pointer_set_cursor(input->pointer,
3833 input->pointer_enter_serial,
3834 NULL, 0, 0);
3835 return true;
3836 }
3837
3838 if (input->current_cursor == CURSOR_UNSET)
3839 return true;
3840
3841 return false;
3842}
3843
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003844static void
Derek Foreman118a4292015-04-22 17:23:35 -05003845schedule_pointer_image_update(struct input *input,
3846 struct wl_cursor *cursor,
3847 uint32_t duration,
3848 bool force_frame)
3849{
3850 /* Some silly cursor sets have enormous pauses in them. In these
3851 * cases it's better to use a timer even if it results in less
3852 * accurate presentation, since it will save us having to set the
3853 * same cursor image over and over again.
3854 *
3855 * This is really not the way we're supposed to time any kind of
3856 * animation, but we're pretending it's OK here because we don't
3857 * want animated cursors with long delays to needlessly hog CPU.
3858 *
3859 * We use force_frame to ensure we don't accumulate large timing
3860 * errors by running off the wrong clock.
3861 */
3862 if (!force_frame && duration > 100) {
3863 struct timespec tp;
3864
3865 clock_gettime(CLOCK_MONOTONIC, &tp);
3866 input->cursor_timer_start = tp.tv_sec * 1000
3867 + tp.tv_nsec / 1000000;
3868 cursor_delay_timer_reset(input, duration);
3869 return;
3870 }
3871
3872 /* for short durations we'll just spin on frame callbacks for
3873 * accurate timing - the way any kind of timing sensitive animation
3874 * should really be done. */
3875 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3876 wl_callback_add_listener(input->cursor_frame_cb,
3877 &pointer_surface_listener, input);
3878
3879}
3880
3881static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003882pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3883 uint32_t time)
3884{
3885 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003886 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003887 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003888 uint32_t duration;
3889 bool force_frame = true;
3890
3891 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003892
3893 if (callback) {
3894 assert(callback == input->cursor_frame_cb);
3895 wl_callback_destroy(callback);
3896 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003897 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003898 }
3899
Daniel Stone80972742012-11-07 17:51:39 +11003900 if (!input->pointer)
3901 return;
3902
Derek Foreman493d9792015-03-04 16:26:25 -06003903 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003904 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003905
Daniel Stonea494f1d2012-06-18 19:31:12 +01003906 cursor = input->display->cursors[input->current_cursor];
3907 if (!cursor)
3908 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003909
3910 /* FIXME We don't have the current time on the first call so we set
3911 * the animation start to the time of the first frame callback. */
3912 if (time == 0)
3913 input->cursor_anim_start = 0;
3914 else if (input->cursor_anim_start == 0)
3915 input->cursor_anim_start = time;
3916
Derek Foreman118a4292015-04-22 17:23:35 -05003917 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003918
Derek Foreman118a4292015-04-22 17:23:35 -05003919 if (time == 0 || input->cursor_anim_start == 0) {
3920 duration = 0;
3921 i = 0;
3922 } else
3923 i = wl_cursor_frame_and_duration(
3924 cursor,
3925 time - input->cursor_anim_start,
3926 &duration);
3927
Simon Ser992ee042021-02-07 11:32:46 +01003928 input_set_pointer_image_index(input, i);
3929
Derek Foreman118a4292015-04-22 17:23:35 -05003930 if (cursor->image_count > 1)
3931 schedule_pointer_image_update(input, cursor, duration,
3932 force_frame);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003933}
3934
Derek Foreman118a4292015-04-22 17:23:35 -05003935static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003936cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003937{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003938 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003939 struct timespec tp;
3940 struct wl_cursor *cursor;
3941 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003942
3943 if (!input->cursor_timer_running)
3944 return;
3945
Derek Foreman118a4292015-04-22 17:23:35 -05003946 cursor = input->display->cursors[input->current_cursor];
3947 if (!cursor)
3948 return;
3949
3950 clock_gettime(CLOCK_MONOTONIC, &tp);
3951 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3952 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3953}
3954
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003955static const struct wl_callback_listener pointer_surface_listener = {
3956 pointer_surface_frame_callback
3957};
3958
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003959void
3960input_set_pointer_image(struct input *input, int pointer)
3961{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003962 int force = 0;
3963
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003964 if (!input->pointer)
3965 return;
3966
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003967 if (input->pointer_enter_serial > input->cursor_serial)
3968 force = 1;
3969
3970 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003971 return;
3972
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003973 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003974 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003975 if (!input->cursor_frame_cb)
3976 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003977 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003978 /* The current frame callback may be stuck if, for instance,
3979 * the set cursor request was processed by the server after
3980 * this client lost the focus. In this case the cursor surface
3981 * might not be mapped and the frame callback wouldn't ever
3982 * complete. Send a set_cursor and attach to try to map the
3983 * cursor surface again so that the callback will finish */
3984 input_set_pointer_image_index(input, 0);
3985 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003986}
3987
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003988struct wl_data_device *
3989input_get_data_device(struct input *input)
3990{
3991 return input->data_device;
3992}
3993
3994void
3995input_set_selection(struct input *input,
3996 struct wl_data_source *source, uint32_t time)
3997{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003998 if (input->data_device)
3999 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004000}
4001
4002void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004003input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004004{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004005 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004006 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004007}
4008
4009static void
4010offer_io_func(struct task *task, uint32_t events)
4011{
4012 struct data_offer *offer =
4013 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01004014 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004015 unsigned int len;
4016 char buffer[4096];
4017
4018 len = read(offer->fd, buffer, sizeof buffer);
4019 offer->func(buffer, len,
4020 offer->x, offer->y, offer->user_data);
4021
4022 if (len == 0) {
Harish Krupo43152a32019-04-19 22:06:44 +05304023 if ((offer != offer->input->selection_offer) &&
4024 (display->data_device_manager_version >=
4025 WL_DATA_OFFER_FINISH_SINCE_VERSION))
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01004026 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004027 close(offer->fd);
4028 data_offer_destroy(offer);
4029 }
4030}
4031
4032static void
4033data_offer_receive_data(struct data_offer *offer, const char *mime_type,
4034 data_func_t func, void *user_data)
4035{
4036 int p[2];
4037
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02004038 if (pipe2(p, O_CLOEXEC) == -1)
4039 return;
4040
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004041 wl_data_offer_receive(offer->offer, mime_type, p[1]);
4042 close(p[1]);
4043
4044 offer->io_task.run = offer_io_func;
4045 offer->fd = p[0];
4046 offer->func = func;
4047 offer->refcount++;
4048 offer->user_data = user_data;
4049
4050 display_watch_fd(offer->input->display,
4051 offer->fd, EPOLLIN, &offer->io_task);
4052}
4053
4054void
4055input_receive_drag_data(struct input *input, const char *mime_type,
4056 data_func_t func, void *data)
4057{
4058 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004059 input->drag_offer->x = input->drag_x;
4060 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004061}
4062
4063int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004064input_receive_drag_data_to_fd(struct input *input,
4065 const char *mime_type, int fd)
4066{
4067 if (input->drag_offer)
4068 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4069
4070 return 0;
4071}
4072
4073int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004074input_receive_selection_data(struct input *input, const char *mime_type,
4075 data_func_t func, void *data)
4076{
4077 char **p;
4078
4079 if (input->selection_offer == NULL)
4080 return -1;
4081
4082 for (p = input->selection_offer->types.data; *p; p++)
4083 if (strcmp(mime_type, *p) == 0)
4084 break;
4085
4086 if (*p == NULL)
4087 return -1;
4088
4089 data_offer_receive_data(input->selection_offer,
4090 mime_type, func, data);
4091 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004092}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004093
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004094int
4095input_receive_selection_data_to_fd(struct input *input,
4096 const char *mime_type, int fd)
4097{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004098 if (input->selection_offer)
4099 wl_data_offer_receive(input->selection_offer->offer,
4100 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004101
4102 return 0;
4103}
4104
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004105void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004106window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004107{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004108 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004109 return;
4110
ant8mef99fac22018-11-28 22:46:37 +01004111 xdg_toplevel_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004112}
4113
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004114static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004115surface_set_synchronized(struct surface *surface)
4116{
4117 if (!surface->subsurface)
4118 return;
4119
4120 if (surface->synchronized)
4121 return;
4122
4123 wl_subsurface_set_sync(surface->subsurface);
4124 surface->synchronized = 1;
4125}
4126
4127static void
4128surface_set_synchronized_default(struct surface *surface)
4129{
4130 if (!surface->subsurface)
4131 return;
4132
4133 if (surface->synchronized == surface->synchronized_default)
4134 return;
4135
4136 if (surface->synchronized_default)
4137 wl_subsurface_set_sync(surface->subsurface);
4138 else
4139 wl_subsurface_set_desync(surface->subsurface);
4140
4141 surface->synchronized = surface->synchronized_default;
4142}
4143
4144static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004145surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004146{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004147 struct widget *widget = surface->widget;
4148 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004149
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004150 if (surface->input_region) {
4151 wl_region_destroy(surface->input_region);
4152 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004153 }
4154
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004155 if (surface->opaque_region)
4156 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004157
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004158 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004159
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004160 if (widget->resize_handler)
4161 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004162 widget->allocation.width,
4163 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004164 widget->user_data);
4165
Pekka Paalanen35e82632013-04-25 13:57:48 +03004166 if (surface->subsurface &&
4167 (surface->allocation.x != widget->allocation.x ||
4168 surface->allocation.y != widget->allocation.y)) {
4169 wl_subsurface_set_position(surface->subsurface,
4170 widget->allocation.x,
4171 widget->allocation.y);
4172 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004173 if (surface->allocation.width != widget->allocation.width ||
4174 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004175 window_schedule_redraw(widget->window);
4176 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004177 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004178
4179 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004180 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004181 widget->allocation.width,
4182 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004183}
4184
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004185static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004186window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004187{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004188 struct surface *surface;
4189
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004190 widget_set_allocation(window->main_surface->widget,
4191 window->pending_allocation.x,
4192 window->pending_allocation.y,
4193 window->pending_allocation.width,
4194 window->pending_allocation.height);
4195
4196 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004197
4198 /* The main surface is in the list, too. Main surface's
4199 * resize_handler is responsible for calling widget_set_allocation()
4200 * on all sub-surface root widgets, so they will be resized
4201 * properly.
4202 */
4203 wl_list_for_each(surface, &window->subsurface_list, link) {
4204 if (surface == window->main_surface)
4205 continue;
4206
4207 surface_set_synchronized(surface);
4208 surface_resize(surface);
4209 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004210
4211 if (!window->fullscreen && !window->maximized)
4212 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004213
4214 if (window->confined && window->confined_widget) {
4215 struct wl_compositor *compositor = window->display->compositor;
4216 struct wl_region *region;
4217 struct widget *widget = window->confined_widget;
4218
4219 region = wl_compositor_create_region(compositor);
4220 wl_region_add(region,
4221 widget->allocation.x,
4222 widget->allocation.y,
4223 widget->allocation.width,
4224 widget->allocation.height);
4225 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4226 region);
4227 wl_region_destroy(region);
4228 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004229}
4230
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004231static void
4232idle_resize(struct window *window)
4233{
4234 window->resize_needed = 0;
4235 window->redraw_needed = 1;
4236
4237 DBG("from %dx%d to %dx%d\n",
4238 window->main_surface->server_allocation.width,
4239 window->main_surface->server_allocation.height,
4240 window->pending_allocation.width,
4241 window->pending_allocation.height);
4242
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004243 window_do_resize(window);
4244}
4245
4246static void
4247undo_resize(struct window *window)
4248{
4249 window->pending_allocation.width =
4250 window->main_surface->server_allocation.width;
4251 window->pending_allocation.height =
4252 window->main_surface->server_allocation.height;
4253
4254 DBG("back to %dx%d\n",
4255 window->main_surface->server_allocation.width,
4256 window->main_surface->server_allocation.height);
4257
4258 window_do_resize(window);
4259
4260 if (window->pending_allocation.width == 0 &&
4261 window->pending_allocation.height == 0) {
4262 fprintf(stderr, "Error: Could not draw a surface, "
4263 "most likely due to insufficient disk space in "
4264 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4265 exit(EXIT_FAILURE);
4266 }
4267}
4268
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004269void
4270window_schedule_resize(struct window *window, int width, int height)
4271{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004272 /* We should probably get these numbers from the theme. */
4273 const int min_width = 200, min_height = 200;
4274
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004275 window->pending_allocation.x = 0;
4276 window->pending_allocation.y = 0;
4277 window->pending_allocation.width = width;
4278 window->pending_allocation.height = height;
4279
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004280 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004281 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004282 window->min_allocation.width = min_width;
4283 else
4284 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004285 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004286 window->min_allocation.height = min_height;
4287 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004288 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004289 }
4290
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004291 if (window->pending_allocation.width < window->min_allocation.width)
4292 window->pending_allocation.width = window->min_allocation.width;
4293 if (window->pending_allocation.height < window->min_allocation.height)
4294 window->pending_allocation.height = window->min_allocation.height;
4295
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004296 window->resize_needed = 1;
4297 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004298}
4299
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004300void
4301widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4302{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004303 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004304}
4305
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004306static int
4307window_get_shadow_margin(struct window *window)
4308{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004309 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004310 return frame_get_shadow_margin(window->frame->frame);
4311 else
4312 return 0;
4313}
4314
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004315void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004316window_inhibit_redraw(struct window *window)
4317{
4318 window->redraw_inhibited = 1;
4319 wl_list_remove(&window->redraw_task.link);
4320 wl_list_init(&window->redraw_task.link);
4321 window->redraw_task_scheduled = 0;
4322}
4323
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004324void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004325window_uninhibit_redraw(struct window *window)
4326{
4327 window->redraw_inhibited = 0;
4328 if (window->redraw_needed || window->resize_needed)
4329 window_schedule_redraw_task(window);
4330}
4331
4332static void
4333xdg_surface_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01004334 struct xdg_surface *xdg_surface,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004335 uint32_t serial)
4336{
4337 struct window *window = data;
4338
ant8mef99fac22018-11-28 22:46:37 +01004339 xdg_surface_ack_configure(window->xdg_surface, serial);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004340
4341 if (window->state_changed_handler)
4342 window->state_changed_handler(window, window->user_data);
4343
4344 window_uninhibit_redraw(window);
4345}
4346
ant8mef99fac22018-11-28 22:46:37 +01004347static const struct xdg_surface_listener xdg_surface_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004348 xdg_surface_handle_configure
4349};
4350
4351static void
ant8mef99fac22018-11-28 22:46:37 +01004352xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004353 int32_t width, int32_t height,
4354 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004355{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004356 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004357 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004358
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004359 window->maximized = 0;
4360 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004361 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004362 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004363
4364 wl_array_for_each(p, states) {
4365 uint32_t state = *p;
4366 switch (state) {
ant8mef99fac22018-11-28 22:46:37 +01004367 case XDG_TOPLEVEL_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004368 window->maximized = 1;
4369 break;
ant8mef99fac22018-11-28 22:46:37 +01004370 case XDG_TOPLEVEL_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004371 window->fullscreen = 1;
4372 break;
ant8mef99fac22018-11-28 22:46:37 +01004373 case XDG_TOPLEVEL_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004374 window->resizing = 1;
4375 break;
ant8mef99fac22018-11-28 22:46:37 +01004376 case XDG_TOPLEVEL_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004377 window->focused = 1;
4378 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004379 default:
4380 /* Unknown state */
4381 break;
4382 }
4383 }
4384
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004385 if (window->frame) {
4386 if (window->maximized) {
4387 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4388 } else {
4389 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4390 }
4391
4392 if (window->focused) {
4393 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4394 } else {
4395 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4396 }
4397 }
4398
Jasper St. Pierref184c382014-05-06 08:33:27 -04004399 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004400 /* The width / height params are for window geometry,
4401 * but window_schedule_resize takes allocation. Add
4402 * on the shadow margin to get the difference. */
4403 int margin = window_get_shadow_margin(window);
4404
4405 window_schedule_resize(window,
4406 width + margin * 2,
4407 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004408 } else if (window->saved_allocation.width > 0 &&
4409 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004410 window_schedule_resize(window,
4411 window->saved_allocation.width,
4412 window->saved_allocation.height);
4413 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004414}
4415
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004416static void
ant8mef99fac22018-11-28 22:46:37 +01004417xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004418{
4419 struct window *window = data;
4420 window_close(window);
4421}
4422
ant8mef99fac22018-11-28 22:46:37 +01004423static const struct xdg_toplevel_listener xdg_toplevel_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004424 xdg_toplevel_handle_configure,
4425 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004426};
4427
4428static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004429window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004430{
ant8mef99fac22018-11-28 22:46:37 +01004431 struct xdg_toplevel *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004432
4433 if (!window->xdg_surface)
4434 return;
4435
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004436 if (window->parent == window->last_parent)
4437 return;
4438
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004439 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004440 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004441 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004442 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004443
ant8mef99fac22018-11-28 22:46:37 +01004444 xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004445 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004446}
4447
4448static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004449window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004450{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004451 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004452 frame_input_rect(window->frame->frame,
4453 &geometry->x,
4454 &geometry->y,
4455 &geometry->width,
4456 &geometry->height);
4457 else
4458 window_get_allocation(window, geometry);
4459}
4460
4461static void
4462window_sync_geometry(struct window *window)
4463{
4464 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004465
4466 if (!window->xdg_surface)
4467 return;
4468
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004469 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004470 if (geometry.x == window->last_geometry.x &&
4471 geometry.y == window->last_geometry.y &&
4472 geometry.width == window->last_geometry.width &&
4473 geometry.height == window->last_geometry.height)
4474 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004475
ant8mef99fac22018-11-28 22:46:37 +01004476 xdg_surface_set_window_geometry(window->xdg_surface,
4477 geometry.x,
4478 geometry.y,
4479 geometry.width,
4480 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004481 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004482}
4483
4484static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004485window_flush(struct window *window)
4486{
4487 struct surface *surface;
4488
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004489 assert(!window->redraw_inhibited);
4490
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004491 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004492 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004493 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004494 if (window->xdg_toplevel)
4495 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004496 }
4497
4498 wl_list_for_each(surface, &window->subsurface_list, link) {
4499 if (surface == window->main_surface)
4500 continue;
4501
4502 surface_flush(surface);
4503 }
4504
4505 surface_flush(window->main_surface);
4506}
4507
4508static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004509menu_destroy(struct menu *menu)
4510{
4511 widget_destroy(menu->widget);
4512 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004513 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004514 free(menu);
4515}
4516
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004517void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004518window_get_allocation(struct window *window,
4519 struct rectangle *allocation)
4520{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004521 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004522}
4523
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004524static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004525widget_redraw(struct widget *widget)
4526{
4527 struct widget *child;
4528
4529 if (widget->redraw_handler)
4530 widget->redraw_handler(widget, widget->user_data);
4531 wl_list_for_each(child, &widget->child_list, link)
4532 widget_redraw(child);
4533}
4534
4535static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004536frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4537{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004538 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004539
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004540 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004541 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004542 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004543 surface->frame_cb = NULL;
4544
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004545 surface->last_time = time;
4546
Pekka Paalanen71233882013-04-25 13:57:53 +03004547 if (surface->redraw_needed || surface->window->redraw_needed) {
4548 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004549 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004550 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004551}
4552
4553static const struct wl_callback_listener listener = {
4554 frame_callback
4555};
4556
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004557static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004558surface_redraw(struct surface *surface)
4559{
Pekka Paalanen71233882013-04-25 13:57:53 +03004560 DBG_OBJ(surface->surface, "begin\n");
4561
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004562 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004563 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004564
4565 /* Whole-window redraw forces a redraw even if the previous has
4566 * not yet hit the screen.
4567 */
4568 if (surface->frame_cb) {
4569 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004570 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004571
Pekka Paalanen71233882013-04-25 13:57:53 +03004572 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004573 wl_callback_destroy(surface->frame_cb);
4574 }
4575
Neil Roberts97b747c2013-12-19 16:17:12 +00004576 if (surface->widget->use_cairo &&
4577 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004578 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004579 return -1;
4580 }
4581
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004582 surface->frame_cb = wl_surface_frame(surface->surface);
4583 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004584 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004585
4586 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004587 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004588 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004589 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004590 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004591}
4592
4593static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004594idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004595{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004596 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004597 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004598 int failed = 0;
4599 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004600
Pekka Paalanen71233882013-04-25 13:57:53 +03004601 DBG(" --------- \n");
4602
Pekka Paalaneneebff542013-04-25 13:57:52 +03004603 wl_list_init(&window->redraw_task.link);
4604 window->redraw_task_scheduled = 0;
4605
4606 if (window->resize_needed) {
4607 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004608 if (window->main_surface->frame_cb) {
4609 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004610 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004611 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004612
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004613 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004614 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004615 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004616
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004617 if (surface_redraw(window->main_surface) < 0) {
4618 /*
4619 * Only main_surface failure will cause us to undo the resize.
4620 * If sub-surfaces fail, they will just be broken with old
4621 * content.
4622 */
4623 failed = 1;
4624 } else {
4625 wl_list_for_each(surface, &window->subsurface_list, link) {
4626 if (surface == window->main_surface)
4627 continue;
4628
4629 surface_redraw(surface);
4630 }
4631 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004632
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004633 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004634 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004635
4636 wl_list_for_each(surface, &window->subsurface_list, link)
4637 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004638
4639 if (resized && failed) {
4640 /* Restore widget tree to correspond to what is on screen. */
4641 undo_resize(window);
4642 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004643}
4644
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004645static void
4646window_schedule_redraw_task(struct window *window)
4647{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004648 if (window->redraw_inhibited)
4649 return;
4650
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004651 if (!window->redraw_task_scheduled) {
4652 window->redraw_task.run = idle_redraw;
4653 display_defer(window->display, &window->redraw_task);
4654 window->redraw_task_scheduled = 1;
4655 }
4656}
4657
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004658void
4659window_schedule_redraw(struct window *window)
4660{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004661 struct surface *surface;
4662
Pekka Paalanen71233882013-04-25 13:57:53 +03004663 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4664
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004665 wl_list_for_each(surface, &window->subsurface_list, link)
4666 surface->redraw_needed = 1;
4667
4668 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004669}
4670
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004671int
4672window_is_fullscreen(struct window *window)
4673{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004674 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004675}
4676
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004677void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004678window_set_fullscreen(struct window *window, int fullscreen)
4679{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004680 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004681 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004682
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004683 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004684 return;
4685
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004686 if (fullscreen)
ant8mef99fac22018-11-28 22:46:37 +01004687 xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004688 else
ant8mef99fac22018-11-28 22:46:37 +01004689 xdg_toplevel_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004690}
4691
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004692int
4693window_is_maximized(struct window *window)
4694{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004695 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004696}
4697
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004698void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004699window_set_maximized(struct window *window, int maximized)
4700{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004701 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004702 return;
4703
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004704 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004705 return;
4706
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004707 if (maximized)
ant8mef99fac22018-11-28 22:46:37 +01004708 xdg_toplevel_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004709 else
ant8mef99fac22018-11-28 22:46:37 +01004710 xdg_toplevel_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004711}
4712
Jasper St. Pierrede680992014-04-10 17:23:49 -07004713int
4714window_is_resizing(struct window *window)
4715{
4716 return window->resizing;
4717}
4718
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004719void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004720window_set_minimized(struct window *window)
4721{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004722 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004723 return;
4724
ant8mef99fac22018-11-28 22:46:37 +01004725 xdg_toplevel_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004726}
4727
4728void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004729window_set_user_data(struct window *window, void *data)
4730{
4731 window->user_data = data;
4732}
4733
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004734void *
4735window_get_user_data(struct window *window)
4736{
4737 return window->user_data;
4738}
4739
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004740void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004741window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004742 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004743{
4744 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004745}
4746
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004747void
4748window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004749 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004750{
4751 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004752}
4753
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004754void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004755window_set_data_handler(struct window *window, window_data_handler_t handler)
4756{
4757 window->data_handler = handler;
4758}
4759
4760void
4761window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4762{
4763 window->drop_handler = handler;
4764}
4765
4766void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004767window_set_close_handler(struct window *window,
4768 window_close_handler_t handler)
4769{
4770 window->close_handler = handler;
4771}
4772
4773void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004774window_set_fullscreen_handler(struct window *window,
4775 window_fullscreen_handler_t handler)
4776{
4777 window->fullscreen_handler = handler;
4778}
4779
4780void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004781window_set_output_handler(struct window *window,
4782 window_output_handler_t handler)
4783{
4784 window->output_handler = handler;
4785}
4786
4787void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004788window_set_state_changed_handler(struct window *window,
4789 window_state_changed_handler_t handler)
4790{
4791 window->state_changed_handler = handler;
4792}
4793
4794void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004795window_set_pointer_locked_handler(struct window *window,
4796 locked_pointer_locked_handler_t locked,
4797 locked_pointer_unlocked_handler_t unlocked)
4798{
4799 window->pointer_unlocked_handler = unlocked;
4800 window->pointer_locked_handler = locked;
4801}
4802
4803void
4804window_set_pointer_confined_handler(struct window *window,
4805 confined_pointer_confined_handler_t confined,
4806 confined_pointer_unconfined_handler_t unconfined)
4807{
4808 window->pointer_confined_handler = confined;
4809 window->pointer_unconfined_handler = unconfined;
4810}
4811
4812void
4813window_set_locked_pointer_motion_handler(struct window *window,
4814 window_locked_pointer_motion_handler_t handler)
4815{
4816 window->locked_pointer_motion_handler = handler;
4817}
4818
4819void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004820window_set_title(struct window *window, const char *title)
4821{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004822 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004823 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004824 if (window->frame) {
4825 frame_set_title(window->frame->frame, title);
4826 widget_schedule_redraw(window->frame->widget);
4827 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004828 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01004829 xdg_toplevel_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004830}
4831
4832const char *
4833window_get_title(struct window *window)
4834{
4835 return window->title;
4836}
4837
4838void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004839window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4840{
4841 struct text_cursor_position *text_cursor_position =
4842 window->display->text_cursor_position;
4843
Scott Moreau9295ce02012-06-01 12:46:10 -06004844 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004845 return;
4846
4847 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004848 window->main_surface->surface,
4849 wl_fixed_from_int(x),
4850 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004851}
4852
Casey Dahlin9074db52012-04-19 22:50:09 -04004853static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004854relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4855 uint32_t utime_hi,
4856 uint32_t utime_lo,
4857 wl_fixed_t dx,
4858 wl_fixed_t dy,
4859 wl_fixed_t dx_unaccel,
4860 wl_fixed_t dy_unaccel)
4861{
4862 struct input *input = data;
4863 struct window *window = input->pointer_focus;
4864 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4865
4866 if (window->locked_pointer_motion_handler &&
4867 window->pointer_locked) {
4868 window->locked_pointer_motion_handler(
4869 window, input, ms,
4870 wl_fixed_to_double(dx),
4871 wl_fixed_to_double(dy),
4872 window->user_data);
4873 }
4874}
4875
4876static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4877 relative_pointer_handle_motion,
4878};
4879
4880static void
4881locked_pointer_locked(void *data,
4882 struct zwp_locked_pointer_v1 *locked_pointer)
4883{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004884 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004885 struct window *window = input->locked_window;
4886
4887 if (!window)
4888 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004889
4890 window->pointer_locked = true;
4891
4892 if (window->pointer_locked_handler) {
4893 window->pointer_locked_handler(window,
4894 input,
4895 window->user_data);
4896 }
4897}
4898
4899static void
4900locked_pointer_unlocked(void *data,
4901 struct zwp_locked_pointer_v1 *locked_pointer)
4902{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004903 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004904 struct window *window = input->locked_window;
4905
4906 if (!window)
4907 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004908
4909 window_unlock_pointer(window);
4910
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004911 input->locked_window = NULL;
4912
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004913 if (window->pointer_unlocked_handler) {
4914 window->pointer_unlocked_handler(window,
4915 input,
4916 window->user_data);
4917 }
4918}
4919
4920static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4921 locked_pointer_locked,
4922 locked_pointer_unlocked,
4923};
4924
4925int
4926window_lock_pointer(struct window *window, struct input *input)
4927{
4928 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4929 window->display->relative_pointer_manager;
4930 struct zwp_pointer_constraints_v1 *pointer_constraints =
4931 window->display->pointer_constraints;
4932 struct zwp_relative_pointer_v1 *relative_pointer;
4933 struct zwp_locked_pointer_v1 *locked_pointer;
4934
4935 if (!window->display->relative_pointer_manager)
4936 return -1;
4937
4938 if (!window->display->pointer_constraints)
4939 return -1;
4940
4941 if (window->locked_pointer)
4942 return -1;
4943
4944 if (window->confined_pointer)
4945 return -1;
4946
4947 if (!input->pointer)
4948 return -1;
4949
4950 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4951 relative_pointer_manager, input->pointer);
4952 zwp_relative_pointer_v1_add_listener(relative_pointer,
4953 &relative_pointer_listener,
4954 input);
4955
4956 locked_pointer =
4957 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4958 window->main_surface->surface,
4959 input->pointer,
4960 NULL,
4961 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4962 zwp_locked_pointer_v1_add_listener(locked_pointer,
4963 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004964 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004965
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004966 window->locked_pointer = locked_pointer;
4967 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004968 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004969
4970 return 0;
4971}
4972
4973void
4974window_unlock_pointer(struct window *window)
4975{
4976 if (!window->locked_pointer)
4977 return;
4978
4979 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4980 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4981 window->locked_pointer = NULL;
4982 window->relative_pointer = NULL;
4983 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004984}
4985
4986void
4987widget_set_locked_pointer_cursor_hint(struct widget *widget,
4988 float x, float y)
4989{
4990 struct window *window = widget->window;
4991
4992 if (!window->locked_pointer)
4993 return;
4994
4995 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4996 wl_fixed_from_double(x),
4997 wl_fixed_from_double(y));
4998 wl_surface_commit(window->main_surface->surface);
4999}
5000
5001static void
5002confined_pointer_confined(void *data,
5003 struct zwp_confined_pointer_v1 *confined_pointer)
5004{
Dima Ryazanov336ce672018-11-14 21:03:35 -08005005 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005006 struct window *window = input->confined_window;
5007
5008 if (!window)
5009 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005010
5011 window->confined = true;
5012
5013 if (window->pointer_confined_handler) {
5014 window->pointer_confined_handler(window,
5015 input,
5016 window->user_data);
5017 }
5018}
5019
5020static void
5021confined_pointer_unconfined(void *data,
5022 struct zwp_confined_pointer_v1 *confined_pointer)
5023{
Dima Ryazanov336ce672018-11-14 21:03:35 -08005024 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005025 struct window *window = input->confined_window;
5026
5027 if (!window)
5028 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005029
5030 window_unconfine_pointer(window);
5031
5032 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005033 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005034
5035 if (window->pointer_unconfined_handler) {
5036 window->pointer_unconfined_handler(window,
5037 input,
5038 window->user_data);
5039 }
5040}
5041
5042static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
5043 confined_pointer_confined,
5044 confined_pointer_unconfined,
5045};
5046
5047int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005048window_confine_pointer_to_rectangles(struct window *window,
5049 struct input *input,
5050 struct rectangle *rectangles,
5051 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005052{
5053 struct zwp_pointer_constraints_v1 *pointer_constraints =
5054 window->display->pointer_constraints;
5055 struct zwp_confined_pointer_v1 *confined_pointer;
5056 struct wl_compositor *compositor = window->display->compositor;
5057 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005058 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005059
5060 if (!window->display->pointer_constraints)
5061 return -1;
5062
5063 if (window->locked_pointer)
5064 return -1;
5065
5066 if (window->confined_pointer)
5067 return -1;
5068
5069 if (!input->pointer)
5070 return -1;
5071
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005072 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005073 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005074 for (i = 0; i < num_rectangles; i++) {
5075 wl_region_add(region,
5076 rectangles[i].x,
5077 rectangles[i].y,
5078 rectangles[i].width,
5079 rectangles[i].height);
5080 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005081 }
5082
5083 confined_pointer =
5084 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5085 window->main_surface->surface,
5086 input->pointer,
5087 region,
5088 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5089 if (region)
5090 wl_region_destroy(region);
5091
5092 zwp_confined_pointer_v1_add_listener(confined_pointer,
5093 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08005094 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005095
5096 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005097 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005098 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005099
5100 return 0;
5101}
5102
5103void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005104window_update_confine_rectangles(struct window *window,
5105 struct rectangle *rectangles,
5106 int num_rectangles)
5107{
5108 struct wl_compositor *compositor = window->display->compositor;
5109 struct wl_region *region;
5110 int i;
5111
5112 region = wl_compositor_create_region(compositor);
5113 for (i = 0; i < num_rectangles; i++) {
5114 wl_region_add(region,
5115 rectangles[i].x,
5116 rectangles[i].y,
5117 rectangles[i].width,
5118 rectangles[i].height);
5119 }
5120
5121 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5122
5123 wl_region_destroy(region);
5124}
5125
5126int
5127window_confine_pointer_to_widget(struct window *window,
5128 struct widget *widget,
5129 struct input *input)
5130{
5131 int ret;
5132
5133 if (widget) {
5134 ret = window_confine_pointer_to_rectangles(window,
5135 input,
5136 &widget->allocation,
5137 1);
5138 window->confined_widget = widget;
5139 return ret;
5140 } else {
5141 return window_confine_pointer_to_rectangles(window,
5142 input,
5143 NULL,
5144 0);
5145 }
5146}
5147
5148void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005149window_unconfine_pointer(struct window *window)
5150{
5151 if (!window->confined_pointer)
5152 return;
5153
5154 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5155 window->confined_pointer = NULL;
5156 window->confined = false;
5157}
5158
5159static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005160surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005161 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005162{
Rob Bradford7507b572012-05-15 17:55:34 +01005163 struct window *window = data;
5164 struct output *output;
5165 struct output *output_found = NULL;
5166 struct window_output *window_output;
5167
5168 wl_list_for_each(output, &window->display->output_list, link) {
5169 if (output->output == wl_output) {
5170 output_found = output;
5171 break;
5172 }
5173 }
5174
5175 if (!output_found)
5176 return;
5177
Brian Lovinbc919262013-08-07 15:34:59 -07005178 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005179 window_output->output = output_found;
5180
5181 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005182
5183 if (window->output_handler)
5184 window->output_handler(window, output_found, 1,
5185 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005186}
5187
5188static void
5189surface_leave(void *data,
5190 struct wl_surface *wl_surface, struct wl_output *output)
5191{
Rob Bradford7507b572012-05-15 17:55:34 +01005192 struct window *window = data;
5193 struct window_output *window_output;
5194 struct window_output *window_output_found = NULL;
5195
5196 wl_list_for_each(window_output, &window->window_output_list, link) {
5197 if (window_output->output->output == output) {
5198 window_output_found = window_output;
5199 break;
5200 }
5201 }
5202
5203 if (window_output_found) {
5204 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005205
5206 if (window->output_handler)
5207 window->output_handler(window, window_output->output,
5208 0, window->user_data);
5209
Rob Bradford7507b572012-05-15 17:55:34 +01005210 free(window_output_found);
5211 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005212}
5213
5214static const struct wl_surface_listener surface_listener = {
5215 surface_enter,
5216 surface_leave
5217};
5218
Pekka Paalanen4e373742013-02-13 16:17:13 +02005219static struct surface *
5220surface_create(struct window *window)
5221{
5222 struct display *display = window->display;
5223 struct surface *surface;
5224
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005225 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005226 surface->window = window;
5227 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005228 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005229 wl_surface_add_listener(surface->surface, &surface_listener, window);
5230
Pekka Paalanen35e82632013-04-25 13:57:48 +03005231 wl_list_insert(&window->subsurface_list, &surface->link);
Harish Krupo7bcbab12018-12-11 13:45:43 +05305232 surface->viewport = NULL;
Pekka Paalanen35e82632013-04-25 13:57:48 +03005233
Pekka Paalanen4e373742013-02-13 16:17:13 +02005234 return surface;
5235}
5236
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005237static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005238get_preferred_buffer_type(struct display *display)
5239{
5240#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005241 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005242 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5243#endif
5244
5245 return WINDOW_BUFFER_TYPE_SHM;
5246}
5247
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005248static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005249window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005250{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005251 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005252 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005253
Peter Huttererf3d62272013-08-08 11:57:05 +10005254 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005255 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005256 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005257
5258 surface = surface_create(window);
5259 window->main_surface = surface;
5260
Michael Teyfeld6371b52017-10-11 17:17:29 +02005261 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005262
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005263 window->custom = custom;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005264
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005265 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005266
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005267 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005268 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005269 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005270
Rob Bradford7507b572012-05-15 17:55:34 +01005271 wl_list_init (&window->window_output_list);
5272
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005273 return window;
5274}
5275
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005276struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005277window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005278{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005279 struct window *window;
5280
5281 window = window_create_internal(display, 0);
5282
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005283 if (window->display->xdg_shell) {
5284 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005285 xdg_wm_base_get_xdg_surface(window->display->xdg_shell,
5286 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005287 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005288
ant8mef99fac22018-11-28 22:46:37 +01005289 xdg_surface_add_listener(window->xdg_surface,
5290 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005291
5292 window->xdg_toplevel =
ant8mef99fac22018-11-28 22:46:37 +01005293 xdg_surface_get_toplevel(window->xdg_surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005294 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5295
ant8mef99fac22018-11-28 22:46:37 +01005296 xdg_toplevel_add_listener(window->xdg_toplevel,
5297 &xdg_toplevel_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005298
5299 window_inhibit_redraw(window);
5300
5301 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005302 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005303
5304 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005305}
5306
5307struct window *
5308window_create_custom(struct display *display)
5309{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005310 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005311}
5312
Jasper St. Pierre53686042013-12-09 15:26:25 -05005313void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005314window_set_parent(struct window *window,
5315 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005316{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005317 window->parent = parent_window;
5318 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005319}
5320
5321struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005322window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005323{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005324 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005325}
5326
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005327static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005328menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005329{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005330 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005331 int next;
5332
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005333 frame_interior(menu->frame, &x, &y, &width, &height);
5334 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005335 if (menu->current != next) {
5336 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005337 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005338 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005339}
5340
5341static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005342menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005343 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005344 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005345{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005346 struct menu *menu = data;
5347
5348 if (widget == menu->widget)
5349 menu_set_item(data, y);
5350
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005351 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005352}
5353
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005354static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005355menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005356 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005357{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005358 struct menu *menu = data;
5359
5360 if (widget == menu->widget)
5361 menu_set_item(data, y);
5362
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005363 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005364}
5365
5366static void
5367menu_leave_handler(struct widget *widget, struct input *input, void *data)
5368{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005369 struct menu *menu = data;
5370
5371 if (widget == menu->widget)
5372 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005373}
5374
5375static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005376menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005377 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005378 uint32_t button, enum wl_pointer_button_state state,
5379 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005380
5381{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005382 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005383
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005384 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5385 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005386 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005387 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005388 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005389 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005390 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005391 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005392 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005393 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005394}
5395
5396static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005397menu_touch_up_handler(struct widget *widget,
5398 struct input *input,
5399 uint32_t serial,
5400 uint32_t time,
5401 int32_t id,
5402 void *data)
5403{
5404 struct menu *menu = data;
5405
5406 input_ungrab(input);
5407 menu_destroy(menu);
5408}
5409
5410static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005411menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005412{
5413 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005414 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005415 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005416
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005417 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005418
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005419 frame_repaint(menu->frame, cr);
5420 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005421
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005422 theme_set_background_source(menu->window->display->theme,
5423 cr, THEME_FRAME_ACTIVE);
5424 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005425 cairo_fill(cr);
5426
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005427 cairo_select_font_face(cr, "sans",
5428 CAIRO_FONT_SLANT_NORMAL,
5429 CAIRO_FONT_WEIGHT_NORMAL);
5430 cairo_set_font_size(cr, 12);
5431
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005432 for (i = 0; i < menu->count; i++) {
5433 if (i == menu->current) {
5434 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005435 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005436 cairo_fill(cr);
5437 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005438 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005439 cairo_show_text(cr, menu->entries[i]);
5440 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005441 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5442 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005443 cairo_show_text(cr, menu->entries[i]);
5444 }
5445 }
5446
5447 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005448}
5449
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005450static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005451xdg_popup_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01005452 struct xdg_popup *xdg_popup,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005453 int32_t x,
5454 int32_t y,
5455 int32_t width,
5456 int32_t height)
5457{
5458}
5459
5460static void
ant8mef99fac22018-11-28 22:46:37 +01005461xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005462{
5463 struct window *window = data;
5464 struct menu *menu = window->main_surface->widget->user_data;
5465
5466 input_ungrab(menu->input);
5467 menu_destroy(menu);
5468}
5469
ant8mef99fac22018-11-28 22:46:37 +01005470static const struct xdg_popup_listener xdg_popup_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005471 xdg_popup_handle_configure,
5472 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005473};
5474
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005475static struct menu *
5476create_menu(struct display *display,
5477 struct input *input, uint32_t time,
5478 menu_func_t func, const char **entries, int count,
5479 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005480{
5481 struct window *window;
5482 struct menu *menu;
5483
5484 menu = malloc(sizeof *menu);
5485 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005486 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005487
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005488 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005489 if (!window) {
5490 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005491 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005492 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005493
5494 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005495 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005496 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005497 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005498 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005499 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005500 menu->entries = entries;
5501 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005502 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005503 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005504 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005505 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005506 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005507
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005508 input_ungrab(input);
5509
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005510 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005511 widget_set_enter_handler(menu->widget, menu_enter_handler);
5512 widget_set_leave_handler(menu->widget, menu_leave_handler);
5513 widget_set_motion_handler(menu->widget, menu_motion_handler);
5514 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005515 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005516
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005517 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005518 frame_resize_inside(menu->frame, 200, count * 20);
5519 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5520 window_schedule_resize(window, frame_width(menu->frame),
5521 frame_height(menu->frame));
5522
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005523 return menu;
5524}
5525
ant8mef99fac22018-11-28 22:46:37 +01005526static struct xdg_positioner *
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005527create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005528 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005529{
ant8mef99fac22018-11-28 22:46:37 +01005530 struct xdg_positioner *positioner;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005531
ant8mef99fac22018-11-28 22:46:37 +01005532 positioner = xdg_wm_base_create_positioner(display->xdg_shell);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005533 fail_on_null(positioner, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005534 xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1);
5535 xdg_positioner_set_size(positioner, w, h);
5536 xdg_positioner_set_anchor(positioner,
5537 XDG_POSITIONER_ANCHOR_TOP_LEFT);
5538 xdg_positioner_set_gravity(positioner,
5539 XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005540
5541 return positioner;
5542}
5543
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005544void
5545window_show_menu(struct display *display,
5546 struct input *input, uint32_t time, struct window *parent,
5547 int32_t x, int32_t y,
5548 menu_func_t func, const char **entries, int count)
5549{
5550 struct menu *menu;
5551 struct window *window;
5552 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005553 struct rectangle parent_geometry;
ant8mef99fac22018-11-28 22:46:37 +01005554 struct xdg_positioner *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005555
5556 menu = create_menu(display, input, time, func, entries, count, parent);
5557
5558 if (menu == NULL)
5559 return;
5560
5561 window = menu->window;
5562
5563 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5564 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5565
5566 window->x = x;
5567 window->y = y;
5568
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005569 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005570 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005571
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005572 if (!display->xdg_shell)
5573 return;
5574
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005575 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005576 xdg_wm_base_get_xdg_surface(display->xdg_shell,
5577 window->main_surface->surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005578 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005579
ant8mef99fac22018-11-28 22:46:37 +01005580 xdg_surface_add_listener(window->xdg_surface,
5581 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005582
5583 positioner = create_simple_positioner(display,
5584 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005585 window->y - (iy + parent_geometry.y),
5586 frame_width(menu->frame),
5587 frame_height(menu->frame));
ant8mef99fac22018-11-28 22:46:37 +01005588 window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
5589 parent->xdg_surface,
5590 positioner);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005591 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005592 xdg_positioner_destroy(positioner);
5593 xdg_popup_grab(window->xdg_popup, input->seat,
5594 display_get_serial(window->display));
5595 xdg_popup_add_listener(window->xdg_popup,
5596 &xdg_popup_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005597
5598 window_inhibit_redraw(window);
5599
5600 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005601}
5602
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005603void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005604window_set_buffer_type(struct window *window, enum window_buffer_type type)
5605{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005606 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005607}
5608
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005609enum window_buffer_type
5610window_get_buffer_type(struct window *window)
5611{
5612 return window->main_surface->buffer_type;
5613}
5614
Pekka Paalanen35e82632013-04-25 13:57:48 +03005615struct widget *
5616window_add_subsurface(struct window *window, void *data,
5617 enum subsurface_mode default_mode)
5618{
5619 struct widget *widget;
5620 struct surface *surface;
5621 struct wl_surface *parent;
5622 struct wl_subcompositor *subcompo = window->display->subcompositor;
5623
Pekka Paalanen35e82632013-04-25 13:57:48 +03005624 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005625 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005626 widget = widget_create(window, surface, data);
5627 wl_list_init(&widget->link);
5628 surface->widget = widget;
5629
5630 parent = window->main_surface->surface;
5631 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5632 surface->surface,
5633 parent);
5634 surface->synchronized = 1;
5635
5636 switch (default_mode) {
5637 case SUBSURFACE_SYNCHRONIZED:
5638 surface->synchronized_default = 1;
5639 break;
5640 case SUBSURFACE_DESYNCHRONIZED:
5641 surface->synchronized_default = 0;
5642 break;
5643 default:
5644 assert(!"bad enum subsurface_mode");
5645 }
5646
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005647 window->resize_needed = 1;
5648 window_schedule_redraw(window);
5649
Pekka Paalanen35e82632013-04-25 13:57:48 +03005650 return widget;
5651}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005652
5653static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005654display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005655 struct wl_output *wl_output,
5656 int x, int y,
5657 int physical_width,
5658 int physical_height,
5659 int subpixel,
5660 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005661 const char *model,
5662 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005663{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005664 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005665
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005666 output->allocation.x = x;
5667 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005668 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005669
5670 if (output->make)
5671 free(output->make);
5672 output->make = strdup(make);
5673
5674 if (output->model)
5675 free(output->model);
5676 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005677}
5678
5679static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005680display_handle_done(void *data,
5681 struct wl_output *wl_output)
5682{
5683}
5684
5685static void
5686display_handle_scale(void *data,
5687 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005688 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005689{
5690 struct output *output = data;
5691
5692 output->scale = scale;
5693}
5694
5695static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005696display_handle_mode(void *data,
5697 struct wl_output *wl_output,
5698 uint32_t flags,
5699 int width,
5700 int height,
5701 int refresh)
5702{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005703 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005704 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005705
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005706 if (flags & WL_OUTPUT_MODE_CURRENT) {
5707 output->allocation.width = width;
5708 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005709 if (display->output_configure_handler)
5710 (*display->output_configure_handler)(
5711 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005712 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005713}
5714
5715static const struct wl_output_listener output_listener = {
5716 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005717 display_handle_mode,
5718 display_handle_done,
5719 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005720};
5721
5722static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005723display_add_output(struct display *d, uint32_t id)
5724{
5725 struct output *output;
5726
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005727 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005728 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005729 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005730 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005731 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005732 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005733 wl_list_insert(d->output_list.prev, &output->link);
5734
5735 wl_output_add_listener(output->output, &output_listener, output);
5736}
5737
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005738static void
5739output_destroy(struct output *output)
5740{
5741 if (output->destroy_handler)
5742 (*output->destroy_handler)(output, output->user_data);
5743
5744 wl_output_destroy(output->output);
5745 wl_list_remove(&output->link);
Lujin Wang928d3a02019-08-12 12:22:23 -07005746 free(output->make);
5747 free(output->model);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005748 free(output);
5749}
5750
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005751static void
5752display_destroy_output(struct display *d, uint32_t id)
5753{
5754 struct output *output;
5755
5756 wl_list_for_each(output, &d->output_list, link) {
5757 if (output->server_output_id == id) {
5758 output_destroy(output);
5759 break;
5760 }
5761 }
5762}
5763
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005764void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005765display_set_global_handler(struct display *display,
5766 display_global_handler_t handler)
5767{
5768 struct global *global;
5769
5770 display->global_handler = handler;
5771 if (!handler)
5772 return;
5773
5774 wl_list_for_each(global, &display->global_list, link)
5775 display->global_handler(display,
5776 global->name, global->interface,
5777 global->version, display->user_data);
5778}
5779
5780void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005781display_set_global_handler_remove(struct display *display,
5782 display_global_handler_t remove_handler)
5783{
5784 display->global_handler_remove = remove_handler;
5785 if (!remove_handler)
5786 return;
5787}
5788
5789void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005790display_set_output_configure_handler(struct display *display,
5791 display_output_handler_t handler)
5792{
5793 struct output *output;
5794
5795 display->output_configure_handler = handler;
5796 if (!handler)
5797 return;
5798
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005799 wl_list_for_each(output, &display->output_list, link) {
5800 if (output->allocation.width == 0 &&
5801 output->allocation.height == 0)
5802 continue;
5803
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005804 (*display->output_configure_handler)(output,
5805 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005806 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005807}
5808
5809void
5810output_set_user_data(struct output *output, void *data)
5811{
5812 output->user_data = data;
5813}
5814
5815void *
5816output_get_user_data(struct output *output)
5817{
5818 return output->user_data;
5819}
5820
5821void
5822output_set_destroy_handler(struct output *output,
5823 display_output_handler_t handler)
5824{
5825 output->destroy_handler = handler;
5826 /* FIXME: implement this, once we have way to remove outputs */
5827}
5828
5829void
Scott Moreau4e072362012-09-29 02:03:11 -06005830output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005831{
Scott Moreau4e072362012-09-29 02:03:11 -06005832 struct rectangle allocation = output->allocation;
5833
5834 switch (output->transform) {
5835 case WL_OUTPUT_TRANSFORM_90:
5836 case WL_OUTPUT_TRANSFORM_270:
5837 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5838 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5839 /* Swap width and height */
5840 allocation.width = output->allocation.height;
5841 allocation.height = output->allocation.width;
5842 break;
5843 }
5844
5845 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005846}
5847
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005848struct wl_output *
5849output_get_wl_output(struct output *output)
5850{
5851 return output->output;
5852}
5853
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005854enum wl_output_transform
5855output_get_transform(struct output *output)
5856{
5857 return output->transform;
5858}
5859
Alexander Larssonafd319a2013-05-22 14:41:27 +02005860uint32_t
5861output_get_scale(struct output *output)
5862{
5863 return output->scale;
5864}
5865
Jason Ekstrand738715d2014-04-02 19:53:50 -05005866const char *
5867output_get_make(struct output *output)
5868{
5869 return output->make;
5870}
5871
5872const char *
5873output_get_model(struct output *output)
5874{
5875 return output->model;
5876}
5877
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005878static void
Daniel Stone97f68542012-05-30 16:32:01 +01005879fini_xkb(struct input *input)
5880{
Pekka Paalanen2af436b2021-05-25 14:10:33 +03005881 xkb_compose_state_unref(input->xkb.compose_state);
5882 xkb_compose_table_unref(input->xkb.compose_table);
Daniel Stone97f68542012-05-30 16:32:01 +01005883 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005884 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005885}
5886
5887static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005888display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005889{
5890 struct input *input;
Sebastian Wickac1b92d2019-11-01 02:27:55 +01005891 int seat_version = MIN(display_seat_version, 7);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005892
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005893 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005894 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005895 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005896 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005897 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005898 input->pointer_focus = NULL;
5899 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005900 input->seat_version = seat_version;
5901
Rusty Lynch041815a2013-08-08 21:20:38 -07005902 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005903 wl_list_insert(d->input_list.prev, &input->link);
5904
Daniel Stone37816df2012-05-16 18:45:18 +01005905 wl_seat_add_listener(input->seat, &seat_listener, input);
5906 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005907
Jason Ekstranda669bd52014-04-02 19:53:51 -05005908 if (d->data_device_manager) {
5909 input->data_device =
5910 wl_data_device_manager_get_data_device(d->data_device_manager,
5911 input->seat);
5912 wl_data_device_add_listener(input->data_device,
5913 &data_device_listener,
5914 input);
5915 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005916
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005917 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5918 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005919
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005920 set_repeat_info(input, 40, 400);
5921 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5922 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005923}
5924
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005925static void
Harish Krupoee4c7a22019-04-19 01:53:27 +05305926display_add_data_device(struct display *d, uint32_t id, int ddm_version)
5927{
5928 struct input *input;
5929
5930 d->data_device_manager_version = MIN(ddm_version, 3);
5931 d->data_device_manager =
5932 wl_registry_bind(d->registry, id,
5933 &wl_data_device_manager_interface,
5934 d->data_device_manager_version);
5935
5936 wl_list_for_each(input, &d->input_list, link) {
5937 if (!input->data_device) {
5938 input->data_device =
5939 wl_data_device_manager_get_data_device(d->data_device_manager,
5940 input->seat);
5941 wl_data_device_add_listener(input->data_device,
5942 &data_device_listener,
5943 input);
5944 }
5945 }
5946}
5947
5948static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005949input_destroy(struct input *input)
5950{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005951 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005952 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005953
5954 if (input->drag_offer)
5955 data_offer_destroy(input->drag_offer);
5956
5957 if (input->selection_offer)
5958 data_offer_destroy(input->selection_offer);
5959
kabeer khan6ce67ec2014-10-20 11:55:29 +05305960 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005961 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305962 wl_data_device_release(input->data_device);
5963 else
5964 wl_data_device_destroy(input->data_device);
5965 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005966 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005967 if (input->touch)
5968 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005969 if (input->pointer)
5970 wl_pointer_release(input->pointer);
5971 if (input->keyboard)
5972 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005973 } else {
5974 if (input->touch)
5975 wl_touch_destroy(input->touch);
5976 if (input->pointer)
5977 wl_pointer_destroy(input->pointer);
5978 if (input->keyboard)
5979 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005980 }
5981
Daniel Stone97f68542012-05-30 16:32:01 +01005982 fini_xkb(input);
5983
Simon Ser992ee042021-02-07 11:32:46 +01005984 if (input->pointer_surface)
5985 wl_surface_destroy(input->pointer_surface);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005986
Pekka Paalanene1207c72011-12-16 12:02:09 +02005987 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005988 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005989 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005990 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005991 free(input);
5992}
5993
5994static void
ant8mef99fac22018-11-28 22:46:37 +01005995xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005996{
ant8mef99fac22018-11-28 22:46:37 +01005997 xdg_wm_base_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005998}
5999
ant8mef99fac22018-11-28 22:46:37 +01006000static const struct xdg_wm_base_listener wm_base_listener = {
6001 xdg_wm_base_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08006002};
6003
6004static void
Pekka Paalanen1e08a812021-05-21 17:29:42 +03006005global_destroy(struct display *disp, struct global *g)
6006{
6007 if (disp->global_handler_remove) {
6008 disp->global_handler_remove(disp, g->name, g->interface,
6009 g->version, disp->user_data);
6010 }
6011
6012 wl_list_remove(&g->link);
6013 free(g->interface);
6014 free(g);
6015}
6016
6017static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006018registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
6019 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006020{
6021 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006022 struct global *global;
6023
Brian Lovinbc919262013-08-07 15:34:59 -07006024 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006025 global->name = id;
6026 global->interface = strdup(interface);
6027 global->version = version;
6028 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006029
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006030 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006031 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05006032 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006033 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006034 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01006035 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05006036 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08006037 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
6038 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
6039 d->relative_pointer_manager =
6040 wl_registry_bind(registry, id,
6041 &zwp_relative_pointer_manager_v1_interface,
6042 1);
6043 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
6044 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
6045 d->pointer_constraints =
6046 wl_registry_bind(registry, id,
6047 &zwp_pointer_constraints_v1_interface,
6048 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006049 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006050 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006051 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Harish Krupoee4c7a22019-04-19 01:53:27 +05306052 display_add_data_device(d, id, version);
ant8mef99fac22018-11-28 22:46:37 +01006053 } else if (strcmp(interface, "xdg_wm_base") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006054 d->xdg_shell = wl_registry_bind(registry, id,
ant8mef99fac22018-11-28 22:46:37 +01006055 &xdg_wm_base_interface, 1);
6056 xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06006057 } else if (strcmp(interface, "text_cursor_position") == 0) {
6058 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006059 wl_registry_bind(registry, id,
6060 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006061 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6062 d->subcompositor =
6063 wl_registry_bind(registry, id,
6064 &wl_subcompositor_interface, 1);
Harish Krupo7bcbab12018-12-11 13:45:43 +05306065 } else if (!strcmp(interface, "wp_viewporter")) {
6066 d->viewporter =
6067 wl_registry_bind(registry, id,
6068 &wp_viewporter_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006069 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006070
6071 if (d->global_handler)
6072 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006073}
6074
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006075static void
6076registry_handle_global_remove(void *data, struct wl_registry *registry,
6077 uint32_t name)
6078{
6079 struct display *d = data;
6080 struct global *global;
6081 struct global *tmp;
6082
6083 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6084 if (global->name != name)
6085 continue;
6086
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006087 if (strcmp(global->interface, "wl_output") == 0)
6088 display_destroy_output(d, name);
6089
Pekka Paalanen1e08a812021-05-21 17:29:42 +03006090 global_destroy(d, global);
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006091 }
6092}
6093
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006094void *
6095display_bind(struct display *display, uint32_t name,
6096 const struct wl_interface *interface, uint32_t version)
6097{
6098 return wl_registry_bind(display->registry, name, interface, version);
6099}
6100
6101static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006102 registry_handle_global,
6103 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006104};
6105
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006106#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006107static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006108init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006109{
6110 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006111 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006112
Rob Clark6396ed32012-03-11 19:48:41 -05006113#ifdef USE_CAIRO_GLESV2
6114# define GL_BIT EGL_OPENGL_ES2_BIT
6115#else
6116# define GL_BIT EGL_OPENGL_BIT
6117#endif
6118
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006119 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006120 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006121 EGL_RED_SIZE, 1,
6122 EGL_GREEN_SIZE, 1,
6123 EGL_BLUE_SIZE, 1,
6124 EGL_ALPHA_SIZE, 1,
6125 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006126 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006127 EGL_NONE
6128 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006129
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006130#ifdef USE_CAIRO_GLESV2
6131 static const EGLint context_attribs[] = {
6132 EGL_CONTEXT_CLIENT_VERSION, 2,
6133 EGL_NONE
6134 };
6135 EGLint api = EGL_OPENGL_ES_API;
6136#else
6137 EGLint *context_attribs = NULL;
6138 EGLint api = EGL_OPENGL_API;
6139#endif
6140
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006141 d->dpy =
6142 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6143 d->display, NULL);
6144
Yuval Fledel45568f62010-12-06 09:18:12 -05006145 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006146 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006147 return -1;
6148 }
6149
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006150 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006151 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006152 return -1;
6153 }
6154
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006155 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6156 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006157 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006158 return -1;
6159 }
6160
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006161 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006162 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006163 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006164 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006165 return -1;
6166 }
6167
Benjamin Franzke0c991632011-09-27 21:57:31 +02006168 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6169 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006170 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006171 return -1;
6172 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006173
6174 return 0;
6175}
6176
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006177static void
6178fini_egl(struct display *display)
6179{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006180 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006181
6182 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6183 EGL_NO_CONTEXT);
6184
6185 eglTerminate(display->dpy);
6186 eglReleaseThread();
6187}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006188#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006189
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006190static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006191init_dummy_surface(struct display *display)
6192{
6193 int len;
6194 void *data;
6195
6196 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006197 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006198 display->dummy_surface =
6199 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6200 1, 1, len);
6201 display->dummy_surface_data = data;
6202}
6203
6204static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006205handle_display_data(struct task *task, uint32_t events)
6206{
6207 struct display *display =
6208 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006209 struct epoll_event ep;
6210 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006211
6212 display->display_fd_events = events;
6213
6214 if (events & EPOLLERR || events & EPOLLHUP) {
6215 display_exit(display);
6216 return;
6217 }
6218
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006219 if (events & EPOLLIN) {
6220 ret = wl_display_dispatch(display->display);
6221 if (ret == -1) {
6222 display_exit(display);
6223 return;
6224 }
6225 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006226
6227 if (events & EPOLLOUT) {
6228 ret = wl_display_flush(display->display);
6229 if (ret == 0) {
6230 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6231 ep.data.ptr = &display->display_task;
6232 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6233 display->display_fd, &ep);
6234 } else if (ret == -1 && errno != EAGAIN) {
6235 display_exit(display);
6236 return;
6237 }
6238 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006239}
6240
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006241static void
6242log_handler(const char *format, va_list args)
6243{
6244 vfprintf(stderr, format, args);
6245}
6246
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006247struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006248display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006249{
6250 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006251
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006252 wl_log_set_handler_client(log_handler);
6253
Peter Huttererf3d62272013-08-08 11:57:05 +10006254 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006255 if (d == NULL)
6256 return NULL;
6257
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006258 wl_list_init(&d->window_list);
6259 wl_list_init(&d->deferred_list);
6260 wl_list_init(&d->input_list);
6261 wl_list_init(&d->output_list);
6262 wl_list_init(&d->global_list);
6263
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006264 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006265 if (d->display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02006266 fprintf(stderr, "failed to connect to Wayland display: %s\n",
6267 strerror(errno));
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006268 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006269 return NULL;
6270 }
6271
Peter Hutterera2086bb2020-05-13 15:24:27 +10006272 d->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Rob Bradford5ab9c752013-07-26 16:29:43 +01006273 if (d->xkb_context == NULL) {
6274 fprintf(stderr, "Failed to create XKB context\n");
6275 free(d);
6276 return NULL;
6277 }
6278
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006279 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006280 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006281 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006282 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6283 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006284
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006285 d->registry = wl_display_get_registry(d->display);
6286 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006287
Marek Chalupaa519d062014-12-05 13:49:40 +01006288 if (wl_display_roundtrip(d->display) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006289 fprintf(stderr, "Failed to process Wayland connection: %s\n",
6290 strerror(errno));
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006291 display_destroy(d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006292 return NULL;
6293 }
6294
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006295#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006296 if (init_egl(d) < 0)
6297 fprintf(stderr, "EGL does not seem to work, "
6298 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006299#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006300
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006301 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006302
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006303 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006304
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006305 init_dummy_surface(d);
6306
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006307 return d;
6308}
6309
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006310static void
6311display_destroy_outputs(struct display *display)
6312{
6313 struct output *tmp;
6314 struct output *output;
6315
6316 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6317 output_destroy(output);
6318}
6319
Pekka Paalanene1207c72011-12-16 12:02:09 +02006320static void
6321display_destroy_inputs(struct display *display)
6322{
6323 struct input *tmp;
6324 struct input *input;
6325
6326 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6327 input_destroy(input);
6328}
6329
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006330void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006331display_destroy(struct display *display)
6332{
Pekka Paalanenc8c53ba2021-05-21 17:31:11 +03006333 struct global *global, *tmp;
6334
Pekka Paalanenc2052982011-12-16 11:41:32 +02006335 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006336 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6337 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006338
6339 if (!wl_list_empty(&display->deferred_list))
6340 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6341
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006342 if (display->dummy_surface)
6343 cairo_surface_destroy(display->dummy_surface);
6344 if (display->dummy_surface_data)
6345 free(display->dummy_surface_data);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006346
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006347 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006348 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006349
Pekka Paalanenc8c53ba2021-05-21 17:31:11 +03006350 wl_list_for_each_safe(global, tmp, &display->global_list, link)
6351 global_destroy(display, global);
6352
Daniel Stone97f68542012-05-30 16:32:01 +01006353 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006354
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006355 if (display->theme)
6356 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006357 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006358
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006359#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006360 if (display->argb_device)
6361 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006362#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006363
Pekka Paalanenc8c53ba2021-05-21 17:31:11 +03006364 if (display->relative_pointer_manager)
6365 zwp_relative_pointer_manager_v1_destroy(display->relative_pointer_manager);
6366
6367 if (display->pointer_constraints)
6368 zwp_pointer_constraints_v1_destroy(display->pointer_constraints);
6369
6370 if (display->viewporter)
6371 wp_viewporter_destroy(display->viewporter);
6372
Pekka Paalanen35e82632013-04-25 13:57:48 +03006373 if (display->subcompositor)
6374 wl_subcompositor_destroy(display->subcompositor);
6375
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006376 if (display->xdg_shell)
ant8mef99fac22018-11-28 22:46:37 +01006377 xdg_wm_base_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006378
6379 if (display->shm)
6380 wl_shm_destroy(display->shm);
6381
6382 if (display->data_device_manager)
6383 wl_data_device_manager_destroy(display->data_device_manager);
6384
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006385 if (display->compositor)
6386 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006387 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006388
6389 close(display->epoll_fd);
6390
U. Artie Eoff44874d92012-10-02 21:12:35 -07006391 if (!(display->display_fd_events & EPOLLERR) &&
6392 !(display->display_fd_events & EPOLLHUP))
6393 wl_display_flush(display->display);
6394
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006395 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006396 free(display);
6397}
6398
6399void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006400display_set_user_data(struct display *display, void *data)
6401{
6402 display->user_data = data;
6403}
6404
6405void *
6406display_get_user_data(struct display *display)
6407{
6408 return display->user_data;
6409}
6410
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006411struct wl_display *
6412display_get_display(struct display *display)
6413{
6414 return display->display;
6415}
6416
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006417int
6418display_has_subcompositor(struct display *display)
6419{
6420 if (display->subcompositor)
6421 return 1;
6422
6423 wl_display_roundtrip(display->display);
6424
6425 return display->subcompositor != NULL;
6426}
6427
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006428cairo_device_t *
6429display_get_cairo_device(struct display *display)
6430{
6431 return display->argb_device;
6432}
6433
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006434struct output *
6435display_get_output(struct display *display)
6436{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006437 if (wl_list_empty(&display->output_list))
6438 return NULL;
6439
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006440 return container_of(display->output_list.next, struct output, link);
6441}
6442
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006443struct wl_compositor *
6444display_get_compositor(struct display *display)
6445{
6446 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006447}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006448
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006449uint32_t
6450display_get_serial(struct display *display)
6451{
6452 return display->serial;
6453}
6454
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006455EGLDisplay
6456display_get_egl_display(struct display *d)
6457{
6458 return d->dpy;
6459}
6460
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006461struct wl_data_source *
6462display_create_data_source(struct display *display)
6463{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006464 if (display->data_device_manager)
6465 return wl_data_device_manager_create_data_source(display->data_device_manager);
6466 else
6467 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006468}
6469
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006470EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006471display_get_argb_egl_config(struct display *d)
6472{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006473 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006474}
6475
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006476int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006477display_acquire_window_surface(struct display *display,
6478 struct window *window,
6479 EGLContext ctx)
6480{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006481 struct surface *surface = window->main_surface;
6482
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006483 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006484 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006485
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006486 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006487 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006488}
6489
6490void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006491display_release_window_surface(struct display *display,
6492 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006493{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006494 struct surface *surface = window->main_surface;
6495
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006496 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006497 return;
6498
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006499 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006500}
6501
6502void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006503display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006504{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006505 wl_list_insert(&display->deferred_list, &task->link);
6506}
6507
6508void
6509display_watch_fd(struct display *display,
6510 int fd, uint32_t events, struct task *task)
6511{
6512 struct epoll_event ep;
6513
6514 ep.events = events;
6515 ep.data.ptr = task;
6516 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6517}
6518
6519void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006520display_unwatch_fd(struct display *display, int fd)
6521{
6522 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6523}
6524
6525void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006526display_run(struct display *display)
6527{
6528 struct task *task;
6529 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006530 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006531
Pekka Paalanen826d7952011-12-15 10:14:07 +02006532 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006533 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006534 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006535 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006536 struct task, link);
6537 wl_list_remove(&task->link);
6538 task->run(task, 0);
6539 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006540
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006541 wl_display_dispatch_pending(display->display);
6542
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006543 if (!display->running)
6544 break;
6545
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006546 ret = wl_display_flush(display->display);
6547 if (ret < 0 && errno == EAGAIN) {
6548 ep[0].events =
6549 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6550 ep[0].data.ptr = &display->display_task;
6551
6552 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6553 display->display_fd, &ep[0]);
6554 } else if (ret < 0) {
6555 break;
6556 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006557
6558 count = epoll_wait(display->epoll_fd,
6559 ep, ARRAY_LENGTH(ep), -1);
6560 for (i = 0; i < count; i++) {
6561 task = ep[i].data.ptr;
6562 task->run(task, ep[i].events);
6563 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006564 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006565}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006566
6567void
6568display_exit(struct display *display)
6569{
6570 display->running = 0;
6571}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006572
Carlos Garnacho9c931792016-01-18 23:52:12 +01006573int
6574display_get_data_device_manager_version(struct display *display)
6575{
6576 return display->data_device_manager_version;
6577}
6578
Jan Arne Petersencd997062012-11-18 19:06:44 +01006579void
6580keysym_modifiers_add(struct wl_array *modifiers_map,
6581 const char *name)
6582{
6583 size_t len = strlen(name) + 1;
6584 char *p;
6585
6586 p = wl_array_add(modifiers_map, len);
6587
6588 if (p == NULL)
6589 return;
6590
6591 strncpy(p, name, len);
6592}
6593
6594static xkb_mod_index_t
6595keysym_modifiers_get_index(struct wl_array *modifiers_map,
6596 const char *name)
6597{
6598 xkb_mod_index_t index = 0;
6599 char *p = modifiers_map->data;
6600
6601 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6602 if (strcmp(p, name) == 0)
6603 return index;
6604
6605 index++;
6606 p += strlen(p) + 1;
6607 }
6608
6609 return XKB_MOD_INVALID;
6610}
6611
6612xkb_mod_mask_t
6613keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6614 const char *name)
6615{
6616 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6617
6618 if (index == XKB_MOD_INVALID)
6619 return XKB_MOD_INVALID;
6620
6621 return 1 << index;
6622}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006623
6624static void
6625toytimer_fire(struct task *tsk, uint32_t events)
6626{
6627 uint64_t e;
6628 struct toytimer *tt;
6629
6630 tt = container_of(tsk, struct toytimer, tsk);
6631
6632 if (events != EPOLLIN)
6633 fprintf(stderr, "unexpected timerfd events %x\n", events);
6634
6635 if (!(events & EPOLLIN))
6636 return;
6637
6638 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6639 /* If we change the timer between the fd becoming
6640 * readable and getting here, there'll be nothing to
6641 * read and we get EAGAIN. */
6642 if (errno != EAGAIN)
Antonio Borneo39578632019-04-26 23:57:31 +02006643 fprintf(stderr, "timer read failed: %s\n",
6644 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006645 return;
6646 }
6647
6648 tt->callback(tt);
6649}
6650
6651void
6652toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6653 toytimer_cb callback)
6654{
6655 memset(tt, 0, sizeof *tt);
6656
6657 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6658 if (tt->fd == -1) {
Antonio Borneo39578632019-04-26 23:57:31 +02006659 fprintf(stderr, "creating timer failed: %s\n",
6660 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006661 abort();
6662 }
6663
6664 tt->display = display;
6665 tt->callback = callback;
6666 tt->tsk.run = toytimer_fire;
6667 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6668}
6669
6670void
6671toytimer_fini(struct toytimer *tt)
6672{
6673 display_unwatch_fd(tt->display, tt->fd);
6674 close(tt->fd);
6675 tt->fd = -1;
6676}
6677
6678void
6679toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6680{
6681 int ret;
6682
6683 ret = timerfd_settime(tt->fd, 0, its, NULL);
6684 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006685 fprintf(stderr, "timer setup failed: %s\n", strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006686 abort();
6687 }
6688}
6689
6690#define USEC_PER_SEC 1000000
6691
6692void
6693toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6694{
6695 struct itimerspec its;
6696
6697 its.it_interval.tv_sec = 0;
6698 its.it_interval.tv_nsec = 0;
6699 its.it_value.tv_sec = usec / USEC_PER_SEC;
6700 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6701 toytimer_arm(tt, &its);
6702}
6703
6704void
6705toytimer_disarm(struct toytimer *tt)
6706{
6707 struct itimerspec its = {};
6708
6709 toytimer_arm(tt, &its);
6710}