blob: 06a7432146a3ed31de0d982038999125b724cfa3 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040026
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080027#include <stdbool.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030031#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040035#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020037#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050038#include <time.h>
39#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040040#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040041#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030042#include <sys/timerfd.h>
Derek Foreman493d9792015-03-04 16:26:25 -060043#include <stdbool.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040044
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050046#include <wayland-egl.h>
47
Rob Clark6396ed32012-03-11 19:48:41 -050048#ifdef USE_CAIRO_GLESV2
49#include <GLES2/gl2.h>
50#include <GLES2/gl2ext.h>
51#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040052#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050053#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <EGL/egl.h>
55#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040056
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040057#include <cairo-gl.h>
Daniel Stone4b341a82017-02-16 21:52:23 +000058#elif !defined(ENABLE_EGL) /* platform.h defines these if EGL is enabled */
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030059typedef void *EGLDisplay;
60typedef void *EGLConfig;
61typedef void *EGLContext;
62#define EGL_NO_DISPLAY ((EGLDisplay)0)
63#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050064
Daniel Stone9d4f0302012-02-15 16:33:21 +000065#include <xkbcommon/xkbcommon.h>
Daniel Stone5b015962016-10-20 14:45:58 +010066#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -070067#include <xkbcommon/xkbcommon-compose.h>
Daniel Stone5b015962016-10-20 14:45:58 +010068#endif
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030069#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040070
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050071#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020072#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070073#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070074#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070075#include "shared/xalloc.h"
Pekka Paalanenecbdcfd2019-04-04 14:46:00 +030076#include <libweston/zalloc.h>
ant8mef99fac22018-11-28 22:46:37 +010077#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060078#include "text-cursor-position-client-protocol.h"
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080079#include "pointer-constraints-unstable-v1-client-protocol.h"
80#include "relative-pointer-unstable-v1-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070081#include "shared/os-compatibility.h"
emersionac71ee52018-11-18 21:42:10 +010082#include "shared/string-helpers.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050083
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050084#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050085
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080086#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
87#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
88
emersionac71ee52018-11-18 21:42:10 +010089#define DEFAULT_XCURSOR_SIZE 32
90
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070091struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030092
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040093struct global {
94 uint32_t name;
95 char *interface;
96 uint32_t version;
97 struct wl_list link;
98};
99
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500100struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500101 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400102 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500103 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300104 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400105 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400106 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600107 struct text_cursor_position *text_cursor_position;
ant8mef99fac22018-11-28 22:46:37 +0100108 struct xdg_wm_base *xdg_shell;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800109 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
110 struct zwp_pointer_constraints_v1 *pointer_constraints;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400111 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500112 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200113 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200114 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400115 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400116
117 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700118 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400119 struct task display_task;
120
121 int epoll_fd;
122 struct wl_list deferred_list;
123
Pekka Paalanen826d7952011-12-15 10:14:07 +0200124 int running;
125
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400126 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400127 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500129 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400130
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400131 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400132
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300133 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300134 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400135
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200136 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400137 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800138 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200139
140 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100141
142 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200143
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200144 /* A hack to get text extents for tooltips */
145 cairo_surface_t *dummy_surface;
146 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200147
148 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530149 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500150};
151
Rob Bradford7507b572012-05-15 17:55:34 +0100152struct window_output {
153 struct output *output;
154 struct wl_list link;
155};
156
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157struct toysurface {
158 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500159 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800160 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200162 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200163 * If flags has SURFACE_HINT_RESIZE set, the user is
164 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200165 * Returns the Cairo surface to draw to.
166 */
167 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200168 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200169 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200170
171 /*
172 * Post the surface to the server, returning the server allocation
173 * rectangle. The Cairo surface from prepare() must be destroyed
174 * after calling this.
175 */
176 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200177 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200178 struct rectangle *server_allocation);
179
180 /*
181 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800182 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200183 */
184 int (*acquire)(struct toysurface *base, EGLContext ctx);
185
186 /*
187 * Release the toysurface from the EGL context, returning control
188 * to Cairo.
189 */
190 void (*release)(struct toysurface *base);
191
192 /*
193 * Destroy the toysurface, including the Cairo surface, any
194 * backing storage, and the Wayland protocol objects.
195 */
196 void (*destroy)(struct toysurface *base);
197};
198
Pekka Paalanen4e373742013-02-13 16:17:13 +0200199struct surface {
200 struct window *window;
201
202 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300203 struct wl_subsurface *subsurface;
204 int synchronized;
205 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200206 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200207 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300208 int redraw_needed;
209 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300210 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200211
212 struct rectangle allocation;
213 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200214
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200215 struct wl_region *input_region;
216 struct wl_region *opaque_region;
217
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200218 enum window_buffer_type buffer_type;
219 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200220 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200221
222 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300223
224 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200225};
226
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500227struct window {
228 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100229 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500230 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200231 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400232 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500233 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200234 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500235 int x, y;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800236 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400237 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300238 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400239 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400240 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500241 int custom;
242 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400243
Pekka Paalanen99436862012-11-19 17:15:59 +0200244 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400245
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500246 int fullscreen;
247 int maximized;
248
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200249 enum preferred_format preferred_format;
250
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500251 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500252 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400253 window_data_handler_t data_handler;
254 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500255 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400256 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200257 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700258 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400259
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800260 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
261
Pekka Paalanen4e373742013-02-13 16:17:13 +0200262 struct surface *main_surface;
ant8mef99fac22018-11-28 22:46:37 +0100263 struct xdg_surface *xdg_surface;
264 struct xdg_toplevel *xdg_toplevel;
265 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300266
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700267 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800268 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500269
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500270 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500271
Pekka Paalanen35e82632013-04-25 13:57:48 +0300272 /* struct surface::link, contains also main_surface */
273 struct wl_list subsurface_list;
274
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800275 struct zwp_relative_pointer_v1 *relative_pointer;
276 struct zwp_locked_pointer_v1 *locked_pointer;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800277 bool pointer_locked;
278 locked_pointer_locked_handler_t pointer_locked_handler;
279 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
280 confined_pointer_confined_handler_t pointer_confined_handler;
281 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
282
283 struct zwp_confined_pointer_v1 *confined_pointer;
284 struct widget *confined_widget;
285 bool confined;
286
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500287 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400288 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500289};
290
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500291struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500292 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200293 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300294 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500295 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400296 struct wl_list link;
297 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500298 widget_resize_handler_t resize_handler;
299 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500300 widget_enter_handler_t enter_handler;
301 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500302 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500303 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 widget_touch_down_handler_t touch_down_handler;
305 widget_touch_up_handler_t touch_up_handler;
306 widget_touch_motion_handler_t touch_motion_handler;
307 widget_touch_frame_handler_t touch_frame_handler;
308 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200309 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000310 widget_pointer_frame_handler_t pointer_frame_handler;
311 widget_axis_source_handler_t axis_source_handler;
312 widget_axis_stop_handler_t axis_stop_handler;
313 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400314 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500315 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300316 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500317 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000318 /* If this is set to false then no cairo surface will be
319 * created before redrawing the surface. This is useful if the
320 * redraw handler is going to do completely custom rendering
321 * such as using EGL directly */
322 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400323};
324
Rusty Lynch041815a2013-08-08 21:20:38 -0700325struct touch_point {
326 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800327 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700328 struct widget *widget;
329 struct wl_list link;
330};
331
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400332struct input {
333 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100334 struct wl_seat *seat;
335 struct wl_pointer *pointer;
336 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700337 struct wl_touch *touch;
338 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400339 struct window *pointer_focus;
340 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700341 struct window *touch_focus;
Dima Ryazanov13bdf252018-11-14 22:17:42 -0800342 struct window *locked_window;
343 struct window *confined_window;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300344 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300345 uint32_t cursor_anim_start;
346 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500347 uint32_t cursor_timer_start;
348 uint32_t cursor_anim_current;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200349 struct toytimer cursor_timer;
Derek Foreman118a4292015-04-22 17:23:35 -0500350 bool cursor_timer_running;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300351 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400352 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400353 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400354 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400355 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400356 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400357
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500358 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500359 struct widget *grab;
360 uint32_t grab_button;
361
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400362 struct wl_data_device *data_device;
363 struct data_offer *drag_offer;
364 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800365 uint32_t touch_grab;
366 int32_t touch_grab_id;
367 float drag_x, drag_y;
368 struct window *drag_focus;
369 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100370
371 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100372 struct xkb_keymap *keymap;
373 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +0100374#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700375 struct xkb_compose_table *compose_table;
376 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +0100377#endif
Daniel Stone97f68542012-05-30 16:32:01 +0100378 xkb_mod_mask_t control_mask;
379 xkb_mod_mask_t alt_mask;
380 xkb_mod_mask_t shift_mask;
381 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400382
Jonny Lamb06959082014-08-12 14:58:27 +0200383 int32_t repeat_rate_sec;
384 int32_t repeat_rate_nsec;
385 int32_t repeat_delay_sec;
386 int32_t repeat_delay_nsec;
387
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200388 struct toytimer repeat_timer;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400389 uint32_t repeat_sym;
390 uint32_t repeat_key;
391 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500392 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400393};
394
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500395struct output {
396 struct display *display;
397 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800398 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500399 struct rectangle allocation;
400 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600401 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200402 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500403 char *make;
404 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200405
406 display_output_handler_t destroy_handler;
407 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500408};
409
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500410struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500411 struct widget *widget;
412 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500413 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800414
415 uint32_t last_time;
416 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800417 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500418};
419
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500420struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400421 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500422 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500423 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500424 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700425 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500426 const char **entries;
427 uint32_t time;
428 int current;
429 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400430 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500431 menu_func_t func;
432};
433
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300434struct tooltip {
435 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300436 struct widget *widget;
437 char *entry;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200438 struct toytimer timer;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300439 float x, y;
440};
441
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700442struct shm_pool {
443 struct wl_shm_pool *pool;
444 size_t size;
445 size_t used;
446 void *data;
447};
448
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400449enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300450 CURSOR_DEFAULT = 100,
451 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400452};
453
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200454static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400455
Pekka Paalanen97777442013-05-22 10:20:05 +0300456/* #define DEBUG */
457
458#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300459
460static void
461debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
462__attribute__ ((format (printf, 4, 5)));
463
464static void
465debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
466{
467 va_list ap;
468 struct timeval tv;
469
470 gettimeofday(&tv, NULL);
471 fprintf(stderr, "%8ld.%03ld ",
472 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
473
474 if (proxy)
475 fprintf(stderr, "%s@%d ",
476 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
477
478 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
479 fprintf(stderr, "%s ", func);
480
481 va_start(ap, fmt);
482 vfprintf(stderr, fmt, ap);
483 va_end(ap);
484}
485
486#define DBG(fmt, ...) \
487 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
488
489#define DBG_OBJ(obj, fmt, ...) \
490 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
491
492#else
493
494#define DBG(...) do {} while (0)
495#define DBG_OBJ(...) do {} while (0)
496
497#endif
498
Alexander Larsson1818e312013-05-22 14:41:31 +0200499static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200500surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200501{
502 int32_t tmp;
503
504 switch (buffer_transform) {
505 case WL_OUTPUT_TRANSFORM_90:
506 case WL_OUTPUT_TRANSFORM_270:
507 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
508 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
509 tmp = *width;
510 *width = *height;
511 *height = tmp;
512 break;
513 default:
514 break;
515 }
516
517 *width *= buffer_scale;
518 *height *= buffer_scale;
519}
520
521static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200522buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200523{
524 int32_t tmp;
525
526 switch (buffer_transform) {
527 case WL_OUTPUT_TRANSFORM_90:
528 case WL_OUTPUT_TRANSFORM_270:
529 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
530 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
531 tmp = *width;
532 *width = *height;
533 *height = tmp;
534 break;
535 default:
536 break;
537 }
538
539 *width /= buffer_scale;
540 *height /= buffer_scale;
541}
542
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500543#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200545struct egl_window_surface {
546 struct toysurface base;
547 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100548 struct display *display;
549 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200550 struct wl_egl_window *egl_window;
551 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100552};
553
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200554static struct egl_window_surface *
555to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100556{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200557 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558}
559
560static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200561egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200562 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200563 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100564{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200565 struct egl_window_surface *surface = to_egl_window_surface(base);
566
Alexander Larsson1818e312013-05-22 14:41:31 +0200567 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
568
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200569 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
570 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
571
572 return cairo_surface_reference(surface->cairo_surface);
573}
574
575static void
576egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200577 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200578 struct rectangle *server_allocation)
579{
580 struct egl_window_surface *surface = to_egl_window_surface(base);
581
582 cairo_gl_surface_swapbuffers(surface->cairo_surface);
583 wl_egl_window_get_attached_size(surface->egl_window,
584 &server_allocation->width,
585 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200586
587 buffer_to_surface_size (buffer_transform, buffer_scale,
588 &server_allocation->width,
589 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200590}
591
592static int
593egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
594{
595 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200596 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100597
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200598 device = cairo_surface_get_device(surface->cairo_surface);
599 if (!device)
600 return -1;
601
602 if (!ctx) {
603 if (device == surface->display->argb_device)
604 ctx = surface->display->argb_ctx;
605 else
606 assert(0);
607 }
608
609 cairo_device_flush(device);
610 cairo_device_acquire(device);
611 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
612 surface->egl_surface, ctx))
613 fprintf(stderr, "failed to make surface current\n");
614
615 return 0;
616}
617
618static void
619egl_window_surface_release(struct toysurface *base)
620{
621 struct egl_window_surface *surface = to_egl_window_surface(base);
622 cairo_device_t *device;
623
624 device = cairo_surface_get_device(surface->cairo_surface);
625 if (!device)
626 return;
627
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200628 if (!eglMakeCurrent(surface->display->dpy,
629 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200630 fprintf(stderr, "failed to make context current\n");
631
632 cairo_device_release(device);
633}
634
635static void
636egl_window_surface_destroy(struct toysurface *base)
637{
638 struct egl_window_surface *surface = to_egl_window_surface(base);
639 struct display *d = surface->display;
640
641 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000642 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200643 wl_egl_window_destroy(surface->egl_window);
644 surface->surface = NULL;
645
646 free(surface);
647}
648
649static struct toysurface *
650egl_window_surface_create(struct display *display,
651 struct wl_surface *wl_surface,
652 uint32_t flags,
653 struct rectangle *rectangle)
654{
655 struct egl_window_surface *surface;
656
Pekka Paalanenb3627362012-11-19 17:16:00 +0200657 if (display->dpy == EGL_NO_DISPLAY)
658 return NULL;
659
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800660 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200661 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100662 return NULL;
663
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200664 surface->base.prepare = egl_window_surface_prepare;
665 surface->base.swap = egl_window_surface_swap;
666 surface->base.acquire = egl_window_surface_acquire;
667 surface->base.release = egl_window_surface_release;
668 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100669
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200670 surface->display = display;
671 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400672
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200673 surface->egl_window = wl_egl_window_create(surface->surface,
674 rectangle->width,
675 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100676
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100677 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100678 weston_platform_create_egl_surface(display->dpy,
679 display->argb_config,
680 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100681
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200682 surface->cairo_surface =
683 cairo_gl_surface_create_for_egl(display->argb_device,
684 surface->egl_surface,
685 rectangle->width,
686 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100687
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200688 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100689}
690
Pekka Paalanenb3627362012-11-19 17:16:00 +0200691#else
692
693static struct toysurface *
694egl_window_surface_create(struct display *display,
695 struct wl_surface *wl_surface,
696 uint32_t flags,
697 struct rectangle *rectangle)
698{
699 return NULL;
700}
701
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702#endif
703
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200704struct shm_surface_data {
705 struct wl_buffer *buffer;
706 struct shm_pool *pool;
707};
708
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400709struct wl_buffer *
710display_get_buffer_for_surface(struct display *display,
711 cairo_surface_t *surface)
712{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200713 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400714
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200715 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400716
717 return data->buffer;
718}
719
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500720static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700721shm_pool_destroy(struct shm_pool *pool);
722
723static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400724shm_surface_data_destroy(void *p)
725{
726 struct shm_surface_data *data = p;
727
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200728 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700729 if (data->pool)
730 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300731
732 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400733}
734
Kristian Høgsberg16626282012-04-03 11:21:27 -0400735static struct wl_shm_pool *
736make_shm_pool(struct display *display, int size, void **data)
737{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400738 struct wl_shm_pool *pool;
739 int fd;
740
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300741 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400742 if (fd < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200743 fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
744 size, strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400745 return NULL;
746 }
747
748 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400749 if (*data == MAP_FAILED) {
Antonio Borneo39578632019-04-26 23:57:31 +0200750 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
Kristian Høgsberg16626282012-04-03 11:21:27 -0400751 close(fd);
752 return NULL;
753 }
754
755 pool = wl_shm_create_pool(display->shm, fd, size);
756
757 close(fd);
758
759 return pool;
760}
761
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700762static struct shm_pool *
763shm_pool_create(struct display *display, size_t size)
764{
765 struct shm_pool *pool = malloc(sizeof *pool);
766
767 if (!pool)
768 return NULL;
769
770 pool->pool = make_shm_pool(display, size, &pool->data);
771 if (!pool->pool) {
772 free(pool);
773 return NULL;
774 }
775
776 pool->size = size;
777 pool->used = 0;
778
779 return pool;
780}
781
782static void *
783shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
784{
785 if (pool->used + size > pool->size)
786 return NULL;
787
788 *offset = pool->used;
789 pool->used += size;
790
791 return (char *) pool->data + *offset;
792}
793
794/* destroy the pool. this does not unmap the memory though */
795static void
796shm_pool_destroy(struct shm_pool *pool)
797{
798 munmap(pool->data, pool->size);
799 wl_shm_pool_destroy(pool->pool);
800 free(pool);
801}
802
803/* Start allocating from the beginning of the pool again */
804static void
805shm_pool_reset(struct shm_pool *pool)
806{
807 pool->used = 0;
808}
809
810static int
811data_length_for_shm_surface(struct rectangle *rect)
812{
813 int stride;
814
815 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
816 rect->width);
817 return stride * rect->height;
818}
819
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500820static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700821display_create_shm_surface_from_pool(struct display *display,
822 struct rectangle *rectangle,
823 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824{
825 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400826 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400827 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200828 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700829 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400830 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831
832 data = malloc(sizeof *data);
833 if (data == NULL)
834 return NULL;
835
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200836 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
837 cairo_format = CAIRO_FORMAT_RGB16_565;
838 else
839 cairo_format = CAIRO_FORMAT_ARGB32;
840
841 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842 length = stride * rectangle->height;
843 data->pool = NULL;
844 map = shm_pool_allocate(pool, length, &offset);
845
846 if (!map) {
847 free(data);
848 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400849 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400851 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200852 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400853 rectangle->width,
854 rectangle->height,
855 stride);
856
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200857 cairo_surface_set_user_data(surface, &shm_surface_data_key,
858 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400859
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200860 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
861 format = WL_SHM_FORMAT_RGB565;
862 else {
863 if (flags & SURFACE_OPAQUE)
864 format = WL_SHM_FORMAT_XRGB8888;
865 else
866 format = WL_SHM_FORMAT_ARGB8888;
867 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400868
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200869 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
870 rectangle->width,
871 rectangle->height,
872 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400873
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700874 return surface;
875}
876
877static cairo_surface_t *
878display_create_shm_surface(struct display *display,
879 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200880 struct shm_pool *alternate_pool,
881 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700882{
883 struct shm_surface_data *data;
884 struct shm_pool *pool;
885 cairo_surface_t *surface;
886
Pekka Paalanen99436862012-11-19 17:15:59 +0200887 if (alternate_pool) {
888 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700889 surface = display_create_shm_surface_from_pool(display,
890 rectangle,
891 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200892 alternate_pool);
893 if (surface) {
894 data = cairo_surface_get_user_data(surface,
895 &shm_surface_data_key);
896 goto out;
897 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700898 }
899
900 pool = shm_pool_create(display,
901 data_length_for_shm_surface(rectangle));
902 if (!pool)
903 return NULL;
904
905 surface =
906 display_create_shm_surface_from_pool(display, rectangle,
907 flags, pool);
908
909 if (!surface) {
910 shm_pool_destroy(pool);
911 return NULL;
912 }
913
914 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200915 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700916 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400917
Pekka Paalanen99436862012-11-19 17:15:59 +0200918out:
919 if (data_ret)
920 *data_ret = data;
921
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400922 return surface;
923}
924
nobled7b87cb02011-02-01 18:51:47 +0000925static int
926check_size(struct rectangle *rect)
927{
928 if (rect->width && rect->height)
929 return 0;
930
931 fprintf(stderr, "tried to create surface of "
932 "width: %d, height: %d\n", rect->width, rect->height);
933 return -1;
934}
935
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400936cairo_surface_t *
937display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100938 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400939 struct rectangle *rectangle,
940 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400941{
nobled7b87cb02011-02-01 18:51:47 +0000942 if (check_size(rectangle) < 0)
943 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200944
945 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200946 return display_create_shm_surface(display, rectangle, flags,
947 NULL, NULL);
948}
949
Pekka Paalanena4eda732012-11-19 17:16:02 +0200950struct shm_surface_leaf {
951 cairo_surface_t *cairo_surface;
952 /* 'data' is automatically destroyed, when 'cairo_surface' is */
953 struct shm_surface_data *data;
954
955 struct shm_pool *resize_pool;
956 int busy;
957};
958
959static void
960shm_surface_leaf_release(struct shm_surface_leaf *leaf)
961{
962 if (leaf->cairo_surface)
963 cairo_surface_destroy(leaf->cairo_surface);
964 /* leaf->data already destroyed via cairo private */
965
966 if (leaf->resize_pool)
967 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200968
969 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200970}
971
Pekka Paalanenaef02542013-04-25 13:57:47 +0300972#define MAX_LEAVES 3
973
Pekka Paalanen99436862012-11-19 17:15:59 +0200974struct shm_surface {
975 struct toysurface base;
976 struct display *display;
977 struct wl_surface *surface;
978 uint32_t flags;
979 int dx, dy;
980
Pekka Paalanenaef02542013-04-25 13:57:47 +0300981 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200982 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200983};
984
985static struct shm_surface *
986to_shm_surface(struct toysurface *base)
987{
988 return container_of(base, struct shm_surface, base);
989}
990
Pekka Paalanena4eda732012-11-19 17:16:02 +0200991static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300992shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
993{
994#ifdef DEBUG
995 struct shm_surface_leaf *leaf;
996 char bufs[MAX_LEAVES + 1];
997 int i;
998
999 for (i = 0; i < MAX_LEAVES; i++) {
1000 leaf = &surface->leaf[i];
1001
1002 if (leaf->busy)
1003 bufs[i] = 'b';
1004 else if (leaf->cairo_surface)
1005 bufs[i] = 'a';
1006 else
1007 bufs[i] = ' ';
1008 }
1009
1010 bufs[MAX_LEAVES] = '\0';
1011 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1012#endif
1013}
1014
1015static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001016shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1017{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001018 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001019 struct shm_surface_leaf *leaf;
1020 int i;
1021 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001022
1023 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001024
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 for (i = 0; i < MAX_LEAVES; i++) {
1026 leaf = &surface->leaf[i];
1027 if (leaf->data && leaf->data->buffer == buffer) {
1028 leaf->busy = 0;
1029 break;
1030 }
1031 }
1032 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001033
Pekka Paalanenaef02542013-04-25 13:57:47 +03001034 /* Leave one free leaf with storage, release others */
1035 free_found = 0;
1036 for (i = 0; i < MAX_LEAVES; i++) {
1037 leaf = &surface->leaf[i];
1038
1039 if (!leaf->cairo_surface || leaf->busy)
1040 continue;
1041
1042 if (!free_found)
1043 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001044 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001045 shm_surface_leaf_release(leaf);
1046 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001047
Pekka Paalanen97777442013-05-22 10:20:05 +03001048 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049}
1050
1051static const struct wl_buffer_listener shm_surface_buffer_listener = {
1052 shm_surface_buffer_release
1053};
1054
Pekka Paalanen99436862012-11-19 17:15:59 +02001055static cairo_surface_t *
1056shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001057 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001058 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001059{
Pekka Paalanenec076692012-11-30 13:37:27 +02001060 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001061 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001062 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001063 struct shm_surface_leaf *leaf = NULL;
1064 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001065
1066 surface->dx = dx;
1067 surface->dy = dy;
1068
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001069 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001070 for (i = 0; i < MAX_LEAVES; i++) {
1071 if (surface->leaf[i].busy)
1072 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001073
Pekka Paalanenaef02542013-04-25 13:57:47 +03001074 if (!leaf || surface->leaf[i].cairo_surface)
1075 leaf = &surface->leaf[i];
1076 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001077 DBG_OBJ(surface->surface, "pick leaf %d\n",
1078 (int)(leaf - &surface->leaf[0]));
1079
Pekka Paalanenaef02542013-04-25 13:57:47 +03001080 if (!leaf) {
1081 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001082 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001083 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001085 }
1086
Pekka Paalanena4eda732012-11-19 17:16:02 +02001087 if (!resize_hint && leaf->resize_pool) {
1088 cairo_surface_destroy(leaf->cairo_surface);
1089 leaf->cairo_surface = NULL;
1090 shm_pool_destroy(leaf->resize_pool);
1091 leaf->resize_pool = NULL;
1092 }
1093
Alexander Larsson1818e312013-05-22 14:41:31 +02001094 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1095
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 if (leaf->cairo_surface &&
1097 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1098 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001099 goto out;
1100
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 if (leaf->cairo_surface)
1102 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001104#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001106 /* Create a big pool to allocate from, while continuously
1107 * resizing. Mmapping a new pool in the server
1108 * is relatively expensive, so reusing a pool performs
1109 * better, but may temporarily reserve unneeded memory.
1110 */
1111 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001112 leaf->resize_pool = shm_pool_create(surface->display,
1113 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001114 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001115#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001116
Alexander Larsson1818e312013-05-22 14:41:31 +02001117 rect.width = width;
1118 rect.height = height;
1119
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001121 display_create_shm_surface(surface->display, &rect,
1122 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001123 leaf->resize_pool,
1124 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001125 if (!leaf->cairo_surface)
1126 return NULL;
1127
Pekka Paalanena4eda732012-11-19 17:16:02 +02001128 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001129 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001130
1131out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001132 surface->current = leaf;
1133
1134 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001135}
1136
1137static void
1138shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001139 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001140 struct rectangle *server_allocation)
1141{
1142 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001143 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001144
1145 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001146 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001147 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001148 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001149
Alexander Larsson1818e312013-05-22 14:41:31 +02001150 buffer_to_surface_size (buffer_transform, buffer_scale,
1151 &server_allocation->width,
1152 &server_allocation->height);
1153
Pekka Paalanena4eda732012-11-19 17:16:02 +02001154 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001155 surface->dx, surface->dy);
1156 wl_surface_damage(surface->surface, 0, 0,
1157 server_allocation->width, server_allocation->height);
1158 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001159
Pekka Paalanen71233882013-04-25 13:57:53 +03001160 DBG_OBJ(surface->surface, "leaf %d busy\n",
1161 (int)(leaf - &surface->leaf[0]));
1162
Pekka Paalanena4eda732012-11-19 17:16:02 +02001163 leaf->busy = 1;
1164 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001165}
1166
1167static int
1168shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1169{
1170 return -1;
1171}
1172
1173static void
1174shm_surface_release(struct toysurface *base)
1175{
1176}
1177
1178static void
1179shm_surface_destroy(struct toysurface *base)
1180{
1181 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001182 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001183
Pekka Paalanenaef02542013-04-25 13:57:47 +03001184 for (i = 0; i < MAX_LEAVES; i++)
1185 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001186
1187 free(surface);
1188}
1189
1190static struct toysurface *
1191shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1192 uint32_t flags, struct rectangle *rectangle)
1193{
1194 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001195 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001196
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001197 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001198 surface->base.prepare = shm_surface_prepare;
1199 surface->base.swap = shm_surface_swap;
1200 surface->base.acquire = shm_surface_acquire;
1201 surface->base.release = shm_surface_release;
1202 surface->base.destroy = shm_surface_destroy;
1203
1204 surface->display = display;
1205 surface->surface = wl_surface;
1206 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001207
1208 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001209}
1210
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001211/*
1212 * The following correspondences between file names and cursors was copied
1213 * from: https://bugs.kde.org/attachment.cgi?id=67313
1214 */
1215
1216static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001217 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001218 "sw-resize",
1219 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001220};
1221
1222static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001223 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001224 "se-resize",
1225 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001226};
1227
1228static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001229 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001230 "s-resize",
1231 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001232};
1233
1234static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001235 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236 "closedhand",
1237 "208530c400c041818281048008011002"
1238};
1239
1240static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001241 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 "default",
1243 "top_left_arrow",
1244 "left-arrow"
1245};
1246
1247static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001248 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001249 "w-resize",
1250 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001251};
1252
1253static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001254 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001255 "e-resize",
1256 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257};
1258
1259static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001260 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001261 "nw-resize",
1262 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001263};
1264
1265static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001266 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001267 "ne-resize",
1268 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001269};
1270
1271static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001272 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001273 "n-resize",
1274 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001275};
1276
1277static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001278 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001279 "ibeam",
1280 "text"
1281};
1282
1283static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001284 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001285 "pointer",
1286 "pointing_hand",
1287 "e29285e634086352946a0e7090d73106"
1288};
1289
1290static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001291 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001292 "wait",
1293 "0426c94ea35c87780ff01dc239897213"
1294};
1295
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001296static const char *move_draggings[] = {
1297 "dnd-move"
1298};
1299
1300static const char *copy_draggings[] = {
1301 "dnd-copy"
1302};
1303
1304static const char *forbidden_draggings[] = {
1305 "dnd-none",
1306 "dnd-no-drop"
1307};
1308
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001309struct cursor_alternatives {
1310 const char **names;
1311 size_t count;
1312};
1313
1314static const struct cursor_alternatives cursors[] = {
1315 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1316 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1317 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1318 {grabbings, ARRAY_LENGTH(grabbings)},
1319 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1320 {left_sides, ARRAY_LENGTH(left_sides)},
1321 {right_sides, ARRAY_LENGTH(right_sides)},
1322 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1323 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1324 {top_sides, ARRAY_LENGTH(top_sides)},
1325 {xterms, ARRAY_LENGTH(xterms)},
1326 {hand1s, ARRAY_LENGTH(hand1s)},
1327 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001328 {move_draggings, ARRAY_LENGTH(move_draggings)},
1329 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1330 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001331};
1332
1333static void
1334create_cursors(struct display *display)
1335{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001336 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001337 struct weston_config *config;
1338 struct weston_config_section *s;
emersionac71ee52018-11-18 21:42:10 +01001339 int size = DEFAULT_XCURSOR_SIZE;
1340 char *theme = NULL, *size_str;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001341 unsigned int i, j;
1342 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001343
emersion807cd2e2018-10-10 09:46:12 +02001344 theme = getenv("XCURSOR_THEME");
emersionac71ee52018-11-18 21:42:10 +01001345
1346 size_str = getenv("XCURSOR_SIZE");
1347 if (size_str) {
1348 safe_strtoint(size_str, &size);
1349 if (size <= 0)
1350 size = DEFAULT_XCURSOR_SIZE;
1351 }
emersion807cd2e2018-10-10 09:46:12 +02001352
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001353 config_file = weston_config_get_name_from_env();
1354 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001355 s = weston_config_get_section(config, "shell", NULL, NULL);
emersion807cd2e2018-10-10 09:46:12 +02001356 weston_config_section_get_string(s, "cursor-theme", &theme, theme);
1357 weston_config_section_get_int(s, "cursor-size", &size, size);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001358 weston_config_destroy(config);
1359
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001360 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001361 if (!display->cursor_theme) {
1362 fprintf(stderr, "could not load theme '%s'\n", theme);
1363 return;
1364 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001365 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001366 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001367 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001368
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001369 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001370 cursor = NULL;
1371 for (j = 0; !cursor && j < cursors[i].count; ++j)
1372 cursor = wl_cursor_theme_get_cursor(
1373 display->cursor_theme, cursors[i].names[j]);
1374
1375 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001376 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001377 cursors[i].names[0]);
1378
1379 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001380 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001381}
1382
1383static void
1384destroy_cursors(struct display *display)
1385{
1386 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001387 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001388}
1389
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001390struct wl_cursor_image *
1391display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001392{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001393 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001394
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001395 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001396}
1397
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001398static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001399surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001400{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001401 if (!surface->cairo_surface)
1402 return;
1403
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001404 if (surface->opaque_region) {
1405 wl_surface_set_opaque_region(surface->surface,
1406 surface->opaque_region);
1407 wl_region_destroy(surface->opaque_region);
1408 surface->opaque_region = NULL;
1409 }
1410
1411 if (surface->input_region) {
1412 wl_surface_set_input_region(surface->surface,
1413 surface->input_region);
1414 wl_region_destroy(surface->input_region);
1415 surface->input_region = NULL;
1416 }
1417
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001419 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001420 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001421
Pekka Paalanen89dee002013-02-13 16:17:20 +02001422 cairo_surface_destroy(surface->cairo_surface);
1423 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001424}
1425
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001426int
1427window_has_focus(struct window *window)
1428{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001429 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001430}
1431
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001432static void
1433window_close(struct window *window)
1434{
1435 if (window->close_handler)
1436 window->close_handler(window->user_data);
1437 else
1438 display_exit(window->display);
1439}
1440
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001441struct display *
1442window_get_display(struct window *window)
1443{
1444 return window->display;
1445}
1446
Pekka Paalanen89dee002013-02-13 16:17:20 +02001447static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001448surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001449{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001450 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001451 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001452
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001453 if (!surface->toysurface && display->dpy &&
1454 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001455 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001457 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001458 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001459 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001460 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001461
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001462 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001463 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001464 surface->surface,
1465 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001466
Pekka Paalanen89dee002013-02-13 16:17:20 +02001467 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001468 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001469 allocation.width, allocation.height, flags,
1470 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001471}
1472
1473static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001474window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001475{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001476 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001477 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001478
Pekka Paalanenec076692012-11-30 13:37:27 +02001479 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001480 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001481
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001482 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1483 flags |= SURFACE_HINT_RGB565;
1484
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001485 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001486}
1487
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001488int
1489window_get_buffer_transform(struct window *window)
1490{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001491 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001492}
1493
1494void
1495window_set_buffer_transform(struct window *window,
1496 enum wl_output_transform transform)
1497{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001498 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001499 wl_surface_set_buffer_transform(window->main_surface->surface,
1500 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001501}
1502
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001503void
1504window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001505 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001506{
1507 window->main_surface->buffer_scale = scale;
1508 wl_surface_set_buffer_scale(window->main_surface->surface,
1509 scale);
1510}
1511
1512uint32_t
1513window_get_buffer_scale(struct window *window)
1514{
1515 return window->main_surface->buffer_scale;
1516}
1517
Alexander Larssond68f5232013-05-22 14:41:33 +02001518uint32_t
1519window_get_output_scale(struct window *window)
1520{
1521 struct window_output *window_output;
1522 struct window_output *window_output_tmp;
1523 int scale = 1;
1524
1525 wl_list_for_each_safe(window_output, window_output_tmp,
1526 &window->window_output_list, link) {
1527 if (window_output->output->scale > scale)
1528 scale = window_output->output->scale;
1529 }
1530
1531 return scale;
1532}
1533
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001534static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001535
Pekka Paalanen4e373742013-02-13 16:17:13 +02001536static void
1537surface_destroy(struct surface *surface)
1538{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001539 if (surface->frame_cb)
1540 wl_callback_destroy(surface->frame_cb);
1541
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001542 if (surface->input_region)
1543 wl_region_destroy(surface->input_region);
1544
1545 if (surface->opaque_region)
1546 wl_region_destroy(surface->opaque_region);
1547
Pekka Paalanen35e82632013-04-25 13:57:48 +03001548 if (surface->subsurface)
1549 wl_subsurface_destroy(surface->subsurface);
1550
Pekka Paalanen4e373742013-02-13 16:17:13 +02001551 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001552
1553 if (surface->toysurface)
1554 surface->toysurface->destroy(surface->toysurface);
1555
Pekka Paalanen35e82632013-04-25 13:57:48 +03001556 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001557 free(surface);
1558}
1559
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001560void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001561window_destroy(struct window *window)
1562{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001563 struct display *display = window->display;
1564 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001565 struct window_output *window_output;
1566 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001567
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001568 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001569
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001570 wl_list_for_each(input, &display->input_list, link) {
Jeffy Chen179458a2019-09-02 10:15:26 +08001571 if (input->touch_focus == window) {
1572 struct touch_point *tp, *tmp;
1573
1574 wl_list_for_each_safe(tp, tmp,
1575 &input->touch_point_list,
1576 link) {
1577 wl_list_remove(&tp->link);
1578 free(tp);
1579 }
1580
Rusty Lynch1084da52013-08-15 09:10:08 -07001581 input->touch_focus = NULL;
Jeffy Chen179458a2019-09-02 10:15:26 +08001582 }
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001583 if (input->pointer_focus == window)
1584 input->pointer_focus = NULL;
1585 if (input->keyboard_focus == window)
1586 input->keyboard_focus = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08001587 if (input->locked_window == window)
1588 input->locked_window = NULL;
1589 if (input->confined_window == window)
1590 input->confined_window = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001591 if (input->focus_widget &&
1592 input->focus_widget->window == window)
1593 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001594 }
1595
Rob Bradford7507b572012-05-15 17:55:34 +01001596 wl_list_for_each_safe(window_output, window_output_tmp,
1597 &window->window_output_list, link) {
1598 free (window_output);
1599 }
1600
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001601 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001602 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001603
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001604 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01001605 xdg_toplevel_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001606 if (window->xdg_popup)
ant8mef99fac22018-11-28 22:46:37 +01001607 xdg_popup_destroy(window->xdg_popup);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001608 if (window->xdg_surface)
ant8mef99fac22018-11-28 22:46:37 +01001609 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001610
1611 surface_destroy(window->main_surface);
1612
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001613 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001614
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001615 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001616 free(window);
1617}
1618
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001619static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001620widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001621{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001622 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001623
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001624 wl_list_for_each(child, &widget->child_list, link) {
1625 target = widget_find_widget(child, x, y);
1626 if (target)
1627 return target;
1628 }
1629
1630 if (widget->allocation.x <= x &&
1631 x < widget->allocation.x + widget->allocation.width &&
1632 widget->allocation.y <= y &&
1633 y < widget->allocation.y + widget->allocation.height) {
1634 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001635 }
1636
1637 return NULL;
1638}
1639
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001640static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001641window_find_widget(struct window *window, int32_t x, int32_t y)
1642{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001643 struct surface *surface;
1644 struct widget *widget;
1645
1646 wl_list_for_each(surface, &window->subsurface_list, link) {
1647 widget = widget_find_widget(surface->widget, x, y);
1648 if (widget)
1649 return widget;
1650 }
1651
1652 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001653}
1654
1655static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001656widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001657{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001658 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001659
Peter Huttererf3d62272013-08-08 11:57:05 +10001660 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001661 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001662 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001663 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001664 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001665 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001666 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001667 widget->tooltip = NULL;
1668 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001669 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001670 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001671
1672 return widget;
1673}
1674
1675struct widget *
1676window_add_widget(struct window *window, void *data)
1677{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001678 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001679
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001680 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001681 wl_list_init(&widget->link);
1682 window->main_surface->widget = widget;
1683
1684 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001685}
1686
1687struct widget *
1688widget_add_widget(struct widget *parent, void *data)
1689{
1690 struct widget *widget;
1691
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001692 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001693 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001695 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001696}
1697
1698void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001699widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001700{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001701 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001702 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001703 struct input *input;
1704
Pekka Paalanen35e82632013-04-25 13:57:48 +03001705 /* Destroy the sub-surface along with the root widget */
1706 if (surface->widget == widget && surface->subsurface)
1707 surface_destroy(widget->surface);
1708
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001709 if (widget->tooltip)
1710 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001711
Pekka Paalanene156fb62012-01-19 13:51:38 +02001712 wl_list_for_each(input, &display->input_list, link) {
1713 if (input->focus_widget == widget)
1714 input->focus_widget = NULL;
1715 }
1716
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001717 wl_list_remove(&widget->link);
1718 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001719}
1720
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001721void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001722widget_set_default_cursor(struct widget *widget, int cursor)
1723{
1724 widget->default_cursor = cursor;
1725}
1726
1727void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001728widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001729{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001730 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001731}
1732
1733void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001734widget_set_size(struct widget *widget, int32_t width, int32_t height)
1735{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001736 widget->allocation.width = width;
1737 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001738}
1739
1740void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001741widget_set_allocation(struct widget *widget,
1742 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001743{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001744 widget->allocation.x = x;
1745 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001746 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747}
1748
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001749void
1750widget_set_transparent(struct widget *widget, int transparent)
1751{
1752 widget->opaque = !transparent;
1753}
1754
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001755void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001756widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001757{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001758 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001759}
1760
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001761static cairo_surface_t *
1762widget_get_cairo_surface(struct widget *widget)
1763{
1764 struct surface *surface = widget->surface;
1765 struct window *window = widget->window;
1766
Neil Roberts97b747c2013-12-19 16:17:12 +00001767 assert(widget->use_cairo);
1768
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001769 if (!surface->cairo_surface) {
1770 if (surface == window->main_surface)
1771 window_create_main_surface(window);
1772 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001773 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001774 }
1775
1776 return surface->cairo_surface;
1777}
1778
Alexander Larsson15901f02013-05-22 14:41:25 +02001779static void
1780widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1781{
1782 struct surface *surface = widget->surface;
1783 double angle;
1784 cairo_matrix_t m;
1785 enum wl_output_transform transform;
1786 int surface_width, surface_height;
1787 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001788 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001789
1790 surface_width = surface->allocation.width;
1791 surface_height = surface->allocation.height;
1792
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001793 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001794 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001795
Alexander Larsson15901f02013-05-22 14:41:25 +02001796 switch (transform) {
1797 case WL_OUTPUT_TRANSFORM_FLIPPED:
1798 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1799 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1800 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1801 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1802 break;
1803 default:
1804 cairo_matrix_init_identity(&m);
1805 break;
1806 }
1807
1808 switch (transform) {
1809 case WL_OUTPUT_TRANSFORM_NORMAL:
1810 default:
1811 angle = 0;
1812 translate_x = 0;
1813 translate_y = 0;
1814 break;
1815 case WL_OUTPUT_TRANSFORM_FLIPPED:
1816 angle = 0;
1817 translate_x = surface_width;
1818 translate_y = 0;
1819 break;
1820 case WL_OUTPUT_TRANSFORM_90:
1821 angle = M_PI_2;
1822 translate_x = surface_height;
1823 translate_y = 0;
1824 break;
1825 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1826 angle = M_PI_2;
1827 translate_x = surface_height;
1828 translate_y = surface_width;
1829 break;
1830 case WL_OUTPUT_TRANSFORM_180:
1831 angle = M_PI;
1832 translate_x = surface_width;
1833 translate_y = surface_height;
1834 break;
1835 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1836 angle = M_PI;
1837 translate_x = 0;
1838 translate_y = surface_height;
1839 break;
1840 case WL_OUTPUT_TRANSFORM_270:
1841 angle = M_PI + M_PI_2;
1842 translate_x = 0;
1843 translate_y = surface_width;
1844 break;
1845 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1846 angle = M_PI + M_PI_2;
1847 translate_x = 0;
1848 translate_y = 0;
1849 break;
1850 }
1851
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001852 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001853 cairo_translate(cr, translate_x, translate_y);
1854 cairo_rotate(cr, angle);
1855 cairo_transform(cr, &m);
1856}
1857
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001858cairo_t *
1859widget_cairo_create(struct widget *widget)
1860{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001861 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001862 cairo_surface_t *cairo_surface;
1863 cairo_t *cr;
1864
1865 cairo_surface = widget_get_cairo_surface(widget);
1866 cr = cairo_create(cairo_surface);
1867
Alexander Larsson15901f02013-05-22 14:41:25 +02001868 widget_cairo_update_transform(widget, cr);
1869
Pekka Paalanen35e82632013-04-25 13:57:48 +03001870 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1871
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001872 return cr;
1873}
1874
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001875struct wl_surface *
1876widget_get_wl_surface(struct widget *widget)
1877{
1878 return widget->surface->surface;
1879}
1880
Neil Roberts5e10a042013-09-09 00:40:17 +01001881struct wl_subsurface *
1882widget_get_wl_subsurface(struct widget *widget)
1883{
1884 return widget->surface->subsurface;
1885}
1886
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001887uint32_t
1888widget_get_last_time(struct widget *widget)
1889{
1890 return widget->surface->last_time;
1891}
1892
1893void
1894widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1895{
1896 struct wl_compositor *comp = widget->window->display->compositor;
1897 struct surface *surface = widget->surface;
1898
1899 if (!surface->input_region)
1900 surface->input_region = wl_compositor_create_region(comp);
1901
1902 if (rect) {
1903 wl_region_add(surface->input_region,
1904 rect->x, rect->y, rect->width, rect->height);
1905 }
1906}
1907
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001908void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001909widget_set_resize_handler(struct widget *widget,
1910 widget_resize_handler_t handler)
1911{
1912 widget->resize_handler = handler;
1913}
1914
1915void
1916widget_set_redraw_handler(struct widget *widget,
1917 widget_redraw_handler_t handler)
1918{
1919 widget->redraw_handler = handler;
1920}
1921
1922void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001923widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001924{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001925 widget->enter_handler = handler;
1926}
1927
1928void
1929widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1930{
1931 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001932}
1933
1934void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001935widget_set_motion_handler(struct widget *widget,
1936 widget_motion_handler_t handler)
1937{
1938 widget->motion_handler = handler;
1939}
1940
1941void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001942widget_set_button_handler(struct widget *widget,
1943 widget_button_handler_t handler)
1944{
1945 widget->button_handler = handler;
1946}
1947
1948void
Rusty Lynch041815a2013-08-08 21:20:38 -07001949widget_set_touch_up_handler(struct widget *widget,
1950 widget_touch_up_handler_t handler)
1951{
1952 widget->touch_up_handler = handler;
1953}
1954
1955void
1956widget_set_touch_down_handler(struct widget *widget,
1957 widget_touch_down_handler_t handler)
1958{
1959 widget->touch_down_handler = handler;
1960}
1961
1962void
1963widget_set_touch_motion_handler(struct widget *widget,
1964 widget_touch_motion_handler_t handler)
1965{
1966 widget->touch_motion_handler = handler;
1967}
1968
1969void
1970widget_set_touch_frame_handler(struct widget *widget,
1971 widget_touch_frame_handler_t handler)
1972{
1973 widget->touch_frame_handler = handler;
1974}
1975
1976void
1977widget_set_touch_cancel_handler(struct widget *widget,
1978 widget_touch_cancel_handler_t handler)
1979{
1980 widget->touch_cancel_handler = handler;
1981}
1982
1983void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001984widget_set_axis_handler(struct widget *widget,
1985 widget_axis_handler_t handler)
1986{
1987 widget->axis_handler = handler;
1988}
1989
Peter Hutterer87743e92016-01-18 16:38:22 +10001990void
1991widget_set_pointer_frame_handler(struct widget *widget,
1992 widget_pointer_frame_handler_t handler)
1993{
1994 widget->pointer_frame_handler = handler;
1995}
1996
1997void
1998widget_set_axis_handlers(struct widget *widget,
1999 widget_axis_handler_t axis_handler,
2000 widget_axis_source_handler_t axis_source_handler,
2001 widget_axis_stop_handler_t axis_stop_handler,
2002 widget_axis_discrete_handler_t axis_discrete_handler)
2003{
2004 widget->axis_handler = axis_handler;
2005 widget->axis_source_handler = axis_source_handler;
2006 widget->axis_stop_handler = axis_stop_handler;
2007 widget->axis_discrete_handler = axis_discrete_handler;
2008}
2009
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002010static void
2011window_schedule_redraw_task(struct window *window);
2012
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002013void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002014widget_schedule_redraw(struct widget *widget)
2015{
Pekka Paalanen71233882013-04-25 13:57:53 +03002016 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002017 widget->surface->redraw_needed = 1;
2018 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002019}
2020
Neil Roberts97b747c2013-12-19 16:17:12 +00002021void
2022widget_set_use_cairo(struct widget *widget,
2023 int use_cairo)
2024{
2025 widget->use_cairo = use_cairo;
2026}
2027
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002028cairo_surface_t *
2029window_get_surface(struct window *window)
2030{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002031 cairo_surface_t *cairo_surface;
2032
2033 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2034
2035 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002036}
2037
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002038struct wl_surface *
2039window_get_wl_surface(struct window *window)
2040{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002041 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002042}
2043
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002044static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002045tooltip_redraw_handler(struct widget *widget, void *data)
2046{
2047 cairo_t *cr;
2048 const int32_t r = 3;
2049 struct tooltip *tooltip = data;
2050 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002051
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002052 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002053 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002054 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2055 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2056 cairo_paint(cr);
2057
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002058 width = widget->allocation.width;
2059 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002060 rounded_rect(cr, 0, 0, width, height, r);
2061
2062 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2063 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2064 cairo_fill(cr);
2065
-c505af82019-06-26 21:00:43 +00002066 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.85);
2067 cairo_move_to(cr, 10, 17);
2068 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002069 cairo_show_text(cr, tooltip->entry);
2070 cairo_destroy(cr);
2071}
2072
2073static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002074get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002075{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002076 cairo_t *cr;
2077 cairo_text_extents_t extents;
2078
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002079 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002080 * created yet, and parent does not have a valid surface
2081 * outside repaint, either.
2082 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002083 cr = cairo_create(display->dummy_surface);
-c505af82019-06-26 21:00:43 +00002084 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002085 cairo_text_extents(cr, tooltip->entry, &extents);
2086 cairo_destroy(cr);
2087
2088 return extents;
2089}
2090
2091static int
2092window_create_tooltip(struct tooltip *tooltip)
2093{
2094 struct widget *parent = tooltip->parent;
2095 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002096 const int offset_y = 27;
2097 const int margin = 3;
2098 cairo_text_extents_t extents;
2099
2100 if (tooltip->widget)
2101 return 0;
2102
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002103 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002104
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002105 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002106 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002107 widget_set_allocation(tooltip->widget,
2108 tooltip->x, tooltip->y + offset_y,
2109 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002110
2111 return 0;
2112}
2113
2114void
2115widget_destroy_tooltip(struct widget *parent)
2116{
2117 struct tooltip *tooltip = parent->tooltip;
2118
2119 parent->tooltip_count = 0;
2120 if (!tooltip)
2121 return;
2122
2123 if (tooltip->widget) {
2124 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002125 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002126 }
2127
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002128 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002129 free(tooltip->entry);
2130 free(tooltip);
2131 parent->tooltip = NULL;
2132}
2133
2134static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002135tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002136{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002137 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002138
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002139 window_create_tooltip(tooltip);
2140}
2141
2142#define TOOLTIP_TIMEOUT 500
2143static int
2144tooltip_timer_reset(struct tooltip *tooltip)
2145{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002146 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002147
2148 return 0;
2149}
2150
2151int
2152widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2153{
2154 struct tooltip *tooltip = parent->tooltip;
2155
2156 parent->tooltip_count++;
2157 if (tooltip) {
2158 tooltip->x = x;
2159 tooltip->y = y;
2160 tooltip_timer_reset(tooltip);
2161 return 0;
2162 }
2163
2164 /* the handler might be triggered too fast via input device motion, so
2165 * we need this check here to make sure tooltip is fully initialized */
2166 if (parent->tooltip_count > 1)
2167 return 0;
2168
2169 tooltip = malloc(sizeof *tooltip);
2170 if (!tooltip)
2171 return -1;
2172
2173 parent->tooltip = tooltip;
2174 tooltip->parent = parent;
2175 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002176 tooltip->x = x;
2177 tooltip->y = y;
2178 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002179 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2180 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002181 tooltip_timer_reset(tooltip);
2182
2183 return 0;
2184}
2185
2186static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002187frame_resize_handler(struct widget *widget,
2188 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002189{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002192 struct rectangle interior;
2193 struct rectangle input;
2194 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002195
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002196 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 interior.x = 0;
2198 interior.y = 0;
2199 interior.width = width;
2200 interior.height = height;
2201 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002202 frame_resize(frame->frame, width, height);
2203 frame_interior(frame->frame, &interior.x, &interior.y,
2204 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002205 }
2206
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002207 widget_set_allocation(child, interior.x, interior.y,
2208 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002209
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002210 if (child->resize_handler) {
2211 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212 child->user_data);
2213
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002214 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002215 width = child->allocation.width;
2216 height = child->allocation.height;
2217 } else {
2218 frame_resize_inside(frame->frame,
2219 child->allocation.width,
2220 child->allocation.height);
2221 width = frame_width(frame->frame);
2222 height = frame_height(frame->frame);
2223 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002224 }
2225
Scott Moreauf7e498c2012-05-14 11:39:29 -06002226 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002227
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002228 widget->surface->input_region =
2229 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002230 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002231 frame_input_rect(frame->frame, &input.x, &input.y,
2232 &input.width, &input.height);
2233 wl_region_add(widget->surface->input_region,
2234 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002235 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002236 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002237 }
2238
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002239 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002240
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002241 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002242 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002243 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2244 &opaque.width, &opaque.height);
2245
2246 wl_region_add(widget->surface->opaque_region,
2247 opaque.x, opaque.y,
2248 opaque.width, opaque.height);
2249 } else {
2250 wl_region_add(widget->surface->opaque_region,
2251 0, 0, width, height);
2252 }
Martin Minarik1998b152012-05-10 02:04:35 +02002253 }
2254
Martin Minarik1998b152012-05-10 02:04:35 +02002255
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002256 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002257}
2258
2259static void
2260frame_redraw_handler(struct widget *widget, void *data)
2261{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002262 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002263 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002264 struct window *window = widget->window;
2265
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002266 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002267 return;
2268
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002269 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002270
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002271 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002272
2273 cairo_destroy(cr);
2274}
2275
2276static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002277frame_get_pointer_image_for_location(struct window_frame *frame,
2278 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002279{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002280 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002281
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002282 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002283 return CURSOR_LEFT_PTR;
2284
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002285 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002286 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002287 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002288 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002289 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002290 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002291 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002292 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002293 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002294 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002295 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002296 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002297 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002298 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002299 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002300 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002301 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002302 case THEME_LOCATION_EXTERIOR:
2303 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002304 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002305 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002306 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002307}
2308
Pekka Paalanen26237862014-09-10 15:10:30 +03002309static void
2310frame_menu_func(void *data, struct input *input, int index)
2311{
2312 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002313
2314 switch (index) {
2315 case 0: /* close */
2316 window_close(window);
2317 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002318 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002319 /* we don't have a way to get out of fullscreen for now */
2320 if (window->fullscreen_handler)
2321 window->fullscreen_handler(window, window->user_data);
2322 break;
2323 }
2324}
2325
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002326void
2327window_show_frame_menu(struct window *window,
2328 struct input *input, uint32_t time)
2329{
2330 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002331 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002332
Pekka Paalanen26237862014-09-10 15:10:30 +03002333 static const char *entries[] = {
2334 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002335 "Fullscreen"
2336 };
2337
2338 if (window->fullscreen_handler)
2339 count = ARRAY_LENGTH(entries);
2340 else
2341 count = ARRAY_LENGTH(entries) - 1;
2342
2343 input_get_position(input, &x, &y);
2344 window_show_menu(window->display, input, time, window,
2345 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002346}
2347
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002348static int
2349frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002350 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002351{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002352 struct window_frame *frame = data;
2353 enum theme_location location;
2354
2355 location = frame_pointer_enter(frame->frame, input, x, y);
2356 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2357 widget_schedule_redraw(frame->widget);
2358
2359 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002360}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002361
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002362static int
2363frame_motion_handler(struct widget *widget,
2364 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002365 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002366{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002367 struct window_frame *frame = data;
2368 enum theme_location location;
2369
2370 location = frame_pointer_motion(frame->frame, input, x, y);
2371 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2372 widget_schedule_redraw(frame->widget);
2373
2374 return frame_get_pointer_image_for_location(data, location);
2375}
2376
2377static void
2378frame_leave_handler(struct widget *widget,
2379 struct input *input, void *data)
2380{
2381 struct window_frame *frame = data;
2382
2383 frame_pointer_leave(frame->frame, input);
2384 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2385 widget_schedule_redraw(frame->widget);
2386}
2387
2388static void
2389frame_handle_status(struct window_frame *frame, struct input *input,
2390 uint32_t time, enum theme_location location)
2391{
2392 struct window *window = frame->widget->window;
2393 uint32_t status;
2394
2395 status = frame_status(frame->frame);
2396 if (status & FRAME_STATUS_REPAINT)
2397 widget_schedule_redraw(frame->widget);
2398
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002399 if (status & FRAME_STATUS_MINIMIZE) {
2400 window_set_minimized(window);
2401 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2402 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002403
2404 if (status & FRAME_STATUS_MENU) {
2405 window_show_frame_menu(window, input, time);
2406 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2407 }
2408
2409 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002410 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002411 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2412 }
2413
2414 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002415 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002416 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002417 }
2418
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002419 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002420 input_ungrab(input);
ant8mef99fac22018-11-28 22:46:37 +01002421 xdg_toplevel_move(window->xdg_toplevel,
2422 input_get_seat(input),
2423 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002424
2425 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2426 }
2427
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002428 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002429 input_ungrab(input);
2430
ant8mef99fac22018-11-28 22:46:37 +01002431 xdg_toplevel_resize(window->xdg_toplevel,
2432 input_get_seat(input),
2433 window->display->serial,
2434 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002435
2436 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2437 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002438}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002439
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002440#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002441static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002442frame_button_handler(struct widget *widget,
2443 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002444 uint32_t button, enum wl_pointer_button_state state,
2445 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002446
2447{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002448 struct window_frame *frame = data;
2449 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002450
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002451 frame->double_click = 0;
2452 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2453 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2454 frame->double_click = 1;
2455 frame->did_double = 1;
2456 } else
2457 frame->did_double = 0;
2458
2459 frame->last_time = time;
2460 } else if (frame->did_double == 1) {
2461 frame->double_click = 1;
2462 frame->did_double = 0;
2463 }
2464
2465 if (frame->double_click)
2466 location = frame_double_click(frame->frame, input,
2467 button, state);
2468 else
2469 location = frame_pointer_button(frame->frame, input,
2470 button, state);
2471
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002472 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002473}
2474
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002475static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002476frame_touch_down_handler(struct widget *widget, struct input *input,
2477 uint32_t serial, uint32_t time, int32_t id,
2478 float x, float y, void *data)
2479{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002480 struct window_frame *frame = data;
2481
Xiong Zhang382de462014-06-12 11:06:26 +08002482 frame->double_click = 0;
2483 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2484 frame->last_id == id) {
2485 frame->double_click = 1;
2486 frame->did_double = 1;
2487 frame->double_id = id;
2488 } else
2489 frame->did_double = 0;
2490
2491 frame->last_time = time;
2492 frame->last_id = id;
2493
2494 if (frame->double_click)
2495 frame_double_touch_down(frame->frame, input, id, x, y);
2496 else
2497 frame_touch_down(frame->frame, input, id, x, y);
2498
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002499 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2500}
2501
2502static void
2503frame_touch_up_handler(struct widget *widget,
2504 struct input *input, uint32_t serial, uint32_t time,
2505 int32_t id, void *data)
2506{
2507 struct window_frame *frame = data;
2508
Xiong Zhang382de462014-06-12 11:06:26 +08002509 if (frame->double_id == id && frame->did_double) {
2510 frame->did_double = 0;
2511 frame->double_id = 0;
2512 frame_double_touch_up(frame->frame, input, id);
2513 } else
2514 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002515 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002516}
2517
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002518struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002519window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002520{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002521 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002522 uint32_t buttons;
2523
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002524 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002525 buttons = FRAME_BUTTON_NONE;
2526 } else {
2527 buttons = FRAME_BUTTON_ALL;
2528 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002529
Peter Huttererf3d62272013-08-08 11:57:05 +10002530 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002531 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002532 buttons, window->title, NULL);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002533
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002534 frame->widget = window_add_widget(window, frame);
2535 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002536
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002537 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2538 widget_set_resize_handler(frame->widget, frame_resize_handler);
2539 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002540 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002541 widget_set_motion_handler(frame->widget, frame_motion_handler);
2542 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002543 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002544 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002545
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002546 window->frame = frame;
2547
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002548 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002549}
2550
Kristian Høgsberga1627922012-06-20 17:30:03 -04002551void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002552window_frame_set_child_size(struct widget *widget, int child_width,
2553 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002554{
2555 struct display *display = widget->window->display;
2556 struct theme *t = display->theme;
2557 int decoration_width, decoration_height;
2558 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002559 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002560
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002561 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002562 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002563 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002564 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002565
2566 width = child_width + decoration_width;
2567 height = child_height + decoration_height;
2568 } else {
2569 width = child_width;
2570 height = child_height;
2571 }
2572
2573 window_schedule_resize(widget->window, width, height);
2574}
2575
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002576static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002577window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002578{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002579 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002580
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002581 /* frame->child must be destroyed by the application */
2582 widget_destroy(frame->widget);
2583 free(frame);
2584}
2585
2586static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002587input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002588 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002589{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002590 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002591 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002592
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002593 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002594 return;
2595
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002596 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002597 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002598 widget = old;
2599 if (input->grab)
2600 widget = input->grab;
2601 if (widget->leave_handler)
2602 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002603 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002604 }
2605
2606 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002607 widget = focus;
2608 if (input->grab)
2609 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002610 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002611 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002612 cursor = widget->enter_handler(focus, input, x, y,
2613 widget->user_data);
2614 else
2615 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002616
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002617 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002618 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002619}
2620
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002621void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002622touch_grab(struct input *input, int32_t touch_id)
2623{
2624 input->touch_grab = 1;
2625 input->touch_grab_id = touch_id;
2626}
2627
2628void
2629touch_ungrab(struct input *input)
2630{
2631 struct touch_point *tp, *tmp;
2632
2633 input->touch_grab = 0;
2634
2635 wl_list_for_each_safe(tp, tmp,
2636 &input->touch_point_list, link) {
2637 if (tp->id != input->touch_grab_id)
2638 continue;
2639 wl_list_remove(&tp->link);
2640 free(tp);
2641
2642 return;
2643 }
2644}
2645
2646void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002647input_grab(struct input *input, struct widget *widget, uint32_t button)
2648{
2649 input->grab = widget;
2650 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002651
2652 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002653}
2654
2655void
2656input_ungrab(struct input *input)
2657{
2658 struct widget *widget;
2659
2660 input->grab = NULL;
2661 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002662 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002663 input->sx, input->sy);
2664 input_set_focus_widget(input, widget, input->sx, input->sy);
2665 }
2666}
2667
2668static void
Derek Foreman118a4292015-04-22 17:23:35 -05002669cursor_delay_timer_reset(struct input *input, uint32_t duration)
2670{
Derek Foreman118a4292015-04-22 17:23:35 -05002671 if (!duration)
2672 input->cursor_timer_running = false;
2673 else
2674 input->cursor_timer_running = true;
2675
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002676 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002677}
2678
2679static void cancel_pointer_image_update(struct input *input)
2680{
2681 if (input->cursor_timer_running)
2682 cursor_delay_timer_reset(input, 0);
2683}
2684
2685static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002686input_remove_pointer_focus(struct input *input)
2687{
2688 struct window *window = input->pointer_focus;
2689
2690 if (!window)
2691 return;
2692
2693 input_set_focus_widget(input, NULL, 0, 0);
2694
2695 input->pointer_focus = NULL;
2696 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002697 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002698}
2699
2700static void
2701pointer_handle_enter(void *data, struct wl_pointer *pointer,
2702 uint32_t serial, struct wl_surface *surface,
2703 wl_fixed_t sx_w, wl_fixed_t sy_w)
2704{
2705 struct input *input = data;
2706 struct window *window;
2707 struct widget *widget;
2708 float sx = wl_fixed_to_double(sx_w);
2709 float sy = wl_fixed_to_double(sy_w);
2710
2711 if (!surface) {
2712 /* enter event for a window we've just destroyed */
2713 return;
2714 }
2715
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002716 window = wl_surface_get_user_data(surface);
2717 if (surface != window->main_surface->surface) {
2718 DBG("Ignoring input event from subsurface %p\n", surface);
2719 return;
2720 }
2721
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002722 input->display->serial = serial;
2723 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002724 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002725
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002726 input->sx = sx;
2727 input->sy = sy;
2728
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002729 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002730 input_set_focus_widget(input, widget, sx, sy);
2731}
2732
2733static void
2734pointer_handle_leave(void *data, struct wl_pointer *pointer,
2735 uint32_t serial, struct wl_surface *surface)
2736{
2737 struct input *input = data;
2738
2739 input->display->serial = serial;
2740 input_remove_pointer_focus(input);
2741}
2742
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002743static void
Daniel Stone37816df2012-05-16 18:45:18 +01002744pointer_handle_motion(void *data, struct wl_pointer *pointer,
2745 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002746{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002747 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002748 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002749 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002750 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002751 float sx = wl_fixed_to_double(sx_w);
2752 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002753
Paul Winwoodb22bf572013-08-29 10:52:54 +01002754 if (!window)
2755 return;
2756
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002757 input->sx = sx;
2758 input->sy = sy;
2759
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002760 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002761 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002762 * based on the old surface dimensions. However, if we have an active
2763 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002764 */
Derek Foreman46812b62015-08-26 17:13:27 -05002765 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002766 sy < window->main_surface->allocation.y ||
2767 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002768 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002769 return;
2770
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002771 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002772 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002773 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002774 }
2775
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002776 if (input->grab)
2777 widget = input->grab;
2778 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002779 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002780 if (widget) {
2781 if (widget->motion_handler)
2782 cursor = widget->motion_handler(input->focus_widget,
2783 input, time, sx, sy,
2784 widget->user_data);
2785 else
2786 cursor = widget->default_cursor;
2787 } else
2788 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002789
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002790 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002791}
2792
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002793static void
Daniel Stone37816df2012-05-16 18:45:18 +01002794pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002795 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002796{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002797 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002798 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002799 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002800
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002801 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002802 if (input->focus_widget && input->grab == NULL &&
2803 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002804 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002805
Neil Roberts6b28aad2012-01-23 19:11:18 +00002806 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002807 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002808 (*widget->button_handler)(widget,
2809 input, time,
2810 button, state,
2811 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002812
Daniel Stone4dbadb12012-05-30 16:31:51 +01002813 if (input->grab && input->grab_button == button &&
2814 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002815 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002816}
2817
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002818static void
Daniel Stone37816df2012-05-16 18:45:18 +01002819pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002820 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002821{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002822 struct input *input = data;
2823 struct widget *widget;
2824
2825 widget = input->focus_widget;
2826 if (input->grab)
2827 widget = input->grab;
2828 if (widget && widget->axis_handler)
2829 (*widget->axis_handler)(widget,
2830 input, time,
2831 axis, value,
2832 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002833}
2834
Peter Hutterer87743e92016-01-18 16:38:22 +10002835static void
2836pointer_handle_frame(void *data, struct wl_pointer *pointer)
2837{
2838 struct input *input = data;
2839 struct widget *widget;
2840
2841 widget = input->focus_widget;
2842 if (input->grab)
2843 widget = input->grab;
2844 if (widget && widget->pointer_frame_handler)
2845 (*widget->pointer_frame_handler)(widget,
2846 input,
2847 widget->user_data);
2848}
2849
2850static void
2851pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2852 uint32_t source)
2853{
2854 struct input *input = data;
2855 struct widget *widget;
2856
2857 widget = input->focus_widget;
2858 if (input->grab)
2859 widget = input->grab;
2860 if (widget && widget->axis_source_handler)
2861 (*widget->axis_source_handler)(widget,
2862 input,
2863 source,
2864 widget->user_data);
2865}
2866
2867static void
2868pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2869 uint32_t time, uint32_t axis)
2870{
2871 struct input *input = data;
2872 struct widget *widget;
2873
2874 widget = input->focus_widget;
2875 if (input->grab)
2876 widget = input->grab;
2877 if (widget && widget->axis_stop_handler)
2878 (*widget->axis_stop_handler)(widget,
2879 input, time,
2880 axis,
2881 widget->user_data);
2882}
2883
2884static void
2885pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2886 uint32_t axis, int32_t discrete)
2887{
2888 struct input *input = data;
2889 struct widget *widget;
2890
2891 widget = input->focus_widget;
2892 if (input->grab)
2893 widget = input->grab;
2894 if (widget && widget->axis_discrete_handler)
2895 (*widget->axis_discrete_handler)(widget,
2896 input,
2897 axis,
2898 discrete,
2899 widget->user_data);
2900}
2901
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002902static const struct wl_pointer_listener pointer_listener = {
2903 pointer_handle_enter,
2904 pointer_handle_leave,
2905 pointer_handle_motion,
2906 pointer_handle_button,
2907 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002908 pointer_handle_frame,
2909 pointer_handle_axis_source,
2910 pointer_handle_axis_stop,
2911 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002912};
2913
2914static void
2915input_remove_keyboard_focus(struct input *input)
2916{
2917 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002918
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002919 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002920
2921 if (!window)
2922 return;
2923
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002924 if (window->keyboard_focus_handler)
2925 (*window->keyboard_focus_handler)(window, NULL,
2926 window->user_data);
2927
2928 input->keyboard_focus = NULL;
2929}
2930
2931static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002932keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002933{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002934 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002935 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002936
2937 if (window && window->key_handler) {
2938 (*window->key_handler)(window, input, input->repeat_time,
2939 input->repeat_key, input->repeat_sym,
2940 WL_KEYBOARD_KEY_STATE_PRESSED,
2941 window->user_data);
2942 }
2943}
2944
2945static void
2946keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2947 uint32_t format, int fd, uint32_t size)
2948{
2949 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002950 struct xkb_keymap *keymap;
2951 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002952#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002953 struct xkb_compose_table *compose_table;
2954 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002955#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002956 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002957 char *map_str;
2958
2959 if (!data) {
2960 close(fd);
2961 return;
2962 }
2963
2964 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2965 close(fd);
2966 return;
2967 }
2968
Sebastian Wickac1b92d2019-11-01 02:27:55 +01002969 map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002970 if (map_str == MAP_FAILED) {
2971 close(fd);
2972 return;
2973 }
2974
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002975 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03002976 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2977 map_str,
2978 XKB_KEYMAP_FORMAT_TEXT_V1,
2979 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002980 munmap(map_str, size);
2981 close(fd);
2982
Rui Matos3eccb862013-10-10 19:44:22 +02002983 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002984 fprintf(stderr, "failed to compile keymap\n");
2985 return;
2986 }
2987
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002988 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02002989 state = xkb_state_new(keymap);
2990 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002991 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002992 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002993 return;
2994 }
2995
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002996 /* Look up the preferred locale, falling back to "C" as default */
2997 if (!(locale = getenv("LC_ALL")))
2998 if (!(locale = getenv("LC_CTYPE")))
2999 if (!(locale = getenv("LANG")))
3000 locale = "C";
3001
3002 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003003#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003004 compose_table =
3005 xkb_compose_table_new_from_locale(input->display->xkb_context,
3006 locale,
3007 XKB_COMPOSE_COMPILE_NO_FLAGS);
3008 if (compose_table) {
3009 /* Set up XKB compose state */
3010 compose_state = xkb_compose_state_new(compose_table,
3011 XKB_COMPOSE_STATE_NO_FLAGS);
3012 if (compose_state) {
3013 xkb_compose_state_unref(input->xkb.compose_state);
3014 xkb_compose_table_unref(input->xkb.compose_table);
3015 input->xkb.compose_state = compose_state;
3016 input->xkb.compose_table = compose_table;
3017 } else {
3018 fprintf(stderr, "could not create XKB compose state. "
3019 "Disabiling compose.\n");
3020 xkb_compose_table_unref(compose_table);
3021 compose_table = NULL;
3022 }
3023 } else {
3024 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3025 "Disabiling compose\n", locale);
3026 }
Daniel Stone5b015962016-10-20 14:45:58 +01003027#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003028
Rui Matos3eccb862013-10-10 19:44:22 +02003029 xkb_keymap_unref(input->xkb.keymap);
3030 xkb_state_unref(input->xkb.state);
3031 input->xkb.keymap = keymap;
3032 input->xkb.state = state;
3033
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003034 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003035 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003036 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003037 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003038 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003039 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003040}
3041
3042static void
3043keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3044 uint32_t serial, struct wl_surface *surface,
3045 struct wl_array *keys)
3046{
3047 struct input *input = data;
3048 struct window *window;
3049
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003050 if (!surface) {
3051 /* enter event for a window we've just destroyed */
3052 return;
3053 }
3054
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003055 input->display->serial = serial;
3056 input->keyboard_focus = wl_surface_get_user_data(surface);
3057
3058 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003059 if (window->keyboard_focus_handler)
3060 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003061 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003062}
3063
3064static void
3065keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3066 uint32_t serial, struct wl_surface *surface)
3067{
3068 struct input *input = data;
3069
3070 input->display->serial = serial;
3071 input_remove_keyboard_focus(input);
3072}
3073
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003074/* Translate symbols appropriately if a compose sequence is being entered */
3075static xkb_keysym_t
3076process_key_press(xkb_keysym_t sym, struct input *input)
3077{
Daniel Stone5b015962016-10-20 14:45:58 +01003078#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003079 if (!input->xkb.compose_state)
3080 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003081 if (sym == XKB_KEY_NoSymbol)
3082 return sym;
3083 if (xkb_compose_state_feed(input->xkb.compose_state,
3084 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3085 return sym;
3086
3087 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3088 case XKB_COMPOSE_COMPOSING:
3089 return XKB_KEY_NoSymbol;
3090 case XKB_COMPOSE_COMPOSED:
3091 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3092 case XKB_COMPOSE_CANCELLED:
3093 return XKB_KEY_NoSymbol;
3094 case XKB_COMPOSE_NOTHING:
3095 return sym;
3096 default:
3097 return sym;
3098 }
Daniel Stone5b015962016-10-20 14:45:58 +01003099#else
3100 return sym;
3101#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003102}
3103
Scott Moreau210d0792012-03-22 10:47:01 -06003104static void
Daniel Stone37816df2012-05-16 18:45:18 +01003105keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003106 uint32_t serial, uint32_t time, uint32_t key,
3107 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003108{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003109 struct input *input = data;
3110 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003111 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003112 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003113 const xkb_keysym_t *syms;
3114 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003115 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003116
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003117 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003118 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003119 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003120 return;
3121
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003122 /* We only use input grabs for pointer events for now, so just
3123 * ignore key presses if a grab is active. We expand the key
3124 * event delivery mechanism to route events to widgets to
3125 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003126 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003127 if (input->grab && input->grab_button == 0)
3128 return;
3129
Ran Benita2e1968f2014-08-19 23:59:51 +03003130 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003131
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003132 sym = XKB_KEY_NoSymbol;
3133 if (num_syms == 1)
3134 sym = syms[0];
3135
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003136
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003137 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003138 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003139 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003140 } else if (sym == XKB_KEY_F11 &&
3141 window->fullscreen_handler &&
3142 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3143 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003144 } else if (sym == XKB_KEY_F4 &&
3145 input->modifiers == MOD_ALT_MASK &&
3146 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003147 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003148 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003149 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3150 sym = process_key_press(sym, input);
3151
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003152 (*window->key_handler)(window, input, time, key,
3153 sym, state, window->user_data);
3154 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003155
3156 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3157 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003158 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003159 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3160 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003161 input->repeat_sym = sym;
3162 input->repeat_key = key;
3163 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003164 its.it_interval.tv_sec = input->repeat_rate_sec;
3165 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3166 its.it_value.tv_sec = input->repeat_delay_sec;
3167 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003168 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003169 }
3170}
3171
3172static void
Daniel Stone351eb612012-05-31 15:27:47 -04003173keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3174 uint32_t serial, uint32_t mods_depressed,
3175 uint32_t mods_latched, uint32_t mods_locked,
3176 uint32_t group)
3177{
3178 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003179 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003180
Matt Ropere61561f2013-06-24 16:52:43 +01003181 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3182 if (!input->xkb.keymap)
3183 return;
3184
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003185 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3186 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003187 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003188 XKB_STATE_MODS_DEPRESSED |
3189 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003190 input->modifiers = 0;
3191 if (mask & input->xkb.control_mask)
3192 input->modifiers |= MOD_CONTROL_MASK;
3193 if (mask & input->xkb.alt_mask)
3194 input->modifiers |= MOD_ALT_MASK;
3195 if (mask & input->xkb.shift_mask)
3196 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003197}
3198
Jonny Lamb06959082014-08-12 14:58:27 +02003199static void
3200set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3201{
3202 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3203 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3204
3205 /* a rate of zero disables any repeating, regardless of the delay's
3206 * value */
3207 if (rate == 0)
3208 return;
3209
3210 if (rate == 1)
3211 input->repeat_rate_sec = 1;
3212 else
3213 input->repeat_rate_nsec = 1000000000 / rate;
3214
3215 input->repeat_delay_sec = delay / 1000;
3216 delay -= (input->repeat_delay_sec * 1000);
3217 input->repeat_delay_nsec = delay * 1000 * 1000;
3218}
3219
3220static void
3221keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3222 int32_t rate, int32_t delay)
3223{
3224 struct input *input = data;
3225
3226 set_repeat_info(input, rate, delay);
3227}
3228
Daniel Stone37816df2012-05-16 18:45:18 +01003229static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003230 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003231 keyboard_handle_enter,
3232 keyboard_handle_leave,
3233 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003234 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003235 keyboard_handle_repeat_info
3236
Daniel Stone37816df2012-05-16 18:45:18 +01003237};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003238
3239static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003240touch_handle_down(void *data, struct wl_touch *wl_touch,
3241 uint32_t serial, uint32_t time, struct wl_surface *surface,
3242 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3243{
3244 struct input *input = data;
3245 struct widget *widget;
3246 float sx = wl_fixed_to_double(x_w);
3247 float sy = wl_fixed_to_double(y_w);
3248
Jeffy Chen97749172019-09-02 10:26:09 +08003249 if (!surface) {
3250 /* down event for a window we've just destroyed */
3251 return;
3252 }
3253
Rusty Lynch1084da52013-08-15 09:10:08 -07003254 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003255 input->touch_focus = wl_surface_get_user_data(surface);
3256 if (!input->touch_focus) {
3257 DBG("Failed to find to touch focus for surface %p\n", surface);
3258 return;
3259 }
3260
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003261 if (surface != input->touch_focus->main_surface->surface) {
3262 DBG("Ignoring input event from subsurface %p\n", surface);
3263 input->touch_focus = NULL;
3264 return;
3265 }
3266
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003267 if (input->grab)
3268 widget = input->grab;
3269 else
3270 widget = window_find_widget(input->touch_focus,
3271 wl_fixed_to_double(x_w),
3272 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003273 if (widget) {
3274 struct touch_point *tp = xmalloc(sizeof *tp);
3275 if (tp) {
3276 tp->id = id;
3277 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003278 tp->x = sx;
3279 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003280 wl_list_insert(&input->touch_point_list, &tp->link);
3281
3282 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003283 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003284 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003285 sx, sy,
3286 widget->user_data);
3287 }
3288 }
3289}
3290
3291static void
3292touch_handle_up(void *data, struct wl_touch *wl_touch,
3293 uint32_t serial, uint32_t time, int32_t id)
3294{
3295 struct input *input = data;
3296 struct touch_point *tp, *tmp;
3297
Rusty Lynch041815a2013-08-08 21:20:38 -07003298 if (!input->touch_focus) {
3299 DBG("No touch focus found for touch up event!\n");
3300 return;
3301 }
3302
3303 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3304 if (tp->id != id)
3305 continue;
3306
3307 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003308 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003309 time, id,
3310 tp->widget->user_data);
3311
3312 wl_list_remove(&tp->link);
3313 free(tp);
3314
3315 return;
3316 }
3317}
3318
3319static void
3320touch_handle_motion(void *data, struct wl_touch *wl_touch,
3321 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3322{
3323 struct input *input = data;
3324 struct touch_point *tp;
3325 float sx = wl_fixed_to_double(x_w);
3326 float sy = wl_fixed_to_double(y_w);
3327
3328 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3329
3330 if (!input->touch_focus) {
3331 DBG("No touch focus found for touch motion event!\n");
3332 return;
3333 }
3334
3335 wl_list_for_each(tp, &input->touch_point_list, link) {
3336 if (tp->id != id)
3337 continue;
3338
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003339 tp->x = sx;
3340 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003341 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003342 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003343 id, sx, sy,
3344 tp->widget->user_data);
3345 return;
3346 }
3347}
3348
3349static void
3350touch_handle_frame(void *data, struct wl_touch *wl_touch)
3351{
3352 struct input *input = data;
3353 struct touch_point *tp, *tmp;
3354
3355 DBG("touch_handle_frame\n");
3356
3357 if (!input->touch_focus) {
3358 DBG("No touch focus found for touch frame event!\n");
3359 return;
3360 }
3361
3362 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3363 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003364 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003365 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003366 }
3367}
3368
3369static void
3370touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3371{
3372 struct input *input = data;
3373 struct touch_point *tp, *tmp;
3374
3375 DBG("touch_handle_cancel\n");
3376
3377 if (!input->touch_focus) {
3378 DBG("No touch focus found for touch cancel event!\n");
3379 return;
3380 }
3381
3382 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3383 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003384 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3385 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003386
3387 wl_list_remove(&tp->link);
3388 free(tp);
3389 }
3390}
3391
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003392void touch_handle_shape(void *data, struct wl_touch *wl_touch, int32_t id,
3393 wl_fixed_t major, wl_fixed_t minor)
3394{
3395}
3396
3397void touch_handle_orientation(void *data, struct wl_touch *wl_touch, int32_t id,
3398 wl_fixed_t orientation)
3399{
3400}
3401
Rusty Lynch041815a2013-08-08 21:20:38 -07003402static const struct wl_touch_listener touch_listener = {
3403 touch_handle_down,
3404 touch_handle_up,
3405 touch_handle_motion,
3406 touch_handle_frame,
3407 touch_handle_cancel,
Sebastian Wick5b64fbd2019-11-01 02:15:10 +01003408 touch_handle_shape,
3409 touch_handle_orientation,
Rusty Lynch041815a2013-08-08 21:20:38 -07003410};
3411
3412static void
Daniel Stone37816df2012-05-16 18:45:18 +01003413seat_handle_capabilities(void *data, struct wl_seat *seat,
3414 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003415{
Daniel Stone37816df2012-05-16 18:45:18 +01003416 struct input *input = data;
3417
3418 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3419 input->pointer = wl_seat_get_pointer(seat);
3420 wl_pointer_set_user_data(input->pointer, input);
3421 wl_pointer_add_listener(input->pointer, &pointer_listener,
3422 input);
3423 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003424 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003425 wl_pointer_release(input->pointer);
3426 else
3427 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003428 input->pointer = NULL;
3429 }
3430
3431 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3432 input->keyboard = wl_seat_get_keyboard(seat);
3433 wl_keyboard_set_user_data(input->keyboard, input);
3434 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3435 input);
3436 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003437 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003438 wl_keyboard_release(input->keyboard);
3439 else
3440 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003441 input->keyboard = NULL;
3442 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003443
3444 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3445 input->touch = wl_seat_get_touch(seat);
3446 wl_touch_set_user_data(input->touch, input);
3447 wl_touch_add_listener(input->touch, &touch_listener, input);
3448 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003449 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003450 wl_touch_release(input->touch);
3451 else
3452 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003453 input->touch = NULL;
3454 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003455}
3456
Rob Bradford08031182013-08-13 20:11:03 +01003457static void
3458seat_handle_name(void *data, struct wl_seat *seat,
3459 const char *name)
3460{
3461
3462}
3463
Daniel Stone37816df2012-05-16 18:45:18 +01003464static const struct wl_seat_listener seat_listener = {
3465 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003466 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003467};
3468
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003469void
3470input_get_position(struct input *input, int32_t *x, int32_t *y)
3471{
3472 *x = input->sx;
3473 *y = input->sy;
3474}
3475
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003476int
3477input_get_touch(struct input *input, int32_t id, float *x, float *y)
3478{
3479 struct touch_point *tp;
3480
3481 wl_list_for_each(tp, &input->touch_point_list, link) {
3482 if (tp->id != id)
3483 continue;
3484
3485 *x = tp->x;
3486 *y = tp->y;
3487 return 0;
3488 }
3489
3490 return -1;
3491}
3492
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003493struct display *
3494input_get_display(struct input *input)
3495{
3496 return input->display;
3497}
3498
Daniel Stone37816df2012-05-16 18:45:18 +01003499struct wl_seat *
3500input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003501{
Daniel Stone37816df2012-05-16 18:45:18 +01003502 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003503}
3504
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003505uint32_t
3506input_get_modifiers(struct input *input)
3507{
3508 return input->modifiers;
3509}
3510
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003511struct widget *
3512input_get_focus_widget(struct input *input)
3513{
3514 return input->focus_widget;
3515}
3516
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003517struct data_offer {
3518 struct wl_data_offer *offer;
3519 struct input *input;
3520 struct wl_array types;
3521 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003522
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003523 struct task io_task;
3524 int fd;
3525 data_func_t func;
3526 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003527 uint32_t dnd_action;
3528 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003529 void *user_data;
3530};
3531
3532static void
3533data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3534{
3535 struct data_offer *offer = data;
3536 char **p;
3537
3538 p = wl_array_add(&offer->types, sizeof *p);
3539 *p = strdup(type);
3540}
3541
Carlos Garnacho9c931792016-01-18 23:52:12 +01003542static void
3543data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3544{
3545 struct data_offer *offer = data;
3546
3547 offer->source_actions = source_actions;
3548}
3549
3550static void
3551data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3552{
3553 struct data_offer *offer = data;
3554
3555 offer->dnd_action = dnd_action;
3556}
3557
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003558static const struct wl_data_offer_listener data_offer_listener = {
3559 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003560 data_offer_source_actions,
3561 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003562};
3563
3564static void
3565data_offer_destroy(struct data_offer *offer)
3566{
3567 char **p;
3568
3569 offer->refcount--;
3570 if (offer->refcount == 0) {
3571 wl_data_offer_destroy(offer->offer);
3572 for (p = offer->types.data; *p; p++)
3573 free(*p);
3574 wl_array_release(&offer->types);
3575 free(offer);
3576 }
3577}
3578
3579static void
3580data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003581 struct wl_data_device *data_device,
3582 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003583{
3584 struct data_offer *offer;
3585
Brian Lovinbc919262013-08-07 15:34:59 -07003586 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003587
3588 wl_array_init(&offer->types);
3589 offer->refcount = 1;
3590 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003591 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003592 wl_data_offer_add_listener(offer->offer,
3593 &data_offer_listener, offer);
3594}
3595
3596static void
3597data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003598 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003599 wl_fixed_t x_w, wl_fixed_t y_w,
3600 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003601{
3602 struct input *input = data;
3603 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003604 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003605 float x = wl_fixed_to_double(x_w);
3606 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003607 char **p;
3608
Jeffy Chen97749172019-09-02 10:26:09 +08003609 if (!surface) {
3610 /* enter event for a window we've just destroyed */
3611 return;
3612 }
3613
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003614 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003615 input->drag_enter_serial = serial;
3616 input->drag_focus = window,
3617 input->drag_x = x;
3618 input->drag_y = y;
3619
3620 if (!input->touch_grab)
3621 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003622
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003623 if (offer) {
3624 input->drag_offer = wl_data_offer_get_user_data(offer);
3625
3626 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3627 *p = NULL;
3628
3629 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003630
3631 if (input->display->data_device_manager_version >=
3632 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3633 wl_data_offer_set_actions(offer,
3634 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3635 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3636 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3637 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003638 } else {
3639 input->drag_offer = NULL;
3640 types_data = NULL;
3641 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003642
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003643 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003644 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003645 window->user_data);
3646}
3647
3648static void
3649data_device_leave(void *data, struct wl_data_device *data_device)
3650{
3651 struct input *input = data;
3652
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003653 if (input->drag_offer) {
3654 data_offer_destroy(input->drag_offer);
3655 input->drag_offer = NULL;
3656 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003657}
3658
3659static void
3660data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003661 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003662{
3663 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003664 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003665 float x = wl_fixed_to_double(x_w);
3666 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003667 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003668
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003669 input->drag_x = x;
3670 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003671
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003672 if (input->drag_offer)
3673 types_data = input->drag_offer->types.data;
3674 else
3675 types_data = NULL;
3676
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003677 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003678 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003679 window->user_data);
3680}
3681
3682static void
3683data_device_drop(void *data, struct wl_data_device *data_device)
3684{
3685 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003686 struct window *window = input->drag_focus;
3687 float x, y;
3688
3689 x = input->drag_x;
3690 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003691
3692 if (window->drop_handler)
3693 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003694 x, y, window->user_data);
3695
3696 if (input->touch_grab)
3697 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003698}
3699
3700static void
3701data_device_selection(void *data,
3702 struct wl_data_device *wl_data_device,
3703 struct wl_data_offer *offer)
3704{
3705 struct input *input = data;
3706 char **p;
3707
3708 if (input->selection_offer)
3709 data_offer_destroy(input->selection_offer);
3710
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003711 if (offer) {
3712 input->selection_offer = wl_data_offer_get_user_data(offer);
3713 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3714 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003715 } else {
3716 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003717 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003718}
3719
3720static const struct wl_data_device_listener data_device_listener = {
3721 data_device_data_offer,
3722 data_device_enter,
3723 data_device_leave,
3724 data_device_motion,
3725 data_device_drop,
3726 data_device_selection
3727};
3728
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003729static void
3730input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003731{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003732 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003733 struct wl_cursor *cursor;
3734 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003735
Daniel Stone80972742012-11-07 17:51:39 +11003736 if (!input->pointer)
3737 return;
3738
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003739 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003740 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003741 return;
3742
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003743 if (index >= (int) cursor->image_count) {
3744 fprintf(stderr, "cursor index out of range\n");
3745 return;
3746 }
3747
3748 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003749 buffer = wl_cursor_image_get_buffer(image);
3750 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003751 return;
3752
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003753 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3754 wl_surface_damage(input->pointer_surface, 0, 0,
3755 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003756 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003757 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3758 input->pointer_surface,
3759 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003760}
3761
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003762static const struct wl_callback_listener pointer_surface_listener;
3763
Derek Foreman493d9792015-03-04 16:26:25 -06003764static bool
3765input_set_pointer_special(struct input *input)
3766{
3767 if (input->current_cursor == CURSOR_BLANK) {
3768 wl_pointer_set_cursor(input->pointer,
3769 input->pointer_enter_serial,
3770 NULL, 0, 0);
3771 return true;
3772 }
3773
3774 if (input->current_cursor == CURSOR_UNSET)
3775 return true;
3776
3777 return false;
3778}
3779
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003780static void
Derek Foreman118a4292015-04-22 17:23:35 -05003781schedule_pointer_image_update(struct input *input,
3782 struct wl_cursor *cursor,
3783 uint32_t duration,
3784 bool force_frame)
3785{
3786 /* Some silly cursor sets have enormous pauses in them. In these
3787 * cases it's better to use a timer even if it results in less
3788 * accurate presentation, since it will save us having to set the
3789 * same cursor image over and over again.
3790 *
3791 * This is really not the way we're supposed to time any kind of
3792 * animation, but we're pretending it's OK here because we don't
3793 * want animated cursors with long delays to needlessly hog CPU.
3794 *
3795 * We use force_frame to ensure we don't accumulate large timing
3796 * errors by running off the wrong clock.
3797 */
3798 if (!force_frame && duration > 100) {
3799 struct timespec tp;
3800
3801 clock_gettime(CLOCK_MONOTONIC, &tp);
3802 input->cursor_timer_start = tp.tv_sec * 1000
3803 + tp.tv_nsec / 1000000;
3804 cursor_delay_timer_reset(input, duration);
3805 return;
3806 }
3807
3808 /* for short durations we'll just spin on frame callbacks for
3809 * accurate timing - the way any kind of timing sensitive animation
3810 * should really be done. */
3811 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3812 wl_callback_add_listener(input->cursor_frame_cb,
3813 &pointer_surface_listener, input);
3814
3815}
3816
3817static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003818pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3819 uint32_t time)
3820{
3821 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003822 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003823 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003824 uint32_t duration;
3825 bool force_frame = true;
3826
3827 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003828
3829 if (callback) {
3830 assert(callback == input->cursor_frame_cb);
3831 wl_callback_destroy(callback);
3832 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003833 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003834 }
3835
Daniel Stone80972742012-11-07 17:51:39 +11003836 if (!input->pointer)
3837 return;
3838
Derek Foreman493d9792015-03-04 16:26:25 -06003839 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003840 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003841
Daniel Stonea494f1d2012-06-18 19:31:12 +01003842 cursor = input->display->cursors[input->current_cursor];
3843 if (!cursor)
3844 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003845
3846 /* FIXME We don't have the current time on the first call so we set
3847 * the animation start to the time of the first frame callback. */
3848 if (time == 0)
3849 input->cursor_anim_start = 0;
3850 else if (input->cursor_anim_start == 0)
3851 input->cursor_anim_start = time;
3852
Derek Foreman118a4292015-04-22 17:23:35 -05003853 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003854
Derek Foreman118a4292015-04-22 17:23:35 -05003855 if (time == 0 || input->cursor_anim_start == 0) {
3856 duration = 0;
3857 i = 0;
3858 } else
3859 i = wl_cursor_frame_and_duration(
3860 cursor,
3861 time - input->cursor_anim_start,
3862 &duration);
3863
3864 if (cursor->image_count > 1)
3865 schedule_pointer_image_update(input, cursor, duration,
3866 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003867
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003868 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003869}
3870
Derek Foreman118a4292015-04-22 17:23:35 -05003871static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003872cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003873{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003874 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003875 struct timespec tp;
3876 struct wl_cursor *cursor;
3877 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003878
3879 if (!input->cursor_timer_running)
3880 return;
3881
Derek Foreman118a4292015-04-22 17:23:35 -05003882 cursor = input->display->cursors[input->current_cursor];
3883 if (!cursor)
3884 return;
3885
3886 clock_gettime(CLOCK_MONOTONIC, &tp);
3887 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3888 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3889}
3890
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003891static const struct wl_callback_listener pointer_surface_listener = {
3892 pointer_surface_frame_callback
3893};
3894
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003895void
3896input_set_pointer_image(struct input *input, int pointer)
3897{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003898 int force = 0;
3899
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003900 if (!input->pointer)
3901 return;
3902
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003903 if (input->pointer_enter_serial > input->cursor_serial)
3904 force = 1;
3905
3906 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003907 return;
3908
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003909 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003910 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003911 if (!input->cursor_frame_cb)
3912 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003913 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003914 /* The current frame callback may be stuck if, for instance,
3915 * the set cursor request was processed by the server after
3916 * this client lost the focus. In this case the cursor surface
3917 * might not be mapped and the frame callback wouldn't ever
3918 * complete. Send a set_cursor and attach to try to map the
3919 * cursor surface again so that the callback will finish */
3920 input_set_pointer_image_index(input, 0);
3921 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003922}
3923
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003924struct wl_data_device *
3925input_get_data_device(struct input *input)
3926{
3927 return input->data_device;
3928}
3929
3930void
3931input_set_selection(struct input *input,
3932 struct wl_data_source *source, uint32_t time)
3933{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003934 if (input->data_device)
3935 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003936}
3937
3938void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003939input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003940{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003941 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003942 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003943}
3944
3945static void
3946offer_io_func(struct task *task, uint32_t events)
3947{
3948 struct data_offer *offer =
3949 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003950 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003951 unsigned int len;
3952 char buffer[4096];
3953
3954 len = read(offer->fd, buffer, sizeof buffer);
3955 offer->func(buffer, len,
3956 offer->x, offer->y, offer->user_data);
3957
3958 if (len == 0) {
Harish Krupo43152a32019-04-19 22:06:44 +05303959 if ((offer != offer->input->selection_offer) &&
3960 (display->data_device_manager_version >=
3961 WL_DATA_OFFER_FINISH_SINCE_VERSION))
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003962 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003963 close(offer->fd);
3964 data_offer_destroy(offer);
3965 }
3966}
3967
3968static void
3969data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3970 data_func_t func, void *user_data)
3971{
3972 int p[2];
3973
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003974 if (pipe2(p, O_CLOEXEC) == -1)
3975 return;
3976
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003977 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3978 close(p[1]);
3979
3980 offer->io_task.run = offer_io_func;
3981 offer->fd = p[0];
3982 offer->func = func;
3983 offer->refcount++;
3984 offer->user_data = user_data;
3985
3986 display_watch_fd(offer->input->display,
3987 offer->fd, EPOLLIN, &offer->io_task);
3988}
3989
3990void
3991input_receive_drag_data(struct input *input, const char *mime_type,
3992 data_func_t func, void *data)
3993{
3994 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003995 input->drag_offer->x = input->drag_x;
3996 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003997}
3998
3999int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004000input_receive_drag_data_to_fd(struct input *input,
4001 const char *mime_type, int fd)
4002{
4003 if (input->drag_offer)
4004 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4005
4006 return 0;
4007}
4008
4009int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004010input_receive_selection_data(struct input *input, const char *mime_type,
4011 data_func_t func, void *data)
4012{
4013 char **p;
4014
4015 if (input->selection_offer == NULL)
4016 return -1;
4017
4018 for (p = input->selection_offer->types.data; *p; p++)
4019 if (strcmp(mime_type, *p) == 0)
4020 break;
4021
4022 if (*p == NULL)
4023 return -1;
4024
4025 data_offer_receive_data(input->selection_offer,
4026 mime_type, func, data);
4027 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004028}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004029
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004030int
4031input_receive_selection_data_to_fd(struct input *input,
4032 const char *mime_type, int fd)
4033{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004034 if (input->selection_offer)
4035 wl_data_offer_receive(input->selection_offer->offer,
4036 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004037
4038 return 0;
4039}
4040
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004041void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004042window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004043{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004044 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004045 return;
4046
ant8mef99fac22018-11-28 22:46:37 +01004047 xdg_toplevel_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004048}
4049
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004050static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004051surface_set_synchronized(struct surface *surface)
4052{
4053 if (!surface->subsurface)
4054 return;
4055
4056 if (surface->synchronized)
4057 return;
4058
4059 wl_subsurface_set_sync(surface->subsurface);
4060 surface->synchronized = 1;
4061}
4062
4063static void
4064surface_set_synchronized_default(struct surface *surface)
4065{
4066 if (!surface->subsurface)
4067 return;
4068
4069 if (surface->synchronized == surface->synchronized_default)
4070 return;
4071
4072 if (surface->synchronized_default)
4073 wl_subsurface_set_sync(surface->subsurface);
4074 else
4075 wl_subsurface_set_desync(surface->subsurface);
4076
4077 surface->synchronized = surface->synchronized_default;
4078}
4079
4080static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004081surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004082{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004083 struct widget *widget = surface->widget;
4084 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004085
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004086 if (surface->input_region) {
4087 wl_region_destroy(surface->input_region);
4088 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004089 }
4090
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004091 if (surface->opaque_region)
4092 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004093
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004094 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004095
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004096 if (widget->resize_handler)
4097 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004098 widget->allocation.width,
4099 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004100 widget->user_data);
4101
Pekka Paalanen35e82632013-04-25 13:57:48 +03004102 if (surface->subsurface &&
4103 (surface->allocation.x != widget->allocation.x ||
4104 surface->allocation.y != widget->allocation.y)) {
4105 wl_subsurface_set_position(surface->subsurface,
4106 widget->allocation.x,
4107 widget->allocation.y);
4108 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004109 if (surface->allocation.width != widget->allocation.width ||
4110 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004111 window_schedule_redraw(widget->window);
4112 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004113 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004114
4115 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004116 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004117 widget->allocation.width,
4118 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004119}
4120
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004121static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004122window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004123{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004124 struct surface *surface;
4125
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004126 widget_set_allocation(window->main_surface->widget,
4127 window->pending_allocation.x,
4128 window->pending_allocation.y,
4129 window->pending_allocation.width,
4130 window->pending_allocation.height);
4131
4132 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004133
4134 /* The main surface is in the list, too. Main surface's
4135 * resize_handler is responsible for calling widget_set_allocation()
4136 * on all sub-surface root widgets, so they will be resized
4137 * properly.
4138 */
4139 wl_list_for_each(surface, &window->subsurface_list, link) {
4140 if (surface == window->main_surface)
4141 continue;
4142
4143 surface_set_synchronized(surface);
4144 surface_resize(surface);
4145 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004146
4147 if (!window->fullscreen && !window->maximized)
4148 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004149
4150 if (window->confined && window->confined_widget) {
4151 struct wl_compositor *compositor = window->display->compositor;
4152 struct wl_region *region;
4153 struct widget *widget = window->confined_widget;
4154
4155 region = wl_compositor_create_region(compositor);
4156 wl_region_add(region,
4157 widget->allocation.x,
4158 widget->allocation.y,
4159 widget->allocation.width,
4160 widget->allocation.height);
4161 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4162 region);
4163 wl_region_destroy(region);
4164 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004165}
4166
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004167static void
4168idle_resize(struct window *window)
4169{
4170 window->resize_needed = 0;
4171 window->redraw_needed = 1;
4172
4173 DBG("from %dx%d to %dx%d\n",
4174 window->main_surface->server_allocation.width,
4175 window->main_surface->server_allocation.height,
4176 window->pending_allocation.width,
4177 window->pending_allocation.height);
4178
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004179 window_do_resize(window);
4180}
4181
4182static void
4183undo_resize(struct window *window)
4184{
4185 window->pending_allocation.width =
4186 window->main_surface->server_allocation.width;
4187 window->pending_allocation.height =
4188 window->main_surface->server_allocation.height;
4189
4190 DBG("back to %dx%d\n",
4191 window->main_surface->server_allocation.width,
4192 window->main_surface->server_allocation.height);
4193
4194 window_do_resize(window);
4195
4196 if (window->pending_allocation.width == 0 &&
4197 window->pending_allocation.height == 0) {
4198 fprintf(stderr, "Error: Could not draw a surface, "
4199 "most likely due to insufficient disk space in "
4200 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4201 exit(EXIT_FAILURE);
4202 }
4203}
4204
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004205void
4206window_schedule_resize(struct window *window, int width, int height)
4207{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004208 /* We should probably get these numbers from the theme. */
4209 const int min_width = 200, min_height = 200;
4210
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004211 window->pending_allocation.x = 0;
4212 window->pending_allocation.y = 0;
4213 window->pending_allocation.width = width;
4214 window->pending_allocation.height = height;
4215
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004216 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004217 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004218 window->min_allocation.width = min_width;
4219 else
4220 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004221 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004222 window->min_allocation.height = min_height;
4223 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004224 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004225 }
4226
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004227 if (window->pending_allocation.width < window->min_allocation.width)
4228 window->pending_allocation.width = window->min_allocation.width;
4229 if (window->pending_allocation.height < window->min_allocation.height)
4230 window->pending_allocation.height = window->min_allocation.height;
4231
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004232 window->resize_needed = 1;
4233 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004234}
4235
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004236void
4237widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4238{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004239 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004240}
4241
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004242static int
4243window_get_shadow_margin(struct window *window)
4244{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004245 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004246 return frame_get_shadow_margin(window->frame->frame);
4247 else
4248 return 0;
4249}
4250
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004251void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004252window_inhibit_redraw(struct window *window)
4253{
4254 window->redraw_inhibited = 1;
4255 wl_list_remove(&window->redraw_task.link);
4256 wl_list_init(&window->redraw_task.link);
4257 window->redraw_task_scheduled = 0;
4258}
4259
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004260void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004261window_uninhibit_redraw(struct window *window)
4262{
4263 window->redraw_inhibited = 0;
4264 if (window->redraw_needed || window->resize_needed)
4265 window_schedule_redraw_task(window);
4266}
4267
4268static void
4269xdg_surface_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01004270 struct xdg_surface *xdg_surface,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004271 uint32_t serial)
4272{
4273 struct window *window = data;
4274
ant8mef99fac22018-11-28 22:46:37 +01004275 xdg_surface_ack_configure(window->xdg_surface, serial);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004276
4277 if (window->state_changed_handler)
4278 window->state_changed_handler(window, window->user_data);
4279
4280 window_uninhibit_redraw(window);
4281}
4282
ant8mef99fac22018-11-28 22:46:37 +01004283static const struct xdg_surface_listener xdg_surface_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004284 xdg_surface_handle_configure
4285};
4286
4287static void
ant8mef99fac22018-11-28 22:46:37 +01004288xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004289 int32_t width, int32_t height,
4290 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004291{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004292 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004293 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004294
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004295 window->maximized = 0;
4296 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004297 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004298 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004299
4300 wl_array_for_each(p, states) {
4301 uint32_t state = *p;
4302 switch (state) {
ant8mef99fac22018-11-28 22:46:37 +01004303 case XDG_TOPLEVEL_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004304 window->maximized = 1;
4305 break;
ant8mef99fac22018-11-28 22:46:37 +01004306 case XDG_TOPLEVEL_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004307 window->fullscreen = 1;
4308 break;
ant8mef99fac22018-11-28 22:46:37 +01004309 case XDG_TOPLEVEL_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004310 window->resizing = 1;
4311 break;
ant8mef99fac22018-11-28 22:46:37 +01004312 case XDG_TOPLEVEL_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004313 window->focused = 1;
4314 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004315 default:
4316 /* Unknown state */
4317 break;
4318 }
4319 }
4320
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004321 if (window->frame) {
4322 if (window->maximized) {
4323 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4324 } else {
4325 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4326 }
4327
4328 if (window->focused) {
4329 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4330 } else {
4331 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4332 }
4333 }
4334
Jasper St. Pierref184c382014-05-06 08:33:27 -04004335 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004336 /* The width / height params are for window geometry,
4337 * but window_schedule_resize takes allocation. Add
4338 * on the shadow margin to get the difference. */
4339 int margin = window_get_shadow_margin(window);
4340
4341 window_schedule_resize(window,
4342 width + margin * 2,
4343 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004344 } else if (window->saved_allocation.width > 0 &&
4345 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004346 window_schedule_resize(window,
4347 window->saved_allocation.width,
4348 window->saved_allocation.height);
4349 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004350}
4351
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004352static void
ant8mef99fac22018-11-28 22:46:37 +01004353xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004354{
4355 struct window *window = data;
4356 window_close(window);
4357}
4358
ant8mef99fac22018-11-28 22:46:37 +01004359static const struct xdg_toplevel_listener xdg_toplevel_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004360 xdg_toplevel_handle_configure,
4361 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004362};
4363
4364static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004365window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004366{
ant8mef99fac22018-11-28 22:46:37 +01004367 struct xdg_toplevel *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004368
4369 if (!window->xdg_surface)
4370 return;
4371
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004372 if (window->parent == window->last_parent)
4373 return;
4374
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004375 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004376 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004377 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004378 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004379
ant8mef99fac22018-11-28 22:46:37 +01004380 xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004381 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004382}
4383
4384static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004385window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004386{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004387 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004388 frame_input_rect(window->frame->frame,
4389 &geometry->x,
4390 &geometry->y,
4391 &geometry->width,
4392 &geometry->height);
4393 else
4394 window_get_allocation(window, geometry);
4395}
4396
4397static void
4398window_sync_geometry(struct window *window)
4399{
4400 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004401
4402 if (!window->xdg_surface)
4403 return;
4404
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004405 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004406 if (geometry.x == window->last_geometry.x &&
4407 geometry.y == window->last_geometry.y &&
4408 geometry.width == window->last_geometry.width &&
4409 geometry.height == window->last_geometry.height)
4410 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004411
ant8mef99fac22018-11-28 22:46:37 +01004412 xdg_surface_set_window_geometry(window->xdg_surface,
4413 geometry.x,
4414 geometry.y,
4415 geometry.width,
4416 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004417 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004418}
4419
4420static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004421window_flush(struct window *window)
4422{
4423 struct surface *surface;
4424
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004425 assert(!window->redraw_inhibited);
4426
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004427 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004428 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004429 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004430 if (window->xdg_toplevel)
4431 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004432 }
4433
4434 wl_list_for_each(surface, &window->subsurface_list, link) {
4435 if (surface == window->main_surface)
4436 continue;
4437
4438 surface_flush(surface);
4439 }
4440
4441 surface_flush(window->main_surface);
4442}
4443
4444static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004445menu_destroy(struct menu *menu)
4446{
4447 widget_destroy(menu->widget);
4448 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004449 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004450 free(menu);
4451}
4452
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004453void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004454window_get_allocation(struct window *window,
4455 struct rectangle *allocation)
4456{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004457 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004458}
4459
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004460static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004461widget_redraw(struct widget *widget)
4462{
4463 struct widget *child;
4464
4465 if (widget->redraw_handler)
4466 widget->redraw_handler(widget, widget->user_data);
4467 wl_list_for_each(child, &widget->child_list, link)
4468 widget_redraw(child);
4469}
4470
4471static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004472frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4473{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004474 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004475
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004476 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004477 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004478 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004479 surface->frame_cb = NULL;
4480
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004481 surface->last_time = time;
4482
Pekka Paalanen71233882013-04-25 13:57:53 +03004483 if (surface->redraw_needed || surface->window->redraw_needed) {
4484 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004485 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004486 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004487}
4488
4489static const struct wl_callback_listener listener = {
4490 frame_callback
4491};
4492
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004493static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004494surface_redraw(struct surface *surface)
4495{
Pekka Paalanen71233882013-04-25 13:57:53 +03004496 DBG_OBJ(surface->surface, "begin\n");
4497
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004498 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004499 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004500
4501 /* Whole-window redraw forces a redraw even if the previous has
4502 * not yet hit the screen.
4503 */
4504 if (surface->frame_cb) {
4505 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004506 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004507
Pekka Paalanen71233882013-04-25 13:57:53 +03004508 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004509 wl_callback_destroy(surface->frame_cb);
4510 }
4511
Neil Roberts97b747c2013-12-19 16:17:12 +00004512 if (surface->widget->use_cairo &&
4513 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004514 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004515 return -1;
4516 }
4517
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004518 surface->frame_cb = wl_surface_frame(surface->surface);
4519 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004520 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004521
4522 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004523 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004524 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004525 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004526 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004527}
4528
4529static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004530idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004531{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004532 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004533 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004534 int failed = 0;
4535 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004536
Pekka Paalanen71233882013-04-25 13:57:53 +03004537 DBG(" --------- \n");
4538
Pekka Paalaneneebff542013-04-25 13:57:52 +03004539 wl_list_init(&window->redraw_task.link);
4540 window->redraw_task_scheduled = 0;
4541
4542 if (window->resize_needed) {
4543 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004544 if (window->main_surface->frame_cb) {
4545 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004546 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004547 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004548
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004549 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004550 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004551 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004552
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004553 if (surface_redraw(window->main_surface) < 0) {
4554 /*
4555 * Only main_surface failure will cause us to undo the resize.
4556 * If sub-surfaces fail, they will just be broken with old
4557 * content.
4558 */
4559 failed = 1;
4560 } else {
4561 wl_list_for_each(surface, &window->subsurface_list, link) {
4562 if (surface == window->main_surface)
4563 continue;
4564
4565 surface_redraw(surface);
4566 }
4567 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004568
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004569 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004570 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004571
4572 wl_list_for_each(surface, &window->subsurface_list, link)
4573 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004574
4575 if (resized && failed) {
4576 /* Restore widget tree to correspond to what is on screen. */
4577 undo_resize(window);
4578 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004579}
4580
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004581static void
4582window_schedule_redraw_task(struct window *window)
4583{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004584 if (window->redraw_inhibited)
4585 return;
4586
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004587 if (!window->redraw_task_scheduled) {
4588 window->redraw_task.run = idle_redraw;
4589 display_defer(window->display, &window->redraw_task);
4590 window->redraw_task_scheduled = 1;
4591 }
4592}
4593
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004594void
4595window_schedule_redraw(struct window *window)
4596{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004597 struct surface *surface;
4598
Pekka Paalanen71233882013-04-25 13:57:53 +03004599 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4600
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004601 wl_list_for_each(surface, &window->subsurface_list, link)
4602 surface->redraw_needed = 1;
4603
4604 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004605}
4606
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004607int
4608window_is_fullscreen(struct window *window)
4609{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004610 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004611}
4612
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004613void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004614window_set_fullscreen(struct window *window, int fullscreen)
4615{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004616 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004617 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004618
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004619 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004620 return;
4621
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004622 if (fullscreen)
ant8mef99fac22018-11-28 22:46:37 +01004623 xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004624 else
ant8mef99fac22018-11-28 22:46:37 +01004625 xdg_toplevel_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004626}
4627
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004628int
4629window_is_maximized(struct window *window)
4630{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004631 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004632}
4633
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004634void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004635window_set_maximized(struct window *window, int maximized)
4636{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004637 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004638 return;
4639
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004640 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004641 return;
4642
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004643 if (maximized)
ant8mef99fac22018-11-28 22:46:37 +01004644 xdg_toplevel_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004645 else
ant8mef99fac22018-11-28 22:46:37 +01004646 xdg_toplevel_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004647}
4648
Jasper St. Pierrede680992014-04-10 17:23:49 -07004649int
4650window_is_resizing(struct window *window)
4651{
4652 return window->resizing;
4653}
4654
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004655void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004656window_set_minimized(struct window *window)
4657{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004658 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004659 return;
4660
ant8mef99fac22018-11-28 22:46:37 +01004661 xdg_toplevel_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004662}
4663
4664void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004665window_set_user_data(struct window *window, void *data)
4666{
4667 window->user_data = data;
4668}
4669
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004670void *
4671window_get_user_data(struct window *window)
4672{
4673 return window->user_data;
4674}
4675
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004676void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004677window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004678 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004679{
4680 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004681}
4682
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004683void
4684window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004685 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004686{
4687 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004688}
4689
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004690void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004691window_set_data_handler(struct window *window, window_data_handler_t handler)
4692{
4693 window->data_handler = handler;
4694}
4695
4696void
4697window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4698{
4699 window->drop_handler = handler;
4700}
4701
4702void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004703window_set_close_handler(struct window *window,
4704 window_close_handler_t handler)
4705{
4706 window->close_handler = handler;
4707}
4708
4709void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004710window_set_fullscreen_handler(struct window *window,
4711 window_fullscreen_handler_t handler)
4712{
4713 window->fullscreen_handler = handler;
4714}
4715
4716void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004717window_set_output_handler(struct window *window,
4718 window_output_handler_t handler)
4719{
4720 window->output_handler = handler;
4721}
4722
4723void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004724window_set_state_changed_handler(struct window *window,
4725 window_state_changed_handler_t handler)
4726{
4727 window->state_changed_handler = handler;
4728}
4729
4730void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004731window_set_pointer_locked_handler(struct window *window,
4732 locked_pointer_locked_handler_t locked,
4733 locked_pointer_unlocked_handler_t unlocked)
4734{
4735 window->pointer_unlocked_handler = unlocked;
4736 window->pointer_locked_handler = locked;
4737}
4738
4739void
4740window_set_pointer_confined_handler(struct window *window,
4741 confined_pointer_confined_handler_t confined,
4742 confined_pointer_unconfined_handler_t unconfined)
4743{
4744 window->pointer_confined_handler = confined;
4745 window->pointer_unconfined_handler = unconfined;
4746}
4747
4748void
4749window_set_locked_pointer_motion_handler(struct window *window,
4750 window_locked_pointer_motion_handler_t handler)
4751{
4752 window->locked_pointer_motion_handler = handler;
4753}
4754
4755void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004756window_set_title(struct window *window, const char *title)
4757{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004758 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004759 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004760 if (window->frame) {
4761 frame_set_title(window->frame->frame, title);
4762 widget_schedule_redraw(window->frame->widget);
4763 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004764 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01004765 xdg_toplevel_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004766}
4767
4768const char *
4769window_get_title(struct window *window)
4770{
4771 return window->title;
4772}
4773
4774void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004775window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4776{
4777 struct text_cursor_position *text_cursor_position =
4778 window->display->text_cursor_position;
4779
Scott Moreau9295ce02012-06-01 12:46:10 -06004780 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004781 return;
4782
4783 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004784 window->main_surface->surface,
4785 wl_fixed_from_int(x),
4786 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004787}
4788
Casey Dahlin9074db52012-04-19 22:50:09 -04004789static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004790relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4791 uint32_t utime_hi,
4792 uint32_t utime_lo,
4793 wl_fixed_t dx,
4794 wl_fixed_t dy,
4795 wl_fixed_t dx_unaccel,
4796 wl_fixed_t dy_unaccel)
4797{
4798 struct input *input = data;
4799 struct window *window = input->pointer_focus;
4800 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4801
4802 if (window->locked_pointer_motion_handler &&
4803 window->pointer_locked) {
4804 window->locked_pointer_motion_handler(
4805 window, input, ms,
4806 wl_fixed_to_double(dx),
4807 wl_fixed_to_double(dy),
4808 window->user_data);
4809 }
4810}
4811
4812static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4813 relative_pointer_handle_motion,
4814};
4815
4816static void
4817locked_pointer_locked(void *data,
4818 struct zwp_locked_pointer_v1 *locked_pointer)
4819{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004820 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004821 struct window *window = input->locked_window;
4822
4823 if (!window)
4824 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004825
4826 window->pointer_locked = true;
4827
4828 if (window->pointer_locked_handler) {
4829 window->pointer_locked_handler(window,
4830 input,
4831 window->user_data);
4832 }
4833}
4834
4835static void
4836locked_pointer_unlocked(void *data,
4837 struct zwp_locked_pointer_v1 *locked_pointer)
4838{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004839 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004840 struct window *window = input->locked_window;
4841
4842 if (!window)
4843 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004844
4845 window_unlock_pointer(window);
4846
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004847 input->locked_window = NULL;
4848
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004849 if (window->pointer_unlocked_handler) {
4850 window->pointer_unlocked_handler(window,
4851 input,
4852 window->user_data);
4853 }
4854}
4855
4856static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4857 locked_pointer_locked,
4858 locked_pointer_unlocked,
4859};
4860
4861int
4862window_lock_pointer(struct window *window, struct input *input)
4863{
4864 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4865 window->display->relative_pointer_manager;
4866 struct zwp_pointer_constraints_v1 *pointer_constraints =
4867 window->display->pointer_constraints;
4868 struct zwp_relative_pointer_v1 *relative_pointer;
4869 struct zwp_locked_pointer_v1 *locked_pointer;
4870
4871 if (!window->display->relative_pointer_manager)
4872 return -1;
4873
4874 if (!window->display->pointer_constraints)
4875 return -1;
4876
4877 if (window->locked_pointer)
4878 return -1;
4879
4880 if (window->confined_pointer)
4881 return -1;
4882
4883 if (!input->pointer)
4884 return -1;
4885
4886 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4887 relative_pointer_manager, input->pointer);
4888 zwp_relative_pointer_v1_add_listener(relative_pointer,
4889 &relative_pointer_listener,
4890 input);
4891
4892 locked_pointer =
4893 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4894 window->main_surface->surface,
4895 input->pointer,
4896 NULL,
4897 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4898 zwp_locked_pointer_v1_add_listener(locked_pointer,
4899 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004900 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004901
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004902 window->locked_pointer = locked_pointer;
4903 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004904 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004905
4906 return 0;
4907}
4908
4909void
4910window_unlock_pointer(struct window *window)
4911{
4912 if (!window->locked_pointer)
4913 return;
4914
4915 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4916 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4917 window->locked_pointer = NULL;
4918 window->relative_pointer = NULL;
4919 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004920}
4921
4922void
4923widget_set_locked_pointer_cursor_hint(struct widget *widget,
4924 float x, float y)
4925{
4926 struct window *window = widget->window;
4927
4928 if (!window->locked_pointer)
4929 return;
4930
4931 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4932 wl_fixed_from_double(x),
4933 wl_fixed_from_double(y));
4934 wl_surface_commit(window->main_surface->surface);
4935}
4936
4937static void
4938confined_pointer_confined(void *data,
4939 struct zwp_confined_pointer_v1 *confined_pointer)
4940{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004941 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004942 struct window *window = input->confined_window;
4943
4944 if (!window)
4945 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004946
4947 window->confined = true;
4948
4949 if (window->pointer_confined_handler) {
4950 window->pointer_confined_handler(window,
4951 input,
4952 window->user_data);
4953 }
4954}
4955
4956static void
4957confined_pointer_unconfined(void *data,
4958 struct zwp_confined_pointer_v1 *confined_pointer)
4959{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004960 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004961 struct window *window = input->confined_window;
4962
4963 if (!window)
4964 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004965
4966 window_unconfine_pointer(window);
4967
4968 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004969 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004970
4971 if (window->pointer_unconfined_handler) {
4972 window->pointer_unconfined_handler(window,
4973 input,
4974 window->user_data);
4975 }
4976}
4977
4978static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4979 confined_pointer_confined,
4980 confined_pointer_unconfined,
4981};
4982
4983int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004984window_confine_pointer_to_rectangles(struct window *window,
4985 struct input *input,
4986 struct rectangle *rectangles,
4987 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004988{
4989 struct zwp_pointer_constraints_v1 *pointer_constraints =
4990 window->display->pointer_constraints;
4991 struct zwp_confined_pointer_v1 *confined_pointer;
4992 struct wl_compositor *compositor = window->display->compositor;
4993 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004994 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004995
4996 if (!window->display->pointer_constraints)
4997 return -1;
4998
4999 if (window->locked_pointer)
5000 return -1;
5001
5002 if (window->confined_pointer)
5003 return -1;
5004
5005 if (!input->pointer)
5006 return -1;
5007
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005008 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005009 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005010 for (i = 0; i < num_rectangles; i++) {
5011 wl_region_add(region,
5012 rectangles[i].x,
5013 rectangles[i].y,
5014 rectangles[i].width,
5015 rectangles[i].height);
5016 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005017 }
5018
5019 confined_pointer =
5020 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5021 window->main_surface->surface,
5022 input->pointer,
5023 region,
5024 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5025 if (region)
5026 wl_region_destroy(region);
5027
5028 zwp_confined_pointer_v1_add_listener(confined_pointer,
5029 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08005030 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005031
5032 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005033 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005034 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005035
5036 return 0;
5037}
5038
5039void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005040window_update_confine_rectangles(struct window *window,
5041 struct rectangle *rectangles,
5042 int num_rectangles)
5043{
5044 struct wl_compositor *compositor = window->display->compositor;
5045 struct wl_region *region;
5046 int i;
5047
5048 region = wl_compositor_create_region(compositor);
5049 for (i = 0; i < num_rectangles; i++) {
5050 wl_region_add(region,
5051 rectangles[i].x,
5052 rectangles[i].y,
5053 rectangles[i].width,
5054 rectangles[i].height);
5055 }
5056
5057 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5058
5059 wl_region_destroy(region);
5060}
5061
5062int
5063window_confine_pointer_to_widget(struct window *window,
5064 struct widget *widget,
5065 struct input *input)
5066{
5067 int ret;
5068
5069 if (widget) {
5070 ret = window_confine_pointer_to_rectangles(window,
5071 input,
5072 &widget->allocation,
5073 1);
5074 window->confined_widget = widget;
5075 return ret;
5076 } else {
5077 return window_confine_pointer_to_rectangles(window,
5078 input,
5079 NULL,
5080 0);
5081 }
5082}
5083
5084void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005085window_unconfine_pointer(struct window *window)
5086{
5087 if (!window->confined_pointer)
5088 return;
5089
5090 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5091 window->confined_pointer = NULL;
5092 window->confined = false;
5093}
5094
5095static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005096surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005097 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005098{
Rob Bradford7507b572012-05-15 17:55:34 +01005099 struct window *window = data;
5100 struct output *output;
5101 struct output *output_found = NULL;
5102 struct window_output *window_output;
5103
5104 wl_list_for_each(output, &window->display->output_list, link) {
5105 if (output->output == wl_output) {
5106 output_found = output;
5107 break;
5108 }
5109 }
5110
5111 if (!output_found)
5112 return;
5113
Brian Lovinbc919262013-08-07 15:34:59 -07005114 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005115 window_output->output = output_found;
5116
5117 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005118
5119 if (window->output_handler)
5120 window->output_handler(window, output_found, 1,
5121 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005122}
5123
5124static void
5125surface_leave(void *data,
5126 struct wl_surface *wl_surface, struct wl_output *output)
5127{
Rob Bradford7507b572012-05-15 17:55:34 +01005128 struct window *window = data;
5129 struct window_output *window_output;
5130 struct window_output *window_output_found = NULL;
5131
5132 wl_list_for_each(window_output, &window->window_output_list, link) {
5133 if (window_output->output->output == output) {
5134 window_output_found = window_output;
5135 break;
5136 }
5137 }
5138
5139 if (window_output_found) {
5140 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005141
5142 if (window->output_handler)
5143 window->output_handler(window, window_output->output,
5144 0, window->user_data);
5145
Rob Bradford7507b572012-05-15 17:55:34 +01005146 free(window_output_found);
5147 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005148}
5149
5150static const struct wl_surface_listener surface_listener = {
5151 surface_enter,
5152 surface_leave
5153};
5154
Pekka Paalanen4e373742013-02-13 16:17:13 +02005155static struct surface *
5156surface_create(struct window *window)
5157{
5158 struct display *display = window->display;
5159 struct surface *surface;
5160
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005161 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005162 surface->window = window;
5163 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005164 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005165 wl_surface_add_listener(surface->surface, &surface_listener, window);
5166
Pekka Paalanen35e82632013-04-25 13:57:48 +03005167 wl_list_insert(&window->subsurface_list, &surface->link);
5168
Pekka Paalanen4e373742013-02-13 16:17:13 +02005169 return surface;
5170}
5171
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005172static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005173get_preferred_buffer_type(struct display *display)
5174{
5175#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005176 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005177 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5178#endif
5179
5180 return WINDOW_BUFFER_TYPE_SHM;
5181}
5182
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005183static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005184window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005185{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005186 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005187 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005188
Peter Huttererf3d62272013-08-08 11:57:05 +10005189 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005190 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005191 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005192
5193 surface = surface_create(window);
5194 window->main_surface = surface;
5195
Michael Teyfeld6371b52017-10-11 17:17:29 +02005196 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005197
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005198 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005199 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005200
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005201 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005202
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005203 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005204 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005205 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005206
Rob Bradford7507b572012-05-15 17:55:34 +01005207 wl_list_init (&window->window_output_list);
5208
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005209 return window;
5210}
5211
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005212struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005213window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005214{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005215 struct window *window;
5216
5217 window = window_create_internal(display, 0);
5218
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005219 if (window->display->xdg_shell) {
5220 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005221 xdg_wm_base_get_xdg_surface(window->display->xdg_shell,
5222 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005223 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005224
ant8mef99fac22018-11-28 22:46:37 +01005225 xdg_surface_add_listener(window->xdg_surface,
5226 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005227
5228 window->xdg_toplevel =
ant8mef99fac22018-11-28 22:46:37 +01005229 xdg_surface_get_toplevel(window->xdg_surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005230 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5231
ant8mef99fac22018-11-28 22:46:37 +01005232 xdg_toplevel_add_listener(window->xdg_toplevel,
5233 &xdg_toplevel_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005234
5235 window_inhibit_redraw(window);
5236
5237 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005238 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005239
5240 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005241}
5242
5243struct window *
5244window_create_custom(struct display *display)
5245{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005246 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005247}
5248
Jasper St. Pierre53686042013-12-09 15:26:25 -05005249void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005250window_set_parent(struct window *window,
5251 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005252{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005253 window->parent = parent_window;
5254 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005255}
5256
5257struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005258window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005259{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005260 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005261}
5262
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005263static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005264menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005265{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005266 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005267 int next;
5268
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005269 frame_interior(menu->frame, &x, &y, &width, &height);
5270 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005271 if (menu->current != next) {
5272 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005273 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005274 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005275}
5276
5277static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005278menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005279 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005280 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005281{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005282 struct menu *menu = data;
5283
5284 if (widget == menu->widget)
5285 menu_set_item(data, y);
5286
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005287 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005288}
5289
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005290static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005291menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005292 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005293{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005294 struct menu *menu = data;
5295
5296 if (widget == menu->widget)
5297 menu_set_item(data, y);
5298
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005299 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005300}
5301
5302static void
5303menu_leave_handler(struct widget *widget, struct input *input, void *data)
5304{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005305 struct menu *menu = data;
5306
5307 if (widget == menu->widget)
5308 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005309}
5310
5311static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005312menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005313 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005314 uint32_t button, enum wl_pointer_button_state state,
5315 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005316
5317{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005318 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005319
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005320 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5321 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005322 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005323 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005324 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005325 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005326 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005327 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005328 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005329 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005330}
5331
5332static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005333menu_touch_up_handler(struct widget *widget,
5334 struct input *input,
5335 uint32_t serial,
5336 uint32_t time,
5337 int32_t id,
5338 void *data)
5339{
5340 struct menu *menu = data;
5341
5342 input_ungrab(input);
5343 menu_destroy(menu);
5344}
5345
5346static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005347menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005348{
5349 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005350 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005351 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005352
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005353 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005354
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005355 frame_repaint(menu->frame, cr);
5356 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005357
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005358 theme_set_background_source(menu->window->display->theme,
5359 cr, THEME_FRAME_ACTIVE);
5360 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005361 cairo_fill(cr);
5362
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005363 cairo_select_font_face(cr, "sans",
5364 CAIRO_FONT_SLANT_NORMAL,
5365 CAIRO_FONT_WEIGHT_NORMAL);
5366 cairo_set_font_size(cr, 12);
5367
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005368 for (i = 0; i < menu->count; i++) {
5369 if (i == menu->current) {
5370 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005371 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005372 cairo_fill(cr);
5373 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005374 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005375 cairo_show_text(cr, menu->entries[i]);
5376 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005377 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5378 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005379 cairo_show_text(cr, menu->entries[i]);
5380 }
5381 }
5382
5383 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005384}
5385
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005386static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005387xdg_popup_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01005388 struct xdg_popup *xdg_popup,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005389 int32_t x,
5390 int32_t y,
5391 int32_t width,
5392 int32_t height)
5393{
5394}
5395
5396static void
ant8mef99fac22018-11-28 22:46:37 +01005397xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005398{
5399 struct window *window = data;
5400 struct menu *menu = window->main_surface->widget->user_data;
5401
5402 input_ungrab(menu->input);
5403 menu_destroy(menu);
5404}
5405
ant8mef99fac22018-11-28 22:46:37 +01005406static const struct xdg_popup_listener xdg_popup_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005407 xdg_popup_handle_configure,
5408 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005409};
5410
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005411static struct menu *
5412create_menu(struct display *display,
5413 struct input *input, uint32_t time,
5414 menu_func_t func, const char **entries, int count,
5415 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005416{
5417 struct window *window;
5418 struct menu *menu;
5419
5420 menu = malloc(sizeof *menu);
5421 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005422 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005423
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005424 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005425 if (!window) {
5426 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005427 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005428 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005429
5430 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005431 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005432 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005433 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005434 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005435 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005436 menu->entries = entries;
5437 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005438 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005439 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005440 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005441 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005442 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005443
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005444 input_ungrab(input);
5445
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005446 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005447 widget_set_enter_handler(menu->widget, menu_enter_handler);
5448 widget_set_leave_handler(menu->widget, menu_leave_handler);
5449 widget_set_motion_handler(menu->widget, menu_motion_handler);
5450 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005451 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005452
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005453 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005454 frame_resize_inside(menu->frame, 200, count * 20);
5455 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5456 window_schedule_resize(window, frame_width(menu->frame),
5457 frame_height(menu->frame));
5458
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005459 return menu;
5460}
5461
ant8mef99fac22018-11-28 22:46:37 +01005462static struct xdg_positioner *
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005463create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005464 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005465{
ant8mef99fac22018-11-28 22:46:37 +01005466 struct xdg_positioner *positioner;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005467
ant8mef99fac22018-11-28 22:46:37 +01005468 positioner = xdg_wm_base_create_positioner(display->xdg_shell);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005469 fail_on_null(positioner, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005470 xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1);
5471 xdg_positioner_set_size(positioner, w, h);
5472 xdg_positioner_set_anchor(positioner,
5473 XDG_POSITIONER_ANCHOR_TOP_LEFT);
5474 xdg_positioner_set_gravity(positioner,
5475 XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005476
5477 return positioner;
5478}
5479
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005480void
5481window_show_menu(struct display *display,
5482 struct input *input, uint32_t time, struct window *parent,
5483 int32_t x, int32_t y,
5484 menu_func_t func, const char **entries, int count)
5485{
5486 struct menu *menu;
5487 struct window *window;
5488 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005489 struct rectangle parent_geometry;
ant8mef99fac22018-11-28 22:46:37 +01005490 struct xdg_positioner *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005491
5492 menu = create_menu(display, input, time, func, entries, count, parent);
5493
5494 if (menu == NULL)
5495 return;
5496
5497 window = menu->window;
5498
5499 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5500 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5501
5502 window->x = x;
5503 window->y = y;
5504
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005505 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005506 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005507
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005508 if (!display->xdg_shell)
5509 return;
5510
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005511 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005512 xdg_wm_base_get_xdg_surface(display->xdg_shell,
5513 window->main_surface->surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005514 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005515
ant8mef99fac22018-11-28 22:46:37 +01005516 xdg_surface_add_listener(window->xdg_surface,
5517 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005518
5519 positioner = create_simple_positioner(display,
5520 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005521 window->y - (iy + parent_geometry.y),
5522 frame_width(menu->frame),
5523 frame_height(menu->frame));
ant8mef99fac22018-11-28 22:46:37 +01005524 window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
5525 parent->xdg_surface,
5526 positioner);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005527 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005528 xdg_positioner_destroy(positioner);
5529 xdg_popup_grab(window->xdg_popup, input->seat,
5530 display_get_serial(window->display));
5531 xdg_popup_add_listener(window->xdg_popup,
5532 &xdg_popup_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005533
5534 window_inhibit_redraw(window);
5535
5536 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005537}
5538
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005539void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005540window_set_buffer_type(struct window *window, enum window_buffer_type type)
5541{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005542 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005543}
5544
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005545enum window_buffer_type
5546window_get_buffer_type(struct window *window)
5547{
5548 return window->main_surface->buffer_type;
5549}
5550
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005551void
5552window_set_preferred_format(struct window *window,
5553 enum preferred_format format)
5554{
5555 window->preferred_format = format;
5556}
5557
Pekka Paalanen35e82632013-04-25 13:57:48 +03005558struct widget *
5559window_add_subsurface(struct window *window, void *data,
5560 enum subsurface_mode default_mode)
5561{
5562 struct widget *widget;
5563 struct surface *surface;
5564 struct wl_surface *parent;
5565 struct wl_subcompositor *subcompo = window->display->subcompositor;
5566
Pekka Paalanen35e82632013-04-25 13:57:48 +03005567 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005568 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005569 widget = widget_create(window, surface, data);
5570 wl_list_init(&widget->link);
5571 surface->widget = widget;
5572
5573 parent = window->main_surface->surface;
5574 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5575 surface->surface,
5576 parent);
5577 surface->synchronized = 1;
5578
5579 switch (default_mode) {
5580 case SUBSURFACE_SYNCHRONIZED:
5581 surface->synchronized_default = 1;
5582 break;
5583 case SUBSURFACE_DESYNCHRONIZED:
5584 surface->synchronized_default = 0;
5585 break;
5586 default:
5587 assert(!"bad enum subsurface_mode");
5588 }
5589
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005590 window->resize_needed = 1;
5591 window_schedule_redraw(window);
5592
Pekka Paalanen35e82632013-04-25 13:57:48 +03005593 return widget;
5594}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005595
5596static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005597display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005598 struct wl_output *wl_output,
5599 int x, int y,
5600 int physical_width,
5601 int physical_height,
5602 int subpixel,
5603 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005604 const char *model,
5605 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005606{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005607 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005608
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005609 output->allocation.x = x;
5610 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005611 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005612
5613 if (output->make)
5614 free(output->make);
5615 output->make = strdup(make);
5616
5617 if (output->model)
5618 free(output->model);
5619 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005620}
5621
5622static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005623display_handle_done(void *data,
5624 struct wl_output *wl_output)
5625{
5626}
5627
5628static void
5629display_handle_scale(void *data,
5630 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005631 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005632{
5633 struct output *output = data;
5634
5635 output->scale = scale;
5636}
5637
5638static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005639display_handle_mode(void *data,
5640 struct wl_output *wl_output,
5641 uint32_t flags,
5642 int width,
5643 int height,
5644 int refresh)
5645{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005646 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005647 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005648
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005649 if (flags & WL_OUTPUT_MODE_CURRENT) {
5650 output->allocation.width = width;
5651 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005652 if (display->output_configure_handler)
5653 (*display->output_configure_handler)(
5654 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005655 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005656}
5657
5658static const struct wl_output_listener output_listener = {
5659 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005660 display_handle_mode,
5661 display_handle_done,
5662 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005663};
5664
5665static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005666display_add_output(struct display *d, uint32_t id)
5667{
5668 struct output *output;
5669
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005670 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005671 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005672 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005673 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005674 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005675 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005676 wl_list_insert(d->output_list.prev, &output->link);
5677
5678 wl_output_add_listener(output->output, &output_listener, output);
5679}
5680
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005681static void
5682output_destroy(struct output *output)
5683{
5684 if (output->destroy_handler)
5685 (*output->destroy_handler)(output, output->user_data);
5686
5687 wl_output_destroy(output->output);
5688 wl_list_remove(&output->link);
5689 free(output);
5690}
5691
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005692static void
5693display_destroy_output(struct display *d, uint32_t id)
5694{
5695 struct output *output;
5696
5697 wl_list_for_each(output, &d->output_list, link) {
5698 if (output->server_output_id == id) {
5699 output_destroy(output);
5700 break;
5701 }
5702 }
5703}
5704
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005705void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005706display_set_global_handler(struct display *display,
5707 display_global_handler_t handler)
5708{
5709 struct global *global;
5710
5711 display->global_handler = handler;
5712 if (!handler)
5713 return;
5714
5715 wl_list_for_each(global, &display->global_list, link)
5716 display->global_handler(display,
5717 global->name, global->interface,
5718 global->version, display->user_data);
5719}
5720
5721void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005722display_set_global_handler_remove(struct display *display,
5723 display_global_handler_t remove_handler)
5724{
5725 display->global_handler_remove = remove_handler;
5726 if (!remove_handler)
5727 return;
5728}
5729
5730void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005731display_set_output_configure_handler(struct display *display,
5732 display_output_handler_t handler)
5733{
5734 struct output *output;
5735
5736 display->output_configure_handler = handler;
5737 if (!handler)
5738 return;
5739
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005740 wl_list_for_each(output, &display->output_list, link) {
5741 if (output->allocation.width == 0 &&
5742 output->allocation.height == 0)
5743 continue;
5744
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005745 (*display->output_configure_handler)(output,
5746 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005747 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005748}
5749
5750void
5751output_set_user_data(struct output *output, void *data)
5752{
5753 output->user_data = data;
5754}
5755
5756void *
5757output_get_user_data(struct output *output)
5758{
5759 return output->user_data;
5760}
5761
5762void
5763output_set_destroy_handler(struct output *output,
5764 display_output_handler_t handler)
5765{
5766 output->destroy_handler = handler;
5767 /* FIXME: implement this, once we have way to remove outputs */
5768}
5769
5770void
Scott Moreau4e072362012-09-29 02:03:11 -06005771output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005772{
Scott Moreau4e072362012-09-29 02:03:11 -06005773 struct rectangle allocation = output->allocation;
5774
5775 switch (output->transform) {
5776 case WL_OUTPUT_TRANSFORM_90:
5777 case WL_OUTPUT_TRANSFORM_270:
5778 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5779 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5780 /* Swap width and height */
5781 allocation.width = output->allocation.height;
5782 allocation.height = output->allocation.width;
5783 break;
5784 }
5785
5786 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005787}
5788
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005789struct wl_output *
5790output_get_wl_output(struct output *output)
5791{
5792 return output->output;
5793}
5794
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005795enum wl_output_transform
5796output_get_transform(struct output *output)
5797{
5798 return output->transform;
5799}
5800
Alexander Larssonafd319a2013-05-22 14:41:27 +02005801uint32_t
5802output_get_scale(struct output *output)
5803{
5804 return output->scale;
5805}
5806
Jason Ekstrand738715d2014-04-02 19:53:50 -05005807const char *
5808output_get_make(struct output *output)
5809{
5810 return output->make;
5811}
5812
5813const char *
5814output_get_model(struct output *output)
5815{
5816 return output->model;
5817}
5818
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005819static void
Daniel Stone97f68542012-05-30 16:32:01 +01005820fini_xkb(struct input *input)
5821{
5822 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005823 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005824}
5825
5826static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005827display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005828{
5829 struct input *input;
Sebastian Wickac1b92d2019-11-01 02:27:55 +01005830 int seat_version = MIN(display_seat_version, 7);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005831
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005832 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005833 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005834 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005835 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005836 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005837 input->pointer_focus = NULL;
5838 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005839 input->seat_version = seat_version;
5840
Rusty Lynch041815a2013-08-08 21:20:38 -07005841 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005842 wl_list_insert(d->input_list.prev, &input->link);
5843
Daniel Stone37816df2012-05-16 18:45:18 +01005844 wl_seat_add_listener(input->seat, &seat_listener, input);
5845 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005846
Jason Ekstranda669bd52014-04-02 19:53:51 -05005847 if (d->data_device_manager) {
5848 input->data_device =
5849 wl_data_device_manager_get_data_device(d->data_device_manager,
5850 input->seat);
5851 wl_data_device_add_listener(input->data_device,
5852 &data_device_listener,
5853 input);
5854 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005855
5856 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005857
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005858 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5859 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005860
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005861 set_repeat_info(input, 40, 400);
5862 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5863 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005864}
5865
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005866static void
Harish Krupoee4c7a22019-04-19 01:53:27 +05305867display_add_data_device(struct display *d, uint32_t id, int ddm_version)
5868{
5869 struct input *input;
5870
5871 d->data_device_manager_version = MIN(ddm_version, 3);
5872 d->data_device_manager =
5873 wl_registry_bind(d->registry, id,
5874 &wl_data_device_manager_interface,
5875 d->data_device_manager_version);
5876
5877 wl_list_for_each(input, &d->input_list, link) {
5878 if (!input->data_device) {
5879 input->data_device =
5880 wl_data_device_manager_get_data_device(d->data_device_manager,
5881 input->seat);
5882 wl_data_device_add_listener(input->data_device,
5883 &data_device_listener,
5884 input);
5885 }
5886 }
5887}
5888
5889static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005890input_destroy(struct input *input)
5891{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005892 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005893 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005894
5895 if (input->drag_offer)
5896 data_offer_destroy(input->drag_offer);
5897
5898 if (input->selection_offer)
5899 data_offer_destroy(input->selection_offer);
5900
kabeer khan6ce67ec2014-10-20 11:55:29 +05305901 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005902 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305903 wl_data_device_release(input->data_device);
5904 else
5905 wl_data_device_destroy(input->data_device);
5906 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005907 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005908 if (input->touch)
5909 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005910 if (input->pointer)
5911 wl_pointer_release(input->pointer);
5912 if (input->keyboard)
5913 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005914 } else {
5915 if (input->touch)
5916 wl_touch_destroy(input->touch);
5917 if (input->pointer)
5918 wl_pointer_destroy(input->pointer);
5919 if (input->keyboard)
5920 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005921 }
5922
Daniel Stone97f68542012-05-30 16:32:01 +01005923 fini_xkb(input);
5924
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005925 wl_surface_destroy(input->pointer_surface);
5926
Pekka Paalanene1207c72011-12-16 12:02:09 +02005927 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005928 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005929 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005930 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005931 free(input);
5932}
5933
5934static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005935shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5936{
5937 struct display *d = data;
5938
5939 if (format == WL_SHM_FORMAT_RGB565)
5940 d->has_rgb565 = 1;
5941}
5942
5943struct wl_shm_listener shm_listener = {
5944 shm_format
5945};
5946
5947static void
ant8mef99fac22018-11-28 22:46:37 +01005948xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005949{
ant8mef99fac22018-11-28 22:46:37 +01005950 xdg_wm_base_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005951}
5952
ant8mef99fac22018-11-28 22:46:37 +01005953static const struct xdg_wm_base_listener wm_base_listener = {
5954 xdg_wm_base_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005955};
5956
5957static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005958registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5959 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005960{
5961 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005962 struct global *global;
5963
Brian Lovinbc919262013-08-07 15:34:59 -07005964 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005965 global->name = id;
5966 global->interface = strdup(interface);
5967 global->version = version;
5968 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005969
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005970 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005971 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005972 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005973 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005974 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005975 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005976 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005977 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5978 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5979 d->relative_pointer_manager =
5980 wl_registry_bind(registry, id,
5981 &zwp_relative_pointer_manager_v1_interface,
5982 1);
5983 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5984 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5985 d->pointer_constraints =
5986 wl_registry_bind(registry, id,
5987 &zwp_pointer_constraints_v1_interface,
5988 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005989 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005990 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005991 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005992 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Harish Krupoee4c7a22019-04-19 01:53:27 +05305993 display_add_data_device(d, id, version);
ant8mef99fac22018-11-28 22:46:37 +01005994 } else if (strcmp(interface, "xdg_wm_base") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005995 d->xdg_shell = wl_registry_bind(registry, id,
ant8mef99fac22018-11-28 22:46:37 +01005996 &xdg_wm_base_interface, 1);
5997 xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005998 } else if (strcmp(interface, "text_cursor_position") == 0) {
5999 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006000 wl_registry_bind(registry, id,
6001 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006002 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6003 d->subcompositor =
6004 wl_registry_bind(registry, id,
6005 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006006 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006007
6008 if (d->global_handler)
6009 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006010}
6011
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006012static void
6013registry_handle_global_remove(void *data, struct wl_registry *registry,
6014 uint32_t name)
6015{
6016 struct display *d = data;
6017 struct global *global;
6018 struct global *tmp;
6019
6020 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6021 if (global->name != name)
6022 continue;
6023
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006024 if (strcmp(global->interface, "wl_output") == 0)
6025 display_destroy_output(d, name);
6026
6027 /* XXX: Should destroy remaining bound globals */
6028
6029 if (d->global_handler_remove)
6030 d->global_handler_remove(d, name, global->interface,
6031 global->version, d->user_data);
6032
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006033 wl_list_remove(&global->link);
6034 free(global->interface);
6035 free(global);
6036 }
6037}
6038
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006039void *
6040display_bind(struct display *display, uint32_t name,
6041 const struct wl_interface *interface, uint32_t version)
6042{
6043 return wl_registry_bind(display->registry, name, interface, version);
6044}
6045
6046static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006047 registry_handle_global,
6048 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006049};
6050
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006051#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006052static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006053init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006054{
6055 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006056 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006057
Rob Clark6396ed32012-03-11 19:48:41 -05006058#ifdef USE_CAIRO_GLESV2
6059# define GL_BIT EGL_OPENGL_ES2_BIT
6060#else
6061# define GL_BIT EGL_OPENGL_BIT
6062#endif
6063
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006064 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006065 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006066 EGL_RED_SIZE, 1,
6067 EGL_GREEN_SIZE, 1,
6068 EGL_BLUE_SIZE, 1,
6069 EGL_ALPHA_SIZE, 1,
6070 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006071 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006072 EGL_NONE
6073 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006074
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006075#ifdef USE_CAIRO_GLESV2
6076 static const EGLint context_attribs[] = {
6077 EGL_CONTEXT_CLIENT_VERSION, 2,
6078 EGL_NONE
6079 };
6080 EGLint api = EGL_OPENGL_ES_API;
6081#else
6082 EGLint *context_attribs = NULL;
6083 EGLint api = EGL_OPENGL_API;
6084#endif
6085
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006086 d->dpy =
6087 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6088 d->display, NULL);
6089
Yuval Fledel45568f62010-12-06 09:18:12 -05006090 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006091 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006092 return -1;
6093 }
6094
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006095 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006096 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006097 return -1;
6098 }
6099
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006100 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6101 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006102 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006103 return -1;
6104 }
6105
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006106 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006107 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006108 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006109 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006110 return -1;
6111 }
6112
Benjamin Franzke0c991632011-09-27 21:57:31 +02006113 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6114 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006115 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006116 return -1;
6117 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006118
6119 return 0;
6120}
6121
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006122static void
6123fini_egl(struct display *display)
6124{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006125 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006126
6127 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6128 EGL_NO_CONTEXT);
6129
6130 eglTerminate(display->dpy);
6131 eglReleaseThread();
6132}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006133#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006134
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006135static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006136init_dummy_surface(struct display *display)
6137{
6138 int len;
6139 void *data;
6140
6141 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006142 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006143 display->dummy_surface =
6144 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6145 1, 1, len);
6146 display->dummy_surface_data = data;
6147}
6148
6149static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006150handle_display_data(struct task *task, uint32_t events)
6151{
6152 struct display *display =
6153 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006154 struct epoll_event ep;
6155 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006156
6157 display->display_fd_events = events;
6158
6159 if (events & EPOLLERR || events & EPOLLHUP) {
6160 display_exit(display);
6161 return;
6162 }
6163
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006164 if (events & EPOLLIN) {
6165 ret = wl_display_dispatch(display->display);
6166 if (ret == -1) {
6167 display_exit(display);
6168 return;
6169 }
6170 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006171
6172 if (events & EPOLLOUT) {
6173 ret = wl_display_flush(display->display);
6174 if (ret == 0) {
6175 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6176 ep.data.ptr = &display->display_task;
6177 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6178 display->display_fd, &ep);
6179 } else if (ret == -1 && errno != EAGAIN) {
6180 display_exit(display);
6181 return;
6182 }
6183 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006184}
6185
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006186static void
6187log_handler(const char *format, va_list args)
6188{
6189 vfprintf(stderr, format, args);
6190}
6191
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006192struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006193display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006194{
6195 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006196
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006197 wl_log_set_handler_client(log_handler);
6198
Peter Huttererf3d62272013-08-08 11:57:05 +10006199 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006200 if (d == NULL)
6201 return NULL;
6202
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006203 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006204 if (d->display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02006205 fprintf(stderr, "failed to connect to Wayland display: %s\n",
6206 strerror(errno));
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006207 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006208 return NULL;
6209 }
6210
Rob Bradford5ab9c752013-07-26 16:29:43 +01006211 d->xkb_context = xkb_context_new(0);
6212 if (d->xkb_context == NULL) {
6213 fprintf(stderr, "Failed to create XKB context\n");
6214 free(d);
6215 return NULL;
6216 }
6217
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006218 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006219 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006220 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006221 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6222 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006223
6224 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006225 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006226 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006227 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006228
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006229 d->registry = wl_display_get_registry(d->display);
6230 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006231
Marek Chalupaa519d062014-12-05 13:49:40 +01006232 if (wl_display_roundtrip(d->display) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006233 fprintf(stderr, "Failed to process Wayland connection: %s\n",
6234 strerror(errno));
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006235 return NULL;
6236 }
6237
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006238#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006239 if (init_egl(d) < 0)
6240 fprintf(stderr, "EGL does not seem to work, "
6241 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006242#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006243
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006244 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006245
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006246 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006247
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006248 wl_list_init(&d->window_list);
6249
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006250 init_dummy_surface(d);
6251
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006252 return d;
6253}
6254
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006255static void
6256display_destroy_outputs(struct display *display)
6257{
6258 struct output *tmp;
6259 struct output *output;
6260
6261 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6262 output_destroy(output);
6263}
6264
Pekka Paalanene1207c72011-12-16 12:02:09 +02006265static void
6266display_destroy_inputs(struct display *display)
6267{
6268 struct input *tmp;
6269 struct input *input;
6270
6271 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6272 input_destroy(input);
6273}
6274
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006275void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006276display_destroy(struct display *display)
6277{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006278 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006279 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6280 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006281
6282 if (!wl_list_empty(&display->deferred_list))
6283 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6284
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006285 cairo_surface_destroy(display->dummy_surface);
6286 free(display->dummy_surface_data);
6287
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006288 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006289 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006290
Daniel Stone97f68542012-05-30 16:32:01 +01006291 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006292
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006293 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006294 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006295
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006296#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006297 if (display->argb_device)
6298 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006299#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006300
Pekka Paalanen35e82632013-04-25 13:57:48 +03006301 if (display->subcompositor)
6302 wl_subcompositor_destroy(display->subcompositor);
6303
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006304 if (display->xdg_shell)
ant8mef99fac22018-11-28 22:46:37 +01006305 xdg_wm_base_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006306
6307 if (display->shm)
6308 wl_shm_destroy(display->shm);
6309
6310 if (display->data_device_manager)
6311 wl_data_device_manager_destroy(display->data_device_manager);
6312
6313 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006314 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006315
6316 close(display->epoll_fd);
6317
U. Artie Eoff44874d92012-10-02 21:12:35 -07006318 if (!(display->display_fd_events & EPOLLERR) &&
6319 !(display->display_fd_events & EPOLLHUP))
6320 wl_display_flush(display->display);
6321
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006322 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006323 free(display);
6324}
6325
6326void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006327display_set_user_data(struct display *display, void *data)
6328{
6329 display->user_data = data;
6330}
6331
6332void *
6333display_get_user_data(struct display *display)
6334{
6335 return display->user_data;
6336}
6337
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006338struct wl_display *
6339display_get_display(struct display *display)
6340{
6341 return display->display;
6342}
6343
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006344int
6345display_has_subcompositor(struct display *display)
6346{
6347 if (display->subcompositor)
6348 return 1;
6349
6350 wl_display_roundtrip(display->display);
6351
6352 return display->subcompositor != NULL;
6353}
6354
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006355cairo_device_t *
6356display_get_cairo_device(struct display *display)
6357{
6358 return display->argb_device;
6359}
6360
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006361struct output *
6362display_get_output(struct display *display)
6363{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006364 if (wl_list_empty(&display->output_list))
6365 return NULL;
6366
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006367 return container_of(display->output_list.next, struct output, link);
6368}
6369
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006370struct wl_compositor *
6371display_get_compositor(struct display *display)
6372{
6373 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006374}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006375
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006376uint32_t
6377display_get_serial(struct display *display)
6378{
6379 return display->serial;
6380}
6381
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006382EGLDisplay
6383display_get_egl_display(struct display *d)
6384{
6385 return d->dpy;
6386}
6387
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006388struct wl_data_source *
6389display_create_data_source(struct display *display)
6390{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006391 if (display->data_device_manager)
6392 return wl_data_device_manager_create_data_source(display->data_device_manager);
6393 else
6394 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006395}
6396
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006397EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006398display_get_argb_egl_config(struct display *d)
6399{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006400 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006401}
6402
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006403int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006404display_acquire_window_surface(struct display *display,
6405 struct window *window,
6406 EGLContext ctx)
6407{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006408 struct surface *surface = window->main_surface;
6409
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006410 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006411 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006412
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006413 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006414 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006415}
6416
6417void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006418display_release_window_surface(struct display *display,
6419 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006420{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006421 struct surface *surface = window->main_surface;
6422
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006423 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006424 return;
6425
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006426 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006427}
6428
6429void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006430display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006431{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006432 wl_list_insert(&display->deferred_list, &task->link);
6433}
6434
6435void
6436display_watch_fd(struct display *display,
6437 int fd, uint32_t events, struct task *task)
6438{
6439 struct epoll_event ep;
6440
6441 ep.events = events;
6442 ep.data.ptr = task;
6443 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6444}
6445
6446void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006447display_unwatch_fd(struct display *display, int fd)
6448{
6449 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6450}
6451
6452void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006453display_run(struct display *display)
6454{
6455 struct task *task;
6456 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006457 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006458
Pekka Paalanen826d7952011-12-15 10:14:07 +02006459 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006460 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006461 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006462 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006463 struct task, link);
6464 wl_list_remove(&task->link);
6465 task->run(task, 0);
6466 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006467
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006468 wl_display_dispatch_pending(display->display);
6469
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006470 if (!display->running)
6471 break;
6472
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006473 ret = wl_display_flush(display->display);
6474 if (ret < 0 && errno == EAGAIN) {
6475 ep[0].events =
6476 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6477 ep[0].data.ptr = &display->display_task;
6478
6479 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6480 display->display_fd, &ep[0]);
6481 } else if (ret < 0) {
6482 break;
6483 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006484
6485 count = epoll_wait(display->epoll_fd,
6486 ep, ARRAY_LENGTH(ep), -1);
6487 for (i = 0; i < count; i++) {
6488 task = ep[i].data.ptr;
6489 task->run(task, ep[i].events);
6490 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006491 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006492}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006493
6494void
6495display_exit(struct display *display)
6496{
6497 display->running = 0;
6498}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006499
Carlos Garnacho9c931792016-01-18 23:52:12 +01006500int
6501display_get_data_device_manager_version(struct display *display)
6502{
6503 return display->data_device_manager_version;
6504}
6505
Jan Arne Petersencd997062012-11-18 19:06:44 +01006506void
6507keysym_modifiers_add(struct wl_array *modifiers_map,
6508 const char *name)
6509{
6510 size_t len = strlen(name) + 1;
6511 char *p;
6512
6513 p = wl_array_add(modifiers_map, len);
6514
6515 if (p == NULL)
6516 return;
6517
6518 strncpy(p, name, len);
6519}
6520
6521static xkb_mod_index_t
6522keysym_modifiers_get_index(struct wl_array *modifiers_map,
6523 const char *name)
6524{
6525 xkb_mod_index_t index = 0;
6526 char *p = modifiers_map->data;
6527
6528 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6529 if (strcmp(p, name) == 0)
6530 return index;
6531
6532 index++;
6533 p += strlen(p) + 1;
6534 }
6535
6536 return XKB_MOD_INVALID;
6537}
6538
6539xkb_mod_mask_t
6540keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6541 const char *name)
6542{
6543 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6544
6545 if (index == XKB_MOD_INVALID)
6546 return XKB_MOD_INVALID;
6547
6548 return 1 << index;
6549}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006550
6551static void
6552toytimer_fire(struct task *tsk, uint32_t events)
6553{
6554 uint64_t e;
6555 struct toytimer *tt;
6556
6557 tt = container_of(tsk, struct toytimer, tsk);
6558
6559 if (events != EPOLLIN)
6560 fprintf(stderr, "unexpected timerfd events %x\n", events);
6561
6562 if (!(events & EPOLLIN))
6563 return;
6564
6565 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6566 /* If we change the timer between the fd becoming
6567 * readable and getting here, there'll be nothing to
6568 * read and we get EAGAIN. */
6569 if (errno != EAGAIN)
Antonio Borneo39578632019-04-26 23:57:31 +02006570 fprintf(stderr, "timer read failed: %s\n",
6571 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006572 return;
6573 }
6574
6575 tt->callback(tt);
6576}
6577
6578void
6579toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6580 toytimer_cb callback)
6581{
6582 memset(tt, 0, sizeof *tt);
6583
6584 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6585 if (tt->fd == -1) {
Antonio Borneo39578632019-04-26 23:57:31 +02006586 fprintf(stderr, "creating timer failed: %s\n",
6587 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006588 abort();
6589 }
6590
6591 tt->display = display;
6592 tt->callback = callback;
6593 tt->tsk.run = toytimer_fire;
6594 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6595}
6596
6597void
6598toytimer_fini(struct toytimer *tt)
6599{
6600 display_unwatch_fd(tt->display, tt->fd);
6601 close(tt->fd);
6602 tt->fd = -1;
6603}
6604
6605void
6606toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6607{
6608 int ret;
6609
6610 ret = timerfd_settime(tt->fd, 0, its, NULL);
6611 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006612 fprintf(stderr, "timer setup failed: %s\n", strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006613 abort();
6614 }
6615}
6616
6617#define USEC_PER_SEC 1000000
6618
6619void
6620toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6621{
6622 struct itimerspec its;
6623
6624 its.it_interval.tv_sec = 0;
6625 its.it_interval.tv_nsec = 0;
6626 its.it_value.tv_sec = usec / USEC_PER_SEC;
6627 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6628 toytimer_arm(tt, &its);
6629}
6630
6631void
6632toytimer_disarm(struct toytimer *tt)
6633{
6634 struct itimerspec its = {};
6635
6636 toytimer_arm(tt, &its);
6637}