blob: b64e96cf5db0eb675b7055e3cee1a6f4dd32941f [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"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080076#include "shared/zalloc.h"
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +080077#include "xdg-shell-unstable-v6-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"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050082
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050083#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050084
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090085#include <sys/types.h>
86#include "ivi-application-client-protocol.h"
87#define IVI_SURFACE_ID 9000
88
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080089#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
90#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
91
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070092struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030093
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040094struct global {
95 uint32_t name;
96 char *interface;
97 uint32_t version;
98 struct wl_list link;
99};
100
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500101struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500102 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400103 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500104 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300105 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400106 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400107 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600108 struct text_cursor_position *text_cursor_position;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800109 struct zxdg_shell_v6 *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900110 struct ivi_application *ivi_application; /* ivi style shell */
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800111 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
112 struct zwp_pointer_constraints_v1 *pointer_constraints;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400113 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500114 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200115 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200116 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400117 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400118
119 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700120 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400121 struct task display_task;
122
123 int epoll_fd;
124 struct wl_list deferred_list;
125
Pekka Paalanen826d7952011-12-15 10:14:07 +0200126 int running;
127
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400128 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400129 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400130 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500131 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400132
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400133 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400134
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300135 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300136 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400137
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200138 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400139 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800140 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200141
142 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100143
144 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200145
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200146 /* A hack to get text extents for tooltips */
147 cairo_surface_t *dummy_surface;
148 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200149
150 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530151 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500152};
153
Rob Bradford7507b572012-05-15 17:55:34 +0100154struct window_output {
155 struct output *output;
156 struct wl_list link;
157};
158
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200159struct toysurface {
160 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500161 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800162 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200163 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200164 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200165 * If flags has SURFACE_HINT_RESIZE set, the user is
166 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200167 * Returns the Cairo surface to draw to.
168 */
169 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200170 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200171 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200172
173 /*
174 * Post the surface to the server, returning the server allocation
175 * rectangle. The Cairo surface from prepare() must be destroyed
176 * after calling this.
177 */
178 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200179 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200180 struct rectangle *server_allocation);
181
182 /*
183 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800184 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200185 */
186 int (*acquire)(struct toysurface *base, EGLContext ctx);
187
188 /*
189 * Release the toysurface from the EGL context, returning control
190 * to Cairo.
191 */
192 void (*release)(struct toysurface *base);
193
194 /*
195 * Destroy the toysurface, including the Cairo surface, any
196 * backing storage, and the Wayland protocol objects.
197 */
198 void (*destroy)(struct toysurface *base);
199};
200
Pekka Paalanen4e373742013-02-13 16:17:13 +0200201struct surface {
202 struct window *window;
203
204 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300205 struct wl_subsurface *subsurface;
206 int synchronized;
207 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200208 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200209 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300210 int redraw_needed;
211 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300212 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200213
214 struct rectangle allocation;
215 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200216
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200217 struct wl_region *input_region;
218 struct wl_region *opaque_region;
219
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200220 enum window_buffer_type buffer_type;
221 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200222 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200223
224 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300225
226 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200227};
228
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500229struct window {
230 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100231 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500232 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200233 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400234 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500235 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200236 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500237 int x, y;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800238 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400239 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300240 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400241 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400242 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500243 int custom;
244 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400245
Pekka Paalanen99436862012-11-19 17:15:59 +0200246 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400247
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500248 int fullscreen;
249 int maximized;
250
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200251 enum preferred_format preferred_format;
252
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500253 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500254 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400255 window_data_handler_t data_handler;
256 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500257 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400258 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200259 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700260 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400261
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800262 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
263
Pekka Paalanen4e373742013-02-13 16:17:13 +0200264 struct surface *main_surface;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800265 struct zxdg_surface_v6 *xdg_surface;
266 struct zxdg_toplevel_v6 *xdg_toplevel;
267 struct zxdg_popup_v6 *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300268
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700269 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800270 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500271
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900272 struct ivi_surface *ivi_surface;
273
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500274 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500275
Pekka Paalanen35e82632013-04-25 13:57:48 +0300276 /* struct surface::link, contains also main_surface */
277 struct wl_list subsurface_list;
278
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800279 struct zwp_relative_pointer_v1 *relative_pointer;
280 struct zwp_locked_pointer_v1 *locked_pointer;
281 struct input *locked_input;
282 bool pointer_locked;
283 locked_pointer_locked_handler_t pointer_locked_handler;
284 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
285 confined_pointer_confined_handler_t pointer_confined_handler;
286 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
287
288 struct zwp_confined_pointer_v1 *confined_pointer;
Dima Ryazanove0dc5d42018-05-10 00:53:38 -0700289 struct input *confined_input;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800290 struct widget *confined_widget;
291 bool confined;
292
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500293 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400294 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500295};
296
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500297struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500298 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200299 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300300 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500301 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400302 struct wl_list link;
303 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500304 widget_resize_handler_t resize_handler;
305 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500306 widget_enter_handler_t enter_handler;
307 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500308 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500309 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700310 widget_touch_down_handler_t touch_down_handler;
311 widget_touch_up_handler_t touch_up_handler;
312 widget_touch_motion_handler_t touch_motion_handler;
313 widget_touch_frame_handler_t touch_frame_handler;
314 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200315 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000316 widget_pointer_frame_handler_t pointer_frame_handler;
317 widget_axis_source_handler_t axis_source_handler;
318 widget_axis_stop_handler_t axis_stop_handler;
319 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400320 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500321 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300322 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500323 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000324 /* If this is set to false then no cairo surface will be
325 * created before redrawing the surface. This is useful if the
326 * redraw handler is going to do completely custom rendering
327 * such as using EGL directly */
328 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400329};
330
Rusty Lynch041815a2013-08-08 21:20:38 -0700331struct touch_point {
332 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800333 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700334 struct widget *widget;
335 struct wl_list link;
336};
337
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400338struct input {
339 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100340 struct wl_seat *seat;
341 struct wl_pointer *pointer;
342 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700343 struct wl_touch *touch;
344 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400345 struct window *pointer_focus;
346 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700347 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300348 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300349 uint32_t cursor_anim_start;
350 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500351 uint32_t cursor_timer_start;
352 uint32_t cursor_anim_current;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200353 struct toytimer cursor_timer;
Derek Foreman118a4292015-04-22 17:23:35 -0500354 bool cursor_timer_running;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300355 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400356 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400357 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400358 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400359 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400360 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400361
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500362 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500363 struct widget *grab;
364 uint32_t grab_button;
365
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400366 struct wl_data_device *data_device;
367 struct data_offer *drag_offer;
368 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800369 uint32_t touch_grab;
370 int32_t touch_grab_id;
371 float drag_x, drag_y;
372 struct window *drag_focus;
373 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100374
375 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100376 struct xkb_keymap *keymap;
377 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +0100378#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700379 struct xkb_compose_table *compose_table;
380 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +0100381#endif
Daniel Stone97f68542012-05-30 16:32:01 +0100382 xkb_mod_mask_t control_mask;
383 xkb_mod_mask_t alt_mask;
384 xkb_mod_mask_t shift_mask;
385 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400386
Jonny Lamb06959082014-08-12 14:58:27 +0200387 int32_t repeat_rate_sec;
388 int32_t repeat_rate_nsec;
389 int32_t repeat_delay_sec;
390 int32_t repeat_delay_nsec;
391
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200392 struct toytimer repeat_timer;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400393 uint32_t repeat_sym;
394 uint32_t repeat_key;
395 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500396 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400397};
398
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500399struct output {
400 struct display *display;
401 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800402 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500403 struct rectangle allocation;
404 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600405 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200406 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500407 char *make;
408 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200409
410 display_output_handler_t destroy_handler;
411 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500412};
413
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500414struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500415 struct widget *widget;
416 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500417 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800418
419 uint32_t last_time;
420 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800421 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500422};
423
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500424struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400425 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500426 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500427 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500428 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700429 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500430 const char **entries;
431 uint32_t time;
432 int current;
433 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400434 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500435 menu_func_t func;
436};
437
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300438struct tooltip {
439 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300440 struct widget *widget;
441 char *entry;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200442 struct toytimer timer;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300443 float x, y;
444};
445
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700446struct shm_pool {
447 struct wl_shm_pool *pool;
448 size_t size;
449 size_t used;
450 void *data;
451};
452
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400453enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300454 CURSOR_DEFAULT = 100,
455 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400456};
457
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200458static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400459
Pekka Paalanen97777442013-05-22 10:20:05 +0300460/* #define DEBUG */
461
462#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300463
464static void
465debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
466__attribute__ ((format (printf, 4, 5)));
467
468static void
469debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
470{
471 va_list ap;
472 struct timeval tv;
473
474 gettimeofday(&tv, NULL);
475 fprintf(stderr, "%8ld.%03ld ",
476 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
477
478 if (proxy)
479 fprintf(stderr, "%s@%d ",
480 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
481
482 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
483 fprintf(stderr, "%s ", func);
484
485 va_start(ap, fmt);
486 vfprintf(stderr, fmt, ap);
487 va_end(ap);
488}
489
490#define DBG(fmt, ...) \
491 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
492
493#define DBG_OBJ(obj, fmt, ...) \
494 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
495
496#else
497
498#define DBG(...) do {} while (0)
499#define DBG_OBJ(...) do {} while (0)
500
501#endif
502
Alexander Larsson1818e312013-05-22 14:41:31 +0200503static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200504surface_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 +0200505{
506 int32_t tmp;
507
508 switch (buffer_transform) {
509 case WL_OUTPUT_TRANSFORM_90:
510 case WL_OUTPUT_TRANSFORM_270:
511 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
512 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
513 tmp = *width;
514 *width = *height;
515 *height = tmp;
516 break;
517 default:
518 break;
519 }
520
521 *width *= buffer_scale;
522 *height *= buffer_scale;
523}
524
525static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200526buffer_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 +0200527{
528 int32_t tmp;
529
530 switch (buffer_transform) {
531 case WL_OUTPUT_TRANSFORM_90:
532 case WL_OUTPUT_TRANSFORM_270:
533 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
534 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
535 tmp = *width;
536 *width = *height;
537 *height = tmp;
538 break;
539 default:
540 break;
541 }
542
543 *width /= buffer_scale;
544 *height /= buffer_scale;
545}
546
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500547#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400548
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200549struct egl_window_surface {
550 struct toysurface base;
551 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100552 struct display *display;
553 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200554 struct wl_egl_window *egl_window;
555 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100556};
557
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200558static struct egl_window_surface *
559to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100560{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200561 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100562}
563
564static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200565egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200566 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200567 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100568{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200569 struct egl_window_surface *surface = to_egl_window_surface(base);
570
Alexander Larsson1818e312013-05-22 14:41:31 +0200571 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
572
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200573 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
574 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
575
576 return cairo_surface_reference(surface->cairo_surface);
577}
578
579static void
580egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200581 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200582 struct rectangle *server_allocation)
583{
584 struct egl_window_surface *surface = to_egl_window_surface(base);
585
586 cairo_gl_surface_swapbuffers(surface->cairo_surface);
587 wl_egl_window_get_attached_size(surface->egl_window,
588 &server_allocation->width,
589 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200590
591 buffer_to_surface_size (buffer_transform, buffer_scale,
592 &server_allocation->width,
593 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200594}
595
596static int
597egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
598{
599 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200600 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100601
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200602 device = cairo_surface_get_device(surface->cairo_surface);
603 if (!device)
604 return -1;
605
606 if (!ctx) {
607 if (device == surface->display->argb_device)
608 ctx = surface->display->argb_ctx;
609 else
610 assert(0);
611 }
612
613 cairo_device_flush(device);
614 cairo_device_acquire(device);
615 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
616 surface->egl_surface, ctx))
617 fprintf(stderr, "failed to make surface current\n");
618
619 return 0;
620}
621
622static void
623egl_window_surface_release(struct toysurface *base)
624{
625 struct egl_window_surface *surface = to_egl_window_surface(base);
626 cairo_device_t *device;
627
628 device = cairo_surface_get_device(surface->cairo_surface);
629 if (!device)
630 return;
631
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200632 if (!eglMakeCurrent(surface->display->dpy,
633 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200634 fprintf(stderr, "failed to make context current\n");
635
636 cairo_device_release(device);
637}
638
639static void
640egl_window_surface_destroy(struct toysurface *base)
641{
642 struct egl_window_surface *surface = to_egl_window_surface(base);
643 struct display *d = surface->display;
644
645 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000646 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200647 wl_egl_window_destroy(surface->egl_window);
648 surface->surface = NULL;
649
650 free(surface);
651}
652
653static struct toysurface *
654egl_window_surface_create(struct display *display,
655 struct wl_surface *wl_surface,
656 uint32_t flags,
657 struct rectangle *rectangle)
658{
659 struct egl_window_surface *surface;
660
Pekka Paalanenb3627362012-11-19 17:16:00 +0200661 if (display->dpy == EGL_NO_DISPLAY)
662 return NULL;
663
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800664 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200665 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100666 return NULL;
667
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200668 surface->base.prepare = egl_window_surface_prepare;
669 surface->base.swap = egl_window_surface_swap;
670 surface->base.acquire = egl_window_surface_acquire;
671 surface->base.release = egl_window_surface_release;
672 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100673
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200674 surface->display = display;
675 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400676
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200677 surface->egl_window = wl_egl_window_create(surface->surface,
678 rectangle->width,
679 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100680
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100681 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100682 weston_platform_create_egl_surface(display->dpy,
683 display->argb_config,
684 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100685
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200686 surface->cairo_surface =
687 cairo_gl_surface_create_for_egl(display->argb_device,
688 surface->egl_surface,
689 rectangle->width,
690 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100691
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200692 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100693}
694
Pekka Paalanenb3627362012-11-19 17:16:00 +0200695#else
696
697static struct toysurface *
698egl_window_surface_create(struct display *display,
699 struct wl_surface *wl_surface,
700 uint32_t flags,
701 struct rectangle *rectangle)
702{
703 return NULL;
704}
705
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706#endif
707
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200708struct shm_surface_data {
709 struct wl_buffer *buffer;
710 struct shm_pool *pool;
711};
712
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400713struct wl_buffer *
714display_get_buffer_for_surface(struct display *display,
715 cairo_surface_t *surface)
716{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200717 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400718
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200719 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400720
721 return data->buffer;
722}
723
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500724static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700725shm_pool_destroy(struct shm_pool *pool);
726
727static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400728shm_surface_data_destroy(void *p)
729{
730 struct shm_surface_data *data = p;
731
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200732 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700733 if (data->pool)
734 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300735
736 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400737}
738
Kristian Høgsberg16626282012-04-03 11:21:27 -0400739static struct wl_shm_pool *
740make_shm_pool(struct display *display, int size, void **data)
741{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400742 struct wl_shm_pool *pool;
743 int fd;
744
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300745 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400746 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300747 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
748 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400749 return NULL;
750 }
751
752 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400753 if (*data == MAP_FAILED) {
754 fprintf(stderr, "mmap failed: %m\n");
755 close(fd);
756 return NULL;
757 }
758
759 pool = wl_shm_create_pool(display->shm, fd, size);
760
761 close(fd);
762
763 return pool;
764}
765
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700766static struct shm_pool *
767shm_pool_create(struct display *display, size_t size)
768{
769 struct shm_pool *pool = malloc(sizeof *pool);
770
771 if (!pool)
772 return NULL;
773
774 pool->pool = make_shm_pool(display, size, &pool->data);
775 if (!pool->pool) {
776 free(pool);
777 return NULL;
778 }
779
780 pool->size = size;
781 pool->used = 0;
782
783 return pool;
784}
785
786static void *
787shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
788{
789 if (pool->used + size > pool->size)
790 return NULL;
791
792 *offset = pool->used;
793 pool->used += size;
794
795 return (char *) pool->data + *offset;
796}
797
798/* destroy the pool. this does not unmap the memory though */
799static void
800shm_pool_destroy(struct shm_pool *pool)
801{
802 munmap(pool->data, pool->size);
803 wl_shm_pool_destroy(pool->pool);
804 free(pool);
805}
806
807/* Start allocating from the beginning of the pool again */
808static void
809shm_pool_reset(struct shm_pool *pool)
810{
811 pool->used = 0;
812}
813
814static int
815data_length_for_shm_surface(struct rectangle *rect)
816{
817 int stride;
818
819 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
820 rect->width);
821 return stride * rect->height;
822}
823
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500824static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700825display_create_shm_surface_from_pool(struct display *display,
826 struct rectangle *rectangle,
827 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400828{
829 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400830 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400831 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200832 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700833 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400834 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400835
836 data = malloc(sizeof *data);
837 if (data == NULL)
838 return NULL;
839
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200840 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
841 cairo_format = CAIRO_FORMAT_RGB16_565;
842 else
843 cairo_format = CAIRO_FORMAT_ARGB32;
844
845 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700846 length = stride * rectangle->height;
847 data->pool = NULL;
848 map = shm_pool_allocate(pool, length, &offset);
849
850 if (!map) {
851 free(data);
852 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400853 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400854
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400855 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200856 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400857 rectangle->width,
858 rectangle->height,
859 stride);
860
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200861 cairo_surface_set_user_data(surface, &shm_surface_data_key,
862 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400863
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200864 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
865 format = WL_SHM_FORMAT_RGB565;
866 else {
867 if (flags & SURFACE_OPAQUE)
868 format = WL_SHM_FORMAT_XRGB8888;
869 else
870 format = WL_SHM_FORMAT_ARGB8888;
871 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400872
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200873 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
874 rectangle->width,
875 rectangle->height,
876 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400877
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700878 return surface;
879}
880
881static cairo_surface_t *
882display_create_shm_surface(struct display *display,
883 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200884 struct shm_pool *alternate_pool,
885 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700886{
887 struct shm_surface_data *data;
888 struct shm_pool *pool;
889 cairo_surface_t *surface;
890
Pekka Paalanen99436862012-11-19 17:15:59 +0200891 if (alternate_pool) {
892 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700893 surface = display_create_shm_surface_from_pool(display,
894 rectangle,
895 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200896 alternate_pool);
897 if (surface) {
898 data = cairo_surface_get_user_data(surface,
899 &shm_surface_data_key);
900 goto out;
901 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700902 }
903
904 pool = shm_pool_create(display,
905 data_length_for_shm_surface(rectangle));
906 if (!pool)
907 return NULL;
908
909 surface =
910 display_create_shm_surface_from_pool(display, rectangle,
911 flags, pool);
912
913 if (!surface) {
914 shm_pool_destroy(pool);
915 return NULL;
916 }
917
918 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200919 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700920 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400921
Pekka Paalanen99436862012-11-19 17:15:59 +0200922out:
923 if (data_ret)
924 *data_ret = data;
925
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400926 return surface;
927}
928
nobled7b87cb02011-02-01 18:51:47 +0000929static int
930check_size(struct rectangle *rect)
931{
932 if (rect->width && rect->height)
933 return 0;
934
935 fprintf(stderr, "tried to create surface of "
936 "width: %d, height: %d\n", rect->width, rect->height);
937 return -1;
938}
939
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940cairo_surface_t *
941display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100942 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400943 struct rectangle *rectangle,
944 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400945{
nobled7b87cb02011-02-01 18:51:47 +0000946 if (check_size(rectangle) < 0)
947 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200948
949 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200950 return display_create_shm_surface(display, rectangle, flags,
951 NULL, NULL);
952}
953
Pekka Paalanena4eda732012-11-19 17:16:02 +0200954struct shm_surface_leaf {
955 cairo_surface_t *cairo_surface;
956 /* 'data' is automatically destroyed, when 'cairo_surface' is */
957 struct shm_surface_data *data;
958
959 struct shm_pool *resize_pool;
960 int busy;
961};
962
963static void
964shm_surface_leaf_release(struct shm_surface_leaf *leaf)
965{
966 if (leaf->cairo_surface)
967 cairo_surface_destroy(leaf->cairo_surface);
968 /* leaf->data already destroyed via cairo private */
969
970 if (leaf->resize_pool)
971 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200972
973 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200974}
975
Pekka Paalanenaef02542013-04-25 13:57:47 +0300976#define MAX_LEAVES 3
977
Pekka Paalanen99436862012-11-19 17:15:59 +0200978struct shm_surface {
979 struct toysurface base;
980 struct display *display;
981 struct wl_surface *surface;
982 uint32_t flags;
983 int dx, dy;
984
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200986 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200987};
988
989static struct shm_surface *
990to_shm_surface(struct toysurface *base)
991{
992 return container_of(base, struct shm_surface, base);
993}
994
Pekka Paalanena4eda732012-11-19 17:16:02 +0200995static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300996shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
997{
998#ifdef DEBUG
999 struct shm_surface_leaf *leaf;
1000 char bufs[MAX_LEAVES + 1];
1001 int i;
1002
1003 for (i = 0; i < MAX_LEAVES; i++) {
1004 leaf = &surface->leaf[i];
1005
1006 if (leaf->busy)
1007 bufs[i] = 'b';
1008 else if (leaf->cairo_surface)
1009 bufs[i] = 'a';
1010 else
1011 bufs[i] = ' ';
1012 }
1013
1014 bufs[MAX_LEAVES] = '\0';
1015 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1016#endif
1017}
1018
1019static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001020shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1021{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001022 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001023 struct shm_surface_leaf *leaf;
1024 int i;
1025 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001026
1027 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001028
Pekka Paalanenaef02542013-04-25 13:57:47 +03001029 for (i = 0; i < MAX_LEAVES; i++) {
1030 leaf = &surface->leaf[i];
1031 if (leaf->data && leaf->data->buffer == buffer) {
1032 leaf->busy = 0;
1033 break;
1034 }
1035 }
1036 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001037
Pekka Paalanenaef02542013-04-25 13:57:47 +03001038 /* Leave one free leaf with storage, release others */
1039 free_found = 0;
1040 for (i = 0; i < MAX_LEAVES; i++) {
1041 leaf = &surface->leaf[i];
1042
1043 if (!leaf->cairo_surface || leaf->busy)
1044 continue;
1045
1046 if (!free_found)
1047 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001048 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001049 shm_surface_leaf_release(leaf);
1050 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001051
Pekka Paalanen97777442013-05-22 10:20:05 +03001052 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001053}
1054
1055static const struct wl_buffer_listener shm_surface_buffer_listener = {
1056 shm_surface_buffer_release
1057};
1058
Pekka Paalanen99436862012-11-19 17:15:59 +02001059static cairo_surface_t *
1060shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001061 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001062 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001063{
Pekka Paalanenec076692012-11-30 13:37:27 +02001064 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001065 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001066 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001067 struct shm_surface_leaf *leaf = NULL;
1068 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001069
1070 surface->dx = dx;
1071 surface->dy = dy;
1072
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001073 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001074 for (i = 0; i < MAX_LEAVES; i++) {
1075 if (surface->leaf[i].busy)
1076 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001077
Pekka Paalanenaef02542013-04-25 13:57:47 +03001078 if (!leaf || surface->leaf[i].cairo_surface)
1079 leaf = &surface->leaf[i];
1080 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001081 DBG_OBJ(surface->surface, "pick leaf %d\n",
1082 (int)(leaf - &surface->leaf[0]));
1083
Pekka Paalanenaef02542013-04-25 13:57:47 +03001084 if (!leaf) {
1085 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001086 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001087 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001089 }
1090
Pekka Paalanena4eda732012-11-19 17:16:02 +02001091 if (!resize_hint && leaf->resize_pool) {
1092 cairo_surface_destroy(leaf->cairo_surface);
1093 leaf->cairo_surface = NULL;
1094 shm_pool_destroy(leaf->resize_pool);
1095 leaf->resize_pool = NULL;
1096 }
1097
Alexander Larsson1818e312013-05-22 14:41:31 +02001098 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1099
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 if (leaf->cairo_surface &&
1101 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1102 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001103 goto out;
1104
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 if (leaf->cairo_surface)
1106 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001107
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001108#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001109 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001110 /* Create a big pool to allocate from, while continuously
1111 * resizing. Mmapping a new pool in the server
1112 * is relatively expensive, so reusing a pool performs
1113 * better, but may temporarily reserve unneeded memory.
1114 */
1115 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 leaf->resize_pool = shm_pool_create(surface->display,
1117 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001118 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001119#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001120
Alexander Larsson1818e312013-05-22 14:41:31 +02001121 rect.width = width;
1122 rect.height = height;
1123
Pekka Paalanena4eda732012-11-19 17:16:02 +02001124 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001125 display_create_shm_surface(surface->display, &rect,
1126 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127 leaf->resize_pool,
1128 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001129 if (!leaf->cairo_surface)
1130 return NULL;
1131
Pekka Paalanena4eda732012-11-19 17:16:02 +02001132 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001133 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001134
1135out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001136 surface->current = leaf;
1137
1138 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001139}
1140
1141static void
1142shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001143 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001144 struct rectangle *server_allocation)
1145{
1146 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001147 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001148
1149 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001150 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001151 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001152 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001153
Alexander Larsson1818e312013-05-22 14:41:31 +02001154 buffer_to_surface_size (buffer_transform, buffer_scale,
1155 &server_allocation->width,
1156 &server_allocation->height);
1157
Pekka Paalanena4eda732012-11-19 17:16:02 +02001158 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001159 surface->dx, surface->dy);
1160 wl_surface_damage(surface->surface, 0, 0,
1161 server_allocation->width, server_allocation->height);
1162 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001163
Pekka Paalanen71233882013-04-25 13:57:53 +03001164 DBG_OBJ(surface->surface, "leaf %d busy\n",
1165 (int)(leaf - &surface->leaf[0]));
1166
Pekka Paalanena4eda732012-11-19 17:16:02 +02001167 leaf->busy = 1;
1168 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001169}
1170
1171static int
1172shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1173{
1174 return -1;
1175}
1176
1177static void
1178shm_surface_release(struct toysurface *base)
1179{
1180}
1181
1182static void
1183shm_surface_destroy(struct toysurface *base)
1184{
1185 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001186 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001187
Pekka Paalanenaef02542013-04-25 13:57:47 +03001188 for (i = 0; i < MAX_LEAVES; i++)
1189 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001190
1191 free(surface);
1192}
1193
1194static struct toysurface *
1195shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1196 uint32_t flags, struct rectangle *rectangle)
1197{
1198 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001199 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001200
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001201 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001202 surface->base.prepare = shm_surface_prepare;
1203 surface->base.swap = shm_surface_swap;
1204 surface->base.acquire = shm_surface_acquire;
1205 surface->base.release = shm_surface_release;
1206 surface->base.destroy = shm_surface_destroy;
1207
1208 surface->display = display;
1209 surface->surface = wl_surface;
1210 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001211
1212 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001213}
1214
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215/*
1216 * The following correspondences between file names and cursors was copied
1217 * from: https://bugs.kde.org/attachment.cgi?id=67313
1218 */
1219
1220static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "sw-resize",
1223 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "se-resize",
1229 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "s-resize",
1235 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001240 "closedhand",
1241 "208530c400c041818281048008011002"
1242};
1243
1244static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001246 "default",
1247 "top_left_arrow",
1248 "left-arrow"
1249};
1250
1251static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001252 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001253 "w-resize",
1254 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001255};
1256
1257static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001258 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001259 "e-resize",
1260 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001261};
1262
1263static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001264 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001265 "nw-resize",
1266 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001267};
1268
1269static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001270 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001271 "ne-resize",
1272 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001273};
1274
1275static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001276 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001277 "n-resize",
1278 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001279};
1280
1281static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001282 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001283 "ibeam",
1284 "text"
1285};
1286
1287static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001288 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001289 "pointer",
1290 "pointing_hand",
1291 "e29285e634086352946a0e7090d73106"
1292};
1293
1294static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001295 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001296 "wait",
1297 "0426c94ea35c87780ff01dc239897213"
1298};
1299
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001300static const char *move_draggings[] = {
1301 "dnd-move"
1302};
1303
1304static const char *copy_draggings[] = {
1305 "dnd-copy"
1306};
1307
1308static const char *forbidden_draggings[] = {
1309 "dnd-none",
1310 "dnd-no-drop"
1311};
1312
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001313struct cursor_alternatives {
1314 const char **names;
1315 size_t count;
1316};
1317
1318static const struct cursor_alternatives cursors[] = {
1319 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1320 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1321 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1322 {grabbings, ARRAY_LENGTH(grabbings)},
1323 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1324 {left_sides, ARRAY_LENGTH(left_sides)},
1325 {right_sides, ARRAY_LENGTH(right_sides)},
1326 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1327 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1328 {top_sides, ARRAY_LENGTH(top_sides)},
1329 {xterms, ARRAY_LENGTH(xterms)},
1330 {hand1s, ARRAY_LENGTH(hand1s)},
1331 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001332 {move_draggings, ARRAY_LENGTH(move_draggings)},
1333 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1334 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001335};
1336
1337static void
1338create_cursors(struct display *display)
1339{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001340 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001341 struct weston_config *config;
1342 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001343 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001344 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001345 unsigned int i, j;
1346 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001347
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001348 config_file = weston_config_get_name_from_env();
1349 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001350 s = weston_config_get_section(config, "shell", NULL, NULL);
1351 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1352 weston_config_section_get_int(s, "cursor-size", &size, 32);
1353 weston_config_destroy(config);
1354
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001355 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001356 if (!display->cursor_theme) {
1357 fprintf(stderr, "could not load theme '%s'\n", theme);
1358 return;
1359 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001360 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001361 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001362 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001363
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001364 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001365 cursor = NULL;
1366 for (j = 0; !cursor && j < cursors[i].count; ++j)
1367 cursor = wl_cursor_theme_get_cursor(
1368 display->cursor_theme, cursors[i].names[j]);
1369
1370 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001371 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001372 cursors[i].names[0]);
1373
1374 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001375 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001376}
1377
1378static void
1379destroy_cursors(struct display *display)
1380{
1381 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001382 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001383}
1384
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001385struct wl_cursor_image *
1386display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001387{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001388 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001389
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001390 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001391}
1392
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001393static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001394surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001395{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001396 if (!surface->cairo_surface)
1397 return;
1398
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001399 if (surface->opaque_region) {
1400 wl_surface_set_opaque_region(surface->surface,
1401 surface->opaque_region);
1402 wl_region_destroy(surface->opaque_region);
1403 surface->opaque_region = NULL;
1404 }
1405
1406 if (surface->input_region) {
1407 wl_surface_set_input_region(surface->surface,
1408 surface->input_region);
1409 wl_region_destroy(surface->input_region);
1410 surface->input_region = NULL;
1411 }
1412
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001413 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001414 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001415 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001416
Pekka Paalanen89dee002013-02-13 16:17:20 +02001417 cairo_surface_destroy(surface->cairo_surface);
1418 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001419}
1420
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001421int
1422window_has_focus(struct window *window)
1423{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001424 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001425}
1426
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001427static void
1428window_close(struct window *window)
1429{
1430 if (window->close_handler)
1431 window->close_handler(window->user_data);
1432 else
1433 display_exit(window->display);
1434}
1435
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001436struct display *
1437window_get_display(struct window *window)
1438{
1439 return window->display;
1440}
1441
Pekka Paalanen89dee002013-02-13 16:17:20 +02001442static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001443handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1444 int32_t width, int32_t height)
1445{
1446 struct window *window = data;
1447
1448 window_schedule_resize(window, width, height);
1449}
1450
1451static const struct ivi_surface_listener ivi_surface_listener = {
1452 handle_ivi_surface_configure,
1453};
1454
1455static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001456surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001457{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001458 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001459 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001460
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001461 if (!surface->toysurface && display->dpy &&
1462 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001463 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001464 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001465 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001466 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001467 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001468 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001469
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001470 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001471 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001472 surface->surface,
1473 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001474
Pekka Paalanen89dee002013-02-13 16:17:20 +02001475 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001476 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001477 allocation.width, allocation.height, flags,
1478 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001479}
1480
1481static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001482window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001483{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001484 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001485 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001486
Pekka Paalanenec076692012-11-30 13:37:27 +02001487 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001488 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001489
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001490 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1491 flags |= SURFACE_HINT_RGB565;
1492
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001493 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001494}
1495
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001496int
1497window_get_buffer_transform(struct window *window)
1498{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001499 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001500}
1501
1502void
1503window_set_buffer_transform(struct window *window,
1504 enum wl_output_transform transform)
1505{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001506 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001507 wl_surface_set_buffer_transform(window->main_surface->surface,
1508 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001509}
1510
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001511void
1512window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001513 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001514{
1515 window->main_surface->buffer_scale = scale;
1516 wl_surface_set_buffer_scale(window->main_surface->surface,
1517 scale);
1518}
1519
1520uint32_t
1521window_get_buffer_scale(struct window *window)
1522{
1523 return window->main_surface->buffer_scale;
1524}
1525
Alexander Larssond68f5232013-05-22 14:41:33 +02001526uint32_t
1527window_get_output_scale(struct window *window)
1528{
1529 struct window_output *window_output;
1530 struct window_output *window_output_tmp;
1531 int scale = 1;
1532
1533 wl_list_for_each_safe(window_output, window_output_tmp,
1534 &window->window_output_list, link) {
1535 if (window_output->output->scale > scale)
1536 scale = window_output->output->scale;
1537 }
1538
1539 return scale;
1540}
1541
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001542static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001543
Pekka Paalanen4e373742013-02-13 16:17:13 +02001544static void
1545surface_destroy(struct surface *surface)
1546{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001547 if (surface->frame_cb)
1548 wl_callback_destroy(surface->frame_cb);
1549
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001550 if (surface->input_region)
1551 wl_region_destroy(surface->input_region);
1552
1553 if (surface->opaque_region)
1554 wl_region_destroy(surface->opaque_region);
1555
Pekka Paalanen35e82632013-04-25 13:57:48 +03001556 if (surface->subsurface)
1557 wl_subsurface_destroy(surface->subsurface);
1558
Pekka Paalanen4e373742013-02-13 16:17:13 +02001559 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001560
1561 if (surface->toysurface)
1562 surface->toysurface->destroy(surface->toysurface);
1563
Pekka Paalanen35e82632013-04-25 13:57:48 +03001564 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001565 free(surface);
1566}
1567
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001568void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001569window_destroy(struct window *window)
1570{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001571 struct display *display = window->display;
1572 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001573 struct window_output *window_output;
1574 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001575
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001576 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001577
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001578 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001579 if (input->touch_focus == window)
1580 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001581 if (input->pointer_focus == window)
1582 input->pointer_focus = NULL;
1583 if (input->keyboard_focus == window)
1584 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001585 if (input->focus_widget &&
1586 input->focus_widget->window == window)
1587 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001588 }
1589
Rob Bradford7507b572012-05-15 17:55:34 +01001590 wl_list_for_each_safe(window_output, window_output_tmp,
1591 &window->window_output_list, link) {
1592 free (window_output);
1593 }
1594
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001595 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001596 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001597
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001598 if (window->xdg_toplevel)
1599 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001600 if (window->xdg_popup)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001601 zxdg_popup_v6_destroy(window->xdg_popup);
1602 if (window->xdg_surface)
1603 zxdg_surface_v6_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001604
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001605 if (window->ivi_surface)
1606 ivi_surface_destroy(window->ivi_surface);
1607
Pekka Paalanen4e373742013-02-13 16:17:13 +02001608 surface_destroy(window->main_surface);
1609
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001610 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001611
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001612 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001613 free(window);
1614}
1615
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001616static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001617widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001618{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001620
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001621 wl_list_for_each(child, &widget->child_list, link) {
1622 target = widget_find_widget(child, x, y);
1623 if (target)
1624 return target;
1625 }
1626
1627 if (widget->allocation.x <= x &&
1628 x < widget->allocation.x + widget->allocation.width &&
1629 widget->allocation.y <= y &&
1630 y < widget->allocation.y + widget->allocation.height) {
1631 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001632 }
1633
1634 return NULL;
1635}
1636
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001637static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001638window_find_widget(struct window *window, int32_t x, int32_t y)
1639{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001640 struct surface *surface;
1641 struct widget *widget;
1642
1643 wl_list_for_each(surface, &window->subsurface_list, link) {
1644 widget = widget_find_widget(surface->widget, x, y);
1645 if (widget)
1646 return widget;
1647 }
1648
1649 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001650}
1651
1652static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001653widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001654{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001655 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001656
Peter Huttererf3d62272013-08-08 11:57:05 +10001657 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001658 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001659 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001660 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001661 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001662 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001663 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001664 widget->tooltip = NULL;
1665 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001666 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001667 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001668
1669 return widget;
1670}
1671
1672struct widget *
1673window_add_widget(struct window *window, void *data)
1674{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001675 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001676
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001677 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001678 wl_list_init(&widget->link);
1679 window->main_surface->widget = widget;
1680
1681 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001682}
1683
1684struct widget *
1685widget_add_widget(struct widget *parent, void *data)
1686{
1687 struct widget *widget;
1688
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001689 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001690 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001691
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001692 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001693}
1694
1695void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001696widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001697{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001698 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001699 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001700 struct input *input;
1701
Pekka Paalanen35e82632013-04-25 13:57:48 +03001702 /* Destroy the sub-surface along with the root widget */
1703 if (surface->widget == widget && surface->subsurface)
1704 surface_destroy(widget->surface);
1705
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001706 if (widget->tooltip)
1707 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001708
Pekka Paalanene156fb62012-01-19 13:51:38 +02001709 wl_list_for_each(input, &display->input_list, link) {
1710 if (input->focus_widget == widget)
1711 input->focus_widget = NULL;
1712 }
1713
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001714 wl_list_remove(&widget->link);
1715 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001716}
1717
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001718void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001719widget_set_default_cursor(struct widget *widget, int cursor)
1720{
1721 widget->default_cursor = cursor;
1722}
1723
1724void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001725widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001726{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001727 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001728}
1729
1730void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001731widget_set_size(struct widget *widget, int32_t width, int32_t height)
1732{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001733 widget->allocation.width = width;
1734 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001735}
1736
1737void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001738widget_set_allocation(struct widget *widget,
1739 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001740{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001741 widget->allocation.x = x;
1742 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001743 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001744}
1745
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001746void
1747widget_set_transparent(struct widget *widget, int transparent)
1748{
1749 widget->opaque = !transparent;
1750}
1751
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001752void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001753widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001754{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001755 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001756}
1757
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001758static cairo_surface_t *
1759widget_get_cairo_surface(struct widget *widget)
1760{
1761 struct surface *surface = widget->surface;
1762 struct window *window = widget->window;
1763
Neil Roberts97b747c2013-12-19 16:17:12 +00001764 assert(widget->use_cairo);
1765
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001766 if (!surface->cairo_surface) {
1767 if (surface == window->main_surface)
1768 window_create_main_surface(window);
1769 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001770 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001771 }
1772
1773 return surface->cairo_surface;
1774}
1775
Alexander Larsson15901f02013-05-22 14:41:25 +02001776static void
1777widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1778{
1779 struct surface *surface = widget->surface;
1780 double angle;
1781 cairo_matrix_t m;
1782 enum wl_output_transform transform;
1783 int surface_width, surface_height;
1784 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001785 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001786
1787 surface_width = surface->allocation.width;
1788 surface_height = surface->allocation.height;
1789
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001790 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001791 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001792
Alexander Larsson15901f02013-05-22 14:41:25 +02001793 switch (transform) {
1794 case WL_OUTPUT_TRANSFORM_FLIPPED:
1795 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1796 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1797 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1798 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1799 break;
1800 default:
1801 cairo_matrix_init_identity(&m);
1802 break;
1803 }
1804
1805 switch (transform) {
1806 case WL_OUTPUT_TRANSFORM_NORMAL:
1807 default:
1808 angle = 0;
1809 translate_x = 0;
1810 translate_y = 0;
1811 break;
1812 case WL_OUTPUT_TRANSFORM_FLIPPED:
1813 angle = 0;
1814 translate_x = surface_width;
1815 translate_y = 0;
1816 break;
1817 case WL_OUTPUT_TRANSFORM_90:
1818 angle = M_PI_2;
1819 translate_x = surface_height;
1820 translate_y = 0;
1821 break;
1822 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1823 angle = M_PI_2;
1824 translate_x = surface_height;
1825 translate_y = surface_width;
1826 break;
1827 case WL_OUTPUT_TRANSFORM_180:
1828 angle = M_PI;
1829 translate_x = surface_width;
1830 translate_y = surface_height;
1831 break;
1832 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1833 angle = M_PI;
1834 translate_x = 0;
1835 translate_y = surface_height;
1836 break;
1837 case WL_OUTPUT_TRANSFORM_270:
1838 angle = M_PI + M_PI_2;
1839 translate_x = 0;
1840 translate_y = surface_width;
1841 break;
1842 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1843 angle = M_PI + M_PI_2;
1844 translate_x = 0;
1845 translate_y = 0;
1846 break;
1847 }
1848
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001849 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001850 cairo_translate(cr, translate_x, translate_y);
1851 cairo_rotate(cr, angle);
1852 cairo_transform(cr, &m);
1853}
1854
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001855cairo_t *
1856widget_cairo_create(struct widget *widget)
1857{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001858 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001859 cairo_surface_t *cairo_surface;
1860 cairo_t *cr;
1861
1862 cairo_surface = widget_get_cairo_surface(widget);
1863 cr = cairo_create(cairo_surface);
1864
Alexander Larsson15901f02013-05-22 14:41:25 +02001865 widget_cairo_update_transform(widget, cr);
1866
Pekka Paalanen35e82632013-04-25 13:57:48 +03001867 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1868
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001869 return cr;
1870}
1871
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001872struct wl_surface *
1873widget_get_wl_surface(struct widget *widget)
1874{
1875 return widget->surface->surface;
1876}
1877
Neil Roberts5e10a042013-09-09 00:40:17 +01001878struct wl_subsurface *
1879widget_get_wl_subsurface(struct widget *widget)
1880{
1881 return widget->surface->subsurface;
1882}
1883
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001884uint32_t
1885widget_get_last_time(struct widget *widget)
1886{
1887 return widget->surface->last_time;
1888}
1889
1890void
1891widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1892{
1893 struct wl_compositor *comp = widget->window->display->compositor;
1894 struct surface *surface = widget->surface;
1895
1896 if (!surface->input_region)
1897 surface->input_region = wl_compositor_create_region(comp);
1898
1899 if (rect) {
1900 wl_region_add(surface->input_region,
1901 rect->x, rect->y, rect->width, rect->height);
1902 }
1903}
1904
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001905void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001906widget_set_resize_handler(struct widget *widget,
1907 widget_resize_handler_t handler)
1908{
1909 widget->resize_handler = handler;
1910}
1911
1912void
1913widget_set_redraw_handler(struct widget *widget,
1914 widget_redraw_handler_t handler)
1915{
1916 widget->redraw_handler = handler;
1917}
1918
1919void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001920widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001921{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001922 widget->enter_handler = handler;
1923}
1924
1925void
1926widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1927{
1928 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001929}
1930
1931void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001932widget_set_motion_handler(struct widget *widget,
1933 widget_motion_handler_t handler)
1934{
1935 widget->motion_handler = handler;
1936}
1937
1938void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001939widget_set_button_handler(struct widget *widget,
1940 widget_button_handler_t handler)
1941{
1942 widget->button_handler = handler;
1943}
1944
1945void
Rusty Lynch041815a2013-08-08 21:20:38 -07001946widget_set_touch_up_handler(struct widget *widget,
1947 widget_touch_up_handler_t handler)
1948{
1949 widget->touch_up_handler = handler;
1950}
1951
1952void
1953widget_set_touch_down_handler(struct widget *widget,
1954 widget_touch_down_handler_t handler)
1955{
1956 widget->touch_down_handler = handler;
1957}
1958
1959void
1960widget_set_touch_motion_handler(struct widget *widget,
1961 widget_touch_motion_handler_t handler)
1962{
1963 widget->touch_motion_handler = handler;
1964}
1965
1966void
1967widget_set_touch_frame_handler(struct widget *widget,
1968 widget_touch_frame_handler_t handler)
1969{
1970 widget->touch_frame_handler = handler;
1971}
1972
1973void
1974widget_set_touch_cancel_handler(struct widget *widget,
1975 widget_touch_cancel_handler_t handler)
1976{
1977 widget->touch_cancel_handler = handler;
1978}
1979
1980void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001981widget_set_axis_handler(struct widget *widget,
1982 widget_axis_handler_t handler)
1983{
1984 widget->axis_handler = handler;
1985}
1986
Peter Hutterer87743e92016-01-18 16:38:22 +10001987void
1988widget_set_pointer_frame_handler(struct widget *widget,
1989 widget_pointer_frame_handler_t handler)
1990{
1991 widget->pointer_frame_handler = handler;
1992}
1993
1994void
1995widget_set_axis_handlers(struct widget *widget,
1996 widget_axis_handler_t axis_handler,
1997 widget_axis_source_handler_t axis_source_handler,
1998 widget_axis_stop_handler_t axis_stop_handler,
1999 widget_axis_discrete_handler_t axis_discrete_handler)
2000{
2001 widget->axis_handler = axis_handler;
2002 widget->axis_source_handler = axis_source_handler;
2003 widget->axis_stop_handler = axis_stop_handler;
2004 widget->axis_discrete_handler = axis_discrete_handler;
2005}
2006
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002007static void
2008window_schedule_redraw_task(struct window *window);
2009
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002010void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002011widget_schedule_redraw(struct widget *widget)
2012{
Pekka Paalanen71233882013-04-25 13:57:53 +03002013 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002014 widget->surface->redraw_needed = 1;
2015 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002016}
2017
Neil Roberts97b747c2013-12-19 16:17:12 +00002018void
2019widget_set_use_cairo(struct widget *widget,
2020 int use_cairo)
2021{
2022 widget->use_cairo = use_cairo;
2023}
2024
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002025cairo_surface_t *
2026window_get_surface(struct window *window)
2027{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002028 cairo_surface_t *cairo_surface;
2029
2030 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2031
2032 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002033}
2034
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002035struct wl_surface *
2036window_get_wl_surface(struct window *window)
2037{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002038 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002039}
2040
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002041static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002042tooltip_redraw_handler(struct widget *widget, void *data)
2043{
2044 cairo_t *cr;
2045 const int32_t r = 3;
2046 struct tooltip *tooltip = data;
2047 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002048
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002049 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002050 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002051 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2052 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2053 cairo_paint(cr);
2054
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002055 width = widget->allocation.width;
2056 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002057 rounded_rect(cr, 0, 0, width, height, r);
2058
2059 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2060 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2061 cairo_fill(cr);
2062
2063 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2064 cairo_move_to(cr, 10, 16);
2065 cairo_show_text(cr, tooltip->entry);
2066 cairo_destroy(cr);
2067}
2068
2069static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002070get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002071{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002072 cairo_t *cr;
2073 cairo_text_extents_t extents;
2074
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002075 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002076 * created yet, and parent does not have a valid surface
2077 * outside repaint, either.
2078 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002079 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002080 cairo_text_extents(cr, tooltip->entry, &extents);
2081 cairo_destroy(cr);
2082
2083 return extents;
2084}
2085
2086static int
2087window_create_tooltip(struct tooltip *tooltip)
2088{
2089 struct widget *parent = tooltip->parent;
2090 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002091 const int offset_y = 27;
2092 const int margin = 3;
2093 cairo_text_extents_t extents;
2094
2095 if (tooltip->widget)
2096 return 0;
2097
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002098 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002099
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002100 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002101 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002102 widget_set_allocation(tooltip->widget,
2103 tooltip->x, tooltip->y + offset_y,
2104 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002105
2106 return 0;
2107}
2108
2109void
2110widget_destroy_tooltip(struct widget *parent)
2111{
2112 struct tooltip *tooltip = parent->tooltip;
2113
2114 parent->tooltip_count = 0;
2115 if (!tooltip)
2116 return;
2117
2118 if (tooltip->widget) {
2119 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002120 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002121 }
2122
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002123 toytimer_fini(&tooltip->timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002124 free(tooltip->entry);
2125 free(tooltip);
2126 parent->tooltip = NULL;
2127}
2128
2129static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002130tooltip_func(struct toytimer *tt)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002131{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002132 struct tooltip *tooltip = container_of(tt, struct tooltip, timer);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002133
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002134 window_create_tooltip(tooltip);
2135}
2136
2137#define TOOLTIP_TIMEOUT 500
2138static int
2139tooltip_timer_reset(struct tooltip *tooltip)
2140{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002141 toytimer_arm_once_usec(&tooltip->timer, TOOLTIP_TIMEOUT * 1000);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002142
2143 return 0;
2144}
2145
2146int
2147widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2148{
2149 struct tooltip *tooltip = parent->tooltip;
2150
2151 parent->tooltip_count++;
2152 if (tooltip) {
2153 tooltip->x = x;
2154 tooltip->y = y;
2155 tooltip_timer_reset(tooltip);
2156 return 0;
2157 }
2158
2159 /* the handler might be triggered too fast via input device motion, so
2160 * we need this check here to make sure tooltip is fully initialized */
2161 if (parent->tooltip_count > 1)
2162 return 0;
2163
2164 tooltip = malloc(sizeof *tooltip);
2165 if (!tooltip)
2166 return -1;
2167
2168 parent->tooltip = tooltip;
2169 tooltip->parent = parent;
2170 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002171 tooltip->x = x;
2172 tooltip->y = y;
2173 tooltip->entry = strdup(entry);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002174 toytimer_init(&tooltip->timer, CLOCK_MONOTONIC,
2175 parent->window->display, tooltip_func);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002176 tooltip_timer_reset(tooltip);
2177
2178 return 0;
2179}
2180
2181static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002182frame_resize_handler(struct widget *widget,
2183 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002184{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002185 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002186 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002187 struct rectangle interior;
2188 struct rectangle input;
2189 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002190
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002191 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002192 interior.x = 0;
2193 interior.y = 0;
2194 interior.width = width;
2195 interior.height = height;
2196 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 frame_resize(frame->frame, width, height);
2198 frame_interior(frame->frame, &interior.x, &interior.y,
2199 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002200 }
2201
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002202 widget_set_allocation(child, interior.x, interior.y,
2203 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002204
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002205 if (child->resize_handler) {
2206 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002207 child->user_data);
2208
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002209 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002210 width = child->allocation.width;
2211 height = child->allocation.height;
2212 } else {
2213 frame_resize_inside(frame->frame,
2214 child->allocation.width,
2215 child->allocation.height);
2216 width = frame_width(frame->frame);
2217 height = frame_height(frame->frame);
2218 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002219 }
2220
Scott Moreauf7e498c2012-05-14 11:39:29 -06002221 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002222
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002223 widget->surface->input_region =
2224 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002225 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002226 frame_input_rect(frame->frame, &input.x, &input.y,
2227 &input.width, &input.height);
2228 wl_region_add(widget->surface->input_region,
2229 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002230 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002231 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002232 }
2233
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002234 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002235
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002236 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002237 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002238 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2239 &opaque.width, &opaque.height);
2240
2241 wl_region_add(widget->surface->opaque_region,
2242 opaque.x, opaque.y,
2243 opaque.width, opaque.height);
2244 } else {
2245 wl_region_add(widget->surface->opaque_region,
2246 0, 0, width, height);
2247 }
Martin Minarik1998b152012-05-10 02:04:35 +02002248 }
2249
Martin Minarik1998b152012-05-10 02:04:35 +02002250
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002251 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002252}
2253
2254static void
2255frame_redraw_handler(struct widget *widget, void *data)
2256{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002257 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002258 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002259 struct window *window = widget->window;
2260
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002261 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002262 return;
2263
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002264 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002265
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002266 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002267
2268 cairo_destroy(cr);
2269}
2270
2271static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002272frame_get_pointer_image_for_location(struct window_frame *frame,
2273 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002274{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002275 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002276
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002277 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002278 return CURSOR_LEFT_PTR;
2279
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002280 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002281 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002282 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002283 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002284 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002285 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002286 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002287 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002288 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002289 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002290 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002291 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002292 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002293 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002294 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002295 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002296 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002297 case THEME_LOCATION_EXTERIOR:
2298 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002299 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002300 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002301 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002302}
2303
Pekka Paalanen26237862014-09-10 15:10:30 +03002304static void
2305frame_menu_func(void *data, struct input *input, int index)
2306{
2307 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002308
2309 switch (index) {
2310 case 0: /* close */
2311 window_close(window);
2312 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002313 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002314 /* we don't have a way to get out of fullscreen for now */
2315 if (window->fullscreen_handler)
2316 window->fullscreen_handler(window, window->user_data);
2317 break;
2318 }
2319}
2320
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002321void
2322window_show_frame_menu(struct window *window,
2323 struct input *input, uint32_t time)
2324{
2325 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002326 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002327
Pekka Paalanen26237862014-09-10 15:10:30 +03002328 static const char *entries[] = {
2329 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002330 "Fullscreen"
2331 };
2332
2333 if (window->fullscreen_handler)
2334 count = ARRAY_LENGTH(entries);
2335 else
2336 count = ARRAY_LENGTH(entries) - 1;
2337
2338 input_get_position(input, &x, &y);
2339 window_show_menu(window->display, input, time, window,
2340 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002341}
2342
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002343static int
2344frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002345 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002346{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002347 struct window_frame *frame = data;
2348 enum theme_location location;
2349
2350 location = frame_pointer_enter(frame->frame, input, x, y);
2351 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2352 widget_schedule_redraw(frame->widget);
2353
2354 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002355}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002356
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002357static int
2358frame_motion_handler(struct widget *widget,
2359 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002360 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002361{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002362 struct window_frame *frame = data;
2363 enum theme_location location;
2364
2365 location = frame_pointer_motion(frame->frame, input, x, y);
2366 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2367 widget_schedule_redraw(frame->widget);
2368
2369 return frame_get_pointer_image_for_location(data, location);
2370}
2371
2372static void
2373frame_leave_handler(struct widget *widget,
2374 struct input *input, void *data)
2375{
2376 struct window_frame *frame = data;
2377
2378 frame_pointer_leave(frame->frame, input);
2379 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2380 widget_schedule_redraw(frame->widget);
2381}
2382
2383static void
2384frame_handle_status(struct window_frame *frame, struct input *input,
2385 uint32_t time, enum theme_location location)
2386{
2387 struct window *window = frame->widget->window;
2388 uint32_t status;
2389
2390 status = frame_status(frame->frame);
2391 if (status & FRAME_STATUS_REPAINT)
2392 widget_schedule_redraw(frame->widget);
2393
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002394 if (status & FRAME_STATUS_MINIMIZE) {
2395 window_set_minimized(window);
2396 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2397 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002398
2399 if (status & FRAME_STATUS_MENU) {
2400 window_show_frame_menu(window, input, time);
2401 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2402 }
2403
2404 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002405 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002406 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2407 }
2408
2409 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002410 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002411 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002412 }
2413
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002414 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002415 input_ungrab(input);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002416 zxdg_toplevel_v6_move(window->xdg_toplevel,
2417 input_get_seat(input),
2418 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002419
2420 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2421 }
2422
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002423 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002424 input_ungrab(input);
2425
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002426 zxdg_toplevel_v6_resize(window->xdg_toplevel,
2427 input_get_seat(input),
2428 window->display->serial,
2429 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002430
2431 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2432 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002433}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002434
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002435#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002436static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002437frame_button_handler(struct widget *widget,
2438 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002439 uint32_t button, enum wl_pointer_button_state state,
2440 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002441
2442{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002443 struct window_frame *frame = data;
2444 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002445
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002446 frame->double_click = 0;
2447 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2448 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2449 frame->double_click = 1;
2450 frame->did_double = 1;
2451 } else
2452 frame->did_double = 0;
2453
2454 frame->last_time = time;
2455 } else if (frame->did_double == 1) {
2456 frame->double_click = 1;
2457 frame->did_double = 0;
2458 }
2459
2460 if (frame->double_click)
2461 location = frame_double_click(frame->frame, input,
2462 button, state);
2463 else
2464 location = frame_pointer_button(frame->frame, input,
2465 button, state);
2466
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002467 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002468}
2469
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002470static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002471frame_touch_down_handler(struct widget *widget, struct input *input,
2472 uint32_t serial, uint32_t time, int32_t id,
2473 float x, float y, void *data)
2474{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002475 struct window_frame *frame = data;
2476
Xiong Zhang382de462014-06-12 11:06:26 +08002477 frame->double_click = 0;
2478 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2479 frame->last_id == id) {
2480 frame->double_click = 1;
2481 frame->did_double = 1;
2482 frame->double_id = id;
2483 } else
2484 frame->did_double = 0;
2485
2486 frame->last_time = time;
2487 frame->last_id = id;
2488
2489 if (frame->double_click)
2490 frame_double_touch_down(frame->frame, input, id, x, y);
2491 else
2492 frame_touch_down(frame->frame, input, id, x, y);
2493
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002494 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2495}
2496
2497static void
2498frame_touch_up_handler(struct widget *widget,
2499 struct input *input, uint32_t serial, uint32_t time,
2500 int32_t id, void *data)
2501{
2502 struct window_frame *frame = data;
2503
Xiong Zhang382de462014-06-12 11:06:26 +08002504 if (frame->double_id == id && frame->did_double) {
2505 frame->did_double = 0;
2506 frame->double_id = 0;
2507 frame_double_touch_up(frame->frame, input, id);
2508 } else
2509 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002510 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002511}
2512
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002513struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002514window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002515{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002516 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002517 uint32_t buttons;
2518
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002519 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002520 buttons = FRAME_BUTTON_NONE;
2521 } else {
2522 buttons = FRAME_BUTTON_ALL;
2523 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524
Peter Huttererf3d62272013-08-08 11:57:05 +10002525 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002526 frame->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01002527 buttons, window->title, NULL);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002528
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002529 frame->widget = window_add_widget(window, frame);
2530 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002531
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002532 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2533 widget_set_resize_handler(frame->widget, frame_resize_handler);
2534 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002535 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002536 widget_set_motion_handler(frame->widget, frame_motion_handler);
2537 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002538 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002539 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002540
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002541 window->frame = frame;
2542
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002543 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002544}
2545
Kristian Høgsberga1627922012-06-20 17:30:03 -04002546void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002547window_frame_set_child_size(struct widget *widget, int child_width,
2548 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002549{
2550 struct display *display = widget->window->display;
2551 struct theme *t = display->theme;
2552 int decoration_width, decoration_height;
2553 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002554 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002555
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002556 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002557 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002558 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002559 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002560
2561 width = child_width + decoration_width;
2562 height = child_height + decoration_height;
2563 } else {
2564 width = child_width;
2565 height = child_height;
2566 }
2567
2568 window_schedule_resize(widget->window, width, height);
2569}
2570
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002571static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002572window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002573{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002574 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002575
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002576 /* frame->child must be destroyed by the application */
2577 widget_destroy(frame->widget);
2578 free(frame);
2579}
2580
2581static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002582input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002583 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002584{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002585 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002586 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002587
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002588 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002589 return;
2590
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002591 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002592 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002593 widget = old;
2594 if (input->grab)
2595 widget = input->grab;
2596 if (widget->leave_handler)
2597 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002598 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002599 }
2600
2601 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002602 widget = focus;
2603 if (input->grab)
2604 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002605 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002606 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002607 cursor = widget->enter_handler(focus, input, x, y,
2608 widget->user_data);
2609 else
2610 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002611
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002612 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002613 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002614}
2615
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002616void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002617touch_grab(struct input *input, int32_t touch_id)
2618{
2619 input->touch_grab = 1;
2620 input->touch_grab_id = touch_id;
2621}
2622
2623void
2624touch_ungrab(struct input *input)
2625{
2626 struct touch_point *tp, *tmp;
2627
2628 input->touch_grab = 0;
2629
2630 wl_list_for_each_safe(tp, tmp,
2631 &input->touch_point_list, link) {
2632 if (tp->id != input->touch_grab_id)
2633 continue;
2634 wl_list_remove(&tp->link);
2635 free(tp);
2636
2637 return;
2638 }
2639}
2640
2641void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002642input_grab(struct input *input, struct widget *widget, uint32_t button)
2643{
2644 input->grab = widget;
2645 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002646
2647 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002648}
2649
2650void
2651input_ungrab(struct input *input)
2652{
2653 struct widget *widget;
2654
2655 input->grab = NULL;
2656 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002657 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002658 input->sx, input->sy);
2659 input_set_focus_widget(input, widget, input->sx, input->sy);
2660 }
2661}
2662
2663static void
Derek Foreman118a4292015-04-22 17:23:35 -05002664cursor_delay_timer_reset(struct input *input, uint32_t duration)
2665{
Derek Foreman118a4292015-04-22 17:23:35 -05002666 if (!duration)
2667 input->cursor_timer_running = false;
2668 else
2669 input->cursor_timer_running = true;
2670
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02002671 toytimer_arm_once_usec(&input->cursor_timer, duration * 1000);
Derek Foreman118a4292015-04-22 17:23:35 -05002672}
2673
2674static void cancel_pointer_image_update(struct input *input)
2675{
2676 if (input->cursor_timer_running)
2677 cursor_delay_timer_reset(input, 0);
2678}
2679
2680static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002681input_remove_pointer_focus(struct input *input)
2682{
2683 struct window *window = input->pointer_focus;
2684
2685 if (!window)
2686 return;
2687
2688 input_set_focus_widget(input, NULL, 0, 0);
2689
2690 input->pointer_focus = NULL;
2691 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002692 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002693}
2694
2695static void
2696pointer_handle_enter(void *data, struct wl_pointer *pointer,
2697 uint32_t serial, struct wl_surface *surface,
2698 wl_fixed_t sx_w, wl_fixed_t sy_w)
2699{
2700 struct input *input = data;
2701 struct window *window;
2702 struct widget *widget;
2703 float sx = wl_fixed_to_double(sx_w);
2704 float sy = wl_fixed_to_double(sy_w);
2705
2706 if (!surface) {
2707 /* enter event for a window we've just destroyed */
2708 return;
2709 }
2710
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002711 window = wl_surface_get_user_data(surface);
2712 if (surface != window->main_surface->surface) {
2713 DBG("Ignoring input event from subsurface %p\n", surface);
2714 return;
2715 }
2716
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002717 input->display->serial = serial;
2718 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002719 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002720
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002721 input->sx = sx;
2722 input->sy = sy;
2723
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002724 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002725 input_set_focus_widget(input, widget, sx, sy);
2726}
2727
2728static void
2729pointer_handle_leave(void *data, struct wl_pointer *pointer,
2730 uint32_t serial, struct wl_surface *surface)
2731{
2732 struct input *input = data;
2733
2734 input->display->serial = serial;
2735 input_remove_pointer_focus(input);
2736}
2737
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002738static void
Daniel Stone37816df2012-05-16 18:45:18 +01002739pointer_handle_motion(void *data, struct wl_pointer *pointer,
2740 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002741{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002742 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002743 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002744 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002745 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002746 float sx = wl_fixed_to_double(sx_w);
2747 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002748
Paul Winwoodb22bf572013-08-29 10:52:54 +01002749 if (!window)
2750 return;
2751
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002752 input->sx = sx;
2753 input->sy = sy;
2754
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002755 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002756 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002757 * based on the old surface dimensions. However, if we have an active
2758 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002759 */
Derek Foreman46812b62015-08-26 17:13:27 -05002760 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002761 sy < window->main_surface->allocation.y ||
2762 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002763 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002764 return;
2765
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002766 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002767 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002768 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002769 }
2770
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002771 if (input->grab)
2772 widget = input->grab;
2773 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002774 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002775 if (widget) {
2776 if (widget->motion_handler)
2777 cursor = widget->motion_handler(input->focus_widget,
2778 input, time, sx, sy,
2779 widget->user_data);
2780 else
2781 cursor = widget->default_cursor;
2782 } else
2783 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002784
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002785 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002786}
2787
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002788static void
Daniel Stone37816df2012-05-16 18:45:18 +01002789pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002790 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002791{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002792 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002793 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002794 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002795
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002796 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002797 if (input->focus_widget && input->grab == NULL &&
2798 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002799 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002800
Neil Roberts6b28aad2012-01-23 19:11:18 +00002801 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002802 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002803 (*widget->button_handler)(widget,
2804 input, time,
2805 button, state,
2806 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002807
Daniel Stone4dbadb12012-05-30 16:31:51 +01002808 if (input->grab && input->grab_button == button &&
2809 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002810 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002811}
2812
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002813static void
Daniel Stone37816df2012-05-16 18:45:18 +01002814pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002815 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002816{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002817 struct input *input = data;
2818 struct widget *widget;
2819
2820 widget = input->focus_widget;
2821 if (input->grab)
2822 widget = input->grab;
2823 if (widget && widget->axis_handler)
2824 (*widget->axis_handler)(widget,
2825 input, time,
2826 axis, value,
2827 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002828}
2829
Peter Hutterer87743e92016-01-18 16:38:22 +10002830static void
2831pointer_handle_frame(void *data, struct wl_pointer *pointer)
2832{
2833 struct input *input = data;
2834 struct widget *widget;
2835
2836 widget = input->focus_widget;
2837 if (input->grab)
2838 widget = input->grab;
2839 if (widget && widget->pointer_frame_handler)
2840 (*widget->pointer_frame_handler)(widget,
2841 input,
2842 widget->user_data);
2843}
2844
2845static void
2846pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2847 uint32_t source)
2848{
2849 struct input *input = data;
2850 struct widget *widget;
2851
2852 widget = input->focus_widget;
2853 if (input->grab)
2854 widget = input->grab;
2855 if (widget && widget->axis_source_handler)
2856 (*widget->axis_source_handler)(widget,
2857 input,
2858 source,
2859 widget->user_data);
2860}
2861
2862static void
2863pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2864 uint32_t time, uint32_t axis)
2865{
2866 struct input *input = data;
2867 struct widget *widget;
2868
2869 widget = input->focus_widget;
2870 if (input->grab)
2871 widget = input->grab;
2872 if (widget && widget->axis_stop_handler)
2873 (*widget->axis_stop_handler)(widget,
2874 input, time,
2875 axis,
2876 widget->user_data);
2877}
2878
2879static void
2880pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2881 uint32_t axis, int32_t discrete)
2882{
2883 struct input *input = data;
2884 struct widget *widget;
2885
2886 widget = input->focus_widget;
2887 if (input->grab)
2888 widget = input->grab;
2889 if (widget && widget->axis_discrete_handler)
2890 (*widget->axis_discrete_handler)(widget,
2891 input,
2892 axis,
2893 discrete,
2894 widget->user_data);
2895}
2896
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002897static const struct wl_pointer_listener pointer_listener = {
2898 pointer_handle_enter,
2899 pointer_handle_leave,
2900 pointer_handle_motion,
2901 pointer_handle_button,
2902 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002903 pointer_handle_frame,
2904 pointer_handle_axis_source,
2905 pointer_handle_axis_stop,
2906 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002907};
2908
2909static void
2910input_remove_keyboard_focus(struct input *input)
2911{
2912 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002913
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002914 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002915
2916 if (!window)
2917 return;
2918
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002919 if (window->keyboard_focus_handler)
2920 (*window->keyboard_focus_handler)(window, NULL,
2921 window->user_data);
2922
2923 input->keyboard_focus = NULL;
2924}
2925
2926static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002927keyboard_repeat_func(struct toytimer *tt)
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002928{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02002929 struct input *input = container_of(tt, struct input, repeat_timer);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002930 struct window *window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002931
2932 if (window && window->key_handler) {
2933 (*window->key_handler)(window, input, input->repeat_time,
2934 input->repeat_key, input->repeat_sym,
2935 WL_KEYBOARD_KEY_STATE_PRESSED,
2936 window->user_data);
2937 }
2938}
2939
2940static void
2941keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2942 uint32_t format, int fd, uint32_t size)
2943{
2944 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002945 struct xkb_keymap *keymap;
2946 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002947#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002948 struct xkb_compose_table *compose_table;
2949 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002950#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002951 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002952 char *map_str;
2953
2954 if (!data) {
2955 close(fd);
2956 return;
2957 }
2958
2959 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2960 close(fd);
2961 return;
2962 }
2963
2964 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2965 if (map_str == MAP_FAILED) {
2966 close(fd);
2967 return;
2968 }
2969
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002970 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03002971 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2972 map_str,
2973 XKB_KEYMAP_FORMAT_TEXT_V1,
2974 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002975 munmap(map_str, size);
2976 close(fd);
2977
Rui Matos3eccb862013-10-10 19:44:22 +02002978 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002979 fprintf(stderr, "failed to compile keymap\n");
2980 return;
2981 }
2982
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002983 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02002984 state = xkb_state_new(keymap);
2985 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002986 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002987 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002988 return;
2989 }
2990
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002991 /* Look up the preferred locale, falling back to "C" as default */
2992 if (!(locale = getenv("LC_ALL")))
2993 if (!(locale = getenv("LC_CTYPE")))
2994 if (!(locale = getenv("LANG")))
2995 locale = "C";
2996
2997 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01002998#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002999 compose_table =
3000 xkb_compose_table_new_from_locale(input->display->xkb_context,
3001 locale,
3002 XKB_COMPOSE_COMPILE_NO_FLAGS);
3003 if (compose_table) {
3004 /* Set up XKB compose state */
3005 compose_state = xkb_compose_state_new(compose_table,
3006 XKB_COMPOSE_STATE_NO_FLAGS);
3007 if (compose_state) {
3008 xkb_compose_state_unref(input->xkb.compose_state);
3009 xkb_compose_table_unref(input->xkb.compose_table);
3010 input->xkb.compose_state = compose_state;
3011 input->xkb.compose_table = compose_table;
3012 } else {
3013 fprintf(stderr, "could not create XKB compose state. "
3014 "Disabiling compose.\n");
3015 xkb_compose_table_unref(compose_table);
3016 compose_table = NULL;
3017 }
3018 } else {
3019 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3020 "Disabiling compose\n", locale);
3021 }
Daniel Stone5b015962016-10-20 14:45:58 +01003022#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003023
Rui Matos3eccb862013-10-10 19:44:22 +02003024 xkb_keymap_unref(input->xkb.keymap);
3025 xkb_state_unref(input->xkb.state);
3026 input->xkb.keymap = keymap;
3027 input->xkb.state = state;
3028
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003029 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003030 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003031 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003032 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003033 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003034 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003035}
3036
3037static void
3038keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3039 uint32_t serial, struct wl_surface *surface,
3040 struct wl_array *keys)
3041{
3042 struct input *input = data;
3043 struct window *window;
3044
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003045 if (!surface) {
3046 /* enter event for a window we've just destroyed */
3047 return;
3048 }
3049
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003050 input->display->serial = serial;
3051 input->keyboard_focus = wl_surface_get_user_data(surface);
3052
3053 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003054 if (window->keyboard_focus_handler)
3055 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003056 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003057}
3058
3059static void
3060keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3061 uint32_t serial, struct wl_surface *surface)
3062{
3063 struct input *input = data;
3064
3065 input->display->serial = serial;
3066 input_remove_keyboard_focus(input);
3067}
3068
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003069/* Translate symbols appropriately if a compose sequence is being entered */
3070static xkb_keysym_t
3071process_key_press(xkb_keysym_t sym, struct input *input)
3072{
Daniel Stone5b015962016-10-20 14:45:58 +01003073#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003074 if (!input->xkb.compose_state)
3075 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003076 if (sym == XKB_KEY_NoSymbol)
3077 return sym;
3078 if (xkb_compose_state_feed(input->xkb.compose_state,
3079 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3080 return sym;
3081
3082 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3083 case XKB_COMPOSE_COMPOSING:
3084 return XKB_KEY_NoSymbol;
3085 case XKB_COMPOSE_COMPOSED:
3086 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3087 case XKB_COMPOSE_CANCELLED:
3088 return XKB_KEY_NoSymbol;
3089 case XKB_COMPOSE_NOTHING:
3090 return sym;
3091 default:
3092 return sym;
3093 }
Daniel Stone5b015962016-10-20 14:45:58 +01003094#else
3095 return sym;
3096#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003097}
3098
Scott Moreau210d0792012-03-22 10:47:01 -06003099static void
Daniel Stone37816df2012-05-16 18:45:18 +01003100keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003101 uint32_t serial, uint32_t time, uint32_t key,
3102 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003103{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003104 struct input *input = data;
3105 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003106 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003107 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003108 const xkb_keysym_t *syms;
3109 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003110 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003111
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003112 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003113 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003114 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003115 return;
3116
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003117 /* We only use input grabs for pointer events for now, so just
3118 * ignore key presses if a grab is active. We expand the key
3119 * event delivery mechanism to route events to widgets to
3120 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003121 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003122 if (input->grab && input->grab_button == 0)
3123 return;
3124
Ran Benita2e1968f2014-08-19 23:59:51 +03003125 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003126
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003127 sym = XKB_KEY_NoSymbol;
3128 if (num_syms == 1)
3129 sym = syms[0];
3130
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003131
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003132 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003133 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003134 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003135 } else if (sym == XKB_KEY_F11 &&
3136 window->fullscreen_handler &&
3137 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3138 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003139 } else if (sym == XKB_KEY_F4 &&
3140 input->modifiers == MOD_ALT_MASK &&
3141 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003142 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003143 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003144 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3145 sym = process_key_press(sym, input);
3146
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003147 (*window->key_handler)(window, input, time, key,
3148 sym, state, window->user_data);
3149 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003150
3151 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3152 key == input->repeat_key) {
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003153 toytimer_disarm(&input->repeat_timer);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003154 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3155 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003156 input->repeat_sym = sym;
3157 input->repeat_key = key;
3158 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003159 its.it_interval.tv_sec = input->repeat_rate_sec;
3160 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3161 its.it_value.tv_sec = input->repeat_delay_sec;
3162 its.it_value.tv_nsec = input->repeat_delay_nsec;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02003163 toytimer_arm(&input->repeat_timer, &its);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003164 }
3165}
3166
3167static void
Daniel Stone351eb612012-05-31 15:27:47 -04003168keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3169 uint32_t serial, uint32_t mods_depressed,
3170 uint32_t mods_latched, uint32_t mods_locked,
3171 uint32_t group)
3172{
3173 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003174 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003175
Matt Ropere61561f2013-06-24 16:52:43 +01003176 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3177 if (!input->xkb.keymap)
3178 return;
3179
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003180 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3181 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003182 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003183 XKB_STATE_MODS_DEPRESSED |
3184 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003185 input->modifiers = 0;
3186 if (mask & input->xkb.control_mask)
3187 input->modifiers |= MOD_CONTROL_MASK;
3188 if (mask & input->xkb.alt_mask)
3189 input->modifiers |= MOD_ALT_MASK;
3190 if (mask & input->xkb.shift_mask)
3191 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003192}
3193
Jonny Lamb06959082014-08-12 14:58:27 +02003194static void
3195set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3196{
3197 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3198 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3199
3200 /* a rate of zero disables any repeating, regardless of the delay's
3201 * value */
3202 if (rate == 0)
3203 return;
3204
3205 if (rate == 1)
3206 input->repeat_rate_sec = 1;
3207 else
3208 input->repeat_rate_nsec = 1000000000 / rate;
3209
3210 input->repeat_delay_sec = delay / 1000;
3211 delay -= (input->repeat_delay_sec * 1000);
3212 input->repeat_delay_nsec = delay * 1000 * 1000;
3213}
3214
3215static void
3216keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3217 int32_t rate, int32_t delay)
3218{
3219 struct input *input = data;
3220
3221 set_repeat_info(input, rate, delay);
3222}
3223
Daniel Stone37816df2012-05-16 18:45:18 +01003224static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003225 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003226 keyboard_handle_enter,
3227 keyboard_handle_leave,
3228 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003229 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003230 keyboard_handle_repeat_info
3231
Daniel Stone37816df2012-05-16 18:45:18 +01003232};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003233
3234static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003235touch_handle_down(void *data, struct wl_touch *wl_touch,
3236 uint32_t serial, uint32_t time, struct wl_surface *surface,
3237 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3238{
3239 struct input *input = data;
3240 struct widget *widget;
3241 float sx = wl_fixed_to_double(x_w);
3242 float sy = wl_fixed_to_double(y_w);
3243
Rusty Lynch1084da52013-08-15 09:10:08 -07003244 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003245 input->touch_focus = wl_surface_get_user_data(surface);
3246 if (!input->touch_focus) {
3247 DBG("Failed to find to touch focus for surface %p\n", surface);
3248 return;
3249 }
3250
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003251 if (surface != input->touch_focus->main_surface->surface) {
3252 DBG("Ignoring input event from subsurface %p\n", surface);
3253 input->touch_focus = NULL;
3254 return;
3255 }
3256
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003257 if (input->grab)
3258 widget = input->grab;
3259 else
3260 widget = window_find_widget(input->touch_focus,
3261 wl_fixed_to_double(x_w),
3262 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003263 if (widget) {
3264 struct touch_point *tp = xmalloc(sizeof *tp);
3265 if (tp) {
3266 tp->id = id;
3267 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003268 tp->x = sx;
3269 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003270 wl_list_insert(&input->touch_point_list, &tp->link);
3271
3272 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003273 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003274 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003275 sx, sy,
3276 widget->user_data);
3277 }
3278 }
3279}
3280
3281static void
3282touch_handle_up(void *data, struct wl_touch *wl_touch,
3283 uint32_t serial, uint32_t time, int32_t id)
3284{
3285 struct input *input = data;
3286 struct touch_point *tp, *tmp;
3287
Rusty Lynch041815a2013-08-08 21:20:38 -07003288 if (!input->touch_focus) {
3289 DBG("No touch focus found for touch up event!\n");
3290 return;
3291 }
3292
3293 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3294 if (tp->id != id)
3295 continue;
3296
3297 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003298 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003299 time, id,
3300 tp->widget->user_data);
3301
3302 wl_list_remove(&tp->link);
3303 free(tp);
3304
3305 return;
3306 }
3307}
3308
3309static void
3310touch_handle_motion(void *data, struct wl_touch *wl_touch,
3311 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3312{
3313 struct input *input = data;
3314 struct touch_point *tp;
3315 float sx = wl_fixed_to_double(x_w);
3316 float sy = wl_fixed_to_double(y_w);
3317
3318 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3319
3320 if (!input->touch_focus) {
3321 DBG("No touch focus found for touch motion event!\n");
3322 return;
3323 }
3324
3325 wl_list_for_each(tp, &input->touch_point_list, link) {
3326 if (tp->id != id)
3327 continue;
3328
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003329 tp->x = sx;
3330 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003331 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003332 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003333 id, sx, sy,
3334 tp->widget->user_data);
3335 return;
3336 }
3337}
3338
3339static void
3340touch_handle_frame(void *data, struct wl_touch *wl_touch)
3341{
3342 struct input *input = data;
3343 struct touch_point *tp, *tmp;
3344
3345 DBG("touch_handle_frame\n");
3346
3347 if (!input->touch_focus) {
3348 DBG("No touch focus found for touch frame event!\n");
3349 return;
3350 }
3351
3352 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3353 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003354 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003355 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003356 }
3357}
3358
3359static void
3360touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3361{
3362 struct input *input = data;
3363 struct touch_point *tp, *tmp;
3364
3365 DBG("touch_handle_cancel\n");
3366
3367 if (!input->touch_focus) {
3368 DBG("No touch focus found for touch cancel event!\n");
3369 return;
3370 }
3371
3372 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3373 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003374 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3375 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003376
3377 wl_list_remove(&tp->link);
3378 free(tp);
3379 }
3380}
3381
3382static const struct wl_touch_listener touch_listener = {
3383 touch_handle_down,
3384 touch_handle_up,
3385 touch_handle_motion,
3386 touch_handle_frame,
3387 touch_handle_cancel,
3388};
3389
3390static void
Daniel Stone37816df2012-05-16 18:45:18 +01003391seat_handle_capabilities(void *data, struct wl_seat *seat,
3392 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003393{
Daniel Stone37816df2012-05-16 18:45:18 +01003394 struct input *input = data;
3395
3396 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3397 input->pointer = wl_seat_get_pointer(seat);
3398 wl_pointer_set_user_data(input->pointer, input);
3399 wl_pointer_add_listener(input->pointer, &pointer_listener,
3400 input);
3401 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003402 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003403 wl_pointer_release(input->pointer);
3404 else
3405 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003406 input->pointer = NULL;
3407 }
3408
3409 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3410 input->keyboard = wl_seat_get_keyboard(seat);
3411 wl_keyboard_set_user_data(input->keyboard, input);
3412 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3413 input);
3414 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003415 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003416 wl_keyboard_release(input->keyboard);
3417 else
3418 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003419 input->keyboard = NULL;
3420 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003421
3422 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3423 input->touch = wl_seat_get_touch(seat);
3424 wl_touch_set_user_data(input->touch, input);
3425 wl_touch_add_listener(input->touch, &touch_listener, input);
3426 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003427 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003428 wl_touch_release(input->touch);
3429 else
3430 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003431 input->touch = NULL;
3432 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003433}
3434
Rob Bradford08031182013-08-13 20:11:03 +01003435static void
3436seat_handle_name(void *data, struct wl_seat *seat,
3437 const char *name)
3438{
3439
3440}
3441
Daniel Stone37816df2012-05-16 18:45:18 +01003442static const struct wl_seat_listener seat_listener = {
3443 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003444 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003445};
3446
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003447void
3448input_get_position(struct input *input, int32_t *x, int32_t *y)
3449{
3450 *x = input->sx;
3451 *y = input->sy;
3452}
3453
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003454int
3455input_get_touch(struct input *input, int32_t id, float *x, float *y)
3456{
3457 struct touch_point *tp;
3458
3459 wl_list_for_each(tp, &input->touch_point_list, link) {
3460 if (tp->id != id)
3461 continue;
3462
3463 *x = tp->x;
3464 *y = tp->y;
3465 return 0;
3466 }
3467
3468 return -1;
3469}
3470
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003471struct display *
3472input_get_display(struct input *input)
3473{
3474 return input->display;
3475}
3476
Daniel Stone37816df2012-05-16 18:45:18 +01003477struct wl_seat *
3478input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003479{
Daniel Stone37816df2012-05-16 18:45:18 +01003480 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003481}
3482
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003483uint32_t
3484input_get_modifiers(struct input *input)
3485{
3486 return input->modifiers;
3487}
3488
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003489struct widget *
3490input_get_focus_widget(struct input *input)
3491{
3492 return input->focus_widget;
3493}
3494
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003495struct data_offer {
3496 struct wl_data_offer *offer;
3497 struct input *input;
3498 struct wl_array types;
3499 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003500
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003501 struct task io_task;
3502 int fd;
3503 data_func_t func;
3504 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003505 uint32_t dnd_action;
3506 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003507 void *user_data;
3508};
3509
3510static void
3511data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3512{
3513 struct data_offer *offer = data;
3514 char **p;
3515
3516 p = wl_array_add(&offer->types, sizeof *p);
3517 *p = strdup(type);
3518}
3519
Carlos Garnacho9c931792016-01-18 23:52:12 +01003520static void
3521data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3522{
3523 struct data_offer *offer = data;
3524
3525 offer->source_actions = source_actions;
3526}
3527
3528static void
3529data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3530{
3531 struct data_offer *offer = data;
3532
3533 offer->dnd_action = dnd_action;
3534}
3535
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003536static const struct wl_data_offer_listener data_offer_listener = {
3537 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003538 data_offer_source_actions,
3539 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003540};
3541
3542static void
3543data_offer_destroy(struct data_offer *offer)
3544{
3545 char **p;
3546
3547 offer->refcount--;
3548 if (offer->refcount == 0) {
3549 wl_data_offer_destroy(offer->offer);
3550 for (p = offer->types.data; *p; p++)
3551 free(*p);
3552 wl_array_release(&offer->types);
3553 free(offer);
3554 }
3555}
3556
3557static void
3558data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003559 struct wl_data_device *data_device,
3560 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003561{
3562 struct data_offer *offer;
3563
Brian Lovinbc919262013-08-07 15:34:59 -07003564 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003565
3566 wl_array_init(&offer->types);
3567 offer->refcount = 1;
3568 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003569 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003570 wl_data_offer_add_listener(offer->offer,
3571 &data_offer_listener, offer);
3572}
3573
3574static void
3575data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003576 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003577 wl_fixed_t x_w, wl_fixed_t y_w,
3578 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003579{
3580 struct input *input = data;
3581 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003582 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003583 float x = wl_fixed_to_double(x_w);
3584 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003585 char **p;
3586
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003587 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003588 input->drag_enter_serial = serial;
3589 input->drag_focus = window,
3590 input->drag_x = x;
3591 input->drag_y = y;
3592
3593 if (!input->touch_grab)
3594 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003595
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003596 if (offer) {
3597 input->drag_offer = wl_data_offer_get_user_data(offer);
3598
3599 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3600 *p = NULL;
3601
3602 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003603
3604 if (input->display->data_device_manager_version >=
3605 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3606 wl_data_offer_set_actions(offer,
3607 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3608 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3609 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3610 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003611 } else {
3612 input->drag_offer = NULL;
3613 types_data = NULL;
3614 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003615
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003616 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003617 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003618 window->user_data);
3619}
3620
3621static void
3622data_device_leave(void *data, struct wl_data_device *data_device)
3623{
3624 struct input *input = data;
3625
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003626 if (input->drag_offer) {
3627 data_offer_destroy(input->drag_offer);
3628 input->drag_offer = NULL;
3629 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003630}
3631
3632static void
3633data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003634 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003635{
3636 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003637 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003638 float x = wl_fixed_to_double(x_w);
3639 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003640 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003641
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003642 input->drag_x = x;
3643 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003644
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003645 if (input->drag_offer)
3646 types_data = input->drag_offer->types.data;
3647 else
3648 types_data = NULL;
3649
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003650 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003651 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003652 window->user_data);
3653}
3654
3655static void
3656data_device_drop(void *data, struct wl_data_device *data_device)
3657{
3658 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003659 struct window *window = input->drag_focus;
3660 float x, y;
3661
3662 x = input->drag_x;
3663 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003664
3665 if (window->drop_handler)
3666 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003667 x, y, window->user_data);
3668
3669 if (input->touch_grab)
3670 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003671}
3672
3673static void
3674data_device_selection(void *data,
3675 struct wl_data_device *wl_data_device,
3676 struct wl_data_offer *offer)
3677{
3678 struct input *input = data;
3679 char **p;
3680
3681 if (input->selection_offer)
3682 data_offer_destroy(input->selection_offer);
3683
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003684 if (offer) {
3685 input->selection_offer = wl_data_offer_get_user_data(offer);
3686 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3687 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003688 } else {
3689 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003690 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003691}
3692
3693static const struct wl_data_device_listener data_device_listener = {
3694 data_device_data_offer,
3695 data_device_enter,
3696 data_device_leave,
3697 data_device_motion,
3698 data_device_drop,
3699 data_device_selection
3700};
3701
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003702static void
3703input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003704{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003705 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003706 struct wl_cursor *cursor;
3707 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003708
Daniel Stone80972742012-11-07 17:51:39 +11003709 if (!input->pointer)
3710 return;
3711
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003712 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003713 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003714 return;
3715
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003716 if (index >= (int) cursor->image_count) {
3717 fprintf(stderr, "cursor index out of range\n");
3718 return;
3719 }
3720
3721 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003722 buffer = wl_cursor_image_get_buffer(image);
3723 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003724 return;
3725
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003726 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3727 wl_surface_damage(input->pointer_surface, 0, 0,
3728 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003729 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003730 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3731 input->pointer_surface,
3732 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003733}
3734
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003735static const struct wl_callback_listener pointer_surface_listener;
3736
Derek Foreman493d9792015-03-04 16:26:25 -06003737static bool
3738input_set_pointer_special(struct input *input)
3739{
3740 if (input->current_cursor == CURSOR_BLANK) {
3741 wl_pointer_set_cursor(input->pointer,
3742 input->pointer_enter_serial,
3743 NULL, 0, 0);
3744 return true;
3745 }
3746
3747 if (input->current_cursor == CURSOR_UNSET)
3748 return true;
3749
3750 return false;
3751}
3752
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003753static void
Derek Foreman118a4292015-04-22 17:23:35 -05003754schedule_pointer_image_update(struct input *input,
3755 struct wl_cursor *cursor,
3756 uint32_t duration,
3757 bool force_frame)
3758{
3759 /* Some silly cursor sets have enormous pauses in them. In these
3760 * cases it's better to use a timer even if it results in less
3761 * accurate presentation, since it will save us having to set the
3762 * same cursor image over and over again.
3763 *
3764 * This is really not the way we're supposed to time any kind of
3765 * animation, but we're pretending it's OK here because we don't
3766 * want animated cursors with long delays to needlessly hog CPU.
3767 *
3768 * We use force_frame to ensure we don't accumulate large timing
3769 * errors by running off the wrong clock.
3770 */
3771 if (!force_frame && duration > 100) {
3772 struct timespec tp;
3773
3774 clock_gettime(CLOCK_MONOTONIC, &tp);
3775 input->cursor_timer_start = tp.tv_sec * 1000
3776 + tp.tv_nsec / 1000000;
3777 cursor_delay_timer_reset(input, duration);
3778 return;
3779 }
3780
3781 /* for short durations we'll just spin on frame callbacks for
3782 * accurate timing - the way any kind of timing sensitive animation
3783 * should really be done. */
3784 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3785 wl_callback_add_listener(input->cursor_frame_cb,
3786 &pointer_surface_listener, input);
3787
3788}
3789
3790static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003791pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3792 uint32_t time)
3793{
3794 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003795 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003796 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003797 uint32_t duration;
3798 bool force_frame = true;
3799
3800 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003801
3802 if (callback) {
3803 assert(callback == input->cursor_frame_cb);
3804 wl_callback_destroy(callback);
3805 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003806 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003807 }
3808
Daniel Stone80972742012-11-07 17:51:39 +11003809 if (!input->pointer)
3810 return;
3811
Derek Foreman493d9792015-03-04 16:26:25 -06003812 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003813 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003814
Daniel Stonea494f1d2012-06-18 19:31:12 +01003815 cursor = input->display->cursors[input->current_cursor];
3816 if (!cursor)
3817 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003818
3819 /* FIXME We don't have the current time on the first call so we set
3820 * the animation start to the time of the first frame callback. */
3821 if (time == 0)
3822 input->cursor_anim_start = 0;
3823 else if (input->cursor_anim_start == 0)
3824 input->cursor_anim_start = time;
3825
Derek Foreman118a4292015-04-22 17:23:35 -05003826 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003827
Derek Foreman118a4292015-04-22 17:23:35 -05003828 if (time == 0 || input->cursor_anim_start == 0) {
3829 duration = 0;
3830 i = 0;
3831 } else
3832 i = wl_cursor_frame_and_duration(
3833 cursor,
3834 time - input->cursor_anim_start,
3835 &duration);
3836
3837 if (cursor->image_count > 1)
3838 schedule_pointer_image_update(input, cursor, duration,
3839 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003840
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003841 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003842}
3843
Derek Foreman118a4292015-04-22 17:23:35 -05003844static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003845cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003846{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003847 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003848 struct timespec tp;
3849 struct wl_cursor *cursor;
3850 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003851
3852 if (!input->cursor_timer_running)
3853 return;
3854
Derek Foreman118a4292015-04-22 17:23:35 -05003855 cursor = input->display->cursors[input->current_cursor];
3856 if (!cursor)
3857 return;
3858
3859 clock_gettime(CLOCK_MONOTONIC, &tp);
3860 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3861 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3862}
3863
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003864static const struct wl_callback_listener pointer_surface_listener = {
3865 pointer_surface_frame_callback
3866};
3867
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003868void
3869input_set_pointer_image(struct input *input, int pointer)
3870{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003871 int force = 0;
3872
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003873 if (!input->pointer)
3874 return;
3875
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003876 if (input->pointer_enter_serial > input->cursor_serial)
3877 force = 1;
3878
3879 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003880 return;
3881
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003882 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003883 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003884 if (!input->cursor_frame_cb)
3885 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003886 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003887 /* The current frame callback may be stuck if, for instance,
3888 * the set cursor request was processed by the server after
3889 * this client lost the focus. In this case the cursor surface
3890 * might not be mapped and the frame callback wouldn't ever
3891 * complete. Send a set_cursor and attach to try to map the
3892 * cursor surface again so that the callback will finish */
3893 input_set_pointer_image_index(input, 0);
3894 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003895}
3896
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003897struct wl_data_device *
3898input_get_data_device(struct input *input)
3899{
3900 return input->data_device;
3901}
3902
3903void
3904input_set_selection(struct input *input,
3905 struct wl_data_source *source, uint32_t time)
3906{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003907 if (input->data_device)
3908 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003909}
3910
3911void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003912input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003913{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003914 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003915 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003916}
3917
3918static void
3919offer_io_func(struct task *task, uint32_t events)
3920{
3921 struct data_offer *offer =
3922 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003923 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003924 unsigned int len;
3925 char buffer[4096];
3926
3927 len = read(offer->fd, buffer, sizeof buffer);
3928 offer->func(buffer, len,
3929 offer->x, offer->y, offer->user_data);
3930
3931 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003932 if (display->data_device_manager_version >=
3933 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3934 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003935 close(offer->fd);
3936 data_offer_destroy(offer);
3937 }
3938}
3939
3940static void
3941data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3942 data_func_t func, void *user_data)
3943{
3944 int p[2];
3945
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003946 if (pipe2(p, O_CLOEXEC) == -1)
3947 return;
3948
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003949 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3950 close(p[1]);
3951
3952 offer->io_task.run = offer_io_func;
3953 offer->fd = p[0];
3954 offer->func = func;
3955 offer->refcount++;
3956 offer->user_data = user_data;
3957
3958 display_watch_fd(offer->input->display,
3959 offer->fd, EPOLLIN, &offer->io_task);
3960}
3961
3962void
3963input_receive_drag_data(struct input *input, const char *mime_type,
3964 data_func_t func, void *data)
3965{
3966 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003967 input->drag_offer->x = input->drag_x;
3968 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003969}
3970
3971int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003972input_receive_drag_data_to_fd(struct input *input,
3973 const char *mime_type, int fd)
3974{
3975 if (input->drag_offer)
3976 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3977
3978 return 0;
3979}
3980
3981int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003982input_receive_selection_data(struct input *input, const char *mime_type,
3983 data_func_t func, void *data)
3984{
3985 char **p;
3986
3987 if (input->selection_offer == NULL)
3988 return -1;
3989
3990 for (p = input->selection_offer->types.data; *p; p++)
3991 if (strcmp(mime_type, *p) == 0)
3992 break;
3993
3994 if (*p == NULL)
3995 return -1;
3996
3997 data_offer_receive_data(input->selection_offer,
3998 mime_type, func, data);
3999 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004000}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004001
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004002int
4003input_receive_selection_data_to_fd(struct input *input,
4004 const char *mime_type, int fd)
4005{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004006 if (input->selection_offer)
4007 wl_data_offer_receive(input->selection_offer->offer,
4008 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004009
4010 return 0;
4011}
4012
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004013void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004014window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004015{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004016 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004017 return;
4018
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004019 zxdg_toplevel_v6_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004020}
4021
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004022static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004023surface_set_synchronized(struct surface *surface)
4024{
4025 if (!surface->subsurface)
4026 return;
4027
4028 if (surface->synchronized)
4029 return;
4030
4031 wl_subsurface_set_sync(surface->subsurface);
4032 surface->synchronized = 1;
4033}
4034
4035static void
4036surface_set_synchronized_default(struct surface *surface)
4037{
4038 if (!surface->subsurface)
4039 return;
4040
4041 if (surface->synchronized == surface->synchronized_default)
4042 return;
4043
4044 if (surface->synchronized_default)
4045 wl_subsurface_set_sync(surface->subsurface);
4046 else
4047 wl_subsurface_set_desync(surface->subsurface);
4048
4049 surface->synchronized = surface->synchronized_default;
4050}
4051
4052static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004053surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004054{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004055 struct widget *widget = surface->widget;
4056 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004057
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004058 if (surface->input_region) {
4059 wl_region_destroy(surface->input_region);
4060 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004061 }
4062
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004063 if (surface->opaque_region)
4064 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004065
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004066 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004067
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004068 if (widget->resize_handler)
4069 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004070 widget->allocation.width,
4071 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004072 widget->user_data);
4073
Pekka Paalanen35e82632013-04-25 13:57:48 +03004074 if (surface->subsurface &&
4075 (surface->allocation.x != widget->allocation.x ||
4076 surface->allocation.y != widget->allocation.y)) {
4077 wl_subsurface_set_position(surface->subsurface,
4078 widget->allocation.x,
4079 widget->allocation.y);
4080 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004081 if (surface->allocation.width != widget->allocation.width ||
4082 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004083 window_schedule_redraw(widget->window);
4084 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004085 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004086
4087 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004088 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004089 widget->allocation.width,
4090 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004091}
4092
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004093static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004094window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004095{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004096 struct surface *surface;
4097
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004098 widget_set_allocation(window->main_surface->widget,
4099 window->pending_allocation.x,
4100 window->pending_allocation.y,
4101 window->pending_allocation.width,
4102 window->pending_allocation.height);
4103
4104 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004105
4106 /* The main surface is in the list, too. Main surface's
4107 * resize_handler is responsible for calling widget_set_allocation()
4108 * on all sub-surface root widgets, so they will be resized
4109 * properly.
4110 */
4111 wl_list_for_each(surface, &window->subsurface_list, link) {
4112 if (surface == window->main_surface)
4113 continue;
4114
4115 surface_set_synchronized(surface);
4116 surface_resize(surface);
4117 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004118
4119 if (!window->fullscreen && !window->maximized)
4120 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004121
4122 if (window->confined && window->confined_widget) {
4123 struct wl_compositor *compositor = window->display->compositor;
4124 struct wl_region *region;
4125 struct widget *widget = window->confined_widget;
4126
4127 region = wl_compositor_create_region(compositor);
4128 wl_region_add(region,
4129 widget->allocation.x,
4130 widget->allocation.y,
4131 widget->allocation.width,
4132 widget->allocation.height);
4133 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4134 region);
4135 wl_region_destroy(region);
4136 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004137}
4138
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004139static void
4140idle_resize(struct window *window)
4141{
4142 window->resize_needed = 0;
4143 window->redraw_needed = 1;
4144
4145 DBG("from %dx%d to %dx%d\n",
4146 window->main_surface->server_allocation.width,
4147 window->main_surface->server_allocation.height,
4148 window->pending_allocation.width,
4149 window->pending_allocation.height);
4150
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004151 window_do_resize(window);
4152}
4153
4154static void
4155undo_resize(struct window *window)
4156{
4157 window->pending_allocation.width =
4158 window->main_surface->server_allocation.width;
4159 window->pending_allocation.height =
4160 window->main_surface->server_allocation.height;
4161
4162 DBG("back to %dx%d\n",
4163 window->main_surface->server_allocation.width,
4164 window->main_surface->server_allocation.height);
4165
4166 window_do_resize(window);
4167
4168 if (window->pending_allocation.width == 0 &&
4169 window->pending_allocation.height == 0) {
4170 fprintf(stderr, "Error: Could not draw a surface, "
4171 "most likely due to insufficient disk space in "
4172 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4173 exit(EXIT_FAILURE);
4174 }
4175}
4176
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004177void
4178window_schedule_resize(struct window *window, int width, int height)
4179{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004180 /* We should probably get these numbers from the theme. */
4181 const int min_width = 200, min_height = 200;
4182
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004183 window->pending_allocation.x = 0;
4184 window->pending_allocation.y = 0;
4185 window->pending_allocation.width = width;
4186 window->pending_allocation.height = height;
4187
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004188 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004189 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004190 window->min_allocation.width = min_width;
4191 else
4192 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004193 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004194 window->min_allocation.height = min_height;
4195 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004196 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004197 }
4198
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004199 if (window->pending_allocation.width < window->min_allocation.width)
4200 window->pending_allocation.width = window->min_allocation.width;
4201 if (window->pending_allocation.height < window->min_allocation.height)
4202 window->pending_allocation.height = window->min_allocation.height;
4203
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004204 window->resize_needed = 1;
4205 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004206}
4207
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004208void
4209widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4210{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004211 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004212}
4213
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004214static int
4215window_get_shadow_margin(struct window *window)
4216{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004217 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004218 return frame_get_shadow_margin(window->frame->frame);
4219 else
4220 return 0;
4221}
4222
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004223void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004224window_inhibit_redraw(struct window *window)
4225{
4226 window->redraw_inhibited = 1;
4227 wl_list_remove(&window->redraw_task.link);
4228 wl_list_init(&window->redraw_task.link);
4229 window->redraw_task_scheduled = 0;
4230}
4231
Louis-Francis Ratté-Boulianneb79dead2017-11-29 16:38:44 -05004232void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004233window_uninhibit_redraw(struct window *window)
4234{
4235 window->redraw_inhibited = 0;
4236 if (window->redraw_needed || window->resize_needed)
4237 window_schedule_redraw_task(window);
4238}
4239
4240static void
4241xdg_surface_handle_configure(void *data,
4242 struct zxdg_surface_v6 *zxdg_surface_v6,
4243 uint32_t serial)
4244{
4245 struct window *window = data;
4246
4247 zxdg_surface_v6_ack_configure(window->xdg_surface, serial);
4248
4249 if (window->state_changed_handler)
4250 window->state_changed_handler(window, window->user_data);
4251
4252 window_uninhibit_redraw(window);
4253}
4254
4255static const struct zxdg_surface_v6_listener xdg_surface_listener = {
4256 xdg_surface_handle_configure
4257};
4258
4259static void
4260xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
4261 int32_t width, int32_t height,
4262 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004263{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004264 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004265 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004266
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004267 window->maximized = 0;
4268 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004269 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004270 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004271
4272 wl_array_for_each(p, states) {
4273 uint32_t state = *p;
4274 switch (state) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004275 case ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004276 window->maximized = 1;
4277 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004278 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004279 window->fullscreen = 1;
4280 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004281 case ZXDG_TOPLEVEL_V6_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004282 window->resizing = 1;
4283 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004284 case ZXDG_TOPLEVEL_V6_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004285 window->focused = 1;
4286 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004287 default:
4288 /* Unknown state */
4289 break;
4290 }
4291 }
4292
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004293 if (window->frame) {
4294 if (window->maximized) {
4295 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4296 } else {
4297 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4298 }
4299
4300 if (window->focused) {
4301 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4302 } else {
4303 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4304 }
4305 }
4306
Jasper St. Pierref184c382014-05-06 08:33:27 -04004307 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004308 /* The width / height params are for window geometry,
4309 * but window_schedule_resize takes allocation. Add
4310 * on the shadow margin to get the difference. */
4311 int margin = window_get_shadow_margin(window);
4312
4313 window_schedule_resize(window,
4314 width + margin * 2,
4315 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004316 } else if (window->saved_allocation.width > 0 &&
4317 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004318 window_schedule_resize(window,
4319 window->saved_allocation.width,
4320 window->saved_allocation.height);
4321 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004322}
4323
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004324static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004325xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004326{
4327 struct window *window = data;
4328 window_close(window);
4329}
4330
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004331static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
4332 xdg_toplevel_handle_configure,
4333 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004334};
4335
4336static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004337window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004338{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004339 struct zxdg_toplevel_v6 *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004340
4341 if (!window->xdg_surface)
4342 return;
4343
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004344 if (window->parent == window->last_parent)
4345 return;
4346
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004347 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004348 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004349 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004350 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004351
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004352 zxdg_toplevel_v6_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004353 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004354}
4355
4356static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004357window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004358{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004359 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004360 frame_input_rect(window->frame->frame,
4361 &geometry->x,
4362 &geometry->y,
4363 &geometry->width,
4364 &geometry->height);
4365 else
4366 window_get_allocation(window, geometry);
4367}
4368
4369static void
4370window_sync_geometry(struct window *window)
4371{
4372 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004373
4374 if (!window->xdg_surface)
4375 return;
4376
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004377 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004378 if (geometry.x == window->last_geometry.x &&
4379 geometry.y == window->last_geometry.y &&
4380 geometry.width == window->last_geometry.width &&
4381 geometry.height == window->last_geometry.height)
4382 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004383
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004384 zxdg_surface_v6_set_window_geometry(window->xdg_surface,
4385 geometry.x,
4386 geometry.y,
4387 geometry.width,
4388 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004389 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004390}
4391
4392static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004393window_flush(struct window *window)
4394{
4395 struct surface *surface;
4396
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004397 assert(!window->redraw_inhibited);
4398
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004399 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004400 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004401 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004402 if (window->xdg_toplevel)
4403 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004404 }
4405
4406 wl_list_for_each(surface, &window->subsurface_list, link) {
4407 if (surface == window->main_surface)
4408 continue;
4409
4410 surface_flush(surface);
4411 }
4412
4413 surface_flush(window->main_surface);
4414}
4415
4416static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004417menu_destroy(struct menu *menu)
4418{
4419 widget_destroy(menu->widget);
4420 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004421 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004422 free(menu);
4423}
4424
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004425void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004426window_get_allocation(struct window *window,
4427 struct rectangle *allocation)
4428{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004429 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004430}
4431
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004432static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004433widget_redraw(struct widget *widget)
4434{
4435 struct widget *child;
4436
4437 if (widget->redraw_handler)
4438 widget->redraw_handler(widget, widget->user_data);
4439 wl_list_for_each(child, &widget->child_list, link)
4440 widget_redraw(child);
4441}
4442
4443static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004444frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4445{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004446 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004447
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004448 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004449 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004450 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004451 surface->frame_cb = NULL;
4452
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004453 surface->last_time = time;
4454
Pekka Paalanen71233882013-04-25 13:57:53 +03004455 if (surface->redraw_needed || surface->window->redraw_needed) {
4456 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004457 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004458 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004459}
4460
4461static const struct wl_callback_listener listener = {
4462 frame_callback
4463};
4464
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004465static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004466surface_redraw(struct surface *surface)
4467{
Pekka Paalanen71233882013-04-25 13:57:53 +03004468 DBG_OBJ(surface->surface, "begin\n");
4469
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004470 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004471 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004472
4473 /* Whole-window redraw forces a redraw even if the previous has
4474 * not yet hit the screen.
4475 */
4476 if (surface->frame_cb) {
4477 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004478 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004479
Pekka Paalanen71233882013-04-25 13:57:53 +03004480 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004481 wl_callback_destroy(surface->frame_cb);
4482 }
4483
Neil Roberts97b747c2013-12-19 16:17:12 +00004484 if (surface->widget->use_cairo &&
4485 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004486 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004487 return -1;
4488 }
4489
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004490 surface->frame_cb = wl_surface_frame(surface->surface);
4491 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004492 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004493
4494 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004495 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004496 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004497 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004498 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004499}
4500
4501static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004502idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004503{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004504 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004505 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004506 int failed = 0;
4507 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004508
Pekka Paalanen71233882013-04-25 13:57:53 +03004509 DBG(" --------- \n");
4510
Pekka Paalaneneebff542013-04-25 13:57:52 +03004511 wl_list_init(&window->redraw_task.link);
4512 window->redraw_task_scheduled = 0;
4513
4514 if (window->resize_needed) {
4515 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004516 if (window->main_surface->frame_cb) {
4517 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004518 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004519 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004520
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004521 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004522 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004523 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004524
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004525 if (surface_redraw(window->main_surface) < 0) {
4526 /*
4527 * Only main_surface failure will cause us to undo the resize.
4528 * If sub-surfaces fail, they will just be broken with old
4529 * content.
4530 */
4531 failed = 1;
4532 } else {
4533 wl_list_for_each(surface, &window->subsurface_list, link) {
4534 if (surface == window->main_surface)
4535 continue;
4536
4537 surface_redraw(surface);
4538 }
4539 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004540
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004541 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004542 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004543
4544 wl_list_for_each(surface, &window->subsurface_list, link)
4545 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004546
4547 if (resized && failed) {
4548 /* Restore widget tree to correspond to what is on screen. */
4549 undo_resize(window);
4550 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004551}
4552
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004553static void
4554window_schedule_redraw_task(struct window *window)
4555{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004556 if (window->redraw_inhibited)
4557 return;
4558
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004559 if (!window->redraw_task_scheduled) {
4560 window->redraw_task.run = idle_redraw;
4561 display_defer(window->display, &window->redraw_task);
4562 window->redraw_task_scheduled = 1;
4563 }
4564}
4565
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004566void
4567window_schedule_redraw(struct window *window)
4568{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004569 struct surface *surface;
4570
Pekka Paalanen71233882013-04-25 13:57:53 +03004571 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4572
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004573 wl_list_for_each(surface, &window->subsurface_list, link)
4574 surface->redraw_needed = 1;
4575
4576 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004577}
4578
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004579int
4580window_is_fullscreen(struct window *window)
4581{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004582 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004583}
4584
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004585void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004586window_set_fullscreen(struct window *window, int fullscreen)
4587{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004588 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004589 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004590
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004591 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004592 return;
4593
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004594 if (fullscreen)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004595 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004596 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004597 zxdg_toplevel_v6_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004598}
4599
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004600int
4601window_is_maximized(struct window *window)
4602{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004603 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004604}
4605
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004606void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004607window_set_maximized(struct window *window, int maximized)
4608{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004609 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004610 return;
4611
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004612 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004613 return;
4614
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004615 if (maximized)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004616 zxdg_toplevel_v6_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004617 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004618 zxdg_toplevel_v6_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004619}
4620
Jasper St. Pierrede680992014-04-10 17:23:49 -07004621int
4622window_is_resizing(struct window *window)
4623{
4624 return window->resizing;
4625}
4626
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004627void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004628window_set_minimized(struct window *window)
4629{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004630 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004631 return;
4632
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004633 zxdg_toplevel_v6_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004634}
4635
4636void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004637window_set_user_data(struct window *window, void *data)
4638{
4639 window->user_data = data;
4640}
4641
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004642void *
4643window_get_user_data(struct window *window)
4644{
4645 return window->user_data;
4646}
4647
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004648void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004649window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004650 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004651{
4652 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004653}
4654
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004655void
4656window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004657 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004658{
4659 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004660}
4661
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004662void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004663window_set_data_handler(struct window *window, window_data_handler_t handler)
4664{
4665 window->data_handler = handler;
4666}
4667
4668void
4669window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4670{
4671 window->drop_handler = handler;
4672}
4673
4674void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004675window_set_close_handler(struct window *window,
4676 window_close_handler_t handler)
4677{
4678 window->close_handler = handler;
4679}
4680
4681void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004682window_set_fullscreen_handler(struct window *window,
4683 window_fullscreen_handler_t handler)
4684{
4685 window->fullscreen_handler = handler;
4686}
4687
4688void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004689window_set_output_handler(struct window *window,
4690 window_output_handler_t handler)
4691{
4692 window->output_handler = handler;
4693}
4694
4695void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004696window_set_state_changed_handler(struct window *window,
4697 window_state_changed_handler_t handler)
4698{
4699 window->state_changed_handler = handler;
4700}
4701
4702void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004703window_set_pointer_locked_handler(struct window *window,
4704 locked_pointer_locked_handler_t locked,
4705 locked_pointer_unlocked_handler_t unlocked)
4706{
4707 window->pointer_unlocked_handler = unlocked;
4708 window->pointer_locked_handler = locked;
4709}
4710
4711void
4712window_set_pointer_confined_handler(struct window *window,
4713 confined_pointer_confined_handler_t confined,
4714 confined_pointer_unconfined_handler_t unconfined)
4715{
4716 window->pointer_confined_handler = confined;
4717 window->pointer_unconfined_handler = unconfined;
4718}
4719
4720void
4721window_set_locked_pointer_motion_handler(struct window *window,
4722 window_locked_pointer_motion_handler_t handler)
4723{
4724 window->locked_pointer_motion_handler = handler;
4725}
4726
4727void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004728window_set_title(struct window *window, const char *title)
4729{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004730 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004731 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004732 if (window->frame) {
4733 frame_set_title(window->frame->frame, title);
4734 widget_schedule_redraw(window->frame->widget);
4735 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004736 if (window->xdg_toplevel)
4737 zxdg_toplevel_v6_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004738}
4739
4740const char *
4741window_get_title(struct window *window)
4742{
4743 return window->title;
4744}
4745
4746void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004747window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4748{
4749 struct text_cursor_position *text_cursor_position =
4750 window->display->text_cursor_position;
4751
Scott Moreau9295ce02012-06-01 12:46:10 -06004752 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004753 return;
4754
4755 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004756 window->main_surface->surface,
4757 wl_fixed_from_int(x),
4758 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004759}
4760
Casey Dahlin9074db52012-04-19 22:50:09 -04004761static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004762relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4763 uint32_t utime_hi,
4764 uint32_t utime_lo,
4765 wl_fixed_t dx,
4766 wl_fixed_t dy,
4767 wl_fixed_t dx_unaccel,
4768 wl_fixed_t dy_unaccel)
4769{
4770 struct input *input = data;
4771 struct window *window = input->pointer_focus;
4772 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4773
4774 if (window->locked_pointer_motion_handler &&
4775 window->pointer_locked) {
4776 window->locked_pointer_motion_handler(
4777 window, input, ms,
4778 wl_fixed_to_double(dx),
4779 wl_fixed_to_double(dy),
4780 window->user_data);
4781 }
4782}
4783
4784static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4785 relative_pointer_handle_motion,
4786};
4787
4788static void
4789locked_pointer_locked(void *data,
4790 struct zwp_locked_pointer_v1 *locked_pointer)
4791{
Dima Ryazanove0dc5d42018-05-10 00:53:38 -07004792 struct window *window = data;
4793 struct input *input = window->locked_input;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004794
4795 window->pointer_locked = true;
4796
4797 if (window->pointer_locked_handler) {
4798 window->pointer_locked_handler(window,
4799 input,
4800 window->user_data);
4801 }
4802}
4803
4804static void
4805locked_pointer_unlocked(void *data,
4806 struct zwp_locked_pointer_v1 *locked_pointer)
4807{
Dima Ryazanove0dc5d42018-05-10 00:53:38 -07004808 struct window *window = data;
4809 struct input *input = window->locked_input;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004810
4811 window_unlock_pointer(window);
4812
4813 if (window->pointer_unlocked_handler) {
4814 window->pointer_unlocked_handler(window,
4815 input,
4816 window->user_data);
4817 }
4818}
4819
4820static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4821 locked_pointer_locked,
4822 locked_pointer_unlocked,
4823};
4824
4825int
4826window_lock_pointer(struct window *window, struct input *input)
4827{
4828 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4829 window->display->relative_pointer_manager;
4830 struct zwp_pointer_constraints_v1 *pointer_constraints =
4831 window->display->pointer_constraints;
4832 struct zwp_relative_pointer_v1 *relative_pointer;
4833 struct zwp_locked_pointer_v1 *locked_pointer;
4834
4835 if (!window->display->relative_pointer_manager)
4836 return -1;
4837
4838 if (!window->display->pointer_constraints)
4839 return -1;
4840
4841 if (window->locked_pointer)
4842 return -1;
4843
4844 if (window->confined_pointer)
4845 return -1;
4846
4847 if (!input->pointer)
4848 return -1;
4849
4850 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4851 relative_pointer_manager, input->pointer);
4852 zwp_relative_pointer_v1_add_listener(relative_pointer,
4853 &relative_pointer_listener,
4854 input);
4855
4856 locked_pointer =
4857 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4858 window->main_surface->surface,
4859 input->pointer,
4860 NULL,
4861 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4862 zwp_locked_pointer_v1_add_listener(locked_pointer,
4863 &locked_pointer_listener,
Dima Ryazanove0dc5d42018-05-10 00:53:38 -07004864 window);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004865
4866 window->locked_input = input;
4867 window->locked_pointer = locked_pointer;
4868 window->relative_pointer = relative_pointer;
4869
4870 return 0;
4871}
4872
4873void
4874window_unlock_pointer(struct window *window)
4875{
4876 if (!window->locked_pointer)
4877 return;
4878
4879 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4880 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4881 window->locked_pointer = NULL;
4882 window->relative_pointer = NULL;
4883 window->pointer_locked = false;
4884 window->locked_input = NULL;
4885}
4886
4887void
4888widget_set_locked_pointer_cursor_hint(struct widget *widget,
4889 float x, float y)
4890{
4891 struct window *window = widget->window;
4892
4893 if (!window->locked_pointer)
4894 return;
4895
4896 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4897 wl_fixed_from_double(x),
4898 wl_fixed_from_double(y));
4899 wl_surface_commit(window->main_surface->surface);
4900}
4901
4902static void
4903confined_pointer_confined(void *data,
4904 struct zwp_confined_pointer_v1 *confined_pointer)
4905{
Dima Ryazanove0dc5d42018-05-10 00:53:38 -07004906 struct window *window = data;
4907 struct input *input = window->confined_input;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004908
4909 window->confined = true;
4910
4911 if (window->pointer_confined_handler) {
4912 window->pointer_confined_handler(window,
4913 input,
4914 window->user_data);
4915 }
4916}
4917
4918static void
4919confined_pointer_unconfined(void *data,
4920 struct zwp_confined_pointer_v1 *confined_pointer)
4921{
Dima Ryazanove0dc5d42018-05-10 00:53:38 -07004922 struct window *window = data;
4923 struct input *input = window->confined_input;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004924
4925 window_unconfine_pointer(window);
4926
4927 window->confined = false;
4928
4929 if (window->pointer_unconfined_handler) {
4930 window->pointer_unconfined_handler(window,
4931 input,
4932 window->user_data);
4933 }
4934}
4935
4936static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4937 confined_pointer_confined,
4938 confined_pointer_unconfined,
4939};
4940
4941int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004942window_confine_pointer_to_rectangles(struct window *window,
4943 struct input *input,
4944 struct rectangle *rectangles,
4945 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004946{
4947 struct zwp_pointer_constraints_v1 *pointer_constraints =
4948 window->display->pointer_constraints;
4949 struct zwp_confined_pointer_v1 *confined_pointer;
4950 struct wl_compositor *compositor = window->display->compositor;
4951 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004952 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004953
4954 if (!window->display->pointer_constraints)
4955 return -1;
4956
4957 if (window->locked_pointer)
4958 return -1;
4959
4960 if (window->confined_pointer)
4961 return -1;
4962
4963 if (!input->pointer)
4964 return -1;
4965
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004966 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004967 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004968 for (i = 0; i < num_rectangles; i++) {
4969 wl_region_add(region,
4970 rectangles[i].x,
4971 rectangles[i].y,
4972 rectangles[i].width,
4973 rectangles[i].height);
4974 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004975 }
4976
4977 confined_pointer =
4978 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
4979 window->main_surface->surface,
4980 input->pointer,
4981 region,
4982 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4983 if (region)
4984 wl_region_destroy(region);
4985
4986 zwp_confined_pointer_v1_add_listener(confined_pointer,
4987 &confined_pointer_listener,
Dima Ryazanove0dc5d42018-05-10 00:53:38 -07004988 window);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004989
Dima Ryazanove0dc5d42018-05-10 00:53:38 -07004990 window->confined_input = input;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004991 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004992 window->confined_widget = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004993
4994 return 0;
4995}
4996
4997void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004998window_update_confine_rectangles(struct window *window,
4999 struct rectangle *rectangles,
5000 int num_rectangles)
5001{
5002 struct wl_compositor *compositor = window->display->compositor;
5003 struct wl_region *region;
5004 int i;
5005
5006 region = wl_compositor_create_region(compositor);
5007 for (i = 0; i < num_rectangles; i++) {
5008 wl_region_add(region,
5009 rectangles[i].x,
5010 rectangles[i].y,
5011 rectangles[i].width,
5012 rectangles[i].height);
5013 }
5014
5015 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5016
5017 wl_region_destroy(region);
5018}
5019
5020int
5021window_confine_pointer_to_widget(struct window *window,
5022 struct widget *widget,
5023 struct input *input)
5024{
5025 int ret;
5026
5027 if (widget) {
5028 ret = window_confine_pointer_to_rectangles(window,
5029 input,
5030 &widget->allocation,
5031 1);
5032 window->confined_widget = widget;
5033 return ret;
5034 } else {
5035 return window_confine_pointer_to_rectangles(window,
5036 input,
5037 NULL,
5038 0);
5039 }
5040}
5041
5042void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005043window_unconfine_pointer(struct window *window)
5044{
5045 if (!window->confined_pointer)
5046 return;
5047
5048 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5049 window->confined_pointer = NULL;
5050 window->confined = false;
Dima Ryazanove0dc5d42018-05-10 00:53:38 -07005051 window->confined_input = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005052}
5053
5054static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005055surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005056 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005057{
Rob Bradford7507b572012-05-15 17:55:34 +01005058 struct window *window = data;
5059 struct output *output;
5060 struct output *output_found = NULL;
5061 struct window_output *window_output;
5062
5063 wl_list_for_each(output, &window->display->output_list, link) {
5064 if (output->output == wl_output) {
5065 output_found = output;
5066 break;
5067 }
5068 }
5069
5070 if (!output_found)
5071 return;
5072
Brian Lovinbc919262013-08-07 15:34:59 -07005073 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005074 window_output->output = output_found;
5075
5076 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005077
5078 if (window->output_handler)
5079 window->output_handler(window, output_found, 1,
5080 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005081}
5082
5083static void
5084surface_leave(void *data,
5085 struct wl_surface *wl_surface, struct wl_output *output)
5086{
Rob Bradford7507b572012-05-15 17:55:34 +01005087 struct window *window = data;
5088 struct window_output *window_output;
5089 struct window_output *window_output_found = NULL;
5090
5091 wl_list_for_each(window_output, &window->window_output_list, link) {
5092 if (window_output->output->output == output) {
5093 window_output_found = window_output;
5094 break;
5095 }
5096 }
5097
5098 if (window_output_found) {
5099 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005100
5101 if (window->output_handler)
5102 window->output_handler(window, window_output->output,
5103 0, window->user_data);
5104
Rob Bradford7507b572012-05-15 17:55:34 +01005105 free(window_output_found);
5106 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005107}
5108
5109static const struct wl_surface_listener surface_listener = {
5110 surface_enter,
5111 surface_leave
5112};
5113
Pekka Paalanen4e373742013-02-13 16:17:13 +02005114static struct surface *
5115surface_create(struct window *window)
5116{
5117 struct display *display = window->display;
5118 struct surface *surface;
5119
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005120 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005121 surface->window = window;
5122 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005123 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005124 wl_surface_add_listener(surface->surface, &surface_listener, window);
5125
Pekka Paalanen35e82632013-04-25 13:57:48 +03005126 wl_list_insert(&window->subsurface_list, &surface->link);
5127
Pekka Paalanen4e373742013-02-13 16:17:13 +02005128 return surface;
5129}
5130
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005131static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005132get_preferred_buffer_type(struct display *display)
5133{
5134#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005135 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005136 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5137#endif
5138
5139 return WINDOW_BUFFER_TYPE_SHM;
5140}
5141
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005142static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005143window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005144{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005145 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005146 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005147
Peter Huttererf3d62272013-08-08 11:57:05 +10005148 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005149 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005150 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005151
5152 surface = surface_create(window);
5153 window->main_surface = surface;
5154
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005155 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005156
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005157 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005158 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005159
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005160 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005161
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005162 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005163 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005164 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005165
Rob Bradford7507b572012-05-15 17:55:34 +01005166 wl_list_init (&window->window_output_list);
5167
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005168 return window;
5169}
5170
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005171struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005172window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005173{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005174 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005175 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005176
5177 window = window_create_internal(display, 0);
5178
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005179 if (window->display->xdg_shell) {
5180 window->xdg_surface =
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005181 zxdg_shell_v6_get_xdg_surface(window->display->xdg_shell,
5182 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005183 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005184
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005185 zxdg_surface_v6_add_listener(window->xdg_surface,
5186 &xdg_surface_listener, window);
5187
5188 window->xdg_toplevel =
5189 zxdg_surface_v6_get_toplevel(window->xdg_surface);
5190 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5191
5192 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
5193 &xdg_toplevel_listener, window);
5194
5195 window_inhibit_redraw(window);
5196
5197 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005198 } else if (display->ivi_application) {
5199 /* auto generation of ivi_id based on process id + basement of id */
5200 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5201 window->ivi_surface =
5202 ivi_application_surface_create(display->ivi_application,
5203 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005204 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005205
5206 ivi_surface_add_listener(window->ivi_surface,
5207 &ivi_surface_listener, window);
5208 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005209
5210 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005211}
5212
5213struct window *
5214window_create_custom(struct display *display)
5215{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005216 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005217}
5218
Jasper St. Pierre53686042013-12-09 15:26:25 -05005219void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005220window_set_parent(struct window *window,
5221 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005222{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005223 window->parent = parent_window;
5224 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005225}
5226
5227struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005228window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005229{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005230 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005231}
5232
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005233static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005234menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005235{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005236 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005237 int next;
5238
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005239 frame_interior(menu->frame, &x, &y, &width, &height);
5240 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005241 if (menu->current != next) {
5242 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005243 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005244 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005245}
5246
5247static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005248menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005249 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005250 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005251{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005252 struct menu *menu = data;
5253
5254 if (widget == menu->widget)
5255 menu_set_item(data, y);
5256
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005257 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005258}
5259
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005260static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005261menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005262 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005263{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005264 struct menu *menu = data;
5265
5266 if (widget == menu->widget)
5267 menu_set_item(data, y);
5268
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005269 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005270}
5271
5272static void
5273menu_leave_handler(struct widget *widget, struct input *input, void *data)
5274{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005275 struct menu *menu = data;
5276
5277 if (widget == menu->widget)
5278 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005279}
5280
5281static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005282menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005283 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005284 uint32_t button, enum wl_pointer_button_state state,
5285 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005286
5287{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005288 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005289
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005290 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5291 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005292 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005293 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005294 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005295 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005296 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005297 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005298 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005299 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005300}
5301
5302static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005303menu_touch_up_handler(struct widget *widget,
5304 struct input *input,
5305 uint32_t serial,
5306 uint32_t time,
5307 int32_t id,
5308 void *data)
5309{
5310 struct menu *menu = data;
5311
5312 input_ungrab(input);
5313 menu_destroy(menu);
5314}
5315
5316static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005317menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005318{
5319 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005320 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005321 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005322
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005323 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005324
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005325 frame_repaint(menu->frame, cr);
5326 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005327
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005328 theme_set_background_source(menu->window->display->theme,
5329 cr, THEME_FRAME_ACTIVE);
5330 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005331 cairo_fill(cr);
5332
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005333 cairo_select_font_face(cr, "sans",
5334 CAIRO_FONT_SLANT_NORMAL,
5335 CAIRO_FONT_WEIGHT_NORMAL);
5336 cairo_set_font_size(cr, 12);
5337
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005338 for (i = 0; i < menu->count; i++) {
5339 if (i == menu->current) {
5340 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005341 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005342 cairo_fill(cr);
5343 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005344 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005345 cairo_show_text(cr, menu->entries[i]);
5346 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005347 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5348 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005349 cairo_show_text(cr, menu->entries[i]);
5350 }
5351 }
5352
5353 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005354}
5355
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005356static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005357xdg_popup_handle_configure(void *data,
5358 struct zxdg_popup_v6 *zxdg_popup_v6,
5359 int32_t x,
5360 int32_t y,
5361 int32_t width,
5362 int32_t height)
5363{
5364}
5365
5366static void
5367xdg_popup_handle_popup_done(void *data, struct zxdg_popup_v6 *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005368{
5369 struct window *window = data;
5370 struct menu *menu = window->main_surface->widget->user_data;
5371
5372 input_ungrab(menu->input);
5373 menu_destroy(menu);
5374}
5375
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005376static const struct zxdg_popup_v6_listener xdg_popup_listener = {
5377 xdg_popup_handle_configure,
5378 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005379};
5380
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005381static struct menu *
5382create_menu(struct display *display,
5383 struct input *input, uint32_t time,
5384 menu_func_t func, const char **entries, int count,
5385 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005386{
5387 struct window *window;
5388 struct menu *menu;
5389
5390 menu = malloc(sizeof *menu);
5391 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005392 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005393
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005394 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005395 if (!window) {
5396 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005397 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005398 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005399
5400 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005401 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005402 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005403 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005404 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005405 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005406 menu->entries = entries;
5407 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005408 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005409 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005410 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005411 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005412 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005413
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005414 input_ungrab(input);
5415
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005416 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005417 widget_set_enter_handler(menu->widget, menu_enter_handler);
5418 widget_set_leave_handler(menu->widget, menu_leave_handler);
5419 widget_set_motion_handler(menu->widget, menu_motion_handler);
5420 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005421 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005422
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005423 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005424 frame_resize_inside(menu->frame, 200, count * 20);
5425 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5426 window_schedule_resize(window, frame_width(menu->frame),
5427 frame_height(menu->frame));
5428
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005429 return menu;
5430}
5431
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005432static struct zxdg_positioner_v6 *
5433create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005434 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005435{
5436 struct zxdg_positioner_v6 *positioner;
5437
5438 positioner = zxdg_shell_v6_create_positioner(display->xdg_shell);
5439 fail_on_null(positioner, 0, __FILE__, __LINE__);
5440 zxdg_positioner_v6_set_anchor_rect(positioner, x, y, 1, 1);
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005441 zxdg_positioner_v6_set_size(positioner, w, h);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005442 zxdg_positioner_v6_set_anchor(positioner,
5443 ZXDG_POSITIONER_V6_ANCHOR_TOP |
5444 ZXDG_POSITIONER_V6_ANCHOR_LEFT);
5445 zxdg_positioner_v6_set_gravity(positioner,
5446 ZXDG_POSITIONER_V6_ANCHOR_BOTTOM |
5447 ZXDG_POSITIONER_V6_ANCHOR_RIGHT);
5448
5449 return positioner;
5450}
5451
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005452void
5453window_show_menu(struct display *display,
5454 struct input *input, uint32_t time, struct window *parent,
5455 int32_t x, int32_t y,
5456 menu_func_t func, const char **entries, int count)
5457{
5458 struct menu *menu;
5459 struct window *window;
5460 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005461 struct rectangle parent_geometry;
5462 struct zxdg_positioner_v6 *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005463
5464 menu = create_menu(display, input, time, func, entries, count, parent);
5465
5466 if (menu == NULL)
5467 return;
5468
5469 window = menu->window;
5470
5471 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5472 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5473
5474 window->x = x;
5475 window->y = y;
5476
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005477 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005478 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005479
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005480 if (!display->xdg_shell)
5481 return;
5482
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005483 window->xdg_surface =
5484 zxdg_shell_v6_get_xdg_surface(display->xdg_shell,
5485 window->main_surface->surface);
5486 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005487
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005488 zxdg_surface_v6_add_listener(window->xdg_surface,
5489 &xdg_surface_listener, window);
5490
5491 positioner = create_simple_positioner(display,
5492 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005493 window->y - (iy + parent_geometry.y),
5494 frame_width(menu->frame),
5495 frame_height(menu->frame));
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005496 window->xdg_popup =
5497 zxdg_surface_v6_get_popup(window->xdg_surface,
5498 parent->xdg_surface,
5499 positioner);
5500 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
5501 zxdg_positioner_v6_destroy(positioner);
5502 zxdg_popup_v6_grab(window->xdg_popup,
5503 input->seat,
5504 display_get_serial(window->display));
5505 zxdg_popup_v6_add_listener(window->xdg_popup,
5506 &xdg_popup_listener, window);
5507
5508 window_inhibit_redraw(window);
5509
5510 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005511}
5512
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005513void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005514window_set_buffer_type(struct window *window, enum window_buffer_type type)
5515{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005516 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005517}
5518
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005519enum window_buffer_type
5520window_get_buffer_type(struct window *window)
5521{
5522 return window->main_surface->buffer_type;
5523}
5524
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005525void
5526window_set_preferred_format(struct window *window,
5527 enum preferred_format format)
5528{
5529 window->preferred_format = format;
5530}
5531
Pekka Paalanen35e82632013-04-25 13:57:48 +03005532struct widget *
5533window_add_subsurface(struct window *window, void *data,
5534 enum subsurface_mode default_mode)
5535{
5536 struct widget *widget;
5537 struct surface *surface;
5538 struct wl_surface *parent;
5539 struct wl_subcompositor *subcompo = window->display->subcompositor;
5540
Pekka Paalanen35e82632013-04-25 13:57:48 +03005541 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005542 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005543 widget = widget_create(window, surface, data);
5544 wl_list_init(&widget->link);
5545 surface->widget = widget;
5546
5547 parent = window->main_surface->surface;
5548 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5549 surface->surface,
5550 parent);
5551 surface->synchronized = 1;
5552
5553 switch (default_mode) {
5554 case SUBSURFACE_SYNCHRONIZED:
5555 surface->synchronized_default = 1;
5556 break;
5557 case SUBSURFACE_DESYNCHRONIZED:
5558 surface->synchronized_default = 0;
5559 break;
5560 default:
5561 assert(!"bad enum subsurface_mode");
5562 }
5563
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005564 window->resize_needed = 1;
5565 window_schedule_redraw(window);
5566
Pekka Paalanen35e82632013-04-25 13:57:48 +03005567 return widget;
5568}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005569
5570static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005571display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005572 struct wl_output *wl_output,
5573 int x, int y,
5574 int physical_width,
5575 int physical_height,
5576 int subpixel,
5577 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005578 const char *model,
5579 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005580{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005581 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005582
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005583 output->allocation.x = x;
5584 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005585 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005586
5587 if (output->make)
5588 free(output->make);
5589 output->make = strdup(make);
5590
5591 if (output->model)
5592 free(output->model);
5593 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005594}
5595
5596static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005597display_handle_done(void *data,
5598 struct wl_output *wl_output)
5599{
5600}
5601
5602static void
5603display_handle_scale(void *data,
5604 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005605 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005606{
5607 struct output *output = data;
5608
5609 output->scale = scale;
5610}
5611
5612static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005613display_handle_mode(void *data,
5614 struct wl_output *wl_output,
5615 uint32_t flags,
5616 int width,
5617 int height,
5618 int refresh)
5619{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005620 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005621 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005622
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005623 if (flags & WL_OUTPUT_MODE_CURRENT) {
5624 output->allocation.width = width;
5625 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005626 if (display->output_configure_handler)
5627 (*display->output_configure_handler)(
5628 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005629 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005630}
5631
5632static const struct wl_output_listener output_listener = {
5633 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005634 display_handle_mode,
5635 display_handle_done,
5636 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005637};
5638
5639static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005640display_add_output(struct display *d, uint32_t id)
5641{
5642 struct output *output;
5643
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005644 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005645 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005646 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005647 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005648 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005649 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005650 wl_list_insert(d->output_list.prev, &output->link);
5651
5652 wl_output_add_listener(output->output, &output_listener, output);
5653}
5654
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005655static void
5656output_destroy(struct output *output)
5657{
5658 if (output->destroy_handler)
5659 (*output->destroy_handler)(output, output->user_data);
5660
5661 wl_output_destroy(output->output);
5662 wl_list_remove(&output->link);
5663 free(output);
5664}
5665
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005666static void
5667display_destroy_output(struct display *d, uint32_t id)
5668{
5669 struct output *output;
5670
5671 wl_list_for_each(output, &d->output_list, link) {
5672 if (output->server_output_id == id) {
5673 output_destroy(output);
5674 break;
5675 }
5676 }
5677}
5678
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005679void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005680display_set_global_handler(struct display *display,
5681 display_global_handler_t handler)
5682{
5683 struct global *global;
5684
5685 display->global_handler = handler;
5686 if (!handler)
5687 return;
5688
5689 wl_list_for_each(global, &display->global_list, link)
5690 display->global_handler(display,
5691 global->name, global->interface,
5692 global->version, display->user_data);
5693}
5694
5695void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005696display_set_global_handler_remove(struct display *display,
5697 display_global_handler_t remove_handler)
5698{
5699 display->global_handler_remove = remove_handler;
5700 if (!remove_handler)
5701 return;
5702}
5703
5704void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005705display_set_output_configure_handler(struct display *display,
5706 display_output_handler_t handler)
5707{
5708 struct output *output;
5709
5710 display->output_configure_handler = handler;
5711 if (!handler)
5712 return;
5713
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005714 wl_list_for_each(output, &display->output_list, link) {
5715 if (output->allocation.width == 0 &&
5716 output->allocation.height == 0)
5717 continue;
5718
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005719 (*display->output_configure_handler)(output,
5720 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005721 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005722}
5723
5724void
5725output_set_user_data(struct output *output, void *data)
5726{
5727 output->user_data = data;
5728}
5729
5730void *
5731output_get_user_data(struct output *output)
5732{
5733 return output->user_data;
5734}
5735
5736void
5737output_set_destroy_handler(struct output *output,
5738 display_output_handler_t handler)
5739{
5740 output->destroy_handler = handler;
5741 /* FIXME: implement this, once we have way to remove outputs */
5742}
5743
5744void
Scott Moreau4e072362012-09-29 02:03:11 -06005745output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005746{
Scott Moreau4e072362012-09-29 02:03:11 -06005747 struct rectangle allocation = output->allocation;
5748
5749 switch (output->transform) {
5750 case WL_OUTPUT_TRANSFORM_90:
5751 case WL_OUTPUT_TRANSFORM_270:
5752 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5753 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5754 /* Swap width and height */
5755 allocation.width = output->allocation.height;
5756 allocation.height = output->allocation.width;
5757 break;
5758 }
5759
5760 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005761}
5762
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005763struct wl_output *
5764output_get_wl_output(struct output *output)
5765{
5766 return output->output;
5767}
5768
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005769enum wl_output_transform
5770output_get_transform(struct output *output)
5771{
5772 return output->transform;
5773}
5774
Alexander Larssonafd319a2013-05-22 14:41:27 +02005775uint32_t
5776output_get_scale(struct output *output)
5777{
5778 return output->scale;
5779}
5780
Jason Ekstrand738715d2014-04-02 19:53:50 -05005781const char *
5782output_get_make(struct output *output)
5783{
5784 return output->make;
5785}
5786
5787const char *
5788output_get_model(struct output *output)
5789{
5790 return output->model;
5791}
5792
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005793static void
Daniel Stone97f68542012-05-30 16:32:01 +01005794fini_xkb(struct input *input)
5795{
5796 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005797 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005798}
5799
5800static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005801display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005802{
5803 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005804 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005805
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005806 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005807 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005808 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005809 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005810 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005811 input->pointer_focus = NULL;
5812 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005813 input->seat_version = seat_version;
5814
Rusty Lynch041815a2013-08-08 21:20:38 -07005815 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005816 wl_list_insert(d->input_list.prev, &input->link);
5817
Daniel Stone37816df2012-05-16 18:45:18 +01005818 wl_seat_add_listener(input->seat, &seat_listener, input);
5819 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005820
Jason Ekstranda669bd52014-04-02 19:53:51 -05005821 if (d->data_device_manager) {
5822 input->data_device =
5823 wl_data_device_manager_get_data_device(d->data_device_manager,
5824 input->seat);
5825 wl_data_device_add_listener(input->data_device,
5826 &data_device_listener,
5827 input);
5828 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005829
5830 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005831
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005832 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5833 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005834
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005835 set_repeat_info(input, 40, 400);
5836 toytimer_init(&input->repeat_timer, CLOCK_MONOTONIC, d,
5837 keyboard_repeat_func);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005838}
5839
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005840static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005841input_destroy(struct input *input)
5842{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005843 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005844 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005845
5846 if (input->drag_offer)
5847 data_offer_destroy(input->drag_offer);
5848
5849 if (input->selection_offer)
5850 data_offer_destroy(input->selection_offer);
5851
kabeer khan6ce67ec2014-10-20 11:55:29 +05305852 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005853 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305854 wl_data_device_release(input->data_device);
5855 else
5856 wl_data_device_destroy(input->data_device);
5857 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005858 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005859 if (input->touch)
5860 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005861 if (input->pointer)
5862 wl_pointer_release(input->pointer);
5863 if (input->keyboard)
5864 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005865 } else {
5866 if (input->touch)
5867 wl_touch_destroy(input->touch);
5868 if (input->pointer)
5869 wl_pointer_destroy(input->pointer);
5870 if (input->keyboard)
5871 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005872 }
5873
Daniel Stone97f68542012-05-30 16:32:01 +01005874 fini_xkb(input);
5875
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005876 wl_surface_destroy(input->pointer_surface);
5877
Pekka Paalanene1207c72011-12-16 12:02:09 +02005878 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005879 wl_seat_destroy(input->seat);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +02005880 toytimer_fini(&input->repeat_timer);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005881 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005882 free(input);
5883}
5884
5885static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005886shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5887{
5888 struct display *d = data;
5889
5890 if (format == WL_SHM_FORMAT_RGB565)
5891 d->has_rgb565 = 1;
5892}
5893
5894struct wl_shm_listener shm_listener = {
5895 shm_format
5896};
5897
5898static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005899xdg_shell_handle_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005900{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005901 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005902}
5903
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005904static const struct zxdg_shell_v6_listener xdg_shell_listener = {
5905 xdg_shell_handle_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005906};
5907
5908static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005909registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5910 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005911{
5912 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005913 struct global *global;
5914
Brian Lovinbc919262013-08-07 15:34:59 -07005915 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005916 global->name = id;
5917 global->interface = strdup(interface);
5918 global->version = version;
5919 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005920
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005921 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005922 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005923 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005924 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005925 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005926 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005927 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005928 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5929 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5930 d->relative_pointer_manager =
5931 wl_registry_bind(registry, id,
5932 &zwp_relative_pointer_manager_v1_interface,
5933 1);
5934 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5935 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5936 d->pointer_constraints =
5937 wl_registry_bind(registry, id,
5938 &zwp_pointer_constraints_v1_interface,
5939 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005940 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005941 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005942 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005943 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005944 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005945 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005946 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305947 &wl_data_device_manager_interface,
5948 d->data_device_manager_version);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005949 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005950 d->xdg_shell = wl_registry_bind(registry, id,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005951 &zxdg_shell_v6_interface, 1);
5952 zxdg_shell_v6_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005953 } else if (strcmp(interface, "text_cursor_position") == 0) {
5954 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005955 wl_registry_bind(registry, id,
5956 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005957 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5958 d->subcompositor =
5959 wl_registry_bind(registry, id,
5960 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005961 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005962 else if (strcmp(interface, "ivi_application") == 0) {
5963 d->ivi_application =
5964 wl_registry_bind(registry, id,
5965 &ivi_application_interface, 1);
5966 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005967
5968 if (d->global_handler)
5969 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005970}
5971
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005972static void
5973registry_handle_global_remove(void *data, struct wl_registry *registry,
5974 uint32_t name)
5975{
5976 struct display *d = data;
5977 struct global *global;
5978 struct global *tmp;
5979
5980 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5981 if (global->name != name)
5982 continue;
5983
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005984 if (strcmp(global->interface, "wl_output") == 0)
5985 display_destroy_output(d, name);
5986
5987 /* XXX: Should destroy remaining bound globals */
5988
5989 if (d->global_handler_remove)
5990 d->global_handler_remove(d, name, global->interface,
5991 global->version, d->user_data);
5992
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005993 wl_list_remove(&global->link);
5994 free(global->interface);
5995 free(global);
5996 }
5997}
5998
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005999void *
6000display_bind(struct display *display, uint32_t name,
6001 const struct wl_interface *interface, uint32_t version)
6002{
6003 return wl_registry_bind(display->registry, name, interface, version);
6004}
6005
6006static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006007 registry_handle_global,
6008 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006009};
6010
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006011#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006012static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006013init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006014{
6015 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006016 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006017
Rob Clark6396ed32012-03-11 19:48:41 -05006018#ifdef USE_CAIRO_GLESV2
6019# define GL_BIT EGL_OPENGL_ES2_BIT
6020#else
6021# define GL_BIT EGL_OPENGL_BIT
6022#endif
6023
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006024 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006025 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006026 EGL_RED_SIZE, 1,
6027 EGL_GREEN_SIZE, 1,
6028 EGL_BLUE_SIZE, 1,
6029 EGL_ALPHA_SIZE, 1,
6030 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006031 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006032 EGL_NONE
6033 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006034
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006035#ifdef USE_CAIRO_GLESV2
6036 static const EGLint context_attribs[] = {
6037 EGL_CONTEXT_CLIENT_VERSION, 2,
6038 EGL_NONE
6039 };
6040 EGLint api = EGL_OPENGL_ES_API;
6041#else
6042 EGLint *context_attribs = NULL;
6043 EGLint api = EGL_OPENGL_API;
6044#endif
6045
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006046 d->dpy =
6047 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6048 d->display, NULL);
6049
Yuval Fledel45568f62010-12-06 09:18:12 -05006050 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006051 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006052 return -1;
6053 }
6054
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006055 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006056 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006057 return -1;
6058 }
6059
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006060 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6061 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006062 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006063 return -1;
6064 }
6065
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006066 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006067 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006068 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006069 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006070 return -1;
6071 }
6072
Benjamin Franzke0c991632011-09-27 21:57:31 +02006073 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6074 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006075 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006076 return -1;
6077 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006078
6079 return 0;
6080}
6081
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006082static void
6083fini_egl(struct display *display)
6084{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006085 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006086
6087 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6088 EGL_NO_CONTEXT);
6089
6090 eglTerminate(display->dpy);
6091 eglReleaseThread();
6092}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006093#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006094
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006095static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006096init_dummy_surface(struct display *display)
6097{
6098 int len;
6099 void *data;
6100
6101 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006102 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006103 display->dummy_surface =
6104 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6105 1, 1, len);
6106 display->dummy_surface_data = data;
6107}
6108
6109static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006110handle_display_data(struct task *task, uint32_t events)
6111{
6112 struct display *display =
6113 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006114 struct epoll_event ep;
6115 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006116
6117 display->display_fd_events = events;
6118
6119 if (events & EPOLLERR || events & EPOLLHUP) {
6120 display_exit(display);
6121 return;
6122 }
6123
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006124 if (events & EPOLLIN) {
6125 ret = wl_display_dispatch(display->display);
6126 if (ret == -1) {
6127 display_exit(display);
6128 return;
6129 }
6130 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006131
6132 if (events & EPOLLOUT) {
6133 ret = wl_display_flush(display->display);
6134 if (ret == 0) {
6135 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6136 ep.data.ptr = &display->display_task;
6137 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6138 display->display_fd, &ep);
6139 } else if (ret == -1 && errno != EAGAIN) {
6140 display_exit(display);
6141 return;
6142 }
6143 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006144}
6145
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006146static void
6147log_handler(const char *format, va_list args)
6148{
6149 vfprintf(stderr, format, args);
6150}
6151
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006152struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006153display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006154{
6155 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006156
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006157 wl_log_set_handler_client(log_handler);
6158
Peter Huttererf3d62272013-08-08 11:57:05 +10006159 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006160 if (d == NULL)
6161 return NULL;
6162
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006163 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006164 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006165 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006166 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006167 return NULL;
6168 }
6169
Rob Bradford5ab9c752013-07-26 16:29:43 +01006170 d->xkb_context = xkb_context_new(0);
6171 if (d->xkb_context == NULL) {
6172 fprintf(stderr, "Failed to create XKB context\n");
6173 free(d);
6174 return NULL;
6175 }
6176
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006177 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006178 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006179 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006180 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6181 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006182
6183 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006184 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006185 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006186 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006187
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006188 d->registry = wl_display_get_registry(d->display);
6189 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006190
Marek Chalupaa519d062014-12-05 13:49:40 +01006191 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006192 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6193 return NULL;
6194 }
6195
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006196#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006197 if (init_egl(d) < 0)
6198 fprintf(stderr, "EGL does not seem to work, "
6199 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006200#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006201
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006202 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006203
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006204 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006205
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006206 wl_list_init(&d->window_list);
6207
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006208 init_dummy_surface(d);
6209
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006210 return d;
6211}
6212
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006213static void
6214display_destroy_outputs(struct display *display)
6215{
6216 struct output *tmp;
6217 struct output *output;
6218
6219 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6220 output_destroy(output);
6221}
6222
Pekka Paalanene1207c72011-12-16 12:02:09 +02006223static void
6224display_destroy_inputs(struct display *display)
6225{
6226 struct input *tmp;
6227 struct input *input;
6228
6229 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6230 input_destroy(input);
6231}
6232
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006233void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006234display_destroy(struct display *display)
6235{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006236 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006237 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6238 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006239
6240 if (!wl_list_empty(&display->deferred_list))
6241 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6242
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006243 cairo_surface_destroy(display->dummy_surface);
6244 free(display->dummy_surface_data);
6245
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006246 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006247 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006248
Daniel Stone97f68542012-05-30 16:32:01 +01006249 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006250
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006251 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006252 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006253
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006254#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006255 if (display->argb_device)
6256 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006257#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006258
Pekka Paalanen35e82632013-04-25 13:57:48 +03006259 if (display->subcompositor)
6260 wl_subcompositor_destroy(display->subcompositor);
6261
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006262 if (display->xdg_shell)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006263 zxdg_shell_v6_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006264
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006265 if (display->ivi_application)
6266 ivi_application_destroy(display->ivi_application);
6267
Pekka Paalanenc2052982011-12-16 11:41:32 +02006268 if (display->shm)
6269 wl_shm_destroy(display->shm);
6270
6271 if (display->data_device_manager)
6272 wl_data_device_manager_destroy(display->data_device_manager);
6273
6274 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006275 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006276
6277 close(display->epoll_fd);
6278
U. Artie Eoff44874d92012-10-02 21:12:35 -07006279 if (!(display->display_fd_events & EPOLLERR) &&
6280 !(display->display_fd_events & EPOLLHUP))
6281 wl_display_flush(display->display);
6282
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006283 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006284 free(display);
6285}
6286
6287void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006288display_set_user_data(struct display *display, void *data)
6289{
6290 display->user_data = data;
6291}
6292
6293void *
6294display_get_user_data(struct display *display)
6295{
6296 return display->user_data;
6297}
6298
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006299struct wl_display *
6300display_get_display(struct display *display)
6301{
6302 return display->display;
6303}
6304
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006305int
6306display_has_subcompositor(struct display *display)
6307{
6308 if (display->subcompositor)
6309 return 1;
6310
6311 wl_display_roundtrip(display->display);
6312
6313 return display->subcompositor != NULL;
6314}
6315
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006316cairo_device_t *
6317display_get_cairo_device(struct display *display)
6318{
6319 return display->argb_device;
6320}
6321
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006322struct output *
6323display_get_output(struct display *display)
6324{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006325 if (wl_list_empty(&display->output_list))
6326 return NULL;
6327
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006328 return container_of(display->output_list.next, struct output, link);
6329}
6330
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006331struct wl_compositor *
6332display_get_compositor(struct display *display)
6333{
6334 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006335}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006336
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006337uint32_t
6338display_get_serial(struct display *display)
6339{
6340 return display->serial;
6341}
6342
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006343EGLDisplay
6344display_get_egl_display(struct display *d)
6345{
6346 return d->dpy;
6347}
6348
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006349struct wl_data_source *
6350display_create_data_source(struct display *display)
6351{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006352 if (display->data_device_manager)
6353 return wl_data_device_manager_create_data_source(display->data_device_manager);
6354 else
6355 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006356}
6357
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006358EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006359display_get_argb_egl_config(struct display *d)
6360{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006361 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006362}
6363
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006364int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006365display_acquire_window_surface(struct display *display,
6366 struct window *window,
6367 EGLContext ctx)
6368{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006369 struct surface *surface = window->main_surface;
6370
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006371 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006372 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006373
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006374 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006375 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006376}
6377
6378void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006379display_release_window_surface(struct display *display,
6380 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006381{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006382 struct surface *surface = window->main_surface;
6383
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006384 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006385 return;
6386
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006387 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006388}
6389
6390void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006391display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006392{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006393 wl_list_insert(&display->deferred_list, &task->link);
6394}
6395
6396void
6397display_watch_fd(struct display *display,
6398 int fd, uint32_t events, struct task *task)
6399{
6400 struct epoll_event ep;
6401
6402 ep.events = events;
6403 ep.data.ptr = task;
6404 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6405}
6406
6407void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006408display_unwatch_fd(struct display *display, int fd)
6409{
6410 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6411}
6412
6413void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006414display_run(struct display *display)
6415{
6416 struct task *task;
6417 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006418 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006419
Pekka Paalanen826d7952011-12-15 10:14:07 +02006420 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006421 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006422 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006423 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006424 struct task, link);
6425 wl_list_remove(&task->link);
6426 task->run(task, 0);
6427 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006428
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006429 wl_display_dispatch_pending(display->display);
6430
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006431 if (!display->running)
6432 break;
6433
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006434 ret = wl_display_flush(display->display);
6435 if (ret < 0 && errno == EAGAIN) {
6436 ep[0].events =
6437 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6438 ep[0].data.ptr = &display->display_task;
6439
6440 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6441 display->display_fd, &ep[0]);
6442 } else if (ret < 0) {
6443 break;
6444 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006445
6446 count = epoll_wait(display->epoll_fd,
6447 ep, ARRAY_LENGTH(ep), -1);
6448 for (i = 0; i < count; i++) {
6449 task = ep[i].data.ptr;
6450 task->run(task, ep[i].events);
6451 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006452 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006453}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006454
6455void
6456display_exit(struct display *display)
6457{
6458 display->running = 0;
6459}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006460
Carlos Garnacho9c931792016-01-18 23:52:12 +01006461int
6462display_get_data_device_manager_version(struct display *display)
6463{
6464 return display->data_device_manager_version;
6465}
6466
Jan Arne Petersencd997062012-11-18 19:06:44 +01006467void
6468keysym_modifiers_add(struct wl_array *modifiers_map,
6469 const char *name)
6470{
6471 size_t len = strlen(name) + 1;
6472 char *p;
6473
6474 p = wl_array_add(modifiers_map, len);
6475
6476 if (p == NULL)
6477 return;
6478
6479 strncpy(p, name, len);
6480}
6481
6482static xkb_mod_index_t
6483keysym_modifiers_get_index(struct wl_array *modifiers_map,
6484 const char *name)
6485{
6486 xkb_mod_index_t index = 0;
6487 char *p = modifiers_map->data;
6488
6489 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6490 if (strcmp(p, name) == 0)
6491 return index;
6492
6493 index++;
6494 p += strlen(p) + 1;
6495 }
6496
6497 return XKB_MOD_INVALID;
6498}
6499
6500xkb_mod_mask_t
6501keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6502 const char *name)
6503{
6504 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6505
6506 if (index == XKB_MOD_INVALID)
6507 return XKB_MOD_INVALID;
6508
6509 return 1 << index;
6510}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006511
6512static void
6513toytimer_fire(struct task *tsk, uint32_t events)
6514{
6515 uint64_t e;
6516 struct toytimer *tt;
6517
6518 tt = container_of(tsk, struct toytimer, tsk);
6519
6520 if (events != EPOLLIN)
6521 fprintf(stderr, "unexpected timerfd events %x\n", events);
6522
6523 if (!(events & EPOLLIN))
6524 return;
6525
6526 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6527 /* If we change the timer between the fd becoming
6528 * readable and getting here, there'll be nothing to
6529 * read and we get EAGAIN. */
6530 if (errno != EAGAIN)
6531 fprintf(stderr, "timer read failed: %m\n");
6532 return;
6533 }
6534
6535 tt->callback(tt);
6536}
6537
6538void
6539toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6540 toytimer_cb callback)
6541{
6542 memset(tt, 0, sizeof *tt);
6543
6544 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6545 if (tt->fd == -1) {
6546 fprintf(stderr, "creating timer failed: %m\n");
6547 abort();
6548 }
6549
6550 tt->display = display;
6551 tt->callback = callback;
6552 tt->tsk.run = toytimer_fire;
6553 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6554}
6555
6556void
6557toytimer_fini(struct toytimer *tt)
6558{
6559 display_unwatch_fd(tt->display, tt->fd);
6560 close(tt->fd);
6561 tt->fd = -1;
6562}
6563
6564void
6565toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6566{
6567 int ret;
6568
6569 ret = timerfd_settime(tt->fd, 0, its, NULL);
6570 if (ret < 0) {
6571 fprintf(stderr, "timer setup failed: %m\n");
6572 abort();
6573 }
6574}
6575
6576#define USEC_PER_SEC 1000000
6577
6578void
6579toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6580{
6581 struct itimerspec its;
6582
6583 its.it_interval.tv_sec = 0;
6584 its.it_interval.tv_nsec = 0;
6585 its.it_value.tv_sec = usec / USEC_PER_SEC;
6586 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6587 toytimer_arm(tt, &its);
6588}
6589
6590void
6591toytimer_disarm(struct toytimer *tt)
6592{
6593 struct itimerspec its = {};
6594
6595 toytimer_arm(tt, &its);
6596}