blob: 49574b5765c20c9ea0928152798e5e3f5789ed70 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080027#include <stdbool.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030031#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040035#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020037#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050038#include <time.h>
39#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040040#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040041#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030042#include <sys/timerfd.h>
Derek Foreman493d9792015-03-04 16:26:25 -060043#include <stdbool.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050046#include <wayland-egl.h>
47
Rob Clark6396ed32012-03-11 19:48:41 -050048#ifdef USE_CAIRO_GLESV2
49#include <GLES2/gl2.h>
50#include <GLES2/gl2ext.h>
51#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050053#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <EGL/egl.h>
55#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040057#include <cairo-gl.h>
Daniel Stone4b341a82017-02-16 21:52:23 +000058#elif !defined(ENABLE_EGL) /* platform.h defines these if EGL is enabled */
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030059typedef void *EGLDisplay;
60typedef void *EGLConfig;
61typedef void *EGLContext;
62#define EGL_NO_DISPLAY ((EGLDisplay)0)
63#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050064
Daniel Stone9d4f0302012-02-15 16:33:21 +000065#include <xkbcommon/xkbcommon.h>
Daniel Stone5b015962016-10-20 14:45:58 +010066#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -070067#include <xkbcommon/xkbcommon-compose.h>
Daniel Stone5b015962016-10-20 14:45:58 +010068#endif
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030069#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040070
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050071#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020072#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070073#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070074#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070075#include "shared/xalloc.h"
Pekka Paalanenecbdcfd2019-04-04 14:46:00 +030076#include <libweston/zalloc.h>
ant8mef99fac22018-11-28 22:46:37 +010077#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060078#include "text-cursor-position-client-protocol.h"
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080079#include "pointer-constraints-unstable-v1-client-protocol.h"
80#include "relative-pointer-unstable-v1-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070081#include "shared/os-compatibility.h"
emersionac71ee52018-11-18 21:42:10 +010082#include "shared/string-helpers.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050083
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050084#include "window.h"
Harish Krupo7bcbab12018-12-11 13:45:43 +053085#include "viewporter-client-protocol.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050086
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080087#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
88#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
89
emersionac71ee52018-11-18 21:42:10 +010090#define DEFAULT_XCURSOR_SIZE 32
91
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070092struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030093
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040094struct global {
95 uint32_t name;
96 char *interface;
97 uint32_t version;
98 struct wl_list link;
99};
100
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500101struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500102 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400103 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500104 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300105 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400106 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400107 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600108 struct text_cursor_position *text_cursor_position;
ant8mef99fac22018-11-28 22:46:37 +0100109 struct xdg_wm_base *xdg_shell;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800110 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
111 struct zwp_pointer_constraints_v1 *pointer_constraints;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400112 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500113 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200114 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200115 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400116 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400117
118 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700119 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400120 struct task display_task;
121
122 int epoll_fd;
123 struct wl_list deferred_list;
124
Pekka Paalanen826d7952011-12-15 10:14:07 +0200125 int running;
126
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400127 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400128 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400129 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500130 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400131
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400132 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400133
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300134 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300135 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400136
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200137 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400138 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800139 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200140
141 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100142
143 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200144
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200145 /* A hack to get text extents for tooltips */
146 cairo_surface_t *dummy_surface;
147 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200148
kabeer khan6ce67ec2014-10-20 11:55:29 +0530149 int data_device_manager_version;
Harish Krupo7bcbab12018-12-11 13:45:43 +0530150 struct wp_viewporter *viewporter;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500151};
152
Rob Bradford7507b572012-05-15 17:55:34 +0100153struct window_output {
154 struct output *output;
155 struct wl_list link;
156};
157
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200158struct toysurface {
159 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500160 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800161 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200162 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200163 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200164 * If flags has SURFACE_HINT_RESIZE set, the user is
165 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200166 * Returns the Cairo surface to draw to.
167 */
168 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200169 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200170 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200171
172 /*
173 * Post the surface to the server, returning the server allocation
174 * rectangle. The Cairo surface from prepare() must be destroyed
175 * after calling this.
176 */
177 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200178 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200179 struct rectangle *server_allocation);
180
181 /*
182 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800183 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200184 */
185 int (*acquire)(struct toysurface *base, EGLContext ctx);
186
187 /*
188 * Release the toysurface from the EGL context, returning control
189 * to Cairo.
190 */
191 void (*release)(struct toysurface *base);
192
193 /*
194 * Destroy the toysurface, including the Cairo surface, any
195 * backing storage, and the Wayland protocol objects.
196 */
197 void (*destroy)(struct toysurface *base);
198};
199
Pekka Paalanen4e373742013-02-13 16:17:13 +0200200struct surface {
201 struct window *window;
202
203 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300204 struct wl_subsurface *subsurface;
205 int synchronized;
206 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200207 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200208 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300209 int redraw_needed;
210 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300211 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200212
213 struct rectangle allocation;
214 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200215
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200216 struct wl_region *input_region;
217 struct wl_region *opaque_region;
218
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200219 enum window_buffer_type buffer_type;
220 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200221 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200222
223 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300224
225 struct wl_list link;
Harish Krupo7bcbab12018-12-11 13:45:43 +0530226 struct wp_viewport *viewport;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200227};
228
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500229struct window {
230 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100231 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500232 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200233 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400234 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500235 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200236 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500237 int x, y;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800238 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400239 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300240 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400241 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400242 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500243 int custom;
244 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400245
Pekka Paalanen99436862012-11-19 17:15:59 +0200246 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400247
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500248 int fullscreen;
249 int maximized;
250
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500251 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500252 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400253 window_data_handler_t data_handler;
254 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500255 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400256 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200257 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700258 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400259
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800260 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
261
Pekka Paalanen4e373742013-02-13 16:17:13 +0200262 struct surface *main_surface;
ant8mef99fac22018-11-28 22:46:37 +0100263 struct xdg_surface *xdg_surface;
264 struct xdg_toplevel *xdg_toplevel;
265 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300266
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700267 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800268 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500269
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500270 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500271
Pekka Paalanen35e82632013-04-25 13:57:48 +0300272 /* struct surface::link, contains also main_surface */
273 struct wl_list subsurface_list;
274
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800275 struct zwp_relative_pointer_v1 *relative_pointer;
276 struct zwp_locked_pointer_v1 *locked_pointer;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800277 bool pointer_locked;
278 locked_pointer_locked_handler_t pointer_locked_handler;
279 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
280 confined_pointer_confined_handler_t pointer_confined_handler;
281 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
282
283 struct zwp_confined_pointer_v1 *confined_pointer;
284 struct widget *confined_widget;
285 bool confined;
286
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500287 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400288 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500289};
290
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500291struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500292 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200293 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300294 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500295 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400296 struct wl_list link;
297 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500298 widget_resize_handler_t resize_handler;
299 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500300 widget_enter_handler_t enter_handler;
301 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500302 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500303 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 widget_touch_down_handler_t touch_down_handler;
305 widget_touch_up_handler_t touch_up_handler;
306 widget_touch_motion_handler_t touch_motion_handler;
307 widget_touch_frame_handler_t touch_frame_handler;
308 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200309 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000310 widget_pointer_frame_handler_t pointer_frame_handler;
311 widget_axis_source_handler_t axis_source_handler;
312 widget_axis_stop_handler_t axis_stop_handler;
313 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400314 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500315 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300316 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500317 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000318 /* If this is set to false then no cairo surface will be
319 * created before redrawing the surface. This is useful if the
320 * redraw handler is going to do completely custom rendering
321 * such as using EGL directly */
322 int use_cairo;
Harish Krupo7bcbab12018-12-11 13:45:43 +0530323 int viewport_dest_width;
324 int viewport_dest_height;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400325};
326
Rusty Lynch041815a2013-08-08 21:20:38 -0700327struct touch_point {
328 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800329 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700330 struct widget *widget;
331 struct wl_list link;
332};
333
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400334struct input {
335 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100336 struct wl_seat *seat;
337 struct wl_pointer *pointer;
338 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700339 struct wl_touch *touch;
340 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400341 struct window *pointer_focus;
342 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700343 struct window *touch_focus;
Dima Ryazanov13bdf252018-11-14 22:17:42 -0800344 struct window *locked_window;
345 struct window *confined_window;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300346 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300347 uint32_t cursor_anim_start;
348 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500349 uint32_t cursor_timer_start;
350 uint32_t cursor_anim_current;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200351 struct toytimer cursor_timer;
Derek Foreman118a4292015-04-22 17:23:35 -0500352 bool cursor_timer_running;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300353 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400354 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400355 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400356 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400357 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400358 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400359
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500360 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500361 struct widget *grab;
362 uint32_t grab_button;
363
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400364 struct wl_data_device *data_device;
365 struct data_offer *drag_offer;
366 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800367 uint32_t touch_grab;
368 int32_t touch_grab_id;
369 float drag_x, drag_y;
370 struct window *drag_focus;
371 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100372
373 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100374 struct xkb_keymap *keymap;
375 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +0100376#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700377 struct xkb_compose_table *compose_table;
378 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +0100379#endif
Daniel Stone97f68542012-05-30 16:32:01 +0100380 xkb_mod_mask_t control_mask;
381 xkb_mod_mask_t alt_mask;
382 xkb_mod_mask_t shift_mask;
383 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400384
Jonny Lamb06959082014-08-12 14:58:27 +0200385 int32_t repeat_rate_sec;
386 int32_t repeat_rate_nsec;
387 int32_t repeat_delay_sec;
388 int32_t repeat_delay_nsec;
389
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200390 struct toytimer repeat_timer;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400391 uint32_t repeat_sym;
392 uint32_t repeat_key;
393 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500394 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400395};
396
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500397struct output {
398 struct display *display;
399 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800400 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500401 struct rectangle allocation;
402 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600403 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200404 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500405 char *make;
406 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200407
408 display_output_handler_t destroy_handler;
409 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500410};
411
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500412struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500413 struct widget *widget;
414 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500415 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800416
417 uint32_t last_time;
418 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800419 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500420};
421
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500422struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400423 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500424 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500425 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500426 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700427 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500428 const char **entries;
429 uint32_t time;
430 int current;
431 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400432 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500433 menu_func_t func;
434};
435
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300436struct tooltip {
437 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300438 struct widget *widget;
439 char *entry;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200440 struct toytimer timer;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300441 float x, y;
442};
443
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700444struct shm_pool {
445 struct wl_shm_pool *pool;
446 size_t size;
447 size_t used;
448 void *data;
449};
450
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400451enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300452 CURSOR_DEFAULT = 100,
453 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400454};
455
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200456static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457
Pekka Paalanen97777442013-05-22 10:20:05 +0300458/* #define DEBUG */
459
460#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300461
462static void
463debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
464__attribute__ ((format (printf, 4, 5)));
465
466static void
467debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
468{
469 va_list ap;
470 struct timeval tv;
471
472 gettimeofday(&tv, NULL);
473 fprintf(stderr, "%8ld.%03ld ",
474 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
475
476 if (proxy)
477 fprintf(stderr, "%s@%d ",
478 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
479
480 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
481 fprintf(stderr, "%s ", func);
482
483 va_start(ap, fmt);
484 vfprintf(stderr, fmt, ap);
485 va_end(ap);
486}
487
488#define DBG(fmt, ...) \
489 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
490
491#define DBG_OBJ(obj, fmt, ...) \
492 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
493
494#else
495
496#define DBG(...) do {} while (0)
497#define DBG_OBJ(...) do {} while (0)
498
499#endif
500
Alexander Larsson1818e312013-05-22 14:41:31 +0200501static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200502surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200503{
504 int32_t tmp;
505
506 switch (buffer_transform) {
507 case WL_OUTPUT_TRANSFORM_90:
508 case WL_OUTPUT_TRANSFORM_270:
509 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
510 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
511 tmp = *width;
512 *width = *height;
513 *height = tmp;
514 break;
515 default:
516 break;
517 }
518
519 *width *= buffer_scale;
520 *height *= buffer_scale;
521}
522
523static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200524buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200525{
526 int32_t tmp;
527
528 switch (buffer_transform) {
529 case WL_OUTPUT_TRANSFORM_90:
530 case WL_OUTPUT_TRANSFORM_270:
531 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
532 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
533 tmp = *width;
534 *width = *height;
535 *height = tmp;
536 break;
537 default:
538 break;
539 }
540
541 *width /= buffer_scale;
542 *height /= buffer_scale;
543}
544
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500545#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547struct egl_window_surface {
548 struct toysurface base;
549 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100550 struct display *display;
551 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200552 struct wl_egl_window *egl_window;
553 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100554};
555
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556static struct egl_window_surface *
557to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100560}
561
562static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200564 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200565 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100566{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200567 struct egl_window_surface *surface = to_egl_window_surface(base);
568
Alexander Larsson1818e312013-05-22 14:41:31 +0200569 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
570
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
572 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
573
574 return cairo_surface_reference(surface->cairo_surface);
575}
576
577static void
578egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200579 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200580 struct rectangle *server_allocation)
581{
582 struct egl_window_surface *surface = to_egl_window_surface(base);
583
584 cairo_gl_surface_swapbuffers(surface->cairo_surface);
585 wl_egl_window_get_attached_size(surface->egl_window,
586 &server_allocation->width,
587 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200588
589 buffer_to_surface_size (buffer_transform, buffer_scale,
590 &server_allocation->width,
591 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200592}
593
594static int
595egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
596{
597 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200598 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100599
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200600 device = cairo_surface_get_device(surface->cairo_surface);
601 if (!device)
602 return -1;
603
604 if (!ctx) {
605 if (device == surface->display->argb_device)
606 ctx = surface->display->argb_ctx;
607 else
608 assert(0);
609 }
610
611 cairo_device_flush(device);
612 cairo_device_acquire(device);
613 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
614 surface->egl_surface, ctx))
615 fprintf(stderr, "failed to make surface current\n");
616
617 return 0;
618}
619
620static void
621egl_window_surface_release(struct toysurface *base)
622{
623 struct egl_window_surface *surface = to_egl_window_surface(base);
624 cairo_device_t *device;
625
626 device = cairo_surface_get_device(surface->cairo_surface);
627 if (!device)
628 return;
629
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200630 if (!eglMakeCurrent(surface->display->dpy,
631 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200632 fprintf(stderr, "failed to make context current\n");
633
634 cairo_device_release(device);
635}
636
637static void
638egl_window_surface_destroy(struct toysurface *base)
639{
640 struct egl_window_surface *surface = to_egl_window_surface(base);
641 struct display *d = surface->display;
642
643 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000644 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200645 wl_egl_window_destroy(surface->egl_window);
646 surface->surface = NULL;
647
648 free(surface);
649}
650
651static struct toysurface *
652egl_window_surface_create(struct display *display,
653 struct wl_surface *wl_surface,
654 uint32_t flags,
655 struct rectangle *rectangle)
656{
657 struct egl_window_surface *surface;
658
Pekka Paalanenb3627362012-11-19 17:16:00 +0200659 if (display->dpy == EGL_NO_DISPLAY)
660 return NULL;
661
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800662 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200663 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100664 return NULL;
665
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200666 surface->base.prepare = egl_window_surface_prepare;
667 surface->base.swap = egl_window_surface_swap;
668 surface->base.acquire = egl_window_surface_acquire;
669 surface->base.release = egl_window_surface_release;
670 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100671
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200672 surface->display = display;
673 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400674
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200675 surface->egl_window = wl_egl_window_create(surface->surface,
676 rectangle->width,
677 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100678
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100679 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100680 weston_platform_create_egl_surface(display->dpy,
681 display->argb_config,
682 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100683
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200684 surface->cairo_surface =
685 cairo_gl_surface_create_for_egl(display->argb_device,
686 surface->egl_surface,
687 rectangle->width,
688 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100689
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200690 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100691}
692
Pekka Paalanenb3627362012-11-19 17:16:00 +0200693#else
694
695static struct toysurface *
696egl_window_surface_create(struct display *display,
697 struct wl_surface *wl_surface,
698 uint32_t flags,
699 struct rectangle *rectangle)
700{
701 return NULL;
702}
703
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704#endif
705
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200706struct shm_surface_data {
707 struct wl_buffer *buffer;
708 struct shm_pool *pool;
709};
710
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400711struct wl_buffer *
712display_get_buffer_for_surface(struct display *display,
713 cairo_surface_t *surface)
714{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200715 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400716
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200717 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400718
719 return data->buffer;
720}
721
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500722static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723shm_pool_destroy(struct shm_pool *pool);
724
725static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400726shm_surface_data_destroy(void *p)
727{
728 struct shm_surface_data *data = p;
729
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200730 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731 if (data->pool)
732 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300733
734 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400735}
736
Kristian Høgsberg16626282012-04-03 11:21:27 -0400737static struct wl_shm_pool *
738make_shm_pool(struct display *display, int size, void **data)
739{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400740 struct wl_shm_pool *pool;
741 int fd;
742
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300743 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400744 if (fd < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200745 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
746 size, strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400747 return NULL;
748 }
749
750 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400751 if (*data == MAP_FAILED) {
Antonio Borneo39578632019-04-26 23:57:31 +0200752 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400753 close(fd);
754 return NULL;
755 }
756
757 pool = wl_shm_create_pool(display->shm, fd, size);
758
759 close(fd);
760
761 return pool;
762}
763
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700764static struct shm_pool *
765shm_pool_create(struct display *display, size_t size)
766{
767 struct shm_pool *pool = malloc(sizeof *pool);
768
769 if (!pool)
770 return NULL;
771
772 pool->pool = make_shm_pool(display, size, &pool->data);
773 if (!pool->pool) {
774 free(pool);
775 return NULL;
776 }
777
778 pool->size = size;
779 pool->used = 0;
780
781 return pool;
782}
783
784static void *
785shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
786{
787 if (pool->used + size > pool->size)
788 return NULL;
789
790 *offset = pool->used;
791 pool->used += size;
792
793 return (char *) pool->data + *offset;
794}
795
796/* destroy the pool. this does not unmap the memory though */
797static void
798shm_pool_destroy(struct shm_pool *pool)
799{
800 munmap(pool->data, pool->size);
801 wl_shm_pool_destroy(pool->pool);
802 free(pool);
803}
804
805/* Start allocating from the beginning of the pool again */
806static void
807shm_pool_reset(struct shm_pool *pool)
808{
809 pool->used = 0;
810}
811
812static int
813data_length_for_shm_surface(struct rectangle *rect)
814{
815 int stride;
816
817 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
818 rect->width);
819 return stride * rect->height;
820}
821
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500822static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700823display_create_shm_surface_from_pool(struct display *display,
824 struct rectangle *rectangle,
825 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826{
827 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400828 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400829 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700830 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400831 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832
833 data = malloc(sizeof *data);
834 if (data == NULL)
835 return NULL;
836
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300837 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
838 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700839 length = stride * rectangle->height;
840 data->pool = NULL;
841 map = shm_pool_allocate(pool, length, &offset);
842
843 if (!map) {
844 free(data);
845 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400846 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400848 surface = cairo_image_surface_create_for_data (map,
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300849 CAIRO_FORMAT_ARGB32,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850 rectangle->width,
851 rectangle->height,
852 stride);
853
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200854 cairo_surface_set_user_data(surface, &shm_surface_data_key,
855 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300857 if (flags & SURFACE_OPAQUE)
858 format = WL_SHM_FORMAT_XRGB8888;
859 else
860 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400861
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200862 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
863 rectangle->width,
864 rectangle->height,
865 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400866
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700867 return surface;
868}
869
870static cairo_surface_t *
871display_create_shm_surface(struct display *display,
872 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200873 struct shm_pool *alternate_pool,
874 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700875{
876 struct shm_surface_data *data;
877 struct shm_pool *pool;
878 cairo_surface_t *surface;
879
Pekka Paalanen99436862012-11-19 17:15:59 +0200880 if (alternate_pool) {
881 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700882 surface = display_create_shm_surface_from_pool(display,
883 rectangle,
884 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200885 alternate_pool);
886 if (surface) {
887 data = cairo_surface_get_user_data(surface,
888 &shm_surface_data_key);
889 goto out;
890 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700891 }
892
Leandro Ribeirof0cd00a2019-12-04 14:48:27 -0300893 pool = shm_pool_create(display, data_length_for_shm_surface(rectangle));
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700894 if (!pool)
895 return NULL;
896
897 surface =
898 display_create_shm_surface_from_pool(display, rectangle,
899 flags, pool);
900
901 if (!surface) {
902 shm_pool_destroy(pool);
903 return NULL;
904 }
905
906 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200907 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700908 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909
Pekka Paalanen99436862012-11-19 17:15:59 +0200910out:
911 if (data_ret)
912 *data_ret = data;
913
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400914 return surface;
915}
916
nobled7b87cb02011-02-01 18:51:47 +0000917static int
918check_size(struct rectangle *rect)
919{
920 if (rect->width && rect->height)
921 return 0;
922
923 fprintf(stderr, "tried to create surface of "
924 "width: %d, height: %d\n", rect->width, rect->height);
925 return -1;
926}
927
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928cairo_surface_t *
929display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100930 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400931 struct rectangle *rectangle,
932 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400933{
nobled7b87cb02011-02-01 18:51:47 +0000934 if (check_size(rectangle) < 0)
935 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200936
937 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200938 return display_create_shm_surface(display, rectangle, flags,
939 NULL, NULL);
940}
941
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942struct shm_surface_leaf {
943 cairo_surface_t *cairo_surface;
944 /* 'data' is automatically destroyed, when 'cairo_surface' is */
945 struct shm_surface_data *data;
946
947 struct shm_pool *resize_pool;
948 int busy;
949};
950
951static void
952shm_surface_leaf_release(struct shm_surface_leaf *leaf)
953{
954 if (leaf->cairo_surface)
955 cairo_surface_destroy(leaf->cairo_surface);
956 /* leaf->data already destroyed via cairo private */
957
958 if (leaf->resize_pool)
959 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200960
961 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200962}
963
Pekka Paalanenaef02542013-04-25 13:57:47 +0300964#define MAX_LEAVES 3
965
Pekka Paalanen99436862012-11-19 17:15:59 +0200966struct shm_surface {
967 struct toysurface base;
968 struct display *display;
969 struct wl_surface *surface;
970 uint32_t flags;
971 int dx, dy;
972
Pekka Paalanenaef02542013-04-25 13:57:47 +0300973 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200974 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200975};
976
977static struct shm_surface *
978to_shm_surface(struct toysurface *base)
979{
980 return container_of(base, struct shm_surface, base);
981}
982
Pekka Paalanena4eda732012-11-19 17:16:02 +0200983static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300984shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
985{
986#ifdef DEBUG
987 struct shm_surface_leaf *leaf;
988 char bufs[MAX_LEAVES + 1];
989 int i;
990
991 for (i = 0; i < MAX_LEAVES; i++) {
992 leaf = &surface->leaf[i];
993
994 if (leaf->busy)
995 bufs[i] = 'b';
996 else if (leaf->cairo_surface)
997 bufs[i] = 'a';
998 else
999 bufs[i] = ' ';
1000 }
1001
1002 bufs[MAX_LEAVES] = '\0';
1003 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1004#endif
1005}
1006
1007static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001008shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1009{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001010 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001011 struct shm_surface_leaf *leaf;
1012 int i;
1013 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001014
1015 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001016
Pekka Paalanenaef02542013-04-25 13:57:47 +03001017 for (i = 0; i < MAX_LEAVES; i++) {
1018 leaf = &surface->leaf[i];
1019 if (leaf->data && leaf->data->buffer == buffer) {
1020 leaf->busy = 0;
1021 break;
1022 }
1023 }
1024 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001025
Pekka Paalanenaef02542013-04-25 13:57:47 +03001026 /* Leave one free leaf with storage, release others */
1027 free_found = 0;
1028 for (i = 0; i < MAX_LEAVES; i++) {
1029 leaf = &surface->leaf[i];
1030
1031 if (!leaf->cairo_surface || leaf->busy)
1032 continue;
1033
1034 if (!free_found)
1035 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001036 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001037 shm_surface_leaf_release(leaf);
1038 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001039
Pekka Paalanen97777442013-05-22 10:20:05 +03001040 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001041}
1042
1043static const struct wl_buffer_listener shm_surface_buffer_listener = {
1044 shm_surface_buffer_release
1045};
1046
Pekka Paalanen99436862012-11-19 17:15:59 +02001047static cairo_surface_t *
1048shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001049 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001050 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001051{
Pekka Paalanenec076692012-11-30 13:37:27 +02001052 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001053 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001054 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001055 struct shm_surface_leaf *leaf = NULL;
1056 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001057
1058 surface->dx = dx;
1059 surface->dy = dy;
1060
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001061 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001062 for (i = 0; i < MAX_LEAVES; i++) {
1063 if (surface->leaf[i].busy)
1064 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001065
Pekka Paalanenaef02542013-04-25 13:57:47 +03001066 if (!leaf || surface->leaf[i].cairo_surface)
1067 leaf = &surface->leaf[i];
1068 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001069 DBG_OBJ(surface->surface, "pick leaf %d\n",
1070 (int)(leaf - &surface->leaf[0]));
1071
Pekka Paalanenaef02542013-04-25 13:57:47 +03001072 if (!leaf) {
1073 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001075 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001076 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001077 }
1078
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 if (!resize_hint && leaf->resize_pool) {
1080 cairo_surface_destroy(leaf->cairo_surface);
1081 leaf->cairo_surface = NULL;
1082 shm_pool_destroy(leaf->resize_pool);
1083 leaf->resize_pool = NULL;
1084 }
1085
Alexander Larsson1818e312013-05-22 14:41:31 +02001086 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1087
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 if (leaf->cairo_surface &&
1089 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1090 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001091 goto out;
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 if (leaf->cairo_surface)
1094 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001095
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001096#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001097 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001098 /* Create a big pool to allocate from, while continuously
1099 * resizing. Mmapping a new pool in the server
1100 * is relatively expensive, so reusing a pool performs
1101 * better, but may temporarily reserve unneeded memory.
1102 */
1103 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 leaf->resize_pool = shm_pool_create(surface->display,
1105 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001106 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001107#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001108
Alexander Larsson1818e312013-05-22 14:41:31 +02001109 rect.width = width;
1110 rect.height = height;
1111
Pekka Paalanena4eda732012-11-19 17:16:02 +02001112 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 display_create_shm_surface(surface->display, &rect,
1114 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115 leaf->resize_pool,
1116 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001117 if (!leaf->cairo_surface)
1118 return NULL;
1119
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001121 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001122
1123out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001124 surface->current = leaf;
1125
1126 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001127}
1128
1129static void
1130shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001131 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001132 struct rectangle *server_allocation)
1133{
1134 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001135 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001136
1137 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001138 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001139 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001140 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001141
Alexander Larsson1818e312013-05-22 14:41:31 +02001142 buffer_to_surface_size (buffer_transform, buffer_scale,
1143 &server_allocation->width,
1144 &server_allocation->height);
1145
Pekka Paalanena4eda732012-11-19 17:16:02 +02001146 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001147 surface->dx, surface->dy);
1148 wl_surface_damage(surface->surface, 0, 0,
1149 server_allocation->width, server_allocation->height);
1150 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001151
Pekka Paalanen71233882013-04-25 13:57:53 +03001152 DBG_OBJ(surface->surface, "leaf %d busy\n",
1153 (int)(leaf - &surface->leaf[0]));
1154
Pekka Paalanena4eda732012-11-19 17:16:02 +02001155 leaf->busy = 1;
1156 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001157}
1158
1159static int
1160shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1161{
1162 return -1;
1163}
1164
1165static void
1166shm_surface_release(struct toysurface *base)
1167{
1168}
1169
1170static void
1171shm_surface_destroy(struct toysurface *base)
1172{
1173 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001174 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001175
Pekka Paalanenaef02542013-04-25 13:57:47 +03001176 for (i = 0; i < MAX_LEAVES; i++)
1177 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001178
1179 free(surface);
1180}
1181
1182static struct toysurface *
1183shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1184 uint32_t flags, struct rectangle *rectangle)
1185{
1186 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001187 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001188
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001189 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001190 surface->base.prepare = shm_surface_prepare;
1191 surface->base.swap = shm_surface_swap;
1192 surface->base.acquire = shm_surface_acquire;
1193 surface->base.release = shm_surface_release;
1194 surface->base.destroy = shm_surface_destroy;
1195
1196 surface->display = display;
1197 surface->surface = wl_surface;
1198 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001199
1200 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001201}
1202
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001203/*
1204 * The following correspondences between file names and cursors was copied
1205 * from: https://bugs.kde.org/attachment.cgi?id=67313
1206 */
1207
1208static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001209 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001210 "sw-resize",
1211 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001212};
1213
1214static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001215 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001216 "se-resize",
1217 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001218};
1219
1220static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "s-resize",
1223 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001228 "closedhand",
1229 "208530c400c041818281048008011002"
1230};
1231
1232static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001234 "default",
1235 "top_left_arrow",
1236 "left-arrow"
1237};
1238
1239static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001240 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001241 "w-resize",
1242 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001243};
1244
1245static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001246 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001247 "e-resize",
1248 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001249};
1250
1251static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001252 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001253 "nw-resize",
1254 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001255};
1256
1257static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001258 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001259 "ne-resize",
1260 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001261};
1262
1263static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001264 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001265 "n-resize",
1266 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001267};
1268
1269static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001270 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001271 "ibeam",
1272 "text"
1273};
1274
1275static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001276 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001277 "pointer",
1278 "pointing_hand",
1279 "e29285e634086352946a0e7090d73106"
1280};
1281
1282static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001283 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001284 "wait",
1285 "0426c94ea35c87780ff01dc239897213"
1286};
1287
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001288static const char *move_draggings[] = {
1289 "dnd-move"
1290};
1291
1292static const char *copy_draggings[] = {
1293 "dnd-copy"
1294};
1295
1296static const char *forbidden_draggings[] = {
1297 "dnd-none",
1298 "dnd-no-drop"
1299};
1300
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001301struct cursor_alternatives {
1302 const char **names;
1303 size_t count;
1304};
1305
1306static const struct cursor_alternatives cursors[] = {
1307 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1308 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1309 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1310 {grabbings, ARRAY_LENGTH(grabbings)},
1311 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1312 {left_sides, ARRAY_LENGTH(left_sides)},
1313 {right_sides, ARRAY_LENGTH(right_sides)},
1314 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1315 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1316 {top_sides, ARRAY_LENGTH(top_sides)},
1317 {xterms, ARRAY_LENGTH(xterms)},
1318 {hand1s, ARRAY_LENGTH(hand1s)},
1319 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001320 {move_draggings, ARRAY_LENGTH(move_draggings)},
1321 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1322 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001323};
1324
1325static void
1326create_cursors(struct display *display)
1327{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001328 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001329 struct weston_config *config;
1330 struct weston_config_section *s;
emersionac71ee52018-11-18 21:42:10 +01001331 int size = DEFAULT_XCURSOR_SIZE;
1332 char *theme = NULL, *size_str;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001333 unsigned int i, j;
1334 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001335
emersion807cd2e2018-10-10 09:46:12 +02001336 theme = getenv("XCURSOR_THEME");
emersionac71ee52018-11-18 21:42:10 +01001337
1338 size_str = getenv("XCURSOR_SIZE");
1339 if (size_str) {
1340 safe_strtoint(size_str, &size);
1341 if (size <= 0)
1342 size = DEFAULT_XCURSOR_SIZE;
1343 }
emersion807cd2e2018-10-10 09:46:12 +02001344
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001345 config_file = weston_config_get_name_from_env();
1346 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001347 s = weston_config_get_section(config, "shell", NULL, NULL);
emersion807cd2e2018-10-10 09:46:12 +02001348 weston_config_section_get_string(s, "cursor-theme", &theme, theme);
1349 weston_config_section_get_int(s, "cursor-size", &size, size);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001350 weston_config_destroy(config);
1351
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001352 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001353 if (!display->cursor_theme) {
1354 fprintf(stderr, "could not load theme '%s'\n", theme);
1355 return;
1356 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001357 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001358 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001359 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001360
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001361 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001362 cursor = NULL;
1363 for (j = 0; !cursor && j < cursors[i].count; ++j)
1364 cursor = wl_cursor_theme_get_cursor(
1365 display->cursor_theme, cursors[i].names[j]);
1366
1367 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001368 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001369 cursors[i].names[0]);
1370
1371 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001372 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001373}
1374
1375static void
1376destroy_cursors(struct display *display)
1377{
1378 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001379 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001380}
1381
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001382struct wl_cursor_image *
1383display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001384{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001385 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001386
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001387 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001388}
1389
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001390static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001391surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001392{
Harish Krupo7bcbab12018-12-11 13:45:43 +05301393 struct widget *widget = surface->widget;
Pekka Paalanen89dee002013-02-13 16:17:20 +02001394 if (!surface->cairo_surface)
1395 return;
1396
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001397 if (surface->opaque_region) {
1398 wl_surface_set_opaque_region(surface->surface,
1399 surface->opaque_region);
1400 wl_region_destroy(surface->opaque_region);
1401 surface->opaque_region = NULL;
1402 }
1403
1404 if (surface->input_region) {
1405 wl_surface_set_input_region(surface->surface,
1406 surface->input_region);
1407 wl_region_destroy(surface->input_region);
1408 surface->input_region = NULL;
1409 }
1410
Harish Krupo7bcbab12018-12-11 13:45:43 +05301411 if (surface->viewport) {
1412 wp_viewport_set_destination(surface->viewport,
1413 widget->viewport_dest_width,
1414 widget->viewport_dest_height);
1415 }
1416
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001418 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001419 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001420
Pekka Paalanen89dee002013-02-13 16:17:20 +02001421 cairo_surface_destroy(surface->cairo_surface);
1422 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001423}
1424
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001425int
1426window_has_focus(struct window *window)
1427{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001428 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001429}
1430
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001431static void
1432window_close(struct window *window)
1433{
1434 if (window->close_handler)
1435 window->close_handler(window->user_data);
1436 else
1437 display_exit(window->display);
1438}
1439
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001440struct display *
1441window_get_display(struct window *window)
1442{
1443 return window->display;
1444}
1445
Pekka Paalanen89dee002013-02-13 16:17:20 +02001446static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001447surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001448{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001449 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001450 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001451
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001452 if (!surface->toysurface && display->dpy &&
1453 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001454 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001455 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001456 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001457 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001458 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001459 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001460
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001461 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001462 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001463 surface->surface,
1464 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001465
Pekka Paalanen89dee002013-02-13 16:17:20 +02001466 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001467 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001468 allocation.width, allocation.height, flags,
1469 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001470}
1471
1472static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001473window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001474{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001475 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001476 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001477
Pekka Paalanenec076692012-11-30 13:37:27 +02001478 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001479 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001480
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001481 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001482}
1483
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001484int
1485window_get_buffer_transform(struct window *window)
1486{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001487 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001488}
1489
1490void
1491window_set_buffer_transform(struct window *window,
1492 enum wl_output_transform transform)
1493{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001494 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001495 wl_surface_set_buffer_transform(window->main_surface->surface,
1496 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001497}
1498
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001499void
1500window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001501 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001502{
1503 window->main_surface->buffer_scale = scale;
1504 wl_surface_set_buffer_scale(window->main_surface->surface,
1505 scale);
1506}
1507
1508uint32_t
1509window_get_buffer_scale(struct window *window)
1510{
1511 return window->main_surface->buffer_scale;
1512}
1513
Alexander Larssond68f5232013-05-22 14:41:33 +02001514uint32_t
1515window_get_output_scale(struct window *window)
1516{
1517 struct window_output *window_output;
1518 struct window_output *window_output_tmp;
1519 int scale = 1;
1520
1521 wl_list_for_each_safe(window_output, window_output_tmp,
1522 &window->window_output_list, link) {
1523 if (window_output->output->scale > scale)
1524 scale = window_output->output->scale;
1525 }
1526
1527 return scale;
1528}
1529
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001530static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001531
Pekka Paalanen4e373742013-02-13 16:17:13 +02001532static void
1533surface_destroy(struct surface *surface)
1534{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001535 if (surface->frame_cb)
1536 wl_callback_destroy(surface->frame_cb);
1537
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001538 if (surface->input_region)
1539 wl_region_destroy(surface->input_region);
1540
1541 if (surface->opaque_region)
1542 wl_region_destroy(surface->opaque_region);
1543
Pekka Paalanen35e82632013-04-25 13:57:48 +03001544 if (surface->subsurface)
1545 wl_subsurface_destroy(surface->subsurface);
1546
Pekka Paalanen4e373742013-02-13 16:17:13 +02001547 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001548
1549 if (surface->toysurface)
1550 surface->toysurface->destroy(surface->toysurface);
1551
Pekka Paalanen35e82632013-04-25 13:57:48 +03001552 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001553 free(surface);
1554}
1555
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001556void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001557window_destroy(struct window *window)
1558{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001559 struct display *display = window->display;
1560 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001561 struct window_output *window_output;
1562 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001563
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001564 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001565
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001566 wl_list_for_each(input, &display->input_list, link) {
Jeffy Chen179458a2019-09-02 10:15:26 +08001567 if (input->touch_focus == window) {
1568 struct touch_point *tp, *tmp;
1569
1570 wl_list_for_each_safe(tp, tmp,
1571 &input->touch_point_list,
1572 link) {
1573 wl_list_remove(&tp->link);
1574 free(tp);
1575 }
1576
Rusty Lynch1084da52013-08-15 09:10:08 -07001577 input->touch_focus = NULL;
Jeffy Chen179458a2019-09-02 10:15:26 +08001578 }
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001579 if (input->pointer_focus == window)
1580 input->pointer_focus = NULL;
1581 if (input->keyboard_focus == window)
1582 input->keyboard_focus = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08001583 if (input->locked_window == window)
1584 input->locked_window = NULL;
1585 if (input->confined_window == window)
1586 input->confined_window = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001587 if (input->focus_widget &&
1588 input->focus_widget->window == window)
1589 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001590 }
1591
Rob Bradford7507b572012-05-15 17:55:34 +01001592 wl_list_for_each_safe(window_output, window_output_tmp,
1593 &window->window_output_list, link) {
1594 free (window_output);
1595 }
1596
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001597 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001598 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001599
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001600 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01001601 xdg_toplevel_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001602 if (window->xdg_popup)
ant8mef99fac22018-11-28 22:46:37 +01001603 xdg_popup_destroy(window->xdg_popup);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001604 if (window->xdg_surface)
ant8mef99fac22018-11-28 22:46:37 +01001605 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001606
1607 surface_destroy(window->main_surface);
1608
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001609 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001610
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001611 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001612 free(window);
1613}
1614
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001615static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001616widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001617{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001618 struct widget *child, *target;
Harish Krupo7bcbab12018-12-11 13:45:43 +05301619 int alloc_x, alloc_y, width, height;
1620 double scale;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001621
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001622 wl_list_for_each(child, &widget->child_list, link) {
1623 target = widget_find_widget(child, x, y);
1624 if (target)
1625 return target;
1626 }
1627
Harish Krupo7bcbab12018-12-11 13:45:43 +05301628 alloc_x = widget->allocation.x;
1629 alloc_y = widget->allocation.y;
1630 width = widget->allocation.width;
1631 height = widget->allocation.height;
1632
1633 if (widget->viewport_dest_width != -1 &&
1634 widget->viewport_dest_height != -1) {
1635 scale = widget->viewport_dest_width / (double) width;
1636 alloc_x = alloc_x * scale;
1637 width = widget->viewport_dest_width;
1638
1639 scale = widget->viewport_dest_height / (double) height;
1640 alloc_y = alloc_y * scale;
1641 height = widget->viewport_dest_height;
1642 }
1643
1644 if (alloc_x <= x && x < alloc_x + width &&
1645 alloc_y <= y && y < alloc_y + height) {
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001646 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647 }
1648
1649 return NULL;
1650}
1651
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001652static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001653window_find_widget(struct window *window, int32_t x, int32_t y)
1654{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001655 struct surface *surface;
1656 struct widget *widget;
1657
1658 wl_list_for_each(surface, &window->subsurface_list, link) {
1659 widget = widget_find_widget(surface->widget, x, y);
1660 if (widget)
1661 return widget;
1662 }
1663
1664 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001665}
1666
1667static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001668widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001669{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001670 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001671
Peter Huttererf3d62272013-08-08 11:57:05 +10001672 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001673 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001674 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001675 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001676 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001677 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001678 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001679 widget->tooltip = NULL;
1680 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001681 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001682 widget->use_cairo = 1;
Harish Krupo7bcbab12018-12-11 13:45:43 +05301683 widget->viewport_dest_width = -1;
1684 widget->viewport_dest_height = -1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001685
1686 return widget;
1687}
1688
1689struct widget *
1690window_add_widget(struct window *window, void *data)
1691{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001692 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001693
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001694 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001695 wl_list_init(&widget->link);
1696 window->main_surface->widget = widget;
1697
1698 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001699}
1700
1701struct widget *
1702widget_add_widget(struct widget *parent, void *data)
1703{
1704 struct widget *widget;
1705
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001706 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001707 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001709 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001710}
1711
1712void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001713widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001714{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001715 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001716 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001717 struct input *input;
1718
Pekka Paalanen35e82632013-04-25 13:57:48 +03001719 /* Destroy the sub-surface along with the root widget */
1720 if (surface->widget == widget && surface->subsurface)
1721 surface_destroy(widget->surface);
1722
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001723 if (widget->tooltip)
1724 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001725
Pekka Paalanene156fb62012-01-19 13:51:38 +02001726 wl_list_for_each(input, &display->input_list, link) {
1727 if (input->focus_widget == widget)
1728 input->focus_widget = NULL;
1729 }
1730
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001731 wl_list_remove(&widget->link);
1732 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001733}
1734
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001735void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001736widget_set_default_cursor(struct widget *widget, int cursor)
1737{
1738 widget->default_cursor = cursor;
1739}
1740
1741void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001742widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001743{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001744 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001745}
1746
1747void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001748widget_set_size(struct widget *widget, int32_t width, int32_t height)
1749{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001750 widget->allocation.width = width;
1751 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001752}
1753
1754void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001755widget_set_allocation(struct widget *widget,
1756 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001757{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001758 widget->allocation.x = x;
1759 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001760 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001761}
1762
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001763void
1764widget_set_transparent(struct widget *widget, int transparent)
1765{
1766 widget->opaque = !transparent;
1767}
1768
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001769void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001770widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001771{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001772 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001773}
1774
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001775static cairo_surface_t *
1776widget_get_cairo_surface(struct widget *widget)
1777{
1778 struct surface *surface = widget->surface;
1779 struct window *window = widget->window;
1780
Neil Roberts97b747c2013-12-19 16:17:12 +00001781 assert(widget->use_cairo);
1782
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001783 if (!surface->cairo_surface) {
1784 if (surface == window->main_surface)
1785 window_create_main_surface(window);
1786 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001787 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001788 }
1789
1790 return surface->cairo_surface;
1791}
1792
Alexander Larsson15901f02013-05-22 14:41:25 +02001793static void
1794widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1795{
1796 struct surface *surface = widget->surface;
1797 double angle;
1798 cairo_matrix_t m;
1799 enum wl_output_transform transform;
1800 int surface_width, surface_height;
1801 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001802 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001803
1804 surface_width = surface->allocation.width;
1805 surface_height = surface->allocation.height;
1806
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001807 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001808 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001809
Alexander Larsson15901f02013-05-22 14:41:25 +02001810 switch (transform) {
1811 case WL_OUTPUT_TRANSFORM_FLIPPED:
1812 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1813 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1814 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1815 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1816 break;
1817 default:
1818 cairo_matrix_init_identity(&m);
1819 break;
1820 }
1821
1822 switch (transform) {
1823 case WL_OUTPUT_TRANSFORM_NORMAL:
1824 default:
1825 angle = 0;
1826 translate_x = 0;
1827 translate_y = 0;
1828 break;
1829 case WL_OUTPUT_TRANSFORM_FLIPPED:
1830 angle = 0;
1831 translate_x = surface_width;
1832 translate_y = 0;
1833 break;
1834 case WL_OUTPUT_TRANSFORM_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001835 angle = M_PI + M_PI_2;
1836 translate_x = 0;
1837 translate_y = surface_width;
Alexander Larsson15901f02013-05-22 14:41:25 +02001838 break;
1839 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001840 angle = M_PI + M_PI_2;
1841 translate_x = 0;
1842 translate_y = 0;
Alexander Larsson15901f02013-05-22 14:41:25 +02001843 break;
1844 case WL_OUTPUT_TRANSFORM_180:
1845 angle = M_PI;
1846 translate_x = surface_width;
1847 translate_y = surface_height;
1848 break;
1849 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1850 angle = M_PI;
1851 translate_x = 0;
1852 translate_y = surface_height;
1853 break;
1854 case WL_OUTPUT_TRANSFORM_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001855 angle = M_PI_2;
1856 translate_x = surface_height;
1857 translate_y = 0;
Alexander Larsson15901f02013-05-22 14:41:25 +02001858 break;
1859 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen8060d822020-02-06 15:27:54 +02001860 angle = M_PI_2;
1861 translate_x = surface_height;
1862 translate_y = surface_width;
Alexander Larsson15901f02013-05-22 14:41:25 +02001863 break;
1864 }
1865
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001866 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001867 cairo_translate(cr, translate_x, translate_y);
1868 cairo_rotate(cr, angle);
1869 cairo_transform(cr, &m);
1870}
1871
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001872cairo_t *
1873widget_cairo_create(struct widget *widget)
1874{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001875 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001876 cairo_surface_t *cairo_surface;
1877 cairo_t *cr;
1878
1879 cairo_surface = widget_get_cairo_surface(widget);
1880 cr = cairo_create(cairo_surface);
1881
Alexander Larsson15901f02013-05-22 14:41:25 +02001882 widget_cairo_update_transform(widget, cr);
1883
Pekka Paalanen35e82632013-04-25 13:57:48 +03001884 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1885
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001886 return cr;
1887}
1888
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001889struct wl_surface *
1890widget_get_wl_surface(struct widget *widget)
1891{
1892 return widget->surface->surface;
1893}
1894
Neil Roberts5e10a042013-09-09 00:40:17 +01001895struct wl_subsurface *
1896widget_get_wl_subsurface(struct widget *widget)
1897{
1898 return widget->surface->subsurface;
1899}
1900
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001901uint32_t
1902widget_get_last_time(struct widget *widget)
1903{
1904 return widget->surface->last_time;
1905}
1906
1907void
1908widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1909{
1910 struct wl_compositor *comp = widget->window->display->compositor;
1911 struct surface *surface = widget->surface;
1912
1913 if (!surface->input_region)
1914 surface->input_region = wl_compositor_create_region(comp);
1915
1916 if (rect) {
1917 wl_region_add(surface->input_region,
1918 rect->x, rect->y, rect->width, rect->height);
1919 }
1920}
1921
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001922void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001923widget_set_resize_handler(struct widget *widget,
1924 widget_resize_handler_t handler)
1925{
1926 widget->resize_handler = handler;
1927}
1928
1929void
1930widget_set_redraw_handler(struct widget *widget,
1931 widget_redraw_handler_t handler)
1932{
1933 widget->redraw_handler = handler;
1934}
1935
1936void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001937widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001938{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001939 widget->enter_handler = handler;
1940}
1941
1942void
1943widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1944{
1945 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001946}
1947
1948void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001949widget_set_motion_handler(struct widget *widget,
1950 widget_motion_handler_t handler)
1951{
1952 widget->motion_handler = handler;
1953}
1954
1955void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001956widget_set_button_handler(struct widget *widget,
1957 widget_button_handler_t handler)
1958{
1959 widget->button_handler = handler;
1960}
1961
1962void
Rusty Lynch041815a2013-08-08 21:20:38 -07001963widget_set_touch_up_handler(struct widget *widget,
1964 widget_touch_up_handler_t handler)
1965{
1966 widget->touch_up_handler = handler;
1967}
1968
1969void
1970widget_set_touch_down_handler(struct widget *widget,
1971 widget_touch_down_handler_t handler)
1972{
1973 widget->touch_down_handler = handler;
1974}
1975
1976void
1977widget_set_touch_motion_handler(struct widget *widget,
1978 widget_touch_motion_handler_t handler)
1979{
1980 widget->touch_motion_handler = handler;
1981}
1982
1983void
1984widget_set_touch_frame_handler(struct widget *widget,
1985 widget_touch_frame_handler_t handler)
1986{
1987 widget->touch_frame_handler = handler;
1988}
1989
1990void
1991widget_set_touch_cancel_handler(struct widget *widget,
1992 widget_touch_cancel_handler_t handler)
1993{
1994 widget->touch_cancel_handler = handler;
1995}
1996
1997void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001998widget_set_axis_handler(struct widget *widget,
1999 widget_axis_handler_t handler)
2000{
2001 widget->axis_handler = handler;
2002}
2003
Peter Hutterer87743e92016-01-18 16:38:22 +10002004void
2005widget_set_pointer_frame_handler(struct widget *widget,
2006 widget_pointer_frame_handler_t handler)
2007{
2008 widget->pointer_frame_handler = handler;
2009}
2010
2011void
2012widget_set_axis_handlers(struct widget *widget,
2013 widget_axis_handler_t axis_handler,
2014 widget_axis_source_handler_t axis_source_handler,
2015 widget_axis_stop_handler_t axis_stop_handler,
2016 widget_axis_discrete_handler_t axis_discrete_handler)
2017{
2018 widget->axis_handler = axis_handler;
2019 widget->axis_source_handler = axis_source_handler;
2020 widget->axis_stop_handler = axis_stop_handler;
2021 widget->axis_discrete_handler = axis_discrete_handler;
2022}
2023
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002024static void
2025window_schedule_redraw_task(struct window *window);
2026
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002027void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002028widget_schedule_redraw(struct widget *widget)
2029{
Pekka Paalanen71233882013-04-25 13:57:53 +03002030 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002031 widget->surface->redraw_needed = 1;
2032 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002033}
2034
Neil Roberts97b747c2013-12-19 16:17:12 +00002035void
2036widget_set_use_cairo(struct widget *widget,
2037 int use_cairo)
2038{
2039 widget->use_cairo = use_cairo;
2040}
2041
Harish Krupo7bcbab12018-12-11 13:45:43 +05302042int
2043widget_set_viewport_destination(struct widget *widget, int width, int height)
2044{
2045 struct window *window = widget->window;
2046 struct display *display = window->display;
2047 struct surface *surface = widget->surface;
2048 if (!display->viewporter)
2049 return -1;
2050
2051 if (width == -1 && height == -1) {
2052 if (surface->viewport) {
2053 wp_viewport_destroy(surface->viewport);
2054 surface->viewport = NULL;
2055 }
2056
2057 widget->viewport_dest_width = -1;
2058 widget->viewport_dest_height = -1;
2059 return 0;
2060 }
2061
2062 if (!surface->viewport) {
2063 surface->viewport = wp_viewporter_get_viewport(display->viewporter,
2064 surface->surface);
2065 if (!surface->viewport)
2066 return -1;
2067 }
2068
2069 widget->viewport_dest_width = width;
2070 widget->viewport_dest_height = height;
2071
2072 return 0;
2073}
2074
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002075cairo_surface_t *
2076window_get_surface(struct window *window)
2077{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002078 cairo_surface_t *cairo_surface;
2079
2080 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2081
2082 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002083}
2084
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002085struct wl_surface *
2086window_get_wl_surface(struct window *window)
2087{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002088 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002089}
2090
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002091static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002092tooltip_redraw_handler(struct widget *widget, void *data)
2093{
2094 cairo_t *cr;
2095 const int32_t r = 3;
2096 struct tooltip *tooltip = data;
2097 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002098
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002099 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002100 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002101 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2102 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2103 cairo_paint(cr);
2104
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002105 width = widget->allocation.width;
2106 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002107 rounded_rect(cr, 0, 0, width, height, r);
2108
2109 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2110 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2111 cairo_fill(cr);
2112
-c505af82019-06-26 21:00:43 +00002113 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.85);
2114 cairo_move_to(cr, 10, 17);
2115 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002116 cairo_show_text(cr, tooltip->entry);
2117 cairo_destroy(cr);
2118}
2119
2120static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002121get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002122{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002123 cairo_t *cr;
2124 cairo_text_extents_t extents;
2125
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002126 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002127 * created yet, and parent does not have a valid surface
2128 * outside repaint, either.
2129 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002130 cr = cairo_create(display->dummy_surface);
-c505af82019-06-26 21:00:43 +00002131 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002132 cairo_text_extents(cr, tooltip->entry, &extents);
2133 cairo_destroy(cr);
2134
2135 return extents;
2136}
2137
2138static int
2139window_create_tooltip(struct tooltip *tooltip)
2140{
2141 struct widget *parent = tooltip->parent;
2142 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002143 const int offset_y = 27;
2144 const int margin = 3;
2145 cairo_text_extents_t extents;
2146
2147 if (tooltip->widget)
2148 return 0;
2149
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002150 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002151
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002152 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002153 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002154 widget_set_allocation(tooltip->widget,
2155 tooltip->x, tooltip->y + offset_y,
2156 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002157
2158 return 0;
2159}
2160
2161void
2162widget_destroy_tooltip(struct widget *parent)
2163{
2164 struct tooltip *tooltip = parent->tooltip;
2165
2166 parent->tooltip_count = 0;
2167 if (!tooltip)
2168 return;
2169
2170 if (tooltip->widget) {
2171 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002172 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002173 }
2174
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002175 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002176 free(tooltip->entry);
2177 free(tooltip);
2178 parent->tooltip = NULL;
2179}
2180
2181static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002182tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002183{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002184 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002185
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002186 window_create_tooltip(tooltip);
2187}
2188
2189#define TOOLTIP_TIMEOUT 500
2190static int
2191tooltip_timer_reset(struct tooltip *tooltip)
2192{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002193 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002194
2195 return 0;
2196}
2197
2198int
2199widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2200{
2201 struct tooltip *tooltip = parent->tooltip;
2202
2203 parent->tooltip_count++;
2204 if (tooltip) {
2205 tooltip->x = x;
2206 tooltip->y = y;
2207 tooltip_timer_reset(tooltip);
2208 return 0;
2209 }
2210
2211 /* the handler might be triggered too fast via input device motion, so
2212 * we need this check here to make sure tooltip is fully initialized */
2213 if (parent->tooltip_count > 1)
2214 return 0;
2215
2216 tooltip = malloc(sizeof *tooltip);
2217 if (!tooltip)
2218 return -1;
2219
2220 parent->tooltip = tooltip;
2221 tooltip->parent = parent;
2222 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002223 tooltip->x = x;
2224 tooltip->y = y;
2225 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002226 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2227 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002228 tooltip_timer_reset(tooltip);
2229
2230 return 0;
2231}
2232
2233static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002234frame_resize_handler(struct widget *widget,
2235 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002236{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002237 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002238 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002239 struct rectangle interior;
2240 struct rectangle input;
2241 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002242
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002243 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002244 interior.x = 0;
2245 interior.y = 0;
2246 interior.width = width;
2247 interior.height = height;
2248 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002249 frame_resize(frame->frame, width, height);
2250 frame_interior(frame->frame, &interior.x, &interior.y,
2251 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002252 }
2253
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002254 widget_set_allocation(child, interior.x, interior.y,
2255 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002256
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002257 if (child->resize_handler) {
2258 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002259 child->user_data);
2260
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002261 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002262 width = child->allocation.width;
2263 height = child->allocation.height;
2264 } else {
2265 frame_resize_inside(frame->frame,
2266 child->allocation.width,
2267 child->allocation.height);
2268 width = frame_width(frame->frame);
2269 height = frame_height(frame->frame);
2270 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002271 }
2272
Scott Moreauf7e498c2012-05-14 11:39:29 -06002273 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002274
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002275 widget->surface->input_region =
2276 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002277 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002278 frame_input_rect(frame->frame, &input.x, &input.y,
2279 &input.width, &input.height);
2280 wl_region_add(widget->surface->input_region,
2281 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002282 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002283 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002284 }
2285
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002286 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002287
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002288 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002289 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002290 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2291 &opaque.width, &opaque.height);
2292
2293 wl_region_add(widget->surface->opaque_region,
2294 opaque.x, opaque.y,
2295 opaque.width, opaque.height);
2296 } else {
2297 wl_region_add(widget->surface->opaque_region,
2298 0, 0, width, height);
2299 }
Martin Minarik1998b152012-05-10 02:04:35 +02002300 }
2301
Martin Minarik1998b152012-05-10 02:04:35 +02002302
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002303 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002304}
2305
2306static void
2307frame_redraw_handler(struct widget *widget, void *data)
2308{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002309 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002310 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002311 struct window *window = widget->window;
2312
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002313 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002314 return;
2315
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002316 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002317
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002318 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002319
2320 cairo_destroy(cr);
2321}
2322
2323static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002324frame_get_pointer_image_for_location(struct window_frame *frame,
2325 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002326{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002327 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002328
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002329 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002330 return CURSOR_LEFT_PTR;
2331
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002332 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002333 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002334 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002335 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002336 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002337 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002338 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002339 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002340 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002341 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002342 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002343 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002344 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002345 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002346 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002347 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002348 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002349 case THEME_LOCATION_EXTERIOR:
2350 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002351 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002352 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002353 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002354}
2355
Pekka Paalanen26237862014-09-10 15:10:30 +03002356static void
2357frame_menu_func(void *data, struct input *input, int index)
2358{
2359 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002360
2361 switch (index) {
2362 case 0: /* close */
2363 window_close(window);
2364 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002365 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002366 /* we don't have a way to get out of fullscreen for now */
2367 if (window->fullscreen_handler)
2368 window->fullscreen_handler(window, window->user_data);
2369 break;
2370 }
2371}
2372
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002373void
2374window_show_frame_menu(struct window *window,
2375 struct input *input, uint32_t time)
2376{
2377 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002378 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002379
Pekka Paalanen26237862014-09-10 15:10:30 +03002380 static const char *entries[] = {
2381 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002382 "Fullscreen"
2383 };
2384
2385 if (window->fullscreen_handler)
2386 count = ARRAY_LENGTH(entries);
2387 else
2388 count = ARRAY_LENGTH(entries) - 1;
2389
2390 input_get_position(input, &x, &y);
2391 window_show_menu(window->display, input, time, window,
2392 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002393}
2394
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002395static int
2396frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002397 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002398{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002399 struct window_frame *frame = data;
2400 enum theme_location location;
2401
2402 location = frame_pointer_enter(frame->frame, input, x, y);
2403 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2404 widget_schedule_redraw(frame->widget);
2405
2406 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002407}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002408
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002409static int
2410frame_motion_handler(struct widget *widget,
2411 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002412 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002413{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002414 struct window_frame *frame = data;
2415 enum theme_location location;
2416
2417 location = frame_pointer_motion(frame->frame, input, x, y);
2418 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2419 widget_schedule_redraw(frame->widget);
2420
2421 return frame_get_pointer_image_for_location(data, location);
2422}
2423
2424static void
2425frame_leave_handler(struct widget *widget,
2426 struct input *input, void *data)
2427{
2428 struct window_frame *frame = data;
2429
2430 frame_pointer_leave(frame->frame, input);
2431 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2432 widget_schedule_redraw(frame->widget);
2433}
2434
2435static void
2436frame_handle_status(struct window_frame *frame, struct input *input,
2437 uint32_t time, enum theme_location location)
2438{
2439 struct window *window = frame->widget->window;
2440 uint32_t status;
2441
2442 status = frame_status(frame->frame);
2443 if (status & FRAME_STATUS_REPAINT)
2444 widget_schedule_redraw(frame->widget);
2445
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002446 if (status & FRAME_STATUS_MINIMIZE) {
2447 window_set_minimized(window);
2448 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2449 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002450
2451 if (status & FRAME_STATUS_MENU) {
2452 window_show_frame_menu(window, input, time);
2453 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2454 }
2455
2456 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002457 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002458 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2459 }
2460
2461 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002462 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002463 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002464 }
2465
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002466 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002467 input_ungrab(input);
ant8mef99fac22018-11-28 22:46:37 +01002468 xdg_toplevel_move(window->xdg_toplevel,
2469 input_get_seat(input),
2470 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002471
2472 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2473 }
2474
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002475 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002476 input_ungrab(input);
2477
ant8mef99fac22018-11-28 22:46:37 +01002478 xdg_toplevel_resize(window->xdg_toplevel,
2479 input_get_seat(input),
2480 window->display->serial,
2481 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002482
2483 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2484 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002485}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002486
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002487#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002488static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002489frame_button_handler(struct widget *widget,
2490 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002491 uint32_t button, enum wl_pointer_button_state state,
2492 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002493
2494{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002495 struct window_frame *frame = data;
2496 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002497
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002498 frame->double_click = 0;
2499 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2500 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2501 frame->double_click = 1;
2502 frame->did_double = 1;
2503 } else
2504 frame->did_double = 0;
2505
2506 frame->last_time = time;
2507 } else if (frame->did_double == 1) {
2508 frame->double_click = 1;
2509 frame->did_double = 0;
2510 }
2511
2512 if (frame->double_click)
2513 location = frame_double_click(frame->frame, input,
2514 button, state);
2515 else
2516 location = frame_pointer_button(frame->frame, input,
2517 button, state);
2518
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002519 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002520}
2521
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002522static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002523frame_touch_down_handler(struct widget *widget, struct input *input,
2524 uint32_t serial, uint32_t time, int32_t id,
2525 float x, float y, void *data)
2526{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002527 struct window_frame *frame = data;
2528
Xiong Zhang382de462014-06-12 11:06:26 +08002529 frame->double_click = 0;
2530 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2531 frame->last_id == id) {
2532 frame->double_click = 1;
2533 frame->did_double = 1;
2534 frame->double_id = id;
2535 } else
2536 frame->did_double = 0;
2537
2538 frame->last_time = time;
2539 frame->last_id = id;
2540
2541 if (frame->double_click)
2542 frame_double_touch_down(frame->frame, input, id, x, y);
2543 else
2544 frame_touch_down(frame->frame, input, id, x, y);
2545
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002546 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2547}
2548
2549static void
2550frame_touch_up_handler(struct widget *widget,
2551 struct input *input, uint32_t serial, uint32_t time,
2552 int32_t id, void *data)
2553{
2554 struct window_frame *frame = data;
2555
Xiong Zhang382de462014-06-12 11:06:26 +08002556 if (frame->double_id == id && frame->did_double) {
2557 frame->did_double = 0;
2558 frame->double_id = 0;
2559 frame_double_touch_up(frame->frame, input, id);
2560 } else
2561 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002562 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002563}
2564
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002565struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002566window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002567{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002568 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002569 uint32_t buttons;
2570
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002571 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002572 buttons = FRAME_BUTTON_NONE;
2573 } else {
2574 buttons = FRAME_BUTTON_ALL;
2575 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002576
Peter Huttererf3d62272013-08-08 11:57:05 +10002577 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002578 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002579 buttons, window->title, NULL);
Simon Ser598d3a12020-02-10 15:49:20 +01002580 if (!frame->frame) {
2581 free(frame);
2582 return NULL;
2583 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002584
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002585 frame->widget = window_add_widget(window, frame);
2586 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002587
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002588 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2589 widget_set_resize_handler(frame->widget, frame_resize_handler);
2590 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002591 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002592 widget_set_motion_handler(frame->widget, frame_motion_handler);
2593 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002594 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002595 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002596
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002597 window->frame = frame;
2598
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002599 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002600}
2601
Kristian Høgsberga1627922012-06-20 17:30:03 -04002602void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002603window_frame_set_child_size(struct widget *widget, int child_width,
2604 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002605{
2606 struct display *display = widget->window->display;
2607 struct theme *t = display->theme;
2608 int decoration_width, decoration_height;
2609 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002610 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002611
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002612 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002613 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002614 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002615 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002616
2617 width = child_width + decoration_width;
2618 height = child_height + decoration_height;
2619 } else {
2620 width = child_width;
2621 height = child_height;
2622 }
2623
2624 window_schedule_resize(widget->window, width, height);
2625}
2626
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002627static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002628window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002629{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002630 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002631
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002632 /* frame->child must be destroyed by the application */
2633 widget_destroy(frame->widget);
2634 free(frame);
2635}
2636
2637static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002638input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002639 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002640{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002641 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002642 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002643
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002644 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002645 return;
2646
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002647 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002648 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002649 widget = old;
2650 if (input->grab)
2651 widget = input->grab;
2652 if (widget->leave_handler)
2653 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002654 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002655 }
2656
2657 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002658 widget = focus;
2659 if (input->grab)
2660 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002661 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002662 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002663 cursor = widget->enter_handler(focus, input, x, y,
2664 widget->user_data);
2665 else
2666 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002667
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002668 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002669 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002670}
2671
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002672void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002673touch_grab(struct input *input, int32_t touch_id)
2674{
2675 input->touch_grab = 1;
2676 input->touch_grab_id = touch_id;
2677}
2678
2679void
2680touch_ungrab(struct input *input)
2681{
2682 struct touch_point *tp, *tmp;
2683
2684 input->touch_grab = 0;
2685
2686 wl_list_for_each_safe(tp, tmp,
2687 &input->touch_point_list, link) {
2688 if (tp->id != input->touch_grab_id)
2689 continue;
2690 wl_list_remove(&tp->link);
2691 free(tp);
2692
2693 return;
2694 }
2695}
2696
2697void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002698input_grab(struct input *input, struct widget *widget, uint32_t button)
2699{
2700 input->grab = widget;
2701 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002702
2703 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002704}
2705
2706void
2707input_ungrab(struct input *input)
2708{
2709 struct widget *widget;
2710
2711 input->grab = NULL;
2712 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002713 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002714 input->sx, input->sy);
2715 input_set_focus_widget(input, widget, input->sx, input->sy);
2716 }
2717}
2718
2719static void
Derek Foreman118a4292015-04-22 17:23:35 -05002720cursor_delay_timer_reset(struct input *input, uint32_t duration)
2721{
Derek Foreman118a4292015-04-22 17:23:35 -05002722 if (!duration)
2723 input->cursor_timer_running = false;
2724 else
2725 input->cursor_timer_running = true;
2726
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002727 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002728}
2729
2730static void cancel_pointer_image_update(struct input *input)
2731{
2732 if (input->cursor_timer_running)
2733 cursor_delay_timer_reset(input, 0);
2734}
2735
2736static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002737input_remove_pointer_focus(struct input *input)
2738{
2739 struct window *window = input->pointer_focus;
2740
2741 if (!window)
2742 return;
2743
2744 input_set_focus_widget(input, NULL, 0, 0);
2745
2746 input->pointer_focus = NULL;
2747 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002748 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002749}
2750
2751static void
2752pointer_handle_enter(void *data, struct wl_pointer *pointer,
2753 uint32_t serial, struct wl_surface *surface,
2754 wl_fixed_t sx_w, wl_fixed_t sy_w)
2755{
2756 struct input *input = data;
2757 struct window *window;
2758 struct widget *widget;
2759 float sx = wl_fixed_to_double(sx_w);
2760 float sy = wl_fixed_to_double(sy_w);
2761
2762 if (!surface) {
2763 /* enter event for a window we've just destroyed */
2764 return;
2765 }
2766
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002767 window = wl_surface_get_user_data(surface);
2768 if (surface != window->main_surface->surface) {
2769 DBG("Ignoring input event from subsurface %p\n", surface);
2770 return;
2771 }
2772
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002773 input->display->serial = serial;
2774 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002775 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002776
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002777 input->sx = sx;
2778 input->sy = sy;
2779
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002780 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002781 input_set_focus_widget(input, widget, sx, sy);
2782}
2783
2784static void
2785pointer_handle_leave(void *data, struct wl_pointer *pointer,
2786 uint32_t serial, struct wl_surface *surface)
2787{
2788 struct input *input = data;
2789
2790 input->display->serial = serial;
2791 input_remove_pointer_focus(input);
2792}
2793
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002794static void
Daniel Stone37816df2012-05-16 18:45:18 +01002795pointer_handle_motion(void *data, struct wl_pointer *pointer,
2796 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002797{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002798 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002799 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002800 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002801 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002802 float sx = wl_fixed_to_double(sx_w);
2803 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002804
Paul Winwoodb22bf572013-08-29 10:52:54 +01002805 if (!window)
2806 return;
2807
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002808 input->sx = sx;
2809 input->sy = sy;
2810
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002811 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002812 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002813 * based on the old surface dimensions. However, if we have an active
2814 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002815 */
Derek Foreman46812b62015-08-26 17:13:27 -05002816 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002817 sy < window->main_surface->allocation.y ||
2818 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002819 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002820 return;
2821
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002822 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002823 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002824 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002825 }
2826
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002827 if (input->grab)
2828 widget = input->grab;
2829 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002830 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002831 if (widget) {
2832 if (widget->motion_handler)
2833 cursor = widget->motion_handler(input->focus_widget,
2834 input, time, sx, sy,
2835 widget->user_data);
2836 else
2837 cursor = widget->default_cursor;
2838 } else
2839 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002840
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002841 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002842}
2843
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002844static void
Daniel Stone37816df2012-05-16 18:45:18 +01002845pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002846 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002847{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002848 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002849 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002850 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002851
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002852 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002853 if (input->focus_widget && input->grab == NULL &&
2854 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002855 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002856
Neil Roberts6b28aad2012-01-23 19:11:18 +00002857 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002858 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002859 (*widget->button_handler)(widget,
2860 input, time,
2861 button, state,
2862 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002863
Daniel Stone4dbadb12012-05-30 16:31:51 +01002864 if (input->grab && input->grab_button == button &&
2865 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002866 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002867}
2868
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002869static void
Daniel Stone37816df2012-05-16 18:45:18 +01002870pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002871 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002872{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002873 struct input *input = data;
2874 struct widget *widget;
2875
2876 widget = input->focus_widget;
2877 if (input->grab)
2878 widget = input->grab;
2879 if (widget && widget->axis_handler)
2880 (*widget->axis_handler)(widget,
2881 input, time,
2882 axis, value,
2883 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002884}
2885
Peter Hutterer87743e92016-01-18 16:38:22 +10002886static void
2887pointer_handle_frame(void *data, struct wl_pointer *pointer)
2888{
2889 struct input *input = data;
2890 struct widget *widget;
2891
2892 widget = input->focus_widget;
2893 if (input->grab)
2894 widget = input->grab;
2895 if (widget && widget->pointer_frame_handler)
2896 (*widget->pointer_frame_handler)(widget,
2897 input,
2898 widget->user_data);
2899}
2900
2901static void
2902pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2903 uint32_t source)
2904{
2905 struct input *input = data;
2906 struct widget *widget;
2907
2908 widget = input->focus_widget;
2909 if (input->grab)
2910 widget = input->grab;
2911 if (widget && widget->axis_source_handler)
2912 (*widget->axis_source_handler)(widget,
2913 input,
2914 source,
2915 widget->user_data);
2916}
2917
2918static void
2919pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2920 uint32_t time, uint32_t axis)
2921{
2922 struct input *input = data;
2923 struct widget *widget;
2924
2925 widget = input->focus_widget;
2926 if (input->grab)
2927 widget = input->grab;
2928 if (widget && widget->axis_stop_handler)
2929 (*widget->axis_stop_handler)(widget,
2930 input, time,
2931 axis,
2932 widget->user_data);
2933}
2934
2935static void
2936pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2937 uint32_t axis, int32_t discrete)
2938{
2939 struct input *input = data;
2940 struct widget *widget;
2941
2942 widget = input->focus_widget;
2943 if (input->grab)
2944 widget = input->grab;
2945 if (widget && widget->axis_discrete_handler)
2946 (*widget->axis_discrete_handler)(widget,
2947 input,
2948 axis,
2949 discrete,
2950 widget->user_data);
2951}
2952
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002953static const struct wl_pointer_listener pointer_listener = {
2954 pointer_handle_enter,
2955 pointer_handle_leave,
2956 pointer_handle_motion,
2957 pointer_handle_button,
2958 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002959 pointer_handle_frame,
2960 pointer_handle_axis_source,
2961 pointer_handle_axis_stop,
2962 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002963};
2964
2965static void
2966input_remove_keyboard_focus(struct input *input)
2967{
2968 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002969
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002970 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002971
2972 if (!window)
2973 return;
2974
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002975 if (window->keyboard_focus_handler)
2976 (*window->keyboard_focus_handler)(window, NULL,
2977 window->user_data);
2978
2979 input->keyboard_focus = NULL;
2980}
2981
2982static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002983keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002984{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002985 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002986 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002987
2988 if (window && window->key_handler) {
2989 (*window->key_handler)(window, input, input->repeat_time,
2990 input->repeat_key, input->repeat_sym,
2991 WL_KEYBOARD_KEY_STATE_PRESSED,
2992 window->user_data);
2993 }
2994}
2995
2996static void
2997keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2998 uint32_t format, int fd, uint32_t size)
2999{
3000 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02003001 struct xkb_keymap *keymap;
3002 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01003003#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003004 struct xkb_compose_table *compose_table;
3005 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01003006#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003007 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003008 char *map_str;
3009
3010 if (!data) {
3011 close(fd);
3012 return;
3013 }
3014
3015 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3016 close(fd);
3017 return;
3018 }
3019
Sebastian Wickac1b92d2019-11-01 02:27:55 +01003020 map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003021 if (map_str == MAP_FAILED) {
3022 close(fd);
3023 return;
3024 }
3025
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003026 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03003027 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
3028 map_str,
3029 XKB_KEYMAP_FORMAT_TEXT_V1,
3030 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003031 munmap(map_str, size);
3032 close(fd);
3033
Rui Matos3eccb862013-10-10 19:44:22 +02003034 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003035 fprintf(stderr, "failed to compile keymap\n");
3036 return;
3037 }
3038
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003039 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02003040 state = xkb_state_new(keymap);
3041 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003042 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003043 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003044 return;
3045 }
3046
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003047 /* Look up the preferred locale, falling back to "C" as default */
3048 if (!(locale = getenv("LC_ALL")))
3049 if (!(locale = getenv("LC_CTYPE")))
3050 if (!(locale = getenv("LANG")))
3051 locale = "C";
3052
3053 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003054#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003055 compose_table =
3056 xkb_compose_table_new_from_locale(input->display->xkb_context,
3057 locale,
3058 XKB_COMPOSE_COMPILE_NO_FLAGS);
3059 if (compose_table) {
3060 /* Set up XKB compose state */
3061 compose_state = xkb_compose_state_new(compose_table,
3062 XKB_COMPOSE_STATE_NO_FLAGS);
3063 if (compose_state) {
3064 xkb_compose_state_unref(input->xkb.compose_state);
3065 xkb_compose_table_unref(input->xkb.compose_table);
3066 input->xkb.compose_state = compose_state;
3067 input->xkb.compose_table = compose_table;
3068 } else {
3069 fprintf(stderr, "could not create XKB compose state. "
3070 "Disabiling compose.\n");
3071 xkb_compose_table_unref(compose_table);
3072 compose_table = NULL;
3073 }
3074 } else {
3075 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3076 "Disabiling compose\n", locale);
3077 }
Daniel Stone5b015962016-10-20 14:45:58 +01003078#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003079
Rui Matos3eccb862013-10-10 19:44:22 +02003080 xkb_keymap_unref(input->xkb.keymap);
3081 xkb_state_unref(input->xkb.state);
3082 input->xkb.keymap = keymap;
3083 input->xkb.state = state;
3084
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003085 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003086 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003087 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003088 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003089 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003090 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003091}
3092
3093static void
3094keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3095 uint32_t serial, struct wl_surface *surface,
3096 struct wl_array *keys)
3097{
3098 struct input *input = data;
3099 struct window *window;
3100
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003101 if (!surface) {
3102 /* enter event for a window we've just destroyed */
3103 return;
3104 }
3105
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003106 input->display->serial = serial;
3107 input->keyboard_focus = wl_surface_get_user_data(surface);
3108
3109 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003110 if (window->keyboard_focus_handler)
3111 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003112 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003113}
3114
3115static void
3116keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3117 uint32_t serial, struct wl_surface *surface)
3118{
3119 struct input *input = data;
3120
3121 input->display->serial = serial;
3122 input_remove_keyboard_focus(input);
3123}
3124
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003125/* Translate symbols appropriately if a compose sequence is being entered */
3126static xkb_keysym_t
3127process_key_press(xkb_keysym_t sym, struct input *input)
3128{
Daniel Stone5b015962016-10-20 14:45:58 +01003129#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003130 if (!input->xkb.compose_state)
3131 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003132 if (sym == XKB_KEY_NoSymbol)
3133 return sym;
3134 if (xkb_compose_state_feed(input->xkb.compose_state,
3135 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3136 return sym;
3137
3138 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3139 case XKB_COMPOSE_COMPOSING:
3140 return XKB_KEY_NoSymbol;
3141 case XKB_COMPOSE_COMPOSED:
3142 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3143 case XKB_COMPOSE_CANCELLED:
3144 return XKB_KEY_NoSymbol;
3145 case XKB_COMPOSE_NOTHING:
3146 return sym;
3147 default:
3148 return sym;
3149 }
Daniel Stone5b015962016-10-20 14:45:58 +01003150#else
3151 return sym;
3152#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003153}
3154
Scott Moreau210d0792012-03-22 10:47:01 -06003155static void
Daniel Stone37816df2012-05-16 18:45:18 +01003156keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003157 uint32_t serial, uint32_t time, uint32_t key,
3158 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003159{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003160 struct input *input = data;
3161 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003162 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003163 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003164 const xkb_keysym_t *syms;
3165 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003166 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003167
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003168 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003169 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003170 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003171 return;
3172
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003173 /* We only use input grabs for pointer events for now, so just
3174 * ignore key presses if a grab is active. We expand the key
3175 * event delivery mechanism to route events to widgets to
3176 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003177 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003178 if (input->grab && input->grab_button == 0)
3179 return;
3180
Ran Benita2e1968f2014-08-19 23:59:51 +03003181 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003182
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003183 sym = XKB_KEY_NoSymbol;
3184 if (num_syms == 1)
3185 sym = syms[0];
3186
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003187
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003188 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003189 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003190 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003191 } else if (sym == XKB_KEY_F11 &&
3192 window->fullscreen_handler &&
3193 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3194 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003195 } else if (sym == XKB_KEY_F4 &&
3196 input->modifiers == MOD_ALT_MASK &&
3197 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003198 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003199 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003200 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3201 sym = process_key_press(sym, input);
3202
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003203 (*window->key_handler)(window, input, time, key,
3204 sym, state, window->user_data);
3205 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003206
3207 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3208 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003209 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003210 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3211 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003212 input->repeat_sym = sym;
3213 input->repeat_key = key;
3214 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003215 its.it_interval.tv_sec = input->repeat_rate_sec;
3216 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3217 its.it_value.tv_sec = input->repeat_delay_sec;
3218 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003219 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003220 }
3221}
3222
3223static void
Daniel Stone351eb612012-05-31 15:27:47 -04003224keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3225 uint32_t serial, uint32_t mods_depressed,
3226 uint32_t mods_latched, uint32_t mods_locked,
3227 uint32_t group)
3228{
3229 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003230 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003231
Matt Ropere61561f2013-06-24 16:52:43 +01003232 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3233 if (!input->xkb.keymap)
3234 return;
3235
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003236 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3237 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003238 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003239 XKB_STATE_MODS_DEPRESSED |
3240 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003241 input->modifiers = 0;
3242 if (mask & input->xkb.control_mask)
3243 input->modifiers |= MOD_CONTROL_MASK;
3244 if (mask & input->xkb.alt_mask)
3245 input->modifiers |= MOD_ALT_MASK;
3246 if (mask & input->xkb.shift_mask)
3247 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003248}
3249
Jonny Lamb06959082014-08-12 14:58:27 +02003250static void
3251set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3252{
3253 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3254 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3255
3256 /* a rate of zero disables any repeating, regardless of the delay's
3257 * value */
3258 if (rate == 0)
3259 return;
3260
3261 if (rate == 1)
3262 input->repeat_rate_sec = 1;
3263 else
3264 input->repeat_rate_nsec = 1000000000 / rate;
3265
3266 input->repeat_delay_sec = delay / 1000;
3267 delay -= (input->repeat_delay_sec * 1000);
3268 input->repeat_delay_nsec = delay * 1000 * 1000;
3269}
3270
3271static void
3272keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3273 int32_t rate, int32_t delay)
3274{
3275 struct input *input = data;
3276
3277 set_repeat_info(input, rate, delay);
3278}
3279
Daniel Stone37816df2012-05-16 18:45:18 +01003280static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003281 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003282 keyboard_handle_enter,
3283 keyboard_handle_leave,
3284 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003285 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003286 keyboard_handle_repeat_info
3287
Daniel Stone37816df2012-05-16 18:45:18 +01003288};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003289
3290static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003291touch_handle_down(void *data, struct wl_touch *wl_touch,
3292 uint32_t serial, uint32_t time, struct wl_surface *surface,
3293 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3294{
3295 struct input *input = data;
3296 struct widget *widget;
3297 float sx = wl_fixed_to_double(x_w);
3298 float sy = wl_fixed_to_double(y_w);
3299
Jeffy Chen97749172019-09-02 10:26:09 +08003300 if (!surface) {
3301 /* down event for a window we've just destroyed */
3302 return;
3303 }
3304
Rusty Lynch1084da52013-08-15 09:10:08 -07003305 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003306 input->touch_focus = wl_surface_get_user_data(surface);
3307 if (!input->touch_focus) {
3308 DBG("Failed to find to touch focus for surface %p\n", surface);
3309 return;
3310 }
3311
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003312 if (surface != input->touch_focus->main_surface->surface) {
3313 DBG("Ignoring input event from subsurface %p\n", surface);
3314 input->touch_focus = NULL;
3315 return;
3316 }
3317
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003318 if (input->grab)
3319 widget = input->grab;
3320 else
3321 widget = window_find_widget(input->touch_focus,
3322 wl_fixed_to_double(x_w),
3323 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003324 if (widget) {
3325 struct touch_point *tp = xmalloc(sizeof *tp);
3326 if (tp) {
3327 tp->id = id;
3328 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003329 tp->x = sx;
3330 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003331 wl_list_insert(&input->touch_point_list, &tp->link);
3332
3333 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003334 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003335 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003336 sx, sy,
3337 widget->user_data);
3338 }
3339 }
3340}
3341
3342static void
3343touch_handle_up(void *data, struct wl_touch *wl_touch,
3344 uint32_t serial, uint32_t time, int32_t id)
3345{
3346 struct input *input = data;
3347 struct touch_point *tp, *tmp;
3348
Rusty Lynch041815a2013-08-08 21:20:38 -07003349 if (!input->touch_focus) {
3350 DBG("No touch focus found for touch up event!\n");
3351 return;
3352 }
3353
3354 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3355 if (tp->id != id)
3356 continue;
3357
3358 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003359 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003360 time, id,
3361 tp->widget->user_data);
3362
3363 wl_list_remove(&tp->link);
3364 free(tp);
3365
3366 return;
3367 }
3368}
3369
3370static void
3371touch_handle_motion(void *data, struct wl_touch *wl_touch,
3372 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3373{
3374 struct input *input = data;
3375 struct touch_point *tp;
3376 float sx = wl_fixed_to_double(x_w);
3377 float sy = wl_fixed_to_double(y_w);
3378
3379 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3380
3381 if (!input->touch_focus) {
3382 DBG("No touch focus found for touch motion event!\n");
3383 return;
3384 }
3385
3386 wl_list_for_each(tp, &input->touch_point_list, link) {
3387 if (tp->id != id)
3388 continue;
3389
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003390 tp->x = sx;
3391 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003392 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003393 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003394 id, sx, sy,
3395 tp->widget->user_data);
3396 return;
3397 }
3398}
3399
3400static void
3401touch_handle_frame(void *data, struct wl_touch *wl_touch)
3402{
3403 struct input *input = data;
3404 struct touch_point *tp, *tmp;
3405
3406 DBG("touch_handle_frame\n");
3407
3408 if (!input->touch_focus) {
3409 DBG("No touch focus found for touch frame event!\n");
3410 return;
3411 }
3412
3413 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3414 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003415 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003416 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003417 }
3418}
3419
3420static void
3421touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3422{
3423 struct input *input = data;
3424 struct touch_point *tp, *tmp;
3425
3426 DBG("touch_handle_cancel\n");
3427
3428 if (!input->touch_focus) {
3429 DBG("No touch focus found for touch cancel event!\n");
3430 return;
3431 }
3432
3433 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3434 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003435 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3436 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003437
3438 wl_list_remove(&tp->link);
3439 free(tp);
3440 }
3441}
3442
Guillaume Champagne1cb09482020-01-27 20:12:16 -05003443static void
3444touch_handle_shape(void *data, struct wl_touch *wl_touch, int32_t id,
3445 wl_fixed_t major, wl_fixed_t minor)
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003446{
3447}
3448
Guillaume Champagne1cb09482020-01-27 20:12:16 -05003449static void
3450touch_handle_orientation(void *data, struct wl_touch *wl_touch, int32_t id,
3451 wl_fixed_t orientation)
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003452{
3453}
3454
Rusty Lynch041815a2013-08-08 21:20:38 -07003455static const struct wl_touch_listener touch_listener = {
3456 touch_handle_down,
3457 touch_handle_up,
3458 touch_handle_motion,
3459 touch_handle_frame,
3460 touch_handle_cancel,
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003461 touch_handle_shape,
3462 touch_handle_orientation,
Rusty Lynch041815a2013-08-08 21:20:38 -07003463};
3464
3465static void
Daniel Stone37816df2012-05-16 18:45:18 +01003466seat_handle_capabilities(void *data, struct wl_seat *seat,
3467 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003468{
Daniel Stone37816df2012-05-16 18:45:18 +01003469 struct input *input = data;
3470
3471 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3472 input->pointer = wl_seat_get_pointer(seat);
3473 wl_pointer_set_user_data(input->pointer, input);
3474 wl_pointer_add_listener(input->pointer, &pointer_listener,
3475 input);
3476 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003477 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003478 wl_pointer_release(input->pointer);
3479 else
3480 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003481 input->pointer = NULL;
3482 }
3483
3484 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3485 input->keyboard = wl_seat_get_keyboard(seat);
3486 wl_keyboard_set_user_data(input->keyboard, input);
3487 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3488 input);
3489 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003490 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003491 wl_keyboard_release(input->keyboard);
3492 else
3493 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003494 input->keyboard = NULL;
3495 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003496
3497 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3498 input->touch = wl_seat_get_touch(seat);
3499 wl_touch_set_user_data(input->touch, input);
3500 wl_touch_add_listener(input->touch, &touch_listener, input);
3501 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003502 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003503 wl_touch_release(input->touch);
3504 else
3505 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003506 input->touch = NULL;
3507 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003508}
3509
Rob Bradford08031182013-08-13 20:11:03 +01003510static void
3511seat_handle_name(void *data, struct wl_seat *seat,
3512 const char *name)
3513{
3514
3515}
3516
Daniel Stone37816df2012-05-16 18:45:18 +01003517static const struct wl_seat_listener seat_listener = {
3518 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003519 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003520};
3521
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003522void
3523input_get_position(struct input *input, int32_t *x, int32_t *y)
3524{
3525 *x = input->sx;
3526 *y = input->sy;
3527}
3528
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003529int
3530input_get_touch(struct input *input, int32_t id, float *x, float *y)
3531{
3532 struct touch_point *tp;
3533
3534 wl_list_for_each(tp, &input->touch_point_list, link) {
3535 if (tp->id != id)
3536 continue;
3537
3538 *x = tp->x;
3539 *y = tp->y;
3540 return 0;
3541 }
3542
3543 return -1;
3544}
3545
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003546struct display *
3547input_get_display(struct input *input)
3548{
3549 return input->display;
3550}
3551
Daniel Stone37816df2012-05-16 18:45:18 +01003552struct wl_seat *
3553input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003554{
Daniel Stone37816df2012-05-16 18:45:18 +01003555 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003556}
3557
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003558uint32_t
3559input_get_modifiers(struct input *input)
3560{
3561 return input->modifiers;
3562}
3563
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003564struct widget *
3565input_get_focus_widget(struct input *input)
3566{
3567 return input->focus_widget;
3568}
3569
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003570struct data_offer {
3571 struct wl_data_offer *offer;
3572 struct input *input;
3573 struct wl_array types;
3574 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003575
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003576 struct task io_task;
3577 int fd;
3578 data_func_t func;
3579 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003580 uint32_t dnd_action;
3581 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003582 void *user_data;
3583};
3584
3585static void
3586data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3587{
3588 struct data_offer *offer = data;
3589 char **p;
3590
3591 p = wl_array_add(&offer->types, sizeof *p);
3592 *p = strdup(type);
3593}
3594
Carlos Garnacho9c931792016-01-18 23:52:12 +01003595static void
3596data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3597{
3598 struct data_offer *offer = data;
3599
3600 offer->source_actions = source_actions;
3601}
3602
3603static void
3604data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3605{
3606 struct data_offer *offer = data;
3607
3608 offer->dnd_action = dnd_action;
3609}
3610
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003611static const struct wl_data_offer_listener data_offer_listener = {
3612 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003613 data_offer_source_actions,
3614 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003615};
3616
3617static void
3618data_offer_destroy(struct data_offer *offer)
3619{
3620 char **p;
3621
3622 offer->refcount--;
3623 if (offer->refcount == 0) {
3624 wl_data_offer_destroy(offer->offer);
3625 for (p = offer->types.data; *p; p++)
3626 free(*p);
3627 wl_array_release(&offer->types);
3628 free(offer);
3629 }
3630}
3631
3632static void
3633data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003634 struct wl_data_device *data_device,
3635 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003636{
3637 struct data_offer *offer;
3638
Brian Lovinbc919262013-08-07 15:34:59 -07003639 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003640
3641 wl_array_init(&offer->types);
3642 offer->refcount = 1;
3643 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003644 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003645 wl_data_offer_add_listener(offer->offer,
3646 &data_offer_listener, offer);
3647}
3648
3649static void
3650data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003651 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003652 wl_fixed_t x_w, wl_fixed_t y_w,
3653 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003654{
3655 struct input *input = data;
3656 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003657 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003658 float x = wl_fixed_to_double(x_w);
3659 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003660 char **p;
3661
Jeffy Chen97749172019-09-02 10:26:09 +08003662 if (!surface) {
3663 /* enter event for a window we've just destroyed */
3664 return;
3665 }
3666
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003667 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003668 input->drag_enter_serial = serial;
3669 input->drag_focus = window,
3670 input->drag_x = x;
3671 input->drag_y = y;
3672
3673 if (!input->touch_grab)
3674 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003675
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003676 if (offer) {
3677 input->drag_offer = wl_data_offer_get_user_data(offer);
3678
3679 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3680 *p = NULL;
3681
3682 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003683
3684 if (input->display->data_device_manager_version >=
3685 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3686 wl_data_offer_set_actions(offer,
3687 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3688 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3689 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3690 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003691 } else {
3692 input->drag_offer = NULL;
3693 types_data = NULL;
3694 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003695
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003696 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003697 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003698 window->user_data);
3699}
3700
3701static void
3702data_device_leave(void *data, struct wl_data_device *data_device)
3703{
3704 struct input *input = data;
3705
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003706 if (input->drag_offer) {
3707 data_offer_destroy(input->drag_offer);
3708 input->drag_offer = NULL;
3709 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003710}
3711
3712static void
3713data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003714 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003715{
3716 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003717 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003718 float x = wl_fixed_to_double(x_w);
3719 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003720 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003721
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003722 input->drag_x = x;
3723 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003724
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003725 if (input->drag_offer)
3726 types_data = input->drag_offer->types.data;
3727 else
3728 types_data = NULL;
3729
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003730 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003731 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003732 window->user_data);
3733}
3734
3735static void
3736data_device_drop(void *data, struct wl_data_device *data_device)
3737{
3738 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003739 struct window *window = input->drag_focus;
3740 float x, y;
3741
3742 x = input->drag_x;
3743 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003744
3745 if (window->drop_handler)
3746 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003747 x, y, window->user_data);
3748
3749 if (input->touch_grab)
3750 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003751}
3752
3753static void
3754data_device_selection(void *data,
3755 struct wl_data_device *wl_data_device,
3756 struct wl_data_offer *offer)
3757{
3758 struct input *input = data;
3759 char **p;
3760
3761 if (input->selection_offer)
3762 data_offer_destroy(input->selection_offer);
3763
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003764 if (offer) {
3765 input->selection_offer = wl_data_offer_get_user_data(offer);
3766 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3767 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003768 } else {
3769 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003770 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003771}
3772
3773static const struct wl_data_device_listener data_device_listener = {
3774 data_device_data_offer,
3775 data_device_enter,
3776 data_device_leave,
3777 data_device_motion,
3778 data_device_drop,
3779 data_device_selection
3780};
3781
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003782static void
3783input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003784{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003785 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003786 struct wl_cursor *cursor;
3787 struct wl_cursor_image *image;
Simon Ser992ee042021-02-07 11:32:46 +01003788 struct wl_surface *prev_surface;
3789 struct display *d = input->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003790
Daniel Stone80972742012-11-07 17:51:39 +11003791 if (!input->pointer)
3792 return;
3793
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003794 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003795 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003796 return;
3797
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003798 if (index >= (int) cursor->image_count) {
3799 fprintf(stderr, "cursor index out of range\n");
3800 return;
3801 }
3802
3803 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003804 buffer = wl_cursor_image_get_buffer(image);
3805 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003806 return;
3807
Simon Ser992ee042021-02-07 11:32:46 +01003808 /* Don't re-use the previous surface, otherwise the new buffer and the
3809 * new hotspot aren't applied atomically. */
3810 prev_surface = input->pointer_surface;
3811 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3812
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003813 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3814 wl_surface_damage(input->pointer_surface, 0, 0,
3815 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003816 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003817 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3818 input->pointer_surface,
3819 image->hotspot_x, image->hotspot_y);
Simon Ser992ee042021-02-07 11:32:46 +01003820
3821 if (prev_surface)
3822 wl_surface_destroy(prev_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003823}
3824
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003825static const struct wl_callback_listener pointer_surface_listener;
3826
Derek Foreman493d9792015-03-04 16:26:25 -06003827static bool
3828input_set_pointer_special(struct input *input)
3829{
3830 if (input->current_cursor == CURSOR_BLANK) {
3831 wl_pointer_set_cursor(input->pointer,
3832 input->pointer_enter_serial,
3833 NULL, 0, 0);
3834 return true;
3835 }
3836
3837 if (input->current_cursor == CURSOR_UNSET)
3838 return true;
3839
3840 return false;
3841}
3842
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003843static void
Derek Foreman118a4292015-04-22 17:23:35 -05003844schedule_pointer_image_update(struct input *input,
3845 struct wl_cursor *cursor,
3846 uint32_t duration,
3847 bool force_frame)
3848{
3849 /* Some silly cursor sets have enormous pauses in them. In these
3850 * cases it's better to use a timer even if it results in less
3851 * accurate presentation, since it will save us having to set the
3852 * same cursor image over and over again.
3853 *
3854 * This is really not the way we're supposed to time any kind of
3855 * animation, but we're pretending it's OK here because we don't
3856 * want animated cursors with long delays to needlessly hog CPU.
3857 *
3858 * We use force_frame to ensure we don't accumulate large timing
3859 * errors by running off the wrong clock.
3860 */
3861 if (!force_frame && duration > 100) {
3862 struct timespec tp;
3863
3864 clock_gettime(CLOCK_MONOTONIC, &tp);
3865 input->cursor_timer_start = tp.tv_sec * 1000
3866 + tp.tv_nsec / 1000000;
3867 cursor_delay_timer_reset(input, duration);
3868 return;
3869 }
3870
3871 /* for short durations we'll just spin on frame callbacks for
3872 * accurate timing - the way any kind of timing sensitive animation
3873 * should really be done. */
3874 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3875 wl_callback_add_listener(input->cursor_frame_cb,
3876 &pointer_surface_listener, input);
3877
3878}
3879
3880static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003881pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3882 uint32_t time)
3883{
3884 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003885 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003886 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003887 uint32_t duration;
3888 bool force_frame = true;
3889
3890 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003891
3892 if (callback) {
3893 assert(callback == input->cursor_frame_cb);
3894 wl_callback_destroy(callback);
3895 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003896 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003897 }
3898
Daniel Stone80972742012-11-07 17:51:39 +11003899 if (!input->pointer)
3900 return;
3901
Derek Foreman493d9792015-03-04 16:26:25 -06003902 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003903 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003904
Daniel Stonea494f1d2012-06-18 19:31:12 +01003905 cursor = input->display->cursors[input->current_cursor];
3906 if (!cursor)
3907 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003908
3909 /* FIXME We don't have the current time on the first call so we set
3910 * the animation start to the time of the first frame callback. */
3911 if (time == 0)
3912 input->cursor_anim_start = 0;
3913 else if (input->cursor_anim_start == 0)
3914 input->cursor_anim_start = time;
3915
Derek Foreman118a4292015-04-22 17:23:35 -05003916 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003917
Derek Foreman118a4292015-04-22 17:23:35 -05003918 if (time == 0 || input->cursor_anim_start == 0) {
3919 duration = 0;
3920 i = 0;
3921 } else
3922 i = wl_cursor_frame_and_duration(
3923 cursor,
3924 time - input->cursor_anim_start,
3925 &duration);
3926
Simon Ser992ee042021-02-07 11:32:46 +01003927 input_set_pointer_image_index(input, i);
3928
Derek Foreman118a4292015-04-22 17:23:35 -05003929 if (cursor->image_count > 1)
3930 schedule_pointer_image_update(input, cursor, duration,
3931 force_frame);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003932}
3933
Derek Foreman118a4292015-04-22 17:23:35 -05003934static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003935cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003936{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003937 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003938 struct timespec tp;
3939 struct wl_cursor *cursor;
3940 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003941
3942 if (!input->cursor_timer_running)
3943 return;
3944
Derek Foreman118a4292015-04-22 17:23:35 -05003945 cursor = input->display->cursors[input->current_cursor];
3946 if (!cursor)
3947 return;
3948
3949 clock_gettime(CLOCK_MONOTONIC, &tp);
3950 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3951 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3952}
3953
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003954static const struct wl_callback_listener pointer_surface_listener = {
3955 pointer_surface_frame_callback
3956};
3957
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003958void
3959input_set_pointer_image(struct input *input, int pointer)
3960{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003961 int force = 0;
3962
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003963 if (!input->pointer)
3964 return;
3965
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003966 if (input->pointer_enter_serial > input->cursor_serial)
3967 force = 1;
3968
3969 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003970 return;
3971
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003972 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003973 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003974 if (!input->cursor_frame_cb)
3975 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003976 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003977 /* The current frame callback may be stuck if, for instance,
3978 * the set cursor request was processed by the server after
3979 * this client lost the focus. In this case the cursor surface
3980 * might not be mapped and the frame callback wouldn't ever
3981 * complete. Send a set_cursor and attach to try to map the
3982 * cursor surface again so that the callback will finish */
3983 input_set_pointer_image_index(input, 0);
3984 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003985}
3986
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003987struct wl_data_device *
3988input_get_data_device(struct input *input)
3989{
3990 return input->data_device;
3991}
3992
3993void
3994input_set_selection(struct input *input,
3995 struct wl_data_source *source, uint32_t time)
3996{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003997 if (input->data_device)
3998 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003999}
4000
4001void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004002input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004003{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004004 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004005 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004006}
4007
4008static void
4009offer_io_func(struct task *task, uint32_t events)
4010{
4011 struct data_offer *offer =
4012 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01004013 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004014 unsigned int len;
4015 char buffer[4096];
4016
4017 len = read(offer->fd, buffer, sizeof buffer);
4018 offer->func(buffer, len,
4019 offer->x, offer->y, offer->user_data);
4020
4021 if (len == 0) {
Harish Krupo43152a32019-04-19 22:06:44 +05304022 if ((offer != offer->input->selection_offer) &&
4023 (display->data_device_manager_version >=
4024 WL_DATA_OFFER_FINISH_SINCE_VERSION))
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01004025 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004026 close(offer->fd);
4027 data_offer_destroy(offer);
4028 }
4029}
4030
4031static void
4032data_offer_receive_data(struct data_offer *offer, const char *mime_type,
4033 data_func_t func, void *user_data)
4034{
4035 int p[2];
4036
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02004037 if (pipe2(p, O_CLOEXEC) == -1)
4038 return;
4039
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004040 wl_data_offer_receive(offer->offer, mime_type, p[1]);
4041 close(p[1]);
4042
4043 offer->io_task.run = offer_io_func;
4044 offer->fd = p[0];
4045 offer->func = func;
4046 offer->refcount++;
4047 offer->user_data = user_data;
4048
4049 display_watch_fd(offer->input->display,
4050 offer->fd, EPOLLIN, &offer->io_task);
4051}
4052
4053void
4054input_receive_drag_data(struct input *input, const char *mime_type,
4055 data_func_t func, void *data)
4056{
4057 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004058 input->drag_offer->x = input->drag_x;
4059 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004060}
4061
4062int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004063input_receive_drag_data_to_fd(struct input *input,
4064 const char *mime_type, int fd)
4065{
4066 if (input->drag_offer)
4067 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4068
4069 return 0;
4070}
4071
4072int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004073input_receive_selection_data(struct input *input, const char *mime_type,
4074 data_func_t func, void *data)
4075{
4076 char **p;
4077
4078 if (input->selection_offer == NULL)
4079 return -1;
4080
4081 for (p = input->selection_offer->types.data; *p; p++)
4082 if (strcmp(mime_type, *p) == 0)
4083 break;
4084
4085 if (*p == NULL)
4086 return -1;
4087
4088 data_offer_receive_data(input->selection_offer,
4089 mime_type, func, data);
4090 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004091}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004092
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004093int
4094input_receive_selection_data_to_fd(struct input *input,
4095 const char *mime_type, int fd)
4096{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004097 if (input->selection_offer)
4098 wl_data_offer_receive(input->selection_offer->offer,
4099 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004100
4101 return 0;
4102}
4103
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004104void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004105window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004106{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004107 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004108 return;
4109
ant8mef99fac22018-11-28 22:46:37 +01004110 xdg_toplevel_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004111}
4112
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004113static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004114surface_set_synchronized(struct surface *surface)
4115{
4116 if (!surface->subsurface)
4117 return;
4118
4119 if (surface->synchronized)
4120 return;
4121
4122 wl_subsurface_set_sync(surface->subsurface);
4123 surface->synchronized = 1;
4124}
4125
4126static void
4127surface_set_synchronized_default(struct surface *surface)
4128{
4129 if (!surface->subsurface)
4130 return;
4131
4132 if (surface->synchronized == surface->synchronized_default)
4133 return;
4134
4135 if (surface->synchronized_default)
4136 wl_subsurface_set_sync(surface->subsurface);
4137 else
4138 wl_subsurface_set_desync(surface->subsurface);
4139
4140 surface->synchronized = surface->synchronized_default;
4141}
4142
4143static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004144surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004145{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004146 struct widget *widget = surface->widget;
4147 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004148
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004149 if (surface->input_region) {
4150 wl_region_destroy(surface->input_region);
4151 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004152 }
4153
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004154 if (surface->opaque_region)
4155 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004156
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004157 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004158
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004159 if (widget->resize_handler)
4160 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004161 widget->allocation.width,
4162 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004163 widget->user_data);
4164
Pekka Paalanen35e82632013-04-25 13:57:48 +03004165 if (surface->subsurface &&
4166 (surface->allocation.x != widget->allocation.x ||
4167 surface->allocation.y != widget->allocation.y)) {
4168 wl_subsurface_set_position(surface->subsurface,
4169 widget->allocation.x,
4170 widget->allocation.y);
4171 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004172 if (surface->allocation.width != widget->allocation.width ||
4173 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004174 window_schedule_redraw(widget->window);
4175 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004176 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004177
4178 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004179 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004180 widget->allocation.width,
4181 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004182}
4183
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004184static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004185window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004186{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004187 struct surface *surface;
4188
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004189 widget_set_allocation(window->main_surface->widget,
4190 window->pending_allocation.x,
4191 window->pending_allocation.y,
4192 window->pending_allocation.width,
4193 window->pending_allocation.height);
4194
4195 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004196
4197 /* The main surface is in the list, too. Main surface's
4198 * resize_handler is responsible for calling widget_set_allocation()
4199 * on all sub-surface root widgets, so they will be resized
4200 * properly.
4201 */
4202 wl_list_for_each(surface, &window->subsurface_list, link) {
4203 if (surface == window->main_surface)
4204 continue;
4205
4206 surface_set_synchronized(surface);
4207 surface_resize(surface);
4208 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004209
4210 if (!window->fullscreen && !window->maximized)
4211 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004212
4213 if (window->confined && window->confined_widget) {
4214 struct wl_compositor *compositor = window->display->compositor;
4215 struct wl_region *region;
4216 struct widget *widget = window->confined_widget;
4217
4218 region = wl_compositor_create_region(compositor);
4219 wl_region_add(region,
4220 widget->allocation.x,
4221 widget->allocation.y,
4222 widget->allocation.width,
4223 widget->allocation.height);
4224 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4225 region);
4226 wl_region_destroy(region);
4227 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004228}
4229
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004230static void
4231idle_resize(struct window *window)
4232{
4233 window->resize_needed = 0;
4234 window->redraw_needed = 1;
4235
4236 DBG("from %dx%d to %dx%d\n",
4237 window->main_surface->server_allocation.width,
4238 window->main_surface->server_allocation.height,
4239 window->pending_allocation.width,
4240 window->pending_allocation.height);
4241
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004242 window_do_resize(window);
4243}
4244
4245static void
4246undo_resize(struct window *window)
4247{
4248 window->pending_allocation.width =
4249 window->main_surface->server_allocation.width;
4250 window->pending_allocation.height =
4251 window->main_surface->server_allocation.height;
4252
4253 DBG("back to %dx%d\n",
4254 window->main_surface->server_allocation.width,
4255 window->main_surface->server_allocation.height);
4256
4257 window_do_resize(window);
4258
4259 if (window->pending_allocation.width == 0 &&
4260 window->pending_allocation.height == 0) {
4261 fprintf(stderr, "Error: Could not draw a surface, "
4262 "most likely due to insufficient disk space in "
4263 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4264 exit(EXIT_FAILURE);
4265 }
4266}
4267
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004268void
4269window_schedule_resize(struct window *window, int width, int height)
4270{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004271 /* We should probably get these numbers from the theme. */
4272 const int min_width = 200, min_height = 200;
4273
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004274 window->pending_allocation.x = 0;
4275 window->pending_allocation.y = 0;
4276 window->pending_allocation.width = width;
4277 window->pending_allocation.height = height;
4278
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004279 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004280 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004281 window->min_allocation.width = min_width;
4282 else
4283 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004284 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004285 window->min_allocation.height = min_height;
4286 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004287 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004288 }
4289
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004290 if (window->pending_allocation.width < window->min_allocation.width)
4291 window->pending_allocation.width = window->min_allocation.width;
4292 if (window->pending_allocation.height < window->min_allocation.height)
4293 window->pending_allocation.height = window->min_allocation.height;
4294
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004295 window->resize_needed = 1;
4296 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004297}
4298
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004299void
4300widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4301{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004302 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004303}
4304
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004305static int
4306window_get_shadow_margin(struct window *window)
4307{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004308 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004309 return frame_get_shadow_margin(window->frame->frame);
4310 else
4311 return 0;
4312}
4313
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004314void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004315window_inhibit_redraw(struct window *window)
4316{
4317 window->redraw_inhibited = 1;
4318 wl_list_remove(&window->redraw_task.link);
4319 wl_list_init(&window->redraw_task.link);
4320 window->redraw_task_scheduled = 0;
4321}
4322
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004323void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004324window_uninhibit_redraw(struct window *window)
4325{
4326 window->redraw_inhibited = 0;
4327 if (window->redraw_needed || window->resize_needed)
4328 window_schedule_redraw_task(window);
4329}
4330
4331static void
4332xdg_surface_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01004333 struct xdg_surface *xdg_surface,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004334 uint32_t serial)
4335{
4336 struct window *window = data;
4337
ant8mef99fac22018-11-28 22:46:37 +01004338 xdg_surface_ack_configure(window->xdg_surface, serial);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004339
4340 if (window->state_changed_handler)
4341 window->state_changed_handler(window, window->user_data);
4342
4343 window_uninhibit_redraw(window);
4344}
4345
ant8mef99fac22018-11-28 22:46:37 +01004346static const struct xdg_surface_listener xdg_surface_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004347 xdg_surface_handle_configure
4348};
4349
4350static void
ant8mef99fac22018-11-28 22:46:37 +01004351xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004352 int32_t width, int32_t height,
4353 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004354{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004355 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004356 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004357
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004358 window->maximized = 0;
4359 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004360 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004361 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004362
4363 wl_array_for_each(p, states) {
4364 uint32_t state = *p;
4365 switch (state) {
ant8mef99fac22018-11-28 22:46:37 +01004366 case XDG_TOPLEVEL_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004367 window->maximized = 1;
4368 break;
ant8mef99fac22018-11-28 22:46:37 +01004369 case XDG_TOPLEVEL_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004370 window->fullscreen = 1;
4371 break;
ant8mef99fac22018-11-28 22:46:37 +01004372 case XDG_TOPLEVEL_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004373 window->resizing = 1;
4374 break;
ant8mef99fac22018-11-28 22:46:37 +01004375 case XDG_TOPLEVEL_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004376 window->focused = 1;
4377 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004378 default:
4379 /* Unknown state */
4380 break;
4381 }
4382 }
4383
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004384 if (window->frame) {
4385 if (window->maximized) {
4386 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4387 } else {
4388 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4389 }
4390
4391 if (window->focused) {
4392 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4393 } else {
4394 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4395 }
4396 }
4397
Jasper St. Pierref184c382014-05-06 08:33:27 -04004398 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004399 /* The width / height params are for window geometry,
4400 * but window_schedule_resize takes allocation. Add
4401 * on the shadow margin to get the difference. */
4402 int margin = window_get_shadow_margin(window);
4403
4404 window_schedule_resize(window,
4405 width + margin * 2,
4406 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004407 } else if (window->saved_allocation.width > 0 &&
4408 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004409 window_schedule_resize(window,
4410 window->saved_allocation.width,
4411 window->saved_allocation.height);
4412 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004413}
4414
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004415static void
ant8mef99fac22018-11-28 22:46:37 +01004416xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004417{
4418 struct window *window = data;
4419 window_close(window);
4420}
4421
ant8mef99fac22018-11-28 22:46:37 +01004422static const struct xdg_toplevel_listener xdg_toplevel_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004423 xdg_toplevel_handle_configure,
4424 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004425};
4426
4427static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004428window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004429{
ant8mef99fac22018-11-28 22:46:37 +01004430 struct xdg_toplevel *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004431
4432 if (!window->xdg_surface)
4433 return;
4434
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004435 if (window->parent == window->last_parent)
4436 return;
4437
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004438 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004439 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004440 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004441 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004442
ant8mef99fac22018-11-28 22:46:37 +01004443 xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004444 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004445}
4446
4447static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004448window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004449{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004450 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004451 frame_input_rect(window->frame->frame,
4452 &geometry->x,
4453 &geometry->y,
4454 &geometry->width,
4455 &geometry->height);
4456 else
4457 window_get_allocation(window, geometry);
4458}
4459
4460static void
4461window_sync_geometry(struct window *window)
4462{
4463 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004464
4465 if (!window->xdg_surface)
4466 return;
4467
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004468 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004469 if (geometry.x == window->last_geometry.x &&
4470 geometry.y == window->last_geometry.y &&
4471 geometry.width == window->last_geometry.width &&
4472 geometry.height == window->last_geometry.height)
4473 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004474
ant8mef99fac22018-11-28 22:46:37 +01004475 xdg_surface_set_window_geometry(window->xdg_surface,
4476 geometry.x,
4477 geometry.y,
4478 geometry.width,
4479 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004480 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004481}
4482
4483static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004484window_flush(struct window *window)
4485{
4486 struct surface *surface;
4487
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004488 assert(!window->redraw_inhibited);
4489
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004490 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004491 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004492 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004493 if (window->xdg_toplevel)
4494 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004495 }
4496
4497 wl_list_for_each(surface, &window->subsurface_list, link) {
4498 if (surface == window->main_surface)
4499 continue;
4500
4501 surface_flush(surface);
4502 }
4503
4504 surface_flush(window->main_surface);
4505}
4506
4507static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004508menu_destroy(struct menu *menu)
4509{
4510 widget_destroy(menu->widget);
4511 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004512 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004513 free(menu);
4514}
4515
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004516void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004517window_get_allocation(struct window *window,
4518 struct rectangle *allocation)
4519{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004520 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004521}
4522
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004523static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004524widget_redraw(struct widget *widget)
4525{
4526 struct widget *child;
4527
4528 if (widget->redraw_handler)
4529 widget->redraw_handler(widget, widget->user_data);
4530 wl_list_for_each(child, &widget->child_list, link)
4531 widget_redraw(child);
4532}
4533
4534static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004535frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4536{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004537 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004538
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004539 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004540 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004541 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004542 surface->frame_cb = NULL;
4543
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004544 surface->last_time = time;
4545
Pekka Paalanen71233882013-04-25 13:57:53 +03004546 if (surface->redraw_needed || surface->window->redraw_needed) {
4547 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004548 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004549 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004550}
4551
4552static const struct wl_callback_listener listener = {
4553 frame_callback
4554};
4555
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004556static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004557surface_redraw(struct surface *surface)
4558{
Pekka Paalanen71233882013-04-25 13:57:53 +03004559 DBG_OBJ(surface->surface, "begin\n");
4560
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004561 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004562 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004563
4564 /* Whole-window redraw forces a redraw even if the previous has
4565 * not yet hit the screen.
4566 */
4567 if (surface->frame_cb) {
4568 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004569 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004570
Pekka Paalanen71233882013-04-25 13:57:53 +03004571 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004572 wl_callback_destroy(surface->frame_cb);
4573 }
4574
Neil Roberts97b747c2013-12-19 16:17:12 +00004575 if (surface->widget->use_cairo &&
4576 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004577 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004578 return -1;
4579 }
4580
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004581 surface->frame_cb = wl_surface_frame(surface->surface);
4582 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004583 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004584
4585 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004586 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004587 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004588 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004589 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004590}
4591
4592static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004593idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004594{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004595 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004596 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004597 int failed = 0;
4598 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004599
Pekka Paalanen71233882013-04-25 13:57:53 +03004600 DBG(" --------- \n");
4601
Pekka Paalaneneebff542013-04-25 13:57:52 +03004602 wl_list_init(&window->redraw_task.link);
4603 window->redraw_task_scheduled = 0;
4604
4605 if (window->resize_needed) {
4606 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004607 if (window->main_surface->frame_cb) {
4608 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004609 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004610 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004611
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004612 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004613 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004614 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004615
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004616 if (surface_redraw(window->main_surface) < 0) {
4617 /*
4618 * Only main_surface failure will cause us to undo the resize.
4619 * If sub-surfaces fail, they will just be broken with old
4620 * content.
4621 */
4622 failed = 1;
4623 } else {
4624 wl_list_for_each(surface, &window->subsurface_list, link) {
4625 if (surface == window->main_surface)
4626 continue;
4627
4628 surface_redraw(surface);
4629 }
4630 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004631
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004632 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004633 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004634
4635 wl_list_for_each(surface, &window->subsurface_list, link)
4636 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004637
4638 if (resized && failed) {
4639 /* Restore widget tree to correspond to what is on screen. */
4640 undo_resize(window);
4641 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004642}
4643
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004644static void
4645window_schedule_redraw_task(struct window *window)
4646{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004647 if (window->redraw_inhibited)
4648 return;
4649
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004650 if (!window->redraw_task_scheduled) {
4651 window->redraw_task.run = idle_redraw;
4652 display_defer(window->display, &window->redraw_task);
4653 window->redraw_task_scheduled = 1;
4654 }
4655}
4656
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004657void
4658window_schedule_redraw(struct window *window)
4659{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004660 struct surface *surface;
4661
Pekka Paalanen71233882013-04-25 13:57:53 +03004662 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4663
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004664 wl_list_for_each(surface, &window->subsurface_list, link)
4665 surface->redraw_needed = 1;
4666
4667 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004668}
4669
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004670int
4671window_is_fullscreen(struct window *window)
4672{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004673 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004674}
4675
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004676void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004677window_set_fullscreen(struct window *window, int fullscreen)
4678{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004679 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004680 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004681
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004682 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004683 return;
4684
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004685 if (fullscreen)
ant8mef99fac22018-11-28 22:46:37 +01004686 xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004687 else
ant8mef99fac22018-11-28 22:46:37 +01004688 xdg_toplevel_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004689}
4690
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004691int
4692window_is_maximized(struct window *window)
4693{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004694 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004695}
4696
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004697void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004698window_set_maximized(struct window *window, int maximized)
4699{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004700 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004701 return;
4702
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004703 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004704 return;
4705
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004706 if (maximized)
ant8mef99fac22018-11-28 22:46:37 +01004707 xdg_toplevel_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004708 else
ant8mef99fac22018-11-28 22:46:37 +01004709 xdg_toplevel_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004710}
4711
Jasper St. Pierrede680992014-04-10 17:23:49 -07004712int
4713window_is_resizing(struct window *window)
4714{
4715 return window->resizing;
4716}
4717
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004718void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004719window_set_minimized(struct window *window)
4720{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004721 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004722 return;
4723
ant8mef99fac22018-11-28 22:46:37 +01004724 xdg_toplevel_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004725}
4726
4727void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004728window_set_user_data(struct window *window, void *data)
4729{
4730 window->user_data = data;
4731}
4732
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004733void *
4734window_get_user_data(struct window *window)
4735{
4736 return window->user_data;
4737}
4738
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004739void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004740window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004741 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004742{
4743 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004744}
4745
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004746void
4747window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004748 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004749{
4750 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004751}
4752
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004753void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004754window_set_data_handler(struct window *window, window_data_handler_t handler)
4755{
4756 window->data_handler = handler;
4757}
4758
4759void
4760window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4761{
4762 window->drop_handler = handler;
4763}
4764
4765void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004766window_set_close_handler(struct window *window,
4767 window_close_handler_t handler)
4768{
4769 window->close_handler = handler;
4770}
4771
4772void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004773window_set_fullscreen_handler(struct window *window,
4774 window_fullscreen_handler_t handler)
4775{
4776 window->fullscreen_handler = handler;
4777}
4778
4779void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004780window_set_output_handler(struct window *window,
4781 window_output_handler_t handler)
4782{
4783 window->output_handler = handler;
4784}
4785
4786void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004787window_set_state_changed_handler(struct window *window,
4788 window_state_changed_handler_t handler)
4789{
4790 window->state_changed_handler = handler;
4791}
4792
4793void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004794window_set_pointer_locked_handler(struct window *window,
4795 locked_pointer_locked_handler_t locked,
4796 locked_pointer_unlocked_handler_t unlocked)
4797{
4798 window->pointer_unlocked_handler = unlocked;
4799 window->pointer_locked_handler = locked;
4800}
4801
4802void
4803window_set_pointer_confined_handler(struct window *window,
4804 confined_pointer_confined_handler_t confined,
4805 confined_pointer_unconfined_handler_t unconfined)
4806{
4807 window->pointer_confined_handler = confined;
4808 window->pointer_unconfined_handler = unconfined;
4809}
4810
4811void
4812window_set_locked_pointer_motion_handler(struct window *window,
4813 window_locked_pointer_motion_handler_t handler)
4814{
4815 window->locked_pointer_motion_handler = handler;
4816}
4817
4818void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004819window_set_title(struct window *window, const char *title)
4820{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004821 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004822 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004823 if (window->frame) {
4824 frame_set_title(window->frame->frame, title);
4825 widget_schedule_redraw(window->frame->widget);
4826 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004827 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01004828 xdg_toplevel_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004829}
4830
4831const char *
4832window_get_title(struct window *window)
4833{
4834 return window->title;
4835}
4836
4837void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004838window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4839{
4840 struct text_cursor_position *text_cursor_position =
4841 window->display->text_cursor_position;
4842
Scott Moreau9295ce02012-06-01 12:46:10 -06004843 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004844 return;
4845
4846 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004847 window->main_surface->surface,
4848 wl_fixed_from_int(x),
4849 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004850}
4851
Casey Dahlin9074db52012-04-19 22:50:09 -04004852static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004853relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4854 uint32_t utime_hi,
4855 uint32_t utime_lo,
4856 wl_fixed_t dx,
4857 wl_fixed_t dy,
4858 wl_fixed_t dx_unaccel,
4859 wl_fixed_t dy_unaccel)
4860{
4861 struct input *input = data;
4862 struct window *window = input->pointer_focus;
4863 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4864
4865 if (window->locked_pointer_motion_handler &&
4866 window->pointer_locked) {
4867 window->locked_pointer_motion_handler(
4868 window, input, ms,
4869 wl_fixed_to_double(dx),
4870 wl_fixed_to_double(dy),
4871 window->user_data);
4872 }
4873}
4874
4875static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4876 relative_pointer_handle_motion,
4877};
4878
4879static void
4880locked_pointer_locked(void *data,
4881 struct zwp_locked_pointer_v1 *locked_pointer)
4882{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004883 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004884 struct window *window = input->locked_window;
4885
4886 if (!window)
4887 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004888
4889 window->pointer_locked = true;
4890
4891 if (window->pointer_locked_handler) {
4892 window->pointer_locked_handler(window,
4893 input,
4894 window->user_data);
4895 }
4896}
4897
4898static void
4899locked_pointer_unlocked(void *data,
4900 struct zwp_locked_pointer_v1 *locked_pointer)
4901{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004902 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004903 struct window *window = input->locked_window;
4904
4905 if (!window)
4906 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004907
4908 window_unlock_pointer(window);
4909
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004910 input->locked_window = NULL;
4911
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004912 if (window->pointer_unlocked_handler) {
4913 window->pointer_unlocked_handler(window,
4914 input,
4915 window->user_data);
4916 }
4917}
4918
4919static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4920 locked_pointer_locked,
4921 locked_pointer_unlocked,
4922};
4923
4924int
4925window_lock_pointer(struct window *window, struct input *input)
4926{
4927 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4928 window->display->relative_pointer_manager;
4929 struct zwp_pointer_constraints_v1 *pointer_constraints =
4930 window->display->pointer_constraints;
4931 struct zwp_relative_pointer_v1 *relative_pointer;
4932 struct zwp_locked_pointer_v1 *locked_pointer;
4933
4934 if (!window->display->relative_pointer_manager)
4935 return -1;
4936
4937 if (!window->display->pointer_constraints)
4938 return -1;
4939
4940 if (window->locked_pointer)
4941 return -1;
4942
4943 if (window->confined_pointer)
4944 return -1;
4945
4946 if (!input->pointer)
4947 return -1;
4948
4949 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4950 relative_pointer_manager, input->pointer);
4951 zwp_relative_pointer_v1_add_listener(relative_pointer,
4952 &relative_pointer_listener,
4953 input);
4954
4955 locked_pointer =
4956 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4957 window->main_surface->surface,
4958 input->pointer,
4959 NULL,
4960 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4961 zwp_locked_pointer_v1_add_listener(locked_pointer,
4962 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004963 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004964
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004965 window->locked_pointer = locked_pointer;
4966 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004967 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004968
4969 return 0;
4970}
4971
4972void
4973window_unlock_pointer(struct window *window)
4974{
4975 if (!window->locked_pointer)
4976 return;
4977
4978 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4979 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4980 window->locked_pointer = NULL;
4981 window->relative_pointer = NULL;
4982 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004983}
4984
4985void
4986widget_set_locked_pointer_cursor_hint(struct widget *widget,
4987 float x, float y)
4988{
4989 struct window *window = widget->window;
4990
4991 if (!window->locked_pointer)
4992 return;
4993
4994 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4995 wl_fixed_from_double(x),
4996 wl_fixed_from_double(y));
4997 wl_surface_commit(window->main_surface->surface);
4998}
4999
5000static void
5001confined_pointer_confined(void *data,
5002 struct zwp_confined_pointer_v1 *confined_pointer)
5003{
Dima Ryazanov336ce672018-11-14 21:03:35 -08005004 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005005 struct window *window = input->confined_window;
5006
5007 if (!window)
5008 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005009
5010 window->confined = true;
5011
5012 if (window->pointer_confined_handler) {
5013 window->pointer_confined_handler(window,
5014 input,
5015 window->user_data);
5016 }
5017}
5018
5019static void
5020confined_pointer_unconfined(void *data,
5021 struct zwp_confined_pointer_v1 *confined_pointer)
5022{
Dima Ryazanov336ce672018-11-14 21:03:35 -08005023 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005024 struct window *window = input->confined_window;
5025
5026 if (!window)
5027 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005028
5029 window_unconfine_pointer(window);
5030
5031 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005032 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005033
5034 if (window->pointer_unconfined_handler) {
5035 window->pointer_unconfined_handler(window,
5036 input,
5037 window->user_data);
5038 }
5039}
5040
5041static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
5042 confined_pointer_confined,
5043 confined_pointer_unconfined,
5044};
5045
5046int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005047window_confine_pointer_to_rectangles(struct window *window,
5048 struct input *input,
5049 struct rectangle *rectangles,
5050 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005051{
5052 struct zwp_pointer_constraints_v1 *pointer_constraints =
5053 window->display->pointer_constraints;
5054 struct zwp_confined_pointer_v1 *confined_pointer;
5055 struct wl_compositor *compositor = window->display->compositor;
5056 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005057 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005058
5059 if (!window->display->pointer_constraints)
5060 return -1;
5061
5062 if (window->locked_pointer)
5063 return -1;
5064
5065 if (window->confined_pointer)
5066 return -1;
5067
5068 if (!input->pointer)
5069 return -1;
5070
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005071 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005072 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005073 for (i = 0; i < num_rectangles; i++) {
5074 wl_region_add(region,
5075 rectangles[i].x,
5076 rectangles[i].y,
5077 rectangles[i].width,
5078 rectangles[i].height);
5079 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005080 }
5081
5082 confined_pointer =
5083 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5084 window->main_surface->surface,
5085 input->pointer,
5086 region,
5087 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5088 if (region)
5089 wl_region_destroy(region);
5090
5091 zwp_confined_pointer_v1_add_listener(confined_pointer,
5092 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08005093 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005094
5095 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005096 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005097 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005098
5099 return 0;
5100}
5101
5102void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005103window_update_confine_rectangles(struct window *window,
5104 struct rectangle *rectangles,
5105 int num_rectangles)
5106{
5107 struct wl_compositor *compositor = window->display->compositor;
5108 struct wl_region *region;
5109 int i;
5110
5111 region = wl_compositor_create_region(compositor);
5112 for (i = 0; i < num_rectangles; i++) {
5113 wl_region_add(region,
5114 rectangles[i].x,
5115 rectangles[i].y,
5116 rectangles[i].width,
5117 rectangles[i].height);
5118 }
5119
5120 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5121
5122 wl_region_destroy(region);
5123}
5124
5125int
5126window_confine_pointer_to_widget(struct window *window,
5127 struct widget *widget,
5128 struct input *input)
5129{
5130 int ret;
5131
5132 if (widget) {
5133 ret = window_confine_pointer_to_rectangles(window,
5134 input,
5135 &widget->allocation,
5136 1);
5137 window->confined_widget = widget;
5138 return ret;
5139 } else {
5140 return window_confine_pointer_to_rectangles(window,
5141 input,
5142 NULL,
5143 0);
5144 }
5145}
5146
5147void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005148window_unconfine_pointer(struct window *window)
5149{
5150 if (!window->confined_pointer)
5151 return;
5152
5153 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5154 window->confined_pointer = NULL;
5155 window->confined = false;
5156}
5157
5158static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005159surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005160 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005161{
Rob Bradford7507b572012-05-15 17:55:34 +01005162 struct window *window = data;
5163 struct output *output;
5164 struct output *output_found = NULL;
5165 struct window_output *window_output;
5166
5167 wl_list_for_each(output, &window->display->output_list, link) {
5168 if (output->output == wl_output) {
5169 output_found = output;
5170 break;
5171 }
5172 }
5173
5174 if (!output_found)
5175 return;
5176
Brian Lovinbc919262013-08-07 15:34:59 -07005177 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005178 window_output->output = output_found;
5179
5180 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005181
5182 if (window->output_handler)
5183 window->output_handler(window, output_found, 1,
5184 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005185}
5186
5187static void
5188surface_leave(void *data,
5189 struct wl_surface *wl_surface, struct wl_output *output)
5190{
Rob Bradford7507b572012-05-15 17:55:34 +01005191 struct window *window = data;
5192 struct window_output *window_output;
5193 struct window_output *window_output_found = NULL;
5194
5195 wl_list_for_each(window_output, &window->window_output_list, link) {
5196 if (window_output->output->output == output) {
5197 window_output_found = window_output;
5198 break;
5199 }
5200 }
5201
5202 if (window_output_found) {
5203 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005204
5205 if (window->output_handler)
5206 window->output_handler(window, window_output->output,
5207 0, window->user_data);
5208
Rob Bradford7507b572012-05-15 17:55:34 +01005209 free(window_output_found);
5210 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005211}
5212
5213static const struct wl_surface_listener surface_listener = {
5214 surface_enter,
5215 surface_leave
5216};
5217
Pekka Paalanen4e373742013-02-13 16:17:13 +02005218static struct surface *
5219surface_create(struct window *window)
5220{
5221 struct display *display = window->display;
5222 struct surface *surface;
5223
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005224 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005225 surface->window = window;
5226 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005227 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005228 wl_surface_add_listener(surface->surface, &surface_listener, window);
5229
Pekka Paalanen35e82632013-04-25 13:57:48 +03005230 wl_list_insert(&window->subsurface_list, &surface->link);
Harish Krupo7bcbab12018-12-11 13:45:43 +05305231 surface->viewport = NULL;
Pekka Paalanen35e82632013-04-25 13:57:48 +03005232
Pekka Paalanen4e373742013-02-13 16:17:13 +02005233 return surface;
5234}
5235
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005236static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005237get_preferred_buffer_type(struct display *display)
5238{
5239#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005240 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005241 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5242#endif
5243
5244 return WINDOW_BUFFER_TYPE_SHM;
5245}
5246
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005247static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005248window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005249{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005250 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005251 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005252
Peter Huttererf3d62272013-08-08 11:57:05 +10005253 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005254 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005255 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005256
5257 surface = surface_create(window);
5258 window->main_surface = surface;
5259
Michael Teyfeld6371b52017-10-11 17:17:29 +02005260 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005261
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005262 window->custom = custom;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005263
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005264 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005265
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005266 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005267 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005268 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005269
Rob Bradford7507b572012-05-15 17:55:34 +01005270 wl_list_init (&window->window_output_list);
5271
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005272 return window;
5273}
5274
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005275struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005276window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005277{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005278 struct window *window;
5279
5280 window = window_create_internal(display, 0);
5281
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005282 if (window->display->xdg_shell) {
5283 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005284 xdg_wm_base_get_xdg_surface(window->display->xdg_shell,
5285 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005286 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005287
ant8mef99fac22018-11-28 22:46:37 +01005288 xdg_surface_add_listener(window->xdg_surface,
5289 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005290
5291 window->xdg_toplevel =
ant8mef99fac22018-11-28 22:46:37 +01005292 xdg_surface_get_toplevel(window->xdg_surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005293 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5294
ant8mef99fac22018-11-28 22:46:37 +01005295 xdg_toplevel_add_listener(window->xdg_toplevel,
5296 &xdg_toplevel_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005297
5298 window_inhibit_redraw(window);
5299
5300 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005301 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005302
5303 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005304}
5305
5306struct window *
5307window_create_custom(struct display *display)
5308{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005309 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005310}
5311
Jasper St. Pierre53686042013-12-09 15:26:25 -05005312void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005313window_set_parent(struct window *window,
5314 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005315{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005316 window->parent = parent_window;
5317 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005318}
5319
5320struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005321window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005322{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005323 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005324}
5325
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005326static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005327menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005328{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005329 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005330 int next;
5331
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005332 frame_interior(menu->frame, &x, &y, &width, &height);
5333 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005334 if (menu->current != next) {
5335 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005336 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005337 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005338}
5339
5340static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005341menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005342 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005343 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005344{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005345 struct menu *menu = data;
5346
5347 if (widget == menu->widget)
5348 menu_set_item(data, y);
5349
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005350 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005351}
5352
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005353static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005354menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005355 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005356{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005357 struct menu *menu = data;
5358
5359 if (widget == menu->widget)
5360 menu_set_item(data, y);
5361
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005362 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005363}
5364
5365static void
5366menu_leave_handler(struct widget *widget, struct input *input, void *data)
5367{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005368 struct menu *menu = data;
5369
5370 if (widget == menu->widget)
5371 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005372}
5373
5374static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005375menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005376 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005377 uint32_t button, enum wl_pointer_button_state state,
5378 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005379
5380{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005381 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005382
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005383 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5384 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005385 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005386 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005387 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005388 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005389 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005390 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005391 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005392 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005393}
5394
5395static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005396menu_touch_up_handler(struct widget *widget,
5397 struct input *input,
5398 uint32_t serial,
5399 uint32_t time,
5400 int32_t id,
5401 void *data)
5402{
5403 struct menu *menu = data;
5404
5405 input_ungrab(input);
5406 menu_destroy(menu);
5407}
5408
5409static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005410menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005411{
5412 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005413 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005414 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005415
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005416 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005417
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005418 frame_repaint(menu->frame, cr);
5419 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005420
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005421 theme_set_background_source(menu->window->display->theme,
5422 cr, THEME_FRAME_ACTIVE);
5423 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005424 cairo_fill(cr);
5425
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005426 cairo_select_font_face(cr, "sans",
5427 CAIRO_FONT_SLANT_NORMAL,
5428 CAIRO_FONT_WEIGHT_NORMAL);
5429 cairo_set_font_size(cr, 12);
5430
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005431 for (i = 0; i < menu->count; i++) {
5432 if (i == menu->current) {
5433 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005434 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005435 cairo_fill(cr);
5436 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005437 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005438 cairo_show_text(cr, menu->entries[i]);
5439 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005440 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5441 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005442 cairo_show_text(cr, menu->entries[i]);
5443 }
5444 }
5445
5446 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005447}
5448
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005449static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005450xdg_popup_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01005451 struct xdg_popup *xdg_popup,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005452 int32_t x,
5453 int32_t y,
5454 int32_t width,
5455 int32_t height)
5456{
5457}
5458
5459static void
ant8mef99fac22018-11-28 22:46:37 +01005460xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005461{
5462 struct window *window = data;
5463 struct menu *menu = window->main_surface->widget->user_data;
5464
5465 input_ungrab(menu->input);
5466 menu_destroy(menu);
5467}
5468
ant8mef99fac22018-11-28 22:46:37 +01005469static const struct xdg_popup_listener xdg_popup_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005470 xdg_popup_handle_configure,
5471 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005472};
5473
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005474static struct menu *
5475create_menu(struct display *display,
5476 struct input *input, uint32_t time,
5477 menu_func_t func, const char **entries, int count,
5478 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005479{
5480 struct window *window;
5481 struct menu *menu;
5482
5483 menu = malloc(sizeof *menu);
5484 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005485 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005486
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005487 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005488 if (!window) {
5489 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005490 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005491 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005492
5493 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005494 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005495 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005496 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005497 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005498 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005499 menu->entries = entries;
5500 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005501 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005502 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005503 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005504 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005505 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005506
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005507 input_ungrab(input);
5508
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005509 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005510 widget_set_enter_handler(menu->widget, menu_enter_handler);
5511 widget_set_leave_handler(menu->widget, menu_leave_handler);
5512 widget_set_motion_handler(menu->widget, menu_motion_handler);
5513 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005514 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005515
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005516 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005517 frame_resize_inside(menu->frame, 200, count * 20);
5518 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5519 window_schedule_resize(window, frame_width(menu->frame),
5520 frame_height(menu->frame));
5521
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005522 return menu;
5523}
5524
ant8mef99fac22018-11-28 22:46:37 +01005525static struct xdg_positioner *
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005526create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005527 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005528{
ant8mef99fac22018-11-28 22:46:37 +01005529 struct xdg_positioner *positioner;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005530
ant8mef99fac22018-11-28 22:46:37 +01005531 positioner = xdg_wm_base_create_positioner(display->xdg_shell);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005532 fail_on_null(positioner, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005533 xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1);
5534 xdg_positioner_set_size(positioner, w, h);
5535 xdg_positioner_set_anchor(positioner,
5536 XDG_POSITIONER_ANCHOR_TOP_LEFT);
5537 xdg_positioner_set_gravity(positioner,
5538 XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005539
5540 return positioner;
5541}
5542
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005543void
5544window_show_menu(struct display *display,
5545 struct input *input, uint32_t time, struct window *parent,
5546 int32_t x, int32_t y,
5547 menu_func_t func, const char **entries, int count)
5548{
5549 struct menu *menu;
5550 struct window *window;
5551 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005552 struct rectangle parent_geometry;
ant8mef99fac22018-11-28 22:46:37 +01005553 struct xdg_positioner *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005554
5555 menu = create_menu(display, input, time, func, entries, count, parent);
5556
5557 if (menu == NULL)
5558 return;
5559
5560 window = menu->window;
5561
5562 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5563 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5564
5565 window->x = x;
5566 window->y = y;
5567
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005568 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005569 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005570
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005571 if (!display->xdg_shell)
5572 return;
5573
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005574 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005575 xdg_wm_base_get_xdg_surface(display->xdg_shell,
5576 window->main_surface->surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005577 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005578
ant8mef99fac22018-11-28 22:46:37 +01005579 xdg_surface_add_listener(window->xdg_surface,
5580 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005581
5582 positioner = create_simple_positioner(display,
5583 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005584 window->y - (iy + parent_geometry.y),
5585 frame_width(menu->frame),
5586 frame_height(menu->frame));
ant8mef99fac22018-11-28 22:46:37 +01005587 window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
5588 parent->xdg_surface,
5589 positioner);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005590 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005591 xdg_positioner_destroy(positioner);
5592 xdg_popup_grab(window->xdg_popup, input->seat,
5593 display_get_serial(window->display));
5594 xdg_popup_add_listener(window->xdg_popup,
5595 &xdg_popup_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005596
5597 window_inhibit_redraw(window);
5598
5599 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005600}
5601
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005602void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005603window_set_buffer_type(struct window *window, enum window_buffer_type type)
5604{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005605 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005606}
5607
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005608enum window_buffer_type
5609window_get_buffer_type(struct window *window)
5610{
5611 return window->main_surface->buffer_type;
5612}
5613
Pekka Paalanen35e82632013-04-25 13:57:48 +03005614struct widget *
5615window_add_subsurface(struct window *window, void *data,
5616 enum subsurface_mode default_mode)
5617{
5618 struct widget *widget;
5619 struct surface *surface;
5620 struct wl_surface *parent;
5621 struct wl_subcompositor *subcompo = window->display->subcompositor;
5622
Pekka Paalanen35e82632013-04-25 13:57:48 +03005623 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005624 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005625 widget = widget_create(window, surface, data);
5626 wl_list_init(&widget->link);
5627 surface->widget = widget;
5628
5629 parent = window->main_surface->surface;
5630 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5631 surface->surface,
5632 parent);
5633 surface->synchronized = 1;
5634
5635 switch (default_mode) {
5636 case SUBSURFACE_SYNCHRONIZED:
5637 surface->synchronized_default = 1;
5638 break;
5639 case SUBSURFACE_DESYNCHRONIZED:
5640 surface->synchronized_default = 0;
5641 break;
5642 default:
5643 assert(!"bad enum subsurface_mode");
5644 }
5645
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005646 window->resize_needed = 1;
5647 window_schedule_redraw(window);
5648
Pekka Paalanen35e82632013-04-25 13:57:48 +03005649 return widget;
5650}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005651
5652static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005653display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005654 struct wl_output *wl_output,
5655 int x, int y,
5656 int physical_width,
5657 int physical_height,
5658 int subpixel,
5659 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005660 const char *model,
5661 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005662{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005663 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005664
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005665 output->allocation.x = x;
5666 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005667 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005668
5669 if (output->make)
5670 free(output->make);
5671 output->make = strdup(make);
5672
5673 if (output->model)
5674 free(output->model);
5675 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005676}
5677
5678static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005679display_handle_done(void *data,
5680 struct wl_output *wl_output)
5681{
5682}
5683
5684static void
5685display_handle_scale(void *data,
5686 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005687 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005688{
5689 struct output *output = data;
5690
5691 output->scale = scale;
5692}
5693
5694static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005695display_handle_mode(void *data,
5696 struct wl_output *wl_output,
5697 uint32_t flags,
5698 int width,
5699 int height,
5700 int refresh)
5701{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005702 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005703 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005704
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005705 if (flags & WL_OUTPUT_MODE_CURRENT) {
5706 output->allocation.width = width;
5707 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005708 if (display->output_configure_handler)
5709 (*display->output_configure_handler)(
5710 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005711 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005712}
5713
5714static const struct wl_output_listener output_listener = {
5715 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005716 display_handle_mode,
5717 display_handle_done,
5718 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005719};
5720
5721static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005722display_add_output(struct display *d, uint32_t id)
5723{
5724 struct output *output;
5725
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005726 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005727 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005728 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005729 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005730 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005731 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005732 wl_list_insert(d->output_list.prev, &output->link);
5733
5734 wl_output_add_listener(output->output, &output_listener, output);
5735}
5736
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005737static void
5738output_destroy(struct output *output)
5739{
5740 if (output->destroy_handler)
5741 (*output->destroy_handler)(output, output->user_data);
5742
5743 wl_output_destroy(output->output);
5744 wl_list_remove(&output->link);
5745 free(output);
5746}
5747
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005748static void
5749display_destroy_output(struct display *d, uint32_t id)
5750{
5751 struct output *output;
5752
5753 wl_list_for_each(output, &d->output_list, link) {
5754 if (output->server_output_id == id) {
5755 output_destroy(output);
5756 break;
5757 }
5758 }
5759}
5760
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005761void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005762display_set_global_handler(struct display *display,
5763 display_global_handler_t handler)
5764{
5765 struct global *global;
5766
5767 display->global_handler = handler;
5768 if (!handler)
5769 return;
5770
5771 wl_list_for_each(global, &display->global_list, link)
5772 display->global_handler(display,
5773 global->name, global->interface,
5774 global->version, display->user_data);
5775}
5776
5777void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005778display_set_global_handler_remove(struct display *display,
5779 display_global_handler_t remove_handler)
5780{
5781 display->global_handler_remove = remove_handler;
5782 if (!remove_handler)
5783 return;
5784}
5785
5786void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005787display_set_output_configure_handler(struct display *display,
5788 display_output_handler_t handler)
5789{
5790 struct output *output;
5791
5792 display->output_configure_handler = handler;
5793 if (!handler)
5794 return;
5795
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005796 wl_list_for_each(output, &display->output_list, link) {
5797 if (output->allocation.width == 0 &&
5798 output->allocation.height == 0)
5799 continue;
5800
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005801 (*display->output_configure_handler)(output,
5802 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005803 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005804}
5805
5806void
5807output_set_user_data(struct output *output, void *data)
5808{
5809 output->user_data = data;
5810}
5811
5812void *
5813output_get_user_data(struct output *output)
5814{
5815 return output->user_data;
5816}
5817
5818void
5819output_set_destroy_handler(struct output *output,
5820 display_output_handler_t handler)
5821{
5822 output->destroy_handler = handler;
5823 /* FIXME: implement this, once we have way to remove outputs */
5824}
5825
5826void
Scott Moreau4e072362012-09-29 02:03:11 -06005827output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005828{
Scott Moreau4e072362012-09-29 02:03:11 -06005829 struct rectangle allocation = output->allocation;
5830
5831 switch (output->transform) {
5832 case WL_OUTPUT_TRANSFORM_90:
5833 case WL_OUTPUT_TRANSFORM_270:
5834 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5835 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5836 /* Swap width and height */
5837 allocation.width = output->allocation.height;
5838 allocation.height = output->allocation.width;
5839 break;
5840 }
5841
5842 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005843}
5844
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005845struct wl_output *
5846output_get_wl_output(struct output *output)
5847{
5848 return output->output;
5849}
5850
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005851enum wl_output_transform
5852output_get_transform(struct output *output)
5853{
5854 return output->transform;
5855}
5856
Alexander Larssonafd319a2013-05-22 14:41:27 +02005857uint32_t
5858output_get_scale(struct output *output)
5859{
5860 return output->scale;
5861}
5862
Jason Ekstrand738715d2014-04-02 19:53:50 -05005863const char *
5864output_get_make(struct output *output)
5865{
5866 return output->make;
5867}
5868
5869const char *
5870output_get_model(struct output *output)
5871{
5872 return output->model;
5873}
5874
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005875static void
Daniel Stone97f68542012-05-30 16:32:01 +01005876fini_xkb(struct input *input)
5877{
5878 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005879 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005880}
5881
5882static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005883display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005884{
5885 struct input *input;
Sebastian Wickac1b92d2019-11-01 02:27:55 +01005886 int seat_version = MIN(display_seat_version, 7);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005887
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005888 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005889 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005890 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005891 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005892 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005893 input->pointer_focus = NULL;
5894 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005895 input->seat_version = seat_version;
5896
Rusty Lynch041815a2013-08-08 21:20:38 -07005897 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005898 wl_list_insert(d->input_list.prev, &input->link);
5899
Daniel Stone37816df2012-05-16 18:45:18 +01005900 wl_seat_add_listener(input->seat, &seat_listener, input);
5901 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005902
Jason Ekstranda669bd52014-04-02 19:53:51 -05005903 if (d->data_device_manager) {
5904 input->data_device =
5905 wl_data_device_manager_get_data_device(d->data_device_manager,
5906 input->seat);
5907 wl_data_device_add_listener(input->data_device,
5908 &data_device_listener,
5909 input);
5910 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005911
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005912 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5913 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005914
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005915 set_repeat_info(input, 40, 400);
5916 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5917 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005918}
5919
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005920static void
Harish Krupoee4c7a22019-04-19 01:53:27 +05305921display_add_data_device(struct display *d, uint32_t id, int ddm_version)
5922{
5923 struct input *input;
5924
5925 d->data_device_manager_version = MIN(ddm_version, 3);
5926 d->data_device_manager =
5927 wl_registry_bind(d->registry, id,
5928 &wl_data_device_manager_interface,
5929 d->data_device_manager_version);
5930
5931 wl_list_for_each(input, &d->input_list, link) {
5932 if (!input->data_device) {
5933 input->data_device =
5934 wl_data_device_manager_get_data_device(d->data_device_manager,
5935 input->seat);
5936 wl_data_device_add_listener(input->data_device,
5937 &data_device_listener,
5938 input);
5939 }
5940 }
5941}
5942
5943static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005944input_destroy(struct input *input)
5945{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005946 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005947 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005948
5949 if (input->drag_offer)
5950 data_offer_destroy(input->drag_offer);
5951
5952 if (input->selection_offer)
5953 data_offer_destroy(input->selection_offer);
5954
kabeer khan6ce67ec2014-10-20 11:55:29 +05305955 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005956 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305957 wl_data_device_release(input->data_device);
5958 else
5959 wl_data_device_destroy(input->data_device);
5960 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005961 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005962 if (input->touch)
5963 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005964 if (input->pointer)
5965 wl_pointer_release(input->pointer);
5966 if (input->keyboard)
5967 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005968 } else {
5969 if (input->touch)
5970 wl_touch_destroy(input->touch);
5971 if (input->pointer)
5972 wl_pointer_destroy(input->pointer);
5973 if (input->keyboard)
5974 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005975 }
5976
Daniel Stone97f68542012-05-30 16:32:01 +01005977 fini_xkb(input);
5978
Simon Ser992ee042021-02-07 11:32:46 +01005979 if (input->pointer_surface)
5980 wl_surface_destroy(input->pointer_surface);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005981
Pekka Paalanene1207c72011-12-16 12:02:09 +02005982 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005983 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005984 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005985 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005986 free(input);
5987}
5988
5989static void
ant8mef99fac22018-11-28 22:46:37 +01005990xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005991{
ant8mef99fac22018-11-28 22:46:37 +01005992 xdg_wm_base_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005993}
5994
ant8mef99fac22018-11-28 22:46:37 +01005995static const struct xdg_wm_base_listener wm_base_listener = {
5996 xdg_wm_base_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005997};
5998
5999static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006000registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
6001 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006002{
6003 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006004 struct global *global;
6005
Brian Lovinbc919262013-08-07 15:34:59 -07006006 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006007 global->name = id;
6008 global->interface = strdup(interface);
6009 global->version = version;
6010 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006011
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006012 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006013 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05006014 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006015 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006016 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01006017 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05006018 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08006019 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
6020 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
6021 d->relative_pointer_manager =
6022 wl_registry_bind(registry, id,
6023 &zwp_relative_pointer_manager_v1_interface,
6024 1);
6025 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
6026 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
6027 d->pointer_constraints =
6028 wl_registry_bind(registry, id,
6029 &zwp_pointer_constraints_v1_interface,
6030 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006031 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006032 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006033 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Harish Krupoee4c7a22019-04-19 01:53:27 +05306034 display_add_data_device(d, id, version);
ant8mef99fac22018-11-28 22:46:37 +01006035 } else if (strcmp(interface, "xdg_wm_base") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006036 d->xdg_shell = wl_registry_bind(registry, id,
ant8mef99fac22018-11-28 22:46:37 +01006037 &xdg_wm_base_interface, 1);
6038 xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06006039 } else if (strcmp(interface, "text_cursor_position") == 0) {
6040 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006041 wl_registry_bind(registry, id,
6042 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006043 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6044 d->subcompositor =
6045 wl_registry_bind(registry, id,
6046 &wl_subcompositor_interface, 1);
Harish Krupo7bcbab12018-12-11 13:45:43 +05306047 } else if (!strcmp(interface, "wp_viewporter")) {
6048 d->viewporter =
6049 wl_registry_bind(registry, id,
6050 &wp_viewporter_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006051 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006052
6053 if (d->global_handler)
6054 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006055}
6056
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006057static void
6058registry_handle_global_remove(void *data, struct wl_registry *registry,
6059 uint32_t name)
6060{
6061 struct display *d = data;
6062 struct global *global;
6063 struct global *tmp;
6064
6065 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6066 if (global->name != name)
6067 continue;
6068
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006069 if (strcmp(global->interface, "wl_output") == 0)
6070 display_destroy_output(d, name);
6071
6072 /* XXX: Should destroy remaining bound globals */
6073
6074 if (d->global_handler_remove)
6075 d->global_handler_remove(d, name, global->interface,
6076 global->version, d->user_data);
6077
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006078 wl_list_remove(&global->link);
6079 free(global->interface);
6080 free(global);
6081 }
6082}
6083
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006084void *
6085display_bind(struct display *display, uint32_t name,
6086 const struct wl_interface *interface, uint32_t version)
6087{
6088 return wl_registry_bind(display->registry, name, interface, version);
6089}
6090
6091static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006092 registry_handle_global,
6093 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006094};
6095
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006096#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006097static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006098init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006099{
6100 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006101 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006102
Rob Clark6396ed32012-03-11 19:48:41 -05006103#ifdef USE_CAIRO_GLESV2
6104# define GL_BIT EGL_OPENGL_ES2_BIT
6105#else
6106# define GL_BIT EGL_OPENGL_BIT
6107#endif
6108
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006109 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006110 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006111 EGL_RED_SIZE, 1,
6112 EGL_GREEN_SIZE, 1,
6113 EGL_BLUE_SIZE, 1,
6114 EGL_ALPHA_SIZE, 1,
6115 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006116 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006117 EGL_NONE
6118 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006119
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006120#ifdef USE_CAIRO_GLESV2
6121 static const EGLint context_attribs[] = {
6122 EGL_CONTEXT_CLIENT_VERSION, 2,
6123 EGL_NONE
6124 };
6125 EGLint api = EGL_OPENGL_ES_API;
6126#else
6127 EGLint *context_attribs = NULL;
6128 EGLint api = EGL_OPENGL_API;
6129#endif
6130
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006131 d->dpy =
6132 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6133 d->display, NULL);
6134
Yuval Fledel45568f62010-12-06 09:18:12 -05006135 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006136 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006137 return -1;
6138 }
6139
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006140 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006141 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006142 return -1;
6143 }
6144
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006145 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6146 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006147 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006148 return -1;
6149 }
6150
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006151 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006152 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006153 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006154 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006155 return -1;
6156 }
6157
Benjamin Franzke0c991632011-09-27 21:57:31 +02006158 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6159 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006160 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006161 return -1;
6162 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006163
6164 return 0;
6165}
6166
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006167static void
6168fini_egl(struct display *display)
6169{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006170 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006171
6172 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6173 EGL_NO_CONTEXT);
6174
6175 eglTerminate(display->dpy);
6176 eglReleaseThread();
6177}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006178#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006179
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006180static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006181init_dummy_surface(struct display *display)
6182{
6183 int len;
6184 void *data;
6185
6186 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006187 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006188 display->dummy_surface =
6189 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6190 1, 1, len);
6191 display->dummy_surface_data = data;
6192}
6193
6194static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006195handle_display_data(struct task *task, uint32_t events)
6196{
6197 struct display *display =
6198 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006199 struct epoll_event ep;
6200 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006201
6202 display->display_fd_events = events;
6203
6204 if (events & EPOLLERR || events & EPOLLHUP) {
6205 display_exit(display);
6206 return;
6207 }
6208
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006209 if (events & EPOLLIN) {
6210 ret = wl_display_dispatch(display->display);
6211 if (ret == -1) {
6212 display_exit(display);
6213 return;
6214 }
6215 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006216
6217 if (events & EPOLLOUT) {
6218 ret = wl_display_flush(display->display);
6219 if (ret == 0) {
6220 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6221 ep.data.ptr = &display->display_task;
6222 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6223 display->display_fd, &ep);
6224 } else if (ret == -1 && errno != EAGAIN) {
6225 display_exit(display);
6226 return;
6227 }
6228 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006229}
6230
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006231static void
6232log_handler(const char *format, va_list args)
6233{
6234 vfprintf(stderr, format, args);
6235}
6236
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006237struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006238display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006239{
6240 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006241
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006242 wl_log_set_handler_client(log_handler);
6243
Peter Huttererf3d62272013-08-08 11:57:05 +10006244 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006245 if (d == NULL)
6246 return NULL;
6247
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006248 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006249 if (d->display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02006250 fprintf(stderr, "failed to connect to Wayland display: %s\n",
6251 strerror(errno));
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006252 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006253 return NULL;
6254 }
6255
Peter Hutterera2086bb2020-05-13 15:24:27 +10006256 d->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Rob Bradford5ab9c752013-07-26 16:29:43 +01006257 if (d->xkb_context == NULL) {
6258 fprintf(stderr, "Failed to create XKB context\n");
6259 free(d);
6260 return NULL;
6261 }
6262
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006263 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006264 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006265 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006266 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6267 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006268
6269 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006270 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006271 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006272 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006273
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006274 d->registry = wl_display_get_registry(d->display);
6275 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006276
Marek Chalupaa519d062014-12-05 13:49:40 +01006277 if (wl_display_roundtrip(d->display) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006278 fprintf(stderr, "Failed to process Wayland connection: %s\n",
6279 strerror(errno));
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006280 return NULL;
6281 }
6282
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006283#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006284 if (init_egl(d) < 0)
6285 fprintf(stderr, "EGL does not seem to work, "
6286 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006287#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006288
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006289 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006290
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006291 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006292
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006293 wl_list_init(&d->window_list);
6294
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006295 init_dummy_surface(d);
6296
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006297 return d;
6298}
6299
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006300static void
6301display_destroy_outputs(struct display *display)
6302{
6303 struct output *tmp;
6304 struct output *output;
6305
6306 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6307 output_destroy(output);
6308}
6309
Pekka Paalanene1207c72011-12-16 12:02:09 +02006310static void
6311display_destroy_inputs(struct display *display)
6312{
6313 struct input *tmp;
6314 struct input *input;
6315
6316 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6317 input_destroy(input);
6318}
6319
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006320void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006321display_destroy(struct display *display)
6322{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006323 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006324 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6325 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006326
6327 if (!wl_list_empty(&display->deferred_list))
6328 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6329
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006330 cairo_surface_destroy(display->dummy_surface);
6331 free(display->dummy_surface_data);
6332
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006333 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006334 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006335
Daniel Stone97f68542012-05-30 16:32:01 +01006336 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006337
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006338 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006339 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006340
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006341#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006342 if (display->argb_device)
6343 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006344#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006345
Pekka Paalanen35e82632013-04-25 13:57:48 +03006346 if (display->subcompositor)
6347 wl_subcompositor_destroy(display->subcompositor);
6348
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006349 if (display->xdg_shell)
ant8mef99fac22018-11-28 22:46:37 +01006350 xdg_wm_base_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006351
6352 if (display->shm)
6353 wl_shm_destroy(display->shm);
6354
6355 if (display->data_device_manager)
6356 wl_data_device_manager_destroy(display->data_device_manager);
6357
6358 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006359 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006360
6361 close(display->epoll_fd);
6362
U. Artie Eoff44874d92012-10-02 21:12:35 -07006363 if (!(display->display_fd_events & EPOLLERR) &&
6364 !(display->display_fd_events & EPOLLHUP))
6365 wl_display_flush(display->display);
6366
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006367 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006368 free(display);
6369}
6370
6371void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006372display_set_user_data(struct display *display, void *data)
6373{
6374 display->user_data = data;
6375}
6376
6377void *
6378display_get_user_data(struct display *display)
6379{
6380 return display->user_data;
6381}
6382
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006383struct wl_display *
6384display_get_display(struct display *display)
6385{
6386 return display->display;
6387}
6388
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006389int
6390display_has_subcompositor(struct display *display)
6391{
6392 if (display->subcompositor)
6393 return 1;
6394
6395 wl_display_roundtrip(display->display);
6396
6397 return display->subcompositor != NULL;
6398}
6399
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006400cairo_device_t *
6401display_get_cairo_device(struct display *display)
6402{
6403 return display->argb_device;
6404}
6405
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006406struct output *
6407display_get_output(struct display *display)
6408{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006409 if (wl_list_empty(&display->output_list))
6410 return NULL;
6411
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006412 return container_of(display->output_list.next, struct output, link);
6413}
6414
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006415struct wl_compositor *
6416display_get_compositor(struct display *display)
6417{
6418 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006419}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006420
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006421uint32_t
6422display_get_serial(struct display *display)
6423{
6424 return display->serial;
6425}
6426
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006427EGLDisplay
6428display_get_egl_display(struct display *d)
6429{
6430 return d->dpy;
6431}
6432
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006433struct wl_data_source *
6434display_create_data_source(struct display *display)
6435{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006436 if (display->data_device_manager)
6437 return wl_data_device_manager_create_data_source(display->data_device_manager);
6438 else
6439 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006440}
6441
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006442EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006443display_get_argb_egl_config(struct display *d)
6444{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006445 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006446}
6447
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006448int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006449display_acquire_window_surface(struct display *display,
6450 struct window *window,
6451 EGLContext ctx)
6452{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006453 struct surface *surface = window->main_surface;
6454
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006455 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006456 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006457
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006458 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006459 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006460}
6461
6462void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006463display_release_window_surface(struct display *display,
6464 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006465{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006466 struct surface *surface = window->main_surface;
6467
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006468 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006469 return;
6470
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006471 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006472}
6473
6474void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006475display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006476{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006477 wl_list_insert(&display->deferred_list, &task->link);
6478}
6479
6480void
6481display_watch_fd(struct display *display,
6482 int fd, uint32_t events, struct task *task)
6483{
6484 struct epoll_event ep;
6485
6486 ep.events = events;
6487 ep.data.ptr = task;
6488 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6489}
6490
6491void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006492display_unwatch_fd(struct display *display, int fd)
6493{
6494 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6495}
6496
6497void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006498display_run(struct display *display)
6499{
6500 struct task *task;
6501 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006502 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006503
Pekka Paalanen826d7952011-12-15 10:14:07 +02006504 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006505 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006506 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006507 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006508 struct task, link);
6509 wl_list_remove(&task->link);
6510 task->run(task, 0);
6511 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006512
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006513 wl_display_dispatch_pending(display->display);
6514
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006515 if (!display->running)
6516 break;
6517
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006518 ret = wl_display_flush(display->display);
6519 if (ret < 0 && errno == EAGAIN) {
6520 ep[0].events =
6521 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6522 ep[0].data.ptr = &display->display_task;
6523
6524 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6525 display->display_fd, &ep[0]);
6526 } else if (ret < 0) {
6527 break;
6528 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006529
6530 count = epoll_wait(display->epoll_fd,
6531 ep, ARRAY_LENGTH(ep), -1);
6532 for (i = 0; i < count; i++) {
6533 task = ep[i].data.ptr;
6534 task->run(task, ep[i].events);
6535 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006536 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006537}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006538
6539void
6540display_exit(struct display *display)
6541{
6542 display->running = 0;
6543}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006544
Carlos Garnacho9c931792016-01-18 23:52:12 +01006545int
6546display_get_data_device_manager_version(struct display *display)
6547{
6548 return display->data_device_manager_version;
6549}
6550
Jan Arne Petersencd997062012-11-18 19:06:44 +01006551void
6552keysym_modifiers_add(struct wl_array *modifiers_map,
6553 const char *name)
6554{
6555 size_t len = strlen(name) + 1;
6556 char *p;
6557
6558 p = wl_array_add(modifiers_map, len);
6559
6560 if (p == NULL)
6561 return;
6562
6563 strncpy(p, name, len);
6564}
6565
6566static xkb_mod_index_t
6567keysym_modifiers_get_index(struct wl_array *modifiers_map,
6568 const char *name)
6569{
6570 xkb_mod_index_t index = 0;
6571 char *p = modifiers_map->data;
6572
6573 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6574 if (strcmp(p, name) == 0)
6575 return index;
6576
6577 index++;
6578 p += strlen(p) + 1;
6579 }
6580
6581 return XKB_MOD_INVALID;
6582}
6583
6584xkb_mod_mask_t
6585keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6586 const char *name)
6587{
6588 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6589
6590 if (index == XKB_MOD_INVALID)
6591 return XKB_MOD_INVALID;
6592
6593 return 1 << index;
6594}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006595
6596static void
6597toytimer_fire(struct task *tsk, uint32_t events)
6598{
6599 uint64_t e;
6600 struct toytimer *tt;
6601
6602 tt = container_of(tsk, struct toytimer, tsk);
6603
6604 if (events != EPOLLIN)
6605 fprintf(stderr, "unexpected timerfd events %x\n", events);
6606
6607 if (!(events & EPOLLIN))
6608 return;
6609
6610 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6611 /* If we change the timer between the fd becoming
6612 * readable and getting here, there'll be nothing to
6613 * read and we get EAGAIN. */
6614 if (errno != EAGAIN)
Antonio Borneo39578632019-04-26 23:57:31 +02006615 fprintf(stderr, "timer read failed: %s\n",
6616 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006617 return;
6618 }
6619
6620 tt->callback(tt);
6621}
6622
6623void
6624toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6625 toytimer_cb callback)
6626{
6627 memset(tt, 0, sizeof *tt);
6628
6629 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6630 if (tt->fd == -1) {
Antonio Borneo39578632019-04-26 23:57:31 +02006631 fprintf(stderr, "creating timer failed: %s\n",
6632 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006633 abort();
6634 }
6635
6636 tt->display = display;
6637 tt->callback = callback;
6638 tt->tsk.run = toytimer_fire;
6639 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6640}
6641
6642void
6643toytimer_fini(struct toytimer *tt)
6644{
6645 display_unwatch_fd(tt->display, tt->fd);
6646 close(tt->fd);
6647 tt->fd = -1;
6648}
6649
6650void
6651toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6652{
6653 int ret;
6654
6655 ret = timerfd_settime(tt->fd, 0, its, NULL);
6656 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006657 fprintf(stderr, "timer setup failed: %s\n", strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006658 abort();
6659 }
6660}
6661
6662#define USEC_PER_SEC 1000000
6663
6664void
6665toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6666{
6667 struct itimerspec its;
6668
6669 its.it_interval.tv_sec = 0;
6670 its.it_interval.tv_nsec = 0;
6671 its.it_value.tv_sec = usec / USEC_PER_SEC;
6672 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6673 toytimer_arm(tt, &its);
6674}
6675
6676void
6677toytimer_disarm(struct toytimer *tt)
6678{
6679 struct itimerspec its = {};
6680
6681 toytimer_arm(tt, &its);
6682}