blob: 1bb9f3dc30686b839ffae6bfc97169a5097ad57b [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:
1835 angle = M_PI_2;
1836 translate_x = surface_height;
1837 translate_y = 0;
1838 break;
1839 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1840 angle = M_PI_2;
1841 translate_x = surface_height;
1842 translate_y = surface_width;
1843 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:
1855 angle = M_PI + M_PI_2;
1856 translate_x = 0;
1857 translate_y = surface_width;
1858 break;
1859 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1860 angle = M_PI + M_PI_2;
1861 translate_x = 0;
1862 translate_y = 0;
1863 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);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002580
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002581 frame->widget = window_add_widget(window, frame);
2582 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002583
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002584 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2585 widget_set_resize_handler(frame->widget, frame_resize_handler);
2586 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002587 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002588 widget_set_motion_handler(frame->widget, frame_motion_handler);
2589 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002590 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002591 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002592
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002593 window->frame = frame;
2594
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002595 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002596}
2597
Kristian Høgsberga1627922012-06-20 17:30:03 -04002598void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002599window_frame_set_child_size(struct widget *widget, int child_width,
2600 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002601{
2602 struct display *display = widget->window->display;
2603 struct theme *t = display->theme;
2604 int decoration_width, decoration_height;
2605 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002606 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002607
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002608 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002609 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002610 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002611 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002612
2613 width = child_width + decoration_width;
2614 height = child_height + decoration_height;
2615 } else {
2616 width = child_width;
2617 height = child_height;
2618 }
2619
2620 window_schedule_resize(widget->window, width, height);
2621}
2622
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002623static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002624window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002625{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002626 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002627
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002628 /* frame->child must be destroyed by the application */
2629 widget_destroy(frame->widget);
2630 free(frame);
2631}
2632
2633static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002634input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002635 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002636{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002637 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002638 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002639
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002640 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002641 return;
2642
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002643 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002644 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002645 widget = old;
2646 if (input->grab)
2647 widget = input->grab;
2648 if (widget->leave_handler)
2649 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002650 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002651 }
2652
2653 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002654 widget = focus;
2655 if (input->grab)
2656 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002657 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002658 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002659 cursor = widget->enter_handler(focus, input, x, y,
2660 widget->user_data);
2661 else
2662 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002663
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002664 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002665 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002666}
2667
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002668void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002669touch_grab(struct input *input, int32_t touch_id)
2670{
2671 input->touch_grab = 1;
2672 input->touch_grab_id = touch_id;
2673}
2674
2675void
2676touch_ungrab(struct input *input)
2677{
2678 struct touch_point *tp, *tmp;
2679
2680 input->touch_grab = 0;
2681
2682 wl_list_for_each_safe(tp, tmp,
2683 &input->touch_point_list, link) {
2684 if (tp->id != input->touch_grab_id)
2685 continue;
2686 wl_list_remove(&tp->link);
2687 free(tp);
2688
2689 return;
2690 }
2691}
2692
2693void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002694input_grab(struct input *input, struct widget *widget, uint32_t button)
2695{
2696 input->grab = widget;
2697 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002698
2699 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002700}
2701
2702void
2703input_ungrab(struct input *input)
2704{
2705 struct widget *widget;
2706
2707 input->grab = NULL;
2708 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002709 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002710 input->sx, input->sy);
2711 input_set_focus_widget(input, widget, input->sx, input->sy);
2712 }
2713}
2714
2715static void
Derek Foreman118a4292015-04-22 17:23:35 -05002716cursor_delay_timer_reset(struct input *input, uint32_t duration)
2717{
Derek Foreman118a4292015-04-22 17:23:35 -05002718 if (!duration)
2719 input->cursor_timer_running = false;
2720 else
2721 input->cursor_timer_running = true;
2722
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002723 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002724}
2725
2726static void cancel_pointer_image_update(struct input *input)
2727{
2728 if (input->cursor_timer_running)
2729 cursor_delay_timer_reset(input, 0);
2730}
2731
2732static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002733input_remove_pointer_focus(struct input *input)
2734{
2735 struct window *window = input->pointer_focus;
2736
2737 if (!window)
2738 return;
2739
2740 input_set_focus_widget(input, NULL, 0, 0);
2741
2742 input->pointer_focus = NULL;
2743 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002744 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002745}
2746
2747static void
2748pointer_handle_enter(void *data, struct wl_pointer *pointer,
2749 uint32_t serial, struct wl_surface *surface,
2750 wl_fixed_t sx_w, wl_fixed_t sy_w)
2751{
2752 struct input *input = data;
2753 struct window *window;
2754 struct widget *widget;
2755 float sx = wl_fixed_to_double(sx_w);
2756 float sy = wl_fixed_to_double(sy_w);
2757
2758 if (!surface) {
2759 /* enter event for a window we've just destroyed */
2760 return;
2761 }
2762
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002763 window = wl_surface_get_user_data(surface);
2764 if (surface != window->main_surface->surface) {
2765 DBG("Ignoring input event from subsurface %p\n", surface);
2766 return;
2767 }
2768
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002769 input->display->serial = serial;
2770 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002771 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002772
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002773 input->sx = sx;
2774 input->sy = sy;
2775
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002776 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002777 input_set_focus_widget(input, widget, sx, sy);
2778}
2779
2780static void
2781pointer_handle_leave(void *data, struct wl_pointer *pointer,
2782 uint32_t serial, struct wl_surface *surface)
2783{
2784 struct input *input = data;
2785
2786 input->display->serial = serial;
2787 input_remove_pointer_focus(input);
2788}
2789
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002790static void
Daniel Stone37816df2012-05-16 18:45:18 +01002791pointer_handle_motion(void *data, struct wl_pointer *pointer,
2792 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002793{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002794 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002795 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002796 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002797 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002798 float sx = wl_fixed_to_double(sx_w);
2799 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002800
Paul Winwoodb22bf572013-08-29 10:52:54 +01002801 if (!window)
2802 return;
2803
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002804 input->sx = sx;
2805 input->sy = sy;
2806
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002807 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002808 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002809 * based on the old surface dimensions. However, if we have an active
2810 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002811 */
Derek Foreman46812b62015-08-26 17:13:27 -05002812 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002813 sy < window->main_surface->allocation.y ||
2814 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002815 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002816 return;
2817
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002818 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002819 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002820 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002821 }
2822
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002823 if (input->grab)
2824 widget = input->grab;
2825 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002826 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002827 if (widget) {
2828 if (widget->motion_handler)
2829 cursor = widget->motion_handler(input->focus_widget,
2830 input, time, sx, sy,
2831 widget->user_data);
2832 else
2833 cursor = widget->default_cursor;
2834 } else
2835 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002836
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002837 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002838}
2839
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002840static void
Daniel Stone37816df2012-05-16 18:45:18 +01002841pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002842 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002843{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002844 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002845 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002846 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002847
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002848 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002849 if (input->focus_widget && input->grab == NULL &&
2850 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002851 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002852
Neil Roberts6b28aad2012-01-23 19:11:18 +00002853 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002854 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002855 (*widget->button_handler)(widget,
2856 input, time,
2857 button, state,
2858 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002859
Daniel Stone4dbadb12012-05-30 16:31:51 +01002860 if (input->grab && input->grab_button == button &&
2861 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002862 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002863}
2864
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002865static void
Daniel Stone37816df2012-05-16 18:45:18 +01002866pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002867 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002868{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002869 struct input *input = data;
2870 struct widget *widget;
2871
2872 widget = input->focus_widget;
2873 if (input->grab)
2874 widget = input->grab;
2875 if (widget && widget->axis_handler)
2876 (*widget->axis_handler)(widget,
2877 input, time,
2878 axis, value,
2879 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002880}
2881
Peter Hutterer87743e92016-01-18 16:38:22 +10002882static void
2883pointer_handle_frame(void *data, struct wl_pointer *pointer)
2884{
2885 struct input *input = data;
2886 struct widget *widget;
2887
2888 widget = input->focus_widget;
2889 if (input->grab)
2890 widget = input->grab;
2891 if (widget && widget->pointer_frame_handler)
2892 (*widget->pointer_frame_handler)(widget,
2893 input,
2894 widget->user_data);
2895}
2896
2897static void
2898pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2899 uint32_t source)
2900{
2901 struct input *input = data;
2902 struct widget *widget;
2903
2904 widget = input->focus_widget;
2905 if (input->grab)
2906 widget = input->grab;
2907 if (widget && widget->axis_source_handler)
2908 (*widget->axis_source_handler)(widget,
2909 input,
2910 source,
2911 widget->user_data);
2912}
2913
2914static void
2915pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2916 uint32_t time, uint32_t axis)
2917{
2918 struct input *input = data;
2919 struct widget *widget;
2920
2921 widget = input->focus_widget;
2922 if (input->grab)
2923 widget = input->grab;
2924 if (widget && widget->axis_stop_handler)
2925 (*widget->axis_stop_handler)(widget,
2926 input, time,
2927 axis,
2928 widget->user_data);
2929}
2930
2931static void
2932pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2933 uint32_t axis, int32_t discrete)
2934{
2935 struct input *input = data;
2936 struct widget *widget;
2937
2938 widget = input->focus_widget;
2939 if (input->grab)
2940 widget = input->grab;
2941 if (widget && widget->axis_discrete_handler)
2942 (*widget->axis_discrete_handler)(widget,
2943 input,
2944 axis,
2945 discrete,
2946 widget->user_data);
2947}
2948
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002949static const struct wl_pointer_listener pointer_listener = {
2950 pointer_handle_enter,
2951 pointer_handle_leave,
2952 pointer_handle_motion,
2953 pointer_handle_button,
2954 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002955 pointer_handle_frame,
2956 pointer_handle_axis_source,
2957 pointer_handle_axis_stop,
2958 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002959};
2960
2961static void
2962input_remove_keyboard_focus(struct input *input)
2963{
2964 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002965
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002966 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002967
2968 if (!window)
2969 return;
2970
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002971 if (window->keyboard_focus_handler)
2972 (*window->keyboard_focus_handler)(window, NULL,
2973 window->user_data);
2974
2975 input->keyboard_focus = NULL;
2976}
2977
2978static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002979keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002980{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002981 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002982 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002983
2984 if (window && window->key_handler) {
2985 (*window->key_handler)(window, input, input->repeat_time,
2986 input->repeat_key, input->repeat_sym,
2987 WL_KEYBOARD_KEY_STATE_PRESSED,
2988 window->user_data);
2989 }
2990}
2991
2992static void
2993keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2994 uint32_t format, int fd, uint32_t size)
2995{
2996 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002997 struct xkb_keymap *keymap;
2998 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002999#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003000 struct xkb_compose_table *compose_table;
3001 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01003002#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003003 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003004 char *map_str;
3005
3006 if (!data) {
3007 close(fd);
3008 return;
3009 }
3010
3011 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3012 close(fd);
3013 return;
3014 }
3015
Sebastian Wickac1b92d2019-11-01 02:27:55 +01003016 map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003017 if (map_str == MAP_FAILED) {
3018 close(fd);
3019 return;
3020 }
3021
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003022 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03003023 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
3024 map_str,
3025 XKB_KEYMAP_FORMAT_TEXT_V1,
3026 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003027 munmap(map_str, size);
3028 close(fd);
3029
Rui Matos3eccb862013-10-10 19:44:22 +02003030 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003031 fprintf(stderr, "failed to compile keymap\n");
3032 return;
3033 }
3034
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003035 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02003036 state = xkb_state_new(keymap);
3037 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003038 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003039 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003040 return;
3041 }
3042
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003043 /* Look up the preferred locale, falling back to "C" as default */
3044 if (!(locale = getenv("LC_ALL")))
3045 if (!(locale = getenv("LC_CTYPE")))
3046 if (!(locale = getenv("LANG")))
3047 locale = "C";
3048
3049 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003050#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003051 compose_table =
3052 xkb_compose_table_new_from_locale(input->display->xkb_context,
3053 locale,
3054 XKB_COMPOSE_COMPILE_NO_FLAGS);
3055 if (compose_table) {
3056 /* Set up XKB compose state */
3057 compose_state = xkb_compose_state_new(compose_table,
3058 XKB_COMPOSE_STATE_NO_FLAGS);
3059 if (compose_state) {
3060 xkb_compose_state_unref(input->xkb.compose_state);
3061 xkb_compose_table_unref(input->xkb.compose_table);
3062 input->xkb.compose_state = compose_state;
3063 input->xkb.compose_table = compose_table;
3064 } else {
3065 fprintf(stderr, "could not create XKB compose state. "
3066 "Disabiling compose.\n");
3067 xkb_compose_table_unref(compose_table);
3068 compose_table = NULL;
3069 }
3070 } else {
3071 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3072 "Disabiling compose\n", locale);
3073 }
Daniel Stone5b015962016-10-20 14:45:58 +01003074#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003075
Rui Matos3eccb862013-10-10 19:44:22 +02003076 xkb_keymap_unref(input->xkb.keymap);
3077 xkb_state_unref(input->xkb.state);
3078 input->xkb.keymap = keymap;
3079 input->xkb.state = state;
3080
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003081 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003082 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003083 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003084 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003085 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003086 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003087}
3088
3089static void
3090keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3091 uint32_t serial, struct wl_surface *surface,
3092 struct wl_array *keys)
3093{
3094 struct input *input = data;
3095 struct window *window;
3096
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003097 if (!surface) {
3098 /* enter event for a window we've just destroyed */
3099 return;
3100 }
3101
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003102 input->display->serial = serial;
3103 input->keyboard_focus = wl_surface_get_user_data(surface);
3104
3105 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003106 if (window->keyboard_focus_handler)
3107 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003108 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003109}
3110
3111static void
3112keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3113 uint32_t serial, struct wl_surface *surface)
3114{
3115 struct input *input = data;
3116
3117 input->display->serial = serial;
3118 input_remove_keyboard_focus(input);
3119}
3120
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003121/* Translate symbols appropriately if a compose sequence is being entered */
3122static xkb_keysym_t
3123process_key_press(xkb_keysym_t sym, struct input *input)
3124{
Daniel Stone5b015962016-10-20 14:45:58 +01003125#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003126 if (!input->xkb.compose_state)
3127 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003128 if (sym == XKB_KEY_NoSymbol)
3129 return sym;
3130 if (xkb_compose_state_feed(input->xkb.compose_state,
3131 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3132 return sym;
3133
3134 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3135 case XKB_COMPOSE_COMPOSING:
3136 return XKB_KEY_NoSymbol;
3137 case XKB_COMPOSE_COMPOSED:
3138 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3139 case XKB_COMPOSE_CANCELLED:
3140 return XKB_KEY_NoSymbol;
3141 case XKB_COMPOSE_NOTHING:
3142 return sym;
3143 default:
3144 return sym;
3145 }
Daniel Stone5b015962016-10-20 14:45:58 +01003146#else
3147 return sym;
3148#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003149}
3150
Scott Moreau210d0792012-03-22 10:47:01 -06003151static void
Daniel Stone37816df2012-05-16 18:45:18 +01003152keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003153 uint32_t serial, uint32_t time, uint32_t key,
3154 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003155{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003156 struct input *input = data;
3157 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003158 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003159 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003160 const xkb_keysym_t *syms;
3161 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003162 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003163
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003164 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003165 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003166 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003167 return;
3168
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003169 /* We only use input grabs for pointer events for now, so just
3170 * ignore key presses if a grab is active. We expand the key
3171 * event delivery mechanism to route events to widgets to
3172 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003173 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003174 if (input->grab && input->grab_button == 0)
3175 return;
3176
Ran Benita2e1968f2014-08-19 23:59:51 +03003177 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003178
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003179 sym = XKB_KEY_NoSymbol;
3180 if (num_syms == 1)
3181 sym = syms[0];
3182
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003183
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003184 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003185 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003186 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003187 } else if (sym == XKB_KEY_F11 &&
3188 window->fullscreen_handler &&
3189 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3190 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003191 } else if (sym == XKB_KEY_F4 &&
3192 input->modifiers == MOD_ALT_MASK &&
3193 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003194 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003195 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003196 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3197 sym = process_key_press(sym, input);
3198
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003199 (*window->key_handler)(window, input, time, key,
3200 sym, state, window->user_data);
3201 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003202
3203 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3204 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003205 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003206 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3207 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003208 input->repeat_sym = sym;
3209 input->repeat_key = key;
3210 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003211 its.it_interval.tv_sec = input->repeat_rate_sec;
3212 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3213 its.it_value.tv_sec = input->repeat_delay_sec;
3214 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003215 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003216 }
3217}
3218
3219static void
Daniel Stone351eb612012-05-31 15:27:47 -04003220keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3221 uint32_t serial, uint32_t mods_depressed,
3222 uint32_t mods_latched, uint32_t mods_locked,
3223 uint32_t group)
3224{
3225 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003226 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003227
Matt Ropere61561f2013-06-24 16:52:43 +01003228 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3229 if (!input->xkb.keymap)
3230 return;
3231
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003232 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3233 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003234 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003235 XKB_STATE_MODS_DEPRESSED |
3236 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003237 input->modifiers = 0;
3238 if (mask & input->xkb.control_mask)
3239 input->modifiers |= MOD_CONTROL_MASK;
3240 if (mask & input->xkb.alt_mask)
3241 input->modifiers |= MOD_ALT_MASK;
3242 if (mask & input->xkb.shift_mask)
3243 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003244}
3245
Jonny Lamb06959082014-08-12 14:58:27 +02003246static void
3247set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3248{
3249 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3250 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3251
3252 /* a rate of zero disables any repeating, regardless of the delay's
3253 * value */
3254 if (rate == 0)
3255 return;
3256
3257 if (rate == 1)
3258 input->repeat_rate_sec = 1;
3259 else
3260 input->repeat_rate_nsec = 1000000000 / rate;
3261
3262 input->repeat_delay_sec = delay / 1000;
3263 delay -= (input->repeat_delay_sec * 1000);
3264 input->repeat_delay_nsec = delay * 1000 * 1000;
3265}
3266
3267static void
3268keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3269 int32_t rate, int32_t delay)
3270{
3271 struct input *input = data;
3272
3273 set_repeat_info(input, rate, delay);
3274}
3275
Daniel Stone37816df2012-05-16 18:45:18 +01003276static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003277 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003278 keyboard_handle_enter,
3279 keyboard_handle_leave,
3280 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003281 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003282 keyboard_handle_repeat_info
3283
Daniel Stone37816df2012-05-16 18:45:18 +01003284};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003285
3286static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003287touch_handle_down(void *data, struct wl_touch *wl_touch,
3288 uint32_t serial, uint32_t time, struct wl_surface *surface,
3289 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3290{
3291 struct input *input = data;
3292 struct widget *widget;
3293 float sx = wl_fixed_to_double(x_w);
3294 float sy = wl_fixed_to_double(y_w);
3295
Jeffy Chen97749172019-09-02 10:26:09 +08003296 if (!surface) {
3297 /* down event for a window we've just destroyed */
3298 return;
3299 }
3300
Rusty Lynch1084da52013-08-15 09:10:08 -07003301 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003302 input->touch_focus = wl_surface_get_user_data(surface);
3303 if (!input->touch_focus) {
3304 DBG("Failed to find to touch focus for surface %p\n", surface);
3305 return;
3306 }
3307
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003308 if (surface != input->touch_focus->main_surface->surface) {
3309 DBG("Ignoring input event from subsurface %p\n", surface);
3310 input->touch_focus = NULL;
3311 return;
3312 }
3313
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003314 if (input->grab)
3315 widget = input->grab;
3316 else
3317 widget = window_find_widget(input->touch_focus,
3318 wl_fixed_to_double(x_w),
3319 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003320 if (widget) {
3321 struct touch_point *tp = xmalloc(sizeof *tp);
3322 if (tp) {
3323 tp->id = id;
3324 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003325 tp->x = sx;
3326 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003327 wl_list_insert(&input->touch_point_list, &tp->link);
3328
3329 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003330 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003331 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003332 sx, sy,
3333 widget->user_data);
3334 }
3335 }
3336}
3337
3338static void
3339touch_handle_up(void *data, struct wl_touch *wl_touch,
3340 uint32_t serial, uint32_t time, int32_t id)
3341{
3342 struct input *input = data;
3343 struct touch_point *tp, *tmp;
3344
Rusty Lynch041815a2013-08-08 21:20:38 -07003345 if (!input->touch_focus) {
3346 DBG("No touch focus found for touch up event!\n");
3347 return;
3348 }
3349
3350 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3351 if (tp->id != id)
3352 continue;
3353
3354 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003355 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003356 time, id,
3357 tp->widget->user_data);
3358
3359 wl_list_remove(&tp->link);
3360 free(tp);
3361
3362 return;
3363 }
3364}
3365
3366static void
3367touch_handle_motion(void *data, struct wl_touch *wl_touch,
3368 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3369{
3370 struct input *input = data;
3371 struct touch_point *tp;
3372 float sx = wl_fixed_to_double(x_w);
3373 float sy = wl_fixed_to_double(y_w);
3374
3375 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3376
3377 if (!input->touch_focus) {
3378 DBG("No touch focus found for touch motion event!\n");
3379 return;
3380 }
3381
3382 wl_list_for_each(tp, &input->touch_point_list, link) {
3383 if (tp->id != id)
3384 continue;
3385
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003386 tp->x = sx;
3387 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003388 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003389 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003390 id, sx, sy,
3391 tp->widget->user_data);
3392 return;
3393 }
3394}
3395
3396static void
3397touch_handle_frame(void *data, struct wl_touch *wl_touch)
3398{
3399 struct input *input = data;
3400 struct touch_point *tp, *tmp;
3401
3402 DBG("touch_handle_frame\n");
3403
3404 if (!input->touch_focus) {
3405 DBG("No touch focus found for touch frame event!\n");
3406 return;
3407 }
3408
3409 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3410 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003411 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003412 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003413 }
3414}
3415
3416static void
3417touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3418{
3419 struct input *input = data;
3420 struct touch_point *tp, *tmp;
3421
3422 DBG("touch_handle_cancel\n");
3423
3424 if (!input->touch_focus) {
3425 DBG("No touch focus found for touch cancel event!\n");
3426 return;
3427 }
3428
3429 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3430 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003431 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3432 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003433
3434 wl_list_remove(&tp->link);
3435 free(tp);
3436 }
3437}
3438
Guillaume Champagne1cb09482020-01-27 20:12:16 -05003439static void
3440touch_handle_shape(void *data, struct wl_touch *wl_touch, int32_t id,
3441 wl_fixed_t major, wl_fixed_t minor)
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003442{
3443}
3444
Guillaume Champagne1cb09482020-01-27 20:12:16 -05003445static void
3446touch_handle_orientation(void *data, struct wl_touch *wl_touch, int32_t id,
3447 wl_fixed_t orientation)
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003448{
3449}
3450
Rusty Lynch041815a2013-08-08 21:20:38 -07003451static const struct wl_touch_listener touch_listener = {
3452 touch_handle_down,
3453 touch_handle_up,
3454 touch_handle_motion,
3455 touch_handle_frame,
3456 touch_handle_cancel,
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003457 touch_handle_shape,
3458 touch_handle_orientation,
Rusty Lynch041815a2013-08-08 21:20:38 -07003459};
3460
3461static void
Daniel Stone37816df2012-05-16 18:45:18 +01003462seat_handle_capabilities(void *data, struct wl_seat *seat,
3463 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003464{
Daniel Stone37816df2012-05-16 18:45:18 +01003465 struct input *input = data;
3466
3467 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3468 input->pointer = wl_seat_get_pointer(seat);
3469 wl_pointer_set_user_data(input->pointer, input);
3470 wl_pointer_add_listener(input->pointer, &pointer_listener,
3471 input);
3472 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003473 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003474 wl_pointer_release(input->pointer);
3475 else
3476 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003477 input->pointer = NULL;
3478 }
3479
3480 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3481 input->keyboard = wl_seat_get_keyboard(seat);
3482 wl_keyboard_set_user_data(input->keyboard, input);
3483 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3484 input);
3485 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003486 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003487 wl_keyboard_release(input->keyboard);
3488 else
3489 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003490 input->keyboard = NULL;
3491 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003492
3493 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3494 input->touch = wl_seat_get_touch(seat);
3495 wl_touch_set_user_data(input->touch, input);
3496 wl_touch_add_listener(input->touch, &touch_listener, input);
3497 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003498 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003499 wl_touch_release(input->touch);
3500 else
3501 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003502 input->touch = NULL;
3503 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003504}
3505
Rob Bradford08031182013-08-13 20:11:03 +01003506static void
3507seat_handle_name(void *data, struct wl_seat *seat,
3508 const char *name)
3509{
3510
3511}
3512
Daniel Stone37816df2012-05-16 18:45:18 +01003513static const struct wl_seat_listener seat_listener = {
3514 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003515 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003516};
3517
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003518void
3519input_get_position(struct input *input, int32_t *x, int32_t *y)
3520{
3521 *x = input->sx;
3522 *y = input->sy;
3523}
3524
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003525int
3526input_get_touch(struct input *input, int32_t id, float *x, float *y)
3527{
3528 struct touch_point *tp;
3529
3530 wl_list_for_each(tp, &input->touch_point_list, link) {
3531 if (tp->id != id)
3532 continue;
3533
3534 *x = tp->x;
3535 *y = tp->y;
3536 return 0;
3537 }
3538
3539 return -1;
3540}
3541
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003542struct display *
3543input_get_display(struct input *input)
3544{
3545 return input->display;
3546}
3547
Daniel Stone37816df2012-05-16 18:45:18 +01003548struct wl_seat *
3549input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003550{
Daniel Stone37816df2012-05-16 18:45:18 +01003551 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003552}
3553
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003554uint32_t
3555input_get_modifiers(struct input *input)
3556{
3557 return input->modifiers;
3558}
3559
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003560struct widget *
3561input_get_focus_widget(struct input *input)
3562{
3563 return input->focus_widget;
3564}
3565
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003566struct data_offer {
3567 struct wl_data_offer *offer;
3568 struct input *input;
3569 struct wl_array types;
3570 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003571
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003572 struct task io_task;
3573 int fd;
3574 data_func_t func;
3575 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003576 uint32_t dnd_action;
3577 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003578 void *user_data;
3579};
3580
3581static void
3582data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3583{
3584 struct data_offer *offer = data;
3585 char **p;
3586
3587 p = wl_array_add(&offer->types, sizeof *p);
3588 *p = strdup(type);
3589}
3590
Carlos Garnacho9c931792016-01-18 23:52:12 +01003591static void
3592data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3593{
3594 struct data_offer *offer = data;
3595
3596 offer->source_actions = source_actions;
3597}
3598
3599static void
3600data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3601{
3602 struct data_offer *offer = data;
3603
3604 offer->dnd_action = dnd_action;
3605}
3606
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003607static const struct wl_data_offer_listener data_offer_listener = {
3608 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003609 data_offer_source_actions,
3610 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003611};
3612
3613static void
3614data_offer_destroy(struct data_offer *offer)
3615{
3616 char **p;
3617
3618 offer->refcount--;
3619 if (offer->refcount == 0) {
3620 wl_data_offer_destroy(offer->offer);
3621 for (p = offer->types.data; *p; p++)
3622 free(*p);
3623 wl_array_release(&offer->types);
3624 free(offer);
3625 }
3626}
3627
3628static void
3629data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003630 struct wl_data_device *data_device,
3631 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003632{
3633 struct data_offer *offer;
3634
Brian Lovinbc919262013-08-07 15:34:59 -07003635 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003636
3637 wl_array_init(&offer->types);
3638 offer->refcount = 1;
3639 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003640 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003641 wl_data_offer_add_listener(offer->offer,
3642 &data_offer_listener, offer);
3643}
3644
3645static void
3646data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003647 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003648 wl_fixed_t x_w, wl_fixed_t y_w,
3649 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003650{
3651 struct input *input = data;
3652 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003653 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003654 float x = wl_fixed_to_double(x_w);
3655 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003656 char **p;
3657
Jeffy Chen97749172019-09-02 10:26:09 +08003658 if (!surface) {
3659 /* enter event for a window we've just destroyed */
3660 return;
3661 }
3662
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003663 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003664 input->drag_enter_serial = serial;
3665 input->drag_focus = window,
3666 input->drag_x = x;
3667 input->drag_y = y;
3668
3669 if (!input->touch_grab)
3670 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003671
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003672 if (offer) {
3673 input->drag_offer = wl_data_offer_get_user_data(offer);
3674
3675 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3676 *p = NULL;
3677
3678 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003679
3680 if (input->display->data_device_manager_version >=
3681 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3682 wl_data_offer_set_actions(offer,
3683 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3684 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3685 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3686 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003687 } else {
3688 input->drag_offer = NULL;
3689 types_data = NULL;
3690 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003691
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003692 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003693 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003694 window->user_data);
3695}
3696
3697static void
3698data_device_leave(void *data, struct wl_data_device *data_device)
3699{
3700 struct input *input = data;
3701
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003702 if (input->drag_offer) {
3703 data_offer_destroy(input->drag_offer);
3704 input->drag_offer = NULL;
3705 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003706}
3707
3708static void
3709data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003710 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003711{
3712 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003713 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003714 float x = wl_fixed_to_double(x_w);
3715 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003716 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003717
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003718 input->drag_x = x;
3719 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003720
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003721 if (input->drag_offer)
3722 types_data = input->drag_offer->types.data;
3723 else
3724 types_data = NULL;
3725
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003726 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003727 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003728 window->user_data);
3729}
3730
3731static void
3732data_device_drop(void *data, struct wl_data_device *data_device)
3733{
3734 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003735 struct window *window = input->drag_focus;
3736 float x, y;
3737
3738 x = input->drag_x;
3739 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003740
3741 if (window->drop_handler)
3742 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003743 x, y, window->user_data);
3744
3745 if (input->touch_grab)
3746 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003747}
3748
3749static void
3750data_device_selection(void *data,
3751 struct wl_data_device *wl_data_device,
3752 struct wl_data_offer *offer)
3753{
3754 struct input *input = data;
3755 char **p;
3756
3757 if (input->selection_offer)
3758 data_offer_destroy(input->selection_offer);
3759
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003760 if (offer) {
3761 input->selection_offer = wl_data_offer_get_user_data(offer);
3762 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3763 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003764 } else {
3765 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003766 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003767}
3768
3769static const struct wl_data_device_listener data_device_listener = {
3770 data_device_data_offer,
3771 data_device_enter,
3772 data_device_leave,
3773 data_device_motion,
3774 data_device_drop,
3775 data_device_selection
3776};
3777
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003778static void
3779input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003780{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003781 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003782 struct wl_cursor *cursor;
3783 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003784
Daniel Stone80972742012-11-07 17:51:39 +11003785 if (!input->pointer)
3786 return;
3787
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003788 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003789 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003790 return;
3791
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003792 if (index >= (int) cursor->image_count) {
3793 fprintf(stderr, "cursor index out of range\n");
3794 return;
3795 }
3796
3797 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003798 buffer = wl_cursor_image_get_buffer(image);
3799 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003800 return;
3801
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003802 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3803 wl_surface_damage(input->pointer_surface, 0, 0,
3804 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003805 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003806 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3807 input->pointer_surface,
3808 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003809}
3810
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003811static const struct wl_callback_listener pointer_surface_listener;
3812
Derek Foreman493d9792015-03-04 16:26:25 -06003813static bool
3814input_set_pointer_special(struct input *input)
3815{
3816 if (input->current_cursor == CURSOR_BLANK) {
3817 wl_pointer_set_cursor(input->pointer,
3818 input->pointer_enter_serial,
3819 NULL, 0, 0);
3820 return true;
3821 }
3822
3823 if (input->current_cursor == CURSOR_UNSET)
3824 return true;
3825
3826 return false;
3827}
3828
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003829static void
Derek Foreman118a4292015-04-22 17:23:35 -05003830schedule_pointer_image_update(struct input *input,
3831 struct wl_cursor *cursor,
3832 uint32_t duration,
3833 bool force_frame)
3834{
3835 /* Some silly cursor sets have enormous pauses in them. In these
3836 * cases it's better to use a timer even if it results in less
3837 * accurate presentation, since it will save us having to set the
3838 * same cursor image over and over again.
3839 *
3840 * This is really not the way we're supposed to time any kind of
3841 * animation, but we're pretending it's OK here because we don't
3842 * want animated cursors with long delays to needlessly hog CPU.
3843 *
3844 * We use force_frame to ensure we don't accumulate large timing
3845 * errors by running off the wrong clock.
3846 */
3847 if (!force_frame && duration > 100) {
3848 struct timespec tp;
3849
3850 clock_gettime(CLOCK_MONOTONIC, &tp);
3851 input->cursor_timer_start = tp.tv_sec * 1000
3852 + tp.tv_nsec / 1000000;
3853 cursor_delay_timer_reset(input, duration);
3854 return;
3855 }
3856
3857 /* for short durations we'll just spin on frame callbacks for
3858 * accurate timing - the way any kind of timing sensitive animation
3859 * should really be done. */
3860 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3861 wl_callback_add_listener(input->cursor_frame_cb,
3862 &pointer_surface_listener, input);
3863
3864}
3865
3866static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003867pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3868 uint32_t time)
3869{
3870 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003871 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003872 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003873 uint32_t duration;
3874 bool force_frame = true;
3875
3876 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003877
3878 if (callback) {
3879 assert(callback == input->cursor_frame_cb);
3880 wl_callback_destroy(callback);
3881 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003882 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003883 }
3884
Daniel Stone80972742012-11-07 17:51:39 +11003885 if (!input->pointer)
3886 return;
3887
Derek Foreman493d9792015-03-04 16:26:25 -06003888 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003889 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003890
Daniel Stonea494f1d2012-06-18 19:31:12 +01003891 cursor = input->display->cursors[input->current_cursor];
3892 if (!cursor)
3893 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003894
3895 /* FIXME We don't have the current time on the first call so we set
3896 * the animation start to the time of the first frame callback. */
3897 if (time == 0)
3898 input->cursor_anim_start = 0;
3899 else if (input->cursor_anim_start == 0)
3900 input->cursor_anim_start = time;
3901
Derek Foreman118a4292015-04-22 17:23:35 -05003902 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003903
Derek Foreman118a4292015-04-22 17:23:35 -05003904 if (time == 0 || input->cursor_anim_start == 0) {
3905 duration = 0;
3906 i = 0;
3907 } else
3908 i = wl_cursor_frame_and_duration(
3909 cursor,
3910 time - input->cursor_anim_start,
3911 &duration);
3912
3913 if (cursor->image_count > 1)
3914 schedule_pointer_image_update(input, cursor, duration,
3915 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003916
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003917 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003918}
3919
Derek Foreman118a4292015-04-22 17:23:35 -05003920static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003921cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003922{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003923 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003924 struct timespec tp;
3925 struct wl_cursor *cursor;
3926 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003927
3928 if (!input->cursor_timer_running)
3929 return;
3930
Derek Foreman118a4292015-04-22 17:23:35 -05003931 cursor = input->display->cursors[input->current_cursor];
3932 if (!cursor)
3933 return;
3934
3935 clock_gettime(CLOCK_MONOTONIC, &tp);
3936 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3937 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3938}
3939
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003940static const struct wl_callback_listener pointer_surface_listener = {
3941 pointer_surface_frame_callback
3942};
3943
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003944void
3945input_set_pointer_image(struct input *input, int pointer)
3946{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003947 int force = 0;
3948
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003949 if (!input->pointer)
3950 return;
3951
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003952 if (input->pointer_enter_serial > input->cursor_serial)
3953 force = 1;
3954
3955 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003956 return;
3957
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003958 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003959 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003960 if (!input->cursor_frame_cb)
3961 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003962 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003963 /* The current frame callback may be stuck if, for instance,
3964 * the set cursor request was processed by the server after
3965 * this client lost the focus. In this case the cursor surface
3966 * might not be mapped and the frame callback wouldn't ever
3967 * complete. Send a set_cursor and attach to try to map the
3968 * cursor surface again so that the callback will finish */
3969 input_set_pointer_image_index(input, 0);
3970 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003971}
3972
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003973struct wl_data_device *
3974input_get_data_device(struct input *input)
3975{
3976 return input->data_device;
3977}
3978
3979void
3980input_set_selection(struct input *input,
3981 struct wl_data_source *source, uint32_t time)
3982{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003983 if (input->data_device)
3984 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003985}
3986
3987void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003988input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003989{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003990 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003991 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003992}
3993
3994static void
3995offer_io_func(struct task *task, uint32_t events)
3996{
3997 struct data_offer *offer =
3998 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003999 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004000 unsigned int len;
4001 char buffer[4096];
4002
4003 len = read(offer->fd, buffer, sizeof buffer);
4004 offer->func(buffer, len,
4005 offer->x, offer->y, offer->user_data);
4006
4007 if (len == 0) {
Harish Krupo43152a32019-04-19 22:06:44 +05304008 if ((offer != offer->input->selection_offer) &&
4009 (display->data_device_manager_version >=
4010 WL_DATA_OFFER_FINISH_SINCE_VERSION))
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01004011 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004012 close(offer->fd);
4013 data_offer_destroy(offer);
4014 }
4015}
4016
4017static void
4018data_offer_receive_data(struct data_offer *offer, const char *mime_type,
4019 data_func_t func, void *user_data)
4020{
4021 int p[2];
4022
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02004023 if (pipe2(p, O_CLOEXEC) == -1)
4024 return;
4025
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004026 wl_data_offer_receive(offer->offer, mime_type, p[1]);
4027 close(p[1]);
4028
4029 offer->io_task.run = offer_io_func;
4030 offer->fd = p[0];
4031 offer->func = func;
4032 offer->refcount++;
4033 offer->user_data = user_data;
4034
4035 display_watch_fd(offer->input->display,
4036 offer->fd, EPOLLIN, &offer->io_task);
4037}
4038
4039void
4040input_receive_drag_data(struct input *input, const char *mime_type,
4041 data_func_t func, void *data)
4042{
4043 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004044 input->drag_offer->x = input->drag_x;
4045 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004046}
4047
4048int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004049input_receive_drag_data_to_fd(struct input *input,
4050 const char *mime_type, int fd)
4051{
4052 if (input->drag_offer)
4053 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4054
4055 return 0;
4056}
4057
4058int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004059input_receive_selection_data(struct input *input, const char *mime_type,
4060 data_func_t func, void *data)
4061{
4062 char **p;
4063
4064 if (input->selection_offer == NULL)
4065 return -1;
4066
4067 for (p = input->selection_offer->types.data; *p; p++)
4068 if (strcmp(mime_type, *p) == 0)
4069 break;
4070
4071 if (*p == NULL)
4072 return -1;
4073
4074 data_offer_receive_data(input->selection_offer,
4075 mime_type, func, data);
4076 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004077}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004078
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004079int
4080input_receive_selection_data_to_fd(struct input *input,
4081 const char *mime_type, int fd)
4082{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004083 if (input->selection_offer)
4084 wl_data_offer_receive(input->selection_offer->offer,
4085 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004086
4087 return 0;
4088}
4089
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004090void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004091window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004092{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004093 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004094 return;
4095
ant8mef99fac22018-11-28 22:46:37 +01004096 xdg_toplevel_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004097}
4098
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004099static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004100surface_set_synchronized(struct surface *surface)
4101{
4102 if (!surface->subsurface)
4103 return;
4104
4105 if (surface->synchronized)
4106 return;
4107
4108 wl_subsurface_set_sync(surface->subsurface);
4109 surface->synchronized = 1;
4110}
4111
4112static void
4113surface_set_synchronized_default(struct surface *surface)
4114{
4115 if (!surface->subsurface)
4116 return;
4117
4118 if (surface->synchronized == surface->synchronized_default)
4119 return;
4120
4121 if (surface->synchronized_default)
4122 wl_subsurface_set_sync(surface->subsurface);
4123 else
4124 wl_subsurface_set_desync(surface->subsurface);
4125
4126 surface->synchronized = surface->synchronized_default;
4127}
4128
4129static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004130surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004131{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004132 struct widget *widget = surface->widget;
4133 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004134
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004135 if (surface->input_region) {
4136 wl_region_destroy(surface->input_region);
4137 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004138 }
4139
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004140 if (surface->opaque_region)
4141 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004142
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004143 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004144
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004145 if (widget->resize_handler)
4146 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004147 widget->allocation.width,
4148 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004149 widget->user_data);
4150
Pekka Paalanen35e82632013-04-25 13:57:48 +03004151 if (surface->subsurface &&
4152 (surface->allocation.x != widget->allocation.x ||
4153 surface->allocation.y != widget->allocation.y)) {
4154 wl_subsurface_set_position(surface->subsurface,
4155 widget->allocation.x,
4156 widget->allocation.y);
4157 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004158 if (surface->allocation.width != widget->allocation.width ||
4159 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004160 window_schedule_redraw(widget->window);
4161 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004162 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004163
4164 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004165 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004166 widget->allocation.width,
4167 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004168}
4169
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004170static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004171window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004172{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004173 struct surface *surface;
4174
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004175 widget_set_allocation(window->main_surface->widget,
4176 window->pending_allocation.x,
4177 window->pending_allocation.y,
4178 window->pending_allocation.width,
4179 window->pending_allocation.height);
4180
4181 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004182
4183 /* The main surface is in the list, too. Main surface's
4184 * resize_handler is responsible for calling widget_set_allocation()
4185 * on all sub-surface root widgets, so they will be resized
4186 * properly.
4187 */
4188 wl_list_for_each(surface, &window->subsurface_list, link) {
4189 if (surface == window->main_surface)
4190 continue;
4191
4192 surface_set_synchronized(surface);
4193 surface_resize(surface);
4194 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004195
4196 if (!window->fullscreen && !window->maximized)
4197 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004198
4199 if (window->confined && window->confined_widget) {
4200 struct wl_compositor *compositor = window->display->compositor;
4201 struct wl_region *region;
4202 struct widget *widget = window->confined_widget;
4203
4204 region = wl_compositor_create_region(compositor);
4205 wl_region_add(region,
4206 widget->allocation.x,
4207 widget->allocation.y,
4208 widget->allocation.width,
4209 widget->allocation.height);
4210 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4211 region);
4212 wl_region_destroy(region);
4213 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004214}
4215
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004216static void
4217idle_resize(struct window *window)
4218{
4219 window->resize_needed = 0;
4220 window->redraw_needed = 1;
4221
4222 DBG("from %dx%d to %dx%d\n",
4223 window->main_surface->server_allocation.width,
4224 window->main_surface->server_allocation.height,
4225 window->pending_allocation.width,
4226 window->pending_allocation.height);
4227
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004228 window_do_resize(window);
4229}
4230
4231static void
4232undo_resize(struct window *window)
4233{
4234 window->pending_allocation.width =
4235 window->main_surface->server_allocation.width;
4236 window->pending_allocation.height =
4237 window->main_surface->server_allocation.height;
4238
4239 DBG("back to %dx%d\n",
4240 window->main_surface->server_allocation.width,
4241 window->main_surface->server_allocation.height);
4242
4243 window_do_resize(window);
4244
4245 if (window->pending_allocation.width == 0 &&
4246 window->pending_allocation.height == 0) {
4247 fprintf(stderr, "Error: Could not draw a surface, "
4248 "most likely due to insufficient disk space in "
4249 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4250 exit(EXIT_FAILURE);
4251 }
4252}
4253
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004254void
4255window_schedule_resize(struct window *window, int width, int height)
4256{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004257 /* We should probably get these numbers from the theme. */
4258 const int min_width = 200, min_height = 200;
4259
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004260 window->pending_allocation.x = 0;
4261 window->pending_allocation.y = 0;
4262 window->pending_allocation.width = width;
4263 window->pending_allocation.height = height;
4264
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004265 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004266 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004267 window->min_allocation.width = min_width;
4268 else
4269 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004270 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004271 window->min_allocation.height = min_height;
4272 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004273 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004274 }
4275
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004276 if (window->pending_allocation.width < window->min_allocation.width)
4277 window->pending_allocation.width = window->min_allocation.width;
4278 if (window->pending_allocation.height < window->min_allocation.height)
4279 window->pending_allocation.height = window->min_allocation.height;
4280
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004281 window->resize_needed = 1;
4282 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004283}
4284
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004285void
4286widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4287{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004288 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004289}
4290
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004291static int
4292window_get_shadow_margin(struct window *window)
4293{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004294 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004295 return frame_get_shadow_margin(window->frame->frame);
4296 else
4297 return 0;
4298}
4299
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004300void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004301window_inhibit_redraw(struct window *window)
4302{
4303 window->redraw_inhibited = 1;
4304 wl_list_remove(&window->redraw_task.link);
4305 wl_list_init(&window->redraw_task.link);
4306 window->redraw_task_scheduled = 0;
4307}
4308
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004309void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004310window_uninhibit_redraw(struct window *window)
4311{
4312 window->redraw_inhibited = 0;
4313 if (window->redraw_needed || window->resize_needed)
4314 window_schedule_redraw_task(window);
4315}
4316
4317static void
4318xdg_surface_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01004319 struct xdg_surface *xdg_surface,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004320 uint32_t serial)
4321{
4322 struct window *window = data;
4323
ant8mef99fac22018-11-28 22:46:37 +01004324 xdg_surface_ack_configure(window->xdg_surface, serial);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004325
4326 if (window->state_changed_handler)
4327 window->state_changed_handler(window, window->user_data);
4328
4329 window_uninhibit_redraw(window);
4330}
4331
ant8mef99fac22018-11-28 22:46:37 +01004332static const struct xdg_surface_listener xdg_surface_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004333 xdg_surface_handle_configure
4334};
4335
4336static void
ant8mef99fac22018-11-28 22:46:37 +01004337xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004338 int32_t width, int32_t height,
4339 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004340{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004341 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004342 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004343
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004344 window->maximized = 0;
4345 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004346 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004347 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004348
4349 wl_array_for_each(p, states) {
4350 uint32_t state = *p;
4351 switch (state) {
ant8mef99fac22018-11-28 22:46:37 +01004352 case XDG_TOPLEVEL_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004353 window->maximized = 1;
4354 break;
ant8mef99fac22018-11-28 22:46:37 +01004355 case XDG_TOPLEVEL_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004356 window->fullscreen = 1;
4357 break;
ant8mef99fac22018-11-28 22:46:37 +01004358 case XDG_TOPLEVEL_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004359 window->resizing = 1;
4360 break;
ant8mef99fac22018-11-28 22:46:37 +01004361 case XDG_TOPLEVEL_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004362 window->focused = 1;
4363 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004364 default:
4365 /* Unknown state */
4366 break;
4367 }
4368 }
4369
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004370 if (window->frame) {
4371 if (window->maximized) {
4372 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4373 } else {
4374 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4375 }
4376
4377 if (window->focused) {
4378 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4379 } else {
4380 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4381 }
4382 }
4383
Jasper St. Pierref184c382014-05-06 08:33:27 -04004384 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004385 /* The width / height params are for window geometry,
4386 * but window_schedule_resize takes allocation. Add
4387 * on the shadow margin to get the difference. */
4388 int margin = window_get_shadow_margin(window);
4389
4390 window_schedule_resize(window,
4391 width + margin * 2,
4392 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004393 } else if (window->saved_allocation.width > 0 &&
4394 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004395 window_schedule_resize(window,
4396 window->saved_allocation.width,
4397 window->saved_allocation.height);
4398 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004399}
4400
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004401static void
ant8mef99fac22018-11-28 22:46:37 +01004402xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004403{
4404 struct window *window = data;
4405 window_close(window);
4406}
4407
ant8mef99fac22018-11-28 22:46:37 +01004408static const struct xdg_toplevel_listener xdg_toplevel_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004409 xdg_toplevel_handle_configure,
4410 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004411};
4412
4413static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004414window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004415{
ant8mef99fac22018-11-28 22:46:37 +01004416 struct xdg_toplevel *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004417
4418 if (!window->xdg_surface)
4419 return;
4420
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004421 if (window->parent == window->last_parent)
4422 return;
4423
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004424 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004425 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004426 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004427 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004428
ant8mef99fac22018-11-28 22:46:37 +01004429 xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004430 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004431}
4432
4433static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004434window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004435{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004436 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004437 frame_input_rect(window->frame->frame,
4438 &geometry->x,
4439 &geometry->y,
4440 &geometry->width,
4441 &geometry->height);
4442 else
4443 window_get_allocation(window, geometry);
4444}
4445
4446static void
4447window_sync_geometry(struct window *window)
4448{
4449 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004450
4451 if (!window->xdg_surface)
4452 return;
4453
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004454 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004455 if (geometry.x == window->last_geometry.x &&
4456 geometry.y == window->last_geometry.y &&
4457 geometry.width == window->last_geometry.width &&
4458 geometry.height == window->last_geometry.height)
4459 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004460
ant8mef99fac22018-11-28 22:46:37 +01004461 xdg_surface_set_window_geometry(window->xdg_surface,
4462 geometry.x,
4463 geometry.y,
4464 geometry.width,
4465 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004466 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004467}
4468
4469static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004470window_flush(struct window *window)
4471{
4472 struct surface *surface;
4473
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004474 assert(!window->redraw_inhibited);
4475
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004476 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004477 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004478 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004479 if (window->xdg_toplevel)
4480 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004481 }
4482
4483 wl_list_for_each(surface, &window->subsurface_list, link) {
4484 if (surface == window->main_surface)
4485 continue;
4486
4487 surface_flush(surface);
4488 }
4489
4490 surface_flush(window->main_surface);
4491}
4492
4493static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004494menu_destroy(struct menu *menu)
4495{
4496 widget_destroy(menu->widget);
4497 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004498 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004499 free(menu);
4500}
4501
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004502void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004503window_get_allocation(struct window *window,
4504 struct rectangle *allocation)
4505{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004506 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004507}
4508
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004509static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004510widget_redraw(struct widget *widget)
4511{
4512 struct widget *child;
4513
4514 if (widget->redraw_handler)
4515 widget->redraw_handler(widget, widget->user_data);
4516 wl_list_for_each(child, &widget->child_list, link)
4517 widget_redraw(child);
4518}
4519
4520static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004521frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4522{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004523 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004524
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004525 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004526 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004527 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004528 surface->frame_cb = NULL;
4529
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004530 surface->last_time = time;
4531
Pekka Paalanen71233882013-04-25 13:57:53 +03004532 if (surface->redraw_needed || surface->window->redraw_needed) {
4533 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004534 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004535 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004536}
4537
4538static const struct wl_callback_listener listener = {
4539 frame_callback
4540};
4541
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004542static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004543surface_redraw(struct surface *surface)
4544{
Pekka Paalanen71233882013-04-25 13:57:53 +03004545 DBG_OBJ(surface->surface, "begin\n");
4546
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004547 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004548 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004549
4550 /* Whole-window redraw forces a redraw even if the previous has
4551 * not yet hit the screen.
4552 */
4553 if (surface->frame_cb) {
4554 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004555 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004556
Pekka Paalanen71233882013-04-25 13:57:53 +03004557 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004558 wl_callback_destroy(surface->frame_cb);
4559 }
4560
Neil Roberts97b747c2013-12-19 16:17:12 +00004561 if (surface->widget->use_cairo &&
4562 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004563 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004564 return -1;
4565 }
4566
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004567 surface->frame_cb = wl_surface_frame(surface->surface);
4568 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004569 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004570
4571 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004572 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004573 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004574 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004575 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004576}
4577
4578static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004579idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004580{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004581 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004582 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004583 int failed = 0;
4584 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004585
Pekka Paalanen71233882013-04-25 13:57:53 +03004586 DBG(" --------- \n");
4587
Pekka Paalaneneebff542013-04-25 13:57:52 +03004588 wl_list_init(&window->redraw_task.link);
4589 window->redraw_task_scheduled = 0;
4590
4591 if (window->resize_needed) {
4592 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004593 if (window->main_surface->frame_cb) {
4594 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004595 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004596 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004597
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004598 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004599 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004600 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004601
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004602 if (surface_redraw(window->main_surface) < 0) {
4603 /*
4604 * Only main_surface failure will cause us to undo the resize.
4605 * If sub-surfaces fail, they will just be broken with old
4606 * content.
4607 */
4608 failed = 1;
4609 } else {
4610 wl_list_for_each(surface, &window->subsurface_list, link) {
4611 if (surface == window->main_surface)
4612 continue;
4613
4614 surface_redraw(surface);
4615 }
4616 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004617
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004618 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004619 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004620
4621 wl_list_for_each(surface, &window->subsurface_list, link)
4622 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004623
4624 if (resized && failed) {
4625 /* Restore widget tree to correspond to what is on screen. */
4626 undo_resize(window);
4627 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004628}
4629
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004630static void
4631window_schedule_redraw_task(struct window *window)
4632{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004633 if (window->redraw_inhibited)
4634 return;
4635
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004636 if (!window->redraw_task_scheduled) {
4637 window->redraw_task.run = idle_redraw;
4638 display_defer(window->display, &window->redraw_task);
4639 window->redraw_task_scheduled = 1;
4640 }
4641}
4642
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004643void
4644window_schedule_redraw(struct window *window)
4645{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004646 struct surface *surface;
4647
Pekka Paalanen71233882013-04-25 13:57:53 +03004648 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4649
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004650 wl_list_for_each(surface, &window->subsurface_list, link)
4651 surface->redraw_needed = 1;
4652
4653 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004654}
4655
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004656int
4657window_is_fullscreen(struct window *window)
4658{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004659 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004660}
4661
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004662void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004663window_set_fullscreen(struct window *window, int fullscreen)
4664{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004665 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004666 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004667
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004668 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004669 return;
4670
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004671 if (fullscreen)
ant8mef99fac22018-11-28 22:46:37 +01004672 xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004673 else
ant8mef99fac22018-11-28 22:46:37 +01004674 xdg_toplevel_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004675}
4676
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004677int
4678window_is_maximized(struct window *window)
4679{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004680 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004681}
4682
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004683void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004684window_set_maximized(struct window *window, int maximized)
4685{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004686 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004687 return;
4688
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004689 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004690 return;
4691
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004692 if (maximized)
ant8mef99fac22018-11-28 22:46:37 +01004693 xdg_toplevel_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004694 else
ant8mef99fac22018-11-28 22:46:37 +01004695 xdg_toplevel_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004696}
4697
Jasper St. Pierrede680992014-04-10 17:23:49 -07004698int
4699window_is_resizing(struct window *window)
4700{
4701 return window->resizing;
4702}
4703
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004704void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004705window_set_minimized(struct window *window)
4706{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004707 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004708 return;
4709
ant8mef99fac22018-11-28 22:46:37 +01004710 xdg_toplevel_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004711}
4712
4713void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004714window_set_user_data(struct window *window, void *data)
4715{
4716 window->user_data = data;
4717}
4718
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004719void *
4720window_get_user_data(struct window *window)
4721{
4722 return window->user_data;
4723}
4724
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004725void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004726window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004727 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004728{
4729 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004730}
4731
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004732void
4733window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004734 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004735{
4736 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004737}
4738
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004739void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004740window_set_data_handler(struct window *window, window_data_handler_t handler)
4741{
4742 window->data_handler = handler;
4743}
4744
4745void
4746window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4747{
4748 window->drop_handler = handler;
4749}
4750
4751void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004752window_set_close_handler(struct window *window,
4753 window_close_handler_t handler)
4754{
4755 window->close_handler = handler;
4756}
4757
4758void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004759window_set_fullscreen_handler(struct window *window,
4760 window_fullscreen_handler_t handler)
4761{
4762 window->fullscreen_handler = handler;
4763}
4764
4765void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004766window_set_output_handler(struct window *window,
4767 window_output_handler_t handler)
4768{
4769 window->output_handler = handler;
4770}
4771
4772void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004773window_set_state_changed_handler(struct window *window,
4774 window_state_changed_handler_t handler)
4775{
4776 window->state_changed_handler = handler;
4777}
4778
4779void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004780window_set_pointer_locked_handler(struct window *window,
4781 locked_pointer_locked_handler_t locked,
4782 locked_pointer_unlocked_handler_t unlocked)
4783{
4784 window->pointer_unlocked_handler = unlocked;
4785 window->pointer_locked_handler = locked;
4786}
4787
4788void
4789window_set_pointer_confined_handler(struct window *window,
4790 confined_pointer_confined_handler_t confined,
4791 confined_pointer_unconfined_handler_t unconfined)
4792{
4793 window->pointer_confined_handler = confined;
4794 window->pointer_unconfined_handler = unconfined;
4795}
4796
4797void
4798window_set_locked_pointer_motion_handler(struct window *window,
4799 window_locked_pointer_motion_handler_t handler)
4800{
4801 window->locked_pointer_motion_handler = handler;
4802}
4803
4804void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004805window_set_title(struct window *window, const char *title)
4806{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004807 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004808 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004809 if (window->frame) {
4810 frame_set_title(window->frame->frame, title);
4811 widget_schedule_redraw(window->frame->widget);
4812 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004813 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01004814 xdg_toplevel_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004815}
4816
4817const char *
4818window_get_title(struct window *window)
4819{
4820 return window->title;
4821}
4822
4823void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004824window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4825{
4826 struct text_cursor_position *text_cursor_position =
4827 window->display->text_cursor_position;
4828
Scott Moreau9295ce02012-06-01 12:46:10 -06004829 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004830 return;
4831
4832 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004833 window->main_surface->surface,
4834 wl_fixed_from_int(x),
4835 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004836}
4837
Casey Dahlin9074db52012-04-19 22:50:09 -04004838static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004839relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4840 uint32_t utime_hi,
4841 uint32_t utime_lo,
4842 wl_fixed_t dx,
4843 wl_fixed_t dy,
4844 wl_fixed_t dx_unaccel,
4845 wl_fixed_t dy_unaccel)
4846{
4847 struct input *input = data;
4848 struct window *window = input->pointer_focus;
4849 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4850
4851 if (window->locked_pointer_motion_handler &&
4852 window->pointer_locked) {
4853 window->locked_pointer_motion_handler(
4854 window, input, ms,
4855 wl_fixed_to_double(dx),
4856 wl_fixed_to_double(dy),
4857 window->user_data);
4858 }
4859}
4860
4861static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4862 relative_pointer_handle_motion,
4863};
4864
4865static void
4866locked_pointer_locked(void *data,
4867 struct zwp_locked_pointer_v1 *locked_pointer)
4868{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004869 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004870 struct window *window = input->locked_window;
4871
4872 if (!window)
4873 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004874
4875 window->pointer_locked = true;
4876
4877 if (window->pointer_locked_handler) {
4878 window->pointer_locked_handler(window,
4879 input,
4880 window->user_data);
4881 }
4882}
4883
4884static void
4885locked_pointer_unlocked(void *data,
4886 struct zwp_locked_pointer_v1 *locked_pointer)
4887{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004888 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004889 struct window *window = input->locked_window;
4890
4891 if (!window)
4892 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004893
4894 window_unlock_pointer(window);
4895
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004896 input->locked_window = NULL;
4897
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004898 if (window->pointer_unlocked_handler) {
4899 window->pointer_unlocked_handler(window,
4900 input,
4901 window->user_data);
4902 }
4903}
4904
4905static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4906 locked_pointer_locked,
4907 locked_pointer_unlocked,
4908};
4909
4910int
4911window_lock_pointer(struct window *window, struct input *input)
4912{
4913 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4914 window->display->relative_pointer_manager;
4915 struct zwp_pointer_constraints_v1 *pointer_constraints =
4916 window->display->pointer_constraints;
4917 struct zwp_relative_pointer_v1 *relative_pointer;
4918 struct zwp_locked_pointer_v1 *locked_pointer;
4919
4920 if (!window->display->relative_pointer_manager)
4921 return -1;
4922
4923 if (!window->display->pointer_constraints)
4924 return -1;
4925
4926 if (window->locked_pointer)
4927 return -1;
4928
4929 if (window->confined_pointer)
4930 return -1;
4931
4932 if (!input->pointer)
4933 return -1;
4934
4935 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4936 relative_pointer_manager, input->pointer);
4937 zwp_relative_pointer_v1_add_listener(relative_pointer,
4938 &relative_pointer_listener,
4939 input);
4940
4941 locked_pointer =
4942 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4943 window->main_surface->surface,
4944 input->pointer,
4945 NULL,
4946 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4947 zwp_locked_pointer_v1_add_listener(locked_pointer,
4948 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004949 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004950
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004951 window->locked_pointer = locked_pointer;
4952 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004953 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004954
4955 return 0;
4956}
4957
4958void
4959window_unlock_pointer(struct window *window)
4960{
4961 if (!window->locked_pointer)
4962 return;
4963
4964 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4965 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4966 window->locked_pointer = NULL;
4967 window->relative_pointer = NULL;
4968 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004969}
4970
4971void
4972widget_set_locked_pointer_cursor_hint(struct widget *widget,
4973 float x, float y)
4974{
4975 struct window *window = widget->window;
4976
4977 if (!window->locked_pointer)
4978 return;
4979
4980 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4981 wl_fixed_from_double(x),
4982 wl_fixed_from_double(y));
4983 wl_surface_commit(window->main_surface->surface);
4984}
4985
4986static void
4987confined_pointer_confined(void *data,
4988 struct zwp_confined_pointer_v1 *confined_pointer)
4989{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004990 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004991 struct window *window = input->confined_window;
4992
4993 if (!window)
4994 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004995
4996 window->confined = true;
4997
4998 if (window->pointer_confined_handler) {
4999 window->pointer_confined_handler(window,
5000 input,
5001 window->user_data);
5002 }
5003}
5004
5005static void
5006confined_pointer_unconfined(void *data,
5007 struct zwp_confined_pointer_v1 *confined_pointer)
5008{
Dima Ryazanov336ce672018-11-14 21:03:35 -08005009 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005010 struct window *window = input->confined_window;
5011
5012 if (!window)
5013 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005014
5015 window_unconfine_pointer(window);
5016
5017 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005018 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005019
5020 if (window->pointer_unconfined_handler) {
5021 window->pointer_unconfined_handler(window,
5022 input,
5023 window->user_data);
5024 }
5025}
5026
5027static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
5028 confined_pointer_confined,
5029 confined_pointer_unconfined,
5030};
5031
5032int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005033window_confine_pointer_to_rectangles(struct window *window,
5034 struct input *input,
5035 struct rectangle *rectangles,
5036 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005037{
5038 struct zwp_pointer_constraints_v1 *pointer_constraints =
5039 window->display->pointer_constraints;
5040 struct zwp_confined_pointer_v1 *confined_pointer;
5041 struct wl_compositor *compositor = window->display->compositor;
5042 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005043 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005044
5045 if (!window->display->pointer_constraints)
5046 return -1;
5047
5048 if (window->locked_pointer)
5049 return -1;
5050
5051 if (window->confined_pointer)
5052 return -1;
5053
5054 if (!input->pointer)
5055 return -1;
5056
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005057 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005058 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005059 for (i = 0; i < num_rectangles; i++) {
5060 wl_region_add(region,
5061 rectangles[i].x,
5062 rectangles[i].y,
5063 rectangles[i].width,
5064 rectangles[i].height);
5065 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005066 }
5067
5068 confined_pointer =
5069 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5070 window->main_surface->surface,
5071 input->pointer,
5072 region,
5073 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5074 if (region)
5075 wl_region_destroy(region);
5076
5077 zwp_confined_pointer_v1_add_listener(confined_pointer,
5078 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08005079 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005080
5081 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005082 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005083 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005084
5085 return 0;
5086}
5087
5088void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005089window_update_confine_rectangles(struct window *window,
5090 struct rectangle *rectangles,
5091 int num_rectangles)
5092{
5093 struct wl_compositor *compositor = window->display->compositor;
5094 struct wl_region *region;
5095 int i;
5096
5097 region = wl_compositor_create_region(compositor);
5098 for (i = 0; i < num_rectangles; i++) {
5099 wl_region_add(region,
5100 rectangles[i].x,
5101 rectangles[i].y,
5102 rectangles[i].width,
5103 rectangles[i].height);
5104 }
5105
5106 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5107
5108 wl_region_destroy(region);
5109}
5110
5111int
5112window_confine_pointer_to_widget(struct window *window,
5113 struct widget *widget,
5114 struct input *input)
5115{
5116 int ret;
5117
5118 if (widget) {
5119 ret = window_confine_pointer_to_rectangles(window,
5120 input,
5121 &widget->allocation,
5122 1);
5123 window->confined_widget = widget;
5124 return ret;
5125 } else {
5126 return window_confine_pointer_to_rectangles(window,
5127 input,
5128 NULL,
5129 0);
5130 }
5131}
5132
5133void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005134window_unconfine_pointer(struct window *window)
5135{
5136 if (!window->confined_pointer)
5137 return;
5138
5139 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5140 window->confined_pointer = NULL;
5141 window->confined = false;
5142}
5143
5144static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005145surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005146 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005147{
Rob Bradford7507b572012-05-15 17:55:34 +01005148 struct window *window = data;
5149 struct output *output;
5150 struct output *output_found = NULL;
5151 struct window_output *window_output;
5152
5153 wl_list_for_each(output, &window->display->output_list, link) {
5154 if (output->output == wl_output) {
5155 output_found = output;
5156 break;
5157 }
5158 }
5159
5160 if (!output_found)
5161 return;
5162
Brian Lovinbc919262013-08-07 15:34:59 -07005163 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005164 window_output->output = output_found;
5165
5166 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005167
5168 if (window->output_handler)
5169 window->output_handler(window, output_found, 1,
5170 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005171}
5172
5173static void
5174surface_leave(void *data,
5175 struct wl_surface *wl_surface, struct wl_output *output)
5176{
Rob Bradford7507b572012-05-15 17:55:34 +01005177 struct window *window = data;
5178 struct window_output *window_output;
5179 struct window_output *window_output_found = NULL;
5180
5181 wl_list_for_each(window_output, &window->window_output_list, link) {
5182 if (window_output->output->output == output) {
5183 window_output_found = window_output;
5184 break;
5185 }
5186 }
5187
5188 if (window_output_found) {
5189 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005190
5191 if (window->output_handler)
5192 window->output_handler(window, window_output->output,
5193 0, window->user_data);
5194
Rob Bradford7507b572012-05-15 17:55:34 +01005195 free(window_output_found);
5196 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005197}
5198
5199static const struct wl_surface_listener surface_listener = {
5200 surface_enter,
5201 surface_leave
5202};
5203
Pekka Paalanen4e373742013-02-13 16:17:13 +02005204static struct surface *
5205surface_create(struct window *window)
5206{
5207 struct display *display = window->display;
5208 struct surface *surface;
5209
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005210 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005211 surface->window = window;
5212 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005213 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005214 wl_surface_add_listener(surface->surface, &surface_listener, window);
5215
Pekka Paalanen35e82632013-04-25 13:57:48 +03005216 wl_list_insert(&window->subsurface_list, &surface->link);
Harish Krupo7bcbab12018-12-11 13:45:43 +05305217 surface->viewport = NULL;
Pekka Paalanen35e82632013-04-25 13:57:48 +03005218
Pekka Paalanen4e373742013-02-13 16:17:13 +02005219 return surface;
5220}
5221
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005222static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005223get_preferred_buffer_type(struct display *display)
5224{
5225#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005226 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005227 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5228#endif
5229
5230 return WINDOW_BUFFER_TYPE_SHM;
5231}
5232
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005233static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005234window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005235{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005236 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005237 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005238
Peter Huttererf3d62272013-08-08 11:57:05 +10005239 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005240 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005241 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005242
5243 surface = surface_create(window);
5244 window->main_surface = surface;
5245
Michael Teyfeld6371b52017-10-11 17:17:29 +02005246 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005247
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005248 window->custom = custom;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005249
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005250 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005251
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005252 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005253 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005254 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005255
Rob Bradford7507b572012-05-15 17:55:34 +01005256 wl_list_init (&window->window_output_list);
5257
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005258 return window;
5259}
5260
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005261struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005262window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005263{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005264 struct window *window;
5265
5266 window = window_create_internal(display, 0);
5267
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005268 if (window->display->xdg_shell) {
5269 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005270 xdg_wm_base_get_xdg_surface(window->display->xdg_shell,
5271 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005272 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005273
ant8mef99fac22018-11-28 22:46:37 +01005274 xdg_surface_add_listener(window->xdg_surface,
5275 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005276
5277 window->xdg_toplevel =
ant8mef99fac22018-11-28 22:46:37 +01005278 xdg_surface_get_toplevel(window->xdg_surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005279 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5280
ant8mef99fac22018-11-28 22:46:37 +01005281 xdg_toplevel_add_listener(window->xdg_toplevel,
5282 &xdg_toplevel_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005283
5284 window_inhibit_redraw(window);
5285
5286 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005287 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005288
5289 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005290}
5291
5292struct window *
5293window_create_custom(struct display *display)
5294{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005295 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005296}
5297
Jasper St. Pierre53686042013-12-09 15:26:25 -05005298void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005299window_set_parent(struct window *window,
5300 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005301{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005302 window->parent = parent_window;
5303 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005304}
5305
5306struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005307window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005308{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005309 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005310}
5311
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005312static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005313menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005314{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005315 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005316 int next;
5317
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005318 frame_interior(menu->frame, &x, &y, &width, &height);
5319 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005320 if (menu->current != next) {
5321 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005322 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005323 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005324}
5325
5326static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005327menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005328 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005329 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005330{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005331 struct menu *menu = data;
5332
5333 if (widget == menu->widget)
5334 menu_set_item(data, y);
5335
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005336 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005337}
5338
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005339static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005340menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005341 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005342{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005343 struct menu *menu = data;
5344
5345 if (widget == menu->widget)
5346 menu_set_item(data, y);
5347
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005348 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005349}
5350
5351static void
5352menu_leave_handler(struct widget *widget, struct input *input, void *data)
5353{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005354 struct menu *menu = data;
5355
5356 if (widget == menu->widget)
5357 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005358}
5359
5360static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005361menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005362 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005363 uint32_t button, enum wl_pointer_button_state state,
5364 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005365
5366{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005367 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005368
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005369 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5370 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005371 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005372 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005373 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005374 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005375 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005376 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005377 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005378 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005379}
5380
5381static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005382menu_touch_up_handler(struct widget *widget,
5383 struct input *input,
5384 uint32_t serial,
5385 uint32_t time,
5386 int32_t id,
5387 void *data)
5388{
5389 struct menu *menu = data;
5390
5391 input_ungrab(input);
5392 menu_destroy(menu);
5393}
5394
5395static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005396menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005397{
5398 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005399 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005400 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005401
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005402 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005403
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005404 frame_repaint(menu->frame, cr);
5405 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005406
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005407 theme_set_background_source(menu->window->display->theme,
5408 cr, THEME_FRAME_ACTIVE);
5409 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005410 cairo_fill(cr);
5411
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005412 cairo_select_font_face(cr, "sans",
5413 CAIRO_FONT_SLANT_NORMAL,
5414 CAIRO_FONT_WEIGHT_NORMAL);
5415 cairo_set_font_size(cr, 12);
5416
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005417 for (i = 0; i < menu->count; i++) {
5418 if (i == menu->current) {
5419 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005420 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005421 cairo_fill(cr);
5422 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005423 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005424 cairo_show_text(cr, menu->entries[i]);
5425 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005426 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5427 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005428 cairo_show_text(cr, menu->entries[i]);
5429 }
5430 }
5431
5432 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005433}
5434
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005435static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005436xdg_popup_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01005437 struct xdg_popup *xdg_popup,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005438 int32_t x,
5439 int32_t y,
5440 int32_t width,
5441 int32_t height)
5442{
5443}
5444
5445static void
ant8mef99fac22018-11-28 22:46:37 +01005446xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005447{
5448 struct window *window = data;
5449 struct menu *menu = window->main_surface->widget->user_data;
5450
5451 input_ungrab(menu->input);
5452 menu_destroy(menu);
5453}
5454
ant8mef99fac22018-11-28 22:46:37 +01005455static const struct xdg_popup_listener xdg_popup_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005456 xdg_popup_handle_configure,
5457 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005458};
5459
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005460static struct menu *
5461create_menu(struct display *display,
5462 struct input *input, uint32_t time,
5463 menu_func_t func, const char **entries, int count,
5464 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005465{
5466 struct window *window;
5467 struct menu *menu;
5468
5469 menu = malloc(sizeof *menu);
5470 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005471 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005472
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005473 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005474 if (!window) {
5475 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005476 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005477 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005478
5479 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005480 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005481 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005482 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005483 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005484 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005485 menu->entries = entries;
5486 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005487 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005488 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005489 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005490 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005491 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005492
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005493 input_ungrab(input);
5494
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005495 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005496 widget_set_enter_handler(menu->widget, menu_enter_handler);
5497 widget_set_leave_handler(menu->widget, menu_leave_handler);
5498 widget_set_motion_handler(menu->widget, menu_motion_handler);
5499 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005500 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005501
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005502 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005503 frame_resize_inside(menu->frame, 200, count * 20);
5504 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5505 window_schedule_resize(window, frame_width(menu->frame),
5506 frame_height(menu->frame));
5507
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005508 return menu;
5509}
5510
ant8mef99fac22018-11-28 22:46:37 +01005511static struct xdg_positioner *
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005512create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005513 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005514{
ant8mef99fac22018-11-28 22:46:37 +01005515 struct xdg_positioner *positioner;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005516
ant8mef99fac22018-11-28 22:46:37 +01005517 positioner = xdg_wm_base_create_positioner(display->xdg_shell);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005518 fail_on_null(positioner, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005519 xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1);
5520 xdg_positioner_set_size(positioner, w, h);
5521 xdg_positioner_set_anchor(positioner,
5522 XDG_POSITIONER_ANCHOR_TOP_LEFT);
5523 xdg_positioner_set_gravity(positioner,
5524 XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005525
5526 return positioner;
5527}
5528
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005529void
5530window_show_menu(struct display *display,
5531 struct input *input, uint32_t time, struct window *parent,
5532 int32_t x, int32_t y,
5533 menu_func_t func, const char **entries, int count)
5534{
5535 struct menu *menu;
5536 struct window *window;
5537 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005538 struct rectangle parent_geometry;
ant8mef99fac22018-11-28 22:46:37 +01005539 struct xdg_positioner *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005540
5541 menu = create_menu(display, input, time, func, entries, count, parent);
5542
5543 if (menu == NULL)
5544 return;
5545
5546 window = menu->window;
5547
5548 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5549 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5550
5551 window->x = x;
5552 window->y = y;
5553
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005554 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005555 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005556
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005557 if (!display->xdg_shell)
5558 return;
5559
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005560 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005561 xdg_wm_base_get_xdg_surface(display->xdg_shell,
5562 window->main_surface->surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005563 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005564
ant8mef99fac22018-11-28 22:46:37 +01005565 xdg_surface_add_listener(window->xdg_surface,
5566 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005567
5568 positioner = create_simple_positioner(display,
5569 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005570 window->y - (iy + parent_geometry.y),
5571 frame_width(menu->frame),
5572 frame_height(menu->frame));
ant8mef99fac22018-11-28 22:46:37 +01005573 window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
5574 parent->xdg_surface,
5575 positioner);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005576 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005577 xdg_positioner_destroy(positioner);
5578 xdg_popup_grab(window->xdg_popup, input->seat,
5579 display_get_serial(window->display));
5580 xdg_popup_add_listener(window->xdg_popup,
5581 &xdg_popup_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005582
5583 window_inhibit_redraw(window);
5584
5585 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005586}
5587
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005588void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005589window_set_buffer_type(struct window *window, enum window_buffer_type type)
5590{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005591 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005592}
5593
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005594enum window_buffer_type
5595window_get_buffer_type(struct window *window)
5596{
5597 return window->main_surface->buffer_type;
5598}
5599
Pekka Paalanen35e82632013-04-25 13:57:48 +03005600struct widget *
5601window_add_subsurface(struct window *window, void *data,
5602 enum subsurface_mode default_mode)
5603{
5604 struct widget *widget;
5605 struct surface *surface;
5606 struct wl_surface *parent;
5607 struct wl_subcompositor *subcompo = window->display->subcompositor;
5608
Pekka Paalanen35e82632013-04-25 13:57:48 +03005609 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005610 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005611 widget = widget_create(window, surface, data);
5612 wl_list_init(&widget->link);
5613 surface->widget = widget;
5614
5615 parent = window->main_surface->surface;
5616 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5617 surface->surface,
5618 parent);
5619 surface->synchronized = 1;
5620
5621 switch (default_mode) {
5622 case SUBSURFACE_SYNCHRONIZED:
5623 surface->synchronized_default = 1;
5624 break;
5625 case SUBSURFACE_DESYNCHRONIZED:
5626 surface->synchronized_default = 0;
5627 break;
5628 default:
5629 assert(!"bad enum subsurface_mode");
5630 }
5631
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005632 window->resize_needed = 1;
5633 window_schedule_redraw(window);
5634
Pekka Paalanen35e82632013-04-25 13:57:48 +03005635 return widget;
5636}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005637
5638static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005639display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005640 struct wl_output *wl_output,
5641 int x, int y,
5642 int physical_width,
5643 int physical_height,
5644 int subpixel,
5645 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005646 const char *model,
5647 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005648{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005649 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005650
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005651 output->allocation.x = x;
5652 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005653 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005654
5655 if (output->make)
5656 free(output->make);
5657 output->make = strdup(make);
5658
5659 if (output->model)
5660 free(output->model);
5661 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005662}
5663
5664static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005665display_handle_done(void *data,
5666 struct wl_output *wl_output)
5667{
5668}
5669
5670static void
5671display_handle_scale(void *data,
5672 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005673 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005674{
5675 struct output *output = data;
5676
5677 output->scale = scale;
5678}
5679
5680static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005681display_handle_mode(void *data,
5682 struct wl_output *wl_output,
5683 uint32_t flags,
5684 int width,
5685 int height,
5686 int refresh)
5687{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005688 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005689 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005690
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005691 if (flags & WL_OUTPUT_MODE_CURRENT) {
5692 output->allocation.width = width;
5693 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005694 if (display->output_configure_handler)
5695 (*display->output_configure_handler)(
5696 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005697 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005698}
5699
5700static const struct wl_output_listener output_listener = {
5701 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005702 display_handle_mode,
5703 display_handle_done,
5704 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005705};
5706
5707static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005708display_add_output(struct display *d, uint32_t id)
5709{
5710 struct output *output;
5711
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005712 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005713 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005714 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005715 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005716 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005717 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005718 wl_list_insert(d->output_list.prev, &output->link);
5719
5720 wl_output_add_listener(output->output, &output_listener, output);
5721}
5722
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005723static void
5724output_destroy(struct output *output)
5725{
5726 if (output->destroy_handler)
5727 (*output->destroy_handler)(output, output->user_data);
5728
5729 wl_output_destroy(output->output);
5730 wl_list_remove(&output->link);
5731 free(output);
5732}
5733
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005734static void
5735display_destroy_output(struct display *d, uint32_t id)
5736{
5737 struct output *output;
5738
5739 wl_list_for_each(output, &d->output_list, link) {
5740 if (output->server_output_id == id) {
5741 output_destroy(output);
5742 break;
5743 }
5744 }
5745}
5746
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005747void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005748display_set_global_handler(struct display *display,
5749 display_global_handler_t handler)
5750{
5751 struct global *global;
5752
5753 display->global_handler = handler;
5754 if (!handler)
5755 return;
5756
5757 wl_list_for_each(global, &display->global_list, link)
5758 display->global_handler(display,
5759 global->name, global->interface,
5760 global->version, display->user_data);
5761}
5762
5763void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005764display_set_global_handler_remove(struct display *display,
5765 display_global_handler_t remove_handler)
5766{
5767 display->global_handler_remove = remove_handler;
5768 if (!remove_handler)
5769 return;
5770}
5771
5772void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005773display_set_output_configure_handler(struct display *display,
5774 display_output_handler_t handler)
5775{
5776 struct output *output;
5777
5778 display->output_configure_handler = handler;
5779 if (!handler)
5780 return;
5781
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005782 wl_list_for_each(output, &display->output_list, link) {
5783 if (output->allocation.width == 0 &&
5784 output->allocation.height == 0)
5785 continue;
5786
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005787 (*display->output_configure_handler)(output,
5788 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005789 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005790}
5791
5792void
5793output_set_user_data(struct output *output, void *data)
5794{
5795 output->user_data = data;
5796}
5797
5798void *
5799output_get_user_data(struct output *output)
5800{
5801 return output->user_data;
5802}
5803
5804void
5805output_set_destroy_handler(struct output *output,
5806 display_output_handler_t handler)
5807{
5808 output->destroy_handler = handler;
5809 /* FIXME: implement this, once we have way to remove outputs */
5810}
5811
5812void
Scott Moreau4e072362012-09-29 02:03:11 -06005813output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005814{
Scott Moreau4e072362012-09-29 02:03:11 -06005815 struct rectangle allocation = output->allocation;
5816
5817 switch (output->transform) {
5818 case WL_OUTPUT_TRANSFORM_90:
5819 case WL_OUTPUT_TRANSFORM_270:
5820 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5821 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5822 /* Swap width and height */
5823 allocation.width = output->allocation.height;
5824 allocation.height = output->allocation.width;
5825 break;
5826 }
5827
5828 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005829}
5830
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005831struct wl_output *
5832output_get_wl_output(struct output *output)
5833{
5834 return output->output;
5835}
5836
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005837enum wl_output_transform
5838output_get_transform(struct output *output)
5839{
5840 return output->transform;
5841}
5842
Alexander Larssonafd319a2013-05-22 14:41:27 +02005843uint32_t
5844output_get_scale(struct output *output)
5845{
5846 return output->scale;
5847}
5848
Jason Ekstrand738715d2014-04-02 19:53:50 -05005849const char *
5850output_get_make(struct output *output)
5851{
5852 return output->make;
5853}
5854
5855const char *
5856output_get_model(struct output *output)
5857{
5858 return output->model;
5859}
5860
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005861static void
Daniel Stone97f68542012-05-30 16:32:01 +01005862fini_xkb(struct input *input)
5863{
5864 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005865 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005866}
5867
5868static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005869display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005870{
5871 struct input *input;
Sebastian Wickac1b92d2019-11-01 02:27:55 +01005872 int seat_version = MIN(display_seat_version, 7);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005873
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005874 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005875 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005876 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005877 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005878 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005879 input->pointer_focus = NULL;
5880 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005881 input->seat_version = seat_version;
5882
Rusty Lynch041815a2013-08-08 21:20:38 -07005883 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005884 wl_list_insert(d->input_list.prev, &input->link);
5885
Daniel Stone37816df2012-05-16 18:45:18 +01005886 wl_seat_add_listener(input->seat, &seat_listener, input);
5887 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005888
Jason Ekstranda669bd52014-04-02 19:53:51 -05005889 if (d->data_device_manager) {
5890 input->data_device =
5891 wl_data_device_manager_get_data_device(d->data_device_manager,
5892 input->seat);
5893 wl_data_device_add_listener(input->data_device,
5894 &data_device_listener,
5895 input);
5896 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005897
5898 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005899
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005900 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5901 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005902
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005903 set_repeat_info(input, 40, 400);
5904 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5905 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005906}
5907
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005908static void
Harish Krupoee4c7a22019-04-19 01:53:27 +05305909display_add_data_device(struct display *d, uint32_t id, int ddm_version)
5910{
5911 struct input *input;
5912
5913 d->data_device_manager_version = MIN(ddm_version, 3);
5914 d->data_device_manager =
5915 wl_registry_bind(d->registry, id,
5916 &wl_data_device_manager_interface,
5917 d->data_device_manager_version);
5918
5919 wl_list_for_each(input, &d->input_list, link) {
5920 if (!input->data_device) {
5921 input->data_device =
5922 wl_data_device_manager_get_data_device(d->data_device_manager,
5923 input->seat);
5924 wl_data_device_add_listener(input->data_device,
5925 &data_device_listener,
5926 input);
5927 }
5928 }
5929}
5930
5931static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005932input_destroy(struct input *input)
5933{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005934 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005935 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005936
5937 if (input->drag_offer)
5938 data_offer_destroy(input->drag_offer);
5939
5940 if (input->selection_offer)
5941 data_offer_destroy(input->selection_offer);
5942
kabeer khan6ce67ec2014-10-20 11:55:29 +05305943 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005944 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305945 wl_data_device_release(input->data_device);
5946 else
5947 wl_data_device_destroy(input->data_device);
5948 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005949 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005950 if (input->touch)
5951 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005952 if (input->pointer)
5953 wl_pointer_release(input->pointer);
5954 if (input->keyboard)
5955 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005956 } else {
5957 if (input->touch)
5958 wl_touch_destroy(input->touch);
5959 if (input->pointer)
5960 wl_pointer_destroy(input->pointer);
5961 if (input->keyboard)
5962 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005963 }
5964
Daniel Stone97f68542012-05-30 16:32:01 +01005965 fini_xkb(input);
5966
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005967 wl_surface_destroy(input->pointer_surface);
5968
Pekka Paalanene1207c72011-12-16 12:02:09 +02005969 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005970 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005971 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005972 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005973 free(input);
5974}
5975
5976static void
ant8mef99fac22018-11-28 22:46:37 +01005977xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005978{
ant8mef99fac22018-11-28 22:46:37 +01005979 xdg_wm_base_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005980}
5981
ant8mef99fac22018-11-28 22:46:37 +01005982static const struct xdg_wm_base_listener wm_base_listener = {
5983 xdg_wm_base_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005984};
5985
5986static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005987registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5988 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005989{
5990 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005991 struct global *global;
5992
Brian Lovinbc919262013-08-07 15:34:59 -07005993 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005994 global->name = id;
5995 global->interface = strdup(interface);
5996 global->version = version;
5997 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005998
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005999 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006000 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05006001 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006002 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006003 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01006004 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05006005 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08006006 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
6007 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
6008 d->relative_pointer_manager =
6009 wl_registry_bind(registry, id,
6010 &zwp_relative_pointer_manager_v1_interface,
6011 1);
6012 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
6013 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
6014 d->pointer_constraints =
6015 wl_registry_bind(registry, id,
6016 &zwp_pointer_constraints_v1_interface,
6017 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04006018 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006019 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006020 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Harish Krupoee4c7a22019-04-19 01:53:27 +05306021 display_add_data_device(d, id, version);
ant8mef99fac22018-11-28 22:46:37 +01006022 } else if (strcmp(interface, "xdg_wm_base") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006023 d->xdg_shell = wl_registry_bind(registry, id,
ant8mef99fac22018-11-28 22:46:37 +01006024 &xdg_wm_base_interface, 1);
6025 xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06006026 } else if (strcmp(interface, "text_cursor_position") == 0) {
6027 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006028 wl_registry_bind(registry, id,
6029 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006030 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6031 d->subcompositor =
6032 wl_registry_bind(registry, id,
6033 &wl_subcompositor_interface, 1);
Harish Krupo7bcbab12018-12-11 13:45:43 +05306034 } else if (!strcmp(interface, "wp_viewporter")) {
6035 d->viewporter =
6036 wl_registry_bind(registry, id,
6037 &wp_viewporter_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006038 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006039
6040 if (d->global_handler)
6041 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006042}
6043
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006044static void
6045registry_handle_global_remove(void *data, struct wl_registry *registry,
6046 uint32_t name)
6047{
6048 struct display *d = data;
6049 struct global *global;
6050 struct global *tmp;
6051
6052 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6053 if (global->name != name)
6054 continue;
6055
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006056 if (strcmp(global->interface, "wl_output") == 0)
6057 display_destroy_output(d, name);
6058
6059 /* XXX: Should destroy remaining bound globals */
6060
6061 if (d->global_handler_remove)
6062 d->global_handler_remove(d, name, global->interface,
6063 global->version, d->user_data);
6064
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006065 wl_list_remove(&global->link);
6066 free(global->interface);
6067 free(global);
6068 }
6069}
6070
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006071void *
6072display_bind(struct display *display, uint32_t name,
6073 const struct wl_interface *interface, uint32_t version)
6074{
6075 return wl_registry_bind(display->registry, name, interface, version);
6076}
6077
6078static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006079 registry_handle_global,
6080 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006081};
6082
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006083#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006084static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006085init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006086{
6087 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006088 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006089
Rob Clark6396ed32012-03-11 19:48:41 -05006090#ifdef USE_CAIRO_GLESV2
6091# define GL_BIT EGL_OPENGL_ES2_BIT
6092#else
6093# define GL_BIT EGL_OPENGL_BIT
6094#endif
6095
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006096 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006097 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006098 EGL_RED_SIZE, 1,
6099 EGL_GREEN_SIZE, 1,
6100 EGL_BLUE_SIZE, 1,
6101 EGL_ALPHA_SIZE, 1,
6102 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006103 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006104 EGL_NONE
6105 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006106
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006107#ifdef USE_CAIRO_GLESV2
6108 static const EGLint context_attribs[] = {
6109 EGL_CONTEXT_CLIENT_VERSION, 2,
6110 EGL_NONE
6111 };
6112 EGLint api = EGL_OPENGL_ES_API;
6113#else
6114 EGLint *context_attribs = NULL;
6115 EGLint api = EGL_OPENGL_API;
6116#endif
6117
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006118 d->dpy =
6119 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6120 d->display, NULL);
6121
Yuval Fledel45568f62010-12-06 09:18:12 -05006122 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006123 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006124 return -1;
6125 }
6126
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006127 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006128 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006129 return -1;
6130 }
6131
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006132 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6133 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006134 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006135 return -1;
6136 }
6137
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006138 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006139 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006140 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006141 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006142 return -1;
6143 }
6144
Benjamin Franzke0c991632011-09-27 21:57:31 +02006145 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6146 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006147 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006148 return -1;
6149 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006150
6151 return 0;
6152}
6153
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006154static void
6155fini_egl(struct display *display)
6156{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006157 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006158
6159 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6160 EGL_NO_CONTEXT);
6161
6162 eglTerminate(display->dpy);
6163 eglReleaseThread();
6164}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006165#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006166
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006167static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006168init_dummy_surface(struct display *display)
6169{
6170 int len;
6171 void *data;
6172
6173 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006174 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006175 display->dummy_surface =
6176 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6177 1, 1, len);
6178 display->dummy_surface_data = data;
6179}
6180
6181static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006182handle_display_data(struct task *task, uint32_t events)
6183{
6184 struct display *display =
6185 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006186 struct epoll_event ep;
6187 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006188
6189 display->display_fd_events = events;
6190
6191 if (events & EPOLLERR || events & EPOLLHUP) {
6192 display_exit(display);
6193 return;
6194 }
6195
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006196 if (events & EPOLLIN) {
6197 ret = wl_display_dispatch(display->display);
6198 if (ret == -1) {
6199 display_exit(display);
6200 return;
6201 }
6202 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006203
6204 if (events & EPOLLOUT) {
6205 ret = wl_display_flush(display->display);
6206 if (ret == 0) {
6207 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6208 ep.data.ptr = &display->display_task;
6209 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6210 display->display_fd, &ep);
6211 } else if (ret == -1 && errno != EAGAIN) {
6212 display_exit(display);
6213 return;
6214 }
6215 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006216}
6217
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006218static void
6219log_handler(const char *format, va_list args)
6220{
6221 vfprintf(stderr, format, args);
6222}
6223
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006224struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006225display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006226{
6227 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006228
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006229 wl_log_set_handler_client(log_handler);
6230
Peter Huttererf3d62272013-08-08 11:57:05 +10006231 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006232 if (d == NULL)
6233 return NULL;
6234
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006235 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006236 if (d->display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02006237 fprintf(stderr, "failed to connect to Wayland display: %s\n",
6238 strerror(errno));
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006239 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006240 return NULL;
6241 }
6242
Rob Bradford5ab9c752013-07-26 16:29:43 +01006243 d->xkb_context = xkb_context_new(0);
6244 if (d->xkb_context == NULL) {
6245 fprintf(stderr, "Failed to create XKB context\n");
6246 free(d);
6247 return NULL;
6248 }
6249
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006250 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006251 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006252 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006253 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6254 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006255
6256 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006257 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006258 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006259 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006260
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006261 d->registry = wl_display_get_registry(d->display);
6262 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006263
Marek Chalupaa519d062014-12-05 13:49:40 +01006264 if (wl_display_roundtrip(d->display) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006265 fprintf(stderr, "Failed to process Wayland connection: %s\n",
6266 strerror(errno));
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006267 return NULL;
6268 }
6269
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006270#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006271 if (init_egl(d) < 0)
6272 fprintf(stderr, "EGL does not seem to work, "
6273 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006274#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006275
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006276 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006277
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006278 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006279
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006280 wl_list_init(&d->window_list);
6281
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006282 init_dummy_surface(d);
6283
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006284 return d;
6285}
6286
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006287static void
6288display_destroy_outputs(struct display *display)
6289{
6290 struct output *tmp;
6291 struct output *output;
6292
6293 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6294 output_destroy(output);
6295}
6296
Pekka Paalanene1207c72011-12-16 12:02:09 +02006297static void
6298display_destroy_inputs(struct display *display)
6299{
6300 struct input *tmp;
6301 struct input *input;
6302
6303 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6304 input_destroy(input);
6305}
6306
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006307void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006308display_destroy(struct display *display)
6309{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006310 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006311 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6312 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006313
6314 if (!wl_list_empty(&display->deferred_list))
6315 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6316
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006317 cairo_surface_destroy(display->dummy_surface);
6318 free(display->dummy_surface_data);
6319
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006320 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006321 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006322
Daniel Stone97f68542012-05-30 16:32:01 +01006323 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006324
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006325 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006326 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006327
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006328#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006329 if (display->argb_device)
6330 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006331#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006332
Pekka Paalanen35e82632013-04-25 13:57:48 +03006333 if (display->subcompositor)
6334 wl_subcompositor_destroy(display->subcompositor);
6335
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006336 if (display->xdg_shell)
ant8mef99fac22018-11-28 22:46:37 +01006337 xdg_wm_base_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006338
6339 if (display->shm)
6340 wl_shm_destroy(display->shm);
6341
6342 if (display->data_device_manager)
6343 wl_data_device_manager_destroy(display->data_device_manager);
6344
6345 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006346 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006347
6348 close(display->epoll_fd);
6349
U. Artie Eoff44874d92012-10-02 21:12:35 -07006350 if (!(display->display_fd_events & EPOLLERR) &&
6351 !(display->display_fd_events & EPOLLHUP))
6352 wl_display_flush(display->display);
6353
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006354 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006355 free(display);
6356}
6357
6358void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006359display_set_user_data(struct display *display, void *data)
6360{
6361 display->user_data = data;
6362}
6363
6364void *
6365display_get_user_data(struct display *display)
6366{
6367 return display->user_data;
6368}
6369
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006370struct wl_display *
6371display_get_display(struct display *display)
6372{
6373 return display->display;
6374}
6375
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006376int
6377display_has_subcompositor(struct display *display)
6378{
6379 if (display->subcompositor)
6380 return 1;
6381
6382 wl_display_roundtrip(display->display);
6383
6384 return display->subcompositor != NULL;
6385}
6386
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006387cairo_device_t *
6388display_get_cairo_device(struct display *display)
6389{
6390 return display->argb_device;
6391}
6392
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006393struct output *
6394display_get_output(struct display *display)
6395{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006396 if (wl_list_empty(&display->output_list))
6397 return NULL;
6398
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006399 return container_of(display->output_list.next, struct output, link);
6400}
6401
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006402struct wl_compositor *
6403display_get_compositor(struct display *display)
6404{
6405 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006406}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006407
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006408uint32_t
6409display_get_serial(struct display *display)
6410{
6411 return display->serial;
6412}
6413
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006414EGLDisplay
6415display_get_egl_display(struct display *d)
6416{
6417 return d->dpy;
6418}
6419
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006420struct wl_data_source *
6421display_create_data_source(struct display *display)
6422{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006423 if (display->data_device_manager)
6424 return wl_data_device_manager_create_data_source(display->data_device_manager);
6425 else
6426 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006427}
6428
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006429EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006430display_get_argb_egl_config(struct display *d)
6431{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006432 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006433}
6434
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006435int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006436display_acquire_window_surface(struct display *display,
6437 struct window *window,
6438 EGLContext ctx)
6439{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006440 struct surface *surface = window->main_surface;
6441
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006442 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006443 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006444
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006445 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006446 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006447}
6448
6449void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006450display_release_window_surface(struct display *display,
6451 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006452{
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 Franzke0c991632011-09-27 21:57:31 +02006456 return;
6457
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006458 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006459}
6460
6461void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006462display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006463{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006464 wl_list_insert(&display->deferred_list, &task->link);
6465}
6466
6467void
6468display_watch_fd(struct display *display,
6469 int fd, uint32_t events, struct task *task)
6470{
6471 struct epoll_event ep;
6472
6473 ep.events = events;
6474 ep.data.ptr = task;
6475 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6476}
6477
6478void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006479display_unwatch_fd(struct display *display, int fd)
6480{
6481 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6482}
6483
6484void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006485display_run(struct display *display)
6486{
6487 struct task *task;
6488 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006489 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006490
Pekka Paalanen826d7952011-12-15 10:14:07 +02006491 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006492 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006493 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006494 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006495 struct task, link);
6496 wl_list_remove(&task->link);
6497 task->run(task, 0);
6498 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006499
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006500 wl_display_dispatch_pending(display->display);
6501
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006502 if (!display->running)
6503 break;
6504
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006505 ret = wl_display_flush(display->display);
6506 if (ret < 0 && errno == EAGAIN) {
6507 ep[0].events =
6508 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6509 ep[0].data.ptr = &display->display_task;
6510
6511 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6512 display->display_fd, &ep[0]);
6513 } else if (ret < 0) {
6514 break;
6515 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006516
6517 count = epoll_wait(display->epoll_fd,
6518 ep, ARRAY_LENGTH(ep), -1);
6519 for (i = 0; i < count; i++) {
6520 task = ep[i].data.ptr;
6521 task->run(task, ep[i].events);
6522 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006523 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006524}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006525
6526void
6527display_exit(struct display *display)
6528{
6529 display->running = 0;
6530}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006531
Carlos Garnacho9c931792016-01-18 23:52:12 +01006532int
6533display_get_data_device_manager_version(struct display *display)
6534{
6535 return display->data_device_manager_version;
6536}
6537
Jan Arne Petersencd997062012-11-18 19:06:44 +01006538void
6539keysym_modifiers_add(struct wl_array *modifiers_map,
6540 const char *name)
6541{
6542 size_t len = strlen(name) + 1;
6543 char *p;
6544
6545 p = wl_array_add(modifiers_map, len);
6546
6547 if (p == NULL)
6548 return;
6549
6550 strncpy(p, name, len);
6551}
6552
6553static xkb_mod_index_t
6554keysym_modifiers_get_index(struct wl_array *modifiers_map,
6555 const char *name)
6556{
6557 xkb_mod_index_t index = 0;
6558 char *p = modifiers_map->data;
6559
6560 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6561 if (strcmp(p, name) == 0)
6562 return index;
6563
6564 index++;
6565 p += strlen(p) + 1;
6566 }
6567
6568 return XKB_MOD_INVALID;
6569}
6570
6571xkb_mod_mask_t
6572keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6573 const char *name)
6574{
6575 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6576
6577 if (index == XKB_MOD_INVALID)
6578 return XKB_MOD_INVALID;
6579
6580 return 1 << index;
6581}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006582
6583static void
6584toytimer_fire(struct task *tsk, uint32_t events)
6585{
6586 uint64_t e;
6587 struct toytimer *tt;
6588
6589 tt = container_of(tsk, struct toytimer, tsk);
6590
6591 if (events != EPOLLIN)
6592 fprintf(stderr, "unexpected timerfd events %x\n", events);
6593
6594 if (!(events & EPOLLIN))
6595 return;
6596
6597 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6598 /* If we change the timer between the fd becoming
6599 * readable and getting here, there'll be nothing to
6600 * read and we get EAGAIN. */
6601 if (errno != EAGAIN)
Antonio Borneo39578632019-04-26 23:57:31 +02006602 fprintf(stderr, "timer read failed: %s\n",
6603 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006604 return;
6605 }
6606
6607 tt->callback(tt);
6608}
6609
6610void
6611toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6612 toytimer_cb callback)
6613{
6614 memset(tt, 0, sizeof *tt);
6615
6616 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6617 if (tt->fd == -1) {
Antonio Borneo39578632019-04-26 23:57:31 +02006618 fprintf(stderr, "creating timer failed: %s\n",
6619 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006620 abort();
6621 }
6622
6623 tt->display = display;
6624 tt->callback = callback;
6625 tt->tsk.run = toytimer_fire;
6626 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6627}
6628
6629void
6630toytimer_fini(struct toytimer *tt)
6631{
6632 display_unwatch_fd(tt->display, tt->fd);
6633 close(tt->fd);
6634 tt->fd = -1;
6635}
6636
6637void
6638toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6639{
6640 int ret;
6641
6642 ret = timerfd_settime(tt->fd, 0, its, NULL);
6643 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006644 fprintf(stderr, "timer setup failed: %s\n", strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006645 abort();
6646 }
6647}
6648
6649#define USEC_PER_SEC 1000000
6650
6651void
6652toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6653{
6654 struct itimerspec its;
6655
6656 its.it_interval.tv_sec = 0;
6657 its.it_interval.tv_nsec = 0;
6658 its.it_value.tv_sec = usec / USEC_PER_SEC;
6659 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6660 toytimer_arm(tt, &its);
6661}
6662
6663void
6664toytimer_disarm(struct toytimer *tt)
6665{
6666 struct itimerspec its = {};
6667
6668 toytimer_arm(tt, &its);
6669}