blob: 81fc7af8efbba5b171128a96bcaf4a4a2c3e1435 [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;
Marius Vladb3544c22021-09-09 13:52:18 +0300233 char *appid;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200234 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400235 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500236 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200237 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500238 int x, y;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800239 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400240 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300241 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400242 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400243 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500244 int custom;
245 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400246
Pekka Paalanen99436862012-11-19 17:15:59 +0200247 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400248
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500249 int fullscreen;
250 int maximized;
251
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500252 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500253 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400254 window_data_handler_t data_handler;
255 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500256 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400257 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200258 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700259 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400260
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800261 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
262
Pekka Paalanen4e373742013-02-13 16:17:13 +0200263 struct surface *main_surface;
ant8mef99fac22018-11-28 22:46:37 +0100264 struct xdg_surface *xdg_surface;
265 struct xdg_toplevel *xdg_toplevel;
266 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300267
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700268 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800269 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500270
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500271 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500272
Pekka Paalanen35e82632013-04-25 13:57:48 +0300273 /* struct surface::link, contains also main_surface */
274 struct wl_list subsurface_list;
275
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800276 struct zwp_relative_pointer_v1 *relative_pointer;
277 struct zwp_locked_pointer_v1 *locked_pointer;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800278 bool pointer_locked;
279 locked_pointer_locked_handler_t pointer_locked_handler;
280 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
281 confined_pointer_confined_handler_t pointer_confined_handler;
282 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
283
284 struct zwp_confined_pointer_v1 *confined_pointer;
285 struct widget *confined_widget;
286 bool confined;
287
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500288 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400289 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500290};
291
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500292struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500293 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200294 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300295 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500296 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400297 struct wl_list link;
298 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500299 widget_resize_handler_t resize_handler;
300 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500301 widget_enter_handler_t enter_handler;
302 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500303 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500304 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 widget_touch_down_handler_t touch_down_handler;
306 widget_touch_up_handler_t touch_up_handler;
307 widget_touch_motion_handler_t touch_motion_handler;
308 widget_touch_frame_handler_t touch_frame_handler;
309 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200310 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000311 widget_pointer_frame_handler_t pointer_frame_handler;
312 widget_axis_source_handler_t axis_source_handler;
313 widget_axis_stop_handler_t axis_stop_handler;
314 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400315 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500316 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300317 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500318 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000319 /* If this is set to false then no cairo surface will be
320 * created before redrawing the surface. This is useful if the
321 * redraw handler is going to do completely custom rendering
322 * such as using EGL directly */
323 int use_cairo;
Harish Krupo7bcbab12018-12-11 13:45:43 +0530324 int viewport_dest_width;
325 int viewport_dest_height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400326};
327
Rusty Lynch041815a2013-08-08 21:20:38 -0700328struct touch_point {
329 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800330 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700331 struct widget *widget;
332 struct wl_list link;
333};
334
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400335struct input {
336 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100337 struct wl_seat *seat;
338 struct wl_pointer *pointer;
339 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700340 struct wl_touch *touch;
341 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400342 struct window *pointer_focus;
343 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700344 struct window *touch_focus;
Dima Ryazanov13bdf252018-11-14 22:17:42 -0800345 struct window *locked_window;
346 struct window *confined_window;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300347 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300348 uint32_t cursor_anim_start;
349 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500350 uint32_t cursor_timer_start;
351 uint32_t cursor_anim_current;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200352 struct toytimer cursor_timer;
Derek Foreman118a4292015-04-22 17:23:35 -0500353 bool cursor_timer_running;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300354 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400355 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400356 uint32_t pointer_enter_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;
leng.fang32af9fc2024-06-13 11:22:15 +0800395
396 bool pointer_hidden;
397 uint32_t pointer_last_motion_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400398};
399
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500400struct output {
401 struct display *display;
402 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800403 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500404 struct rectangle allocation;
405 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600406 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200407 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500408 char *make;
409 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200410
411 display_output_handler_t destroy_handler;
412 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500413};
414
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500415struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500416 struct widget *widget;
417 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500418 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800419
420 uint32_t last_time;
421 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800422 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500423};
424
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500425struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400426 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500427 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500428 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500429 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700430 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500431 const char **entries;
432 uint32_t time;
433 int current;
434 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400435 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500436 menu_func_t func;
437};
438
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300439struct tooltip {
440 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300441 struct widget *widget;
442 char *entry;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200443 struct toytimer timer;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300444 float x, y;
445};
446
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700447struct shm_pool {
448 struct wl_shm_pool *pool;
449 size_t size;
450 size_t used;
451 void *data;
452};
453
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400454enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300455 CURSOR_DEFAULT = 100,
456 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400457};
458
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200459static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400460
Pekka Paalanen97777442013-05-22 10:20:05 +0300461/* #define DEBUG */
462
463#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300464
465static void
466debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
467__attribute__ ((format (printf, 4, 5)));
468
469static void
470debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
471{
472 va_list ap;
473 struct timeval tv;
474
475 gettimeofday(&tv, NULL);
476 fprintf(stderr, "%8ld.%03ld ",
477 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
478
479 if (proxy)
480 fprintf(stderr, "%s@%d ",
481 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
482
483 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
484 fprintf(stderr, "%s ", func);
485
486 va_start(ap, fmt);
487 vfprintf(stderr, fmt, ap);
488 va_end(ap);
489}
490
491#define DBG(fmt, ...) \
492 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
493
494#define DBG_OBJ(obj, fmt, ...) \
495 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
496
497#else
498
499#define DBG(...) do {} while (0)
500#define DBG_OBJ(...) do {} while (0)
501
502#endif
503
Alexander Larsson1818e312013-05-22 14:41:31 +0200504static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200505surface_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 +0200506{
507 int32_t tmp;
508
509 switch (buffer_transform) {
510 case WL_OUTPUT_TRANSFORM_90:
511 case WL_OUTPUT_TRANSFORM_270:
512 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
513 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
514 tmp = *width;
515 *width = *height;
516 *height = tmp;
517 break;
518 default:
519 break;
520 }
521
522 *width *= buffer_scale;
523 *height *= buffer_scale;
524}
525
526static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200527buffer_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 +0200528{
529 int32_t tmp;
530
531 switch (buffer_transform) {
532 case WL_OUTPUT_TRANSFORM_90:
533 case WL_OUTPUT_TRANSFORM_270:
534 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
535 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
536 tmp = *width;
537 *width = *height;
538 *height = tmp;
539 break;
540 default:
541 break;
542 }
543
544 *width /= buffer_scale;
545 *height /= buffer_scale;
546}
547
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500548#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400549
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200550struct egl_window_surface {
551 struct toysurface base;
552 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100553 struct display *display;
554 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200555 struct wl_egl_window *egl_window;
556 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100557};
558
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559static struct egl_window_surface *
560to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100561{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200562 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100563}
564
565static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200566egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200567 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200568 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100569{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200570 struct egl_window_surface *surface = to_egl_window_surface(base);
571
Alexander Larsson1818e312013-05-22 14:41:31 +0200572 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
573
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200574 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
575 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
576
577 return cairo_surface_reference(surface->cairo_surface);
578}
579
580static void
581egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200582 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200583 struct rectangle *server_allocation)
584{
585 struct egl_window_surface *surface = to_egl_window_surface(base);
586
587 cairo_gl_surface_swapbuffers(surface->cairo_surface);
588 wl_egl_window_get_attached_size(surface->egl_window,
589 &server_allocation->width,
590 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200591
592 buffer_to_surface_size (buffer_transform, buffer_scale,
593 &server_allocation->width,
594 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200595}
596
597static int
598egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
599{
600 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200601 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100602
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200603 device = cairo_surface_get_device(surface->cairo_surface);
604 if (!device)
605 return -1;
606
607 if (!ctx) {
608 if (device == surface->display->argb_device)
609 ctx = surface->display->argb_ctx;
610 else
611 assert(0);
612 }
613
614 cairo_device_flush(device);
615 cairo_device_acquire(device);
616 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
617 surface->egl_surface, ctx))
618 fprintf(stderr, "failed to make surface current\n");
619
620 return 0;
621}
622
623static void
624egl_window_surface_release(struct toysurface *base)
625{
626 struct egl_window_surface *surface = to_egl_window_surface(base);
627 cairo_device_t *device;
628
629 device = cairo_surface_get_device(surface->cairo_surface);
630 if (!device)
631 return;
632
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200633 if (!eglMakeCurrent(surface->display->dpy,
634 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200635 fprintf(stderr, "failed to make context current\n");
636
637 cairo_device_release(device);
638}
639
640static void
641egl_window_surface_destroy(struct toysurface *base)
642{
643 struct egl_window_surface *surface = to_egl_window_surface(base);
644 struct display *d = surface->display;
645
646 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000647 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200648 wl_egl_window_destroy(surface->egl_window);
649 surface->surface = NULL;
650
651 free(surface);
652}
653
654static struct toysurface *
655egl_window_surface_create(struct display *display,
656 struct wl_surface *wl_surface,
657 uint32_t flags,
658 struct rectangle *rectangle)
659{
660 struct egl_window_surface *surface;
661
Pekka Paalanenb3627362012-11-19 17:16:00 +0200662 if (display->dpy == EGL_NO_DISPLAY)
663 return NULL;
664
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800665 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200666 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100667 return NULL;
668
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200669 surface->base.prepare = egl_window_surface_prepare;
670 surface->base.swap = egl_window_surface_swap;
671 surface->base.acquire = egl_window_surface_acquire;
672 surface->base.release = egl_window_surface_release;
673 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100674
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200675 surface->display = display;
676 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400677
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200678 surface->egl_window = wl_egl_window_create(surface->surface,
679 rectangle->width,
680 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100681
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100682 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100683 weston_platform_create_egl_surface(display->dpy,
684 display->argb_config,
685 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100686
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200687 surface->cairo_surface =
688 cairo_gl_surface_create_for_egl(display->argb_device,
689 surface->egl_surface,
690 rectangle->width,
691 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100692
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200693 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100694}
695
Pekka Paalanenb3627362012-11-19 17:16:00 +0200696#else
697
698static struct toysurface *
699egl_window_surface_create(struct display *display,
700 struct wl_surface *wl_surface,
701 uint32_t flags,
702 struct rectangle *rectangle)
703{
704 return NULL;
705}
706
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400707#endif
708
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200709struct shm_surface_data {
710 struct wl_buffer *buffer;
711 struct shm_pool *pool;
712};
713
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400714struct wl_buffer *
715display_get_buffer_for_surface(struct display *display,
716 cairo_surface_t *surface)
717{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200718 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400719
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200720 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400721
722 return data->buffer;
723}
724
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500725static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700726shm_pool_destroy(struct shm_pool *pool);
727
728static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400729shm_surface_data_destroy(void *p)
730{
731 struct shm_surface_data *data = p;
732
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200733 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700734 if (data->pool)
735 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300736
737 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400738}
739
Kristian Høgsberg16626282012-04-03 11:21:27 -0400740static struct wl_shm_pool *
741make_shm_pool(struct display *display, int size, void **data)
742{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400743 struct wl_shm_pool *pool;
744 int fd;
745
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300746 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400747 if (fd < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200748 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
749 size, strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400750 return NULL;
751 }
752
753 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400754 if (*data == MAP_FAILED) {
Antonio Borneo39578632019-04-26 23:57:31 +0200755 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400756 close(fd);
757 return NULL;
758 }
759
760 pool = wl_shm_create_pool(display->shm, fd, size);
761
762 close(fd);
763
764 return pool;
765}
766
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700767static struct shm_pool *
768shm_pool_create(struct display *display, size_t size)
769{
770 struct shm_pool *pool = malloc(sizeof *pool);
771
772 if (!pool)
773 return NULL;
774
775 pool->pool = make_shm_pool(display, size, &pool->data);
776 if (!pool->pool) {
777 free(pool);
778 return NULL;
779 }
780
781 pool->size = size;
782 pool->used = 0;
783
784 return pool;
785}
786
787static void *
788shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
789{
790 if (pool->used + size > pool->size)
791 return NULL;
792
793 *offset = pool->used;
794 pool->used += size;
795
796 return (char *) pool->data + *offset;
797}
798
799/* destroy the pool. this does not unmap the memory though */
800static void
801shm_pool_destroy(struct shm_pool *pool)
802{
803 munmap(pool->data, pool->size);
804 wl_shm_pool_destroy(pool->pool);
805 free(pool);
806}
807
808/* Start allocating from the beginning of the pool again */
809static void
810shm_pool_reset(struct shm_pool *pool)
811{
812 pool->used = 0;
813}
814
815static int
816data_length_for_shm_surface(struct rectangle *rect)
817{
818 int stride;
819
820 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
821 rect->width);
822 return stride * rect->height;
823}
824
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500825static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700826display_create_shm_surface_from_pool(struct display *display,
827 struct rectangle *rectangle,
828 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829{
830 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400831 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400832 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700833 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400834 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835
836 data = malloc(sizeof *data);
837 if (data == NULL)
838 return NULL;
839
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300840 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
841 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842 length = stride * rectangle->height;
843 data->pool = NULL;
844 map = shm_pool_allocate(pool, length, &offset);
845
846 if (!map) {
847 free(data);
848 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400849 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400851 surface = cairo_image_surface_create_for_data (map,
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300852 CAIRO_FORMAT_ARGB32,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400853 rectangle->width,
854 rectangle->height,
855 stride);
856
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200857 cairo_surface_set_user_data(surface, &shm_surface_data_key,
858 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400859
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300860 if (flags & SURFACE_OPAQUE)
861 format = WL_SHM_FORMAT_XRGB8888;
862 else
863 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400864
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200865 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
866 rectangle->width,
867 rectangle->height,
868 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400869
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700870 return surface;
871}
872
873static cairo_surface_t *
874display_create_shm_surface(struct display *display,
875 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200876 struct shm_pool *alternate_pool,
877 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700878{
879 struct shm_surface_data *data;
880 struct shm_pool *pool;
881 cairo_surface_t *surface;
882
Pekka Paalanen99436862012-11-19 17:15:59 +0200883 if (alternate_pool) {
884 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700885 surface = display_create_shm_surface_from_pool(display,
886 rectangle,
887 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200888 alternate_pool);
889 if (surface) {
890 data = cairo_surface_get_user_data(surface,
891 &shm_surface_data_key);
892 goto out;
893 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700894 }
895
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300896 pool = shm_pool_create(display, data_length_for_shm_surface(rectangle));
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700897 if (!pool)
898 return NULL;
899
900 surface =
901 display_create_shm_surface_from_pool(display, rectangle,
902 flags, pool);
903
904 if (!surface) {
905 shm_pool_destroy(pool);
906 return NULL;
907 }
908
909 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200910 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700911 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400912
Pekka Paalanen99436862012-11-19 17:15:59 +0200913out:
914 if (data_ret)
915 *data_ret = data;
916
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400917 return surface;
918}
919
nobled7b87cb02011-02-01 18:51:47 +0000920static int
921check_size(struct rectangle *rect)
922{
923 if (rect->width && rect->height)
924 return 0;
925
926 fprintf(stderr, "tried to create surface of "
927 "width: %d, height: %d\n", rect->width, rect->height);
928 return -1;
929}
930
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400931cairo_surface_t *
932display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100933 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400934 struct rectangle *rectangle,
935 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936{
nobled7b87cb02011-02-01 18:51:47 +0000937 if (check_size(rectangle) < 0)
938 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200939
940 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200941 return display_create_shm_surface(display, rectangle, flags,
942 NULL, NULL);
943}
944
Pekka Paalanena4eda732012-11-19 17:16:02 +0200945struct shm_surface_leaf {
946 cairo_surface_t *cairo_surface;
947 /* 'data' is automatically destroyed, when 'cairo_surface' is */
948 struct shm_surface_data *data;
949
950 struct shm_pool *resize_pool;
951 int busy;
952};
953
954static void
955shm_surface_leaf_release(struct shm_surface_leaf *leaf)
956{
957 if (leaf->cairo_surface)
958 cairo_surface_destroy(leaf->cairo_surface);
959 /* leaf->data already destroyed via cairo private */
960
961 if (leaf->resize_pool)
962 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200963
964 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200965}
966
Pekka Paalanenaef02542013-04-25 13:57:47 +0300967#define MAX_LEAVES 3
968
Pekka Paalanen99436862012-11-19 17:15:59 +0200969struct shm_surface {
970 struct toysurface base;
971 struct display *display;
972 struct wl_surface *surface;
973 uint32_t flags;
974 int dx, dy;
975
Pekka Paalanenaef02542013-04-25 13:57:47 +0300976 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200977 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200978};
979
980static struct shm_surface *
981to_shm_surface(struct toysurface *base)
982{
983 return container_of(base, struct shm_surface, base);
984}
985
Pekka Paalanena4eda732012-11-19 17:16:02 +0200986static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300987shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
988{
989#ifdef DEBUG
990 struct shm_surface_leaf *leaf;
991 char bufs[MAX_LEAVES + 1];
992 int i;
993
994 for (i = 0; i < MAX_LEAVES; i++) {
995 leaf = &surface->leaf[i];
996
997 if (leaf->busy)
998 bufs[i] = 'b';
999 else if (leaf->cairo_surface)
1000 bufs[i] = 'a';
1001 else
1002 bufs[i] = ' ';
1003 }
1004
1005 bufs[MAX_LEAVES] = '\0';
1006 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1007#endif
1008}
1009
1010static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001011shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1012{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001013 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001014 struct shm_surface_leaf *leaf;
1015 int i;
1016 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001017
1018 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001019
Pekka Paalanenaef02542013-04-25 13:57:47 +03001020 for (i = 0; i < MAX_LEAVES; i++) {
1021 leaf = &surface->leaf[i];
1022 if (leaf->data && leaf->data->buffer == buffer) {
1023 leaf->busy = 0;
1024 break;
1025 }
1026 }
1027 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001028
Pekka Paalanenaef02542013-04-25 13:57:47 +03001029 /* Leave one free leaf with storage, release others */
1030 free_found = 0;
1031 for (i = 0; i < MAX_LEAVES; i++) {
1032 leaf = &surface->leaf[i];
1033
1034 if (!leaf->cairo_surface || leaf->busy)
1035 continue;
1036
1037 if (!free_found)
1038 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001039 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001040 shm_surface_leaf_release(leaf);
1041 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001042
Pekka Paalanen97777442013-05-22 10:20:05 +03001043 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044}
1045
1046static const struct wl_buffer_listener shm_surface_buffer_listener = {
1047 shm_surface_buffer_release
1048};
1049
Pekka Paalanen99436862012-11-19 17:15:59 +02001050static cairo_surface_t *
1051shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001052 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001053 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001054{
Pekka Paalanenec076692012-11-30 13:37:27 +02001055 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001056 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001057 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001058 struct shm_surface_leaf *leaf = NULL;
1059 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001060
1061 surface->dx = dx;
1062 surface->dy = dy;
1063
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001064 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001065 for (i = 0; i < MAX_LEAVES; i++) {
1066 if (surface->leaf[i].busy)
1067 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001068
Pekka Paalanenaef02542013-04-25 13:57:47 +03001069 if (!leaf || surface->leaf[i].cairo_surface)
1070 leaf = &surface->leaf[i];
1071 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001072 DBG_OBJ(surface->surface, "pick leaf %d\n",
1073 (int)(leaf - &surface->leaf[0]));
1074
Pekka Paalanenaef02542013-04-25 13:57:47 +03001075 if (!leaf) {
1076 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001077 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001078 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001080 }
1081
Pekka Paalanena4eda732012-11-19 17:16:02 +02001082 if (!resize_hint && leaf->resize_pool) {
1083 cairo_surface_destroy(leaf->cairo_surface);
1084 leaf->cairo_surface = NULL;
1085 shm_pool_destroy(leaf->resize_pool);
1086 leaf->resize_pool = NULL;
1087 }
1088
Alexander Larsson1818e312013-05-22 14:41:31 +02001089 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1090
Pekka Paalanena4eda732012-11-19 17:16:02 +02001091 if (leaf->cairo_surface &&
1092 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1093 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001094 goto out;
1095
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 if (leaf->cairo_surface)
1097 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001098
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001099#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001101 /* Create a big pool to allocate from, while continuously
1102 * resizing. Mmapping a new pool in the server
1103 * is relatively expensive, so reusing a pool performs
1104 * better, but may temporarily reserve unneeded memory.
1105 */
1106 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001107 leaf->resize_pool = shm_pool_create(surface->display,
1108 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001109 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001110#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001111
Alexander Larsson1818e312013-05-22 14:41:31 +02001112 rect.width = width;
1113 rect.height = height;
1114
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001116 display_create_shm_surface(surface->display, &rect,
1117 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001118 leaf->resize_pool,
1119 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001120 if (!leaf->cairo_surface)
1121 return NULL;
1122
Pekka Paalanena4eda732012-11-19 17:16:02 +02001123 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001124 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001125
1126out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127 surface->current = leaf;
1128
1129 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001130}
1131
1132static void
1133shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001134 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001135 struct rectangle *server_allocation)
1136{
1137 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001138 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001139
1140 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001141 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001142 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001143 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001144
Alexander Larsson1818e312013-05-22 14:41:31 +02001145 buffer_to_surface_size (buffer_transform, buffer_scale,
1146 &server_allocation->width,
1147 &server_allocation->height);
1148
Pekka Paalanena4eda732012-11-19 17:16:02 +02001149 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001150 surface->dx, surface->dy);
1151 wl_surface_damage(surface->surface, 0, 0,
1152 server_allocation->width, server_allocation->height);
1153 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001154
Pekka Paalanen71233882013-04-25 13:57:53 +03001155 DBG_OBJ(surface->surface, "leaf %d busy\n",
1156 (int)(leaf - &surface->leaf[0]));
1157
Pekka Paalanena4eda732012-11-19 17:16:02 +02001158 leaf->busy = 1;
1159 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001160}
1161
1162static int
1163shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1164{
1165 return -1;
1166}
1167
1168static void
1169shm_surface_release(struct toysurface *base)
1170{
1171}
1172
1173static void
1174shm_surface_destroy(struct toysurface *base)
1175{
1176 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001177 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001178
Pekka Paalanenaef02542013-04-25 13:57:47 +03001179 for (i = 0; i < MAX_LEAVES; i++)
1180 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001181
1182 free(surface);
1183}
1184
1185static struct toysurface *
1186shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1187 uint32_t flags, struct rectangle *rectangle)
1188{
1189 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001190 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001191
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001192 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001193 surface->base.prepare = shm_surface_prepare;
1194 surface->base.swap = shm_surface_swap;
1195 surface->base.acquire = shm_surface_acquire;
1196 surface->base.release = shm_surface_release;
1197 surface->base.destroy = shm_surface_destroy;
1198
1199 surface->display = display;
1200 surface->surface = wl_surface;
1201 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001202
1203 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001204}
1205
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001206/*
1207 * The following correspondences between file names and cursors was copied
1208 * from: https://bugs.kde.org/attachment.cgi?id=67313
1209 */
1210
1211static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001212 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001213 "sw-resize",
1214 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215};
1216
1217static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001218 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001219 "se-resize",
1220 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001221};
1222
1223static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001224 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001225 "s-resize",
1226 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001227};
1228
1229static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001230 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001231 "closedhand",
1232 "208530c400c041818281048008011002"
1233};
1234
1235static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001236 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001237 "default",
1238 "top_left_arrow",
1239 "left-arrow"
1240};
1241
1242static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001243 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001244 "w-resize",
1245 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001246};
1247
1248static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001249 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001250 "e-resize",
1251 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252};
1253
1254static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001255 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001256 "nw-resize",
1257 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258};
1259
1260static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001261 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001262 "ne-resize",
1263 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001264};
1265
1266static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001267 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001268 "n-resize",
1269 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001270};
1271
1272static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001273 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001274 "ibeam",
1275 "text"
1276};
1277
1278static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001279 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001280 "pointer",
1281 "pointing_hand",
1282 "e29285e634086352946a0e7090d73106"
1283};
1284
1285static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001286 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001287 "wait",
1288 "0426c94ea35c87780ff01dc239897213"
1289};
1290
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001291static const char *move_draggings[] = {
1292 "dnd-move"
1293};
1294
1295static const char *copy_draggings[] = {
1296 "dnd-copy"
1297};
1298
1299static const char *forbidden_draggings[] = {
1300 "dnd-none",
1301 "dnd-no-drop"
1302};
1303
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001304struct cursor_alternatives {
1305 const char **names;
1306 size_t count;
1307};
1308
1309static const struct cursor_alternatives cursors[] = {
1310 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1311 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1312 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1313 {grabbings, ARRAY_LENGTH(grabbings)},
1314 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1315 {left_sides, ARRAY_LENGTH(left_sides)},
1316 {right_sides, ARRAY_LENGTH(right_sides)},
1317 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1318 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1319 {top_sides, ARRAY_LENGTH(top_sides)},
1320 {xterms, ARRAY_LENGTH(xterms)},
1321 {hand1s, ARRAY_LENGTH(hand1s)},
1322 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001323 {move_draggings, ARRAY_LENGTH(move_draggings)},
1324 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1325 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001326};
1327
1328static void
1329create_cursors(struct display *display)
1330{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001331 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001332 struct weston_config *config;
1333 struct weston_config_section *s;
emersionac71ee52018-11-18 21:42:10 +01001334 int size = DEFAULT_XCURSOR_SIZE;
1335 char *theme = NULL, *size_str;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001336 unsigned int i, j;
1337 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001338
emersion807cd2e2018-10-10 09:46:12 +02001339 theme = getenv("XCURSOR_THEME");
emersionac71ee52018-11-18 21:42:10 +01001340
1341 size_str = getenv("XCURSOR_SIZE");
1342 if (size_str) {
1343 safe_strtoint(size_str, &size);
1344 if (size <= 0)
1345 size = DEFAULT_XCURSOR_SIZE;
1346 }
emersion807cd2e2018-10-10 09:46:12 +02001347
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001348 config_file = weston_config_get_name_from_env();
1349 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001350 s = weston_config_get_section(config, "shell", NULL, NULL);
emersion807cd2e2018-10-10 09:46:12 +02001351 weston_config_section_get_string(s, "cursor-theme", &theme, theme);
1352 weston_config_section_get_int(s, "cursor-size", &size, size);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001353 weston_config_destroy(config);
1354
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001355 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001356 if (!display->cursor_theme) {
1357 fprintf(stderr, "could not load theme '%s'\n", theme);
1358 return;
1359 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001360 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001361 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001362 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001363
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001364 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001365 cursor = NULL;
1366 for (j = 0; !cursor && j < cursors[i].count; ++j)
1367 cursor = wl_cursor_theme_get_cursor(
1368 display->cursor_theme, cursors[i].names[j]);
1369
1370 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001371 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001372 cursors[i].names[0]);
1373
1374 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001375 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001376}
1377
1378static void
1379destroy_cursors(struct display *display)
1380{
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03001381 if (display->cursor_theme)
1382 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001383 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001384}
1385
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001386struct wl_cursor_image *
1387display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001388{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001389 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001390
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001391 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001392}
1393
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001394static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001395surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001396{
Harish Krupo7bcbab12018-12-11 13:45:43 +05301397 struct widget *widget = surface->widget;
Pekka Paalanen89dee002013-02-13 16:17:20 +02001398 if (!surface->cairo_surface)
1399 return;
1400
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001401 if (surface->opaque_region) {
1402 wl_surface_set_opaque_region(surface->surface,
1403 surface->opaque_region);
1404 wl_region_destroy(surface->opaque_region);
1405 surface->opaque_region = NULL;
1406 }
1407
1408 if (surface->input_region) {
1409 wl_surface_set_input_region(surface->surface,
1410 surface->input_region);
1411 wl_region_destroy(surface->input_region);
1412 surface->input_region = NULL;
1413 }
1414
Harish Krupo7bcbab12018-12-11 13:45:43 +05301415 if (surface->viewport) {
1416 wp_viewport_set_destination(surface->viewport,
1417 widget->viewport_dest_width,
1418 widget->viewport_dest_height);
1419 }
1420
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001421 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001422 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001423 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001424
Pekka Paalanen89dee002013-02-13 16:17:20 +02001425 cairo_surface_destroy(surface->cairo_surface);
1426 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001427}
1428
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001429int
1430window_has_focus(struct window *window)
1431{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001432 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001433}
1434
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001435static void
1436window_close(struct window *window)
1437{
1438 if (window->close_handler)
1439 window->close_handler(window->user_data);
1440 else
1441 display_exit(window->display);
1442}
1443
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001444struct display *
1445window_get_display(struct window *window)
1446{
1447 return window->display;
1448}
1449
Pekka Paalanen89dee002013-02-13 16:17:20 +02001450static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001451surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001452{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001453 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001454 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001455
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456 if (!surface->toysurface && display->dpy &&
1457 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001458 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001459 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001460 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001461 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001462 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001463 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001464
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001465 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001466 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001467 surface->surface,
1468 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001469
Pekka Paalanen89dee002013-02-13 16:17:20 +02001470 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001471 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001472 allocation.width, allocation.height, flags,
1473 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001474}
1475
1476static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001477window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001478{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001479 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001480 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001481
Pekka Paalanenec076692012-11-30 13:37:27 +02001482 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001483 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001484
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001485 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001486}
1487
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001488int
1489window_get_buffer_transform(struct window *window)
1490{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001491 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001492}
1493
1494void
1495window_set_buffer_transform(struct window *window,
1496 enum wl_output_transform transform)
1497{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001498 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001499 wl_surface_set_buffer_transform(window->main_surface->surface,
1500 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001501}
1502
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001503void
1504window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001505 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001506{
1507 window->main_surface->buffer_scale = scale;
1508 wl_surface_set_buffer_scale(window->main_surface->surface,
1509 scale);
1510}
1511
1512uint32_t
1513window_get_buffer_scale(struct window *window)
1514{
1515 return window->main_surface->buffer_scale;
1516}
1517
Alexander Larssond68f5232013-05-22 14:41:33 +02001518uint32_t
1519window_get_output_scale(struct window *window)
1520{
1521 struct window_output *window_output;
1522 struct window_output *window_output_tmp;
1523 int scale = 1;
1524
1525 wl_list_for_each_safe(window_output, window_output_tmp,
1526 &window->window_output_list, link) {
1527 if (window_output->output->scale > scale)
1528 scale = window_output->output->scale;
1529 }
1530
1531 return scale;
1532}
1533
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001534static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001535
Pekka Paalanen4e373742013-02-13 16:17:13 +02001536static void
1537surface_destroy(struct surface *surface)
1538{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001539 if (surface->frame_cb)
1540 wl_callback_destroy(surface->frame_cb);
1541
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001542 if (surface->input_region)
1543 wl_region_destroy(surface->input_region);
1544
1545 if (surface->opaque_region)
1546 wl_region_destroy(surface->opaque_region);
1547
Pekka Paalanen35e82632013-04-25 13:57:48 +03001548 if (surface->subsurface)
1549 wl_subsurface_destroy(surface->subsurface);
1550
Pekka Paalanen4e373742013-02-13 16:17:13 +02001551 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001552
1553 if (surface->toysurface)
1554 surface->toysurface->destroy(surface->toysurface);
1555
Pekka Paalanen35e82632013-04-25 13:57:48 +03001556 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001557 free(surface);
1558}
1559
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001560void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001561window_destroy(struct window *window)
1562{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001563 struct display *display = window->display;
1564 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001565 struct window_output *window_output;
1566 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001567
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001568 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001569
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001570 wl_list_for_each(input, &display->input_list, link) {
Jeffy Chen179458a2019-09-02 10:15:26 +08001571 if (input->touch_focus == window) {
1572 struct touch_point *tp, *tmp;
1573
1574 wl_list_for_each_safe(tp, tmp,
1575 &input->touch_point_list,
1576 link) {
1577 wl_list_remove(&tp->link);
1578 free(tp);
1579 }
1580
Rusty Lynch1084da52013-08-15 09:10:08 -07001581 input->touch_focus = NULL;
Jeffy Chen179458a2019-09-02 10:15:26 +08001582 }
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001583 if (input->pointer_focus == window)
1584 input->pointer_focus = NULL;
1585 if (input->keyboard_focus == window)
1586 input->keyboard_focus = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08001587 if (input->locked_window == window)
1588 input->locked_window = NULL;
1589 if (input->confined_window == window)
1590 input->confined_window = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001591 if (input->focus_widget &&
1592 input->focus_widget->window == window)
1593 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001594 }
1595
Rob Bradford7507b572012-05-15 17:55:34 +01001596 wl_list_for_each_safe(window_output, window_output_tmp,
1597 &window->window_output_list, link) {
1598 free (window_output);
1599 }
1600
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001601 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001602 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001603
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001604 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01001605 xdg_toplevel_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001606 if (window->xdg_popup)
ant8mef99fac22018-11-28 22:46:37 +01001607 xdg_popup_destroy(window->xdg_popup);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001608 if (window->xdg_surface)
ant8mef99fac22018-11-28 22:46:37 +01001609 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001610
1611 surface_destroy(window->main_surface);
1612
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001613 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001614
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001615 free(window->title);
Marius Vladb3544c22021-09-09 13:52:18 +03001616 free(window->appid);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001617 free(window);
1618}
1619
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001620static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001621widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001623 struct widget *child, *target;
Harish Krupo7bcbab12018-12-11 13:45:43 +05301624 int alloc_x, alloc_y, width, height;
1625 double scale;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001626
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001627 wl_list_for_each(child, &widget->child_list, link) {
1628 target = widget_find_widget(child, x, y);
1629 if (target)
1630 return target;
1631 }
1632
Harish Krupo7bcbab12018-12-11 13:45:43 +05301633 alloc_x = widget->allocation.x;
1634 alloc_y = widget->allocation.y;
1635 width = widget->allocation.width;
1636 height = widget->allocation.height;
1637
1638 if (widget->viewport_dest_width != -1 &&
1639 widget->viewport_dest_height != -1) {
1640 scale = widget->viewport_dest_width / (double) width;
1641 alloc_x = alloc_x * scale;
1642 width = widget->viewport_dest_width;
1643
1644 scale = widget->viewport_dest_height / (double) height;
1645 alloc_y = alloc_y * scale;
1646 height = widget->viewport_dest_height;
1647 }
1648
1649 if (alloc_x <= x && x < alloc_x + width &&
1650 alloc_y <= y && y < alloc_y + height) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001651 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001652 }
1653
1654 return NULL;
1655}
1656
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001657static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001658window_find_widget(struct window *window, int32_t x, int32_t y)
1659{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001660 struct surface *surface;
1661 struct widget *widget;
1662
1663 wl_list_for_each(surface, &window->subsurface_list, link) {
1664 widget = widget_find_widget(surface->widget, x, y);
1665 if (widget)
1666 return widget;
1667 }
1668
1669 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001670}
1671
1672static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001673widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001674{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001675 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001676
Peter Huttererf3d62272013-08-08 11:57:05 +10001677 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001678 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001679 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001680 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001681 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001682 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001683 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001684 widget->tooltip = NULL;
1685 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001686 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001687 widget->use_cairo = 1;
Harish Krupo7bcbab12018-12-11 13:45:43 +05301688 widget->viewport_dest_width = -1;
1689 widget->viewport_dest_height = -1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001690
1691 return widget;
1692}
1693
1694struct widget *
1695window_add_widget(struct window *window, void *data)
1696{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001697 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001698
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001699 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001700 wl_list_init(&widget->link);
1701 window->main_surface->widget = widget;
1702
1703 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001704}
1705
1706struct widget *
1707widget_add_widget(struct widget *parent, void *data)
1708{
1709 struct widget *widget;
1710
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001711 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001712 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001713
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001714 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001715}
1716
1717void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001718widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001719{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001720 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001721 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001722 struct input *input;
1723
Pekka Paalanen35e82632013-04-25 13:57:48 +03001724 /* Destroy the sub-surface along with the root widget */
1725 if (surface->widget == widget && surface->subsurface)
1726 surface_destroy(widget->surface);
1727
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001728 if (widget->tooltip)
1729 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001730
Pekka Paalanene156fb62012-01-19 13:51:38 +02001731 wl_list_for_each(input, &display->input_list, link) {
1732 if (input->focus_widget == widget)
1733 input->focus_widget = NULL;
1734 }
1735
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001736 wl_list_remove(&widget->link);
1737 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001738}
1739
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001740void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001741widget_set_default_cursor(struct widget *widget, int cursor)
1742{
1743 widget->default_cursor = cursor;
1744}
1745
1746void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001747widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001748{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001749 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001750}
1751
1752void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001753widget_set_size(struct widget *widget, int32_t width, int32_t height)
1754{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001755 widget->allocation.width = width;
1756 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001757}
1758
1759void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001760widget_set_allocation(struct widget *widget,
1761 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001762{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001763 widget->allocation.x = x;
1764 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001765 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001766}
1767
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001768void
1769widget_set_transparent(struct widget *widget, int transparent)
1770{
1771 widget->opaque = !transparent;
1772}
1773
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001774void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001775widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001776{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001777 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001778}
1779
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001780static cairo_surface_t *
1781widget_get_cairo_surface(struct widget *widget)
1782{
1783 struct surface *surface = widget->surface;
1784 struct window *window = widget->window;
1785
Neil Roberts97b747c2013-12-19 16:17:12 +00001786 assert(widget->use_cairo);
1787
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001788 if (!surface->cairo_surface) {
1789 if (surface == window->main_surface)
1790 window_create_main_surface(window);
1791 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001792 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001793 }
1794
1795 return surface->cairo_surface;
1796}
1797
Alexander Larsson15901f02013-05-22 14:41:25 +02001798static void
1799widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1800{
1801 struct surface *surface = widget->surface;
1802 double angle;
1803 cairo_matrix_t m;
1804 enum wl_output_transform transform;
1805 int surface_width, surface_height;
1806 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001807 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001808
1809 surface_width = surface->allocation.width;
1810 surface_height = surface->allocation.height;
1811
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001812 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001813 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001814
Alexander Larsson15901f02013-05-22 14:41:25 +02001815 switch (transform) {
1816 case WL_OUTPUT_TRANSFORM_FLIPPED:
1817 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1818 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1819 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1820 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1821 break;
1822 default:
1823 cairo_matrix_init_identity(&m);
1824 break;
1825 }
1826
1827 switch (transform) {
1828 case WL_OUTPUT_TRANSFORM_NORMAL:
1829 default:
1830 angle = 0;
1831 translate_x = 0;
1832 translate_y = 0;
1833 break;
1834 case WL_OUTPUT_TRANSFORM_FLIPPED:
1835 angle = 0;
1836 translate_x = surface_width;
1837 translate_y = 0;
1838 break;
1839 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001840 angle = M_PI + M_PI_2;
1841 translate_x = 0;
1842 translate_y = surface_width;
Alexander Larsson15901f02013-05-22 14:41:25 +02001843 break;
1844 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001845 angle = M_PI + M_PI_2;
1846 translate_x = 0;
1847 translate_y = 0;
Alexander Larsson15901f02013-05-22 14:41:25 +02001848 break;
1849 case WL_OUTPUT_TRANSFORM_180:
1850 angle = M_PI;
1851 translate_x = surface_width;
1852 translate_y = surface_height;
1853 break;
1854 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1855 angle = M_PI;
1856 translate_x = 0;
1857 translate_y = surface_height;
1858 break;
1859 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001860 angle = M_PI_2;
1861 translate_x = surface_height;
1862 translate_y = 0;
Alexander Larsson15901f02013-05-22 14:41:25 +02001863 break;
1864 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001865 angle = M_PI_2;
1866 translate_x = surface_height;
1867 translate_y = surface_width;
Alexander Larsson15901f02013-05-22 14:41:25 +02001868 break;
1869 }
1870
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001871 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001872 cairo_translate(cr, translate_x, translate_y);
1873 cairo_rotate(cr, angle);
1874 cairo_transform(cr, &m);
1875}
1876
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001877cairo_t *
1878widget_cairo_create(struct widget *widget)
1879{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001880 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001881 cairo_surface_t *cairo_surface;
1882 cairo_t *cr;
1883
1884 cairo_surface = widget_get_cairo_surface(widget);
1885 cr = cairo_create(cairo_surface);
1886
Alexander Larsson15901f02013-05-22 14:41:25 +02001887 widget_cairo_update_transform(widget, cr);
1888
Pekka Paalanen35e82632013-04-25 13:57:48 +03001889 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1890
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001891 return cr;
1892}
1893
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001894struct wl_surface *
1895widget_get_wl_surface(struct widget *widget)
1896{
1897 return widget->surface->surface;
1898}
1899
Neil Roberts5e10a042013-09-09 00:40:17 +01001900struct wl_subsurface *
1901widget_get_wl_subsurface(struct widget *widget)
1902{
1903 return widget->surface->subsurface;
1904}
1905
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001906uint32_t
1907widget_get_last_time(struct widget *widget)
1908{
1909 return widget->surface->last_time;
1910}
1911
1912void
1913widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1914{
1915 struct wl_compositor *comp = widget->window->display->compositor;
1916 struct surface *surface = widget->surface;
1917
1918 if (!surface->input_region)
1919 surface->input_region = wl_compositor_create_region(comp);
1920
1921 if (rect) {
1922 wl_region_add(surface->input_region,
1923 rect->x, rect->y, rect->width, rect->height);
1924 }
1925}
1926
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001927void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001928widget_set_resize_handler(struct widget *widget,
1929 widget_resize_handler_t handler)
1930{
1931 widget->resize_handler = handler;
1932}
1933
1934void
1935widget_set_redraw_handler(struct widget *widget,
1936 widget_redraw_handler_t handler)
1937{
1938 widget->redraw_handler = handler;
1939}
1940
1941void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001942widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001943{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001944 widget->enter_handler = handler;
1945}
1946
1947void
1948widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1949{
1950 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001951}
1952
1953void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001954widget_set_motion_handler(struct widget *widget,
1955 widget_motion_handler_t handler)
1956{
1957 widget->motion_handler = handler;
1958}
1959
1960void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001961widget_set_button_handler(struct widget *widget,
1962 widget_button_handler_t handler)
1963{
1964 widget->button_handler = handler;
1965}
1966
1967void
Rusty Lynch041815a2013-08-08 21:20:38 -07001968widget_set_touch_up_handler(struct widget *widget,
1969 widget_touch_up_handler_t handler)
1970{
1971 widget->touch_up_handler = handler;
1972}
1973
1974void
1975widget_set_touch_down_handler(struct widget *widget,
1976 widget_touch_down_handler_t handler)
1977{
1978 widget->touch_down_handler = handler;
1979}
1980
1981void
1982widget_set_touch_motion_handler(struct widget *widget,
1983 widget_touch_motion_handler_t handler)
1984{
1985 widget->touch_motion_handler = handler;
1986}
1987
1988void
1989widget_set_touch_frame_handler(struct widget *widget,
1990 widget_touch_frame_handler_t handler)
1991{
1992 widget->touch_frame_handler = handler;
1993}
1994
1995void
1996widget_set_touch_cancel_handler(struct widget *widget,
1997 widget_touch_cancel_handler_t handler)
1998{
1999 widget->touch_cancel_handler = handler;
2000}
2001
2002void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002003widget_set_axis_handler(struct widget *widget,
2004 widget_axis_handler_t handler)
2005{
2006 widget->axis_handler = handler;
2007}
2008
Peter Hutterer87743e92016-01-18 16:38:22 +10002009void
2010widget_set_pointer_frame_handler(struct widget *widget,
2011 widget_pointer_frame_handler_t handler)
2012{
2013 widget->pointer_frame_handler = handler;
2014}
2015
2016void
2017widget_set_axis_handlers(struct widget *widget,
2018 widget_axis_handler_t axis_handler,
2019 widget_axis_source_handler_t axis_source_handler,
2020 widget_axis_stop_handler_t axis_stop_handler,
2021 widget_axis_discrete_handler_t axis_discrete_handler)
2022{
2023 widget->axis_handler = axis_handler;
2024 widget->axis_source_handler = axis_source_handler;
2025 widget->axis_stop_handler = axis_stop_handler;
2026 widget->axis_discrete_handler = axis_discrete_handler;
2027}
2028
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002029static void
2030window_schedule_redraw_task(struct window *window);
2031
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002032void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002033widget_schedule_redraw(struct widget *widget)
2034{
Pekka Paalanen71233882013-04-25 13:57:53 +03002035 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002036 widget->surface->redraw_needed = 1;
2037 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002038}
2039
Neil Roberts97b747c2013-12-19 16:17:12 +00002040void
2041widget_set_use_cairo(struct widget *widget,
2042 int use_cairo)
2043{
2044 widget->use_cairo = use_cairo;
2045}
2046
Harish Krupo7bcbab12018-12-11 13:45:43 +05302047int
2048widget_set_viewport_destination(struct widget *widget, int width, int height)
2049{
2050 struct window *window = widget->window;
2051 struct display *display = window->display;
2052 struct surface *surface = widget->surface;
2053 if (!display->viewporter)
2054 return -1;
2055
2056 if (width == -1 && height == -1) {
2057 if (surface->viewport) {
2058 wp_viewport_destroy(surface->viewport);
2059 surface->viewport = NULL;
2060 }
2061
2062 widget->viewport_dest_width = -1;
2063 widget->viewport_dest_height = -1;
2064 return 0;
2065 }
2066
2067 if (!surface->viewport) {
2068 surface->viewport = wp_viewporter_get_viewport(display->viewporter,
2069 surface->surface);
2070 if (!surface->viewport)
2071 return -1;
2072 }
2073
2074 widget->viewport_dest_width = width;
2075 widget->viewport_dest_height = height;
2076
2077 return 0;
2078}
2079
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002080cairo_surface_t *
2081window_get_surface(struct window *window)
2082{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002083 cairo_surface_t *cairo_surface;
2084
2085 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2086
2087 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002088}
2089
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002090struct wl_surface *
2091window_get_wl_surface(struct window *window)
2092{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002093 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002094}
2095
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002096static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002097tooltip_redraw_handler(struct widget *widget, void *data)
2098{
2099 cairo_t *cr;
2100 const int32_t r = 3;
2101 struct tooltip *tooltip = data;
2102 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002103
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002104 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002105 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002106 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2107 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2108 cairo_paint(cr);
2109
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002110 width = widget->allocation.width;
2111 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002112 rounded_rect(cr, 0, 0, width, height, r);
2113
2114 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2115 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2116 cairo_fill(cr);
2117
-c505af82019-06-26 21:00:43 +00002118 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.85);
2119 cairo_move_to(cr, 10, 17);
2120 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002121 cairo_show_text(cr, tooltip->entry);
2122 cairo_destroy(cr);
2123}
2124
2125static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002126get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002127{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002128 cairo_t *cr;
2129 cairo_text_extents_t extents;
2130
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002131 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002132 * created yet, and parent does not have a valid surface
2133 * outside repaint, either.
2134 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002135 cr = cairo_create(display->dummy_surface);
-c505af82019-06-26 21:00:43 +00002136 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002137 cairo_text_extents(cr, tooltip->entry, &extents);
2138 cairo_destroy(cr);
2139
2140 return extents;
2141}
2142
2143static int
2144window_create_tooltip(struct tooltip *tooltip)
2145{
2146 struct widget *parent = tooltip->parent;
2147 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002148 const int offset_y = 27;
2149 const int margin = 3;
2150 cairo_text_extents_t extents;
2151
2152 if (tooltip->widget)
2153 return 0;
2154
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002155 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002156
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002157 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002158 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002159 widget_set_allocation(tooltip->widget,
2160 tooltip->x, tooltip->y + offset_y,
2161 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002162
2163 return 0;
2164}
2165
2166void
2167widget_destroy_tooltip(struct widget *parent)
2168{
2169 struct tooltip *tooltip = parent->tooltip;
2170
2171 parent->tooltip_count = 0;
2172 if (!tooltip)
2173 return;
2174
2175 if (tooltip->widget) {
2176 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002177 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002178 }
2179
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002180 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002181 free(tooltip->entry);
2182 free(tooltip);
2183 parent->tooltip = NULL;
2184}
2185
2186static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002187tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002188{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002189 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002190
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002191 window_create_tooltip(tooltip);
2192}
2193
2194#define TOOLTIP_TIMEOUT 500
2195static int
2196tooltip_timer_reset(struct tooltip *tooltip)
2197{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002198 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002199
2200 return 0;
2201}
2202
2203int
2204widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2205{
2206 struct tooltip *tooltip = parent->tooltip;
2207
2208 parent->tooltip_count++;
2209 if (tooltip) {
2210 tooltip->x = x;
2211 tooltip->y = y;
2212 tooltip_timer_reset(tooltip);
2213 return 0;
2214 }
2215
2216 /* the handler might be triggered too fast via input device motion, so
2217 * we need this check here to make sure tooltip is fully initialized */
2218 if (parent->tooltip_count > 1)
2219 return 0;
2220
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002221 tooltip = malloc(sizeof *tooltip);
2222 if (!tooltip)
2223 return -1;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002224
2225 parent->tooltip = tooltip;
2226 tooltip->parent = parent;
2227 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002228 tooltip->x = x;
2229 tooltip->y = y;
2230 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002231 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2232 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002233 tooltip_timer_reset(tooltip);
2234
2235 return 0;
2236}
2237
2238static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002239frame_resize_handler(struct widget *widget,
2240 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002241{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002242 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002243 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002244 struct rectangle interior;
2245 struct rectangle input;
2246 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002247
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002248 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002249 interior.x = 0;
2250 interior.y = 0;
2251 interior.width = width;
2252 interior.height = height;
2253 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002254 frame_resize(frame->frame, width, height);
2255 frame_interior(frame->frame, &interior.x, &interior.y,
2256 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002257 }
2258
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002259 widget_set_allocation(child, interior.x, interior.y,
2260 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002261
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002262 if (child->resize_handler) {
2263 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002264 child->user_data);
2265
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002266 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002267 width = child->allocation.width;
2268 height = child->allocation.height;
2269 } else {
2270 frame_resize_inside(frame->frame,
2271 child->allocation.width,
2272 child->allocation.height);
2273 width = frame_width(frame->frame);
2274 height = frame_height(frame->frame);
2275 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002276 }
2277
Scott Moreauf7e498c2012-05-14 11:39:29 -06002278 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002279
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002280 widget->surface->input_region =
2281 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002282 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002283 frame_input_rect(frame->frame, &input.x, &input.y,
2284 &input.width, &input.height);
2285 wl_region_add(widget->surface->input_region,
2286 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002287 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002288 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002289 }
2290
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002291 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002292
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002293 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002294 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002295 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2296 &opaque.width, &opaque.height);
2297
2298 wl_region_add(widget->surface->opaque_region,
2299 opaque.x, opaque.y,
2300 opaque.width, opaque.height);
2301 } else {
2302 wl_region_add(widget->surface->opaque_region,
2303 0, 0, width, height);
2304 }
Martin Minarik1998b152012-05-10 02:04:35 +02002305 }
2306
Martin Minarik1998b152012-05-10 02:04:35 +02002307
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002308 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002309}
2310
2311static void
2312frame_redraw_handler(struct widget *widget, void *data)
2313{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002314 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002315 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002316 struct window *window = widget->window;
2317
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002318 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002319 return;
2320
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002321 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002322
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002323 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002324
2325 cairo_destroy(cr);
2326}
2327
2328static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002329frame_get_pointer_image_for_location(struct window_frame *frame,
2330 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002331{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002332 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002333
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002334 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002335 return CURSOR_LEFT_PTR;
2336
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002337 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002338 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002339 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002340 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002341 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002342 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002343 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002344 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002345 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002346 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002347 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002348 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002349 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002350 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002351 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002352 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002353 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002354 case THEME_LOCATION_EXTERIOR:
2355 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002356 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002357 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002358 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002359}
2360
Pekka Paalanen26237862014-09-10 15:10:30 +03002361static void
2362frame_menu_func(void *data, struct input *input, int index)
2363{
2364 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002365
2366 switch (index) {
2367 case 0: /* close */
2368 window_close(window);
2369 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002370 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002371 /* we don't have a way to get out of fullscreen for now */
2372 if (window->fullscreen_handler)
2373 window->fullscreen_handler(window, window->user_data);
2374 break;
2375 }
2376}
2377
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002378void
2379window_show_frame_menu(struct window *window,
2380 struct input *input, uint32_t time)
2381{
2382 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002383 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002384
Pekka Paalanen26237862014-09-10 15:10:30 +03002385 static const char *entries[] = {
2386 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002387 "Fullscreen"
2388 };
2389
2390 if (window->fullscreen_handler)
2391 count = ARRAY_LENGTH(entries);
2392 else
2393 count = ARRAY_LENGTH(entries) - 1;
2394
2395 input_get_position(input, &x, &y);
2396 window_show_menu(window->display, input, time, window,
2397 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002398}
2399
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002400static int
2401frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002402 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002403{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002404 struct window_frame *frame = data;
2405 enum theme_location location;
2406
2407 location = frame_pointer_enter(frame->frame, input, x, y);
2408 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2409 widget_schedule_redraw(frame->widget);
2410
2411 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002412}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002413
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002414static int
2415frame_motion_handler(struct widget *widget,
2416 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002417 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002419 struct window_frame *frame = data;
2420 enum theme_location location;
2421
2422 location = frame_pointer_motion(frame->frame, input, x, y);
2423 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2424 widget_schedule_redraw(frame->widget);
2425
2426 return frame_get_pointer_image_for_location(data, location);
2427}
2428
2429static void
2430frame_leave_handler(struct widget *widget,
2431 struct input *input, void *data)
2432{
2433 struct window_frame *frame = data;
2434
2435 frame_pointer_leave(frame->frame, input);
2436 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2437 widget_schedule_redraw(frame->widget);
2438}
2439
2440static void
2441frame_handle_status(struct window_frame *frame, struct input *input,
2442 uint32_t time, enum theme_location location)
2443{
2444 struct window *window = frame->widget->window;
2445 uint32_t status;
2446
2447 status = frame_status(frame->frame);
2448 if (status & FRAME_STATUS_REPAINT)
2449 widget_schedule_redraw(frame->widget);
2450
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002451 if (status & FRAME_STATUS_MINIMIZE) {
2452 window_set_minimized(window);
2453 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2454 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002455
2456 if (status & FRAME_STATUS_MENU) {
2457 window_show_frame_menu(window, input, time);
2458 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2459 }
2460
2461 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002462 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002463 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2464 }
2465
2466 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002467 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002468 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002469 }
2470
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002471 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002472 input_ungrab(input);
ant8mef99fac22018-11-28 22:46:37 +01002473 xdg_toplevel_move(window->xdg_toplevel,
2474 input_get_seat(input),
2475 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002476
2477 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2478 }
2479
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002480 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002481 input_ungrab(input);
2482
ant8mef99fac22018-11-28 22:46:37 +01002483 xdg_toplevel_resize(window->xdg_toplevel,
2484 input_get_seat(input),
2485 window->display->serial,
2486 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002487
2488 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2489 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002490}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002491
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002492#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002493static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002494frame_button_handler(struct widget *widget,
2495 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002496 uint32_t button, enum wl_pointer_button_state state,
2497 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002498
2499{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002500 struct window_frame *frame = data;
2501 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002502
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002503 frame->double_click = 0;
2504 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2505 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2506 frame->double_click = 1;
2507 frame->did_double = 1;
2508 } else
2509 frame->did_double = 0;
2510
2511 frame->last_time = time;
2512 } else if (frame->did_double == 1) {
2513 frame->double_click = 1;
2514 frame->did_double = 0;
2515 }
2516
2517 if (frame->double_click)
2518 location = frame_double_click(frame->frame, input,
2519 button, state);
2520 else
2521 location = frame_pointer_button(frame->frame, input,
2522 button, state);
2523
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002524 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002525}
2526
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002527static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002528frame_touch_down_handler(struct widget *widget, struct input *input,
2529 uint32_t serial, uint32_t time, int32_t id,
2530 float x, float y, void *data)
2531{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002532 struct window_frame *frame = data;
2533
Xiong Zhang382de462014-06-12 11:06:26 +08002534 frame->double_click = 0;
2535 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2536 frame->last_id == id) {
2537 frame->double_click = 1;
2538 frame->did_double = 1;
2539 frame->double_id = id;
2540 } else
2541 frame->did_double = 0;
2542
2543 frame->last_time = time;
2544 frame->last_id = id;
2545
2546 if (frame->double_click)
2547 frame_double_touch_down(frame->frame, input, id, x, y);
2548 else
2549 frame_touch_down(frame->frame, input, id, x, y);
2550
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002551 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2552}
2553
2554static void
2555frame_touch_up_handler(struct widget *widget,
2556 struct input *input, uint32_t serial, uint32_t time,
2557 int32_t id, void *data)
2558{
2559 struct window_frame *frame = data;
2560
Xiong Zhang382de462014-06-12 11:06:26 +08002561 if (frame->double_id == id && frame->did_double) {
2562 frame->did_double = 0;
2563 frame->double_id = 0;
2564 frame_double_touch_up(frame->frame, input, id);
2565 } else
2566 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002567 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002568}
2569
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002570struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002571window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002572{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002573 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002574 uint32_t buttons;
2575
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002576 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002577 buttons = FRAME_BUTTON_NONE;
2578 } else {
2579 buttons = FRAME_BUTTON_ALL;
2580 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002581
Peter Huttererf3d62272013-08-08 11:57:05 +10002582 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002583 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002584 buttons, window->title, NULL);
Simon Ser598d3a12020-02-10 15:49:20 +01002585 if (!frame->frame) {
2586 free(frame);
2587 return NULL;
2588 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002589
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002590 frame->widget = window_add_widget(window, frame);
2591 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002592
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002593 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2594 widget_set_resize_handler(frame->widget, frame_resize_handler);
2595 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002596 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002597 widget_set_motion_handler(frame->widget, frame_motion_handler);
2598 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002599 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002600 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002601
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002602 window->frame = frame;
2603
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002604 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002605}
2606
Kristian Høgsberga1627922012-06-20 17:30:03 -04002607void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002608window_frame_set_child_size(struct widget *widget, int child_width,
2609 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002610{
2611 struct display *display = widget->window->display;
2612 struct theme *t = display->theme;
2613 int decoration_width, decoration_height;
2614 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002615 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002616
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002617 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002618 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002619 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002620 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002621
2622 width = child_width + decoration_width;
2623 height = child_height + decoration_height;
2624 } else {
2625 width = child_width;
2626 height = child_height;
2627 }
2628
2629 window_schedule_resize(widget->window, width, height);
2630}
2631
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002632static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002633window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002634{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002635 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002636
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002637 /* frame->child must be destroyed by the application */
2638 widget_destroy(frame->widget);
2639 free(frame);
2640}
2641
2642static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002643input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002644 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002645{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002646 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002647 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002648
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002649 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002650 return;
2651
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002652 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002653 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002654 widget = old;
2655 if (input->grab)
2656 widget = input->grab;
2657 if (widget->leave_handler)
2658 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002659 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002660 }
2661
2662 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002663 widget = focus;
2664 if (input->grab)
2665 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002666 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002667 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002668 cursor = widget->enter_handler(focus, input, x, y,
2669 widget->user_data);
2670 else
2671 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002672
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002673 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002674 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002675}
2676
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002677void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002678touch_grab(struct input *input, int32_t touch_id)
2679{
2680 input->touch_grab = 1;
2681 input->touch_grab_id = touch_id;
2682}
2683
2684void
2685touch_ungrab(struct input *input)
2686{
2687 struct touch_point *tp, *tmp;
2688
2689 input->touch_grab = 0;
2690
2691 wl_list_for_each_safe(tp, tmp,
2692 &input->touch_point_list, link) {
2693 if (tp->id != input->touch_grab_id)
2694 continue;
2695 wl_list_remove(&tp->link);
2696 free(tp);
2697
2698 return;
2699 }
2700}
2701
2702void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002703input_grab(struct input *input, struct widget *widget, uint32_t button)
2704{
2705 input->grab = widget;
2706 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002707
2708 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002709}
2710
2711void
2712input_ungrab(struct input *input)
2713{
2714 struct widget *widget;
2715
2716 input->grab = NULL;
2717 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002718 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002719 input->sx, input->sy);
2720 input_set_focus_widget(input, widget, input->sx, input->sy);
2721 }
2722}
2723
2724static void
Derek Foreman118a4292015-04-22 17:23:35 -05002725cursor_delay_timer_reset(struct input *input, uint32_t duration)
2726{
Derek Foreman118a4292015-04-22 17:23:35 -05002727 if (!duration)
2728 input->cursor_timer_running = false;
2729 else
2730 input->cursor_timer_running = true;
2731
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002732 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002733}
2734
2735static void cancel_pointer_image_update(struct input *input)
2736{
2737 if (input->cursor_timer_running)
2738 cursor_delay_timer_reset(input, 0);
2739}
2740
2741static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002742input_remove_pointer_focus(struct input *input)
2743{
2744 struct window *window = input->pointer_focus;
2745
2746 if (!window)
2747 return;
2748
2749 input_set_focus_widget(input, NULL, 0, 0);
2750
2751 input->pointer_focus = NULL;
2752 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002753 cancel_pointer_image_update(input);
leng.fang32af9fc2024-06-13 11:22:15 +08002754 if (input->pointer_surface) {
2755 wl_surface_destroy(input->pointer_surface);
2756 input->pointer_surface = NULL;
2757 if (input->cursor_frame_cb) {
2758 wl_callback_destroy(input->cursor_frame_cb);
2759 input->cursor_frame_cb = NULL;
2760 }
Derek Foremanf079f432022-01-12 16:06:17 -06002761 }
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002762}
2763
2764static void
2765pointer_handle_enter(void *data, struct wl_pointer *pointer,
2766 uint32_t serial, struct wl_surface *surface,
2767 wl_fixed_t sx_w, wl_fixed_t sy_w)
2768{
2769 struct input *input = data;
2770 struct window *window;
2771 struct widget *widget;
2772 float sx = wl_fixed_to_double(sx_w);
2773 float sy = wl_fixed_to_double(sy_w);
2774
2775 if (!surface) {
2776 /* enter event for a window we've just destroyed */
2777 return;
2778 }
2779
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002780 window = wl_surface_get_user_data(surface);
2781 if (surface != window->main_surface->surface) {
2782 DBG("Ignoring input event from subsurface %p\n", surface);
2783 return;
2784 }
2785
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002786 input->display->serial = serial;
2787 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002788 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002789
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002790 input->sx = sx;
2791 input->sy = sy;
2792
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002793 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002794 input_set_focus_widget(input, widget, sx, sy);
2795}
2796
2797static void
2798pointer_handle_leave(void *data, struct wl_pointer *pointer,
2799 uint32_t serial, struct wl_surface *surface)
2800{
2801 struct input *input = data;
2802
2803 input->display->serial = serial;
2804 input_remove_pointer_focus(input);
2805}
2806
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002807static void
Daniel Stone37816df2012-05-16 18:45:18 +01002808pointer_handle_motion(void *data, struct wl_pointer *pointer,
2809 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002810{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002811 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002812 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002813 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002814 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002815 float sx = wl_fixed_to_double(sx_w);
2816 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002817
Paul Winwoodb22bf572013-08-29 10:52:54 +01002818 if (!window)
2819 return;
2820
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002821 input->sx = sx;
2822 input->sy = sy;
2823
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002824 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002825 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002826 * based on the old surface dimensions. However, if we have an active
2827 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002828 */
Derek Foreman46812b62015-08-26 17:13:27 -05002829 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002830 sy < window->main_surface->allocation.y ||
2831 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002832 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002833 return;
2834
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002835 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002836 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002837 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002838 }
2839
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002840 if (input->grab)
2841 widget = input->grab;
2842 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002843 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002844 if (widget) {
2845 if (widget->motion_handler)
2846 cursor = widget->motion_handler(input->focus_widget,
2847 input, time, sx, sy,
2848 widget->user_data);
2849 else
2850 cursor = widget->default_cursor;
2851 } else
2852 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002853
leng.fang32af9fc2024-06-13 11:22:15 +08002854 input->pointer_last_motion_time = time;
2855
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002856 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002857}
2858
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002859static void
Daniel Stone37816df2012-05-16 18:45:18 +01002860pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002861 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002862{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002863 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002864 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002865 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002866
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002867 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002868 if (input->focus_widget && input->grab == NULL &&
2869 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002870 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002871
Neil Roberts6b28aad2012-01-23 19:11:18 +00002872 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002873 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002874 (*widget->button_handler)(widget,
2875 input, time,
2876 button, state,
2877 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002878
Daniel Stone4dbadb12012-05-30 16:31:51 +01002879 if (input->grab && input->grab_button == button &&
2880 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002881 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002882}
2883
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002884static void
Daniel Stone37816df2012-05-16 18:45:18 +01002885pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002886 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002887{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002888 struct input *input = data;
2889 struct widget *widget;
2890
2891 widget = input->focus_widget;
2892 if (input->grab)
2893 widget = input->grab;
2894 if (widget && widget->axis_handler)
2895 (*widget->axis_handler)(widget,
2896 input, time,
2897 axis, value,
2898 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002899}
2900
Peter Hutterer87743e92016-01-18 16:38:22 +10002901static void
2902pointer_handle_frame(void *data, struct wl_pointer *pointer)
2903{
2904 struct input *input = data;
2905 struct widget *widget;
2906
2907 widget = input->focus_widget;
2908 if (input->grab)
2909 widget = input->grab;
2910 if (widget && widget->pointer_frame_handler)
2911 (*widget->pointer_frame_handler)(widget,
2912 input,
2913 widget->user_data);
2914}
2915
2916static void
2917pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2918 uint32_t source)
2919{
2920 struct input *input = data;
2921 struct widget *widget;
2922
2923 widget = input->focus_widget;
2924 if (input->grab)
2925 widget = input->grab;
2926 if (widget && widget->axis_source_handler)
2927 (*widget->axis_source_handler)(widget,
2928 input,
2929 source,
2930 widget->user_data);
2931}
2932
2933static void
2934pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2935 uint32_t time, uint32_t axis)
2936{
2937 struct input *input = data;
2938 struct widget *widget;
2939
2940 widget = input->focus_widget;
2941 if (input->grab)
2942 widget = input->grab;
2943 if (widget && widget->axis_stop_handler)
2944 (*widget->axis_stop_handler)(widget,
2945 input, time,
2946 axis,
2947 widget->user_data);
2948}
2949
2950static void
2951pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2952 uint32_t axis, int32_t discrete)
2953{
2954 struct input *input = data;
2955 struct widget *widget;
2956
2957 widget = input->focus_widget;
2958 if (input->grab)
2959 widget = input->grab;
2960 if (widget && widget->axis_discrete_handler)
2961 (*widget->axis_discrete_handler)(widget,
2962 input,
2963 axis,
2964 discrete,
2965 widget->user_data);
2966}
2967
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002968static const struct wl_pointer_listener pointer_listener = {
2969 pointer_handle_enter,
2970 pointer_handle_leave,
2971 pointer_handle_motion,
2972 pointer_handle_button,
2973 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002974 pointer_handle_frame,
2975 pointer_handle_axis_source,
2976 pointer_handle_axis_stop,
2977 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002978};
2979
2980static void
2981input_remove_keyboard_focus(struct input *input)
2982{
2983 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002984
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002985 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002986
2987 if (!window)
2988 return;
2989
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002990 if (window->keyboard_focus_handler)
2991 (*window->keyboard_focus_handler)(window, NULL,
2992 window->user_data);
2993
2994 input->keyboard_focus = NULL;
2995}
2996
2997static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002998keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002999{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003000 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003001 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003002
3003 if (window && window->key_handler) {
3004 (*window->key_handler)(window, input, input->repeat_time,
3005 input->repeat_key, input->repeat_sym,
3006 WL_KEYBOARD_KEY_STATE_PRESSED,
3007 window->user_data);
3008 }
3009}
3010
3011static void
3012keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
3013 uint32_t format, int fd, uint32_t size)
3014{
3015 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02003016 struct xkb_keymap *keymap;
3017 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01003018#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003019 struct xkb_compose_table *compose_table;
3020 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01003021#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003022 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003023 char *map_str;
3024
3025 if (!data) {
3026 close(fd);
3027 return;
3028 }
3029
3030 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3031 close(fd);
3032 return;
3033 }
3034
Sebastian Wickac1b92d2019-11-01 02:27:55 +01003035 map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003036 if (map_str == MAP_FAILED) {
3037 close(fd);
3038 return;
3039 }
3040
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003041 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03003042 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
3043 map_str,
3044 XKB_KEYMAP_FORMAT_TEXT_V1,
3045 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003046 munmap(map_str, size);
3047 close(fd);
3048
Rui Matos3eccb862013-10-10 19:44:22 +02003049 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003050 fprintf(stderr, "failed to compile keymap\n");
3051 return;
3052 }
3053
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003054 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02003055 state = xkb_state_new(keymap);
3056 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003057 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003058 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003059 return;
3060 }
3061
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003062 /* Look up the preferred locale, falling back to "C" as default */
3063 if (!(locale = getenv("LC_ALL")))
3064 if (!(locale = getenv("LC_CTYPE")))
3065 if (!(locale = getenv("LANG")))
3066 locale = "C";
3067
3068 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003069#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003070 compose_table =
3071 xkb_compose_table_new_from_locale(input->display->xkb_context,
3072 locale,
3073 XKB_COMPOSE_COMPILE_NO_FLAGS);
3074 if (compose_table) {
3075 /* Set up XKB compose state */
3076 compose_state = xkb_compose_state_new(compose_table,
3077 XKB_COMPOSE_STATE_NO_FLAGS);
3078 if (compose_state) {
3079 xkb_compose_state_unref(input->xkb.compose_state);
3080 xkb_compose_table_unref(input->xkb.compose_table);
3081 input->xkb.compose_state = compose_state;
3082 input->xkb.compose_table = compose_table;
3083 } else {
3084 fprintf(stderr, "could not create XKB compose state. "
3085 "Disabiling compose.\n");
3086 xkb_compose_table_unref(compose_table);
3087 compose_table = NULL;
3088 }
3089 } else {
3090 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3091 "Disabiling compose\n", locale);
3092 }
Daniel Stone5b015962016-10-20 14:45:58 +01003093#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003094
Rui Matos3eccb862013-10-10 19:44:22 +02003095 xkb_keymap_unref(input->xkb.keymap);
3096 xkb_state_unref(input->xkb.state);
3097 input->xkb.keymap = keymap;
3098 input->xkb.state = state;
3099
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003100 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003101 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003102 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003103 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003104 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003105 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003106}
3107
3108static void
3109keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3110 uint32_t serial, struct wl_surface *surface,
3111 struct wl_array *keys)
3112{
3113 struct input *input = data;
3114 struct window *window;
3115
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003116 if (!surface) {
3117 /* enter event for a window we've just destroyed */
3118 return;
3119 }
3120
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003121 input->display->serial = serial;
3122 input->keyboard_focus = wl_surface_get_user_data(surface);
3123
3124 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003125 if (window->keyboard_focus_handler)
3126 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003127 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003128}
3129
3130static void
3131keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3132 uint32_t serial, struct wl_surface *surface)
3133{
3134 struct input *input = data;
3135
3136 input->display->serial = serial;
3137 input_remove_keyboard_focus(input);
3138}
3139
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003140/* Translate symbols appropriately if a compose sequence is being entered */
3141static xkb_keysym_t
3142process_key_press(xkb_keysym_t sym, struct input *input)
3143{
Daniel Stone5b015962016-10-20 14:45:58 +01003144#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003145 if (!input->xkb.compose_state)
3146 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003147 if (sym == XKB_KEY_NoSymbol)
3148 return sym;
3149 if (xkb_compose_state_feed(input->xkb.compose_state,
3150 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3151 return sym;
3152
3153 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3154 case XKB_COMPOSE_COMPOSING:
3155 return XKB_KEY_NoSymbol;
3156 case XKB_COMPOSE_COMPOSED:
3157 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3158 case XKB_COMPOSE_CANCELLED:
3159 return XKB_KEY_NoSymbol;
3160 case XKB_COMPOSE_NOTHING:
3161 return sym;
3162 default:
3163 return sym;
3164 }
Daniel Stone5b015962016-10-20 14:45:58 +01003165#else
3166 return sym;
3167#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003168}
3169
Scott Moreau210d0792012-03-22 10:47:01 -06003170static void
Daniel Stone37816df2012-05-16 18:45:18 +01003171keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003172 uint32_t serial, uint32_t time, uint32_t key,
3173 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003174{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003175 struct input *input = data;
3176 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003177 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003178 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003179 const xkb_keysym_t *syms;
3180 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003181 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003182
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003183 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003184 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003185 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003186 return;
3187
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003188 /* We only use input grabs for pointer events for now, so just
3189 * ignore key presses if a grab is active. We expand the key
3190 * event delivery mechanism to route events to widgets to
3191 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003192 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003193 if (input->grab && input->grab_button == 0)
3194 return;
3195
Ran Benita2e1968f2014-08-19 23:59:51 +03003196 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003197
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003198 sym = XKB_KEY_NoSymbol;
3199 if (num_syms == 1)
3200 sym = syms[0];
3201
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003202
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003203 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003204 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003205 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003206 } else if (sym == XKB_KEY_F11 &&
3207 window->fullscreen_handler &&
3208 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3209 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003210 } else if (sym == XKB_KEY_F4 &&
3211 input->modifiers == MOD_ALT_MASK &&
3212 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003213 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003214 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003215 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3216 sym = process_key_press(sym, input);
3217
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003218 (*window->key_handler)(window, input, time, key,
3219 sym, state, window->user_data);
3220 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003221
3222 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3223 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003224 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003225 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3226 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003227 input->repeat_sym = sym;
3228 input->repeat_key = key;
3229 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003230 its.it_interval.tv_sec = input->repeat_rate_sec;
3231 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3232 its.it_value.tv_sec = input->repeat_delay_sec;
3233 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003234 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003235 }
3236}
3237
3238static void
Daniel Stone351eb612012-05-31 15:27:47 -04003239keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3240 uint32_t serial, uint32_t mods_depressed,
3241 uint32_t mods_latched, uint32_t mods_locked,
3242 uint32_t group)
3243{
3244 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003245 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003246
Matt Ropere61561f2013-06-24 16:52:43 +01003247 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3248 if (!input->xkb.keymap)
3249 return;
3250
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003251 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3252 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003253 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003254 XKB_STATE_MODS_DEPRESSED |
3255 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003256 input->modifiers = 0;
3257 if (mask & input->xkb.control_mask)
3258 input->modifiers |= MOD_CONTROL_MASK;
3259 if (mask & input->xkb.alt_mask)
3260 input->modifiers |= MOD_ALT_MASK;
3261 if (mask & input->xkb.shift_mask)
3262 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003263}
3264
Jonny Lamb06959082014-08-12 14:58:27 +02003265static void
3266set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3267{
3268 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3269 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3270
3271 /* a rate of zero disables any repeating, regardless of the delay's
3272 * value */
3273 if (rate == 0)
3274 return;
3275
3276 if (rate == 1)
3277 input->repeat_rate_sec = 1;
3278 else
3279 input->repeat_rate_nsec = 1000000000 / rate;
3280
3281 input->repeat_delay_sec = delay / 1000;
3282 delay -= (input->repeat_delay_sec * 1000);
3283 input->repeat_delay_nsec = delay * 1000 * 1000;
3284}
3285
3286static void
3287keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3288 int32_t rate, int32_t delay)
3289{
3290 struct input *input = data;
3291
3292 set_repeat_info(input, rate, delay);
3293}
3294
Daniel Stone37816df2012-05-16 18:45:18 +01003295static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003296 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003297 keyboard_handle_enter,
3298 keyboard_handle_leave,
3299 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003300 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003301 keyboard_handle_repeat_info
3302
Daniel Stone37816df2012-05-16 18:45:18 +01003303};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003304
3305static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003306touch_handle_down(void *data, struct wl_touch *wl_touch,
3307 uint32_t serial, uint32_t time, struct wl_surface *surface,
3308 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3309{
3310 struct input *input = data;
3311 struct widget *widget;
3312 float sx = wl_fixed_to_double(x_w);
3313 float sy = wl_fixed_to_double(y_w);
3314
Jeffy Chen97749172019-09-02 10:26:09 +08003315 if (!surface) {
3316 /* down event for a window we've just destroyed */
3317 return;
3318 }
3319
Rusty Lynch1084da52013-08-15 09:10:08 -07003320 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003321 input->touch_focus = wl_surface_get_user_data(surface);
3322 if (!input->touch_focus) {
3323 DBG("Failed to find to touch focus for surface %p\n", surface);
3324 return;
3325 }
3326
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003327 if (surface != input->touch_focus->main_surface->surface) {
3328 DBG("Ignoring input event from subsurface %p\n", surface);
3329 input->touch_focus = NULL;
3330 return;
3331 }
3332
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003333 if (input->grab)
3334 widget = input->grab;
3335 else
3336 widget = window_find_widget(input->touch_focus,
3337 wl_fixed_to_double(x_w),
3338 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003339 if (widget) {
3340 struct touch_point *tp = xmalloc(sizeof *tp);
3341 if (tp) {
3342 tp->id = id;
3343 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003344 tp->x = sx;
3345 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003346 wl_list_insert(&input->touch_point_list, &tp->link);
3347
3348 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003349 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003350 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003351 sx, sy,
3352 widget->user_data);
3353 }
3354 }
3355}
3356
3357static void
3358touch_handle_up(void *data, struct wl_touch *wl_touch,
3359 uint32_t serial, uint32_t time, int32_t id)
3360{
3361 struct input *input = data;
3362 struct touch_point *tp, *tmp;
3363
Rusty Lynch041815a2013-08-08 21:20:38 -07003364 if (!input->touch_focus) {
3365 DBG("No touch focus found for touch up event!\n");
3366 return;
3367 }
3368
3369 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3370 if (tp->id != id)
3371 continue;
3372
3373 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003374 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003375 time, id,
3376 tp->widget->user_data);
3377
3378 wl_list_remove(&tp->link);
3379 free(tp);
3380
3381 return;
3382 }
3383}
3384
3385static void
3386touch_handle_motion(void *data, struct wl_touch *wl_touch,
3387 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3388{
3389 struct input *input = data;
3390 struct touch_point *tp;
3391 float sx = wl_fixed_to_double(x_w);
3392 float sy = wl_fixed_to_double(y_w);
3393
3394 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3395
3396 if (!input->touch_focus) {
3397 DBG("No touch focus found for touch motion event!\n");
3398 return;
3399 }
3400
3401 wl_list_for_each(tp, &input->touch_point_list, link) {
3402 if (tp->id != id)
3403 continue;
3404
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003405 tp->x = sx;
3406 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003407 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003408 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003409 id, sx, sy,
3410 tp->widget->user_data);
3411 return;
3412 }
3413}
3414
3415static void
3416touch_handle_frame(void *data, struct wl_touch *wl_touch)
3417{
3418 struct input *input = data;
3419 struct touch_point *tp, *tmp;
3420
3421 DBG("touch_handle_frame\n");
3422
3423 if (!input->touch_focus) {
3424 DBG("No touch focus found for touch frame event!\n");
3425 return;
3426 }
3427
3428 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3429 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003430 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003431 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003432 }
3433}
3434
3435static void
3436touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3437{
3438 struct input *input = data;
3439 struct touch_point *tp, *tmp;
3440
3441 DBG("touch_handle_cancel\n");
3442
3443 if (!input->touch_focus) {
3444 DBG("No touch focus found for touch cancel event!\n");
3445 return;
3446 }
3447
3448 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3449 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003450 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3451 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003452
3453 wl_list_remove(&tp->link);
3454 free(tp);
3455 }
3456}
3457
Guillaume Champagne1cb09482020-01-27 20:12:16 -05003458static void
3459touch_handle_shape(void *data, struct wl_touch *wl_touch, int32_t id,
3460 wl_fixed_t major, wl_fixed_t minor)
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003461{
3462}
3463
Guillaume Champagne1cb09482020-01-27 20:12:16 -05003464static void
3465touch_handle_orientation(void *data, struct wl_touch *wl_touch, int32_t id,
3466 wl_fixed_t orientation)
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003467{
3468}
3469
Rusty Lynch041815a2013-08-08 21:20:38 -07003470static const struct wl_touch_listener touch_listener = {
3471 touch_handle_down,
3472 touch_handle_up,
3473 touch_handle_motion,
3474 touch_handle_frame,
3475 touch_handle_cancel,
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003476 touch_handle_shape,
3477 touch_handle_orientation,
Rusty Lynch041815a2013-08-08 21:20:38 -07003478};
3479
3480static void
Daniel Stone37816df2012-05-16 18:45:18 +01003481seat_handle_capabilities(void *data, struct wl_seat *seat,
3482 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003483{
Daniel Stone37816df2012-05-16 18:45:18 +01003484 struct input *input = data;
3485
3486 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3487 input->pointer = wl_seat_get_pointer(seat);
3488 wl_pointer_set_user_data(input->pointer, input);
3489 wl_pointer_add_listener(input->pointer, &pointer_listener,
3490 input);
3491 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003492 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003493 wl_pointer_release(input->pointer);
3494 else
3495 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003496 input->pointer = NULL;
3497 }
3498
3499 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3500 input->keyboard = wl_seat_get_keyboard(seat);
3501 wl_keyboard_set_user_data(input->keyboard, input);
3502 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3503 input);
3504 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003505 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003506 wl_keyboard_release(input->keyboard);
3507 else
3508 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003509 input->keyboard = NULL;
3510 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003511
3512 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3513 input->touch = wl_seat_get_touch(seat);
3514 wl_touch_set_user_data(input->touch, input);
3515 wl_touch_add_listener(input->touch, &touch_listener, input);
3516 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003517 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003518 wl_touch_release(input->touch);
3519 else
3520 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003521 input->touch = NULL;
3522 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003523}
3524
Rob Bradford08031182013-08-13 20:11:03 +01003525static void
3526seat_handle_name(void *data, struct wl_seat *seat,
3527 const char *name)
3528{
3529
3530}
3531
Daniel Stone37816df2012-05-16 18:45:18 +01003532static const struct wl_seat_listener seat_listener = {
3533 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003534 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003535};
3536
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003537void
3538input_get_position(struct input *input, int32_t *x, int32_t *y)
3539{
3540 *x = input->sx;
3541 *y = input->sy;
3542}
3543
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003544int
3545input_get_touch(struct input *input, int32_t id, float *x, float *y)
3546{
3547 struct touch_point *tp;
3548
3549 wl_list_for_each(tp, &input->touch_point_list, link) {
3550 if (tp->id != id)
3551 continue;
3552
3553 *x = tp->x;
3554 *y = tp->y;
3555 return 0;
3556 }
3557
3558 return -1;
3559}
3560
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003561struct display *
3562input_get_display(struct input *input)
3563{
3564 return input->display;
3565}
3566
Daniel Stone37816df2012-05-16 18:45:18 +01003567struct wl_seat *
3568input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003569{
Daniel Stone37816df2012-05-16 18:45:18 +01003570 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003571}
3572
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003573uint32_t
3574input_get_modifiers(struct input *input)
3575{
3576 return input->modifiers;
3577}
3578
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003579struct widget *
3580input_get_focus_widget(struct input *input)
3581{
3582 return input->focus_widget;
3583}
3584
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003585struct data_offer {
3586 struct wl_data_offer *offer;
3587 struct input *input;
3588 struct wl_array types;
3589 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003590
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003591 struct task io_task;
3592 int fd;
3593 data_func_t func;
3594 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003595 uint32_t dnd_action;
3596 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003597 void *user_data;
3598};
3599
3600static void
3601data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3602{
3603 struct data_offer *offer = data;
3604 char **p;
3605
3606 p = wl_array_add(&offer->types, sizeof *p);
3607 *p = strdup(type);
3608}
3609
Carlos Garnacho9c931792016-01-18 23:52:12 +01003610static void
3611data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3612{
3613 struct data_offer *offer = data;
3614
3615 offer->source_actions = source_actions;
3616}
3617
3618static void
3619data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3620{
3621 struct data_offer *offer = data;
3622
3623 offer->dnd_action = dnd_action;
3624}
3625
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003626static const struct wl_data_offer_listener data_offer_listener = {
3627 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003628 data_offer_source_actions,
3629 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003630};
3631
3632static void
3633data_offer_destroy(struct data_offer *offer)
3634{
3635 char **p;
3636
3637 offer->refcount--;
3638 if (offer->refcount == 0) {
3639 wl_data_offer_destroy(offer->offer);
3640 for (p = offer->types.data; *p; p++)
3641 free(*p);
3642 wl_array_release(&offer->types);
3643 free(offer);
3644 }
3645}
3646
3647static void
3648data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003649 struct wl_data_device *data_device,
3650 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003651{
3652 struct data_offer *offer;
3653
Brian Lovinbc919262013-08-07 15:34:59 -07003654 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003655
3656 wl_array_init(&offer->types);
3657 offer->refcount = 1;
3658 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003659 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003660 wl_data_offer_add_listener(offer->offer,
3661 &data_offer_listener, offer);
3662}
3663
3664static void
3665data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003666 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003667 wl_fixed_t x_w, wl_fixed_t y_w,
3668 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003669{
3670 struct input *input = data;
3671 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003672 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003673 float x = wl_fixed_to_double(x_w);
3674 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003675 char **p;
3676
Jeffy Chen97749172019-09-02 10:26:09 +08003677 if (!surface) {
3678 /* enter event for a window we've just destroyed */
3679 return;
3680 }
3681
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003682 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003683 input->drag_enter_serial = serial;
3684 input->drag_focus = window,
3685 input->drag_x = x;
3686 input->drag_y = y;
3687
3688 if (!input->touch_grab)
3689 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003690
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003691 if (offer) {
3692 input->drag_offer = wl_data_offer_get_user_data(offer);
3693
3694 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3695 *p = NULL;
3696
3697 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003698
3699 if (input->display->data_device_manager_version >=
3700 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3701 wl_data_offer_set_actions(offer,
3702 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3703 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3704 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3705 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003706 } else {
3707 input->drag_offer = NULL;
3708 types_data = NULL;
3709 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003710
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003711 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003712 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003713 window->user_data);
3714}
3715
3716static void
3717data_device_leave(void *data, struct wl_data_device *data_device)
3718{
3719 struct input *input = data;
3720
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003721 if (input->drag_offer) {
3722 data_offer_destroy(input->drag_offer);
3723 input->drag_offer = NULL;
3724 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003725}
3726
3727static void
3728data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003729 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003730{
3731 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003732 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003733 float x = wl_fixed_to_double(x_w);
3734 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003735 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003736
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003737 input->drag_x = x;
3738 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003739
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003740 if (input->drag_offer)
3741 types_data = input->drag_offer->types.data;
3742 else
3743 types_data = NULL;
3744
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003745 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003746 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003747 window->user_data);
3748}
3749
3750static void
3751data_device_drop(void *data, struct wl_data_device *data_device)
3752{
3753 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003754 struct window *window = input->drag_focus;
3755 float x, y;
3756
3757 x = input->drag_x;
3758 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003759
3760 if (window->drop_handler)
3761 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003762 x, y, window->user_data);
3763
3764 if (input->touch_grab)
3765 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003766}
3767
3768static void
3769data_device_selection(void *data,
3770 struct wl_data_device *wl_data_device,
3771 struct wl_data_offer *offer)
3772{
3773 struct input *input = data;
3774 char **p;
3775
3776 if (input->selection_offer)
3777 data_offer_destroy(input->selection_offer);
3778
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003779 if (offer) {
3780 input->selection_offer = wl_data_offer_get_user_data(offer);
3781 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3782 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003783 } else {
3784 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003785 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003786}
3787
3788static const struct wl_data_device_listener data_device_listener = {
3789 data_device_data_offer,
3790 data_device_enter,
3791 data_device_leave,
3792 data_device_motion,
3793 data_device_drop,
3794 data_device_selection
3795};
3796
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003797static void
3798input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003799{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003800 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003801 struct wl_cursor *cursor;
3802 struct wl_cursor_image *image;
Simon Ser992ee042021-02-07 11:32:46 +01003803 struct wl_surface *prev_surface;
3804 struct display *d = input->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003805
Daniel Stone80972742012-11-07 17:51:39 +11003806 if (!input->pointer)
3807 return;
3808
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003809 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003810 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003811 return;
3812
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003813 if (index >= (int) cursor->image_count) {
3814 fprintf(stderr, "cursor index out of range\n");
3815 return;
3816 }
3817
3818 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003819 buffer = wl_cursor_image_get_buffer(image);
3820 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003821 return;
3822
Simon Ser992ee042021-02-07 11:32:46 +01003823 /* Don't re-use the previous surface, otherwise the new buffer and the
3824 * new hotspot aren't applied atomically. */
3825 prev_surface = input->pointer_surface;
3826 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3827
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003828 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3829 wl_surface_damage(input->pointer_surface, 0, 0,
3830 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003831 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003832 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3833 input->pointer_surface,
3834 image->hotspot_x, image->hotspot_y);
Simon Ser992ee042021-02-07 11:32:46 +01003835
3836 if (prev_surface)
3837 wl_surface_destroy(prev_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003838}
3839
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003840static const struct wl_callback_listener pointer_surface_listener;
3841
Derek Foreman493d9792015-03-04 16:26:25 -06003842static bool
3843input_set_pointer_special(struct input *input)
3844{
3845 if (input->current_cursor == CURSOR_BLANK) {
3846 wl_pointer_set_cursor(input->pointer,
3847 input->pointer_enter_serial,
3848 NULL, 0, 0);
3849 return true;
3850 }
3851
3852 if (input->current_cursor == CURSOR_UNSET)
3853 return true;
3854
3855 return false;
3856}
3857
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003858static void
Derek Foreman118a4292015-04-22 17:23:35 -05003859schedule_pointer_image_update(struct input *input,
3860 struct wl_cursor *cursor,
3861 uint32_t duration,
3862 bool force_frame)
3863{
3864 /* Some silly cursor sets have enormous pauses in them. In these
3865 * cases it's better to use a timer even if it results in less
3866 * accurate presentation, since it will save us having to set the
3867 * same cursor image over and over again.
3868 *
3869 * This is really not the way we're supposed to time any kind of
3870 * animation, but we're pretending it's OK here because we don't
3871 * want animated cursors with long delays to needlessly hog CPU.
3872 *
3873 * We use force_frame to ensure we don't accumulate large timing
3874 * errors by running off the wrong clock.
3875 */
3876 if (!force_frame && duration > 100) {
3877 struct timespec tp;
3878
3879 clock_gettime(CLOCK_MONOTONIC, &tp);
3880 input->cursor_timer_start = tp.tv_sec * 1000
3881 + tp.tv_nsec / 1000000;
3882 cursor_delay_timer_reset(input, duration);
3883 return;
3884 }
3885
3886 /* for short durations we'll just spin on frame callbacks for
3887 * accurate timing - the way any kind of timing sensitive animation
3888 * should really be done. */
3889 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3890 wl_callback_add_listener(input->cursor_frame_cb,
3891 &pointer_surface_listener, input);
3892
Derek Foremanf079f432022-01-12 16:06:17 -06003893 wl_surface_commit(input->pointer_surface);
Derek Foreman118a4292015-04-22 17:23:35 -05003894}
3895
leng.fang32af9fc2024-06-13 11:22:15 +08003896static bool
3897hide_pointer_surface_needed(struct input* input) {
3898 uint32_t duration;
3899 if (input->pointer_last_motion_time == 0) {
3900 //For remote with mouse support but not use motion
3901 input->pointer_hidden = true;
3902 } else {
3903 input->pointer_hidden = false;
3904 }
3905 return input->pointer_hidden;
3906}
3907
Derek Foreman118a4292015-04-22 17:23:35 -05003908static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003909pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3910 uint32_t time)
3911{
3912 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003913 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003914 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003915 uint32_t duration;
3916 bool force_frame = true;
3917
3918 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003919
3920 if (callback) {
3921 assert(callback == input->cursor_frame_cb);
3922 wl_callback_destroy(callback);
3923 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003924 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003925 }
3926
Daniel Stone80972742012-11-07 17:51:39 +11003927 if (!input->pointer)
3928 return;
3929
Derek Foreman493d9792015-03-04 16:26:25 -06003930 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003931 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003932
leng.fang32af9fc2024-06-13 11:22:15 +08003933 if (hide_pointer_surface_needed(input))
3934 return;
3935
Daniel Stonea494f1d2012-06-18 19:31:12 +01003936 cursor = input->display->cursors[input->current_cursor];
3937 if (!cursor)
3938 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003939
3940 /* FIXME We don't have the current time on the first call so we set
3941 * the animation start to the time of the first frame callback. */
3942 if (time == 0)
3943 input->cursor_anim_start = 0;
3944 else if (input->cursor_anim_start == 0)
3945 input->cursor_anim_start = time;
3946
Derek Foreman118a4292015-04-22 17:23:35 -05003947 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003948
Derek Foreman118a4292015-04-22 17:23:35 -05003949 if (time == 0 || input->cursor_anim_start == 0) {
3950 duration = 0;
3951 i = 0;
3952 } else
3953 i = wl_cursor_frame_and_duration(
3954 cursor,
3955 time - input->cursor_anim_start,
3956 &duration);
3957
Simon Ser992ee042021-02-07 11:32:46 +01003958 input_set_pointer_image_index(input, i);
3959
Derek Foreman118a4292015-04-22 17:23:35 -05003960 if (cursor->image_count > 1)
3961 schedule_pointer_image_update(input, cursor, duration,
3962 force_frame);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003963}
3964
Derek Foreman118a4292015-04-22 17:23:35 -05003965static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003966cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003967{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003968 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003969 struct timespec tp;
3970 struct wl_cursor *cursor;
3971 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003972
3973 if (!input->cursor_timer_running)
3974 return;
3975
Derek Foreman118a4292015-04-22 17:23:35 -05003976 cursor = input->display->cursors[input->current_cursor];
3977 if (!cursor)
3978 return;
3979
3980 clock_gettime(CLOCK_MONOTONIC, &tp);
3981 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3982 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3983}
3984
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003985static const struct wl_callback_listener pointer_surface_listener = {
3986 pointer_surface_frame_callback
3987};
3988
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003989void
3990input_set_pointer_image(struct input *input, int pointer)
3991{
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003992 if (!input->pointer)
3993 return;
3994
Derek Foremanf079f432022-01-12 16:06:17 -06003995 if (pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003996 return;
3997
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003998 input->current_cursor = pointer;
3999 if (!input->cursor_frame_cb)
4000 pointer_surface_frame_callback(input, NULL, 0);
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04004001}
4002
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004003struct wl_data_device *
4004input_get_data_device(struct input *input)
4005{
4006 return input->data_device;
4007}
4008
4009void
4010input_set_selection(struct input *input,
4011 struct wl_data_source *source, uint32_t time)
4012{
Jason Ekstranda669bd52014-04-02 19:53:51 -05004013 if (input->data_device)
4014 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004015}
4016
4017void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004018input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004019{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004020 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004021 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004022}
4023
4024static void
4025offer_io_func(struct task *task, uint32_t events)
4026{
4027 struct data_offer *offer =
4028 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01004029 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004030 unsigned int len;
4031 char buffer[4096];
4032
4033 len = read(offer->fd, buffer, sizeof buffer);
4034 offer->func(buffer, len,
4035 offer->x, offer->y, offer->user_data);
4036
4037 if (len == 0) {
Harish Krupo43152a32019-04-19 22:06:44 +05304038 if ((offer != offer->input->selection_offer) &&
4039 (display->data_device_manager_version >=
4040 WL_DATA_OFFER_FINISH_SINCE_VERSION))
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01004041 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004042 close(offer->fd);
4043 data_offer_destroy(offer);
4044 }
4045}
4046
4047static void
4048data_offer_receive_data(struct data_offer *offer, const char *mime_type,
4049 data_func_t func, void *user_data)
4050{
4051 int p[2];
4052
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02004053 if (pipe2(p, O_CLOEXEC) == -1)
4054 return;
4055
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004056 wl_data_offer_receive(offer->offer, mime_type, p[1]);
4057 close(p[1]);
4058
4059 offer->io_task.run = offer_io_func;
4060 offer->fd = p[0];
4061 offer->func = func;
4062 offer->refcount++;
4063 offer->user_data = user_data;
4064
4065 display_watch_fd(offer->input->display,
4066 offer->fd, EPOLLIN, &offer->io_task);
4067}
4068
4069void
4070input_receive_drag_data(struct input *input, const char *mime_type,
4071 data_func_t func, void *data)
4072{
4073 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004074 input->drag_offer->x = input->drag_x;
4075 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004076}
4077
4078int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004079input_receive_drag_data_to_fd(struct input *input,
4080 const char *mime_type, int fd)
4081{
4082 if (input->drag_offer)
4083 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4084
4085 return 0;
4086}
4087
4088int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004089input_receive_selection_data(struct input *input, const char *mime_type,
4090 data_func_t func, void *data)
4091{
4092 char **p;
4093
4094 if (input->selection_offer == NULL)
4095 return -1;
4096
4097 for (p = input->selection_offer->types.data; *p; p++)
4098 if (strcmp(mime_type, *p) == 0)
4099 break;
4100
4101 if (*p == NULL)
4102 return -1;
4103
4104 data_offer_receive_data(input->selection_offer,
4105 mime_type, func, data);
4106 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004107}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004108
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004109int
4110input_receive_selection_data_to_fd(struct input *input,
4111 const char *mime_type, int fd)
4112{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004113 if (input->selection_offer)
4114 wl_data_offer_receive(input->selection_offer->offer,
4115 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004116
4117 return 0;
4118}
4119
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004120void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004121window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004122{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004123 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004124 return;
4125
ant8mef99fac22018-11-28 22:46:37 +01004126 xdg_toplevel_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004127}
4128
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004129static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004130surface_set_synchronized(struct surface *surface)
4131{
4132 if (!surface->subsurface)
4133 return;
4134
4135 if (surface->synchronized)
4136 return;
4137
4138 wl_subsurface_set_sync(surface->subsurface);
4139 surface->synchronized = 1;
4140}
4141
4142static void
4143surface_set_synchronized_default(struct surface *surface)
4144{
4145 if (!surface->subsurface)
4146 return;
4147
4148 if (surface->synchronized == surface->synchronized_default)
4149 return;
4150
4151 if (surface->synchronized_default)
4152 wl_subsurface_set_sync(surface->subsurface);
4153 else
4154 wl_subsurface_set_desync(surface->subsurface);
4155
4156 surface->synchronized = surface->synchronized_default;
4157}
4158
4159static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004160surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004161{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004162 struct widget *widget = surface->widget;
4163 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004164
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004165 if (surface->input_region) {
4166 wl_region_destroy(surface->input_region);
4167 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004168 }
4169
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004170 if (surface->opaque_region)
4171 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004172
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004173 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004174
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004175 if (widget->resize_handler)
4176 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004177 widget->allocation.width,
4178 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004179 widget->user_data);
4180
Pekka Paalanen35e82632013-04-25 13:57:48 +03004181 if (surface->subsurface &&
4182 (surface->allocation.x != widget->allocation.x ||
4183 surface->allocation.y != widget->allocation.y)) {
4184 wl_subsurface_set_position(surface->subsurface,
4185 widget->allocation.x,
4186 widget->allocation.y);
4187 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004188 if (surface->allocation.width != widget->allocation.width ||
4189 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004190 window_schedule_redraw(widget->window);
4191 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004192 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004193
4194 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004195 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004196 widget->allocation.width,
4197 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004198}
4199
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004200static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004201window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004202{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004203 struct surface *surface;
4204
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004205 widget_set_allocation(window->main_surface->widget,
4206 window->pending_allocation.x,
4207 window->pending_allocation.y,
4208 window->pending_allocation.width,
4209 window->pending_allocation.height);
4210
4211 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004212
4213 /* The main surface is in the list, too. Main surface's
4214 * resize_handler is responsible for calling widget_set_allocation()
4215 * on all sub-surface root widgets, so they will be resized
4216 * properly.
4217 */
4218 wl_list_for_each(surface, &window->subsurface_list, link) {
4219 if (surface == window->main_surface)
4220 continue;
4221
4222 surface_set_synchronized(surface);
4223 surface_resize(surface);
4224 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004225
4226 if (!window->fullscreen && !window->maximized)
4227 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004228
4229 if (window->confined && window->confined_widget) {
4230 struct wl_compositor *compositor = window->display->compositor;
4231 struct wl_region *region;
4232 struct widget *widget = window->confined_widget;
4233
4234 region = wl_compositor_create_region(compositor);
4235 wl_region_add(region,
4236 widget->allocation.x,
4237 widget->allocation.y,
4238 widget->allocation.width,
4239 widget->allocation.height);
4240 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4241 region);
4242 wl_region_destroy(region);
4243 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004244}
4245
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004246static void
4247idle_resize(struct window *window)
4248{
4249 window->resize_needed = 0;
4250 window->redraw_needed = 1;
4251
4252 DBG("from %dx%d to %dx%d\n",
4253 window->main_surface->server_allocation.width,
4254 window->main_surface->server_allocation.height,
4255 window->pending_allocation.width,
4256 window->pending_allocation.height);
4257
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004258 window_do_resize(window);
4259}
4260
4261static void
4262undo_resize(struct window *window)
4263{
4264 window->pending_allocation.width =
4265 window->main_surface->server_allocation.width;
4266 window->pending_allocation.height =
4267 window->main_surface->server_allocation.height;
4268
4269 DBG("back to %dx%d\n",
4270 window->main_surface->server_allocation.width,
4271 window->main_surface->server_allocation.height);
4272
4273 window_do_resize(window);
4274
4275 if (window->pending_allocation.width == 0 &&
4276 window->pending_allocation.height == 0) {
4277 fprintf(stderr, "Error: Could not draw a surface, "
4278 "most likely due to insufficient disk space in "
4279 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4280 exit(EXIT_FAILURE);
4281 }
4282}
4283
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004284void
4285window_schedule_resize(struct window *window, int width, int height)
4286{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004287 /* We should probably get these numbers from the theme. */
4288 const int min_width = 200, min_height = 200;
4289
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004290 window->pending_allocation.x = 0;
4291 window->pending_allocation.y = 0;
4292 window->pending_allocation.width = width;
4293 window->pending_allocation.height = height;
4294
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004295 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004296 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004297 window->min_allocation.width = min_width;
4298 else
4299 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004300 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004301 window->min_allocation.height = min_height;
4302 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004303 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004304 }
4305
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004306 if (window->pending_allocation.width < window->min_allocation.width)
4307 window->pending_allocation.width = window->min_allocation.width;
4308 if (window->pending_allocation.height < window->min_allocation.height)
4309 window->pending_allocation.height = window->min_allocation.height;
4310
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004311 window->resize_needed = 1;
4312 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004313}
4314
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004315void
4316widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4317{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004318 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004319}
4320
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004321static int
4322window_get_shadow_margin(struct window *window)
4323{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004324 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004325 return frame_get_shadow_margin(window->frame->frame);
4326 else
4327 return 0;
4328}
4329
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004330void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004331window_inhibit_redraw(struct window *window)
4332{
4333 window->redraw_inhibited = 1;
4334 wl_list_remove(&window->redraw_task.link);
4335 wl_list_init(&window->redraw_task.link);
4336 window->redraw_task_scheduled = 0;
4337}
4338
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004339void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004340window_uninhibit_redraw(struct window *window)
4341{
4342 window->redraw_inhibited = 0;
4343 if (window->redraw_needed || window->resize_needed)
4344 window_schedule_redraw_task(window);
4345}
4346
4347static void
4348xdg_surface_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01004349 struct xdg_surface *xdg_surface,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004350 uint32_t serial)
4351{
4352 struct window *window = data;
4353
ant8mef99fac22018-11-28 22:46:37 +01004354 xdg_surface_ack_configure(window->xdg_surface, serial);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004355
4356 if (window->state_changed_handler)
4357 window->state_changed_handler(window, window->user_data);
4358
4359 window_uninhibit_redraw(window);
4360}
4361
ant8mef99fac22018-11-28 22:46:37 +01004362static const struct xdg_surface_listener xdg_surface_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004363 xdg_surface_handle_configure
4364};
4365
4366static void
ant8mef99fac22018-11-28 22:46:37 +01004367xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004368 int32_t width, int32_t height,
4369 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004370{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004371 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004372 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004373
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004374 window->maximized = 0;
4375 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004376 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004377 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004378
4379 wl_array_for_each(p, states) {
4380 uint32_t state = *p;
4381 switch (state) {
ant8mef99fac22018-11-28 22:46:37 +01004382 case XDG_TOPLEVEL_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004383 window->maximized = 1;
4384 break;
ant8mef99fac22018-11-28 22:46:37 +01004385 case XDG_TOPLEVEL_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004386 window->fullscreen = 1;
4387 break;
ant8mef99fac22018-11-28 22:46:37 +01004388 case XDG_TOPLEVEL_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004389 window->resizing = 1;
4390 break;
ant8mef99fac22018-11-28 22:46:37 +01004391 case XDG_TOPLEVEL_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004392 window->focused = 1;
4393 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004394 default:
4395 /* Unknown state */
4396 break;
4397 }
4398 }
4399
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004400 if (window->frame) {
4401 if (window->maximized) {
4402 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4403 } else {
4404 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4405 }
4406
4407 if (window->focused) {
4408 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4409 } else {
4410 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4411 }
4412 }
4413
Jasper St. Pierref184c382014-05-06 08:33:27 -04004414 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004415 /* The width / height params are for window geometry,
4416 * but window_schedule_resize takes allocation. Add
4417 * on the shadow margin to get the difference. */
4418 int margin = window_get_shadow_margin(window);
4419
4420 window_schedule_resize(window,
4421 width + margin * 2,
4422 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004423 } else if (window->saved_allocation.width > 0 &&
4424 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004425 window_schedule_resize(window,
4426 window->saved_allocation.width,
4427 window->saved_allocation.height);
4428 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004429}
4430
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004431static void
ant8mef99fac22018-11-28 22:46:37 +01004432xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004433{
4434 struct window *window = data;
4435 window_close(window);
4436}
4437
ant8mef99fac22018-11-28 22:46:37 +01004438static const struct xdg_toplevel_listener xdg_toplevel_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004439 xdg_toplevel_handle_configure,
4440 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004441};
4442
4443static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004444window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004445{
ant8mef99fac22018-11-28 22:46:37 +01004446 struct xdg_toplevel *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004447
4448 if (!window->xdg_surface)
4449 return;
4450
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004451 if (window->parent == window->last_parent)
4452 return;
4453
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004454 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004455 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004456 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004457 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004458
ant8mef99fac22018-11-28 22:46:37 +01004459 xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004460 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004461}
4462
4463static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004464window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004465{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004466 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004467 frame_input_rect(window->frame->frame,
4468 &geometry->x,
4469 &geometry->y,
4470 &geometry->width,
4471 &geometry->height);
4472 else
4473 window_get_allocation(window, geometry);
4474}
4475
4476static void
4477window_sync_geometry(struct window *window)
4478{
4479 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004480
4481 if (!window->xdg_surface)
4482 return;
4483
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004484 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004485 if (geometry.x == window->last_geometry.x &&
4486 geometry.y == window->last_geometry.y &&
4487 geometry.width == window->last_geometry.width &&
4488 geometry.height == window->last_geometry.height)
4489 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004490
ant8mef99fac22018-11-28 22:46:37 +01004491 xdg_surface_set_window_geometry(window->xdg_surface,
4492 geometry.x,
4493 geometry.y,
4494 geometry.width,
4495 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004496 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004497}
4498
4499static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004500window_flush(struct window *window)
4501{
4502 struct surface *surface;
4503
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004504 assert(!window->redraw_inhibited);
4505
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004506 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004507 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004508 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004509 if (window->xdg_toplevel)
4510 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004511 }
4512
4513 wl_list_for_each(surface, &window->subsurface_list, link) {
4514 if (surface == window->main_surface)
4515 continue;
4516
4517 surface_flush(surface);
4518 }
4519
4520 surface_flush(window->main_surface);
4521}
4522
4523static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004524menu_destroy(struct menu *menu)
4525{
4526 widget_destroy(menu->widget);
4527 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004528 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004529 free(menu);
4530}
4531
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004532void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004533window_get_allocation(struct window *window,
4534 struct rectangle *allocation)
4535{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004536 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004537}
4538
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004539static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004540widget_redraw(struct widget *widget)
4541{
4542 struct widget *child;
4543
4544 if (widget->redraw_handler)
4545 widget->redraw_handler(widget, widget->user_data);
4546 wl_list_for_each(child, &widget->child_list, link)
4547 widget_redraw(child);
4548}
4549
4550static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004551frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4552{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004553 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004554
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004555 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004556 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004557 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004558 surface->frame_cb = NULL;
4559
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004560 surface->last_time = time;
4561
Pekka Paalanen71233882013-04-25 13:57:53 +03004562 if (surface->redraw_needed || surface->window->redraw_needed) {
4563 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004564 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004565 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004566}
4567
4568static const struct wl_callback_listener listener = {
4569 frame_callback
4570};
4571
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004572static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004573surface_redraw(struct surface *surface)
4574{
Pekka Paalanen71233882013-04-25 13:57:53 +03004575 DBG_OBJ(surface->surface, "begin\n");
4576
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004577 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004578 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004579
4580 /* Whole-window redraw forces a redraw even if the previous has
4581 * not yet hit the screen.
4582 */
4583 if (surface->frame_cb) {
4584 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004585 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004586
Pekka Paalanen71233882013-04-25 13:57:53 +03004587 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004588 wl_callback_destroy(surface->frame_cb);
4589 }
4590
Neil Roberts97b747c2013-12-19 16:17:12 +00004591 if (surface->widget->use_cairo &&
4592 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004593 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004594 return -1;
4595 }
4596
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004597 surface->frame_cb = wl_surface_frame(surface->surface);
4598 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004599 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004600
4601 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004602 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004603 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004604 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004605 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004606}
4607
4608static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004609idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004610{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004611 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004612 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004613 int failed = 0;
4614 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004615
Pekka Paalanen71233882013-04-25 13:57:53 +03004616 DBG(" --------- \n");
4617
Pekka Paalaneneebff542013-04-25 13:57:52 +03004618 wl_list_init(&window->redraw_task.link);
4619 window->redraw_task_scheduled = 0;
4620
4621 if (window->resize_needed) {
4622 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004623 if (window->main_surface->frame_cb) {
4624 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004625 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004626 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004627
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004628 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004629 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004630 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004631
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004632 if (surface_redraw(window->main_surface) < 0) {
4633 /*
4634 * Only main_surface failure will cause us to undo the resize.
4635 * If sub-surfaces fail, they will just be broken with old
4636 * content.
4637 */
4638 failed = 1;
4639 } else {
4640 wl_list_for_each(surface, &window->subsurface_list, link) {
4641 if (surface == window->main_surface)
4642 continue;
4643
4644 surface_redraw(surface);
4645 }
4646 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004647
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004648 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004649 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004650
4651 wl_list_for_each(surface, &window->subsurface_list, link)
4652 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004653
4654 if (resized && failed) {
4655 /* Restore widget tree to correspond to what is on screen. */
4656 undo_resize(window);
4657 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004658}
4659
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004660static void
4661window_schedule_redraw_task(struct window *window)
4662{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004663 if (window->redraw_inhibited)
4664 return;
4665
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004666 if (!window->redraw_task_scheduled) {
4667 window->redraw_task.run = idle_redraw;
4668 display_defer(window->display, &window->redraw_task);
4669 window->redraw_task_scheduled = 1;
4670 }
4671}
4672
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004673void
4674window_schedule_redraw(struct window *window)
4675{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004676 struct surface *surface;
4677
Pekka Paalanen71233882013-04-25 13:57:53 +03004678 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4679
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004680 wl_list_for_each(surface, &window->subsurface_list, link)
4681 surface->redraw_needed = 1;
4682
4683 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004684}
4685
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004686int
4687window_is_fullscreen(struct window *window)
4688{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004689 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004690}
4691
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004692void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004693window_set_fullscreen(struct window *window, int fullscreen)
4694{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004695 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004696 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004697
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004698 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004699 return;
4700
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004701 if (fullscreen)
ant8mef99fac22018-11-28 22:46:37 +01004702 xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004703 else
ant8mef99fac22018-11-28 22:46:37 +01004704 xdg_toplevel_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004705}
4706
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004707int
4708window_is_maximized(struct window *window)
4709{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004710 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004711}
4712
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004713void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004714window_set_maximized(struct window *window, int maximized)
4715{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004716 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004717 return;
4718
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004719 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004720 return;
4721
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004722 if (maximized)
ant8mef99fac22018-11-28 22:46:37 +01004723 xdg_toplevel_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004724 else
ant8mef99fac22018-11-28 22:46:37 +01004725 xdg_toplevel_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004726}
4727
Jasper St. Pierrede680992014-04-10 17:23:49 -07004728int
4729window_is_resizing(struct window *window)
4730{
4731 return window->resizing;
4732}
4733
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004734void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004735window_set_minimized(struct window *window)
4736{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004737 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004738 return;
4739
ant8mef99fac22018-11-28 22:46:37 +01004740 xdg_toplevel_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004741}
4742
4743void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004744window_set_user_data(struct window *window, void *data)
4745{
4746 window->user_data = data;
4747}
4748
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004749void *
4750window_get_user_data(struct window *window)
4751{
4752 return window->user_data;
4753}
4754
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004755void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004756window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004757 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004758{
4759 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004760}
4761
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004762void
4763window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004764 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004765{
4766 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004767}
4768
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004769void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004770window_set_data_handler(struct window *window, window_data_handler_t handler)
4771{
4772 window->data_handler = handler;
4773}
4774
4775void
4776window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4777{
4778 window->drop_handler = handler;
4779}
4780
4781void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004782window_set_close_handler(struct window *window,
4783 window_close_handler_t handler)
4784{
4785 window->close_handler = handler;
4786}
4787
4788void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004789window_set_fullscreen_handler(struct window *window,
4790 window_fullscreen_handler_t handler)
4791{
4792 window->fullscreen_handler = handler;
4793}
4794
4795void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004796window_set_output_handler(struct window *window,
4797 window_output_handler_t handler)
4798{
4799 window->output_handler = handler;
4800}
4801
4802void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004803window_set_state_changed_handler(struct window *window,
4804 window_state_changed_handler_t handler)
4805{
4806 window->state_changed_handler = handler;
4807}
4808
4809void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004810window_set_pointer_locked_handler(struct window *window,
4811 locked_pointer_locked_handler_t locked,
4812 locked_pointer_unlocked_handler_t unlocked)
4813{
4814 window->pointer_unlocked_handler = unlocked;
4815 window->pointer_locked_handler = locked;
4816}
4817
4818void
4819window_set_pointer_confined_handler(struct window *window,
4820 confined_pointer_confined_handler_t confined,
4821 confined_pointer_unconfined_handler_t unconfined)
4822{
4823 window->pointer_confined_handler = confined;
4824 window->pointer_unconfined_handler = unconfined;
4825}
4826
4827void
4828window_set_locked_pointer_motion_handler(struct window *window,
4829 window_locked_pointer_motion_handler_t handler)
4830{
4831 window->locked_pointer_motion_handler = handler;
4832}
4833
4834void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004835window_set_title(struct window *window, const char *title)
4836{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004837 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004838 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004839 if (window->frame) {
4840 frame_set_title(window->frame->frame, title);
4841 widget_schedule_redraw(window->frame->widget);
4842 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004843 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01004844 xdg_toplevel_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004845}
4846
4847const char *
4848window_get_title(struct window *window)
4849{
4850 return window->title;
4851}
4852
4853void
Marius Vladb3544c22021-09-09 13:52:18 +03004854window_set_appid(struct window *window, const char *appid)
4855{
4856 assert(!window->appid);
4857 window->appid = strdup(appid);
4858
4859 if (window->xdg_toplevel)
4860 xdg_toplevel_set_app_id(window->xdg_toplevel, window->appid);
4861}
4862
4863const char *
4864window_get_appid(struct window *window)
4865{
4866 return window->appid;
4867}
4868
4869void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004870window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4871{
4872 struct text_cursor_position *text_cursor_position =
4873 window->display->text_cursor_position;
4874
Scott Moreau9295ce02012-06-01 12:46:10 -06004875 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004876 return;
4877
4878 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004879 window->main_surface->surface,
4880 wl_fixed_from_int(x),
4881 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004882}
4883
Casey Dahlin9074db52012-04-19 22:50:09 -04004884static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004885relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4886 uint32_t utime_hi,
4887 uint32_t utime_lo,
4888 wl_fixed_t dx,
4889 wl_fixed_t dy,
4890 wl_fixed_t dx_unaccel,
4891 wl_fixed_t dy_unaccel)
4892{
4893 struct input *input = data;
4894 struct window *window = input->pointer_focus;
4895 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4896
4897 if (window->locked_pointer_motion_handler &&
4898 window->pointer_locked) {
4899 window->locked_pointer_motion_handler(
4900 window, input, ms,
4901 wl_fixed_to_double(dx),
4902 wl_fixed_to_double(dy),
4903 window->user_data);
4904 }
4905}
4906
4907static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4908 relative_pointer_handle_motion,
4909};
4910
4911static void
4912locked_pointer_locked(void *data,
4913 struct zwp_locked_pointer_v1 *locked_pointer)
4914{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004915 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004916 struct window *window = input->locked_window;
4917
4918 if (!window)
4919 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004920
4921 window->pointer_locked = true;
4922
4923 if (window->pointer_locked_handler) {
4924 window->pointer_locked_handler(window,
4925 input,
4926 window->user_data);
4927 }
4928}
4929
4930static void
4931locked_pointer_unlocked(void *data,
4932 struct zwp_locked_pointer_v1 *locked_pointer)
4933{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004934 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004935 struct window *window = input->locked_window;
4936
4937 if (!window)
4938 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004939
4940 window_unlock_pointer(window);
4941
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004942 input->locked_window = NULL;
4943
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004944 if (window->pointer_unlocked_handler) {
4945 window->pointer_unlocked_handler(window,
4946 input,
4947 window->user_data);
4948 }
4949}
4950
4951static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4952 locked_pointer_locked,
4953 locked_pointer_unlocked,
4954};
4955
4956int
4957window_lock_pointer(struct window *window, struct input *input)
4958{
4959 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4960 window->display->relative_pointer_manager;
4961 struct zwp_pointer_constraints_v1 *pointer_constraints =
4962 window->display->pointer_constraints;
4963 struct zwp_relative_pointer_v1 *relative_pointer;
4964 struct zwp_locked_pointer_v1 *locked_pointer;
4965
4966 if (!window->display->relative_pointer_manager)
4967 return -1;
4968
4969 if (!window->display->pointer_constraints)
4970 return -1;
4971
4972 if (window->locked_pointer)
4973 return -1;
4974
4975 if (window->confined_pointer)
4976 return -1;
4977
4978 if (!input->pointer)
4979 return -1;
4980
4981 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4982 relative_pointer_manager, input->pointer);
4983 zwp_relative_pointer_v1_add_listener(relative_pointer,
4984 &relative_pointer_listener,
4985 input);
4986
4987 locked_pointer =
4988 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4989 window->main_surface->surface,
4990 input->pointer,
4991 NULL,
4992 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4993 zwp_locked_pointer_v1_add_listener(locked_pointer,
4994 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004995 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004996
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004997 window->locked_pointer = locked_pointer;
4998 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004999 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005000
5001 return 0;
5002}
5003
5004void
5005window_unlock_pointer(struct window *window)
5006{
5007 if (!window->locked_pointer)
5008 return;
5009
5010 zwp_locked_pointer_v1_destroy(window->locked_pointer);
5011 zwp_relative_pointer_v1_destroy(window->relative_pointer);
5012 window->locked_pointer = NULL;
5013 window->relative_pointer = NULL;
5014 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005015}
5016
5017void
5018widget_set_locked_pointer_cursor_hint(struct widget *widget,
5019 float x, float y)
5020{
5021 struct window *window = widget->window;
5022
5023 if (!window->locked_pointer)
5024 return;
5025
5026 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
5027 wl_fixed_from_double(x),
5028 wl_fixed_from_double(y));
5029 wl_surface_commit(window->main_surface->surface);
5030}
5031
5032static void
5033confined_pointer_confined(void *data,
5034 struct zwp_confined_pointer_v1 *confined_pointer)
5035{
Dima Ryazanov336ce672018-11-14 21:03:35 -08005036 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005037 struct window *window = input->confined_window;
5038
5039 if (!window)
5040 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005041
5042 window->confined = true;
5043
5044 if (window->pointer_confined_handler) {
5045 window->pointer_confined_handler(window,
5046 input,
5047 window->user_data);
5048 }
5049}
5050
5051static void
5052confined_pointer_unconfined(void *data,
5053 struct zwp_confined_pointer_v1 *confined_pointer)
5054{
Dima Ryazanov336ce672018-11-14 21:03:35 -08005055 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005056 struct window *window = input->confined_window;
5057
5058 if (!window)
5059 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005060
5061 window_unconfine_pointer(window);
5062
5063 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005064 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005065
5066 if (window->pointer_unconfined_handler) {
5067 window->pointer_unconfined_handler(window,
5068 input,
5069 window->user_data);
5070 }
5071}
5072
5073static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
5074 confined_pointer_confined,
5075 confined_pointer_unconfined,
5076};
5077
5078int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005079window_confine_pointer_to_rectangles(struct window *window,
5080 struct input *input,
5081 struct rectangle *rectangles,
5082 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005083{
5084 struct zwp_pointer_constraints_v1 *pointer_constraints =
5085 window->display->pointer_constraints;
5086 struct zwp_confined_pointer_v1 *confined_pointer;
5087 struct wl_compositor *compositor = window->display->compositor;
5088 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005089 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005090
5091 if (!window->display->pointer_constraints)
5092 return -1;
5093
5094 if (window->locked_pointer)
5095 return -1;
5096
5097 if (window->confined_pointer)
5098 return -1;
5099
5100 if (!input->pointer)
5101 return -1;
5102
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005103 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005104 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005105 for (i = 0; i < num_rectangles; i++) {
5106 wl_region_add(region,
5107 rectangles[i].x,
5108 rectangles[i].y,
5109 rectangles[i].width,
5110 rectangles[i].height);
5111 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005112 }
5113
5114 confined_pointer =
5115 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5116 window->main_surface->surface,
5117 input->pointer,
5118 region,
5119 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5120 if (region)
5121 wl_region_destroy(region);
5122
5123 zwp_confined_pointer_v1_add_listener(confined_pointer,
5124 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08005125 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005126
5127 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005128 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005129 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005130
5131 return 0;
5132}
5133
5134void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005135window_update_confine_rectangles(struct window *window,
5136 struct rectangle *rectangles,
5137 int num_rectangles)
5138{
5139 struct wl_compositor *compositor = window->display->compositor;
5140 struct wl_region *region;
5141 int i;
5142
5143 region = wl_compositor_create_region(compositor);
5144 for (i = 0; i < num_rectangles; i++) {
5145 wl_region_add(region,
5146 rectangles[i].x,
5147 rectangles[i].y,
5148 rectangles[i].width,
5149 rectangles[i].height);
5150 }
5151
5152 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5153
5154 wl_region_destroy(region);
5155}
5156
5157int
5158window_confine_pointer_to_widget(struct window *window,
5159 struct widget *widget,
5160 struct input *input)
5161{
5162 int ret;
5163
5164 if (widget) {
5165 ret = window_confine_pointer_to_rectangles(window,
5166 input,
5167 &widget->allocation,
5168 1);
5169 window->confined_widget = widget;
5170 return ret;
5171 } else {
5172 return window_confine_pointer_to_rectangles(window,
5173 input,
5174 NULL,
5175 0);
5176 }
5177}
5178
5179void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005180window_unconfine_pointer(struct window *window)
5181{
5182 if (!window->confined_pointer)
5183 return;
5184
5185 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5186 window->confined_pointer = NULL;
5187 window->confined = false;
5188}
5189
5190static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005191surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005192 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005193{
Rob Bradford7507b572012-05-15 17:55:34 +01005194 struct window *window = data;
5195 struct output *output;
5196 struct output *output_found = NULL;
5197 struct window_output *window_output;
5198
5199 wl_list_for_each(output, &window->display->output_list, link) {
5200 if (output->output == wl_output) {
5201 output_found = output;
5202 break;
5203 }
5204 }
5205
5206 if (!output_found)
5207 return;
5208
Brian Lovinbc919262013-08-07 15:34:59 -07005209 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005210 window_output->output = output_found;
5211
5212 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005213
5214 if (window->output_handler)
5215 window->output_handler(window, output_found, 1,
5216 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005217}
5218
5219static void
5220surface_leave(void *data,
5221 struct wl_surface *wl_surface, struct wl_output *output)
5222{
Rob Bradford7507b572012-05-15 17:55:34 +01005223 struct window *window = data;
5224 struct window_output *window_output;
5225 struct window_output *window_output_found = NULL;
5226
5227 wl_list_for_each(window_output, &window->window_output_list, link) {
5228 if (window_output->output->output == output) {
5229 window_output_found = window_output;
5230 break;
5231 }
5232 }
5233
5234 if (window_output_found) {
5235 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005236
5237 if (window->output_handler)
5238 window->output_handler(window, window_output->output,
5239 0, window->user_data);
5240
Rob Bradford7507b572012-05-15 17:55:34 +01005241 free(window_output_found);
5242 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005243}
5244
5245static const struct wl_surface_listener surface_listener = {
5246 surface_enter,
5247 surface_leave
5248};
5249
Pekka Paalanen4e373742013-02-13 16:17:13 +02005250static struct surface *
5251surface_create(struct window *window)
5252{
5253 struct display *display = window->display;
5254 struct surface *surface;
5255
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005256 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005257 surface->window = window;
5258 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005259 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005260 wl_surface_add_listener(surface->surface, &surface_listener, window);
5261
Pekka Paalanen35e82632013-04-25 13:57:48 +03005262 wl_list_insert(&window->subsurface_list, &surface->link);
Harish Krupo7bcbab12018-12-11 13:45:43 +05305263 surface->viewport = NULL;
Pekka Paalanen35e82632013-04-25 13:57:48 +03005264
Pekka Paalanen4e373742013-02-13 16:17:13 +02005265 return surface;
5266}
5267
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005268static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005269get_preferred_buffer_type(struct display *display)
5270{
5271#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005272 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005273 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5274#endif
5275
5276 return WINDOW_BUFFER_TYPE_SHM;
5277}
5278
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005279static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005280window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005281{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005282 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005283 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005284
Peter Huttererf3d62272013-08-08 11:57:05 +10005285 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005286 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005287 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005288
5289 surface = surface_create(window);
5290 window->main_surface = surface;
5291
Michael Teyfeld6371b52017-10-11 17:17:29 +02005292 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005293
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005294 window->custom = custom;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005295
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005296 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005297
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005298 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005299 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005300 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005301
Rob Bradford7507b572012-05-15 17:55:34 +01005302 wl_list_init (&window->window_output_list);
5303
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005304 return window;
5305}
5306
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005307struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005308window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005309{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005310 struct window *window;
5311
5312 window = window_create_internal(display, 0);
5313
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005314 if (window->display->xdg_shell) {
5315 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005316 xdg_wm_base_get_xdg_surface(window->display->xdg_shell,
5317 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005318 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005319
ant8mef99fac22018-11-28 22:46:37 +01005320 xdg_surface_add_listener(window->xdg_surface,
5321 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005322
5323 window->xdg_toplevel =
ant8mef99fac22018-11-28 22:46:37 +01005324 xdg_surface_get_toplevel(window->xdg_surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005325 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5326
ant8mef99fac22018-11-28 22:46:37 +01005327 xdg_toplevel_add_listener(window->xdg_toplevel,
5328 &xdg_toplevel_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005329
5330 window_inhibit_redraw(window);
5331
5332 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005333 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005334
5335 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005336}
5337
5338struct window *
5339window_create_custom(struct display *display)
5340{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005341 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005342}
5343
Jasper St. Pierre53686042013-12-09 15:26:25 -05005344void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005345window_set_parent(struct window *window,
5346 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005347{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005348 window->parent = parent_window;
5349 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005350}
5351
5352struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005353window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005354{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005355 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005356}
5357
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005358static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005359menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005360{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005361 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005362 int next;
5363
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005364 frame_interior(menu->frame, &x, &y, &width, &height);
5365 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005366 if (menu->current != next) {
5367 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005368 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005369 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005370}
5371
5372static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005373menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005374 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005375 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005376{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005377 struct menu *menu = data;
5378
5379 if (widget == menu->widget)
5380 menu_set_item(data, y);
5381
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005382 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005383}
5384
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005385static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005386menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005387 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005388{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005389 struct menu *menu = data;
5390
5391 if (widget == menu->widget)
5392 menu_set_item(data, y);
5393
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005394 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005395}
5396
5397static void
5398menu_leave_handler(struct widget *widget, struct input *input, void *data)
5399{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005400 struct menu *menu = data;
5401
5402 if (widget == menu->widget)
5403 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005404}
5405
5406static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005407menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005408 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005409 uint32_t button, enum wl_pointer_button_state state,
5410 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005411
5412{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005413 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005414
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005415 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5416 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005417 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005418 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005419 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005420 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005421 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005422 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005423 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005424 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005425}
5426
5427static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005428menu_touch_up_handler(struct widget *widget,
5429 struct input *input,
5430 uint32_t serial,
5431 uint32_t time,
5432 int32_t id,
5433 void *data)
5434{
5435 struct menu *menu = data;
5436
5437 input_ungrab(input);
5438 menu_destroy(menu);
5439}
5440
5441static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005442menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005443{
5444 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005445 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005446 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005447
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005448 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005449
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005450 frame_repaint(menu->frame, cr);
5451 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005452
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005453 theme_set_background_source(menu->window->display->theme,
5454 cr, THEME_FRAME_ACTIVE);
5455 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005456 cairo_fill(cr);
5457
Alyssa Ross7c121822021-11-17 17:25:17 +00005458 cairo_select_font_face(cr, "sans-serif",
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005459 CAIRO_FONT_SLANT_NORMAL,
5460 CAIRO_FONT_WEIGHT_NORMAL);
5461 cairo_set_font_size(cr, 12);
5462
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005463 for (i = 0; i < menu->count; i++) {
5464 if (i == menu->current) {
5465 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005466 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005467 cairo_fill(cr);
5468 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005469 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005470 cairo_show_text(cr, menu->entries[i]);
5471 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005472 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5473 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005474 cairo_show_text(cr, menu->entries[i]);
5475 }
5476 }
5477
5478 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005479}
5480
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005481static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005482xdg_popup_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01005483 struct xdg_popup *xdg_popup,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005484 int32_t x,
5485 int32_t y,
5486 int32_t width,
5487 int32_t height)
5488{
5489}
5490
5491static void
ant8mef99fac22018-11-28 22:46:37 +01005492xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005493{
5494 struct window *window = data;
5495 struct menu *menu = window->main_surface->widget->user_data;
5496
5497 input_ungrab(menu->input);
5498 menu_destroy(menu);
5499}
5500
ant8mef99fac22018-11-28 22:46:37 +01005501static const struct xdg_popup_listener xdg_popup_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005502 xdg_popup_handle_configure,
5503 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005504};
5505
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005506static struct menu *
5507create_menu(struct display *display,
5508 struct input *input, uint32_t time,
5509 menu_func_t func, const char **entries, int count,
5510 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005511{
5512 struct window *window;
5513 struct menu *menu;
5514
5515 menu = malloc(sizeof *menu);
5516 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005517 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005518
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005519 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005520 if (!window) {
5521 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005522 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005523 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005524
5525 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005526 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005527 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005528 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005529 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005530 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005531 menu->entries = entries;
5532 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005533 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005534 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005535 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005536 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005537 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005538
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005539 input_ungrab(input);
5540
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005541 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005542 widget_set_enter_handler(menu->widget, menu_enter_handler);
5543 widget_set_leave_handler(menu->widget, menu_leave_handler);
5544 widget_set_motion_handler(menu->widget, menu_motion_handler);
5545 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005546 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005547
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005548 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005549 frame_resize_inside(menu->frame, 200, count * 20);
5550 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5551 window_schedule_resize(window, frame_width(menu->frame),
5552 frame_height(menu->frame));
5553
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005554 return menu;
5555}
5556
ant8mef99fac22018-11-28 22:46:37 +01005557static struct xdg_positioner *
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005558create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005559 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005560{
ant8mef99fac22018-11-28 22:46:37 +01005561 struct xdg_positioner *positioner;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005562
ant8mef99fac22018-11-28 22:46:37 +01005563 positioner = xdg_wm_base_create_positioner(display->xdg_shell);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005564 fail_on_null(positioner, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005565 xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1);
5566 xdg_positioner_set_size(positioner, w, h);
5567 xdg_positioner_set_anchor(positioner,
5568 XDG_POSITIONER_ANCHOR_TOP_LEFT);
5569 xdg_positioner_set_gravity(positioner,
5570 XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005571
5572 return positioner;
5573}
5574
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005575void
5576window_show_menu(struct display *display,
5577 struct input *input, uint32_t time, struct window *parent,
5578 int32_t x, int32_t y,
5579 menu_func_t func, const char **entries, int count)
5580{
5581 struct menu *menu;
5582 struct window *window;
5583 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005584 struct rectangle parent_geometry;
ant8mef99fac22018-11-28 22:46:37 +01005585 struct xdg_positioner *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005586
5587 menu = create_menu(display, input, time, func, entries, count, parent);
5588
5589 if (menu == NULL)
5590 return;
5591
5592 window = menu->window;
5593
5594 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5595 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5596
5597 window->x = x;
5598 window->y = y;
5599
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005600 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005601 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005602
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005603 if (!display->xdg_shell)
5604 return;
5605
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005606 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005607 xdg_wm_base_get_xdg_surface(display->xdg_shell,
5608 window->main_surface->surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005609 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005610
ant8mef99fac22018-11-28 22:46:37 +01005611 xdg_surface_add_listener(window->xdg_surface,
5612 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005613
5614 positioner = create_simple_positioner(display,
5615 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005616 window->y - (iy + parent_geometry.y),
5617 frame_width(menu->frame),
5618 frame_height(menu->frame));
ant8mef99fac22018-11-28 22:46:37 +01005619 window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
5620 parent->xdg_surface,
5621 positioner);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005622 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005623 xdg_positioner_destroy(positioner);
5624 xdg_popup_grab(window->xdg_popup, input->seat,
5625 display_get_serial(window->display));
5626 xdg_popup_add_listener(window->xdg_popup,
5627 &xdg_popup_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005628
5629 window_inhibit_redraw(window);
5630
5631 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005632}
5633
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005634void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005635window_set_buffer_type(struct window *window, enum window_buffer_type type)
5636{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005637 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005638}
5639
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005640enum window_buffer_type
5641window_get_buffer_type(struct window *window)
5642{
5643 return window->main_surface->buffer_type;
5644}
5645
Pekka Paalanen35e82632013-04-25 13:57:48 +03005646struct widget *
5647window_add_subsurface(struct window *window, void *data,
5648 enum subsurface_mode default_mode)
5649{
5650 struct widget *widget;
5651 struct surface *surface;
5652 struct wl_surface *parent;
5653 struct wl_subcompositor *subcompo = window->display->subcompositor;
5654
Pekka Paalanen35e82632013-04-25 13:57:48 +03005655 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005656 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005657 widget = widget_create(window, surface, data);
5658 wl_list_init(&widget->link);
5659 surface->widget = widget;
5660
5661 parent = window->main_surface->surface;
5662 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5663 surface->surface,
5664 parent);
5665 surface->synchronized = 1;
5666
5667 switch (default_mode) {
5668 case SUBSURFACE_SYNCHRONIZED:
5669 surface->synchronized_default = 1;
5670 break;
5671 case SUBSURFACE_DESYNCHRONIZED:
5672 surface->synchronized_default = 0;
5673 break;
5674 default:
5675 assert(!"bad enum subsurface_mode");
5676 }
5677
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005678 window->resize_needed = 1;
5679 window_schedule_redraw(window);
5680
Pekka Paalanen35e82632013-04-25 13:57:48 +03005681 return widget;
5682}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005683
5684static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005685display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005686 struct wl_output *wl_output,
5687 int x, int y,
5688 int physical_width,
5689 int physical_height,
5690 int subpixel,
5691 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005692 const char *model,
5693 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005694{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005695 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005696
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005697 output->allocation.x = x;
5698 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005699 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005700
5701 if (output->make)
5702 free(output->make);
5703 output->make = strdup(make);
5704
5705 if (output->model)
5706 free(output->model);
5707 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005708}
5709
5710static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005711display_handle_done(void *data,
5712 struct wl_output *wl_output)
5713{
5714}
5715
5716static void
5717display_handle_scale(void *data,
5718 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005719 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005720{
5721 struct output *output = data;
5722
5723 output->scale = scale;
5724}
5725
5726static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005727display_handle_mode(void *data,
5728 struct wl_output *wl_output,
5729 uint32_t flags,
5730 int width,
5731 int height,
5732 int refresh)
5733{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005734 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005735 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005736
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005737 if (flags & WL_OUTPUT_MODE_CURRENT) {
5738 output->allocation.width = width;
5739 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005740 if (display->output_configure_handler)
5741 (*display->output_configure_handler)(
5742 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005743 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005744}
5745
5746static const struct wl_output_listener output_listener = {
5747 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005748 display_handle_mode,
5749 display_handle_done,
5750 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005751};
5752
5753static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005754display_add_output(struct display *d, uint32_t id)
5755{
5756 struct output *output;
5757
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005758 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005759 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005760 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005761 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005762 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005763 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005764 wl_list_insert(d->output_list.prev, &output->link);
5765
5766 wl_output_add_listener(output->output, &output_listener, output);
5767}
5768
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005769static void
5770output_destroy(struct output *output)
5771{
5772 if (output->destroy_handler)
5773 (*output->destroy_handler)(output, output->user_data);
5774
5775 wl_output_destroy(output->output);
5776 wl_list_remove(&output->link);
Lujin Wang928d3a02019-08-12 12:22:23 -07005777 free(output->make);
5778 free(output->model);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005779 free(output);
5780}
5781
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005782static void
5783display_destroy_output(struct display *d, uint32_t id)
5784{
5785 struct output *output;
5786
5787 wl_list_for_each(output, &d->output_list, link) {
5788 if (output->server_output_id == id) {
5789 output_destroy(output);
5790 break;
5791 }
5792 }
5793}
5794
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005795void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005796display_set_global_handler(struct display *display,
5797 display_global_handler_t handler)
5798{
5799 struct global *global;
5800
5801 display->global_handler = handler;
5802 if (!handler)
5803 return;
5804
5805 wl_list_for_each(global, &display->global_list, link)
5806 display->global_handler(display,
5807 global->name, global->interface,
5808 global->version, display->user_data);
5809}
5810
5811void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005812display_set_global_handler_remove(struct display *display,
5813 display_global_handler_t remove_handler)
5814{
5815 display->global_handler_remove = remove_handler;
5816 if (!remove_handler)
5817 return;
5818}
5819
5820void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005821display_set_output_configure_handler(struct display *display,
5822 display_output_handler_t handler)
5823{
5824 struct output *output;
5825
5826 display->output_configure_handler = handler;
5827 if (!handler)
5828 return;
5829
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005830 wl_list_for_each(output, &display->output_list, link) {
5831 if (output->allocation.width == 0 &&
5832 output->allocation.height == 0)
5833 continue;
5834
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005835 (*display->output_configure_handler)(output,
5836 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005837 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005838}
5839
5840void
5841output_set_user_data(struct output *output, void *data)
5842{
5843 output->user_data = data;
5844}
5845
5846void *
5847output_get_user_data(struct output *output)
5848{
5849 return output->user_data;
5850}
5851
5852void
5853output_set_destroy_handler(struct output *output,
5854 display_output_handler_t handler)
5855{
5856 output->destroy_handler = handler;
5857 /* FIXME: implement this, once we have way to remove outputs */
5858}
5859
5860void
Scott Moreau4e072362012-09-29 02:03:11 -06005861output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005862{
Scott Moreau4e072362012-09-29 02:03:11 -06005863 struct rectangle allocation = output->allocation;
5864
5865 switch (output->transform) {
5866 case WL_OUTPUT_TRANSFORM_90:
5867 case WL_OUTPUT_TRANSFORM_270:
5868 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5869 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5870 /* Swap width and height */
5871 allocation.width = output->allocation.height;
5872 allocation.height = output->allocation.width;
5873 break;
5874 }
5875
5876 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005877}
5878
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005879struct wl_output *
5880output_get_wl_output(struct output *output)
5881{
5882 return output->output;
5883}
5884
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005885enum wl_output_transform
5886output_get_transform(struct output *output)
5887{
5888 return output->transform;
5889}
5890
Alexander Larssonafd319a2013-05-22 14:41:27 +02005891uint32_t
5892output_get_scale(struct output *output)
5893{
5894 return output->scale;
5895}
5896
Jason Ekstrand738715d2014-04-02 19:53:50 -05005897const char *
5898output_get_make(struct output *output)
5899{
5900 return output->make;
5901}
5902
5903const char *
5904output_get_model(struct output *output)
5905{
5906 return output->model;
5907}
5908
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005909static void
Daniel Stone97f68542012-05-30 16:32:01 +01005910fini_xkb(struct input *input)
5911{
Pekka Paalanen2af436b2021-05-25 14:10:33 +03005912 xkb_compose_state_unref(input->xkb.compose_state);
5913 xkb_compose_table_unref(input->xkb.compose_table);
Daniel Stone97f68542012-05-30 16:32:01 +01005914 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005915 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005916}
5917
5918static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005919display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005920{
5921 struct input *input;
Sebastian Wickac1b92d2019-11-01 02:27:55 +01005922 int seat_version = MIN(display_seat_version, 7);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005923
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005924 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005925 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005926 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005927 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005928 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005929 input->pointer_focus = NULL;
5930 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005931 input->seat_version = seat_version;
leng.fang32af9fc2024-06-13 11:22:15 +08005932 input->pointer_hidden = true;
5933 input->pointer_last_motion_time = 0;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005934
Rusty Lynch041815a2013-08-08 21:20:38 -07005935 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005936 wl_list_insert(d->input_list.prev, &input->link);
5937
Daniel Stone37816df2012-05-16 18:45:18 +01005938 wl_seat_add_listener(input->seat, &seat_listener, input);
5939 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005940
Jason Ekstranda669bd52014-04-02 19:53:51 -05005941 if (d->data_device_manager) {
5942 input->data_device =
5943 wl_data_device_manager_get_data_device(d->data_device_manager,
5944 input->seat);
5945 wl_data_device_add_listener(input->data_device,
5946 &data_device_listener,
5947 input);
5948 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005949
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005950 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5951 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005952
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005953 set_repeat_info(input, 40, 400);
5954 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5955 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005956}
5957
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005958static void
Harish Krupoee4c7a22019-04-19 01:53:27 +05305959display_add_data_device(struct display *d, uint32_t id, int ddm_version)
5960{
5961 struct input *input;
5962
5963 d->data_device_manager_version = MIN(ddm_version, 3);
5964 d->data_device_manager =
5965 wl_registry_bind(d->registry, id,
5966 &wl_data_device_manager_interface,
5967 d->data_device_manager_version);
5968
5969 wl_list_for_each(input, &d->input_list, link) {
5970 if (!input->data_device) {
5971 input->data_device =
5972 wl_data_device_manager_get_data_device(d->data_device_manager,
5973 input->seat);
5974 wl_data_device_add_listener(input->data_device,
5975 &data_device_listener,
5976 input);
5977 }
5978 }
5979}
5980
5981static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005982input_destroy(struct input *input)
5983{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005984 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005985 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005986
5987 if (input->drag_offer)
5988 data_offer_destroy(input->drag_offer);
5989
5990 if (input->selection_offer)
5991 data_offer_destroy(input->selection_offer);
5992
kabeer khan6ce67ec2014-10-20 11:55:29 +05305993 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005994 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305995 wl_data_device_release(input->data_device);
5996 else
5997 wl_data_device_destroy(input->data_device);
5998 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005999 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02006000 if (input->touch)
6001 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01006002 if (input->pointer)
6003 wl_pointer_release(input->pointer);
6004 if (input->keyboard)
6005 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02006006 } else {
6007 if (input->touch)
6008 wl_touch_destroy(input->touch);
6009 if (input->pointer)
6010 wl_pointer_destroy(input->pointer);
6011 if (input->keyboard)
6012 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01006013 }
6014
Daniel Stone97f68542012-05-30 16:32:01 +01006015 fini_xkb(input);
6016
Simon Ser992ee042021-02-07 11:32:46 +01006017 if (input->pointer_surface)
6018 wl_surface_destroy(input->pointer_surface);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03006019
Pekka Paalanene1207c72011-12-16 12:02:09 +02006020 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01006021 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02006022 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006023 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006024 free(input);
6025}
6026
6027static void
ant8mef99fac22018-11-28 22:46:37 +01006028xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08006029{
ant8mef99fac22018-11-28 22:46:37 +01006030 xdg_wm_base_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08006031}
6032
ant8mef99fac22018-11-28 22:46:37 +01006033static const struct xdg_wm_base_listener wm_base_listener = {
6034 xdg_wm_base_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08006035};
6036
6037static void
Pekka Paalanen1e08a812021-05-21 17:29:42 +03006038global_destroy(struct display *disp, struct global *g)
6039{
6040 if (disp->global_handler_remove) {
6041 disp->global_handler_remove(disp, g->name, g->interface,
6042 g->version, disp->user_data);
6043 }
6044
6045 wl_list_remove(&g->link);
6046 free(g->interface);
6047 free(g);
6048}
6049
6050static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006051registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
6052 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006053{
6054 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006055 struct global *global;
6056
Brian Lovinbc919262013-08-07 15:34:59 -07006057 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006058 global->name = id;
6059 global->interface = strdup(interface);
6060 global->version = version;
6061 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006062
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006063 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006064 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05006065 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006066 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006067 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01006068 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05006069 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08006070 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
6071 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
6072 d->relative_pointer_manager =
6073 wl_registry_bind(registry, id,
6074 &zwp_relative_pointer_manager_v1_interface,
6075 1);
6076 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
6077 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
6078 d->pointer_constraints =
6079 wl_registry_bind(registry, id,
6080 &zwp_pointer_constraints_v1_interface,
6081 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006082 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006083 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006084 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Harish Krupoee4c7a22019-04-19 01:53:27 +05306085 display_add_data_device(d, id, version);
ant8mef99fac22018-11-28 22:46:37 +01006086 } else if (strcmp(interface, "xdg_wm_base") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006087 d->xdg_shell = wl_registry_bind(registry, id,
ant8mef99fac22018-11-28 22:46:37 +01006088 &xdg_wm_base_interface, 1);
6089 xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06006090 } else if (strcmp(interface, "text_cursor_position") == 0) {
6091 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006092 wl_registry_bind(registry, id,
6093 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006094 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6095 d->subcompositor =
6096 wl_registry_bind(registry, id,
6097 &wl_subcompositor_interface, 1);
Harish Krupo7bcbab12018-12-11 13:45:43 +05306098 } else if (!strcmp(interface, "wp_viewporter")) {
6099 d->viewporter =
6100 wl_registry_bind(registry, id,
6101 &wp_viewporter_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006102 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006103
6104 if (d->global_handler)
6105 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006106}
6107
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006108static void
6109registry_handle_global_remove(void *data, struct wl_registry *registry,
6110 uint32_t name)
6111{
6112 struct display *d = data;
6113 struct global *global;
6114 struct global *tmp;
6115
6116 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6117 if (global->name != name)
6118 continue;
6119
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006120 if (strcmp(global->interface, "wl_output") == 0)
6121 display_destroy_output(d, name);
6122
Pekka Paalanen1e08a812021-05-21 17:29:42 +03006123 global_destroy(d, global);
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006124 }
6125}
6126
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006127void *
6128display_bind(struct display *display, uint32_t name,
6129 const struct wl_interface *interface, uint32_t version)
6130{
6131 return wl_registry_bind(display->registry, name, interface, version);
6132}
6133
6134static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006135 registry_handle_global,
6136 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006137};
6138
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006139#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006140static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006141init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006142{
6143 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006144 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006145
Rob Clark6396ed32012-03-11 19:48:41 -05006146#ifdef USE_CAIRO_GLESV2
6147# define GL_BIT EGL_OPENGL_ES2_BIT
6148#else
6149# define GL_BIT EGL_OPENGL_BIT
6150#endif
6151
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006152 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006153 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006154 EGL_RED_SIZE, 1,
6155 EGL_GREEN_SIZE, 1,
6156 EGL_BLUE_SIZE, 1,
6157 EGL_ALPHA_SIZE, 1,
6158 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006159 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006160 EGL_NONE
6161 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006162
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006163#ifdef USE_CAIRO_GLESV2
6164 static const EGLint context_attribs[] = {
6165 EGL_CONTEXT_CLIENT_VERSION, 2,
6166 EGL_NONE
6167 };
6168 EGLint api = EGL_OPENGL_ES_API;
6169#else
6170 EGLint *context_attribs = NULL;
6171 EGLint api = EGL_OPENGL_API;
6172#endif
6173
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006174 d->dpy =
6175 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6176 d->display, NULL);
6177
Yuval Fledel45568f62010-12-06 09:18:12 -05006178 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006179 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006180 return -1;
6181 }
6182
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006183 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006184 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006185 return -1;
6186 }
6187
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006188 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6189 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006190 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006191 return -1;
6192 }
6193
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006194 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006195 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006196 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006197 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006198 return -1;
6199 }
6200
Benjamin Franzke0c991632011-09-27 21:57:31 +02006201 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6202 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006203 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006204 return -1;
6205 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006206
6207 return 0;
6208}
6209
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006210static void
6211fini_egl(struct display *display)
6212{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006213 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006214
6215 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6216 EGL_NO_CONTEXT);
6217
6218 eglTerminate(display->dpy);
6219 eglReleaseThread();
6220}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006221#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006222
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006223static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006224init_dummy_surface(struct display *display)
6225{
6226 int len;
6227 void *data;
6228
6229 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006230 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006231 display->dummy_surface =
6232 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6233 1, 1, len);
6234 display->dummy_surface_data = data;
6235}
6236
6237static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006238handle_display_data(struct task *task, uint32_t events)
6239{
6240 struct display *display =
6241 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006242 struct epoll_event ep;
6243 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006244
6245 display->display_fd_events = events;
6246
6247 if (events & EPOLLERR || events & EPOLLHUP) {
6248 display_exit(display);
6249 return;
6250 }
6251
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006252 if (events & EPOLLIN) {
6253 ret = wl_display_dispatch(display->display);
6254 if (ret == -1) {
6255 display_exit(display);
6256 return;
6257 }
6258 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006259
6260 if (events & EPOLLOUT) {
6261 ret = wl_display_flush(display->display);
6262 if (ret == 0) {
6263 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6264 ep.data.ptr = &display->display_task;
6265 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6266 display->display_fd, &ep);
6267 } else if (ret == -1 && errno != EAGAIN) {
6268 display_exit(display);
6269 return;
6270 }
6271 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006272}
6273
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006274static void
6275log_handler(const char *format, va_list args)
6276{
6277 vfprintf(stderr, format, args);
6278}
6279
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006280struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006281display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006282{
6283 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006284
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006285 wl_log_set_handler_client(log_handler);
6286
Peter Huttererf3d62272013-08-08 11:57:05 +10006287 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006288 if (d == NULL)
6289 return NULL;
6290
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006291 wl_list_init(&d->window_list);
6292 wl_list_init(&d->deferred_list);
6293 wl_list_init(&d->input_list);
6294 wl_list_init(&d->output_list);
6295 wl_list_init(&d->global_list);
6296
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006297 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006298 if (d->display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02006299 fprintf(stderr, "failed to connect to Wayland display: %s\n",
6300 strerror(errno));
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006301 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006302 return NULL;
6303 }
6304
Peter Hutterera2086bb2020-05-13 15:24:27 +10006305 d->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Rob Bradford5ab9c752013-07-26 16:29:43 +01006306 if (d->xkb_context == NULL) {
6307 fprintf(stderr, "Failed to create XKB context\n");
6308 free(d);
6309 return NULL;
6310 }
6311
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006312 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006313 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006314 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006315 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6316 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006317
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006318 d->registry = wl_display_get_registry(d->display);
6319 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006320
Marek Chalupaa519d062014-12-05 13:49:40 +01006321 if (wl_display_roundtrip(d->display) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006322 fprintf(stderr, "Failed to process Wayland connection: %s\n",
6323 strerror(errno));
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006324 display_destroy(d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006325 return NULL;
6326 }
6327
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006328#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006329 if (init_egl(d) < 0)
6330 fprintf(stderr, "EGL does not seem to work, "
6331 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006332#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006333
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006334 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006335
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006336 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006337
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006338 init_dummy_surface(d);
6339
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006340 return d;
6341}
6342
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006343static void
6344display_destroy_outputs(struct display *display)
6345{
6346 struct output *tmp;
6347 struct output *output;
6348
6349 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6350 output_destroy(output);
6351}
6352
Pekka Paalanene1207c72011-12-16 12:02:09 +02006353static void
6354display_destroy_inputs(struct display *display)
6355{
6356 struct input *tmp;
6357 struct input *input;
6358
6359 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6360 input_destroy(input);
6361}
6362
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006363void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006364display_destroy(struct display *display)
6365{
Pekka Paalanenc8c53ba2021-05-21 17:31:11 +03006366 struct global *global, *tmp;
6367
Pekka Paalanenc2052982011-12-16 11:41:32 +02006368 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006369 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6370 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006371
6372 if (!wl_list_empty(&display->deferred_list))
6373 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6374
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006375 if (display->dummy_surface)
6376 cairo_surface_destroy(display->dummy_surface);
6377 if (display->dummy_surface_data)
6378 free(display->dummy_surface_data);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006379
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006380 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006381 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006382
Pekka Paalanenc8c53ba2021-05-21 17:31:11 +03006383 wl_list_for_each_safe(global, tmp, &display->global_list, link)
6384 global_destroy(display, global);
6385
Daniel Stone97f68542012-05-30 16:32:01 +01006386 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006387
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006388 if (display->theme)
6389 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006390 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006391
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006392#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006393 if (display->argb_device)
6394 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006395#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006396
Pekka Paalanenc8c53ba2021-05-21 17:31:11 +03006397 if (display->relative_pointer_manager)
6398 zwp_relative_pointer_manager_v1_destroy(display->relative_pointer_manager);
6399
6400 if (display->pointer_constraints)
6401 zwp_pointer_constraints_v1_destroy(display->pointer_constraints);
6402
6403 if (display->viewporter)
6404 wp_viewporter_destroy(display->viewporter);
6405
Pekka Paalanen35e82632013-04-25 13:57:48 +03006406 if (display->subcompositor)
6407 wl_subcompositor_destroy(display->subcompositor);
6408
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006409 if (display->xdg_shell)
ant8mef99fac22018-11-28 22:46:37 +01006410 xdg_wm_base_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006411
6412 if (display->shm)
6413 wl_shm_destroy(display->shm);
6414
6415 if (display->data_device_manager)
6416 wl_data_device_manager_destroy(display->data_device_manager);
6417
Pekka Paalanen1f3e9042021-05-21 16:38:30 +03006418 if (display->compositor)
6419 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006420 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006421
6422 close(display->epoll_fd);
6423
U. Artie Eoff44874d92012-10-02 21:12:35 -07006424 if (!(display->display_fd_events & EPOLLERR) &&
6425 !(display->display_fd_events & EPOLLHUP))
6426 wl_display_flush(display->display);
6427
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006428 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006429 free(display);
6430}
6431
6432void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006433display_set_user_data(struct display *display, void *data)
6434{
6435 display->user_data = data;
6436}
6437
6438void *
6439display_get_user_data(struct display *display)
6440{
6441 return display->user_data;
6442}
6443
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006444struct wl_display *
6445display_get_display(struct display *display)
6446{
6447 return display->display;
6448}
6449
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006450int
6451display_has_subcompositor(struct display *display)
6452{
6453 if (display->subcompositor)
6454 return 1;
6455
6456 wl_display_roundtrip(display->display);
6457
6458 return display->subcompositor != NULL;
6459}
6460
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006461cairo_device_t *
6462display_get_cairo_device(struct display *display)
6463{
6464 return display->argb_device;
6465}
6466
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006467struct output *
6468display_get_output(struct display *display)
6469{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006470 if (wl_list_empty(&display->output_list))
6471 return NULL;
6472
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006473 return container_of(display->output_list.next, struct output, link);
6474}
6475
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006476struct wl_compositor *
6477display_get_compositor(struct display *display)
6478{
6479 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006480}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006481
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006482uint32_t
6483display_get_serial(struct display *display)
6484{
6485 return display->serial;
6486}
6487
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006488EGLDisplay
6489display_get_egl_display(struct display *d)
6490{
6491 return d->dpy;
6492}
6493
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006494struct wl_data_source *
6495display_create_data_source(struct display *display)
6496{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006497 if (display->data_device_manager)
6498 return wl_data_device_manager_create_data_source(display->data_device_manager);
6499 else
6500 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006501}
6502
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006503EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006504display_get_argb_egl_config(struct display *d)
6505{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006506 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006507}
6508
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006509int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006510display_acquire_window_surface(struct display *display,
6511 struct window *window,
6512 EGLContext ctx)
6513{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006514 struct surface *surface = window->main_surface;
6515
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006516 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006517 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006518
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006519 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006520 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006521}
6522
6523void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006524display_release_window_surface(struct display *display,
6525 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006526{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006527 struct surface *surface = window->main_surface;
6528
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006529 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006530 return;
6531
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006532 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006533}
6534
6535void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006536display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006537{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006538 wl_list_insert(&display->deferred_list, &task->link);
6539}
6540
6541void
6542display_watch_fd(struct display *display,
6543 int fd, uint32_t events, struct task *task)
6544{
6545 struct epoll_event ep;
6546
6547 ep.events = events;
6548 ep.data.ptr = task;
6549 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6550}
6551
6552void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006553display_unwatch_fd(struct display *display, int fd)
6554{
6555 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6556}
6557
6558void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006559display_run(struct display *display)
6560{
6561 struct task *task;
6562 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006563 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006564
Pekka Paalanen826d7952011-12-15 10:14:07 +02006565 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006566 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006567 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006568 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006569 struct task, link);
6570 wl_list_remove(&task->link);
6571 task->run(task, 0);
6572 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006573
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006574 wl_display_dispatch_pending(display->display);
6575
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006576 if (!display->running)
6577 break;
6578
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006579 ret = wl_display_flush(display->display);
6580 if (ret < 0 && errno == EAGAIN) {
6581 ep[0].events =
6582 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6583 ep[0].data.ptr = &display->display_task;
6584
6585 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6586 display->display_fd, &ep[0]);
6587 } else if (ret < 0) {
6588 break;
6589 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006590
6591 count = epoll_wait(display->epoll_fd,
6592 ep, ARRAY_LENGTH(ep), -1);
6593 for (i = 0; i < count; i++) {
6594 task = ep[i].data.ptr;
6595 task->run(task, ep[i].events);
6596 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006597 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006598}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006599
6600void
6601display_exit(struct display *display)
6602{
6603 display->running = 0;
6604}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006605
Carlos Garnacho9c931792016-01-18 23:52:12 +01006606int
6607display_get_data_device_manager_version(struct display *display)
6608{
6609 return display->data_device_manager_version;
6610}
6611
Jan Arne Petersencd997062012-11-18 19:06:44 +01006612void
6613keysym_modifiers_add(struct wl_array *modifiers_map,
6614 const char *name)
6615{
6616 size_t len = strlen(name) + 1;
6617 char *p;
6618
6619 p = wl_array_add(modifiers_map, len);
6620
6621 if (p == NULL)
6622 return;
6623
6624 strncpy(p, name, len);
6625}
6626
6627static xkb_mod_index_t
6628keysym_modifiers_get_index(struct wl_array *modifiers_map,
6629 const char *name)
6630{
6631 xkb_mod_index_t index = 0;
6632 char *p = modifiers_map->data;
6633
6634 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6635 if (strcmp(p, name) == 0)
6636 return index;
6637
6638 index++;
6639 p += strlen(p) + 1;
6640 }
6641
6642 return XKB_MOD_INVALID;
6643}
6644
6645xkb_mod_mask_t
6646keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6647 const char *name)
6648{
6649 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6650
6651 if (index == XKB_MOD_INVALID)
6652 return XKB_MOD_INVALID;
6653
6654 return 1 << index;
6655}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006656
6657static void
6658toytimer_fire(struct task *tsk, uint32_t events)
6659{
6660 uint64_t e;
6661 struct toytimer *tt;
6662
6663 tt = container_of(tsk, struct toytimer, tsk);
6664
6665 if (events != EPOLLIN)
6666 fprintf(stderr, "unexpected timerfd events %x\n", events);
6667
6668 if (!(events & EPOLLIN))
6669 return;
6670
6671 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6672 /* If we change the timer between the fd becoming
6673 * readable and getting here, there'll be nothing to
6674 * read and we get EAGAIN. */
6675 if (errno != EAGAIN)
Antonio Borneo39578632019-04-26 23:57:31 +02006676 fprintf(stderr, "timer read failed: %s\n",
6677 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006678 return;
6679 }
6680
6681 tt->callback(tt);
6682}
6683
6684void
6685toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6686 toytimer_cb callback)
6687{
6688 memset(tt, 0, sizeof *tt);
6689
6690 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6691 if (tt->fd == -1) {
Antonio Borneo39578632019-04-26 23:57:31 +02006692 fprintf(stderr, "creating timer failed: %s\n",
6693 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006694 abort();
6695 }
6696
6697 tt->display = display;
6698 tt->callback = callback;
6699 tt->tsk.run = toytimer_fire;
6700 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6701}
6702
6703void
6704toytimer_fini(struct toytimer *tt)
6705{
6706 display_unwatch_fd(tt->display, tt->fd);
6707 close(tt->fd);
6708 tt->fd = -1;
6709}
6710
6711void
6712toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6713{
6714 int ret;
6715
6716 ret = timerfd_settime(tt->fd, 0, its, NULL);
6717 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006718 fprintf(stderr, "timer setup failed: %s\n", strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006719 abort();
6720 }
6721}
6722
6723#define USEC_PER_SEC 1000000
6724
6725void
6726toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6727{
6728 struct itimerspec its;
6729
6730 its.it_interval.tv_sec = 0;
6731 its.it_interval.tv_nsec = 0;
6732 its.it_value.tv_sec = usec / USEC_PER_SEC;
6733 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6734 toytimer_arm(tt, &its);
6735}
6736
6737void
6738toytimer_disarm(struct toytimer *tt)
6739{
6740 struct itimerspec its = {};
6741
6742 toytimer_arm(tt, &its);
6743}