blob: 7c3d3bdd8b1726d2cfb176b2ba2167323f70d268 [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;
289 struct widget *confined_widget;
290 bool confined;
291
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500292 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400293 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500294};
295
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500296struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500297 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200298 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300299 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500300 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400301 struct wl_list link;
302 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500303 widget_resize_handler_t resize_handler;
304 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500305 widget_enter_handler_t enter_handler;
306 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500307 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500308 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700309 widget_touch_down_handler_t touch_down_handler;
310 widget_touch_up_handler_t touch_up_handler;
311 widget_touch_motion_handler_t touch_motion_handler;
312 widget_touch_frame_handler_t touch_frame_handler;
313 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200314 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000315 widget_pointer_frame_handler_t pointer_frame_handler;
316 widget_axis_source_handler_t axis_source_handler;
317 widget_axis_stop_handler_t axis_stop_handler;
318 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400319 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500320 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300321 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500322 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000323 /* If this is set to false then no cairo surface will be
324 * created before redrawing the surface. This is useful if the
325 * redraw handler is going to do completely custom rendering
326 * such as using EGL directly */
327 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400328};
329
Rusty Lynch041815a2013-08-08 21:20:38 -0700330struct touch_point {
331 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800332 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700333 struct widget *widget;
334 struct wl_list link;
335};
336
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400337struct input {
338 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100339 struct wl_seat *seat;
340 struct wl_pointer *pointer;
341 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700342 struct wl_touch *touch;
343 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400344 struct window *pointer_focus;
345 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700346 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300347 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300348 uint32_t cursor_anim_start;
349 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500350 uint32_t cursor_timer_start;
351 uint32_t cursor_anim_current;
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +0200352 struct toytimer cursor_timer;
Derek Foreman118a4292015-04-22 17:23:35 -0500353 bool cursor_timer_running;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300354 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400355 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400356 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400357 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400358 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400359 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400360
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500361 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500362 struct widget *grab;
363 uint32_t grab_button;
364
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400365 struct wl_data_device *data_device;
366 struct data_offer *drag_offer;
367 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800368 uint32_t touch_grab;
369 int32_t touch_grab_id;
370 float drag_x, drag_y;
371 struct window *drag_focus;
372 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100373
374 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100375 struct xkb_keymap *keymap;
376 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +0100377#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700378 struct xkb_compose_table *compose_table;
379 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +0100380#endif
Daniel Stone97f68542012-05-30 16:32:01 +0100381 xkb_mod_mask_t control_mask;
382 xkb_mod_mask_t alt_mask;
383 xkb_mod_mask_t shift_mask;
384 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400385
Jonny Lamb06959082014-08-12 14:58:27 +0200386 int32_t repeat_rate_sec;
387 int32_t repeat_rate_nsec;
388 int32_t repeat_delay_sec;
389 int32_t repeat_delay_nsec;
390
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400391 struct task repeat_task;
392 int repeat_timer_fd;
393 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;
2913 struct itimerspec its;
2914
2915 its.it_interval.tv_sec = 0;
2916 its.it_interval.tv_nsec = 0;
2917 its.it_value.tv_sec = 0;
2918 its.it_value.tv_nsec = 0;
2919 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2920
2921 if (!window)
2922 return;
2923
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002924 if (window->keyboard_focus_handler)
2925 (*window->keyboard_focus_handler)(window, NULL,
2926 window->user_data);
2927
2928 input->keyboard_focus = NULL;
2929}
2930
2931static void
2932keyboard_repeat_func(struct task *task, uint32_t events)
2933{
2934 struct input *input =
2935 container_of(task, struct input, repeat_task);
2936 struct window *window = input->keyboard_focus;
2937 uint64_t exp;
2938
2939 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2940 /* If we change the timer between the fd becoming
2941 * readable and getting here, there'll be nothing to
2942 * read and we get EAGAIN. */
2943 return;
2944
2945 if (window && window->key_handler) {
2946 (*window->key_handler)(window, input, input->repeat_time,
2947 input->repeat_key, input->repeat_sym,
2948 WL_KEYBOARD_KEY_STATE_PRESSED,
2949 window->user_data);
2950 }
2951}
2952
2953static void
2954keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2955 uint32_t format, int fd, uint32_t size)
2956{
2957 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002958 struct xkb_keymap *keymap;
2959 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002960#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002961 struct xkb_compose_table *compose_table;
2962 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002963#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002964 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002965 char *map_str;
2966
2967 if (!data) {
2968 close(fd);
2969 return;
2970 }
2971
2972 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2973 close(fd);
2974 return;
2975 }
2976
2977 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2978 if (map_str == MAP_FAILED) {
2979 close(fd);
2980 return;
2981 }
2982
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002983 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03002984 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2985 map_str,
2986 XKB_KEYMAP_FORMAT_TEXT_V1,
2987 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002988 munmap(map_str, size);
2989 close(fd);
2990
Rui Matos3eccb862013-10-10 19:44:22 +02002991 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002992 fprintf(stderr, "failed to compile keymap\n");
2993 return;
2994 }
2995
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002996 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02002997 state = xkb_state_new(keymap);
2998 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002999 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003000 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003001 return;
3002 }
3003
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003004 /* Look up the preferred locale, falling back to "C" as default */
3005 if (!(locale = getenv("LC_ALL")))
3006 if (!(locale = getenv("LC_CTYPE")))
3007 if (!(locale = getenv("LANG")))
3008 locale = "C";
3009
3010 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003011#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003012 compose_table =
3013 xkb_compose_table_new_from_locale(input->display->xkb_context,
3014 locale,
3015 XKB_COMPOSE_COMPILE_NO_FLAGS);
3016 if (compose_table) {
3017 /* Set up XKB compose state */
3018 compose_state = xkb_compose_state_new(compose_table,
3019 XKB_COMPOSE_STATE_NO_FLAGS);
3020 if (compose_state) {
3021 xkb_compose_state_unref(input->xkb.compose_state);
3022 xkb_compose_table_unref(input->xkb.compose_table);
3023 input->xkb.compose_state = compose_state;
3024 input->xkb.compose_table = compose_table;
3025 } else {
3026 fprintf(stderr, "could not create XKB compose state. "
3027 "Disabiling compose.\n");
3028 xkb_compose_table_unref(compose_table);
3029 compose_table = NULL;
3030 }
3031 } else {
3032 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3033 "Disabiling compose\n", locale);
3034 }
Daniel Stone5b015962016-10-20 14:45:58 +01003035#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003036
Rui Matos3eccb862013-10-10 19:44:22 +02003037 xkb_keymap_unref(input->xkb.keymap);
3038 xkb_state_unref(input->xkb.state);
3039 input->xkb.keymap = keymap;
3040 input->xkb.state = state;
3041
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003042 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003043 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003044 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003045 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003046 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003047 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003048}
3049
3050static void
3051keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3052 uint32_t serial, struct wl_surface *surface,
3053 struct wl_array *keys)
3054{
3055 struct input *input = data;
3056 struct window *window;
3057
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003058 if (!surface) {
3059 /* enter event for a window we've just destroyed */
3060 return;
3061 }
3062
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003063 input->display->serial = serial;
3064 input->keyboard_focus = wl_surface_get_user_data(surface);
3065
3066 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003067 if (window->keyboard_focus_handler)
3068 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003069 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003070}
3071
3072static void
3073keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3074 uint32_t serial, struct wl_surface *surface)
3075{
3076 struct input *input = data;
3077
3078 input->display->serial = serial;
3079 input_remove_keyboard_focus(input);
3080}
3081
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003082/* Translate symbols appropriately if a compose sequence is being entered */
3083static xkb_keysym_t
3084process_key_press(xkb_keysym_t sym, struct input *input)
3085{
Daniel Stone5b015962016-10-20 14:45:58 +01003086#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003087 if (!input->xkb.compose_state)
3088 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003089 if (sym == XKB_KEY_NoSymbol)
3090 return sym;
3091 if (xkb_compose_state_feed(input->xkb.compose_state,
3092 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3093 return sym;
3094
3095 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3096 case XKB_COMPOSE_COMPOSING:
3097 return XKB_KEY_NoSymbol;
3098 case XKB_COMPOSE_COMPOSED:
3099 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3100 case XKB_COMPOSE_CANCELLED:
3101 return XKB_KEY_NoSymbol;
3102 case XKB_COMPOSE_NOTHING:
3103 return sym;
3104 default:
3105 return sym;
3106 }
Daniel Stone5b015962016-10-20 14:45:58 +01003107#else
3108 return sym;
3109#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003110}
3111
Scott Moreau210d0792012-03-22 10:47:01 -06003112static void
Daniel Stone37816df2012-05-16 18:45:18 +01003113keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003114 uint32_t serial, uint32_t time, uint32_t key,
3115 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003116{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003117 struct input *input = data;
3118 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003119 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003120 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003121 const xkb_keysym_t *syms;
3122 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003123 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003124
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003125 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003126 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003127 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003128 return;
3129
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003130 /* We only use input grabs for pointer events for now, so just
3131 * ignore key presses if a grab is active. We expand the key
3132 * event delivery mechanism to route events to widgets to
3133 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003134 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003135 if (input->grab && input->grab_button == 0)
3136 return;
3137
Ran Benita2e1968f2014-08-19 23:59:51 +03003138 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003139
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003140 sym = XKB_KEY_NoSymbol;
3141 if (num_syms == 1)
3142 sym = syms[0];
3143
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003144
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003145 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003146 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003147 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003148 } else if (sym == XKB_KEY_F11 &&
3149 window->fullscreen_handler &&
3150 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3151 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003152 } else if (sym == XKB_KEY_F4 &&
3153 input->modifiers == MOD_ALT_MASK &&
3154 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003155 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003156 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003157 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3158 sym = process_key_press(sym, input);
3159
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003160 (*window->key_handler)(window, input, time, key,
3161 sym, state, window->user_data);
3162 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003163
3164 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3165 key == input->repeat_key) {
3166 its.it_interval.tv_sec = 0;
3167 its.it_interval.tv_nsec = 0;
3168 its.it_value.tv_sec = 0;
3169 its.it_value.tv_nsec = 0;
3170 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003171 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3172 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003173 input->repeat_sym = sym;
3174 input->repeat_key = key;
3175 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003176 its.it_interval.tv_sec = input->repeat_rate_sec;
3177 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3178 its.it_value.tv_sec = input->repeat_delay_sec;
3179 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003180 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3181 }
3182}
3183
3184static void
Daniel Stone351eb612012-05-31 15:27:47 -04003185keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3186 uint32_t serial, uint32_t mods_depressed,
3187 uint32_t mods_latched, uint32_t mods_locked,
3188 uint32_t group)
3189{
3190 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003191 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003192
Matt Ropere61561f2013-06-24 16:52:43 +01003193 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3194 if (!input->xkb.keymap)
3195 return;
3196
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003197 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3198 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003199 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003200 XKB_STATE_MODS_DEPRESSED |
3201 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003202 input->modifiers = 0;
3203 if (mask & input->xkb.control_mask)
3204 input->modifiers |= MOD_CONTROL_MASK;
3205 if (mask & input->xkb.alt_mask)
3206 input->modifiers |= MOD_ALT_MASK;
3207 if (mask & input->xkb.shift_mask)
3208 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003209}
3210
Jonny Lamb06959082014-08-12 14:58:27 +02003211static void
3212set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3213{
3214 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3215 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3216
3217 /* a rate of zero disables any repeating, regardless of the delay's
3218 * value */
3219 if (rate == 0)
3220 return;
3221
3222 if (rate == 1)
3223 input->repeat_rate_sec = 1;
3224 else
3225 input->repeat_rate_nsec = 1000000000 / rate;
3226
3227 input->repeat_delay_sec = delay / 1000;
3228 delay -= (input->repeat_delay_sec * 1000);
3229 input->repeat_delay_nsec = delay * 1000 * 1000;
3230}
3231
3232static void
3233keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3234 int32_t rate, int32_t delay)
3235{
3236 struct input *input = data;
3237
3238 set_repeat_info(input, rate, delay);
3239}
3240
Daniel Stone37816df2012-05-16 18:45:18 +01003241static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003242 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003243 keyboard_handle_enter,
3244 keyboard_handle_leave,
3245 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003246 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003247 keyboard_handle_repeat_info
3248
Daniel Stone37816df2012-05-16 18:45:18 +01003249};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003250
3251static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003252touch_handle_down(void *data, struct wl_touch *wl_touch,
3253 uint32_t serial, uint32_t time, struct wl_surface *surface,
3254 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3255{
3256 struct input *input = data;
3257 struct widget *widget;
3258 float sx = wl_fixed_to_double(x_w);
3259 float sy = wl_fixed_to_double(y_w);
3260
Rusty Lynch1084da52013-08-15 09:10:08 -07003261 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003262 input->touch_focus = wl_surface_get_user_data(surface);
3263 if (!input->touch_focus) {
3264 DBG("Failed to find to touch focus for surface %p\n", surface);
3265 return;
3266 }
3267
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003268 if (surface != input->touch_focus->main_surface->surface) {
3269 DBG("Ignoring input event from subsurface %p\n", surface);
3270 input->touch_focus = NULL;
3271 return;
3272 }
3273
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003274 if (input->grab)
3275 widget = input->grab;
3276 else
3277 widget = window_find_widget(input->touch_focus,
3278 wl_fixed_to_double(x_w),
3279 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003280 if (widget) {
3281 struct touch_point *tp = xmalloc(sizeof *tp);
3282 if (tp) {
3283 tp->id = id;
3284 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003285 tp->x = sx;
3286 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003287 wl_list_insert(&input->touch_point_list, &tp->link);
3288
3289 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003290 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003291 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003292 sx, sy,
3293 widget->user_data);
3294 }
3295 }
3296}
3297
3298static void
3299touch_handle_up(void *data, struct wl_touch *wl_touch,
3300 uint32_t serial, uint32_t time, int32_t id)
3301{
3302 struct input *input = data;
3303 struct touch_point *tp, *tmp;
3304
Rusty Lynch041815a2013-08-08 21:20:38 -07003305 if (!input->touch_focus) {
3306 DBG("No touch focus found for touch up event!\n");
3307 return;
3308 }
3309
3310 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3311 if (tp->id != id)
3312 continue;
3313
3314 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003315 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003316 time, id,
3317 tp->widget->user_data);
3318
3319 wl_list_remove(&tp->link);
3320 free(tp);
3321
3322 return;
3323 }
3324}
3325
3326static void
3327touch_handle_motion(void *data, struct wl_touch *wl_touch,
3328 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3329{
3330 struct input *input = data;
3331 struct touch_point *tp;
3332 float sx = wl_fixed_to_double(x_w);
3333 float sy = wl_fixed_to_double(y_w);
3334
3335 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3336
3337 if (!input->touch_focus) {
3338 DBG("No touch focus found for touch motion event!\n");
3339 return;
3340 }
3341
3342 wl_list_for_each(tp, &input->touch_point_list, link) {
3343 if (tp->id != id)
3344 continue;
3345
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003346 tp->x = sx;
3347 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003348 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003349 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003350 id, sx, sy,
3351 tp->widget->user_data);
3352 return;
3353 }
3354}
3355
3356static void
3357touch_handle_frame(void *data, struct wl_touch *wl_touch)
3358{
3359 struct input *input = data;
3360 struct touch_point *tp, *tmp;
3361
3362 DBG("touch_handle_frame\n");
3363
3364 if (!input->touch_focus) {
3365 DBG("No touch focus found for touch frame event!\n");
3366 return;
3367 }
3368
3369 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3370 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003371 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003372 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003373 }
3374}
3375
3376static void
3377touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3378{
3379 struct input *input = data;
3380 struct touch_point *tp, *tmp;
3381
3382 DBG("touch_handle_cancel\n");
3383
3384 if (!input->touch_focus) {
3385 DBG("No touch focus found for touch cancel event!\n");
3386 return;
3387 }
3388
3389 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3390 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003391 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3392 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003393
3394 wl_list_remove(&tp->link);
3395 free(tp);
3396 }
3397}
3398
3399static const struct wl_touch_listener touch_listener = {
3400 touch_handle_down,
3401 touch_handle_up,
3402 touch_handle_motion,
3403 touch_handle_frame,
3404 touch_handle_cancel,
3405};
3406
3407static void
Daniel Stone37816df2012-05-16 18:45:18 +01003408seat_handle_capabilities(void *data, struct wl_seat *seat,
3409 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003410{
Daniel Stone37816df2012-05-16 18:45:18 +01003411 struct input *input = data;
3412
3413 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3414 input->pointer = wl_seat_get_pointer(seat);
3415 wl_pointer_set_user_data(input->pointer, input);
3416 wl_pointer_add_listener(input->pointer, &pointer_listener,
3417 input);
3418 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003419 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003420 wl_pointer_release(input->pointer);
3421 else
3422 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003423 input->pointer = NULL;
3424 }
3425
3426 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3427 input->keyboard = wl_seat_get_keyboard(seat);
3428 wl_keyboard_set_user_data(input->keyboard, input);
3429 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3430 input);
3431 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003432 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003433 wl_keyboard_release(input->keyboard);
3434 else
3435 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003436 input->keyboard = NULL;
3437 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003438
3439 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3440 input->touch = wl_seat_get_touch(seat);
3441 wl_touch_set_user_data(input->touch, input);
3442 wl_touch_add_listener(input->touch, &touch_listener, input);
3443 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003444 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003445 wl_touch_release(input->touch);
3446 else
3447 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003448 input->touch = NULL;
3449 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003450}
3451
Rob Bradford08031182013-08-13 20:11:03 +01003452static void
3453seat_handle_name(void *data, struct wl_seat *seat,
3454 const char *name)
3455{
3456
3457}
3458
Daniel Stone37816df2012-05-16 18:45:18 +01003459static const struct wl_seat_listener seat_listener = {
3460 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003461 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003462};
3463
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003464void
3465input_get_position(struct input *input, int32_t *x, int32_t *y)
3466{
3467 *x = input->sx;
3468 *y = input->sy;
3469}
3470
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003471int
3472input_get_touch(struct input *input, int32_t id, float *x, float *y)
3473{
3474 struct touch_point *tp;
3475
3476 wl_list_for_each(tp, &input->touch_point_list, link) {
3477 if (tp->id != id)
3478 continue;
3479
3480 *x = tp->x;
3481 *y = tp->y;
3482 return 0;
3483 }
3484
3485 return -1;
3486}
3487
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003488struct display *
3489input_get_display(struct input *input)
3490{
3491 return input->display;
3492}
3493
Daniel Stone37816df2012-05-16 18:45:18 +01003494struct wl_seat *
3495input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003496{
Daniel Stone37816df2012-05-16 18:45:18 +01003497 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003498}
3499
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003500uint32_t
3501input_get_modifiers(struct input *input)
3502{
3503 return input->modifiers;
3504}
3505
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003506struct widget *
3507input_get_focus_widget(struct input *input)
3508{
3509 return input->focus_widget;
3510}
3511
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003512struct data_offer {
3513 struct wl_data_offer *offer;
3514 struct input *input;
3515 struct wl_array types;
3516 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003517
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003518 struct task io_task;
3519 int fd;
3520 data_func_t func;
3521 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003522 uint32_t dnd_action;
3523 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003524 void *user_data;
3525};
3526
3527static void
3528data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3529{
3530 struct data_offer *offer = data;
3531 char **p;
3532
3533 p = wl_array_add(&offer->types, sizeof *p);
3534 *p = strdup(type);
3535}
3536
Carlos Garnacho9c931792016-01-18 23:52:12 +01003537static void
3538data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3539{
3540 struct data_offer *offer = data;
3541
3542 offer->source_actions = source_actions;
3543}
3544
3545static void
3546data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3547{
3548 struct data_offer *offer = data;
3549
3550 offer->dnd_action = dnd_action;
3551}
3552
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003553static const struct wl_data_offer_listener data_offer_listener = {
3554 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003555 data_offer_source_actions,
3556 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003557};
3558
3559static void
3560data_offer_destroy(struct data_offer *offer)
3561{
3562 char **p;
3563
3564 offer->refcount--;
3565 if (offer->refcount == 0) {
3566 wl_data_offer_destroy(offer->offer);
3567 for (p = offer->types.data; *p; p++)
3568 free(*p);
3569 wl_array_release(&offer->types);
3570 free(offer);
3571 }
3572}
3573
3574static void
3575data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003576 struct wl_data_device *data_device,
3577 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003578{
3579 struct data_offer *offer;
3580
Brian Lovinbc919262013-08-07 15:34:59 -07003581 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003582
3583 wl_array_init(&offer->types);
3584 offer->refcount = 1;
3585 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003586 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003587 wl_data_offer_add_listener(offer->offer,
3588 &data_offer_listener, offer);
3589}
3590
3591static void
3592data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003593 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003594 wl_fixed_t x_w, wl_fixed_t y_w,
3595 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003596{
3597 struct input *input = data;
3598 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003599 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003600 float x = wl_fixed_to_double(x_w);
3601 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003602 char **p;
3603
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003604 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003605 input->drag_enter_serial = serial;
3606 input->drag_focus = window,
3607 input->drag_x = x;
3608 input->drag_y = y;
3609
3610 if (!input->touch_grab)
3611 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003612
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003613 if (offer) {
3614 input->drag_offer = wl_data_offer_get_user_data(offer);
3615
3616 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3617 *p = NULL;
3618
3619 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003620
3621 if (input->display->data_device_manager_version >=
3622 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3623 wl_data_offer_set_actions(offer,
3624 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3625 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3626 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3627 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003628 } else {
3629 input->drag_offer = NULL;
3630 types_data = NULL;
3631 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003632
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003633 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003634 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003635 window->user_data);
3636}
3637
3638static void
3639data_device_leave(void *data, struct wl_data_device *data_device)
3640{
3641 struct input *input = data;
3642
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003643 if (input->drag_offer) {
3644 data_offer_destroy(input->drag_offer);
3645 input->drag_offer = NULL;
3646 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003647}
3648
3649static void
3650data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003651 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003652{
3653 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003654 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003655 float x = wl_fixed_to_double(x_w);
3656 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003657 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003658
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003659 input->drag_x = x;
3660 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003661
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003662 if (input->drag_offer)
3663 types_data = input->drag_offer->types.data;
3664 else
3665 types_data = NULL;
3666
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003667 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003668 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003669 window->user_data);
3670}
3671
3672static void
3673data_device_drop(void *data, struct wl_data_device *data_device)
3674{
3675 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003676 struct window *window = input->drag_focus;
3677 float x, y;
3678
3679 x = input->drag_x;
3680 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003681
3682 if (window->drop_handler)
3683 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003684 x, y, window->user_data);
3685
3686 if (input->touch_grab)
3687 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003688}
3689
3690static void
3691data_device_selection(void *data,
3692 struct wl_data_device *wl_data_device,
3693 struct wl_data_offer *offer)
3694{
3695 struct input *input = data;
3696 char **p;
3697
3698 if (input->selection_offer)
3699 data_offer_destroy(input->selection_offer);
3700
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003701 if (offer) {
3702 input->selection_offer = wl_data_offer_get_user_data(offer);
3703 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3704 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003705 } else {
3706 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003707 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003708}
3709
3710static const struct wl_data_device_listener data_device_listener = {
3711 data_device_data_offer,
3712 data_device_enter,
3713 data_device_leave,
3714 data_device_motion,
3715 data_device_drop,
3716 data_device_selection
3717};
3718
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003719static void
3720input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003721{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003722 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003723 struct wl_cursor *cursor;
3724 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003725
Daniel Stone80972742012-11-07 17:51:39 +11003726 if (!input->pointer)
3727 return;
3728
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003729 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003730 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003731 return;
3732
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003733 if (index >= (int) cursor->image_count) {
3734 fprintf(stderr, "cursor index out of range\n");
3735 return;
3736 }
3737
3738 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003739 buffer = wl_cursor_image_get_buffer(image);
3740 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003741 return;
3742
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003743 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3744 wl_surface_damage(input->pointer_surface, 0, 0,
3745 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003746 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003747 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3748 input->pointer_surface,
3749 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003750}
3751
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003752static const struct wl_callback_listener pointer_surface_listener;
3753
Derek Foreman493d9792015-03-04 16:26:25 -06003754static bool
3755input_set_pointer_special(struct input *input)
3756{
3757 if (input->current_cursor == CURSOR_BLANK) {
3758 wl_pointer_set_cursor(input->pointer,
3759 input->pointer_enter_serial,
3760 NULL, 0, 0);
3761 return true;
3762 }
3763
3764 if (input->current_cursor == CURSOR_UNSET)
3765 return true;
3766
3767 return false;
3768}
3769
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003770static void
Derek Foreman118a4292015-04-22 17:23:35 -05003771schedule_pointer_image_update(struct input *input,
3772 struct wl_cursor *cursor,
3773 uint32_t duration,
3774 bool force_frame)
3775{
3776 /* Some silly cursor sets have enormous pauses in them. In these
3777 * cases it's better to use a timer even if it results in less
3778 * accurate presentation, since it will save us having to set the
3779 * same cursor image over and over again.
3780 *
3781 * This is really not the way we're supposed to time any kind of
3782 * animation, but we're pretending it's OK here because we don't
3783 * want animated cursors with long delays to needlessly hog CPU.
3784 *
3785 * We use force_frame to ensure we don't accumulate large timing
3786 * errors by running off the wrong clock.
3787 */
3788 if (!force_frame && duration > 100) {
3789 struct timespec tp;
3790
3791 clock_gettime(CLOCK_MONOTONIC, &tp);
3792 input->cursor_timer_start = tp.tv_sec * 1000
3793 + tp.tv_nsec / 1000000;
3794 cursor_delay_timer_reset(input, duration);
3795 return;
3796 }
3797
3798 /* for short durations we'll just spin on frame callbacks for
3799 * accurate timing - the way any kind of timing sensitive animation
3800 * should really be done. */
3801 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3802 wl_callback_add_listener(input->cursor_frame_cb,
3803 &pointer_surface_listener, input);
3804
3805}
3806
3807static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003808pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3809 uint32_t time)
3810{
3811 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003812 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003813 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003814 uint32_t duration;
3815 bool force_frame = true;
3816
3817 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003818
3819 if (callback) {
3820 assert(callback == input->cursor_frame_cb);
3821 wl_callback_destroy(callback);
3822 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003823 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003824 }
3825
Daniel Stone80972742012-11-07 17:51:39 +11003826 if (!input->pointer)
3827 return;
3828
Derek Foreman493d9792015-03-04 16:26:25 -06003829 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003830 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003831
Daniel Stonea494f1d2012-06-18 19:31:12 +01003832 cursor = input->display->cursors[input->current_cursor];
3833 if (!cursor)
3834 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003835
3836 /* FIXME We don't have the current time on the first call so we set
3837 * the animation start to the time of the first frame callback. */
3838 if (time == 0)
3839 input->cursor_anim_start = 0;
3840 else if (input->cursor_anim_start == 0)
3841 input->cursor_anim_start = time;
3842
Derek Foreman118a4292015-04-22 17:23:35 -05003843 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003844
Derek Foreman118a4292015-04-22 17:23:35 -05003845 if (time == 0 || input->cursor_anim_start == 0) {
3846 duration = 0;
3847 i = 0;
3848 } else
3849 i = wl_cursor_frame_and_duration(
3850 cursor,
3851 time - input->cursor_anim_start,
3852 &duration);
3853
3854 if (cursor->image_count > 1)
3855 schedule_pointer_image_update(input, cursor, duration,
3856 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003857
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003858 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003859}
3860
Derek Foreman118a4292015-04-22 17:23:35 -05003861static void
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003862cursor_timer_func(struct toytimer *tt)
Derek Foreman118a4292015-04-22 17:23:35 -05003863{
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02003864 struct input *input = container_of(tt, struct input, cursor_timer);
Derek Foreman118a4292015-04-22 17:23:35 -05003865 struct timespec tp;
3866 struct wl_cursor *cursor;
3867 uint32_t time;
Derek Foreman118a4292015-04-22 17:23:35 -05003868
3869 if (!input->cursor_timer_running)
3870 return;
3871
Derek Foreman118a4292015-04-22 17:23:35 -05003872 cursor = input->display->cursors[input->current_cursor];
3873 if (!cursor)
3874 return;
3875
3876 clock_gettime(CLOCK_MONOTONIC, &tp);
3877 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3878 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3879}
3880
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003881static const struct wl_callback_listener pointer_surface_listener = {
3882 pointer_surface_frame_callback
3883};
3884
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003885void
3886input_set_pointer_image(struct input *input, int pointer)
3887{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003888 int force = 0;
3889
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003890 if (!input->pointer)
3891 return;
3892
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003893 if (input->pointer_enter_serial > input->cursor_serial)
3894 force = 1;
3895
3896 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003897 return;
3898
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003899 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003900 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003901 if (!input->cursor_frame_cb)
3902 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003903 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003904 /* The current frame callback may be stuck if, for instance,
3905 * the set cursor request was processed by the server after
3906 * this client lost the focus. In this case the cursor surface
3907 * might not be mapped and the frame callback wouldn't ever
3908 * complete. Send a set_cursor and attach to try to map the
3909 * cursor surface again so that the callback will finish */
3910 input_set_pointer_image_index(input, 0);
3911 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003912}
3913
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003914struct wl_data_device *
3915input_get_data_device(struct input *input)
3916{
3917 return input->data_device;
3918}
3919
3920void
3921input_set_selection(struct input *input,
3922 struct wl_data_source *source, uint32_t time)
3923{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003924 if (input->data_device)
3925 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003926}
3927
3928void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003929input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003930{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003931 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003932 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003933}
3934
3935static void
3936offer_io_func(struct task *task, uint32_t events)
3937{
3938 struct data_offer *offer =
3939 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003940 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003941 unsigned int len;
3942 char buffer[4096];
3943
3944 len = read(offer->fd, buffer, sizeof buffer);
3945 offer->func(buffer, len,
3946 offer->x, offer->y, offer->user_data);
3947
3948 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003949 if (display->data_device_manager_version >=
3950 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3951 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003952 close(offer->fd);
3953 data_offer_destroy(offer);
3954 }
3955}
3956
3957static void
3958data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3959 data_func_t func, void *user_data)
3960{
3961 int p[2];
3962
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003963 if (pipe2(p, O_CLOEXEC) == -1)
3964 return;
3965
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003966 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3967 close(p[1]);
3968
3969 offer->io_task.run = offer_io_func;
3970 offer->fd = p[0];
3971 offer->func = func;
3972 offer->refcount++;
3973 offer->user_data = user_data;
3974
3975 display_watch_fd(offer->input->display,
3976 offer->fd, EPOLLIN, &offer->io_task);
3977}
3978
3979void
3980input_receive_drag_data(struct input *input, const char *mime_type,
3981 data_func_t func, void *data)
3982{
3983 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003984 input->drag_offer->x = input->drag_x;
3985 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003986}
3987
3988int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003989input_receive_drag_data_to_fd(struct input *input,
3990 const char *mime_type, int fd)
3991{
3992 if (input->drag_offer)
3993 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3994
3995 return 0;
3996}
3997
3998int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003999input_receive_selection_data(struct input *input, const char *mime_type,
4000 data_func_t func, void *data)
4001{
4002 char **p;
4003
4004 if (input->selection_offer == NULL)
4005 return -1;
4006
4007 for (p = input->selection_offer->types.data; *p; p++)
4008 if (strcmp(mime_type, *p) == 0)
4009 break;
4010
4011 if (*p == NULL)
4012 return -1;
4013
4014 data_offer_receive_data(input->selection_offer,
4015 mime_type, func, data);
4016 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004017}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004018
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004019int
4020input_receive_selection_data_to_fd(struct input *input,
4021 const char *mime_type, int fd)
4022{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004023 if (input->selection_offer)
4024 wl_data_offer_receive(input->selection_offer->offer,
4025 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004026
4027 return 0;
4028}
4029
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004030void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004031window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004032{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004033 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004034 return;
4035
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004036 zxdg_toplevel_v6_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004037}
4038
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004039static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004040surface_set_synchronized(struct surface *surface)
4041{
4042 if (!surface->subsurface)
4043 return;
4044
4045 if (surface->synchronized)
4046 return;
4047
4048 wl_subsurface_set_sync(surface->subsurface);
4049 surface->synchronized = 1;
4050}
4051
4052static void
4053surface_set_synchronized_default(struct surface *surface)
4054{
4055 if (!surface->subsurface)
4056 return;
4057
4058 if (surface->synchronized == surface->synchronized_default)
4059 return;
4060
4061 if (surface->synchronized_default)
4062 wl_subsurface_set_sync(surface->subsurface);
4063 else
4064 wl_subsurface_set_desync(surface->subsurface);
4065
4066 surface->synchronized = surface->synchronized_default;
4067}
4068
4069static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004070surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004071{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004072 struct widget *widget = surface->widget;
4073 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004074
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004075 if (surface->input_region) {
4076 wl_region_destroy(surface->input_region);
4077 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004078 }
4079
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004080 if (surface->opaque_region)
4081 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004082
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004083 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004084
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004085 if (widget->resize_handler)
4086 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004087 widget->allocation.width,
4088 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004089 widget->user_data);
4090
Pekka Paalanen35e82632013-04-25 13:57:48 +03004091 if (surface->subsurface &&
4092 (surface->allocation.x != widget->allocation.x ||
4093 surface->allocation.y != widget->allocation.y)) {
4094 wl_subsurface_set_position(surface->subsurface,
4095 widget->allocation.x,
4096 widget->allocation.y);
4097 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004098 if (surface->allocation.width != widget->allocation.width ||
4099 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004100 window_schedule_redraw(widget->window);
4101 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004102 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004103
4104 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004105 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004106 widget->allocation.width,
4107 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004108}
4109
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004110static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004111window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004112{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004113 struct surface *surface;
4114
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004115 widget_set_allocation(window->main_surface->widget,
4116 window->pending_allocation.x,
4117 window->pending_allocation.y,
4118 window->pending_allocation.width,
4119 window->pending_allocation.height);
4120
4121 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004122
4123 /* The main surface is in the list, too. Main surface's
4124 * resize_handler is responsible for calling widget_set_allocation()
4125 * on all sub-surface root widgets, so they will be resized
4126 * properly.
4127 */
4128 wl_list_for_each(surface, &window->subsurface_list, link) {
4129 if (surface == window->main_surface)
4130 continue;
4131
4132 surface_set_synchronized(surface);
4133 surface_resize(surface);
4134 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004135
4136 if (!window->fullscreen && !window->maximized)
4137 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004138
4139 if (window->confined && window->confined_widget) {
4140 struct wl_compositor *compositor = window->display->compositor;
4141 struct wl_region *region;
4142 struct widget *widget = window->confined_widget;
4143
4144 region = wl_compositor_create_region(compositor);
4145 wl_region_add(region,
4146 widget->allocation.x,
4147 widget->allocation.y,
4148 widget->allocation.width,
4149 widget->allocation.height);
4150 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4151 region);
4152 wl_region_destroy(region);
4153 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004154}
4155
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004156static void
4157idle_resize(struct window *window)
4158{
4159 window->resize_needed = 0;
4160 window->redraw_needed = 1;
4161
4162 DBG("from %dx%d to %dx%d\n",
4163 window->main_surface->server_allocation.width,
4164 window->main_surface->server_allocation.height,
4165 window->pending_allocation.width,
4166 window->pending_allocation.height);
4167
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004168 window_do_resize(window);
4169}
4170
4171static void
4172undo_resize(struct window *window)
4173{
4174 window->pending_allocation.width =
4175 window->main_surface->server_allocation.width;
4176 window->pending_allocation.height =
4177 window->main_surface->server_allocation.height;
4178
4179 DBG("back to %dx%d\n",
4180 window->main_surface->server_allocation.width,
4181 window->main_surface->server_allocation.height);
4182
4183 window_do_resize(window);
4184
4185 if (window->pending_allocation.width == 0 &&
4186 window->pending_allocation.height == 0) {
4187 fprintf(stderr, "Error: Could not draw a surface, "
4188 "most likely due to insufficient disk space in "
4189 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4190 exit(EXIT_FAILURE);
4191 }
4192}
4193
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004194void
4195window_schedule_resize(struct window *window, int width, int height)
4196{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004197 /* We should probably get these numbers from the theme. */
4198 const int min_width = 200, min_height = 200;
4199
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004200 window->pending_allocation.x = 0;
4201 window->pending_allocation.y = 0;
4202 window->pending_allocation.width = width;
4203 window->pending_allocation.height = height;
4204
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004205 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004206 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004207 window->min_allocation.width = min_width;
4208 else
4209 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004210 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004211 window->min_allocation.height = min_height;
4212 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004213 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004214 }
4215
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004216 if (window->pending_allocation.width < window->min_allocation.width)
4217 window->pending_allocation.width = window->min_allocation.width;
4218 if (window->pending_allocation.height < window->min_allocation.height)
4219 window->pending_allocation.height = window->min_allocation.height;
4220
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004221 window->resize_needed = 1;
4222 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004223}
4224
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004225void
4226widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4227{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004228 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004229}
4230
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004231static int
4232window_get_shadow_margin(struct window *window)
4233{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004234 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004235 return frame_get_shadow_margin(window->frame->frame);
4236 else
4237 return 0;
4238}
4239
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004240static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004241window_inhibit_redraw(struct window *window)
4242{
4243 window->redraw_inhibited = 1;
4244 wl_list_remove(&window->redraw_task.link);
4245 wl_list_init(&window->redraw_task.link);
4246 window->redraw_task_scheduled = 0;
4247}
4248
4249static void
4250window_uninhibit_redraw(struct window *window)
4251{
4252 window->redraw_inhibited = 0;
4253 if (window->redraw_needed || window->resize_needed)
4254 window_schedule_redraw_task(window);
4255}
4256
4257static void
4258xdg_surface_handle_configure(void *data,
4259 struct zxdg_surface_v6 *zxdg_surface_v6,
4260 uint32_t serial)
4261{
4262 struct window *window = data;
4263
4264 zxdg_surface_v6_ack_configure(window->xdg_surface, serial);
4265
4266 if (window->state_changed_handler)
4267 window->state_changed_handler(window, window->user_data);
4268
4269 window_uninhibit_redraw(window);
4270}
4271
4272static const struct zxdg_surface_v6_listener xdg_surface_listener = {
4273 xdg_surface_handle_configure
4274};
4275
4276static void
4277xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
4278 int32_t width, int32_t height,
4279 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004280{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004281 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004282 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004283
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004284 window->maximized = 0;
4285 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004286 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004287 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004288
4289 wl_array_for_each(p, states) {
4290 uint32_t state = *p;
4291 switch (state) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004292 case ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004293 window->maximized = 1;
4294 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004295 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004296 window->fullscreen = 1;
4297 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004298 case ZXDG_TOPLEVEL_V6_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004299 window->resizing = 1;
4300 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004301 case ZXDG_TOPLEVEL_V6_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004302 window->focused = 1;
4303 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004304 default:
4305 /* Unknown state */
4306 break;
4307 }
4308 }
4309
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004310 if (window->frame) {
4311 if (window->maximized) {
4312 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4313 } else {
4314 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4315 }
4316
4317 if (window->focused) {
4318 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4319 } else {
4320 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4321 }
4322 }
4323
Jasper St. Pierref184c382014-05-06 08:33:27 -04004324 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004325 /* The width / height params are for window geometry,
4326 * but window_schedule_resize takes allocation. Add
4327 * on the shadow margin to get the difference. */
4328 int margin = window_get_shadow_margin(window);
4329
4330 window_schedule_resize(window,
4331 width + margin * 2,
4332 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004333 } else if (window->saved_allocation.width > 0 &&
4334 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004335 window_schedule_resize(window,
4336 window->saved_allocation.width,
4337 window->saved_allocation.height);
4338 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004339}
4340
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004341static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004342xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004343{
4344 struct window *window = data;
4345 window_close(window);
4346}
4347
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004348static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
4349 xdg_toplevel_handle_configure,
4350 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004351};
4352
4353static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004354window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004355{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004356 struct zxdg_toplevel_v6 *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004357
4358 if (!window->xdg_surface)
4359 return;
4360
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004361 if (window->parent == window->last_parent)
4362 return;
4363
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004364 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004365 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004366 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004367 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004368
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004369 zxdg_toplevel_v6_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004370 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004371}
4372
4373static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004374window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004375{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004376 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004377 frame_input_rect(window->frame->frame,
4378 &geometry->x,
4379 &geometry->y,
4380 &geometry->width,
4381 &geometry->height);
4382 else
4383 window_get_allocation(window, geometry);
4384}
4385
4386static void
4387window_sync_geometry(struct window *window)
4388{
4389 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004390
4391 if (!window->xdg_surface)
4392 return;
4393
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004394 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004395 if (geometry.x == window->last_geometry.x &&
4396 geometry.y == window->last_geometry.y &&
4397 geometry.width == window->last_geometry.width &&
4398 geometry.height == window->last_geometry.height)
4399 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004400
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004401 zxdg_surface_v6_set_window_geometry(window->xdg_surface,
4402 geometry.x,
4403 geometry.y,
4404 geometry.width,
4405 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004406 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004407}
4408
4409static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004410window_flush(struct window *window)
4411{
4412 struct surface *surface;
4413
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004414 assert(!window->redraw_inhibited);
4415
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004416 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004417 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004418 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004419 if (window->xdg_toplevel)
4420 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004421 }
4422
4423 wl_list_for_each(surface, &window->subsurface_list, link) {
4424 if (surface == window->main_surface)
4425 continue;
4426
4427 surface_flush(surface);
4428 }
4429
4430 surface_flush(window->main_surface);
4431}
4432
4433static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004434menu_destroy(struct menu *menu)
4435{
4436 widget_destroy(menu->widget);
4437 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004438 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004439 free(menu);
4440}
4441
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004442void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004443window_get_allocation(struct window *window,
4444 struct rectangle *allocation)
4445{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004446 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004447}
4448
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004449static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004450widget_redraw(struct widget *widget)
4451{
4452 struct widget *child;
4453
4454 if (widget->redraw_handler)
4455 widget->redraw_handler(widget, widget->user_data);
4456 wl_list_for_each(child, &widget->child_list, link)
4457 widget_redraw(child);
4458}
4459
4460static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004461frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4462{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004463 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004464
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004465 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004466 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004467 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004468 surface->frame_cb = NULL;
4469
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004470 surface->last_time = time;
4471
Pekka Paalanen71233882013-04-25 13:57:53 +03004472 if (surface->redraw_needed || surface->window->redraw_needed) {
4473 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004474 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004475 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004476}
4477
4478static const struct wl_callback_listener listener = {
4479 frame_callback
4480};
4481
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004482static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004483surface_redraw(struct surface *surface)
4484{
Pekka Paalanen71233882013-04-25 13:57:53 +03004485 DBG_OBJ(surface->surface, "begin\n");
4486
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004487 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004488 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004489
4490 /* Whole-window redraw forces a redraw even if the previous has
4491 * not yet hit the screen.
4492 */
4493 if (surface->frame_cb) {
4494 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004495 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004496
Pekka Paalanen71233882013-04-25 13:57:53 +03004497 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004498 wl_callback_destroy(surface->frame_cb);
4499 }
4500
Neil Roberts97b747c2013-12-19 16:17:12 +00004501 if (surface->widget->use_cairo &&
4502 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004503 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004504 return -1;
4505 }
4506
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004507 surface->frame_cb = wl_surface_frame(surface->surface);
4508 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004509 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004510
4511 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004512 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004513 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004514 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004515 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004516}
4517
4518static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004519idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004520{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004521 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004522 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004523 int failed = 0;
4524 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004525
Pekka Paalanen71233882013-04-25 13:57:53 +03004526 DBG(" --------- \n");
4527
Pekka Paalaneneebff542013-04-25 13:57:52 +03004528 wl_list_init(&window->redraw_task.link);
4529 window->redraw_task_scheduled = 0;
4530
4531 if (window->resize_needed) {
4532 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004533 if (window->main_surface->frame_cb) {
4534 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004535 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004536 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004537
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004538 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004539 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004540 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004541
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004542 if (surface_redraw(window->main_surface) < 0) {
4543 /*
4544 * Only main_surface failure will cause us to undo the resize.
4545 * If sub-surfaces fail, they will just be broken with old
4546 * content.
4547 */
4548 failed = 1;
4549 } else {
4550 wl_list_for_each(surface, &window->subsurface_list, link) {
4551 if (surface == window->main_surface)
4552 continue;
4553
4554 surface_redraw(surface);
4555 }
4556 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004557
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004558 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004559 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004560
4561 wl_list_for_each(surface, &window->subsurface_list, link)
4562 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004563
4564 if (resized && failed) {
4565 /* Restore widget tree to correspond to what is on screen. */
4566 undo_resize(window);
4567 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004568}
4569
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004570static void
4571window_schedule_redraw_task(struct window *window)
4572{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004573 if (window->redraw_inhibited)
4574 return;
4575
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004576 if (!window->redraw_task_scheduled) {
4577 window->redraw_task.run = idle_redraw;
4578 display_defer(window->display, &window->redraw_task);
4579 window->redraw_task_scheduled = 1;
4580 }
4581}
4582
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004583void
4584window_schedule_redraw(struct window *window)
4585{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004586 struct surface *surface;
4587
Pekka Paalanen71233882013-04-25 13:57:53 +03004588 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4589
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004590 wl_list_for_each(surface, &window->subsurface_list, link)
4591 surface->redraw_needed = 1;
4592
4593 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004594}
4595
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004596int
4597window_is_fullscreen(struct window *window)
4598{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004599 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004600}
4601
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004602void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004603window_set_fullscreen(struct window *window, int fullscreen)
4604{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004605 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004606 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004607
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004608 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004609 return;
4610
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004611 if (fullscreen)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004612 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004613 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004614 zxdg_toplevel_v6_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004615}
4616
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004617int
4618window_is_maximized(struct window *window)
4619{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004620 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004621}
4622
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004623void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004624window_set_maximized(struct window *window, int maximized)
4625{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004626 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004627 return;
4628
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004629 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004630 return;
4631
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004632 if (maximized)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004633 zxdg_toplevel_v6_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004634 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004635 zxdg_toplevel_v6_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004636}
4637
Jasper St. Pierrede680992014-04-10 17:23:49 -07004638int
4639window_is_resizing(struct window *window)
4640{
4641 return window->resizing;
4642}
4643
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004644void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004645window_set_minimized(struct window *window)
4646{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004647 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004648 return;
4649
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004650 zxdg_toplevel_v6_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004651}
4652
4653void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004654window_set_user_data(struct window *window, void *data)
4655{
4656 window->user_data = data;
4657}
4658
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004659void *
4660window_get_user_data(struct window *window)
4661{
4662 return window->user_data;
4663}
4664
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004665void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004666window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004667 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004668{
4669 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004670}
4671
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004672void
4673window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004674 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004675{
4676 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004677}
4678
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004679void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004680window_set_data_handler(struct window *window, window_data_handler_t handler)
4681{
4682 window->data_handler = handler;
4683}
4684
4685void
4686window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4687{
4688 window->drop_handler = handler;
4689}
4690
4691void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004692window_set_close_handler(struct window *window,
4693 window_close_handler_t handler)
4694{
4695 window->close_handler = handler;
4696}
4697
4698void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004699window_set_fullscreen_handler(struct window *window,
4700 window_fullscreen_handler_t handler)
4701{
4702 window->fullscreen_handler = handler;
4703}
4704
4705void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004706window_set_output_handler(struct window *window,
4707 window_output_handler_t handler)
4708{
4709 window->output_handler = handler;
4710}
4711
4712void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004713window_set_state_changed_handler(struct window *window,
4714 window_state_changed_handler_t handler)
4715{
4716 window->state_changed_handler = handler;
4717}
4718
4719void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004720window_set_pointer_locked_handler(struct window *window,
4721 locked_pointer_locked_handler_t locked,
4722 locked_pointer_unlocked_handler_t unlocked)
4723{
4724 window->pointer_unlocked_handler = unlocked;
4725 window->pointer_locked_handler = locked;
4726}
4727
4728void
4729window_set_pointer_confined_handler(struct window *window,
4730 confined_pointer_confined_handler_t confined,
4731 confined_pointer_unconfined_handler_t unconfined)
4732{
4733 window->pointer_confined_handler = confined;
4734 window->pointer_unconfined_handler = unconfined;
4735}
4736
4737void
4738window_set_locked_pointer_motion_handler(struct window *window,
4739 window_locked_pointer_motion_handler_t handler)
4740{
4741 window->locked_pointer_motion_handler = handler;
4742}
4743
4744void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004745window_set_title(struct window *window, const char *title)
4746{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004747 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004748 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004749 if (window->frame) {
4750 frame_set_title(window->frame->frame, title);
4751 widget_schedule_redraw(window->frame->widget);
4752 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004753 if (window->xdg_toplevel)
4754 zxdg_toplevel_v6_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004755}
4756
4757const char *
4758window_get_title(struct window *window)
4759{
4760 return window->title;
4761}
4762
4763void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004764window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4765{
4766 struct text_cursor_position *text_cursor_position =
4767 window->display->text_cursor_position;
4768
Scott Moreau9295ce02012-06-01 12:46:10 -06004769 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004770 return;
4771
4772 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004773 window->main_surface->surface,
4774 wl_fixed_from_int(x),
4775 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004776}
4777
Casey Dahlin9074db52012-04-19 22:50:09 -04004778static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004779relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4780 uint32_t utime_hi,
4781 uint32_t utime_lo,
4782 wl_fixed_t dx,
4783 wl_fixed_t dy,
4784 wl_fixed_t dx_unaccel,
4785 wl_fixed_t dy_unaccel)
4786{
4787 struct input *input = data;
4788 struct window *window = input->pointer_focus;
4789 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4790
4791 if (window->locked_pointer_motion_handler &&
4792 window->pointer_locked) {
4793 window->locked_pointer_motion_handler(
4794 window, input, ms,
4795 wl_fixed_to_double(dx),
4796 wl_fixed_to_double(dy),
4797 window->user_data);
4798 }
4799}
4800
4801static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4802 relative_pointer_handle_motion,
4803};
4804
4805static void
4806locked_pointer_locked(void *data,
4807 struct zwp_locked_pointer_v1 *locked_pointer)
4808{
4809 struct input *input = data;
4810 struct window *window = input->pointer_focus;
4811
4812 window->pointer_locked = true;
4813
4814 if (window->pointer_locked_handler) {
4815 window->pointer_locked_handler(window,
4816 input,
4817 window->user_data);
4818 }
4819}
4820
4821static void
4822locked_pointer_unlocked(void *data,
4823 struct zwp_locked_pointer_v1 *locked_pointer)
4824{
4825 struct input *input = data;
4826 struct window *window = input->pointer_focus;
4827
4828 window_unlock_pointer(window);
4829
4830 if (window->pointer_unlocked_handler) {
4831 window->pointer_unlocked_handler(window,
4832 input,
4833 window->user_data);
4834 }
4835}
4836
4837static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4838 locked_pointer_locked,
4839 locked_pointer_unlocked,
4840};
4841
4842int
4843window_lock_pointer(struct window *window, struct input *input)
4844{
4845 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4846 window->display->relative_pointer_manager;
4847 struct zwp_pointer_constraints_v1 *pointer_constraints =
4848 window->display->pointer_constraints;
4849 struct zwp_relative_pointer_v1 *relative_pointer;
4850 struct zwp_locked_pointer_v1 *locked_pointer;
4851
4852 if (!window->display->relative_pointer_manager)
4853 return -1;
4854
4855 if (!window->display->pointer_constraints)
4856 return -1;
4857
4858 if (window->locked_pointer)
4859 return -1;
4860
4861 if (window->confined_pointer)
4862 return -1;
4863
4864 if (!input->pointer)
4865 return -1;
4866
4867 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4868 relative_pointer_manager, input->pointer);
4869 zwp_relative_pointer_v1_add_listener(relative_pointer,
4870 &relative_pointer_listener,
4871 input);
4872
4873 locked_pointer =
4874 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4875 window->main_surface->surface,
4876 input->pointer,
4877 NULL,
4878 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4879 zwp_locked_pointer_v1_add_listener(locked_pointer,
4880 &locked_pointer_listener,
4881 input);
4882
4883 window->locked_input = input;
4884 window->locked_pointer = locked_pointer;
4885 window->relative_pointer = relative_pointer;
4886
4887 return 0;
4888}
4889
4890void
4891window_unlock_pointer(struct window *window)
4892{
4893 if (!window->locked_pointer)
4894 return;
4895
4896 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4897 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4898 window->locked_pointer = NULL;
4899 window->relative_pointer = NULL;
4900 window->pointer_locked = false;
4901 window->locked_input = NULL;
4902}
4903
4904void
4905widget_set_locked_pointer_cursor_hint(struct widget *widget,
4906 float x, float y)
4907{
4908 struct window *window = widget->window;
4909
4910 if (!window->locked_pointer)
4911 return;
4912
4913 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4914 wl_fixed_from_double(x),
4915 wl_fixed_from_double(y));
4916 wl_surface_commit(window->main_surface->surface);
4917}
4918
4919static void
4920confined_pointer_confined(void *data,
4921 struct zwp_confined_pointer_v1 *confined_pointer)
4922{
4923 struct input *input = data;
4924 struct window *window = input->pointer_focus;
4925
4926 window->confined = true;
4927
4928 if (window->pointer_confined_handler) {
4929 window->pointer_confined_handler(window,
4930 input,
4931 window->user_data);
4932 }
4933}
4934
4935static void
4936confined_pointer_unconfined(void *data,
4937 struct zwp_confined_pointer_v1 *confined_pointer)
4938{
4939 struct input *input = data;
4940 struct window *window = input->pointer_focus;
4941
4942 window_unconfine_pointer(window);
4943
4944 window->confined = false;
4945
4946 if (window->pointer_unconfined_handler) {
4947 window->pointer_unconfined_handler(window,
4948 input,
4949 window->user_data);
4950 }
4951}
4952
4953static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4954 confined_pointer_confined,
4955 confined_pointer_unconfined,
4956};
4957
4958int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004959window_confine_pointer_to_rectangles(struct window *window,
4960 struct input *input,
4961 struct rectangle *rectangles,
4962 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004963{
4964 struct zwp_pointer_constraints_v1 *pointer_constraints =
4965 window->display->pointer_constraints;
4966 struct zwp_confined_pointer_v1 *confined_pointer;
4967 struct wl_compositor *compositor = window->display->compositor;
4968 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004969 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004970
4971 if (!window->display->pointer_constraints)
4972 return -1;
4973
4974 if (window->locked_pointer)
4975 return -1;
4976
4977 if (window->confined_pointer)
4978 return -1;
4979
4980 if (!input->pointer)
4981 return -1;
4982
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004983 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004984 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004985 for (i = 0; i < num_rectangles; i++) {
4986 wl_region_add(region,
4987 rectangles[i].x,
4988 rectangles[i].y,
4989 rectangles[i].width,
4990 rectangles[i].height);
4991 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004992 }
4993
4994 confined_pointer =
4995 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
4996 window->main_surface->surface,
4997 input->pointer,
4998 region,
4999 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5000 if (region)
5001 wl_region_destroy(region);
5002
5003 zwp_confined_pointer_v1_add_listener(confined_pointer,
5004 &confined_pointer_listener,
5005 input);
5006
5007 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005008 window->confined_widget = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005009
5010 return 0;
5011}
5012
5013void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005014window_update_confine_rectangles(struct window *window,
5015 struct rectangle *rectangles,
5016 int num_rectangles)
5017{
5018 struct wl_compositor *compositor = window->display->compositor;
5019 struct wl_region *region;
5020 int i;
5021
5022 region = wl_compositor_create_region(compositor);
5023 for (i = 0; i < num_rectangles; i++) {
5024 wl_region_add(region,
5025 rectangles[i].x,
5026 rectangles[i].y,
5027 rectangles[i].width,
5028 rectangles[i].height);
5029 }
5030
5031 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5032
5033 wl_region_destroy(region);
5034}
5035
5036int
5037window_confine_pointer_to_widget(struct window *window,
5038 struct widget *widget,
5039 struct input *input)
5040{
5041 int ret;
5042
5043 if (widget) {
5044 ret = window_confine_pointer_to_rectangles(window,
5045 input,
5046 &widget->allocation,
5047 1);
5048 window->confined_widget = widget;
5049 return ret;
5050 } else {
5051 return window_confine_pointer_to_rectangles(window,
5052 input,
5053 NULL,
5054 0);
5055 }
5056}
5057
5058void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005059window_unconfine_pointer(struct window *window)
5060{
5061 if (!window->confined_pointer)
5062 return;
5063
5064 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5065 window->confined_pointer = NULL;
5066 window->confined = false;
5067}
5068
5069static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005070surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005071 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005072{
Rob Bradford7507b572012-05-15 17:55:34 +01005073 struct window *window = data;
5074 struct output *output;
5075 struct output *output_found = NULL;
5076 struct window_output *window_output;
5077
5078 wl_list_for_each(output, &window->display->output_list, link) {
5079 if (output->output == wl_output) {
5080 output_found = output;
5081 break;
5082 }
5083 }
5084
5085 if (!output_found)
5086 return;
5087
Brian Lovinbc919262013-08-07 15:34:59 -07005088 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005089 window_output->output = output_found;
5090
5091 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005092
5093 if (window->output_handler)
5094 window->output_handler(window, output_found, 1,
5095 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005096}
5097
5098static void
5099surface_leave(void *data,
5100 struct wl_surface *wl_surface, struct wl_output *output)
5101{
Rob Bradford7507b572012-05-15 17:55:34 +01005102 struct window *window = data;
5103 struct window_output *window_output;
5104 struct window_output *window_output_found = NULL;
5105
5106 wl_list_for_each(window_output, &window->window_output_list, link) {
5107 if (window_output->output->output == output) {
5108 window_output_found = window_output;
5109 break;
5110 }
5111 }
5112
5113 if (window_output_found) {
5114 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005115
5116 if (window->output_handler)
5117 window->output_handler(window, window_output->output,
5118 0, window->user_data);
5119
Rob Bradford7507b572012-05-15 17:55:34 +01005120 free(window_output_found);
5121 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005122}
5123
5124static const struct wl_surface_listener surface_listener = {
5125 surface_enter,
5126 surface_leave
5127};
5128
Pekka Paalanen4e373742013-02-13 16:17:13 +02005129static struct surface *
5130surface_create(struct window *window)
5131{
5132 struct display *display = window->display;
5133 struct surface *surface;
5134
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005135 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005136 surface->window = window;
5137 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005138 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005139 wl_surface_add_listener(surface->surface, &surface_listener, window);
5140
Pekka Paalanen35e82632013-04-25 13:57:48 +03005141 wl_list_insert(&window->subsurface_list, &surface->link);
5142
Pekka Paalanen4e373742013-02-13 16:17:13 +02005143 return surface;
5144}
5145
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005146static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005147get_preferred_buffer_type(struct display *display)
5148{
5149#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005150 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005151 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5152#endif
5153
5154 return WINDOW_BUFFER_TYPE_SHM;
5155}
5156
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005157static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005158window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005159{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005160 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005161 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005162
Peter Huttererf3d62272013-08-08 11:57:05 +10005163 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005164 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005165 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005166
5167 surface = surface_create(window);
5168 window->main_surface = surface;
5169
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005170 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005171
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005172 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005173 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005174
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005175 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005176
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005177 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005178 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005179 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005180
Rob Bradford7507b572012-05-15 17:55:34 +01005181 wl_list_init (&window->window_output_list);
5182
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005183 return window;
5184}
5185
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005186struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005187window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005188{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005189 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005190 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005191
5192 window = window_create_internal(display, 0);
5193
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005194 if (window->display->xdg_shell) {
5195 window->xdg_surface =
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005196 zxdg_shell_v6_get_xdg_surface(window->display->xdg_shell,
5197 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005198 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005199
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005200 zxdg_surface_v6_add_listener(window->xdg_surface,
5201 &xdg_surface_listener, window);
5202
5203 window->xdg_toplevel =
5204 zxdg_surface_v6_get_toplevel(window->xdg_surface);
5205 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5206
5207 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
5208 &xdg_toplevel_listener, window);
5209
5210 window_inhibit_redraw(window);
5211
5212 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005213 } else if (display->ivi_application) {
5214 /* auto generation of ivi_id based on process id + basement of id */
5215 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5216 window->ivi_surface =
5217 ivi_application_surface_create(display->ivi_application,
5218 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005219 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005220
5221 ivi_surface_add_listener(window->ivi_surface,
5222 &ivi_surface_listener, window);
5223 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005224
5225 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005226}
5227
5228struct window *
5229window_create_custom(struct display *display)
5230{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005231 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005232}
5233
Jasper St. Pierre53686042013-12-09 15:26:25 -05005234void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005235window_set_parent(struct window *window,
5236 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005237{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005238 window->parent = parent_window;
5239 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005240}
5241
5242struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005243window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005244{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005245 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005246}
5247
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005248static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005249menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005250{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005251 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005252 int next;
5253
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005254 frame_interior(menu->frame, &x, &y, &width, &height);
5255 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005256 if (menu->current != next) {
5257 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005258 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005259 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005260}
5261
5262static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005263menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005264 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005265 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005266{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005267 struct menu *menu = data;
5268
5269 if (widget == menu->widget)
5270 menu_set_item(data, y);
5271
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005272 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005273}
5274
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005275static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005276menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005277 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005278{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005279 struct menu *menu = data;
5280
5281 if (widget == menu->widget)
5282 menu_set_item(data, y);
5283
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005284 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005285}
5286
5287static void
5288menu_leave_handler(struct widget *widget, struct input *input, void *data)
5289{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005290 struct menu *menu = data;
5291
5292 if (widget == menu->widget)
5293 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005294}
5295
5296static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005297menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005298 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005299 uint32_t button, enum wl_pointer_button_state state,
5300 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005301
5302{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005303 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005304
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005305 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5306 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005307 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005308 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005309 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005310 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005311 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005312 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005313 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005314 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005315}
5316
5317static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005318menu_touch_up_handler(struct widget *widget,
5319 struct input *input,
5320 uint32_t serial,
5321 uint32_t time,
5322 int32_t id,
5323 void *data)
5324{
5325 struct menu *menu = data;
5326
5327 input_ungrab(input);
5328 menu_destroy(menu);
5329}
5330
5331static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005332menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005333{
5334 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005335 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005336 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005337
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005338 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005339
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005340 frame_repaint(menu->frame, cr);
5341 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005342
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005343 theme_set_background_source(menu->window->display->theme,
5344 cr, THEME_FRAME_ACTIVE);
5345 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005346 cairo_fill(cr);
5347
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005348 cairo_select_font_face(cr, "sans",
5349 CAIRO_FONT_SLANT_NORMAL,
5350 CAIRO_FONT_WEIGHT_NORMAL);
5351 cairo_set_font_size(cr, 12);
5352
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005353 for (i = 0; i < menu->count; i++) {
5354 if (i == menu->current) {
5355 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005356 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005357 cairo_fill(cr);
5358 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005359 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005360 cairo_show_text(cr, menu->entries[i]);
5361 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005362 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5363 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005364 cairo_show_text(cr, menu->entries[i]);
5365 }
5366 }
5367
5368 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005369}
5370
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005371static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005372xdg_popup_handle_configure(void *data,
5373 struct zxdg_popup_v6 *zxdg_popup_v6,
5374 int32_t x,
5375 int32_t y,
5376 int32_t width,
5377 int32_t height)
5378{
5379}
5380
5381static void
5382xdg_popup_handle_popup_done(void *data, struct zxdg_popup_v6 *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005383{
5384 struct window *window = data;
5385 struct menu *menu = window->main_surface->widget->user_data;
5386
5387 input_ungrab(menu->input);
5388 menu_destroy(menu);
5389}
5390
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005391static const struct zxdg_popup_v6_listener xdg_popup_listener = {
5392 xdg_popup_handle_configure,
5393 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005394};
5395
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005396static struct menu *
5397create_menu(struct display *display,
5398 struct input *input, uint32_t time,
5399 menu_func_t func, const char **entries, int count,
5400 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005401{
5402 struct window *window;
5403 struct menu *menu;
5404
5405 menu = malloc(sizeof *menu);
5406 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005407 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005408
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005409 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005410 if (!window) {
5411 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005412 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005413 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005414
5415 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005416 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005417 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005418 menu->frame = frame_create(window->display->theme, 0, 0,
Emmanuel Gil Peyrot6b58ea82017-12-01 19:20:40 +01005419 FRAME_BUTTON_NONE, NULL, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005420 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005421 menu->entries = entries;
5422 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005423 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005424 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005425 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005426 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005427 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005428
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005429 input_ungrab(input);
5430
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005431 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005432 widget_set_enter_handler(menu->widget, menu_enter_handler);
5433 widget_set_leave_handler(menu->widget, menu_leave_handler);
5434 widget_set_motion_handler(menu->widget, menu_motion_handler);
5435 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005436 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005437
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005438 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005439 frame_resize_inside(menu->frame, 200, count * 20);
5440 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5441 window_schedule_resize(window, frame_width(menu->frame),
5442 frame_height(menu->frame));
5443
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005444 return menu;
5445}
5446
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005447static struct zxdg_positioner_v6 *
5448create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005449 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005450{
5451 struct zxdg_positioner_v6 *positioner;
5452
5453 positioner = zxdg_shell_v6_create_positioner(display->xdg_shell);
5454 fail_on_null(positioner, 0, __FILE__, __LINE__);
5455 zxdg_positioner_v6_set_anchor_rect(positioner, x, y, 1, 1);
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005456 zxdg_positioner_v6_set_size(positioner, w, h);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005457 zxdg_positioner_v6_set_anchor(positioner,
5458 ZXDG_POSITIONER_V6_ANCHOR_TOP |
5459 ZXDG_POSITIONER_V6_ANCHOR_LEFT);
5460 zxdg_positioner_v6_set_gravity(positioner,
5461 ZXDG_POSITIONER_V6_ANCHOR_BOTTOM |
5462 ZXDG_POSITIONER_V6_ANCHOR_RIGHT);
5463
5464 return positioner;
5465}
5466
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005467void
5468window_show_menu(struct display *display,
5469 struct input *input, uint32_t time, struct window *parent,
5470 int32_t x, int32_t y,
5471 menu_func_t func, const char **entries, int count)
5472{
5473 struct menu *menu;
5474 struct window *window;
5475 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005476 struct rectangle parent_geometry;
5477 struct zxdg_positioner_v6 *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005478
5479 menu = create_menu(display, input, time, func, entries, count, parent);
5480
5481 if (menu == NULL)
5482 return;
5483
5484 window = menu->window;
5485
5486 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5487 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5488
5489 window->x = x;
5490 window->y = y;
5491
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005492 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005493 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005494
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005495 if (!display->xdg_shell)
5496 return;
5497
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005498 window->xdg_surface =
5499 zxdg_shell_v6_get_xdg_surface(display->xdg_shell,
5500 window->main_surface->surface);
5501 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005502
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005503 zxdg_surface_v6_add_listener(window->xdg_surface,
5504 &xdg_surface_listener, window);
5505
5506 positioner = create_simple_positioner(display,
5507 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005508 window->y - (iy + parent_geometry.y),
5509 frame_width(menu->frame),
5510 frame_height(menu->frame));
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005511 window->xdg_popup =
5512 zxdg_surface_v6_get_popup(window->xdg_surface,
5513 parent->xdg_surface,
5514 positioner);
5515 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
5516 zxdg_positioner_v6_destroy(positioner);
5517 zxdg_popup_v6_grab(window->xdg_popup,
5518 input->seat,
5519 display_get_serial(window->display));
5520 zxdg_popup_v6_add_listener(window->xdg_popup,
5521 &xdg_popup_listener, window);
5522
5523 window_inhibit_redraw(window);
5524
5525 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005526}
5527
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005528void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005529window_set_buffer_type(struct window *window, enum window_buffer_type type)
5530{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005531 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005532}
5533
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005534enum window_buffer_type
5535window_get_buffer_type(struct window *window)
5536{
5537 return window->main_surface->buffer_type;
5538}
5539
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005540void
5541window_set_preferred_format(struct window *window,
5542 enum preferred_format format)
5543{
5544 window->preferred_format = format;
5545}
5546
Pekka Paalanen35e82632013-04-25 13:57:48 +03005547struct widget *
5548window_add_subsurface(struct window *window, void *data,
5549 enum subsurface_mode default_mode)
5550{
5551 struct widget *widget;
5552 struct surface *surface;
5553 struct wl_surface *parent;
5554 struct wl_subcompositor *subcompo = window->display->subcompositor;
5555
Pekka Paalanen35e82632013-04-25 13:57:48 +03005556 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005557 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005558 widget = widget_create(window, surface, data);
5559 wl_list_init(&widget->link);
5560 surface->widget = widget;
5561
5562 parent = window->main_surface->surface;
5563 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5564 surface->surface,
5565 parent);
5566 surface->synchronized = 1;
5567
5568 switch (default_mode) {
5569 case SUBSURFACE_SYNCHRONIZED:
5570 surface->synchronized_default = 1;
5571 break;
5572 case SUBSURFACE_DESYNCHRONIZED:
5573 surface->synchronized_default = 0;
5574 break;
5575 default:
5576 assert(!"bad enum subsurface_mode");
5577 }
5578
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005579 window->resize_needed = 1;
5580 window_schedule_redraw(window);
5581
Pekka Paalanen35e82632013-04-25 13:57:48 +03005582 return widget;
5583}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005584
5585static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005586display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005587 struct wl_output *wl_output,
5588 int x, int y,
5589 int physical_width,
5590 int physical_height,
5591 int subpixel,
5592 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005593 const char *model,
5594 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005595{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005596 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005597
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005598 output->allocation.x = x;
5599 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005600 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005601
5602 if (output->make)
5603 free(output->make);
5604 output->make = strdup(make);
5605
5606 if (output->model)
5607 free(output->model);
5608 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005609}
5610
5611static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005612display_handle_done(void *data,
5613 struct wl_output *wl_output)
5614{
5615}
5616
5617static void
5618display_handle_scale(void *data,
5619 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005620 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005621{
5622 struct output *output = data;
5623
5624 output->scale = scale;
5625}
5626
5627static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005628display_handle_mode(void *data,
5629 struct wl_output *wl_output,
5630 uint32_t flags,
5631 int width,
5632 int height,
5633 int refresh)
5634{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005635 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005636 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005637
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005638 if (flags & WL_OUTPUT_MODE_CURRENT) {
5639 output->allocation.width = width;
5640 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005641 if (display->output_configure_handler)
5642 (*display->output_configure_handler)(
5643 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005644 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005645}
5646
5647static const struct wl_output_listener output_listener = {
5648 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005649 display_handle_mode,
5650 display_handle_done,
5651 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005652};
5653
5654static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005655display_add_output(struct display *d, uint32_t id)
5656{
5657 struct output *output;
5658
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005659 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005660 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005661 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005662 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005663 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005664 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005665 wl_list_insert(d->output_list.prev, &output->link);
5666
5667 wl_output_add_listener(output->output, &output_listener, output);
5668}
5669
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005670static void
5671output_destroy(struct output *output)
5672{
5673 if (output->destroy_handler)
5674 (*output->destroy_handler)(output, output->user_data);
5675
5676 wl_output_destroy(output->output);
5677 wl_list_remove(&output->link);
5678 free(output);
5679}
5680
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005681static void
5682display_destroy_output(struct display *d, uint32_t id)
5683{
5684 struct output *output;
5685
5686 wl_list_for_each(output, &d->output_list, link) {
5687 if (output->server_output_id == id) {
5688 output_destroy(output);
5689 break;
5690 }
5691 }
5692}
5693
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005694void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005695display_set_global_handler(struct display *display,
5696 display_global_handler_t handler)
5697{
5698 struct global *global;
5699
5700 display->global_handler = handler;
5701 if (!handler)
5702 return;
5703
5704 wl_list_for_each(global, &display->global_list, link)
5705 display->global_handler(display,
5706 global->name, global->interface,
5707 global->version, display->user_data);
5708}
5709
5710void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005711display_set_global_handler_remove(struct display *display,
5712 display_global_handler_t remove_handler)
5713{
5714 display->global_handler_remove = remove_handler;
5715 if (!remove_handler)
5716 return;
5717}
5718
5719void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005720display_set_output_configure_handler(struct display *display,
5721 display_output_handler_t handler)
5722{
5723 struct output *output;
5724
5725 display->output_configure_handler = handler;
5726 if (!handler)
5727 return;
5728
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005729 wl_list_for_each(output, &display->output_list, link) {
5730 if (output->allocation.width == 0 &&
5731 output->allocation.height == 0)
5732 continue;
5733
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005734 (*display->output_configure_handler)(output,
5735 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005736 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005737}
5738
5739void
5740output_set_user_data(struct output *output, void *data)
5741{
5742 output->user_data = data;
5743}
5744
5745void *
5746output_get_user_data(struct output *output)
5747{
5748 return output->user_data;
5749}
5750
5751void
5752output_set_destroy_handler(struct output *output,
5753 display_output_handler_t handler)
5754{
5755 output->destroy_handler = handler;
5756 /* FIXME: implement this, once we have way to remove outputs */
5757}
5758
5759void
Scott Moreau4e072362012-09-29 02:03:11 -06005760output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005761{
Scott Moreau4e072362012-09-29 02:03:11 -06005762 struct rectangle allocation = output->allocation;
5763
5764 switch (output->transform) {
5765 case WL_OUTPUT_TRANSFORM_90:
5766 case WL_OUTPUT_TRANSFORM_270:
5767 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5768 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5769 /* Swap width and height */
5770 allocation.width = output->allocation.height;
5771 allocation.height = output->allocation.width;
5772 break;
5773 }
5774
5775 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005776}
5777
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005778struct wl_output *
5779output_get_wl_output(struct output *output)
5780{
5781 return output->output;
5782}
5783
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005784enum wl_output_transform
5785output_get_transform(struct output *output)
5786{
5787 return output->transform;
5788}
5789
Alexander Larssonafd319a2013-05-22 14:41:27 +02005790uint32_t
5791output_get_scale(struct output *output)
5792{
5793 return output->scale;
5794}
5795
Jason Ekstrand738715d2014-04-02 19:53:50 -05005796const char *
5797output_get_make(struct output *output)
5798{
5799 return output->make;
5800}
5801
5802const char *
5803output_get_model(struct output *output)
5804{
5805 return output->model;
5806}
5807
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005808static void
Daniel Stone97f68542012-05-30 16:32:01 +01005809fini_xkb(struct input *input)
5810{
5811 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005812 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005813}
5814
5815static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005816display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005817{
5818 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005819 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005820
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005821 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005822 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005823 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005824 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005825 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005826 input->pointer_focus = NULL;
5827 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005828 input->seat_version = seat_version;
5829
Rusty Lynch041815a2013-08-08 21:20:38 -07005830 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005831 wl_list_insert(d->input_list.prev, &input->link);
5832
Daniel Stone37816df2012-05-16 18:45:18 +01005833 wl_seat_add_listener(input->seat, &seat_listener, input);
5834 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005835
Jason Ekstranda669bd52014-04-02 19:53:51 -05005836 if (d->data_device_manager) {
5837 input->data_device =
5838 wl_data_device_manager_get_data_device(d->data_device_manager,
5839 input->seat);
5840 wl_data_device_add_listener(input->data_device,
5841 &data_device_listener,
5842 input);
5843 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005844
5845 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005846
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005847 toytimer_init(&input->cursor_timer, CLOCK_MONOTONIC, d,
5848 cursor_timer_func);
Jonny Lamb06959082014-08-12 14:58:27 +02005849 set_repeat_info(input, 40, 400);
5850
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005851 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5852 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005853 input->repeat_task.run = keyboard_repeat_func;
5854 display_watch_fd(d, input->repeat_timer_fd,
5855 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005856}
5857
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005858static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005859input_destroy(struct input *input)
5860{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005861 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005862 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005863
5864 if (input->drag_offer)
5865 data_offer_destroy(input->drag_offer);
5866
5867 if (input->selection_offer)
5868 data_offer_destroy(input->selection_offer);
5869
kabeer khan6ce67ec2014-10-20 11:55:29 +05305870 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005871 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305872 wl_data_device_release(input->data_device);
5873 else
5874 wl_data_device_destroy(input->data_device);
5875 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005876 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005877 if (input->touch)
5878 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005879 if (input->pointer)
5880 wl_pointer_release(input->pointer);
5881 if (input->keyboard)
5882 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005883 } else {
5884 if (input->touch)
5885 wl_touch_destroy(input->touch);
5886 if (input->pointer)
5887 wl_pointer_destroy(input->pointer);
5888 if (input->keyboard)
5889 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005890 }
5891
Daniel Stone97f68542012-05-30 16:32:01 +01005892 fini_xkb(input);
5893
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005894 wl_surface_destroy(input->pointer_surface);
5895
Pekka Paalanene1207c72011-12-16 12:02:09 +02005896 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005897 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005898 close(input->repeat_timer_fd);
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02005899 toytimer_fini(&input->cursor_timer);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005900 free(input);
5901}
5902
5903static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005904shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5905{
5906 struct display *d = data;
5907
5908 if (format == WL_SHM_FORMAT_RGB565)
5909 d->has_rgb565 = 1;
5910}
5911
5912struct wl_shm_listener shm_listener = {
5913 shm_format
5914};
5915
5916static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005917xdg_shell_handle_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005918{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005919 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005920}
5921
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005922static const struct zxdg_shell_v6_listener xdg_shell_listener = {
5923 xdg_shell_handle_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005924};
5925
5926static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005927registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5928 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005929{
5930 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005931 struct global *global;
5932
Brian Lovinbc919262013-08-07 15:34:59 -07005933 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005934 global->name = id;
5935 global->interface = strdup(interface);
5936 global->version = version;
5937 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005938
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005939 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005940 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005941 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005942 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005943 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005944 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005945 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005946 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5947 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5948 d->relative_pointer_manager =
5949 wl_registry_bind(registry, id,
5950 &zwp_relative_pointer_manager_v1_interface,
5951 1);
5952 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5953 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5954 d->pointer_constraints =
5955 wl_registry_bind(registry, id,
5956 &zwp_pointer_constraints_v1_interface,
5957 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005958 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005959 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005960 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005961 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005962 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005963 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005964 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305965 &wl_data_device_manager_interface,
5966 d->data_device_manager_version);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005967 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005968 d->xdg_shell = wl_registry_bind(registry, id,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005969 &zxdg_shell_v6_interface, 1);
5970 zxdg_shell_v6_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005971 } else if (strcmp(interface, "text_cursor_position") == 0) {
5972 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005973 wl_registry_bind(registry, id,
5974 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005975 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5976 d->subcompositor =
5977 wl_registry_bind(registry, id,
5978 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005979 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005980 else if (strcmp(interface, "ivi_application") == 0) {
5981 d->ivi_application =
5982 wl_registry_bind(registry, id,
5983 &ivi_application_interface, 1);
5984 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005985
5986 if (d->global_handler)
5987 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005988}
5989
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005990static void
5991registry_handle_global_remove(void *data, struct wl_registry *registry,
5992 uint32_t name)
5993{
5994 struct display *d = data;
5995 struct global *global;
5996 struct global *tmp;
5997
5998 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5999 if (global->name != name)
6000 continue;
6001
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006002 if (strcmp(global->interface, "wl_output") == 0)
6003 display_destroy_output(d, name);
6004
6005 /* XXX: Should destroy remaining bound globals */
6006
6007 if (d->global_handler_remove)
6008 d->global_handler_remove(d, name, global->interface,
6009 global->version, d->user_data);
6010
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006011 wl_list_remove(&global->link);
6012 free(global->interface);
6013 free(global);
6014 }
6015}
6016
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006017void *
6018display_bind(struct display *display, uint32_t name,
6019 const struct wl_interface *interface, uint32_t version)
6020{
6021 return wl_registry_bind(display->registry, name, interface, version);
6022}
6023
6024static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006025 registry_handle_global,
6026 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006027};
6028
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006029#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006030static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006031init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006032{
6033 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006034 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006035
Rob Clark6396ed32012-03-11 19:48:41 -05006036#ifdef USE_CAIRO_GLESV2
6037# define GL_BIT EGL_OPENGL_ES2_BIT
6038#else
6039# define GL_BIT EGL_OPENGL_BIT
6040#endif
6041
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006042 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006043 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006044 EGL_RED_SIZE, 1,
6045 EGL_GREEN_SIZE, 1,
6046 EGL_BLUE_SIZE, 1,
6047 EGL_ALPHA_SIZE, 1,
6048 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006049 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006050 EGL_NONE
6051 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006052
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006053#ifdef USE_CAIRO_GLESV2
6054 static const EGLint context_attribs[] = {
6055 EGL_CONTEXT_CLIENT_VERSION, 2,
6056 EGL_NONE
6057 };
6058 EGLint api = EGL_OPENGL_ES_API;
6059#else
6060 EGLint *context_attribs = NULL;
6061 EGLint api = EGL_OPENGL_API;
6062#endif
6063
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006064 d->dpy =
6065 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6066 d->display, NULL);
6067
Yuval Fledel45568f62010-12-06 09:18:12 -05006068 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006069 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006070 return -1;
6071 }
6072
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006073 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006074 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006075 return -1;
6076 }
6077
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006078 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6079 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006080 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006081 return -1;
6082 }
6083
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006084 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006085 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006086 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006087 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006088 return -1;
6089 }
6090
Benjamin Franzke0c991632011-09-27 21:57:31 +02006091 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6092 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006093 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006094 return -1;
6095 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006096
6097 return 0;
6098}
6099
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006100static void
6101fini_egl(struct display *display)
6102{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006103 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006104
6105 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6106 EGL_NO_CONTEXT);
6107
6108 eglTerminate(display->dpy);
6109 eglReleaseThread();
6110}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006111#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006112
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006113static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006114init_dummy_surface(struct display *display)
6115{
6116 int len;
6117 void *data;
6118
6119 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006120 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006121 display->dummy_surface =
6122 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6123 1, 1, len);
6124 display->dummy_surface_data = data;
6125}
6126
6127static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006128handle_display_data(struct task *task, uint32_t events)
6129{
6130 struct display *display =
6131 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006132 struct epoll_event ep;
6133 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006134
6135 display->display_fd_events = events;
6136
6137 if (events & EPOLLERR || events & EPOLLHUP) {
6138 display_exit(display);
6139 return;
6140 }
6141
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006142 if (events & EPOLLIN) {
6143 ret = wl_display_dispatch(display->display);
6144 if (ret == -1) {
6145 display_exit(display);
6146 return;
6147 }
6148 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006149
6150 if (events & EPOLLOUT) {
6151 ret = wl_display_flush(display->display);
6152 if (ret == 0) {
6153 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6154 ep.data.ptr = &display->display_task;
6155 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6156 display->display_fd, &ep);
6157 } else if (ret == -1 && errno != EAGAIN) {
6158 display_exit(display);
6159 return;
6160 }
6161 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006162}
6163
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006164static void
6165log_handler(const char *format, va_list args)
6166{
6167 vfprintf(stderr, format, args);
6168}
6169
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006170struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006171display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006172{
6173 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006174
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006175 wl_log_set_handler_client(log_handler);
6176
Peter Huttererf3d62272013-08-08 11:57:05 +10006177 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006178 if (d == NULL)
6179 return NULL;
6180
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006181 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006182 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006183 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006184 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006185 return NULL;
6186 }
6187
Rob Bradford5ab9c752013-07-26 16:29:43 +01006188 d->xkb_context = xkb_context_new(0);
6189 if (d->xkb_context == NULL) {
6190 fprintf(stderr, "Failed to create XKB context\n");
6191 free(d);
6192 return NULL;
6193 }
6194
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006195 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006196 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006197 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006198 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6199 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006200
6201 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006202 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006203 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006204 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006205
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006206 d->registry = wl_display_get_registry(d->display);
6207 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006208
Marek Chalupaa519d062014-12-05 13:49:40 +01006209 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006210 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6211 return NULL;
6212 }
6213
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006214#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006215 if (init_egl(d) < 0)
6216 fprintf(stderr, "EGL does not seem to work, "
6217 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006218#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006219
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006220 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006221
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006222 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006223
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006224 wl_list_init(&d->window_list);
6225
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006226 init_dummy_surface(d);
6227
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006228 return d;
6229}
6230
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006231static void
6232display_destroy_outputs(struct display *display)
6233{
6234 struct output *tmp;
6235 struct output *output;
6236
6237 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6238 output_destroy(output);
6239}
6240
Pekka Paalanene1207c72011-12-16 12:02:09 +02006241static void
6242display_destroy_inputs(struct display *display)
6243{
6244 struct input *tmp;
6245 struct input *input;
6246
6247 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6248 input_destroy(input);
6249}
6250
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006251void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006252display_destroy(struct display *display)
6253{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006254 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006255 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6256 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006257
6258 if (!wl_list_empty(&display->deferred_list))
6259 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6260
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006261 cairo_surface_destroy(display->dummy_surface);
6262 free(display->dummy_surface_data);
6263
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006264 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006265 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006266
Daniel Stone97f68542012-05-30 16:32:01 +01006267 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006268
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006269 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006270 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006271
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006272#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006273 if (display->argb_device)
6274 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006275#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006276
Pekka Paalanen35e82632013-04-25 13:57:48 +03006277 if (display->subcompositor)
6278 wl_subcompositor_destroy(display->subcompositor);
6279
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006280 if (display->xdg_shell)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006281 zxdg_shell_v6_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006282
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006283 if (display->ivi_application)
6284 ivi_application_destroy(display->ivi_application);
6285
Pekka Paalanenc2052982011-12-16 11:41:32 +02006286 if (display->shm)
6287 wl_shm_destroy(display->shm);
6288
6289 if (display->data_device_manager)
6290 wl_data_device_manager_destroy(display->data_device_manager);
6291
6292 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006293 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006294
6295 close(display->epoll_fd);
6296
U. Artie Eoff44874d92012-10-02 21:12:35 -07006297 if (!(display->display_fd_events & EPOLLERR) &&
6298 !(display->display_fd_events & EPOLLHUP))
6299 wl_display_flush(display->display);
6300
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006301 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006302 free(display);
6303}
6304
6305void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006306display_set_user_data(struct display *display, void *data)
6307{
6308 display->user_data = data;
6309}
6310
6311void *
6312display_get_user_data(struct display *display)
6313{
6314 return display->user_data;
6315}
6316
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006317struct wl_display *
6318display_get_display(struct display *display)
6319{
6320 return display->display;
6321}
6322
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006323int
6324display_has_subcompositor(struct display *display)
6325{
6326 if (display->subcompositor)
6327 return 1;
6328
6329 wl_display_roundtrip(display->display);
6330
6331 return display->subcompositor != NULL;
6332}
6333
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006334cairo_device_t *
6335display_get_cairo_device(struct display *display)
6336{
6337 return display->argb_device;
6338}
6339
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006340struct output *
6341display_get_output(struct display *display)
6342{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006343 if (wl_list_empty(&display->output_list))
6344 return NULL;
6345
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006346 return container_of(display->output_list.next, struct output, link);
6347}
6348
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006349struct wl_compositor *
6350display_get_compositor(struct display *display)
6351{
6352 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006353}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006354
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006355uint32_t
6356display_get_serial(struct display *display)
6357{
6358 return display->serial;
6359}
6360
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006361EGLDisplay
6362display_get_egl_display(struct display *d)
6363{
6364 return d->dpy;
6365}
6366
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006367struct wl_data_source *
6368display_create_data_source(struct display *display)
6369{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006370 if (display->data_device_manager)
6371 return wl_data_device_manager_create_data_source(display->data_device_manager);
6372 else
6373 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006374}
6375
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006376EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006377display_get_argb_egl_config(struct display *d)
6378{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006379 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006380}
6381
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006382int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006383display_acquire_window_surface(struct display *display,
6384 struct window *window,
6385 EGLContext ctx)
6386{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006387 struct surface *surface = window->main_surface;
6388
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006389 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006390 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006391
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006392 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006393 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006394}
6395
6396void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006397display_release_window_surface(struct display *display,
6398 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006399{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006400 struct surface *surface = window->main_surface;
6401
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006402 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006403 return;
6404
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006405 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006406}
6407
6408void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006409display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006410{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006411 wl_list_insert(&display->deferred_list, &task->link);
6412}
6413
6414void
6415display_watch_fd(struct display *display,
6416 int fd, uint32_t events, struct task *task)
6417{
6418 struct epoll_event ep;
6419
6420 ep.events = events;
6421 ep.data.ptr = task;
6422 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6423}
6424
6425void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006426display_unwatch_fd(struct display *display, int fd)
6427{
6428 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6429}
6430
6431void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006432display_run(struct display *display)
6433{
6434 struct task *task;
6435 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006436 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006437
Pekka Paalanen826d7952011-12-15 10:14:07 +02006438 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006439 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006440 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006441 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006442 struct task, link);
6443 wl_list_remove(&task->link);
6444 task->run(task, 0);
6445 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006446
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006447 wl_display_dispatch_pending(display->display);
6448
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006449 if (!display->running)
6450 break;
6451
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006452 ret = wl_display_flush(display->display);
6453 if (ret < 0 && errno == EAGAIN) {
6454 ep[0].events =
6455 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6456 ep[0].data.ptr = &display->display_task;
6457
6458 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6459 display->display_fd, &ep[0]);
6460 } else if (ret < 0) {
6461 break;
6462 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006463
6464 count = epoll_wait(display->epoll_fd,
6465 ep, ARRAY_LENGTH(ep), -1);
6466 for (i = 0; i < count; i++) {
6467 task = ep[i].data.ptr;
6468 task->run(task, ep[i].events);
6469 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006470 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006471}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006472
6473void
6474display_exit(struct display *display)
6475{
6476 display->running = 0;
6477}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006478
Carlos Garnacho9c931792016-01-18 23:52:12 +01006479int
6480display_get_data_device_manager_version(struct display *display)
6481{
6482 return display->data_device_manager_version;
6483}
6484
Jan Arne Petersencd997062012-11-18 19:06:44 +01006485void
6486keysym_modifiers_add(struct wl_array *modifiers_map,
6487 const char *name)
6488{
6489 size_t len = strlen(name) + 1;
6490 char *p;
6491
6492 p = wl_array_add(modifiers_map, len);
6493
6494 if (p == NULL)
6495 return;
6496
6497 strncpy(p, name, len);
6498}
6499
6500static xkb_mod_index_t
6501keysym_modifiers_get_index(struct wl_array *modifiers_map,
6502 const char *name)
6503{
6504 xkb_mod_index_t index = 0;
6505 char *p = modifiers_map->data;
6506
6507 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6508 if (strcmp(p, name) == 0)
6509 return index;
6510
6511 index++;
6512 p += strlen(p) + 1;
6513 }
6514
6515 return XKB_MOD_INVALID;
6516}
6517
6518xkb_mod_mask_t
6519keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6520 const char *name)
6521{
6522 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6523
6524 if (index == XKB_MOD_INVALID)
6525 return XKB_MOD_INVALID;
6526
6527 return 1 << index;
6528}
Pekka Paalanen3f5f3af2018-03-09 11:54:40 +02006529
6530static void
6531toytimer_fire(struct task *tsk, uint32_t events)
6532{
6533 uint64_t e;
6534 struct toytimer *tt;
6535
6536 tt = container_of(tsk, struct toytimer, tsk);
6537
6538 if (events != EPOLLIN)
6539 fprintf(stderr, "unexpected timerfd events %x\n", events);
6540
6541 if (!(events & EPOLLIN))
6542 return;
6543
6544 if (read(tt->fd, &e, sizeof e) != sizeof e) {
6545 /* If we change the timer between the fd becoming
6546 * readable and getting here, there'll be nothing to
6547 * read and we get EAGAIN. */
6548 if (errno != EAGAIN)
6549 fprintf(stderr, "timer read failed: %m\n");
6550 return;
6551 }
6552
6553 tt->callback(tt);
6554}
6555
6556void
6557toytimer_init(struct toytimer *tt, clockid_t clock, struct display *display,
6558 toytimer_cb callback)
6559{
6560 memset(tt, 0, sizeof *tt);
6561
6562 tt->fd = timerfd_create(clock, TFD_CLOEXEC | TFD_NONBLOCK);
6563 if (tt->fd == -1) {
6564 fprintf(stderr, "creating timer failed: %m\n");
6565 abort();
6566 }
6567
6568 tt->display = display;
6569 tt->callback = callback;
6570 tt->tsk.run = toytimer_fire;
6571 display_watch_fd(display, tt->fd, EPOLLIN, &tt->tsk);
6572}
6573
6574void
6575toytimer_fini(struct toytimer *tt)
6576{
6577 display_unwatch_fd(tt->display, tt->fd);
6578 close(tt->fd);
6579 tt->fd = -1;
6580}
6581
6582void
6583toytimer_arm(struct toytimer *tt, const struct itimerspec *its)
6584{
6585 int ret;
6586
6587 ret = timerfd_settime(tt->fd, 0, its, NULL);
6588 if (ret < 0) {
6589 fprintf(stderr, "timer setup failed: %m\n");
6590 abort();
6591 }
6592}
6593
6594#define USEC_PER_SEC 1000000
6595
6596void
6597toytimer_arm_once_usec(struct toytimer *tt, uint32_t usec)
6598{
6599 struct itimerspec its;
6600
6601 its.it_interval.tv_sec = 0;
6602 its.it_interval.tv_nsec = 0;
6603 its.it_value.tv_sec = usec / USEC_PER_SEC;
6604 its.it_value.tv_nsec = (usec % USEC_PER_SEC) * 1000;
6605 toytimer_arm(tt, &its);
6606}
6607
6608void
6609toytimer_disarm(struct toytimer *tt)
6610{
6611 struct itimerspec its = {};
6612
6613 toytimer_arm(tt, &its);
6614}