blob: b00834595c368532472f52aeb5fa2dc51c748398 [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) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001571 if (input->touch_focus == window)
1572 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001573 if (input->pointer_focus == window)
1574 input->pointer_focus = NULL;
1575 if (input->keyboard_focus == window)
1576 input->keyboard_focus = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08001577 if (input->locked_window == window)
1578 input->locked_window = NULL;
1579 if (input->confined_window == window)
1580 input->confined_window = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001581 if (input->focus_widget &&
1582 input->focus_widget->window == window)
1583 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001584 }
1585
Rob Bradford7507b572012-05-15 17:55:34 +01001586 wl_list_for_each_safe(window_output, window_output_tmp,
1587 &window->window_output_list, link) {
1588 free (window_output);
1589 }
1590
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001591 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001592 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001593
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001594 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01001595 xdg_toplevel_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001596 if (window->xdg_popup)
ant8mef99fac22018-11-28 22:46:37 +01001597 xdg_popup_destroy(window->xdg_popup);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001598 if (window->xdg_surface)
ant8mef99fac22018-11-28 22:46:37 +01001599 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001600
1601 surface_destroy(window->main_surface);
1602
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001603 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001604
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001605 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001606 free(window);
1607}
1608
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001609static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001610widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001611{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001613
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001614 wl_list_for_each(child, &widget->child_list, link) {
1615 target = widget_find_widget(child, x, y);
1616 if (target)
1617 return target;
1618 }
1619
1620 if (widget->allocation.x <= x &&
1621 x < widget->allocation.x + widget->allocation.width &&
1622 widget->allocation.y <= y &&
1623 y < widget->allocation.y + widget->allocation.height) {
1624 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001625 }
1626
1627 return NULL;
1628}
1629
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001630static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001631window_find_widget(struct window *window, int32_t x, int32_t y)
1632{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001633 struct surface *surface;
1634 struct widget *widget;
1635
1636 wl_list_for_each(surface, &window->subsurface_list, link) {
1637 widget = widget_find_widget(surface->widget, x, y);
1638 if (widget)
1639 return widget;
1640 }
1641
1642 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001643}
1644
1645static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001646widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001648 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001649
Peter Huttererf3d62272013-08-08 11:57:05 +10001650 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001651 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001652 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001653 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001654 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001655 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001656 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001657 widget->tooltip = NULL;
1658 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001659 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001660 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001661
1662 return widget;
1663}
1664
1665struct widget *
1666window_add_widget(struct window *window, void *data)
1667{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001668 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001669
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001670 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001671 wl_list_init(&widget->link);
1672 window->main_surface->widget = widget;
1673
1674 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001675}
1676
1677struct widget *
1678widget_add_widget(struct widget *parent, void *data)
1679{
1680 struct widget *widget;
1681
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001682 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001683 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001684
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001685 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686}
1687
1688void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001689widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001691 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001692 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001693 struct input *input;
1694
Pekka Paalanen35e82632013-04-25 13:57:48 +03001695 /* Destroy the sub-surface along with the root widget */
1696 if (surface->widget == widget && surface->subsurface)
1697 surface_destroy(widget->surface);
1698
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001699 if (widget->tooltip)
1700 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001701
Pekka Paalanene156fb62012-01-19 13:51:38 +02001702 wl_list_for_each(input, &display->input_list, link) {
1703 if (input->focus_widget == widget)
1704 input->focus_widget = NULL;
1705 }
1706
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001707 wl_list_remove(&widget->link);
1708 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001709}
1710
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001711void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001712widget_set_default_cursor(struct widget *widget, int cursor)
1713{
1714 widget->default_cursor = cursor;
1715}
1716
1717void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001718widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001719{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001720 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001721}
1722
1723void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001724widget_set_size(struct widget *widget, int32_t width, int32_t height)
1725{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001726 widget->allocation.width = width;
1727 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001728}
1729
1730void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001731widget_set_allocation(struct widget *widget,
1732 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001733{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001734 widget->allocation.x = x;
1735 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001736 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001737}
1738
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001739void
1740widget_set_transparent(struct widget *widget, int transparent)
1741{
1742 widget->opaque = !transparent;
1743}
1744
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001745void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001746widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001748 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001749}
1750
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001751static cairo_surface_t *
1752widget_get_cairo_surface(struct widget *widget)
1753{
1754 struct surface *surface = widget->surface;
1755 struct window *window = widget->window;
1756
Neil Roberts97b747c2013-12-19 16:17:12 +00001757 assert(widget->use_cairo);
1758
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001759 if (!surface->cairo_surface) {
1760 if (surface == window->main_surface)
1761 window_create_main_surface(window);
1762 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001763 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001764 }
1765
1766 return surface->cairo_surface;
1767}
1768
Alexander Larsson15901f02013-05-22 14:41:25 +02001769static void
1770widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1771{
1772 struct surface *surface = widget->surface;
1773 double angle;
1774 cairo_matrix_t m;
1775 enum wl_output_transform transform;
1776 int surface_width, surface_height;
1777 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001778 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001779
1780 surface_width = surface->allocation.width;
1781 surface_height = surface->allocation.height;
1782
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001783 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001784 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001785
Alexander Larsson15901f02013-05-22 14:41:25 +02001786 switch (transform) {
1787 case WL_OUTPUT_TRANSFORM_FLIPPED:
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1789 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1790 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1791 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1792 break;
1793 default:
1794 cairo_matrix_init_identity(&m);
1795 break;
1796 }
1797
1798 switch (transform) {
1799 case WL_OUTPUT_TRANSFORM_NORMAL:
1800 default:
1801 angle = 0;
1802 translate_x = 0;
1803 translate_y = 0;
1804 break;
1805 case WL_OUTPUT_TRANSFORM_FLIPPED:
1806 angle = 0;
1807 translate_x = surface_width;
1808 translate_y = 0;
1809 break;
1810 case WL_OUTPUT_TRANSFORM_90:
1811 angle = M_PI_2;
1812 translate_x = surface_height;
1813 translate_y = 0;
1814 break;
1815 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1816 angle = M_PI_2;
1817 translate_x = surface_height;
1818 translate_y = surface_width;
1819 break;
1820 case WL_OUTPUT_TRANSFORM_180:
1821 angle = M_PI;
1822 translate_x = surface_width;
1823 translate_y = surface_height;
1824 break;
1825 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1826 angle = M_PI;
1827 translate_x = 0;
1828 translate_y = surface_height;
1829 break;
1830 case WL_OUTPUT_TRANSFORM_270:
1831 angle = M_PI + M_PI_2;
1832 translate_x = 0;
1833 translate_y = surface_width;
1834 break;
1835 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1836 angle = M_PI + M_PI_2;
1837 translate_x = 0;
1838 translate_y = 0;
1839 break;
1840 }
1841
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001842 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001843 cairo_translate(cr, translate_x, translate_y);
1844 cairo_rotate(cr, angle);
1845 cairo_transform(cr, &m);
1846}
1847
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001848cairo_t *
1849widget_cairo_create(struct widget *widget)
1850{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001851 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001852 cairo_surface_t *cairo_surface;
1853 cairo_t *cr;
1854
1855 cairo_surface = widget_get_cairo_surface(widget);
1856 cr = cairo_create(cairo_surface);
1857
Alexander Larsson15901f02013-05-22 14:41:25 +02001858 widget_cairo_update_transform(widget, cr);
1859
Pekka Paalanen35e82632013-04-25 13:57:48 +03001860 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1861
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001862 return cr;
1863}
1864
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001865struct wl_surface *
1866widget_get_wl_surface(struct widget *widget)
1867{
1868 return widget->surface->surface;
1869}
1870
Neil Roberts5e10a042013-09-09 00:40:17 +01001871struct wl_subsurface *
1872widget_get_wl_subsurface(struct widget *widget)
1873{
1874 return widget->surface->subsurface;
1875}
1876
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001877uint32_t
1878widget_get_last_time(struct widget *widget)
1879{
1880 return widget->surface->last_time;
1881}
1882
1883void
1884widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1885{
1886 struct wl_compositor *comp = widget->window->display->compositor;
1887 struct surface *surface = widget->surface;
1888
1889 if (!surface->input_region)
1890 surface->input_region = wl_compositor_create_region(comp);
1891
1892 if (rect) {
1893 wl_region_add(surface->input_region,
1894 rect->x, rect->y, rect->width, rect->height);
1895 }
1896}
1897
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001898void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001899widget_set_resize_handler(struct widget *widget,
1900 widget_resize_handler_t handler)
1901{
1902 widget->resize_handler = handler;
1903}
1904
1905void
1906widget_set_redraw_handler(struct widget *widget,
1907 widget_redraw_handler_t handler)
1908{
1909 widget->redraw_handler = handler;
1910}
1911
1912void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001913widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001914{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001915 widget->enter_handler = handler;
1916}
1917
1918void
1919widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1920{
1921 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001922}
1923
1924void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001925widget_set_motion_handler(struct widget *widget,
1926 widget_motion_handler_t handler)
1927{
1928 widget->motion_handler = handler;
1929}
1930
1931void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001932widget_set_button_handler(struct widget *widget,
1933 widget_button_handler_t handler)
1934{
1935 widget->button_handler = handler;
1936}
1937
1938void
Rusty Lynch041815a2013-08-08 21:20:38 -07001939widget_set_touch_up_handler(struct widget *widget,
1940 widget_touch_up_handler_t handler)
1941{
1942 widget->touch_up_handler = handler;
1943}
1944
1945void
1946widget_set_touch_down_handler(struct widget *widget,
1947 widget_touch_down_handler_t handler)
1948{
1949 widget->touch_down_handler = handler;
1950}
1951
1952void
1953widget_set_touch_motion_handler(struct widget *widget,
1954 widget_touch_motion_handler_t handler)
1955{
1956 widget->touch_motion_handler = handler;
1957}
1958
1959void
1960widget_set_touch_frame_handler(struct widget *widget,
1961 widget_touch_frame_handler_t handler)
1962{
1963 widget->touch_frame_handler = handler;
1964}
1965
1966void
1967widget_set_touch_cancel_handler(struct widget *widget,
1968 widget_touch_cancel_handler_t handler)
1969{
1970 widget->touch_cancel_handler = handler;
1971}
1972
1973void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001974widget_set_axis_handler(struct widget *widget,
1975 widget_axis_handler_t handler)
1976{
1977 widget->axis_handler = handler;
1978}
1979
Peter Hutterer87743e92016-01-18 16:38:22 +10001980void
1981widget_set_pointer_frame_handler(struct widget *widget,
1982 widget_pointer_frame_handler_t handler)
1983{
1984 widget->pointer_frame_handler = handler;
1985}
1986
1987void
1988widget_set_axis_handlers(struct widget *widget,
1989 widget_axis_handler_t axis_handler,
1990 widget_axis_source_handler_t axis_source_handler,
1991 widget_axis_stop_handler_t axis_stop_handler,
1992 widget_axis_discrete_handler_t axis_discrete_handler)
1993{
1994 widget->axis_handler = axis_handler;
1995 widget->axis_source_handler = axis_source_handler;
1996 widget->axis_stop_handler = axis_stop_handler;
1997 widget->axis_discrete_handler = axis_discrete_handler;
1998}
1999
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002000static void
2001window_schedule_redraw_task(struct window *window);
2002
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002003void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002004widget_schedule_redraw(struct widget *widget)
2005{
Pekka Paalanen71233882013-04-25 13:57:53 +03002006 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002007 widget->surface->redraw_needed = 1;
2008 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002009}
2010
Neil Roberts97b747c2013-12-19 16:17:12 +00002011void
2012widget_set_use_cairo(struct widget *widget,
2013 int use_cairo)
2014{
2015 widget->use_cairo = use_cairo;
2016}
2017
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002018cairo_surface_t *
2019window_get_surface(struct window *window)
2020{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002021 cairo_surface_t *cairo_surface;
2022
2023 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2024
2025 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002026}
2027
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002028struct wl_surface *
2029window_get_wl_surface(struct window *window)
2030{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002031 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002032}
2033
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002034static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002035tooltip_redraw_handler(struct widget *widget, void *data)
2036{
2037 cairo_t *cr;
2038 const int32_t r = 3;
2039 struct tooltip *tooltip = data;
2040 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002041
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002042 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002043 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002044 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2045 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2046 cairo_paint(cr);
2047
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002048 width = widget->allocation.width;
2049 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002050 rounded_rect(cr, 0, 0, width, height, r);
2051
2052 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2053 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2054 cairo_fill(cr);
2055
-c505af82019-06-26 21:00:43 +00002056 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.85);
2057 cairo_move_to(cr, 10, 17);
2058 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002059 cairo_show_text(cr, tooltip->entry);
2060 cairo_destroy(cr);
2061}
2062
2063static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002064get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002065{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002066 cairo_t *cr;
2067 cairo_text_extents_t extents;
2068
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002069 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002070 * created yet, and parent does not have a valid surface
2071 * outside repaint, either.
2072 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002073 cr = cairo_create(display->dummy_surface);
-c505af82019-06-26 21:00:43 +00002074 cairo_set_font_size(cr, 14);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002075 cairo_text_extents(cr, tooltip->entry, &extents);
2076 cairo_destroy(cr);
2077
2078 return extents;
2079}
2080
2081static int
2082window_create_tooltip(struct tooltip *tooltip)
2083{
2084 struct widget *parent = tooltip->parent;
2085 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002086 const int offset_y = 27;
2087 const int margin = 3;
2088 cairo_text_extents_t extents;
2089
2090 if (tooltip->widget)
2091 return 0;
2092
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002093 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002094
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002095 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002096 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002097 widget_set_allocation(tooltip->widget,
2098 tooltip->x, tooltip->y + offset_y,
2099 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002100
2101 return 0;
2102}
2103
2104void
2105widget_destroy_tooltip(struct widget *parent)
2106{
2107 struct tooltip *tooltip = parent->tooltip;
2108
2109 parent->tooltip_count = 0;
2110 if (!tooltip)
2111 return;
2112
2113 if (tooltip->widget) {
2114 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002115 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002116 }
2117
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002118 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002119 free(tooltip->entry);
2120 free(tooltip);
2121 parent->tooltip = NULL;
2122}
2123
2124static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002125tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002126{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002127 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002128
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002129 window_create_tooltip(tooltip);
2130}
2131
2132#define TOOLTIP_TIMEOUT 500
2133static int
2134tooltip_timer_reset(struct tooltip *tooltip)
2135{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002136 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002137
2138 return 0;
2139}
2140
2141int
2142widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2143{
2144 struct tooltip *tooltip = parent->tooltip;
2145
2146 parent->tooltip_count++;
2147 if (tooltip) {
2148 tooltip->x = x;
2149 tooltip->y = y;
2150 tooltip_timer_reset(tooltip);
2151 return 0;
2152 }
2153
2154 /* the handler might be triggered too fast via input device motion, so
2155 * we need this check here to make sure tooltip is fully initialized */
2156 if (parent->tooltip_count > 1)
2157 return 0;
2158
2159 tooltip = malloc(sizeof *tooltip);
2160 if (!tooltip)
2161 return -1;
2162
2163 parent->tooltip = tooltip;
2164 tooltip->parent = parent;
2165 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002166 tooltip->x = x;
2167 tooltip->y = y;
2168 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002169 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2170 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002171 tooltip_timer_reset(tooltip);
2172
2173 return 0;
2174}
2175
2176static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177frame_resize_handler(struct widget *widget,
2178 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002179{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002180 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002181 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002182 struct rectangle interior;
2183 struct rectangle input;
2184 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002185
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002186 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002187 interior.x = 0;
2188 interior.y = 0;
2189 interior.width = width;
2190 interior.height = height;
2191 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002192 frame_resize(frame->frame, width, height);
2193 frame_interior(frame->frame, &interior.x, &interior.y,
2194 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002195 }
2196
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 widget_set_allocation(child, interior.x, interior.y,
2198 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002199
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002200 if (child->resize_handler) {
2201 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002202 child->user_data);
2203
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002204 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002205 width = child->allocation.width;
2206 height = child->allocation.height;
2207 } else {
2208 frame_resize_inside(frame->frame,
2209 child->allocation.width,
2210 child->allocation.height);
2211 width = frame_width(frame->frame);
2212 height = frame_height(frame->frame);
2213 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002214 }
2215
Scott Moreauf7e498c2012-05-14 11:39:29 -06002216 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002217
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 widget->surface->input_region =
2219 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002220 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002221 frame_input_rect(frame->frame, &input.x, &input.y,
2222 &input.width, &input.height);
2223 wl_region_add(widget->surface->input_region,
2224 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002225 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002226 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002227 }
2228
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002229 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002230
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002231 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002232 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002233 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2234 &opaque.width, &opaque.height);
2235
2236 wl_region_add(widget->surface->opaque_region,
2237 opaque.x, opaque.y,
2238 opaque.width, opaque.height);
2239 } else {
2240 wl_region_add(widget->surface->opaque_region,
2241 0, 0, width, height);
2242 }
Martin Minarik1998b152012-05-10 02:04:35 +02002243 }
2244
Martin Minarik1998b152012-05-10 02:04:35 +02002245
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002246 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002247}
2248
2249static void
2250frame_redraw_handler(struct widget *widget, void *data)
2251{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002252 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002253 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002254 struct window *window = widget->window;
2255
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002256 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002257 return;
2258
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002259 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002260
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002261 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002262
2263 cairo_destroy(cr);
2264}
2265
2266static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002267frame_get_pointer_image_for_location(struct window_frame *frame,
2268 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002269{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002270 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002271
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002272 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002273 return CURSOR_LEFT_PTR;
2274
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002275 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002276 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002277 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002278 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002279 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002280 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002281 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002282 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002283 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002284 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002285 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002286 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002287 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002288 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002289 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002290 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002291 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002292 case THEME_LOCATION_EXTERIOR:
2293 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002294 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002295 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002296 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002297}
2298
Pekka Paalanen26237862014-09-10 15:10:30 +03002299static void
2300frame_menu_func(void *data, struct input *input, int index)
2301{
2302 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002303
2304 switch (index) {
2305 case 0: /* close */
2306 window_close(window);
2307 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002308 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002309 /* we don't have a way to get out of fullscreen for now */
2310 if (window->fullscreen_handler)
2311 window->fullscreen_handler(window, window->user_data);
2312 break;
2313 }
2314}
2315
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002316void
2317window_show_frame_menu(struct window *window,
2318 struct input *input, uint32_t time)
2319{
2320 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002321 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002322
Pekka Paalanen26237862014-09-10 15:10:30 +03002323 static const char *entries[] = {
2324 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002325 "Fullscreen"
2326 };
2327
2328 if (window->fullscreen_handler)
2329 count = ARRAY_LENGTH(entries);
2330 else
2331 count = ARRAY_LENGTH(entries) - 1;
2332
2333 input_get_position(input, &x, &y);
2334 window_show_menu(window->display, input, time, window,
2335 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002336}
2337
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002338static int
2339frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002340 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002342 struct window_frame *frame = data;
2343 enum theme_location location;
2344
2345 location = frame_pointer_enter(frame->frame, input, x, y);
2346 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2347 widget_schedule_redraw(frame->widget);
2348
2349 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002350}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002351
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002352static int
2353frame_motion_handler(struct widget *widget,
2354 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002355 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002356{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002357 struct window_frame *frame = data;
2358 enum theme_location location;
2359
2360 location = frame_pointer_motion(frame->frame, input, x, y);
2361 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2362 widget_schedule_redraw(frame->widget);
2363
2364 return frame_get_pointer_image_for_location(data, location);
2365}
2366
2367static void
2368frame_leave_handler(struct widget *widget,
2369 struct input *input, void *data)
2370{
2371 struct window_frame *frame = data;
2372
2373 frame_pointer_leave(frame->frame, input);
2374 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2375 widget_schedule_redraw(frame->widget);
2376}
2377
2378static void
2379frame_handle_status(struct window_frame *frame, struct input *input,
2380 uint32_t time, enum theme_location location)
2381{
2382 struct window *window = frame->widget->window;
2383 uint32_t status;
2384
2385 status = frame_status(frame->frame);
2386 if (status & FRAME_STATUS_REPAINT)
2387 widget_schedule_redraw(frame->widget);
2388
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002389 if (status & FRAME_STATUS_MINIMIZE) {
2390 window_set_minimized(window);
2391 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2392 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002393
2394 if (status & FRAME_STATUS_MENU) {
2395 window_show_frame_menu(window, input, time);
2396 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2397 }
2398
2399 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002400 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002401 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2402 }
2403
2404 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002405 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002406 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002407 }
2408
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002409 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002410 input_ungrab(input);
ant8mef99fac22018-11-28 22:46:37 +01002411 xdg_toplevel_move(window->xdg_toplevel,
2412 input_get_seat(input),
2413 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002414
2415 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2416 }
2417
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002418 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002419 input_ungrab(input);
2420
ant8mef99fac22018-11-28 22:46:37 +01002421 xdg_toplevel_resize(window->xdg_toplevel,
2422 input_get_seat(input),
2423 window->display->serial,
2424 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002425
2426 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2427 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002428}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002429
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002430#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002431static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002432frame_button_handler(struct widget *widget,
2433 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002434 uint32_t button, enum wl_pointer_button_state state,
2435 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002436
2437{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002438 struct window_frame *frame = data;
2439 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002440
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002441 frame->double_click = 0;
2442 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2443 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2444 frame->double_click = 1;
2445 frame->did_double = 1;
2446 } else
2447 frame->did_double = 0;
2448
2449 frame->last_time = time;
2450 } else if (frame->did_double == 1) {
2451 frame->double_click = 1;
2452 frame->did_double = 0;
2453 }
2454
2455 if (frame->double_click)
2456 location = frame_double_click(frame->frame, input,
2457 button, state);
2458 else
2459 location = frame_pointer_button(frame->frame, input,
2460 button, state);
2461
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002462 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002463}
2464
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002465static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002466frame_touch_down_handler(struct widget *widget, struct input *input,
2467 uint32_t serial, uint32_t time, int32_t id,
2468 float x, float y, void *data)
2469{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002470 struct window_frame *frame = data;
2471
Xiong Zhang382de462014-06-12 11:06:26 +08002472 frame->double_click = 0;
2473 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2474 frame->last_id == id) {
2475 frame->double_click = 1;
2476 frame->did_double = 1;
2477 frame->double_id = id;
2478 } else
2479 frame->did_double = 0;
2480
2481 frame->last_time = time;
2482 frame->last_id = id;
2483
2484 if (frame->double_click)
2485 frame_double_touch_down(frame->frame, input, id, x, y);
2486 else
2487 frame_touch_down(frame->frame, input, id, x, y);
2488
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002489 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2490}
2491
2492static void
2493frame_touch_up_handler(struct widget *widget,
2494 struct input *input, uint32_t serial, uint32_t time,
2495 int32_t id, void *data)
2496{
2497 struct window_frame *frame = data;
2498
Xiong Zhang382de462014-06-12 11:06:26 +08002499 if (frame->double_id == id && frame->did_double) {
2500 frame->did_double = 0;
2501 frame->double_id = 0;
2502 frame_double_touch_up(frame->frame, input, id);
2503 } else
2504 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002505 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002506}
2507
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002508struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002509window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002510{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002511 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002512 uint32_t buttons;
2513
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002514 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002515 buttons = FRAME_BUTTON_NONE;
2516 } else {
2517 buttons = FRAME_BUTTON_ALL;
2518 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002519
Peter Huttererf3d62272013-08-08 11:57:05 +10002520 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002521 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002522 buttons, window->title, NULL);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002523
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524 frame->widget = window_add_widget(window, frame);
2525 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002526
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002527 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2528 widget_set_resize_handler(frame->widget, frame_resize_handler);
2529 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002530 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002531 widget_set_motion_handler(frame->widget, frame_motion_handler);
2532 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002533 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002534 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002535
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002536 window->frame = frame;
2537
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002538 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002539}
2540
Kristian Høgsberga1627922012-06-20 17:30:03 -04002541void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002542window_frame_set_child_size(struct widget *widget, int child_width,
2543 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002544{
2545 struct display *display = widget->window->display;
2546 struct theme *t = display->theme;
2547 int decoration_width, decoration_height;
2548 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002549 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002550
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002551 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002552 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002553 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002554 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002555
2556 width = child_width + decoration_width;
2557 height = child_height + decoration_height;
2558 } else {
2559 width = child_width;
2560 height = child_height;
2561 }
2562
2563 window_schedule_resize(widget->window, width, height);
2564}
2565
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002566static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002567window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002568{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002569 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002570
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002571 /* frame->child must be destroyed by the application */
2572 widget_destroy(frame->widget);
2573 free(frame);
2574}
2575
2576static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002577input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002578 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002579{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002580 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002581 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002582
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002583 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002584 return;
2585
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002586 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002587 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002588 widget = old;
2589 if (input->grab)
2590 widget = input->grab;
2591 if (widget->leave_handler)
2592 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002593 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002594 }
2595
2596 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002597 widget = focus;
2598 if (input->grab)
2599 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002600 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002601 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002602 cursor = widget->enter_handler(focus, input, x, y,
2603 widget->user_data);
2604 else
2605 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002606
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002607 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002608 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002609}
2610
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002611void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002612touch_grab(struct input *input, int32_t touch_id)
2613{
2614 input->touch_grab = 1;
2615 input->touch_grab_id = touch_id;
2616}
2617
2618void
2619touch_ungrab(struct input *input)
2620{
2621 struct touch_point *tp, *tmp;
2622
2623 input->touch_grab = 0;
2624
2625 wl_list_for_each_safe(tp, tmp,
2626 &input->touch_point_list, link) {
2627 if (tp->id != input->touch_grab_id)
2628 continue;
2629 wl_list_remove(&tp->link);
2630 free(tp);
2631
2632 return;
2633 }
2634}
2635
2636void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002637input_grab(struct input *input, struct widget *widget, uint32_t button)
2638{
2639 input->grab = widget;
2640 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002641
2642 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002643}
2644
2645void
2646input_ungrab(struct input *input)
2647{
2648 struct widget *widget;
2649
2650 input->grab = NULL;
2651 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002652 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002653 input->sx, input->sy);
2654 input_set_focus_widget(input, widget, input->sx, input->sy);
2655 }
2656}
2657
2658static void
Derek Foreman118a4292015-04-22 17:23:35 -05002659cursor_delay_timer_reset(struct input *input, uint32_t duration)
2660{
Derek Foreman118a4292015-04-22 17:23:35 -05002661 if (!duration)
2662 input->cursor_timer_running = false;
2663 else
2664 input->cursor_timer_running = true;
2665
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002666 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002667}
2668
2669static void cancel_pointer_image_update(struct input *input)
2670{
2671 if (input->cursor_timer_running)
2672 cursor_delay_timer_reset(input, 0);
2673}
2674
2675static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002676input_remove_pointer_focus(struct input *input)
2677{
2678 struct window *window = input->pointer_focus;
2679
2680 if (!window)
2681 return;
2682
2683 input_set_focus_widget(input, NULL, 0, 0);
2684
2685 input->pointer_focus = NULL;
2686 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002687 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002688}
2689
2690static void
2691pointer_handle_enter(void *data, struct wl_pointer *pointer,
2692 uint32_t serial, struct wl_surface *surface,
2693 wl_fixed_t sx_w, wl_fixed_t sy_w)
2694{
2695 struct input *input = data;
2696 struct window *window;
2697 struct widget *widget;
2698 float sx = wl_fixed_to_double(sx_w);
2699 float sy = wl_fixed_to_double(sy_w);
2700
2701 if (!surface) {
2702 /* enter event for a window we've just destroyed */
2703 return;
2704 }
2705
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002706 window = wl_surface_get_user_data(surface);
2707 if (surface != window->main_surface->surface) {
2708 DBG("Ignoring input event from subsurface %p\n", surface);
2709 return;
2710 }
2711
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002712 input->display->serial = serial;
2713 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002714 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002715
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002716 input->sx = sx;
2717 input->sy = sy;
2718
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002719 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002720 input_set_focus_widget(input, widget, sx, sy);
2721}
2722
2723static void
2724pointer_handle_leave(void *data, struct wl_pointer *pointer,
2725 uint32_t serial, struct wl_surface *surface)
2726{
2727 struct input *input = data;
2728
2729 input->display->serial = serial;
2730 input_remove_pointer_focus(input);
2731}
2732
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002733static void
Daniel Stone37816df2012-05-16 18:45:18 +01002734pointer_handle_motion(void *data, struct wl_pointer *pointer,
2735 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002736{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002737 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002738 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002739 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002740 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002741 float sx = wl_fixed_to_double(sx_w);
2742 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002743
Paul Winwoodb22bf572013-08-29 10:52:54 +01002744 if (!window)
2745 return;
2746
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002747 input->sx = sx;
2748 input->sy = sy;
2749
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002750 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002751 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002752 * based on the old surface dimensions. However, if we have an active
2753 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002754 */
Derek Foreman46812b62015-08-26 17:13:27 -05002755 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002756 sy < window->main_surface->allocation.y ||
2757 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002758 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002759 return;
2760
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002761 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002762 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002763 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002764 }
2765
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002766 if (input->grab)
2767 widget = input->grab;
2768 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002769 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002770 if (widget) {
2771 if (widget->motion_handler)
2772 cursor = widget->motion_handler(input->focus_widget,
2773 input, time, sx, sy,
2774 widget->user_data);
2775 else
2776 cursor = widget->default_cursor;
2777 } else
2778 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002779
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002780 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002781}
2782
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002783static void
Daniel Stone37816df2012-05-16 18:45:18 +01002784pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002785 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002786{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002787 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002788 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002789 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002790
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002791 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002792 if (input->focus_widget && input->grab == NULL &&
2793 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002794 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002795
Neil Roberts6b28aad2012-01-23 19:11:18 +00002796 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002797 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002798 (*widget->button_handler)(widget,
2799 input, time,
2800 button, state,
2801 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002802
Daniel Stone4dbadb12012-05-30 16:31:51 +01002803 if (input->grab && input->grab_button == button &&
2804 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002805 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002806}
2807
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002808static void
Daniel Stone37816df2012-05-16 18:45:18 +01002809pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002810 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002811{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002812 struct input *input = data;
2813 struct widget *widget;
2814
2815 widget = input->focus_widget;
2816 if (input->grab)
2817 widget = input->grab;
2818 if (widget && widget->axis_handler)
2819 (*widget->axis_handler)(widget,
2820 input, time,
2821 axis, value,
2822 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002823}
2824
Peter Hutterer87743e92016-01-18 16:38:22 +10002825static void
2826pointer_handle_frame(void *data, struct wl_pointer *pointer)
2827{
2828 struct input *input = data;
2829 struct widget *widget;
2830
2831 widget = input->focus_widget;
2832 if (input->grab)
2833 widget = input->grab;
2834 if (widget && widget->pointer_frame_handler)
2835 (*widget->pointer_frame_handler)(widget,
2836 input,
2837 widget->user_data);
2838}
2839
2840static void
2841pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2842 uint32_t source)
2843{
2844 struct input *input = data;
2845 struct widget *widget;
2846
2847 widget = input->focus_widget;
2848 if (input->grab)
2849 widget = input->grab;
2850 if (widget && widget->axis_source_handler)
2851 (*widget->axis_source_handler)(widget,
2852 input,
2853 source,
2854 widget->user_data);
2855}
2856
2857static void
2858pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2859 uint32_t time, uint32_t axis)
2860{
2861 struct input *input = data;
2862 struct widget *widget;
2863
2864 widget = input->focus_widget;
2865 if (input->grab)
2866 widget = input->grab;
2867 if (widget && widget->axis_stop_handler)
2868 (*widget->axis_stop_handler)(widget,
2869 input, time,
2870 axis,
2871 widget->user_data);
2872}
2873
2874static void
2875pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2876 uint32_t axis, int32_t discrete)
2877{
2878 struct input *input = data;
2879 struct widget *widget;
2880
2881 widget = input->focus_widget;
2882 if (input->grab)
2883 widget = input->grab;
2884 if (widget && widget->axis_discrete_handler)
2885 (*widget->axis_discrete_handler)(widget,
2886 input,
2887 axis,
2888 discrete,
2889 widget->user_data);
2890}
2891
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002892static const struct wl_pointer_listener pointer_listener = {
2893 pointer_handle_enter,
2894 pointer_handle_leave,
2895 pointer_handle_motion,
2896 pointer_handle_button,
2897 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002898 pointer_handle_frame,
2899 pointer_handle_axis_source,
2900 pointer_handle_axis_stop,
2901 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002902};
2903
2904static void
2905input_remove_keyboard_focus(struct input *input)
2906{
2907 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002908
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002909 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002910
2911 if (!window)
2912 return;
2913
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002914 if (window->keyboard_focus_handler)
2915 (*window->keyboard_focus_handler)(window, NULL,
2916 window->user_data);
2917
2918 input->keyboard_focus = NULL;
2919}
2920
2921static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002922keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002923{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002924 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002925 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002926
2927 if (window && window->key_handler) {
2928 (*window->key_handler)(window, input, input->repeat_time,
2929 input->repeat_key, input->repeat_sym,
2930 WL_KEYBOARD_KEY_STATE_PRESSED,
2931 window->user_data);
2932 }
2933}
2934
2935static void
2936keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2937 uint32_t format, int fd, uint32_t size)
2938{
2939 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002940 struct xkb_keymap *keymap;
2941 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002942#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002943 struct xkb_compose_table *compose_table;
2944 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002945#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002946 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002947 char *map_str;
2948
2949 if (!data) {
2950 close(fd);
2951 return;
2952 }
2953
2954 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2955 close(fd);
2956 return;
2957 }
2958
2959 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2960 if (map_str == MAP_FAILED) {
2961 close(fd);
2962 return;
2963 }
2964
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002965 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03002966 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2967 map_str,
2968 XKB_KEYMAP_FORMAT_TEXT_V1,
2969 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002970 munmap(map_str, size);
2971 close(fd);
2972
Rui Matos3eccb862013-10-10 19:44:22 +02002973 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002974 fprintf(stderr, "failed to compile keymap\n");
2975 return;
2976 }
2977
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002978 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02002979 state = xkb_state_new(keymap);
2980 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002981 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002982 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002983 return;
2984 }
2985
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002986 /* Look up the preferred locale, falling back to "C" as default */
2987 if (!(locale = getenv("LC_ALL")))
2988 if (!(locale = getenv("LC_CTYPE")))
2989 if (!(locale = getenv("LANG")))
2990 locale = "C";
2991
2992 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01002993#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002994 compose_table =
2995 xkb_compose_table_new_from_locale(input->display->xkb_context,
2996 locale,
2997 XKB_COMPOSE_COMPILE_NO_FLAGS);
2998 if (compose_table) {
2999 /* Set up XKB compose state */
3000 compose_state = xkb_compose_state_new(compose_table,
3001 XKB_COMPOSE_STATE_NO_FLAGS);
3002 if (compose_state) {
3003 xkb_compose_state_unref(input->xkb.compose_state);
3004 xkb_compose_table_unref(input->xkb.compose_table);
3005 input->xkb.compose_state = compose_state;
3006 input->xkb.compose_table = compose_table;
3007 } else {
3008 fprintf(stderr, "could not create XKB compose state. "
3009 "Disabiling compose.\n");
3010 xkb_compose_table_unref(compose_table);
3011 compose_table = NULL;
3012 }
3013 } else {
3014 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3015 "Disabiling compose\n", locale);
3016 }
Daniel Stone5b015962016-10-20 14:45:58 +01003017#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003018
Rui Matos3eccb862013-10-10 19:44:22 +02003019 xkb_keymap_unref(input->xkb.keymap);
3020 xkb_state_unref(input->xkb.state);
3021 input->xkb.keymap = keymap;
3022 input->xkb.state = state;
3023
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003024 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003025 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003026 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003027 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003028 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003029 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003030}
3031
3032static void
3033keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3034 uint32_t serial, struct wl_surface *surface,
3035 struct wl_array *keys)
3036{
3037 struct input *input = data;
3038 struct window *window;
3039
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003040 if (!surface) {
3041 /* enter event for a window we've just destroyed */
3042 return;
3043 }
3044
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003045 input->display->serial = serial;
3046 input->keyboard_focus = wl_surface_get_user_data(surface);
3047
3048 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003049 if (window->keyboard_focus_handler)
3050 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003051 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003052}
3053
3054static void
3055keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3056 uint32_t serial, struct wl_surface *surface)
3057{
3058 struct input *input = data;
3059
3060 input->display->serial = serial;
3061 input_remove_keyboard_focus(input);
3062}
3063
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003064/* Translate symbols appropriately if a compose sequence is being entered */
3065static xkb_keysym_t
3066process_key_press(xkb_keysym_t sym, struct input *input)
3067{
Daniel Stone5b015962016-10-20 14:45:58 +01003068#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003069 if (!input->xkb.compose_state)
3070 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003071 if (sym == XKB_KEY_NoSymbol)
3072 return sym;
3073 if (xkb_compose_state_feed(input->xkb.compose_state,
3074 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3075 return sym;
3076
3077 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3078 case XKB_COMPOSE_COMPOSING:
3079 return XKB_KEY_NoSymbol;
3080 case XKB_COMPOSE_COMPOSED:
3081 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3082 case XKB_COMPOSE_CANCELLED:
3083 return XKB_KEY_NoSymbol;
3084 case XKB_COMPOSE_NOTHING:
3085 return sym;
3086 default:
3087 return sym;
3088 }
Daniel Stone5b015962016-10-20 14:45:58 +01003089#else
3090 return sym;
3091#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003092}
3093
Scott Moreau210d0792012-03-22 10:47:01 -06003094static void
Daniel Stone37816df2012-05-16 18:45:18 +01003095keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003096 uint32_t serial, uint32_t time, uint32_t key,
3097 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003098{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003099 struct input *input = data;
3100 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003101 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003102 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003103 const xkb_keysym_t *syms;
3104 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003105 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003106
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003107 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003108 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003109 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003110 return;
3111
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003112 /* We only use input grabs for pointer events for now, so just
3113 * ignore key presses if a grab is active. We expand the key
3114 * event delivery mechanism to route events to widgets to
3115 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003116 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003117 if (input->grab && input->grab_button == 0)
3118 return;
3119
Ran Benita2e1968f2014-08-19 23:59:51 +03003120 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003121
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003122 sym = XKB_KEY_NoSymbol;
3123 if (num_syms == 1)
3124 sym = syms[0];
3125
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003126
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003127 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003128 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003129 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003130 } else if (sym == XKB_KEY_F11 &&
3131 window->fullscreen_handler &&
3132 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3133 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003134 } else if (sym == XKB_KEY_F4 &&
3135 input->modifiers == MOD_ALT_MASK &&
3136 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003137 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003138 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003139 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3140 sym = process_key_press(sym, input);
3141
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003142 (*window->key_handler)(window, input, time, key,
3143 sym, state, window->user_data);
3144 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003145
3146 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3147 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003148 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003149 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3150 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003151 input->repeat_sym = sym;
3152 input->repeat_key = key;
3153 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003154 its.it_interval.tv_sec = input->repeat_rate_sec;
3155 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3156 its.it_value.tv_sec = input->repeat_delay_sec;
3157 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003158 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003159 }
3160}
3161
3162static void
Daniel Stone351eb612012-05-31 15:27:47 -04003163keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3164 uint32_t serial, uint32_t mods_depressed,
3165 uint32_t mods_latched, uint32_t mods_locked,
3166 uint32_t group)
3167{
3168 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003169 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003170
Matt Ropere61561f2013-06-24 16:52:43 +01003171 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3172 if (!input->xkb.keymap)
3173 return;
3174
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003175 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3176 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003177 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003178 XKB_STATE_MODS_DEPRESSED |
3179 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003180 input->modifiers = 0;
3181 if (mask & input->xkb.control_mask)
3182 input->modifiers |= MOD_CONTROL_MASK;
3183 if (mask & input->xkb.alt_mask)
3184 input->modifiers |= MOD_ALT_MASK;
3185 if (mask & input->xkb.shift_mask)
3186 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003187}
3188
Jonny Lamb06959082014-08-12 14:58:27 +02003189static void
3190set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3191{
3192 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3193 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3194
3195 /* a rate of zero disables any repeating, regardless of the delay's
3196 * value */
3197 if (rate == 0)
3198 return;
3199
3200 if (rate == 1)
3201 input->repeat_rate_sec = 1;
3202 else
3203 input->repeat_rate_nsec = 1000000000 / rate;
3204
3205 input->repeat_delay_sec = delay / 1000;
3206 delay -= (input->repeat_delay_sec * 1000);
3207 input->repeat_delay_nsec = delay * 1000 * 1000;
3208}
3209
3210static void
3211keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3212 int32_t rate, int32_t delay)
3213{
3214 struct input *input = data;
3215
3216 set_repeat_info(input, rate, delay);
3217}
3218
Daniel Stone37816df2012-05-16 18:45:18 +01003219static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003220 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003221 keyboard_handle_enter,
3222 keyboard_handle_leave,
3223 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003224 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003225 keyboard_handle_repeat_info
3226
Daniel Stone37816df2012-05-16 18:45:18 +01003227};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003228
3229static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003230touch_handle_down(void *data, struct wl_touch *wl_touch,
3231 uint32_t serial, uint32_t time, struct wl_surface *surface,
3232 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3233{
3234 struct input *input = data;
3235 struct widget *widget;
3236 float sx = wl_fixed_to_double(x_w);
3237 float sy = wl_fixed_to_double(y_w);
3238
Rusty Lynch1084da52013-08-15 09:10:08 -07003239 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003240 input->touch_focus = wl_surface_get_user_data(surface);
3241 if (!input->touch_focus) {
3242 DBG("Failed to find to touch focus for surface %p\n", surface);
3243 return;
3244 }
3245
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003246 if (surface != input->touch_focus->main_surface->surface) {
3247 DBG("Ignoring input event from subsurface %p\n", surface);
3248 input->touch_focus = NULL;
3249 return;
3250 }
3251
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003252 if (input->grab)
3253 widget = input->grab;
3254 else
3255 widget = window_find_widget(input->touch_focus,
3256 wl_fixed_to_double(x_w),
3257 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003258 if (widget) {
3259 struct touch_point *tp = xmalloc(sizeof *tp);
3260 if (tp) {
3261 tp->id = id;
3262 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003263 tp->x = sx;
3264 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003265 wl_list_insert(&input->touch_point_list, &tp->link);
3266
3267 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003268 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003269 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003270 sx, sy,
3271 widget->user_data);
3272 }
3273 }
3274}
3275
3276static void
3277touch_handle_up(void *data, struct wl_touch *wl_touch,
3278 uint32_t serial, uint32_t time, int32_t id)
3279{
3280 struct input *input = data;
3281 struct touch_point *tp, *tmp;
3282
Rusty Lynch041815a2013-08-08 21:20:38 -07003283 if (!input->touch_focus) {
3284 DBG("No touch focus found for touch up event!\n");
3285 return;
3286 }
3287
3288 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3289 if (tp->id != id)
3290 continue;
3291
3292 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003293 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003294 time, id,
3295 tp->widget->user_data);
3296
3297 wl_list_remove(&tp->link);
3298 free(tp);
3299
3300 return;
3301 }
3302}
3303
3304static void
3305touch_handle_motion(void *data, struct wl_touch *wl_touch,
3306 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3307{
3308 struct input *input = data;
3309 struct touch_point *tp;
3310 float sx = wl_fixed_to_double(x_w);
3311 float sy = wl_fixed_to_double(y_w);
3312
3313 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3314
3315 if (!input->touch_focus) {
3316 DBG("No touch focus found for touch motion event!\n");
3317 return;
3318 }
3319
3320 wl_list_for_each(tp, &input->touch_point_list, link) {
3321 if (tp->id != id)
3322 continue;
3323
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003324 tp->x = sx;
3325 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003326 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003327 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003328 id, sx, sy,
3329 tp->widget->user_data);
3330 return;
3331 }
3332}
3333
3334static void
3335touch_handle_frame(void *data, struct wl_touch *wl_touch)
3336{
3337 struct input *input = data;
3338 struct touch_point *tp, *tmp;
3339
3340 DBG("touch_handle_frame\n");
3341
3342 if (!input->touch_focus) {
3343 DBG("No touch focus found for touch frame event!\n");
3344 return;
3345 }
3346
3347 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3348 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003349 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003350 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003351 }
3352}
3353
3354static void
3355touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3356{
3357 struct input *input = data;
3358 struct touch_point *tp, *tmp;
3359
3360 DBG("touch_handle_cancel\n");
3361
3362 if (!input->touch_focus) {
3363 DBG("No touch focus found for touch cancel event!\n");
3364 return;
3365 }
3366
3367 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3368 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003369 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3370 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003371
3372 wl_list_remove(&tp->link);
3373 free(tp);
3374 }
3375}
3376
3377static const struct wl_touch_listener touch_listener = {
3378 touch_handle_down,
3379 touch_handle_up,
3380 touch_handle_motion,
3381 touch_handle_frame,
3382 touch_handle_cancel,
3383};
3384
3385static void
Daniel Stone37816df2012-05-16 18:45:18 +01003386seat_handle_capabilities(void *data, struct wl_seat *seat,
3387 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003388{
Daniel Stone37816df2012-05-16 18:45:18 +01003389 struct input *input = data;
3390
3391 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3392 input->pointer = wl_seat_get_pointer(seat);
3393 wl_pointer_set_user_data(input->pointer, input);
3394 wl_pointer_add_listener(input->pointer, &pointer_listener,
3395 input);
3396 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003397 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003398 wl_pointer_release(input->pointer);
3399 else
3400 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003401 input->pointer = NULL;
3402 }
3403
3404 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3405 input->keyboard = wl_seat_get_keyboard(seat);
3406 wl_keyboard_set_user_data(input->keyboard, input);
3407 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3408 input);
3409 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003410 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003411 wl_keyboard_release(input->keyboard);
3412 else
3413 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003414 input->keyboard = NULL;
3415 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003416
3417 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3418 input->touch = wl_seat_get_touch(seat);
3419 wl_touch_set_user_data(input->touch, input);
3420 wl_touch_add_listener(input->touch, &touch_listener, input);
3421 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003422 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003423 wl_touch_release(input->touch);
3424 else
3425 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003426 input->touch = NULL;
3427 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003428}
3429
Rob Bradford08031182013-08-13 20:11:03 +01003430static void
3431seat_handle_name(void *data, struct wl_seat *seat,
3432 const char *name)
3433{
3434
3435}
3436
Daniel Stone37816df2012-05-16 18:45:18 +01003437static const struct wl_seat_listener seat_listener = {
3438 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003439 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003440};
3441
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003442void
3443input_get_position(struct input *input, int32_t *x, int32_t *y)
3444{
3445 *x = input->sx;
3446 *y = input->sy;
3447}
3448
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003449int
3450input_get_touch(struct input *input, int32_t id, float *x, float *y)
3451{
3452 struct touch_point *tp;
3453
3454 wl_list_for_each(tp, &input->touch_point_list, link) {
3455 if (tp->id != id)
3456 continue;
3457
3458 *x = tp->x;
3459 *y = tp->y;
3460 return 0;
3461 }
3462
3463 return -1;
3464}
3465
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003466struct display *
3467input_get_display(struct input *input)
3468{
3469 return input->display;
3470}
3471
Daniel Stone37816df2012-05-16 18:45:18 +01003472struct wl_seat *
3473input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003474{
Daniel Stone37816df2012-05-16 18:45:18 +01003475 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003476}
3477
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003478uint32_t
3479input_get_modifiers(struct input *input)
3480{
3481 return input->modifiers;
3482}
3483
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003484struct widget *
3485input_get_focus_widget(struct input *input)
3486{
3487 return input->focus_widget;
3488}
3489
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003490struct data_offer {
3491 struct wl_data_offer *offer;
3492 struct input *input;
3493 struct wl_array types;
3494 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003495
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003496 struct task io_task;
3497 int fd;
3498 data_func_t func;
3499 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003500 uint32_t dnd_action;
3501 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003502 void *user_data;
3503};
3504
3505static void
3506data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3507{
3508 struct data_offer *offer = data;
3509 char **p;
3510
3511 p = wl_array_add(&offer->types, sizeof *p);
3512 *p = strdup(type);
3513}
3514
Carlos Garnacho9c931792016-01-18 23:52:12 +01003515static void
3516data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3517{
3518 struct data_offer *offer = data;
3519
3520 offer->source_actions = source_actions;
3521}
3522
3523static void
3524data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3525{
3526 struct data_offer *offer = data;
3527
3528 offer->dnd_action = dnd_action;
3529}
3530
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003531static const struct wl_data_offer_listener data_offer_listener = {
3532 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003533 data_offer_source_actions,
3534 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003535};
3536
3537static void
3538data_offer_destroy(struct data_offer *offer)
3539{
3540 char **p;
3541
3542 offer->refcount--;
3543 if (offer->refcount == 0) {
3544 wl_data_offer_destroy(offer->offer);
3545 for (p = offer->types.data; *p; p++)
3546 free(*p);
3547 wl_array_release(&offer->types);
3548 free(offer);
3549 }
3550}
3551
3552static void
3553data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003554 struct wl_data_device *data_device,
3555 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003556{
3557 struct data_offer *offer;
3558
Brian Lovinbc919262013-08-07 15:34:59 -07003559 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003560
3561 wl_array_init(&offer->types);
3562 offer->refcount = 1;
3563 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003564 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003565 wl_data_offer_add_listener(offer->offer,
3566 &data_offer_listener, offer);
3567}
3568
3569static void
3570data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003571 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003572 wl_fixed_t x_w, wl_fixed_t y_w,
3573 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003574{
3575 struct input *input = data;
3576 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003577 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003578 float x = wl_fixed_to_double(x_w);
3579 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003580 char **p;
3581
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003582 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003583 input->drag_enter_serial = serial;
3584 input->drag_focus = window,
3585 input->drag_x = x;
3586 input->drag_y = y;
3587
3588 if (!input->touch_grab)
3589 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003590
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003591 if (offer) {
3592 input->drag_offer = wl_data_offer_get_user_data(offer);
3593
3594 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3595 *p = NULL;
3596
3597 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003598
3599 if (input->display->data_device_manager_version >=
3600 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3601 wl_data_offer_set_actions(offer,
3602 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3603 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3604 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3605 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003606 } else {
3607 input->drag_offer = NULL;
3608 types_data = NULL;
3609 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003610
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003611 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003612 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003613 window->user_data);
3614}
3615
3616static void
3617data_device_leave(void *data, struct wl_data_device *data_device)
3618{
3619 struct input *input = data;
3620
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003621 if (input->drag_offer) {
3622 data_offer_destroy(input->drag_offer);
3623 input->drag_offer = NULL;
3624 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003625}
3626
3627static void
3628data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003629 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003630{
3631 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003632 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003633 float x = wl_fixed_to_double(x_w);
3634 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003635 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003636
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003637 input->drag_x = x;
3638 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003639
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003640 if (input->drag_offer)
3641 types_data = input->drag_offer->types.data;
3642 else
3643 types_data = NULL;
3644
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003645 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003646 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003647 window->user_data);
3648}
3649
3650static void
3651data_device_drop(void *data, struct wl_data_device *data_device)
3652{
3653 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003654 struct window *window = input->drag_focus;
3655 float x, y;
3656
3657 x = input->drag_x;
3658 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003659
3660 if (window->drop_handler)
3661 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003662 x, y, window->user_data);
3663
3664 if (input->touch_grab)
3665 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003666}
3667
3668static void
3669data_device_selection(void *data,
3670 struct wl_data_device *wl_data_device,
3671 struct wl_data_offer *offer)
3672{
3673 struct input *input = data;
3674 char **p;
3675
3676 if (input->selection_offer)
3677 data_offer_destroy(input->selection_offer);
3678
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003679 if (offer) {
3680 input->selection_offer = wl_data_offer_get_user_data(offer);
3681 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3682 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003683 } else {
3684 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003685 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003686}
3687
3688static const struct wl_data_device_listener data_device_listener = {
3689 data_device_data_offer,
3690 data_device_enter,
3691 data_device_leave,
3692 data_device_motion,
3693 data_device_drop,
3694 data_device_selection
3695};
3696
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003697static void
3698input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003699{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003700 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003701 struct wl_cursor *cursor;
3702 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003703
Daniel Stone80972742012-11-07 17:51:39 +11003704 if (!input->pointer)
3705 return;
3706
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003707 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003708 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003709 return;
3710
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003711 if (index >= (int) cursor->image_count) {
3712 fprintf(stderr, "cursor index out of range\n");
3713 return;
3714 }
3715
3716 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003717 buffer = wl_cursor_image_get_buffer(image);
3718 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003719 return;
3720
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003721 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3722 wl_surface_damage(input->pointer_surface, 0, 0,
3723 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003724 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003725 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3726 input->pointer_surface,
3727 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003728}
3729
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003730static const struct wl_callback_listener pointer_surface_listener;
3731
Derek Foreman493d9792015-03-04 16:26:25 -06003732static bool
3733input_set_pointer_special(struct input *input)
3734{
3735 if (input->current_cursor == CURSOR_BLANK) {
3736 wl_pointer_set_cursor(input->pointer,
3737 input->pointer_enter_serial,
3738 NULL, 0, 0);
3739 return true;
3740 }
3741
3742 if (input->current_cursor == CURSOR_UNSET)
3743 return true;
3744
3745 return false;
3746}
3747
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003748static void
Derek Foreman118a4292015-04-22 17:23:35 -05003749schedule_pointer_image_update(struct input *input,
3750 struct wl_cursor *cursor,
3751 uint32_t duration,
3752 bool force_frame)
3753{
3754 /* Some silly cursor sets have enormous pauses in them. In these
3755 * cases it's better to use a timer even if it results in less
3756 * accurate presentation, since it will save us having to set the
3757 * same cursor image over and over again.
3758 *
3759 * This is really not the way we're supposed to time any kind of
3760 * animation, but we're pretending it's OK here because we don't
3761 * want animated cursors with long delays to needlessly hog CPU.
3762 *
3763 * We use force_frame to ensure we don't accumulate large timing
3764 * errors by running off the wrong clock.
3765 */
3766 if (!force_frame && duration > 100) {
3767 struct timespec tp;
3768
3769 clock_gettime(CLOCK_MONOTONIC, &tp);
3770 input->cursor_timer_start = tp.tv_sec * 1000
3771 + tp.tv_nsec / 1000000;
3772 cursor_delay_timer_reset(input, duration);
3773 return;
3774 }
3775
3776 /* for short durations we'll just spin on frame callbacks for
3777 * accurate timing - the way any kind of timing sensitive animation
3778 * should really be done. */
3779 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3780 wl_callback_add_listener(input->cursor_frame_cb,
3781 &pointer_surface_listener, input);
3782
3783}
3784
3785static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003786pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3787 uint32_t time)
3788{
3789 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003790 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003791 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003792 uint32_t duration;
3793 bool force_frame = true;
3794
3795 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003796
3797 if (callback) {
3798 assert(callback == input->cursor_frame_cb);
3799 wl_callback_destroy(callback);
3800 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003801 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003802 }
3803
Daniel Stone80972742012-11-07 17:51:39 +11003804 if (!input->pointer)
3805 return;
3806
Derek Foreman493d9792015-03-04 16:26:25 -06003807 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003808 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003809
Daniel Stonea494f1d2012-06-18 19:31:12 +01003810 cursor = input->display->cursors[input->current_cursor];
3811 if (!cursor)
3812 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003813
3814 /* FIXME We don't have the current time on the first call so we set
3815 * the animation start to the time of the first frame callback. */
3816 if (time == 0)
3817 input->cursor_anim_start = 0;
3818 else if (input->cursor_anim_start == 0)
3819 input->cursor_anim_start = time;
3820
Derek Foreman118a4292015-04-22 17:23:35 -05003821 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003822
Derek Foreman118a4292015-04-22 17:23:35 -05003823 if (time == 0 || input->cursor_anim_start == 0) {
3824 duration = 0;
3825 i = 0;
3826 } else
3827 i = wl_cursor_frame_and_duration(
3828 cursor,
3829 time - input->cursor_anim_start,
3830 &duration);
3831
3832 if (cursor->image_count > 1)
3833 schedule_pointer_image_update(input, cursor, duration,
3834 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003835
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003836 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003837}
3838
Derek Foreman118a4292015-04-22 17:23:35 -05003839static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003840cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003841{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003842 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003843 struct timespec tp;
3844 struct wl_cursor *cursor;
3845 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003846
3847 if (!input->cursor_timer_running)
3848 return;
3849
Derek Foreman118a4292015-04-22 17:23:35 -05003850 cursor = input->display->cursors[input->current_cursor];
3851 if (!cursor)
3852 return;
3853
3854 clock_gettime(CLOCK_MONOTONIC, &tp);
3855 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3856 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3857}
3858
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003859static const struct wl_callback_listener pointer_surface_listener = {
3860 pointer_surface_frame_callback
3861};
3862
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003863void
3864input_set_pointer_image(struct input *input, int pointer)
3865{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003866 int force = 0;
3867
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003868 if (!input->pointer)
3869 return;
3870
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003871 if (input->pointer_enter_serial > input->cursor_serial)
3872 force = 1;
3873
3874 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003875 return;
3876
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003877 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003878 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003879 if (!input->cursor_frame_cb)
3880 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003881 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003882 /* The current frame callback may be stuck if, for instance,
3883 * the set cursor request was processed by the server after
3884 * this client lost the focus. In this case the cursor surface
3885 * might not be mapped and the frame callback wouldn't ever
3886 * complete. Send a set_cursor and attach to try to map the
3887 * cursor surface again so that the callback will finish */
3888 input_set_pointer_image_index(input, 0);
3889 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003890}
3891
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003892struct wl_data_device *
3893input_get_data_device(struct input *input)
3894{
3895 return input->data_device;
3896}
3897
3898void
3899input_set_selection(struct input *input,
3900 struct wl_data_source *source, uint32_t time)
3901{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003902 if (input->data_device)
3903 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003904}
3905
3906void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003907input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003908{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003909 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003910 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003911}
3912
3913static void
3914offer_io_func(struct task *task, uint32_t events)
3915{
3916 struct data_offer *offer =
3917 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003918 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003919 unsigned int len;
3920 char buffer[4096];
3921
3922 len = read(offer->fd, buffer, sizeof buffer);
3923 offer->func(buffer, len,
3924 offer->x, offer->y, offer->user_data);
3925
3926 if (len == 0) {
Harish Krupo43152a32019-04-19 22:06:44 +05303927 if ((offer != offer->input->selection_offer) &&
3928 (display->data_device_manager_version >=
3929 WL_DATA_OFFER_FINISH_SINCE_VERSION))
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003930 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003931 close(offer->fd);
3932 data_offer_destroy(offer);
3933 }
3934}
3935
3936static void
3937data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3938 data_func_t func, void *user_data)
3939{
3940 int p[2];
3941
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003942 if (pipe2(p, O_CLOEXEC) == -1)
3943 return;
3944
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003945 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3946 close(p[1]);
3947
3948 offer->io_task.run = offer_io_func;
3949 offer->fd = p[0];
3950 offer->func = func;
3951 offer->refcount++;
3952 offer->user_data = user_data;
3953
3954 display_watch_fd(offer->input->display,
3955 offer->fd, EPOLLIN, &offer->io_task);
3956}
3957
3958void
3959input_receive_drag_data(struct input *input, const char *mime_type,
3960 data_func_t func, void *data)
3961{
3962 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003963 input->drag_offer->x = input->drag_x;
3964 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003965}
3966
3967int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003968input_receive_drag_data_to_fd(struct input *input,
3969 const char *mime_type, int fd)
3970{
3971 if (input->drag_offer)
3972 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3973
3974 return 0;
3975}
3976
3977int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003978input_receive_selection_data(struct input *input, const char *mime_type,
3979 data_func_t func, void *data)
3980{
3981 char **p;
3982
3983 if (input->selection_offer == NULL)
3984 return -1;
3985
3986 for (p = input->selection_offer->types.data; *p; p++)
3987 if (strcmp(mime_type, *p) == 0)
3988 break;
3989
3990 if (*p == NULL)
3991 return -1;
3992
3993 data_offer_receive_data(input->selection_offer,
3994 mime_type, func, data);
3995 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003996}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003997
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003998int
3999input_receive_selection_data_to_fd(struct input *input,
4000 const char *mime_type, int fd)
4001{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004002 if (input->selection_offer)
4003 wl_data_offer_receive(input->selection_offer->offer,
4004 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004005
4006 return 0;
4007}
4008
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004009void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004010window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004011{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004012 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004013 return;
4014
ant8mef99fac22018-11-28 22:46:37 +01004015 xdg_toplevel_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004016}
4017
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004018static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004019surface_set_synchronized(struct surface *surface)
4020{
4021 if (!surface->subsurface)
4022 return;
4023
4024 if (surface->synchronized)
4025 return;
4026
4027 wl_subsurface_set_sync(surface->subsurface);
4028 surface->synchronized = 1;
4029}
4030
4031static void
4032surface_set_synchronized_default(struct surface *surface)
4033{
4034 if (!surface->subsurface)
4035 return;
4036
4037 if (surface->synchronized == surface->synchronized_default)
4038 return;
4039
4040 if (surface->synchronized_default)
4041 wl_subsurface_set_sync(surface->subsurface);
4042 else
4043 wl_subsurface_set_desync(surface->subsurface);
4044
4045 surface->synchronized = surface->synchronized_default;
4046}
4047
4048static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004049surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004050{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004051 struct widget *widget = surface->widget;
4052 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004053
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004054 if (surface->input_region) {
4055 wl_region_destroy(surface->input_region);
4056 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004057 }
4058
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004059 if (surface->opaque_region)
4060 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004061
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004062 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004063
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004064 if (widget->resize_handler)
4065 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004066 widget->allocation.width,
4067 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004068 widget->user_data);
4069
Pekka Paalanen35e82632013-04-25 13:57:48 +03004070 if (surface->subsurface &&
4071 (surface->allocation.x != widget->allocation.x ||
4072 surface->allocation.y != widget->allocation.y)) {
4073 wl_subsurface_set_position(surface->subsurface,
4074 widget->allocation.x,
4075 widget->allocation.y);
4076 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004077 if (surface->allocation.width != widget->allocation.width ||
4078 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004079 window_schedule_redraw(widget->window);
4080 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004081 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004082
4083 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004084 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004085 widget->allocation.width,
4086 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004087}
4088
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004089static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004090window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004091{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004092 struct surface *surface;
4093
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004094 widget_set_allocation(window->main_surface->widget,
4095 window->pending_allocation.x,
4096 window->pending_allocation.y,
4097 window->pending_allocation.width,
4098 window->pending_allocation.height);
4099
4100 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004101
4102 /* The main surface is in the list, too. Main surface's
4103 * resize_handler is responsible for calling widget_set_allocation()
4104 * on all sub-surface root widgets, so they will be resized
4105 * properly.
4106 */
4107 wl_list_for_each(surface, &window->subsurface_list, link) {
4108 if (surface == window->main_surface)
4109 continue;
4110
4111 surface_set_synchronized(surface);
4112 surface_resize(surface);
4113 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004114
4115 if (!window->fullscreen && !window->maximized)
4116 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004117
4118 if (window->confined && window->confined_widget) {
4119 struct wl_compositor *compositor = window->display->compositor;
4120 struct wl_region *region;
4121 struct widget *widget = window->confined_widget;
4122
4123 region = wl_compositor_create_region(compositor);
4124 wl_region_add(region,
4125 widget->allocation.x,
4126 widget->allocation.y,
4127 widget->allocation.width,
4128 widget->allocation.height);
4129 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4130 region);
4131 wl_region_destroy(region);
4132 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004133}
4134
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004135static void
4136idle_resize(struct window *window)
4137{
4138 window->resize_needed = 0;
4139 window->redraw_needed = 1;
4140
4141 DBG("from %dx%d to %dx%d\n",
4142 window->main_surface->server_allocation.width,
4143 window->main_surface->server_allocation.height,
4144 window->pending_allocation.width,
4145 window->pending_allocation.height);
4146
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004147 window_do_resize(window);
4148}
4149
4150static void
4151undo_resize(struct window *window)
4152{
4153 window->pending_allocation.width =
4154 window->main_surface->server_allocation.width;
4155 window->pending_allocation.height =
4156 window->main_surface->server_allocation.height;
4157
4158 DBG("back to %dx%d\n",
4159 window->main_surface->server_allocation.width,
4160 window->main_surface->server_allocation.height);
4161
4162 window_do_resize(window);
4163
4164 if (window->pending_allocation.width == 0 &&
4165 window->pending_allocation.height == 0) {
4166 fprintf(stderr, "Error: Could not draw a surface, "
4167 "most likely due to insufficient disk space in "
4168 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4169 exit(EXIT_FAILURE);
4170 }
4171}
4172
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004173void
4174window_schedule_resize(struct window *window, int width, int height)
4175{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004176 /* We should probably get these numbers from the theme. */
4177 const int min_width = 200, min_height = 200;
4178
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004179 window->pending_allocation.x = 0;
4180 window->pending_allocation.y = 0;
4181 window->pending_allocation.width = width;
4182 window->pending_allocation.height = height;
4183
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004184 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004185 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004186 window->min_allocation.width = min_width;
4187 else
4188 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004189 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004190 window->min_allocation.height = min_height;
4191 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004192 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004193 }
4194
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004195 if (window->pending_allocation.width < window->min_allocation.width)
4196 window->pending_allocation.width = window->min_allocation.width;
4197 if (window->pending_allocation.height < window->min_allocation.height)
4198 window->pending_allocation.height = window->min_allocation.height;
4199
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004200 window->resize_needed = 1;
4201 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004202}
4203
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004204void
4205widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4206{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004207 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004208}
4209
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004210static int
4211window_get_shadow_margin(struct window *window)
4212{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004213 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004214 return frame_get_shadow_margin(window->frame->frame);
4215 else
4216 return 0;
4217}
4218
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004219void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004220window_inhibit_redraw(struct window *window)
4221{
4222 window->redraw_inhibited = 1;
4223 wl_list_remove(&window->redraw_task.link);
4224 wl_list_init(&window->redraw_task.link);
4225 window->redraw_task_scheduled = 0;
4226}
4227
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004228void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004229window_uninhibit_redraw(struct window *window)
4230{
4231 window->redraw_inhibited = 0;
4232 if (window->redraw_needed || window->resize_needed)
4233 window_schedule_redraw_task(window);
4234}
4235
4236static void
4237xdg_surface_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01004238 struct xdg_surface *xdg_surface,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004239 uint32_t serial)
4240{
4241 struct window *window = data;
4242
ant8mef99fac22018-11-28 22:46:37 +01004243 xdg_surface_ack_configure(window->xdg_surface, serial);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004244
4245 if (window->state_changed_handler)
4246 window->state_changed_handler(window, window->user_data);
4247
4248 window_uninhibit_redraw(window);
4249}
4250
ant8mef99fac22018-11-28 22:46:37 +01004251static const struct xdg_surface_listener xdg_surface_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004252 xdg_surface_handle_configure
4253};
4254
4255static void
ant8mef99fac22018-11-28 22:46:37 +01004256xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *xdg_toplevel,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004257 int32_t width, int32_t height,
4258 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004259{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004260 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004261 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004262
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004263 window->maximized = 0;
4264 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004265 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004266 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004267
4268 wl_array_for_each(p, states) {
4269 uint32_t state = *p;
4270 switch (state) {
ant8mef99fac22018-11-28 22:46:37 +01004271 case XDG_TOPLEVEL_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004272 window->maximized = 1;
4273 break;
ant8mef99fac22018-11-28 22:46:37 +01004274 case XDG_TOPLEVEL_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004275 window->fullscreen = 1;
4276 break;
ant8mef99fac22018-11-28 22:46:37 +01004277 case XDG_TOPLEVEL_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004278 window->resizing = 1;
4279 break;
ant8mef99fac22018-11-28 22:46:37 +01004280 case XDG_TOPLEVEL_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004281 window->focused = 1;
4282 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004283 default:
4284 /* Unknown state */
4285 break;
4286 }
4287 }
4288
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004289 if (window->frame) {
4290 if (window->maximized) {
4291 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4292 } else {
4293 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4294 }
4295
4296 if (window->focused) {
4297 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4298 } else {
4299 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4300 }
4301 }
4302
Jasper St. Pierref184c382014-05-06 08:33:27 -04004303 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004304 /* The width / height params are for window geometry,
4305 * but window_schedule_resize takes allocation. Add
4306 * on the shadow margin to get the difference. */
4307 int margin = window_get_shadow_margin(window);
4308
4309 window_schedule_resize(window,
4310 width + margin * 2,
4311 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004312 } else if (window->saved_allocation.width > 0 &&
4313 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004314 window_schedule_resize(window,
4315 window->saved_allocation.width,
4316 window->saved_allocation.height);
4317 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004318}
4319
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004320static void
ant8mef99fac22018-11-28 22:46:37 +01004321xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004322{
4323 struct window *window = data;
4324 window_close(window);
4325}
4326
ant8mef99fac22018-11-28 22:46:37 +01004327static const struct xdg_toplevel_listener xdg_toplevel_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004328 xdg_toplevel_handle_configure,
4329 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004330};
4331
4332static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004333window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004334{
ant8mef99fac22018-11-28 22:46:37 +01004335 struct xdg_toplevel *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004336
4337 if (!window->xdg_surface)
4338 return;
4339
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004340 if (window->parent == window->last_parent)
4341 return;
4342
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004343 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004344 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004345 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004346 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004347
ant8mef99fac22018-11-28 22:46:37 +01004348 xdg_toplevel_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004349 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004350}
4351
4352static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004353window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004354{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004355 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004356 frame_input_rect(window->frame->frame,
4357 &geometry->x,
4358 &geometry->y,
4359 &geometry->width,
4360 &geometry->height);
4361 else
4362 window_get_allocation(window, geometry);
4363}
4364
4365static void
4366window_sync_geometry(struct window *window)
4367{
4368 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004369
4370 if (!window->xdg_surface)
4371 return;
4372
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004373 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004374 if (geometry.x == window->last_geometry.x &&
4375 geometry.y == window->last_geometry.y &&
4376 geometry.width == window->last_geometry.width &&
4377 geometry.height == window->last_geometry.height)
4378 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004379
ant8mef99fac22018-11-28 22:46:37 +01004380 xdg_surface_set_window_geometry(window->xdg_surface,
4381 geometry.x,
4382 geometry.y,
4383 geometry.width,
4384 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004385 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004386}
4387
4388static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004389window_flush(struct window *window)
4390{
4391 struct surface *surface;
4392
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004393 assert(!window->redraw_inhibited);
4394
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004395 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004396 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004397 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004398 if (window->xdg_toplevel)
4399 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004400 }
4401
4402 wl_list_for_each(surface, &window->subsurface_list, link) {
4403 if (surface == window->main_surface)
4404 continue;
4405
4406 surface_flush(surface);
4407 }
4408
4409 surface_flush(window->main_surface);
4410}
4411
4412static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004413menu_destroy(struct menu *menu)
4414{
4415 widget_destroy(menu->widget);
4416 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004417 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004418 free(menu);
4419}
4420
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004421void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004422window_get_allocation(struct window *window,
4423 struct rectangle *allocation)
4424{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004425 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004426}
4427
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004428static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004429widget_redraw(struct widget *widget)
4430{
4431 struct widget *child;
4432
4433 if (widget->redraw_handler)
4434 widget->redraw_handler(widget, widget->user_data);
4435 wl_list_for_each(child, &widget->child_list, link)
4436 widget_redraw(child);
4437}
4438
4439static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004440frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4441{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004442 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004443
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004444 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004445 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004446 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004447 surface->frame_cb = NULL;
4448
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004449 surface->last_time = time;
4450
Pekka Paalanen71233882013-04-25 13:57:53 +03004451 if (surface->redraw_needed || surface->window->redraw_needed) {
4452 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004453 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004454 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004455}
4456
4457static const struct wl_callback_listener listener = {
4458 frame_callback
4459};
4460
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004461static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004462surface_redraw(struct surface *surface)
4463{
Pekka Paalanen71233882013-04-25 13:57:53 +03004464 DBG_OBJ(surface->surface, "begin\n");
4465
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004466 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004467 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004468
4469 /* Whole-window redraw forces a redraw even if the previous has
4470 * not yet hit the screen.
4471 */
4472 if (surface->frame_cb) {
4473 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004474 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004475
Pekka Paalanen71233882013-04-25 13:57:53 +03004476 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004477 wl_callback_destroy(surface->frame_cb);
4478 }
4479
Neil Roberts97b747c2013-12-19 16:17:12 +00004480 if (surface->widget->use_cairo &&
4481 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004482 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004483 return -1;
4484 }
4485
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004486 surface->frame_cb = wl_surface_frame(surface->surface);
4487 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004488 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004489
4490 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004491 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004492 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004493 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004494 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004495}
4496
4497static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004498idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004499{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004500 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004501 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004502 int failed = 0;
4503 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004504
Pekka Paalanen71233882013-04-25 13:57:53 +03004505 DBG(" --------- \n");
4506
Pekka Paalaneneebff542013-04-25 13:57:52 +03004507 wl_list_init(&window->redraw_task.link);
4508 window->redraw_task_scheduled = 0;
4509
4510 if (window->resize_needed) {
4511 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004512 if (window->main_surface->frame_cb) {
4513 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004514 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004515 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004516
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004517 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004518 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004519 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004520
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004521 if (surface_redraw(window->main_surface) < 0) {
4522 /*
4523 * Only main_surface failure will cause us to undo the resize.
4524 * If sub-surfaces fail, they will just be broken with old
4525 * content.
4526 */
4527 failed = 1;
4528 } else {
4529 wl_list_for_each(surface, &window->subsurface_list, link) {
4530 if (surface == window->main_surface)
4531 continue;
4532
4533 surface_redraw(surface);
4534 }
4535 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004536
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004537 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004538 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004539
4540 wl_list_for_each(surface, &window->subsurface_list, link)
4541 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004542
4543 if (resized && failed) {
4544 /* Restore widget tree to correspond to what is on screen. */
4545 undo_resize(window);
4546 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004547}
4548
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004549static void
4550window_schedule_redraw_task(struct window *window)
4551{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004552 if (window->redraw_inhibited)
4553 return;
4554
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004555 if (!window->redraw_task_scheduled) {
4556 window->redraw_task.run = idle_redraw;
4557 display_defer(window->display, &window->redraw_task);
4558 window->redraw_task_scheduled = 1;
4559 }
4560}
4561
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004562void
4563window_schedule_redraw(struct window *window)
4564{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004565 struct surface *surface;
4566
Pekka Paalanen71233882013-04-25 13:57:53 +03004567 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4568
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004569 wl_list_for_each(surface, &window->subsurface_list, link)
4570 surface->redraw_needed = 1;
4571
4572 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004573}
4574
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004575int
4576window_is_fullscreen(struct window *window)
4577{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004578 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004579}
4580
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004581void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004582window_set_fullscreen(struct window *window, int fullscreen)
4583{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004584 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004585 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004586
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004587 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004588 return;
4589
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004590 if (fullscreen)
ant8mef99fac22018-11-28 22:46:37 +01004591 xdg_toplevel_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004592 else
ant8mef99fac22018-11-28 22:46:37 +01004593 xdg_toplevel_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004594}
4595
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004596int
4597window_is_maximized(struct window *window)
4598{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004599 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004600}
4601
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004602void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004603window_set_maximized(struct window *window, int maximized)
4604{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004605 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004606 return;
4607
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004608 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004609 return;
4610
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004611 if (maximized)
ant8mef99fac22018-11-28 22:46:37 +01004612 xdg_toplevel_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004613 else
ant8mef99fac22018-11-28 22:46:37 +01004614 xdg_toplevel_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004615}
4616
Jasper St. Pierrede680992014-04-10 17:23:49 -07004617int
4618window_is_resizing(struct window *window)
4619{
4620 return window->resizing;
4621}
4622
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004623void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004624window_set_minimized(struct window *window)
4625{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004626 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004627 return;
4628
ant8mef99fac22018-11-28 22:46:37 +01004629 xdg_toplevel_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004630}
4631
4632void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004633window_set_user_data(struct window *window, void *data)
4634{
4635 window->user_data = data;
4636}
4637
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004638void *
4639window_get_user_data(struct window *window)
4640{
4641 return window->user_data;
4642}
4643
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004644void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004645window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004646 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004647{
4648 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004649}
4650
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004651void
4652window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004653 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004654{
4655 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004656}
4657
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004658void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004659window_set_data_handler(struct window *window, window_data_handler_t handler)
4660{
4661 window->data_handler = handler;
4662}
4663
4664void
4665window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4666{
4667 window->drop_handler = handler;
4668}
4669
4670void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004671window_set_close_handler(struct window *window,
4672 window_close_handler_t handler)
4673{
4674 window->close_handler = handler;
4675}
4676
4677void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004678window_set_fullscreen_handler(struct window *window,
4679 window_fullscreen_handler_t handler)
4680{
4681 window->fullscreen_handler = handler;
4682}
4683
4684void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004685window_set_output_handler(struct window *window,
4686 window_output_handler_t handler)
4687{
4688 window->output_handler = handler;
4689}
4690
4691void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004692window_set_state_changed_handler(struct window *window,
4693 window_state_changed_handler_t handler)
4694{
4695 window->state_changed_handler = handler;
4696}
4697
4698void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004699window_set_pointer_locked_handler(struct window *window,
4700 locked_pointer_locked_handler_t locked,
4701 locked_pointer_unlocked_handler_t unlocked)
4702{
4703 window->pointer_unlocked_handler = unlocked;
4704 window->pointer_locked_handler = locked;
4705}
4706
4707void
4708window_set_pointer_confined_handler(struct window *window,
4709 confined_pointer_confined_handler_t confined,
4710 confined_pointer_unconfined_handler_t unconfined)
4711{
4712 window->pointer_confined_handler = confined;
4713 window->pointer_unconfined_handler = unconfined;
4714}
4715
4716void
4717window_set_locked_pointer_motion_handler(struct window *window,
4718 window_locked_pointer_motion_handler_t handler)
4719{
4720 window->locked_pointer_motion_handler = handler;
4721}
4722
4723void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004724window_set_title(struct window *window, const char *title)
4725{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004726 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004727 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004728 if (window->frame) {
4729 frame_set_title(window->frame->frame, title);
4730 widget_schedule_redraw(window->frame->widget);
4731 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004732 if (window->xdg_toplevel)
ant8mef99fac22018-11-28 22:46:37 +01004733 xdg_toplevel_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004734}
4735
4736const char *
4737window_get_title(struct window *window)
4738{
4739 return window->title;
4740}
4741
4742void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004743window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4744{
4745 struct text_cursor_position *text_cursor_position =
4746 window->display->text_cursor_position;
4747
Scott Moreau9295ce02012-06-01 12:46:10 -06004748 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004749 return;
4750
4751 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004752 window->main_surface->surface,
4753 wl_fixed_from_int(x),
4754 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004755}
4756
Casey Dahlin9074db52012-04-19 22:50:09 -04004757static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004758relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4759 uint32_t utime_hi,
4760 uint32_t utime_lo,
4761 wl_fixed_t dx,
4762 wl_fixed_t dy,
4763 wl_fixed_t dx_unaccel,
4764 wl_fixed_t dy_unaccel)
4765{
4766 struct input *input = data;
4767 struct window *window = input->pointer_focus;
4768 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4769
4770 if (window->locked_pointer_motion_handler &&
4771 window->pointer_locked) {
4772 window->locked_pointer_motion_handler(
4773 window, input, ms,
4774 wl_fixed_to_double(dx),
4775 wl_fixed_to_double(dy),
4776 window->user_data);
4777 }
4778}
4779
4780static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4781 relative_pointer_handle_motion,
4782};
4783
4784static void
4785locked_pointer_locked(void *data,
4786 struct zwp_locked_pointer_v1 *locked_pointer)
4787{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004788 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004789 struct window *window = input->locked_window;
4790
4791 if (!window)
4792 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004793
4794 window->pointer_locked = true;
4795
4796 if (window->pointer_locked_handler) {
4797 window->pointer_locked_handler(window,
4798 input,
4799 window->user_data);
4800 }
4801}
4802
4803static void
4804locked_pointer_unlocked(void *data,
4805 struct zwp_locked_pointer_v1 *locked_pointer)
4806{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004807 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004808 struct window *window = input->locked_window;
4809
4810 if (!window)
4811 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004812
4813 window_unlock_pointer(window);
4814
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004815 input->locked_window = NULL;
4816
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004817 if (window->pointer_unlocked_handler) {
4818 window->pointer_unlocked_handler(window,
4819 input,
4820 window->user_data);
4821 }
4822}
4823
4824static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4825 locked_pointer_locked,
4826 locked_pointer_unlocked,
4827};
4828
4829int
4830window_lock_pointer(struct window *window, struct input *input)
4831{
4832 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4833 window->display->relative_pointer_manager;
4834 struct zwp_pointer_constraints_v1 *pointer_constraints =
4835 window->display->pointer_constraints;
4836 struct zwp_relative_pointer_v1 *relative_pointer;
4837 struct zwp_locked_pointer_v1 *locked_pointer;
4838
4839 if (!window->display->relative_pointer_manager)
4840 return -1;
4841
4842 if (!window->display->pointer_constraints)
4843 return -1;
4844
4845 if (window->locked_pointer)
4846 return -1;
4847
4848 if (window->confined_pointer)
4849 return -1;
4850
4851 if (!input->pointer)
4852 return -1;
4853
4854 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4855 relative_pointer_manager, input->pointer);
4856 zwp_relative_pointer_v1_add_listener(relative_pointer,
4857 &relative_pointer_listener,
4858 input);
4859
4860 locked_pointer =
4861 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4862 window->main_surface->surface,
4863 input->pointer,
4864 NULL,
4865 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4866 zwp_locked_pointer_v1_add_listener(locked_pointer,
4867 &locked_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004868 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004869
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004870 window->locked_pointer = locked_pointer;
4871 window->relative_pointer = relative_pointer;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004872 input->locked_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004873
4874 return 0;
4875}
4876
4877void
4878window_unlock_pointer(struct window *window)
4879{
4880 if (!window->locked_pointer)
4881 return;
4882
4883 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4884 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4885 window->locked_pointer = NULL;
4886 window->relative_pointer = NULL;
4887 window->pointer_locked = false;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004888}
4889
4890void
4891widget_set_locked_pointer_cursor_hint(struct widget *widget,
4892 float x, float y)
4893{
4894 struct window *window = widget->window;
4895
4896 if (!window->locked_pointer)
4897 return;
4898
4899 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4900 wl_fixed_from_double(x),
4901 wl_fixed_from_double(y));
4902 wl_surface_commit(window->main_surface->surface);
4903}
4904
4905static void
4906confined_pointer_confined(void *data,
4907 struct zwp_confined_pointer_v1 *confined_pointer)
4908{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004909 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004910 struct window *window = input->confined_window;
4911
4912 if (!window)
4913 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004914
4915 window->confined = true;
4916
4917 if (window->pointer_confined_handler) {
4918 window->pointer_confined_handler(window,
4919 input,
4920 window->user_data);
4921 }
4922}
4923
4924static void
4925confined_pointer_unconfined(void *data,
4926 struct zwp_confined_pointer_v1 *confined_pointer)
4927{
Dima Ryazanov336ce672018-11-14 21:03:35 -08004928 struct input *input = data;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004929 struct window *window = input->confined_window;
4930
4931 if (!window)
4932 return;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004933
4934 window_unconfine_pointer(window);
4935
4936 window->confined = false;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08004937 input->confined_window = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004938
4939 if (window->pointer_unconfined_handler) {
4940 window->pointer_unconfined_handler(window,
4941 input,
4942 window->user_data);
4943 }
4944}
4945
4946static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4947 confined_pointer_confined,
4948 confined_pointer_unconfined,
4949};
4950
4951int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004952window_confine_pointer_to_rectangles(struct window *window,
4953 struct input *input,
4954 struct rectangle *rectangles,
4955 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004956{
4957 struct zwp_pointer_constraints_v1 *pointer_constraints =
4958 window->display->pointer_constraints;
4959 struct zwp_confined_pointer_v1 *confined_pointer;
4960 struct wl_compositor *compositor = window->display->compositor;
4961 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004962 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004963
4964 if (!window->display->pointer_constraints)
4965 return -1;
4966
4967 if (window->locked_pointer)
4968 return -1;
4969
4970 if (window->confined_pointer)
4971 return -1;
4972
4973 if (!input->pointer)
4974 return -1;
4975
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004976 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004977 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004978 for (i = 0; i < num_rectangles; i++) {
4979 wl_region_add(region,
4980 rectangles[i].x,
4981 rectangles[i].y,
4982 rectangles[i].width,
4983 rectangles[i].height);
4984 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004985 }
4986
4987 confined_pointer =
4988 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
4989 window->main_surface->surface,
4990 input->pointer,
4991 region,
4992 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4993 if (region)
4994 wl_region_destroy(region);
4995
4996 zwp_confined_pointer_v1_add_listener(confined_pointer,
4997 &confined_pointer_listener,
Dima Ryazanov336ce672018-11-14 21:03:35 -08004998 input);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004999
5000 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005001 window->confined_widget = NULL;
Dima Ryazanov13bdf252018-11-14 22:17:42 -08005002 input->confined_window = window;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005003
5004 return 0;
5005}
5006
5007void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005008window_update_confine_rectangles(struct window *window,
5009 struct rectangle *rectangles,
5010 int num_rectangles)
5011{
5012 struct wl_compositor *compositor = window->display->compositor;
5013 struct wl_region *region;
5014 int i;
5015
5016 region = wl_compositor_create_region(compositor);
5017 for (i = 0; i < num_rectangles; i++) {
5018 wl_region_add(region,
5019 rectangles[i].x,
5020 rectangles[i].y,
5021 rectangles[i].width,
5022 rectangles[i].height);
5023 }
5024
5025 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5026
5027 wl_region_destroy(region);
5028}
5029
5030int
5031window_confine_pointer_to_widget(struct window *window,
5032 struct widget *widget,
5033 struct input *input)
5034{
5035 int ret;
5036
5037 if (widget) {
5038 ret = window_confine_pointer_to_rectangles(window,
5039 input,
5040 &widget->allocation,
5041 1);
5042 window->confined_widget = widget;
5043 return ret;
5044 } else {
5045 return window_confine_pointer_to_rectangles(window,
5046 input,
5047 NULL,
5048 0);
5049 }
5050}
5051
5052void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005053window_unconfine_pointer(struct window *window)
5054{
5055 if (!window->confined_pointer)
5056 return;
5057
5058 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5059 window->confined_pointer = NULL;
5060 window->confined = false;
5061}
5062
5063static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005064surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005065 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005066{
Rob Bradford7507b572012-05-15 17:55:34 +01005067 struct window *window = data;
5068 struct output *output;
5069 struct output *output_found = NULL;
5070 struct window_output *window_output;
5071
5072 wl_list_for_each(output, &window->display->output_list, link) {
5073 if (output->output == wl_output) {
5074 output_found = output;
5075 break;
5076 }
5077 }
5078
5079 if (!output_found)
5080 return;
5081
Brian Lovinbc919262013-08-07 15:34:59 -07005082 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005083 window_output->output = output_found;
5084
5085 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005086
5087 if (window->output_handler)
5088 window->output_handler(window, output_found, 1,
5089 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005090}
5091
5092static void
5093surface_leave(void *data,
5094 struct wl_surface *wl_surface, struct wl_output *output)
5095{
Rob Bradford7507b572012-05-15 17:55:34 +01005096 struct window *window = data;
5097 struct window_output *window_output;
5098 struct window_output *window_output_found = NULL;
5099
5100 wl_list_for_each(window_output, &window->window_output_list, link) {
5101 if (window_output->output->output == output) {
5102 window_output_found = window_output;
5103 break;
5104 }
5105 }
5106
5107 if (window_output_found) {
5108 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005109
5110 if (window->output_handler)
5111 window->output_handler(window, window_output->output,
5112 0, window->user_data);
5113
Rob Bradford7507b572012-05-15 17:55:34 +01005114 free(window_output_found);
5115 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005116}
5117
5118static const struct wl_surface_listener surface_listener = {
5119 surface_enter,
5120 surface_leave
5121};
5122
Pekka Paalanen4e373742013-02-13 16:17:13 +02005123static struct surface *
5124surface_create(struct window *window)
5125{
5126 struct display *display = window->display;
5127 struct surface *surface;
5128
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005129 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005130 surface->window = window;
5131 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005132 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005133 wl_surface_add_listener(surface->surface, &surface_listener, window);
5134
Pekka Paalanen35e82632013-04-25 13:57:48 +03005135 wl_list_insert(&window->subsurface_list, &surface->link);
5136
Pekka Paalanen4e373742013-02-13 16:17:13 +02005137 return surface;
5138}
5139
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005140static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005141get_preferred_buffer_type(struct display *display)
5142{
5143#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005144 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005145 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5146#endif
5147
5148 return WINDOW_BUFFER_TYPE_SHM;
5149}
5150
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005151static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005152window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005153{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005154 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005155 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005156
Peter Huttererf3d62272013-08-08 11:57:05 +10005157 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005158 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005159 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005160
5161 surface = surface_create(window);
5162 window->main_surface = surface;
5163
Michael Teyfeld6371b52017-10-11 17:17:29 +02005164 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005165
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005166 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005167 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005168
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005169 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005170
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005171 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005172 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005173 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005174
Rob Bradford7507b572012-05-15 17:55:34 +01005175 wl_list_init (&window->window_output_list);
5176
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005177 return window;
5178}
5179
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005180struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005181window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005182{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005183 struct window *window;
5184
5185 window = window_create_internal(display, 0);
5186
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005187 if (window->display->xdg_shell) {
5188 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005189 xdg_wm_base_get_xdg_surface(window->display->xdg_shell,
5190 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005191 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005192
ant8mef99fac22018-11-28 22:46:37 +01005193 xdg_surface_add_listener(window->xdg_surface,
5194 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005195
5196 window->xdg_toplevel =
ant8mef99fac22018-11-28 22:46:37 +01005197 xdg_surface_get_toplevel(window->xdg_surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005198 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5199
ant8mef99fac22018-11-28 22:46:37 +01005200 xdg_toplevel_add_listener(window->xdg_toplevel,
5201 &xdg_toplevel_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005202
5203 window_inhibit_redraw(window);
5204
5205 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005206 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005207
5208 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005209}
5210
5211struct window *
5212window_create_custom(struct display *display)
5213{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005214 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005215}
5216
Jasper St. Pierre53686042013-12-09 15:26:25 -05005217void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005218window_set_parent(struct window *window,
5219 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005220{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005221 window->parent = parent_window;
5222 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005223}
5224
5225struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005226window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005227{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005228 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005229}
5230
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005231static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005232menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005233{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005234 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005235 int next;
5236
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005237 frame_interior(menu->frame, &x, &y, &width, &height);
5238 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005239 if (menu->current != next) {
5240 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005241 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005242 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005243}
5244
5245static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005246menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005247 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005248 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005249{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005250 struct menu *menu = data;
5251
5252 if (widget == menu->widget)
5253 menu_set_item(data, y);
5254
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005255 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005256}
5257
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005258static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005259menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005260 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005261{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005262 struct menu *menu = data;
5263
5264 if (widget == menu->widget)
5265 menu_set_item(data, y);
5266
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005267 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005268}
5269
5270static void
5271menu_leave_handler(struct widget *widget, struct input *input, void *data)
5272{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005273 struct menu *menu = data;
5274
5275 if (widget == menu->widget)
5276 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005277}
5278
5279static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005280menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005281 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005282 uint32_t button, enum wl_pointer_button_state state,
5283 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005284
5285{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005286 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005287
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005288 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5289 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005290 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005291 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005292 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005293 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005294 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005295 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005296 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005297 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005298}
5299
5300static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005301menu_touch_up_handler(struct widget *widget,
5302 struct input *input,
5303 uint32_t serial,
5304 uint32_t time,
5305 int32_t id,
5306 void *data)
5307{
5308 struct menu *menu = data;
5309
5310 input_ungrab(input);
5311 menu_destroy(menu);
5312}
5313
5314static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005315menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005316{
5317 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005318 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005319 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005320
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005321 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005322
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005323 frame_repaint(menu->frame, cr);
5324 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005325
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005326 theme_set_background_source(menu->window->display->theme,
5327 cr, THEME_FRAME_ACTIVE);
5328 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005329 cairo_fill(cr);
5330
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005331 cairo_select_font_face(cr, "sans",
5332 CAIRO_FONT_SLANT_NORMAL,
5333 CAIRO_FONT_WEIGHT_NORMAL);
5334 cairo_set_font_size(cr, 12);
5335
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005336 for (i = 0; i < menu->count; i++) {
5337 if (i == menu->current) {
5338 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005339 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005340 cairo_fill(cr);
5341 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005342 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005343 cairo_show_text(cr, menu->entries[i]);
5344 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005345 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5346 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005347 cairo_show_text(cr, menu->entries[i]);
5348 }
5349 }
5350
5351 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005352}
5353
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005354static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005355xdg_popup_handle_configure(void *data,
ant8mef99fac22018-11-28 22:46:37 +01005356 struct xdg_popup *xdg_popup,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005357 int32_t x,
5358 int32_t y,
5359 int32_t width,
5360 int32_t height)
5361{
5362}
5363
5364static void
ant8mef99fac22018-11-28 22:46:37 +01005365xdg_popup_handle_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005366{
5367 struct window *window = data;
5368 struct menu *menu = window->main_surface->widget->user_data;
5369
5370 input_ungrab(menu->input);
5371 menu_destroy(menu);
5372}
5373
ant8mef99fac22018-11-28 22:46:37 +01005374static const struct xdg_popup_listener xdg_popup_listener = {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005375 xdg_popup_handle_configure,
5376 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005377};
5378
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005379static struct menu *
5380create_menu(struct display *display,
5381 struct input *input, uint32_t time,
5382 menu_func_t func, const char **entries, int count,
5383 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005384{
5385 struct window *window;
5386 struct menu *menu;
5387
5388 menu = malloc(sizeof *menu);
5389 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005390 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005391
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005392 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005393 if (!window) {
5394 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005395 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005396 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005397
5398 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005399 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005400 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005401 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005402 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005403 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005404 menu->entries = entries;
5405 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005406 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005407 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005408 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005409 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005410 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005411
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005412 input_ungrab(input);
5413
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005414 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005415 widget_set_enter_handler(menu->widget, menu_enter_handler);
5416 widget_set_leave_handler(menu->widget, menu_leave_handler);
5417 widget_set_motion_handler(menu->widget, menu_motion_handler);
5418 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005419 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005420
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005421 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005422 frame_resize_inside(menu->frame, 200, count * 20);
5423 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5424 window_schedule_resize(window, frame_width(menu->frame),
5425 frame_height(menu->frame));
5426
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005427 return menu;
5428}
5429
ant8mef99fac22018-11-28 22:46:37 +01005430static struct xdg_positioner *
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005431create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005432 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005433{
ant8mef99fac22018-11-28 22:46:37 +01005434 struct xdg_positioner *positioner;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005435
ant8mef99fac22018-11-28 22:46:37 +01005436 positioner = xdg_wm_base_create_positioner(display->xdg_shell);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005437 fail_on_null(positioner, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005438 xdg_positioner_set_anchor_rect(positioner, x, y, 1, 1);
5439 xdg_positioner_set_size(positioner, w, h);
5440 xdg_positioner_set_anchor(positioner,
5441 XDG_POSITIONER_ANCHOR_TOP_LEFT);
5442 xdg_positioner_set_gravity(positioner,
5443 XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005444
5445 return positioner;
5446}
5447
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005448void
5449window_show_menu(struct display *display,
5450 struct input *input, uint32_t time, struct window *parent,
5451 int32_t x, int32_t y,
5452 menu_func_t func, const char **entries, int count)
5453{
5454 struct menu *menu;
5455 struct window *window;
5456 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005457 struct rectangle parent_geometry;
ant8mef99fac22018-11-28 22:46:37 +01005458 struct xdg_positioner *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005459
5460 menu = create_menu(display, input, time, func, entries, count, parent);
5461
5462 if (menu == NULL)
5463 return;
5464
5465 window = menu->window;
5466
5467 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5468 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5469
5470 window->x = x;
5471 window->y = y;
5472
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005473 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005474 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005475
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005476 if (!display->xdg_shell)
5477 return;
5478
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005479 window->xdg_surface =
ant8mef99fac22018-11-28 22:46:37 +01005480 xdg_wm_base_get_xdg_surface(display->xdg_shell,
5481 window->main_surface->surface);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005482 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005483
ant8mef99fac22018-11-28 22:46:37 +01005484 xdg_surface_add_listener(window->xdg_surface,
5485 &xdg_surface_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005486
5487 positioner = create_simple_positioner(display,
5488 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005489 window->y - (iy + parent_geometry.y),
5490 frame_width(menu->frame),
5491 frame_height(menu->frame));
ant8mef99fac22018-11-28 22:46:37 +01005492 window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
5493 parent->xdg_surface,
5494 positioner);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005495 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
ant8mef99fac22018-11-28 22:46:37 +01005496 xdg_positioner_destroy(positioner);
5497 xdg_popup_grab(window->xdg_popup, input->seat,
5498 display_get_serial(window->display));
5499 xdg_popup_add_listener(window->xdg_popup,
5500 &xdg_popup_listener, window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005501
5502 window_inhibit_redraw(window);
5503
5504 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005505}
5506
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005507void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005508window_set_buffer_type(struct window *window, enum window_buffer_type type)
5509{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005510 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005511}
5512
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005513enum window_buffer_type
5514window_get_buffer_type(struct window *window)
5515{
5516 return window->main_surface->buffer_type;
5517}
5518
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005519void
5520window_set_preferred_format(struct window *window,
5521 enum preferred_format format)
5522{
5523 window->preferred_format = format;
5524}
5525
Pekka Paalanen35e82632013-04-25 13:57:48 +03005526struct widget *
5527window_add_subsurface(struct window *window, void *data,
5528 enum subsurface_mode default_mode)
5529{
5530 struct widget *widget;
5531 struct surface *surface;
5532 struct wl_surface *parent;
5533 struct wl_subcompositor *subcompo = window->display->subcompositor;
5534
Pekka Paalanen35e82632013-04-25 13:57:48 +03005535 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005536 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005537 widget = widget_create(window, surface, data);
5538 wl_list_init(&widget->link);
5539 surface->widget = widget;
5540
5541 parent = window->main_surface->surface;
5542 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5543 surface->surface,
5544 parent);
5545 surface->synchronized = 1;
5546
5547 switch (default_mode) {
5548 case SUBSURFACE_SYNCHRONIZED:
5549 surface->synchronized_default = 1;
5550 break;
5551 case SUBSURFACE_DESYNCHRONIZED:
5552 surface->synchronized_default = 0;
5553 break;
5554 default:
5555 assert(!"bad enum subsurface_mode");
5556 }
5557
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005558 window->resize_needed = 1;
5559 window_schedule_redraw(window);
5560
Pekka Paalanen35e82632013-04-25 13:57:48 +03005561 return widget;
5562}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005563
5564static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005565display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005566 struct wl_output *wl_output,
5567 int x, int y,
5568 int physical_width,
5569 int physical_height,
5570 int subpixel,
5571 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005572 const char *model,
5573 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005574{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005575 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005576
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005577 output->allocation.x = x;
5578 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005579 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005580
5581 if (output->make)
5582 free(output->make);
5583 output->make = strdup(make);
5584
5585 if (output->model)
5586 free(output->model);
5587 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005588}
5589
5590static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005591display_handle_done(void *data,
5592 struct wl_output *wl_output)
5593{
5594}
5595
5596static void
5597display_handle_scale(void *data,
5598 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005599 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005600{
5601 struct output *output = data;
5602
5603 output->scale = scale;
5604}
5605
5606static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005607display_handle_mode(void *data,
5608 struct wl_output *wl_output,
5609 uint32_t flags,
5610 int width,
5611 int height,
5612 int refresh)
5613{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005614 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005615 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005616
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005617 if (flags & WL_OUTPUT_MODE_CURRENT) {
5618 output->allocation.width = width;
5619 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005620 if (display->output_configure_handler)
5621 (*display->output_configure_handler)(
5622 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005623 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005624}
5625
5626static const struct wl_output_listener output_listener = {
5627 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005628 display_handle_mode,
5629 display_handle_done,
5630 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005631};
5632
5633static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005634display_add_output(struct display *d, uint32_t id)
5635{
5636 struct output *output;
5637
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005638 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005639 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005640 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005641 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005642 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005643 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005644 wl_list_insert(d->output_list.prev, &output->link);
5645
5646 wl_output_add_listener(output->output, &output_listener, output);
5647}
5648
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005649static void
5650output_destroy(struct output *output)
5651{
5652 if (output->destroy_handler)
5653 (*output->destroy_handler)(output, output->user_data);
5654
5655 wl_output_destroy(output->output);
5656 wl_list_remove(&output->link);
5657 free(output);
5658}
5659
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005660static void
5661display_destroy_output(struct display *d, uint32_t id)
5662{
5663 struct output *output;
5664
5665 wl_list_for_each(output, &d->output_list, link) {
5666 if (output->server_output_id == id) {
5667 output_destroy(output);
5668 break;
5669 }
5670 }
5671}
5672
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005673void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005674display_set_global_handler(struct display *display,
5675 display_global_handler_t handler)
5676{
5677 struct global *global;
5678
5679 display->global_handler = handler;
5680 if (!handler)
5681 return;
5682
5683 wl_list_for_each(global, &display->global_list, link)
5684 display->global_handler(display,
5685 global->name, global->interface,
5686 global->version, display->user_data);
5687}
5688
5689void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005690display_set_global_handler_remove(struct display *display,
5691 display_global_handler_t remove_handler)
5692{
5693 display->global_handler_remove = remove_handler;
5694 if (!remove_handler)
5695 return;
5696}
5697
5698void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005699display_set_output_configure_handler(struct display *display,
5700 display_output_handler_t handler)
5701{
5702 struct output *output;
5703
5704 display->output_configure_handler = handler;
5705 if (!handler)
5706 return;
5707
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005708 wl_list_for_each(output, &display->output_list, link) {
5709 if (output->allocation.width == 0 &&
5710 output->allocation.height == 0)
5711 continue;
5712
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005713 (*display->output_configure_handler)(output,
5714 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005715 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005716}
5717
5718void
5719output_set_user_data(struct output *output, void *data)
5720{
5721 output->user_data = data;
5722}
5723
5724void *
5725output_get_user_data(struct output *output)
5726{
5727 return output->user_data;
5728}
5729
5730void
5731output_set_destroy_handler(struct output *output,
5732 display_output_handler_t handler)
5733{
5734 output->destroy_handler = handler;
5735 /* FIXME: implement this, once we have way to remove outputs */
5736}
5737
5738void
Scott Moreau4e072362012-09-29 02:03:11 -06005739output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005740{
Scott Moreau4e072362012-09-29 02:03:11 -06005741 struct rectangle allocation = output->allocation;
5742
5743 switch (output->transform) {
5744 case WL_OUTPUT_TRANSFORM_90:
5745 case WL_OUTPUT_TRANSFORM_270:
5746 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5747 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5748 /* Swap width and height */
5749 allocation.width = output->allocation.height;
5750 allocation.height = output->allocation.width;
5751 break;
5752 }
5753
5754 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005755}
5756
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005757struct wl_output *
5758output_get_wl_output(struct output *output)
5759{
5760 return output->output;
5761}
5762
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005763enum wl_output_transform
5764output_get_transform(struct output *output)
5765{
5766 return output->transform;
5767}
5768
Alexander Larssonafd319a2013-05-22 14:41:27 +02005769uint32_t
5770output_get_scale(struct output *output)
5771{
5772 return output->scale;
5773}
5774
Jason Ekstrand738715d2014-04-02 19:53:50 -05005775const char *
5776output_get_make(struct output *output)
5777{
5778 return output->make;
5779}
5780
5781const char *
5782output_get_model(struct output *output)
5783{
5784 return output->model;
5785}
5786
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005787static void
Daniel Stone97f68542012-05-30 16:32:01 +01005788fini_xkb(struct input *input)
5789{
5790 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005791 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005792}
5793
5794static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005795display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005796{
5797 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005798 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005799
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005800 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005801 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005802 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005803 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005804 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005805 input->pointer_focus = NULL;
5806 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005807 input->seat_version = seat_version;
5808
Rusty Lynch041815a2013-08-08 21:20:38 -07005809 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005810 wl_list_insert(d->input_list.prev, &input->link);
5811
Daniel Stone37816df2012-05-16 18:45:18 +01005812 wl_seat_add_listener(input->seat, &seat_listener, input);
5813 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005814
Jason Ekstranda669bd52014-04-02 19:53:51 -05005815 if (d->data_device_manager) {
5816 input->data_device =
5817 wl_data_device_manager_get_data_device(d->data_device_manager,
5818 input->seat);
5819 wl_data_device_add_listener(input->data_device,
5820 &data_device_listener,
5821 input);
5822 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005823
5824 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005825
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005826 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5827 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005828
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005829 set_repeat_info(input, 40, 400);
5830 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5831 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005832}
5833
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005834static void
Harish Krupoee4c7a22019-04-19 01:53:27 +05305835display_add_data_device(struct display *d, uint32_t id, int ddm_version)
5836{
5837 struct input *input;
5838
5839 d->data_device_manager_version = MIN(ddm_version, 3);
5840 d->data_device_manager =
5841 wl_registry_bind(d->registry, id,
5842 &wl_data_device_manager_interface,
5843 d->data_device_manager_version);
5844
5845 wl_list_for_each(input, &d->input_list, link) {
5846 if (!input->data_device) {
5847 input->data_device =
5848 wl_data_device_manager_get_data_device(d->data_device_manager,
5849 input->seat);
5850 wl_data_device_add_listener(input->data_device,
5851 &data_device_listener,
5852 input);
5853 }
5854 }
5855}
5856
5857static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005858input_destroy(struct input *input)
5859{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005860 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005861 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005862
5863 if (input->drag_offer)
5864 data_offer_destroy(input->drag_offer);
5865
5866 if (input->selection_offer)
5867 data_offer_destroy(input->selection_offer);
5868
kabeer khan6ce67ec2014-10-20 11:55:29 +05305869 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005870 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305871 wl_data_device_release(input->data_device);
5872 else
5873 wl_data_device_destroy(input->data_device);
5874 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005875 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005876 if (input->touch)
5877 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005878 if (input->pointer)
5879 wl_pointer_release(input->pointer);
5880 if (input->keyboard)
5881 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005882 } else {
5883 if (input->touch)
5884 wl_touch_destroy(input->touch);
5885 if (input->pointer)
5886 wl_pointer_destroy(input->pointer);
5887 if (input->keyboard)
5888 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005889 }
5890
Daniel Stone97f68542012-05-30 16:32:01 +01005891 fini_xkb(input);
5892
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005893 wl_surface_destroy(input->pointer_surface);
5894
Pekka Paalanene1207c72011-12-16 12:02:09 +02005895 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005896 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005897 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005898 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005899 free(input);
5900}
5901
5902static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005903shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5904{
5905 struct display *d = data;
5906
5907 if (format == WL_SHM_FORMAT_RGB565)
5908 d->has_rgb565 = 1;
5909}
5910
5911struct wl_shm_listener shm_listener = {
5912 shm_format
5913};
5914
5915static void
ant8mef99fac22018-11-28 22:46:37 +01005916xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005917{
ant8mef99fac22018-11-28 22:46:37 +01005918 xdg_wm_base_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005919}
5920
ant8mef99fac22018-11-28 22:46:37 +01005921static const struct xdg_wm_base_listener wm_base_listener = {
5922 xdg_wm_base_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005923};
5924
5925static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005926registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5927 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005928{
5929 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005930 struct global *global;
5931
Brian Lovinbc919262013-08-07 15:34:59 -07005932 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005933 global->name = id;
5934 global->interface = strdup(interface);
5935 global->version = version;
5936 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005937
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005938 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005939 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005940 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005941 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005942 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005943 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005944 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005945 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5946 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5947 d->relative_pointer_manager =
5948 wl_registry_bind(registry, id,
5949 &zwp_relative_pointer_manager_v1_interface,
5950 1);
5951 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5952 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5953 d->pointer_constraints =
5954 wl_registry_bind(registry, id,
5955 &zwp_pointer_constraints_v1_interface,
5956 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005957 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005958 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005959 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005960 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Harish Krupoee4c7a22019-04-19 01:53:27 +05305961 display_add_data_device(d, id, version);
ant8mef99fac22018-11-28 22:46:37 +01005962 } else if (strcmp(interface, "xdg_wm_base") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005963 d->xdg_shell = wl_registry_bind(registry, id,
ant8mef99fac22018-11-28 22:46:37 +01005964 &xdg_wm_base_interface, 1);
5965 xdg_wm_base_add_listener(d->xdg_shell, &wm_base_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005966 } else if (strcmp(interface, "text_cursor_position") == 0) {
5967 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005968 wl_registry_bind(registry, id,
5969 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005970 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5971 d->subcompositor =
5972 wl_registry_bind(registry, id,
5973 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005974 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005975
5976 if (d->global_handler)
5977 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005978}
5979
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005980static void
5981registry_handle_global_remove(void *data, struct wl_registry *registry,
5982 uint32_t name)
5983{
5984 struct display *d = data;
5985 struct global *global;
5986 struct global *tmp;
5987
5988 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5989 if (global->name != name)
5990 continue;
5991
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005992 if (strcmp(global->interface, "wl_output") == 0)
5993 display_destroy_output(d, name);
5994
5995 /* XXX: Should destroy remaining bound globals */
5996
5997 if (d->global_handler_remove)
5998 d->global_handler_remove(d, name, global->interface,
5999 global->version, d->user_data);
6000
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006001 wl_list_remove(&global->link);
6002 free(global->interface);
6003 free(global);
6004 }
6005}
6006
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006007void *
6008display_bind(struct display *display, uint32_t name,
6009 const struct wl_interface *interface, uint32_t version)
6010{
6011 return wl_registry_bind(display->registry, name, interface, version);
6012}
6013
6014static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006015 registry_handle_global,
6016 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006017};
6018
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006019#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006020static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006021init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006022{
6023 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006024 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006025
Rob Clark6396ed32012-03-11 19:48:41 -05006026#ifdef USE_CAIRO_GLESV2
6027# define GL_BIT EGL_OPENGL_ES2_BIT
6028#else
6029# define GL_BIT EGL_OPENGL_BIT
6030#endif
6031
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006032 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006033 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006034 EGL_RED_SIZE, 1,
6035 EGL_GREEN_SIZE, 1,
6036 EGL_BLUE_SIZE, 1,
6037 EGL_ALPHA_SIZE, 1,
6038 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006039 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006040 EGL_NONE
6041 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006042
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006043#ifdef USE_CAIRO_GLESV2
6044 static const EGLint context_attribs[] = {
6045 EGL_CONTEXT_CLIENT_VERSION, 2,
6046 EGL_NONE
6047 };
6048 EGLint api = EGL_OPENGL_ES_API;
6049#else
6050 EGLint *context_attribs = NULL;
6051 EGLint api = EGL_OPENGL_API;
6052#endif
6053
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006054 d->dpy =
6055 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6056 d->display, NULL);
6057
Yuval Fledel45568f62010-12-06 09:18:12 -05006058 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006059 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006060 return -1;
6061 }
6062
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006063 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006064 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006065 return -1;
6066 }
6067
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006068 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6069 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006070 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006071 return -1;
6072 }
6073
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006074 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006075 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006076 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006077 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006078 return -1;
6079 }
6080
Benjamin Franzke0c991632011-09-27 21:57:31 +02006081 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6082 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006083 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006084 return -1;
6085 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006086
6087 return 0;
6088}
6089
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006090static void
6091fini_egl(struct display *display)
6092{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006093 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006094
6095 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6096 EGL_NO_CONTEXT);
6097
6098 eglTerminate(display->dpy);
6099 eglReleaseThread();
6100}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006101#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006102
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006103static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006104init_dummy_surface(struct display *display)
6105{
6106 int len;
6107 void *data;
6108
6109 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006110 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006111 display->dummy_surface =
6112 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6113 1, 1, len);
6114 display->dummy_surface_data = data;
6115}
6116
6117static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006118handle_display_data(struct task *task, uint32_t events)
6119{
6120 struct display *display =
6121 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006122 struct epoll_event ep;
6123 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006124
6125 display->display_fd_events = events;
6126
6127 if (events & EPOLLERR || events & EPOLLHUP) {
6128 display_exit(display);
6129 return;
6130 }
6131
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006132 if (events & EPOLLIN) {
6133 ret = wl_display_dispatch(display->display);
6134 if (ret == -1) {
6135 display_exit(display);
6136 return;
6137 }
6138 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006139
6140 if (events & EPOLLOUT) {
6141 ret = wl_display_flush(display->display);
6142 if (ret == 0) {
6143 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6144 ep.data.ptr = &display->display_task;
6145 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6146 display->display_fd, &ep);
6147 } else if (ret == -1 && errno != EAGAIN) {
6148 display_exit(display);
6149 return;
6150 }
6151 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006152}
6153
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006154static void
6155log_handler(const char *format, va_list args)
6156{
6157 vfprintf(stderr, format, args);
6158}
6159
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006160struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006161display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006162{
6163 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006164
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006165 wl_log_set_handler_client(log_handler);
6166
Peter Huttererf3d62272013-08-08 11:57:05 +10006167 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006168 if (d == NULL)
6169 return NULL;
6170
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006171 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006172 if (d->display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02006173 fprintf(stderr, "failed to connect to Wayland display: %s\n",
6174 strerror(errno));
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006175 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006176 return NULL;
6177 }
6178
Rob Bradford5ab9c752013-07-26 16:29:43 +01006179 d->xkb_context = xkb_context_new(0);
6180 if (d->xkb_context == NULL) {
6181 fprintf(stderr, "Failed to create XKB context\n");
6182 free(d);
6183 return NULL;
6184 }
6185
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006186 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006187 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006188 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006189 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6190 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006191
6192 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006193 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006194 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006195 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006196
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006197 d->registry = wl_display_get_registry(d->display);
6198 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006199
Marek Chalupaa519d062014-12-05 13:49:40 +01006200 if (wl_display_roundtrip(d->display) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006201 fprintf(stderr, "Failed to process Wayland connection: %s\n",
6202 strerror(errno));
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006203 return NULL;
6204 }
6205
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006206#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006207 if (init_egl(d) < 0)
6208 fprintf(stderr, "EGL does not seem to work, "
6209 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006210#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006211
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006212 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006213
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006214 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006215
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006216 wl_list_init(&d->window_list);
6217
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006218 init_dummy_surface(d);
6219
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006220 return d;
6221}
6222
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006223static void
6224display_destroy_outputs(struct display *display)
6225{
6226 struct output *tmp;
6227 struct output *output;
6228
6229 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6230 output_destroy(output);
6231}
6232
Pekka Paalanene1207c72011-12-16 12:02:09 +02006233static void
6234display_destroy_inputs(struct display *display)
6235{
6236 struct input *tmp;
6237 struct input *input;
6238
6239 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6240 input_destroy(input);
6241}
6242
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006243void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006244display_destroy(struct display *display)
6245{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006246 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006247 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6248 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006249
6250 if (!wl_list_empty(&display->deferred_list))
6251 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6252
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006253 cairo_surface_destroy(display->dummy_surface);
6254 free(display->dummy_surface_data);
6255
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006256 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006257 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006258
Daniel Stone97f68542012-05-30 16:32:01 +01006259 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006260
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006261 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006262 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006263
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006264#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006265 if (display->argb_device)
6266 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006267#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006268
Pekka Paalanen35e82632013-04-25 13:57:48 +03006269 if (display->subcompositor)
6270 wl_subcompositor_destroy(display->subcompositor);
6271
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006272 if (display->xdg_shell)
ant8mef99fac22018-11-28 22:46:37 +01006273 xdg_wm_base_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006274
6275 if (display->shm)
6276 wl_shm_destroy(display->shm);
6277
6278 if (display->data_device_manager)
6279 wl_data_device_manager_destroy(display->data_device_manager);
6280
6281 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006282 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006283
6284 close(display->epoll_fd);
6285
U. Artie Eoff44874d92012-10-02 21:12:35 -07006286 if (!(display->display_fd_events & EPOLLERR) &&
6287 !(display->display_fd_events & EPOLLHUP))
6288 wl_display_flush(display->display);
6289
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006290 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006291 free(display);
6292}
6293
6294void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006295display_set_user_data(struct display *display, void *data)
6296{
6297 display->user_data = data;
6298}
6299
6300void *
6301display_get_user_data(struct display *display)
6302{
6303 return display->user_data;
6304}
6305
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006306struct wl_display *
6307display_get_display(struct display *display)
6308{
6309 return display->display;
6310}
6311
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006312int
6313display_has_subcompositor(struct display *display)
6314{
6315 if (display->subcompositor)
6316 return 1;
6317
6318 wl_display_roundtrip(display->display);
6319
6320 return display->subcompositor != NULL;
6321}
6322
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006323cairo_device_t *
6324display_get_cairo_device(struct display *display)
6325{
6326 return display->argb_device;
6327}
6328
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006329struct output *
6330display_get_output(struct display *display)
6331{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006332 if (wl_list_empty(&display->output_list))
6333 return NULL;
6334
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006335 return container_of(display->output_list.next, struct output, link);
6336}
6337
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006338struct wl_compositor *
6339display_get_compositor(struct display *display)
6340{
6341 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006342}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006343
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006344uint32_t
6345display_get_serial(struct display *display)
6346{
6347 return display->serial;
6348}
6349
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006350EGLDisplay
6351display_get_egl_display(struct display *d)
6352{
6353 return d->dpy;
6354}
6355
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006356struct wl_data_source *
6357display_create_data_source(struct display *display)
6358{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006359 if (display->data_device_manager)
6360 return wl_data_device_manager_create_data_source(display->data_device_manager);
6361 else
6362 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006363}
6364
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006365EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006366display_get_argb_egl_config(struct display *d)
6367{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006368 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006369}
6370
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006371int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006372display_acquire_window_surface(struct display *display,
6373 struct window *window,
6374 EGLContext ctx)
6375{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006376 struct surface *surface = window->main_surface;
6377
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006378 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006379 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006380
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006381 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006382 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006383}
6384
6385void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006386display_release_window_surface(struct display *display,
6387 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006388{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006389 struct surface *surface = window->main_surface;
6390
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006391 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006392 return;
6393
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006394 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006395}
6396
6397void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006398display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006399{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006400 wl_list_insert(&display->deferred_list, &task->link);
6401}
6402
6403void
6404display_watch_fd(struct display *display,
6405 int fd, uint32_t events, struct task *task)
6406{
6407 struct epoll_event ep;
6408
6409 ep.events = events;
6410 ep.data.ptr = task;
6411 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6412}
6413
6414void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006415display_unwatch_fd(struct display *display, int fd)
6416{
6417 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6418}
6419
6420void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006421display_run(struct display *display)
6422{
6423 struct task *task;
6424 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006425 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006426
Pekka Paalanen826d7952011-12-15 10:14:07 +02006427 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006428 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006429 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006430 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006431 struct task, link);
6432 wl_list_remove(&task->link);
6433 task->run(task, 0);
6434 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006435
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006436 wl_display_dispatch_pending(display->display);
6437
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006438 if (!display->running)
6439 break;
6440
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006441 ret = wl_display_flush(display->display);
6442 if (ret < 0 && errno == EAGAIN) {
6443 ep[0].events =
6444 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6445 ep[0].data.ptr = &display->display_task;
6446
6447 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6448 display->display_fd, &ep[0]);
6449 } else if (ret < 0) {
6450 break;
6451 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006452
6453 count = epoll_wait(display->epoll_fd,
6454 ep, ARRAY_LENGTH(ep), -1);
6455 for (i = 0; i < count; i++) {
6456 task = ep[i].data.ptr;
6457 task->run(task, ep[i].events);
6458 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006459 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006460}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006461
6462void
6463display_exit(struct display *display)
6464{
6465 display->running = 0;
6466}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006467
Carlos Garnacho9c931792016-01-18 23:52:12 +01006468int
6469display_get_data_device_manager_version(struct display *display)
6470{
6471 return display->data_device_manager_version;
6472}
6473
Jan Arne Petersencd997062012-11-18 19:06:44 +01006474void
6475keysym_modifiers_add(struct wl_array *modifiers_map,
6476 const char *name)
6477{
6478 size_t len = strlen(name) + 1;
6479 char *p;
6480
6481 p = wl_array_add(modifiers_map, len);
6482
6483 if (p == NULL)
6484 return;
6485
6486 strncpy(p, name, len);
6487}
6488
6489static xkb_mod_index_t
6490keysym_modifiers_get_index(struct wl_array *modifiers_map,
6491 const char *name)
6492{
6493 xkb_mod_index_t index = 0;
6494 char *p = modifiers_map->data;
6495
6496 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6497 if (strcmp(p, name) == 0)
6498 return index;
6499
6500 index++;
6501 p += strlen(p) + 1;
6502 }
6503
6504 return XKB_MOD_INVALID;
6505}
6506
6507xkb_mod_mask_t
6508keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6509 const char *name)
6510{
6511 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6512
6513 if (index == XKB_MOD_INVALID)
6514 return XKB_MOD_INVALID;
6515
6516 return 1 << index;
6517}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006518
6519static void
6520toytimer_fire(struct task *tsk, uint32_t events)
6521{
6522 uint64_t e;
6523 struct toytimer *tt;
6524
6525 tt = container_of(tsk, struct toytimer, tsk);
6526
6527 if (events != EPOLLIN)
6528 fprintf(stderr, "unexpected timerfd events %x\n", events);
6529
6530 if (!(events & EPOLLIN))
6531 return;
6532
6533 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6534 /* If we change the timer between the fd becoming
6535 * readable and getting here, there'll be nothing to
6536 * read and we get EAGAIN. */
6537 if (errno != EAGAIN)
Antonio Borneo39578632019-04-26 23:57:31 +02006538 fprintf(stderr, "timer read failed: %s\n",
6539 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006540 return;
6541 }
6542
6543 tt->callback(tt);
6544}
6545
6546void
6547toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6548 toytimer_cb callback)
6549{
6550 memset(tt, 0, sizeof *tt);
6551
6552 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6553 if (tt->fd == -1) {
Antonio Borneo39578632019-04-26 23:57:31 +02006554 fprintf(stderr, "creating timer failed: %s\n",
6555 strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006556 abort();
6557 }
6558
6559 tt->display = display;
6560 tt->callback = callback;
6561 tt->tsk.run = toytimer_fire;
6562 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6563}
6564
6565void
6566toytimer_fini(struct toytimer *tt)
6567{
6568 display_unwatch_fd(tt->display, tt->fd);
6569 close(tt->fd);
6570 tt->fd = -1;
6571}
6572
6573void
6574toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6575{
6576 int ret;
6577
6578 ret = timerfd_settime(tt->fd, 0, its, NULL);
6579 if (ret < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02006580 fprintf(stderr, "timer setup failed: %s\n", strerror(errno));
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006581 abort();
6582 }
6583}
6584
6585#define USEC_PER_SEC 1000000
6586
6587void
6588toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6589{
6590 struct itimerspec its;
6591
6592 its.it_interval.tv_sec = 0;
6593 its.it_interval.tv_nsec = 0;
6594 its.it_value.tv_sec = usec / USEC_PER_SEC;
6595 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6596 toytimer_arm(tt, &its);
6597}
6598
6599void
6600toytimer_disarm(struct toytimer *tt)
6601{
6602 struct itimerspec its = {};
6603
6604 toytimer_arm(tt, &its);
6605}