blob: 95796d465b2e7234daf7142f8f24f1ad31a0ddaf [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;
352 int cursor_delay_fd;
353 bool cursor_timer_running;
354 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300355 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400356 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400357 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400358 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400359 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400360 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400361
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500362 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500363 struct widget *grab;
364 uint32_t grab_button;
365
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400366 struct wl_data_device *data_device;
367 struct data_offer *drag_offer;
368 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800369 uint32_t touch_grab;
370 int32_t touch_grab_id;
371 float drag_x, drag_y;
372 struct window *drag_focus;
373 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100374
375 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100376 struct xkb_keymap *keymap;
377 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +0100378#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700379 struct xkb_compose_table *compose_table;
380 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +0100381#endif
Daniel Stone97f68542012-05-30 16:32:01 +0100382 xkb_mod_mask_t control_mask;
383 xkb_mod_mask_t alt_mask;
384 xkb_mod_mask_t shift_mask;
385 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400386
Jonny Lamb06959082014-08-12 14:58:27 +0200387 int32_t repeat_rate_sec;
388 int32_t repeat_rate_nsec;
389 int32_t repeat_delay_sec;
390 int32_t repeat_delay_nsec;
391
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400392 struct task repeat_task;
393 int repeat_timer_fd;
394 uint32_t repeat_sym;
395 uint32_t repeat_key;
396 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500397 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400398};
399
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500400struct output {
401 struct display *display;
402 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800403 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500404 struct rectangle allocation;
405 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600406 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200407 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500408 char *make;
409 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200410
411 display_output_handler_t destroy_handler;
412 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500413};
414
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500415struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500416 struct widget *widget;
417 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500418 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800419
420 uint32_t last_time;
421 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800422 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500423};
424
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500425struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400426 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500427 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500428 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500429 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700430 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500431 const char **entries;
432 uint32_t time;
433 int current;
434 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400435 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500436 menu_func_t func;
437};
438
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300439struct tooltip {
440 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300441 struct widget *widget;
442 char *entry;
443 struct task tooltip_task;
444 int tooltip_fd;
445 float x, y;
446};
447
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700448struct shm_pool {
449 struct wl_shm_pool *pool;
450 size_t size;
451 size_t used;
452 void *data;
453};
454
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400455enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300456 CURSOR_DEFAULT = 100,
457 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400458};
459
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200460static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400461
Pekka Paalanen97777442013-05-22 10:20:05 +0300462/* #define DEBUG */
463
464#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300465
466static void
467debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
468__attribute__ ((format (printf, 4, 5)));
469
470static void
471debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
472{
473 va_list ap;
474 struct timeval tv;
475
476 gettimeofday(&tv, NULL);
477 fprintf(stderr, "%8ld.%03ld ",
478 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
479
480 if (proxy)
481 fprintf(stderr, "%s@%d ",
482 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
483
484 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
485 fprintf(stderr, "%s ", func);
486
487 va_start(ap, fmt);
488 vfprintf(stderr, fmt, ap);
489 va_end(ap);
490}
491
492#define DBG(fmt, ...) \
493 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
494
495#define DBG_OBJ(obj, fmt, ...) \
496 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
497
498#else
499
500#define DBG(...) do {} while (0)
501#define DBG_OBJ(...) do {} while (0)
502
503#endif
504
Alexander Larsson1818e312013-05-22 14:41:31 +0200505static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200506surface_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 +0200507{
508 int32_t tmp;
509
510 switch (buffer_transform) {
511 case WL_OUTPUT_TRANSFORM_90:
512 case WL_OUTPUT_TRANSFORM_270:
513 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
514 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
515 tmp = *width;
516 *width = *height;
517 *height = tmp;
518 break;
519 default:
520 break;
521 }
522
523 *width *= buffer_scale;
524 *height *= buffer_scale;
525}
526
527static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200528buffer_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 +0200529{
530 int32_t tmp;
531
532 switch (buffer_transform) {
533 case WL_OUTPUT_TRANSFORM_90:
534 case WL_OUTPUT_TRANSFORM_270:
535 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
536 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
537 tmp = *width;
538 *width = *height;
539 *height = tmp;
540 break;
541 default:
542 break;
543 }
544
545 *width /= buffer_scale;
546 *height /= buffer_scale;
547}
548
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500549#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400550
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551struct egl_window_surface {
552 struct toysurface base;
553 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100554 struct display *display;
555 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556 struct wl_egl_window *egl_window;
557 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558};
559
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200560static struct egl_window_surface *
561to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100562{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100564}
565
566static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200567egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200568 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200569 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100570{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 struct egl_window_surface *surface = to_egl_window_surface(base);
572
Alexander Larsson1818e312013-05-22 14:41:31 +0200573 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
574
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200575 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
576 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
577
578 return cairo_surface_reference(surface->cairo_surface);
579}
580
581static void
582egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200583 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200584 struct rectangle *server_allocation)
585{
586 struct egl_window_surface *surface = to_egl_window_surface(base);
587
588 cairo_gl_surface_swapbuffers(surface->cairo_surface);
589 wl_egl_window_get_attached_size(surface->egl_window,
590 &server_allocation->width,
591 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200592
593 buffer_to_surface_size (buffer_transform, buffer_scale,
594 &server_allocation->width,
595 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200596}
597
598static int
599egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
600{
601 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200602 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100603
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200604 device = cairo_surface_get_device(surface->cairo_surface);
605 if (!device)
606 return -1;
607
608 if (!ctx) {
609 if (device == surface->display->argb_device)
610 ctx = surface->display->argb_ctx;
611 else
612 assert(0);
613 }
614
615 cairo_device_flush(device);
616 cairo_device_acquire(device);
617 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
618 surface->egl_surface, ctx))
619 fprintf(stderr, "failed to make surface current\n");
620
621 return 0;
622}
623
624static void
625egl_window_surface_release(struct toysurface *base)
626{
627 struct egl_window_surface *surface = to_egl_window_surface(base);
628 cairo_device_t *device;
629
630 device = cairo_surface_get_device(surface->cairo_surface);
631 if (!device)
632 return;
633
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200634 if (!eglMakeCurrent(surface->display->dpy,
635 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200636 fprintf(stderr, "failed to make context current\n");
637
638 cairo_device_release(device);
639}
640
641static void
642egl_window_surface_destroy(struct toysurface *base)
643{
644 struct egl_window_surface *surface = to_egl_window_surface(base);
645 struct display *d = surface->display;
646
647 cairo_surface_destroy(surface->cairo_surface);
Emil Velikov050e5d02016-11-18 19:12:58 +0000648 weston_platform_destroy_egl_surface(d->dpy, surface->egl_surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200649 wl_egl_window_destroy(surface->egl_window);
650 surface->surface = NULL;
651
652 free(surface);
653}
654
655static struct toysurface *
656egl_window_surface_create(struct display *display,
657 struct wl_surface *wl_surface,
658 uint32_t flags,
659 struct rectangle *rectangle)
660{
661 struct egl_window_surface *surface;
662
Pekka Paalanenb3627362012-11-19 17:16:00 +0200663 if (display->dpy == EGL_NO_DISPLAY)
664 return NULL;
665
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800666 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200667 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100668 return NULL;
669
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200670 surface->base.prepare = egl_window_surface_prepare;
671 surface->base.swap = egl_window_surface_swap;
672 surface->base.acquire = egl_window_surface_acquire;
673 surface->base.release = egl_window_surface_release;
674 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100675
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200676 surface->display = display;
677 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400678
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200679 surface->egl_window = wl_egl_window_create(surface->surface,
680 rectangle->width,
681 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100682
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100683 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100684 weston_platform_create_egl_surface(display->dpy,
685 display->argb_config,
686 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100687
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200688 surface->cairo_surface =
689 cairo_gl_surface_create_for_egl(display->argb_device,
690 surface->egl_surface,
691 rectangle->width,
692 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100693
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200694 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100695}
696
Pekka Paalanenb3627362012-11-19 17:16:00 +0200697#else
698
699static struct toysurface *
700egl_window_surface_create(struct display *display,
701 struct wl_surface *wl_surface,
702 uint32_t flags,
703 struct rectangle *rectangle)
704{
705 return NULL;
706}
707
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400708#endif
709
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200710struct shm_surface_data {
711 struct wl_buffer *buffer;
712 struct shm_pool *pool;
713};
714
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400715struct wl_buffer *
716display_get_buffer_for_surface(struct display *display,
717 cairo_surface_t *surface)
718{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200719 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400720
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200721 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400722
723 return data->buffer;
724}
725
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500726static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700727shm_pool_destroy(struct shm_pool *pool);
728
729static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400730shm_surface_data_destroy(void *p)
731{
732 struct shm_surface_data *data = p;
733
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200734 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700735 if (data->pool)
736 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300737
738 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400739}
740
Kristian Høgsberg16626282012-04-03 11:21:27 -0400741static struct wl_shm_pool *
742make_shm_pool(struct display *display, int size, void **data)
743{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400744 struct wl_shm_pool *pool;
745 int fd;
746
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300747 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400748 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300749 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
750 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400751 return NULL;
752 }
753
754 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400755 if (*data == MAP_FAILED) {
756 fprintf(stderr, "mmap failed: %m\n");
757 close(fd);
758 return NULL;
759 }
760
761 pool = wl_shm_create_pool(display->shm, fd, size);
762
763 close(fd);
764
765 return pool;
766}
767
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700768static struct shm_pool *
769shm_pool_create(struct display *display, size_t size)
770{
771 struct shm_pool *pool = malloc(sizeof *pool);
772
773 if (!pool)
774 return NULL;
775
776 pool->pool = make_shm_pool(display, size, &pool->data);
777 if (!pool->pool) {
778 free(pool);
779 return NULL;
780 }
781
782 pool->size = size;
783 pool->used = 0;
784
785 return pool;
786}
787
788static void *
789shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
790{
791 if (pool->used + size > pool->size)
792 return NULL;
793
794 *offset = pool->used;
795 pool->used += size;
796
797 return (char *) pool->data + *offset;
798}
799
800/* destroy the pool. this does not unmap the memory though */
801static void
802shm_pool_destroy(struct shm_pool *pool)
803{
804 munmap(pool->data, pool->size);
805 wl_shm_pool_destroy(pool->pool);
806 free(pool);
807}
808
809/* Start allocating from the beginning of the pool again */
810static void
811shm_pool_reset(struct shm_pool *pool)
812{
813 pool->used = 0;
814}
815
816static int
817data_length_for_shm_surface(struct rectangle *rect)
818{
819 int stride;
820
821 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
822 rect->width);
823 return stride * rect->height;
824}
825
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500826static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700827display_create_shm_surface_from_pool(struct display *display,
828 struct rectangle *rectangle,
829 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830{
831 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400832 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400833 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200834 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700835 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400836 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837
838 data = malloc(sizeof *data);
839 if (data == NULL)
840 return NULL;
841
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200842 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
843 cairo_format = CAIRO_FORMAT_RGB16_565;
844 else
845 cairo_format = CAIRO_FORMAT_ARGB32;
846
847 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700848 length = stride * rectangle->height;
849 data->pool = NULL;
850 map = shm_pool_allocate(pool, length, &offset);
851
852 if (!map) {
853 free(data);
854 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400855 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400857 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200858 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400859 rectangle->width,
860 rectangle->height,
861 stride);
862
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200863 cairo_surface_set_user_data(surface, &shm_surface_data_key,
864 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200866 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
867 format = WL_SHM_FORMAT_RGB565;
868 else {
869 if (flags & SURFACE_OPAQUE)
870 format = WL_SHM_FORMAT_XRGB8888;
871 else
872 format = WL_SHM_FORMAT_ARGB8888;
873 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400874
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200875 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
876 rectangle->width,
877 rectangle->height,
878 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700880 return surface;
881}
882
883static cairo_surface_t *
884display_create_shm_surface(struct display *display,
885 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200886 struct shm_pool *alternate_pool,
887 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700888{
889 struct shm_surface_data *data;
890 struct shm_pool *pool;
891 cairo_surface_t *surface;
892
Pekka Paalanen99436862012-11-19 17:15:59 +0200893 if (alternate_pool) {
894 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700895 surface = display_create_shm_surface_from_pool(display,
896 rectangle,
897 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200898 alternate_pool);
899 if (surface) {
900 data = cairo_surface_get_user_data(surface,
901 &shm_surface_data_key);
902 goto out;
903 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700904 }
905
906 pool = shm_pool_create(display,
907 data_length_for_shm_surface(rectangle));
908 if (!pool)
909 return NULL;
910
911 surface =
912 display_create_shm_surface_from_pool(display, rectangle,
913 flags, pool);
914
915 if (!surface) {
916 shm_pool_destroy(pool);
917 return NULL;
918 }
919
920 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200921 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700922 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400923
Pekka Paalanen99436862012-11-19 17:15:59 +0200924out:
925 if (data_ret)
926 *data_ret = data;
927
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400928 return surface;
929}
930
nobled7b87cb02011-02-01 18:51:47 +0000931static int
932check_size(struct rectangle *rect)
933{
934 if (rect->width && rect->height)
935 return 0;
936
937 fprintf(stderr, "tried to create surface of "
938 "width: %d, height: %d\n", rect->width, rect->height);
939 return -1;
940}
941
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400942cairo_surface_t *
943display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100944 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400945 struct rectangle *rectangle,
946 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400947{
nobled7b87cb02011-02-01 18:51:47 +0000948 if (check_size(rectangle) < 0)
949 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200950
951 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200952 return display_create_shm_surface(display, rectangle, flags,
953 NULL, NULL);
954}
955
Pekka Paalanena4eda732012-11-19 17:16:02 +0200956struct shm_surface_leaf {
957 cairo_surface_t *cairo_surface;
958 /* 'data' is automatically destroyed, when 'cairo_surface' is */
959 struct shm_surface_data *data;
960
961 struct shm_pool *resize_pool;
962 int busy;
963};
964
965static void
966shm_surface_leaf_release(struct shm_surface_leaf *leaf)
967{
968 if (leaf->cairo_surface)
969 cairo_surface_destroy(leaf->cairo_surface);
970 /* leaf->data already destroyed via cairo private */
971
972 if (leaf->resize_pool)
973 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200974
975 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200976}
977
Pekka Paalanenaef02542013-04-25 13:57:47 +0300978#define MAX_LEAVES 3
979
Pekka Paalanen99436862012-11-19 17:15:59 +0200980struct shm_surface {
981 struct toysurface base;
982 struct display *display;
983 struct wl_surface *surface;
984 uint32_t flags;
985 int dx, dy;
986
Pekka Paalanenaef02542013-04-25 13:57:47 +0300987 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200988 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200989};
990
991static struct shm_surface *
992to_shm_surface(struct toysurface *base)
993{
994 return container_of(base, struct shm_surface, base);
995}
996
Pekka Paalanena4eda732012-11-19 17:16:02 +0200997static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300998shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
999{
1000#ifdef DEBUG
1001 struct shm_surface_leaf *leaf;
1002 char bufs[MAX_LEAVES + 1];
1003 int i;
1004
1005 for (i = 0; i < MAX_LEAVES; i++) {
1006 leaf = &surface->leaf[i];
1007
1008 if (leaf->busy)
1009 bufs[i] = 'b';
1010 else if (leaf->cairo_surface)
1011 bufs[i] = 'a';
1012 else
1013 bufs[i] = ' ';
1014 }
1015
1016 bufs[MAX_LEAVES] = '\0';
1017 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1018#endif
1019}
1020
1021static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001022shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1023{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001024 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 struct shm_surface_leaf *leaf;
1026 int i;
1027 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001028
1029 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001030
Pekka Paalanenaef02542013-04-25 13:57:47 +03001031 for (i = 0; i < MAX_LEAVES; i++) {
1032 leaf = &surface->leaf[i];
1033 if (leaf->data && leaf->data->buffer == buffer) {
1034 leaf->busy = 0;
1035 break;
1036 }
1037 }
1038 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001039
Pekka Paalanenaef02542013-04-25 13:57:47 +03001040 /* Leave one free leaf with storage, release others */
1041 free_found = 0;
1042 for (i = 0; i < MAX_LEAVES; i++) {
1043 leaf = &surface->leaf[i];
1044
1045 if (!leaf->cairo_surface || leaf->busy)
1046 continue;
1047
1048 if (!free_found)
1049 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001050 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001051 shm_surface_leaf_release(leaf);
1052 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001053
Pekka Paalanen97777442013-05-22 10:20:05 +03001054 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055}
1056
1057static const struct wl_buffer_listener shm_surface_buffer_listener = {
1058 shm_surface_buffer_release
1059};
1060
Pekka Paalanen99436862012-11-19 17:15:59 +02001061static cairo_surface_t *
1062shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001063 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001064 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001065{
Pekka Paalanenec076692012-11-30 13:37:27 +02001066 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001067 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001068 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001069 struct shm_surface_leaf *leaf = NULL;
1070 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001071
1072 surface->dx = dx;
1073 surface->dy = dy;
1074
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001075 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001076 for (i = 0; i < MAX_LEAVES; i++) {
1077 if (surface->leaf[i].busy)
1078 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001079
Pekka Paalanenaef02542013-04-25 13:57:47 +03001080 if (!leaf || surface->leaf[i].cairo_surface)
1081 leaf = &surface->leaf[i];
1082 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001083 DBG_OBJ(surface->surface, "pick leaf %d\n",
1084 (int)(leaf - &surface->leaf[0]));
1085
Pekka Paalanenaef02542013-04-25 13:57:47 +03001086 if (!leaf) {
1087 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001089 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001090 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001091 }
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 if (!resize_hint && leaf->resize_pool) {
1094 cairo_surface_destroy(leaf->cairo_surface);
1095 leaf->cairo_surface = NULL;
1096 shm_pool_destroy(leaf->resize_pool);
1097 leaf->resize_pool = NULL;
1098 }
1099
Alexander Larsson1818e312013-05-22 14:41:31 +02001100 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1101
Pekka Paalanena4eda732012-11-19 17:16:02 +02001102 if (leaf->cairo_surface &&
1103 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1104 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001105 goto out;
1106
Pekka Paalanena4eda732012-11-19 17:16:02 +02001107 if (leaf->cairo_surface)
1108 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001109
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001110#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001112 /* Create a big pool to allocate from, while continuously
1113 * resizing. Mmapping a new pool in the server
1114 * is relatively expensive, so reusing a pool performs
1115 * better, but may temporarily reserve unneeded memory.
1116 */
1117 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001118 leaf->resize_pool = shm_pool_create(surface->display,
1119 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001120 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001121#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001122
Alexander Larsson1818e312013-05-22 14:41:31 +02001123 rect.width = width;
1124 rect.height = height;
1125
Pekka Paalanena4eda732012-11-19 17:16:02 +02001126 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001127 display_create_shm_surface(surface->display, &rect,
1128 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001129 leaf->resize_pool,
1130 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001131 if (!leaf->cairo_surface)
1132 return NULL;
1133
Pekka Paalanena4eda732012-11-19 17:16:02 +02001134 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001135 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001136
1137out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001138 surface->current = leaf;
1139
1140 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001141}
1142
1143static void
1144shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001145 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001146 struct rectangle *server_allocation)
1147{
1148 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001149 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001150
1151 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001152 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001153 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001154 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001155
Alexander Larsson1818e312013-05-22 14:41:31 +02001156 buffer_to_surface_size (buffer_transform, buffer_scale,
1157 &server_allocation->width,
1158 &server_allocation->height);
1159
Pekka Paalanena4eda732012-11-19 17:16:02 +02001160 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001161 surface->dx, surface->dy);
1162 wl_surface_damage(surface->surface, 0, 0,
1163 server_allocation->width, server_allocation->height);
1164 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001165
Pekka Paalanen71233882013-04-25 13:57:53 +03001166 DBG_OBJ(surface->surface, "leaf %d busy\n",
1167 (int)(leaf - &surface->leaf[0]));
1168
Pekka Paalanena4eda732012-11-19 17:16:02 +02001169 leaf->busy = 1;
1170 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001171}
1172
1173static int
1174shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1175{
1176 return -1;
1177}
1178
1179static void
1180shm_surface_release(struct toysurface *base)
1181{
1182}
1183
1184static void
1185shm_surface_destroy(struct toysurface *base)
1186{
1187 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001188 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001189
Pekka Paalanenaef02542013-04-25 13:57:47 +03001190 for (i = 0; i < MAX_LEAVES; i++)
1191 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001192
1193 free(surface);
1194}
1195
1196static struct toysurface *
1197shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1198 uint32_t flags, struct rectangle *rectangle)
1199{
1200 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001201 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001202
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001203 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001204 surface->base.prepare = shm_surface_prepare;
1205 surface->base.swap = shm_surface_swap;
1206 surface->base.acquire = shm_surface_acquire;
1207 surface->base.release = shm_surface_release;
1208 surface->base.destroy = shm_surface_destroy;
1209
1210 surface->display = display;
1211 surface->surface = wl_surface;
1212 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001213
1214 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001215}
1216
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001217/*
1218 * The following correspondences between file names and cursors was copied
1219 * from: https://bugs.kde.org/attachment.cgi?id=67313
1220 */
1221
1222static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001223 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001224 "sw-resize",
1225 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001226};
1227
1228static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001229 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001230 "se-resize",
1231 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001232};
1233
1234static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001235 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001236 "s-resize",
1237 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001238};
1239
1240static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001241 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 "closedhand",
1243 "208530c400c041818281048008011002"
1244};
1245
1246static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001247 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248 "default",
1249 "top_left_arrow",
1250 "left-arrow"
1251};
1252
1253static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001254 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001255 "w-resize",
1256 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257};
1258
1259static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001260 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001261 "e-resize",
1262 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001263};
1264
1265static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001266 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001267 "nw-resize",
1268 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001269};
1270
1271static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001272 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001273 "ne-resize",
1274 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001275};
1276
1277static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001278 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001279 "n-resize",
1280 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001281};
1282
1283static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001284 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001285 "ibeam",
1286 "text"
1287};
1288
1289static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001290 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001291 "pointer",
1292 "pointing_hand",
1293 "e29285e634086352946a0e7090d73106"
1294};
1295
1296static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001297 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001298 "wait",
1299 "0426c94ea35c87780ff01dc239897213"
1300};
1301
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001302static const char *move_draggings[] = {
1303 "dnd-move"
1304};
1305
1306static const char *copy_draggings[] = {
1307 "dnd-copy"
1308};
1309
1310static const char *forbidden_draggings[] = {
1311 "dnd-none",
1312 "dnd-no-drop"
1313};
1314
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001315struct cursor_alternatives {
1316 const char **names;
1317 size_t count;
1318};
1319
1320static const struct cursor_alternatives cursors[] = {
1321 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1322 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1323 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1324 {grabbings, ARRAY_LENGTH(grabbings)},
1325 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1326 {left_sides, ARRAY_LENGTH(left_sides)},
1327 {right_sides, ARRAY_LENGTH(right_sides)},
1328 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1329 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1330 {top_sides, ARRAY_LENGTH(top_sides)},
1331 {xterms, ARRAY_LENGTH(xterms)},
1332 {hand1s, ARRAY_LENGTH(hand1s)},
1333 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001334 {move_draggings, ARRAY_LENGTH(move_draggings)},
1335 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1336 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001337};
1338
1339static void
1340create_cursors(struct display *display)
1341{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001342 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001343 struct weston_config *config;
1344 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001345 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001346 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001347 unsigned int i, j;
1348 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001349
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001350 config_file = weston_config_get_name_from_env();
1351 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001352 s = weston_config_get_section(config, "shell", NULL, NULL);
1353 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1354 weston_config_section_get_int(s, "cursor-size", &size, 32);
1355 weston_config_destroy(config);
1356
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001357 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001358 if (!display->cursor_theme) {
1359 fprintf(stderr, "could not load theme '%s'\n", theme);
1360 return;
1361 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001362 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001363 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001364 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001365
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001366 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001367 cursor = NULL;
1368 for (j = 0; !cursor && j < cursors[i].count; ++j)
1369 cursor = wl_cursor_theme_get_cursor(
1370 display->cursor_theme, cursors[i].names[j]);
1371
1372 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001373 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001374 cursors[i].names[0]);
1375
1376 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001377 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001378}
1379
1380static void
1381destroy_cursors(struct display *display)
1382{
1383 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001384 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001385}
1386
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001387struct wl_cursor_image *
1388display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001389{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001390 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001391
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001392 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001393}
1394
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001395static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001396surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001397{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001398 if (!surface->cairo_surface)
1399 return;
1400
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001401 if (surface->opaque_region) {
1402 wl_surface_set_opaque_region(surface->surface,
1403 surface->opaque_region);
1404 wl_region_destroy(surface->opaque_region);
1405 surface->opaque_region = NULL;
1406 }
1407
1408 if (surface->input_region) {
1409 wl_surface_set_input_region(surface->surface,
1410 surface->input_region);
1411 wl_region_destroy(surface->input_region);
1412 surface->input_region = NULL;
1413 }
1414
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001415 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001416 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001417 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001418
Pekka Paalanen89dee002013-02-13 16:17:20 +02001419 cairo_surface_destroy(surface->cairo_surface);
1420 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001421}
1422
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001423int
1424window_has_focus(struct window *window)
1425{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001426 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001427}
1428
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001429static void
1430window_close(struct window *window)
1431{
1432 if (window->close_handler)
1433 window->close_handler(window->user_data);
1434 else
1435 display_exit(window->display);
1436}
1437
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001438struct display *
1439window_get_display(struct window *window)
1440{
1441 return window->display;
1442}
1443
Pekka Paalanen89dee002013-02-13 16:17:20 +02001444static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001445handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1446 int32_t width, int32_t height)
1447{
1448 struct window *window = data;
1449
1450 window_schedule_resize(window, width, height);
1451}
1452
1453static const struct ivi_surface_listener ivi_surface_listener = {
1454 handle_ivi_surface_configure,
1455};
1456
1457static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001458surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001459{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001460 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001461 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001462
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001463 if (!surface->toysurface && display->dpy &&
1464 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001465 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001466 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001467 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001468 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001469 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001470 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001471
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001472 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001473 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001474 surface->surface,
1475 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001476
Pekka Paalanen89dee002013-02-13 16:17:20 +02001477 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001478 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001479 allocation.width, allocation.height, flags,
1480 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001481}
1482
1483static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001484window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001485{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001486 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001487 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001488
Pekka Paalanenec076692012-11-30 13:37:27 +02001489 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001490 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001491
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001492 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1493 flags |= SURFACE_HINT_RGB565;
1494
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001495 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001496}
1497
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001498int
1499window_get_buffer_transform(struct window *window)
1500{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001501 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001502}
1503
1504void
1505window_set_buffer_transform(struct window *window,
1506 enum wl_output_transform transform)
1507{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001508 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001509 wl_surface_set_buffer_transform(window->main_surface->surface,
1510 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001511}
1512
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001513void
1514window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001515 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001516{
1517 window->main_surface->buffer_scale = scale;
1518 wl_surface_set_buffer_scale(window->main_surface->surface,
1519 scale);
1520}
1521
1522uint32_t
1523window_get_buffer_scale(struct window *window)
1524{
1525 return window->main_surface->buffer_scale;
1526}
1527
Alexander Larssond68f5232013-05-22 14:41:33 +02001528uint32_t
1529window_get_output_scale(struct window *window)
1530{
1531 struct window_output *window_output;
1532 struct window_output *window_output_tmp;
1533 int scale = 1;
1534
1535 wl_list_for_each_safe(window_output, window_output_tmp,
1536 &window->window_output_list, link) {
1537 if (window_output->output->scale > scale)
1538 scale = window_output->output->scale;
1539 }
1540
1541 return scale;
1542}
1543
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001544static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001545
Pekka Paalanen4e373742013-02-13 16:17:13 +02001546static void
1547surface_destroy(struct surface *surface)
1548{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001549 if (surface->frame_cb)
1550 wl_callback_destroy(surface->frame_cb);
1551
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001552 if (surface->input_region)
1553 wl_region_destroy(surface->input_region);
1554
1555 if (surface->opaque_region)
1556 wl_region_destroy(surface->opaque_region);
1557
Pekka Paalanen35e82632013-04-25 13:57:48 +03001558 if (surface->subsurface)
1559 wl_subsurface_destroy(surface->subsurface);
1560
Pekka Paalanen4e373742013-02-13 16:17:13 +02001561 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001562
1563 if (surface->toysurface)
1564 surface->toysurface->destroy(surface->toysurface);
1565
Pekka Paalanen35e82632013-04-25 13:57:48 +03001566 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001567 free(surface);
1568}
1569
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001570void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001571window_destroy(struct window *window)
1572{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001573 struct display *display = window->display;
1574 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001575 struct window_output *window_output;
1576 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001577
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001578 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001579
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001580 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001581 if (input->touch_focus == window)
1582 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001583 if (input->pointer_focus == window)
1584 input->pointer_focus = NULL;
1585 if (input->keyboard_focus == window)
1586 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001587 if (input->focus_widget &&
1588 input->focus_widget->window == window)
1589 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001590 }
1591
Rob Bradford7507b572012-05-15 17:55:34 +01001592 wl_list_for_each_safe(window_output, window_output_tmp,
1593 &window->window_output_list, link) {
1594 free (window_output);
1595 }
1596
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001597 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001598 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001599
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001600 if (window->xdg_toplevel)
1601 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001602 if (window->xdg_popup)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001603 zxdg_popup_v6_destroy(window->xdg_popup);
1604 if (window->xdg_surface)
1605 zxdg_surface_v6_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001606
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001607 if (window->ivi_surface)
1608 ivi_surface_destroy(window->ivi_surface);
1609
Pekka Paalanen4e373742013-02-13 16:17:13 +02001610 surface_destroy(window->main_surface);
1611
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001612 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001613
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001614 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001615 free(window);
1616}
1617
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001618static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001620{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001621 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001623 wl_list_for_each(child, &widget->child_list, link) {
1624 target = widget_find_widget(child, x, y);
1625 if (target)
1626 return target;
1627 }
1628
1629 if (widget->allocation.x <= x &&
1630 x < widget->allocation.x + widget->allocation.width &&
1631 widget->allocation.y <= y &&
1632 y < widget->allocation.y + widget->allocation.height) {
1633 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001634 }
1635
1636 return NULL;
1637}
1638
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001639static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001640window_find_widget(struct window *window, int32_t x, int32_t y)
1641{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001642 struct surface *surface;
1643 struct widget *widget;
1644
1645 wl_list_for_each(surface, &window->subsurface_list, link) {
1646 widget = widget_find_widget(surface->widget, x, y);
1647 if (widget)
1648 return widget;
1649 }
1650
1651 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001652}
1653
1654static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001655widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001656{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001657 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001658
Peter Huttererf3d62272013-08-08 11:57:05 +10001659 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001660 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001661 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001662 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001663 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001664 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001665 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001666 widget->tooltip = NULL;
1667 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001668 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001669 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001670
1671 return widget;
1672}
1673
1674struct widget *
1675window_add_widget(struct window *window, void *data)
1676{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001677 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001678
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001679 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001680 wl_list_init(&widget->link);
1681 window->main_surface->widget = widget;
1682
1683 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001684}
1685
1686struct widget *
1687widget_add_widget(struct widget *parent, void *data)
1688{
1689 struct widget *widget;
1690
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001691 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001692 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001693
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001694 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695}
1696
1697void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001698widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001699{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001700 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001701 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001702 struct input *input;
1703
Pekka Paalanen35e82632013-04-25 13:57:48 +03001704 /* Destroy the sub-surface along with the root widget */
1705 if (surface->widget == widget && surface->subsurface)
1706 surface_destroy(widget->surface);
1707
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001708 if (widget->tooltip)
1709 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001710
Pekka Paalanene156fb62012-01-19 13:51:38 +02001711 wl_list_for_each(input, &display->input_list, link) {
1712 if (input->focus_widget == widget)
1713 input->focus_widget = NULL;
1714 }
1715
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001716 wl_list_remove(&widget->link);
1717 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001718}
1719
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001720void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001721widget_set_default_cursor(struct widget *widget, int cursor)
1722{
1723 widget->default_cursor = cursor;
1724}
1725
1726void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001727widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001728{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001729 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001730}
1731
1732void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001733widget_set_size(struct widget *widget, int32_t width, int32_t height)
1734{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001735 widget->allocation.width = width;
1736 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001737}
1738
1739void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001740widget_set_allocation(struct widget *widget,
1741 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001742{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001743 widget->allocation.x = x;
1744 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001745 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001746}
1747
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001748void
1749widget_set_transparent(struct widget *widget, int transparent)
1750{
1751 widget->opaque = !transparent;
1752}
1753
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001754void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001755widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001756{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001757 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001758}
1759
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001760static cairo_surface_t *
1761widget_get_cairo_surface(struct widget *widget)
1762{
1763 struct surface *surface = widget->surface;
1764 struct window *window = widget->window;
1765
Neil Roberts97b747c2013-12-19 16:17:12 +00001766 assert(widget->use_cairo);
1767
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001768 if (!surface->cairo_surface) {
1769 if (surface == window->main_surface)
1770 window_create_main_surface(window);
1771 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001772 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001773 }
1774
1775 return surface->cairo_surface;
1776}
1777
Alexander Larsson15901f02013-05-22 14:41:25 +02001778static void
1779widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1780{
1781 struct surface *surface = widget->surface;
1782 double angle;
1783 cairo_matrix_t m;
1784 enum wl_output_transform transform;
1785 int surface_width, surface_height;
1786 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001787 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001788
1789 surface_width = surface->allocation.width;
1790 surface_height = surface->allocation.height;
1791
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001792 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001793 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001794
Alexander Larsson15901f02013-05-22 14:41:25 +02001795 switch (transform) {
1796 case WL_OUTPUT_TRANSFORM_FLIPPED:
1797 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1798 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1799 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1800 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1801 break;
1802 default:
1803 cairo_matrix_init_identity(&m);
1804 break;
1805 }
1806
1807 switch (transform) {
1808 case WL_OUTPUT_TRANSFORM_NORMAL:
1809 default:
1810 angle = 0;
1811 translate_x = 0;
1812 translate_y = 0;
1813 break;
1814 case WL_OUTPUT_TRANSFORM_FLIPPED:
1815 angle = 0;
1816 translate_x = surface_width;
1817 translate_y = 0;
1818 break;
1819 case WL_OUTPUT_TRANSFORM_90:
1820 angle = M_PI_2;
1821 translate_x = surface_height;
1822 translate_y = 0;
1823 break;
1824 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1825 angle = M_PI_2;
1826 translate_x = surface_height;
1827 translate_y = surface_width;
1828 break;
1829 case WL_OUTPUT_TRANSFORM_180:
1830 angle = M_PI;
1831 translate_x = surface_width;
1832 translate_y = surface_height;
1833 break;
1834 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1835 angle = M_PI;
1836 translate_x = 0;
1837 translate_y = surface_height;
1838 break;
1839 case WL_OUTPUT_TRANSFORM_270:
1840 angle = M_PI + M_PI_2;
1841 translate_x = 0;
1842 translate_y = surface_width;
1843 break;
1844 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1845 angle = M_PI + M_PI_2;
1846 translate_x = 0;
1847 translate_y = 0;
1848 break;
1849 }
1850
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001851 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001852 cairo_translate(cr, translate_x, translate_y);
1853 cairo_rotate(cr, angle);
1854 cairo_transform(cr, &m);
1855}
1856
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001857cairo_t *
1858widget_cairo_create(struct widget *widget)
1859{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001860 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001861 cairo_surface_t *cairo_surface;
1862 cairo_t *cr;
1863
1864 cairo_surface = widget_get_cairo_surface(widget);
1865 cr = cairo_create(cairo_surface);
1866
Alexander Larsson15901f02013-05-22 14:41:25 +02001867 widget_cairo_update_transform(widget, cr);
1868
Pekka Paalanen35e82632013-04-25 13:57:48 +03001869 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1870
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001871 return cr;
1872}
1873
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001874struct wl_surface *
1875widget_get_wl_surface(struct widget *widget)
1876{
1877 return widget->surface->surface;
1878}
1879
Neil Roberts5e10a042013-09-09 00:40:17 +01001880struct wl_subsurface *
1881widget_get_wl_subsurface(struct widget *widget)
1882{
1883 return widget->surface->subsurface;
1884}
1885
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001886uint32_t
1887widget_get_last_time(struct widget *widget)
1888{
1889 return widget->surface->last_time;
1890}
1891
1892void
1893widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1894{
1895 struct wl_compositor *comp = widget->window->display->compositor;
1896 struct surface *surface = widget->surface;
1897
1898 if (!surface->input_region)
1899 surface->input_region = wl_compositor_create_region(comp);
1900
1901 if (rect) {
1902 wl_region_add(surface->input_region,
1903 rect->x, rect->y, rect->width, rect->height);
1904 }
1905}
1906
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001907void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001908widget_set_resize_handler(struct widget *widget,
1909 widget_resize_handler_t handler)
1910{
1911 widget->resize_handler = handler;
1912}
1913
1914void
1915widget_set_redraw_handler(struct widget *widget,
1916 widget_redraw_handler_t handler)
1917{
1918 widget->redraw_handler = handler;
1919}
1920
1921void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001922widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001923{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001924 widget->enter_handler = handler;
1925}
1926
1927void
1928widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1929{
1930 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001931}
1932
1933void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001934widget_set_motion_handler(struct widget *widget,
1935 widget_motion_handler_t handler)
1936{
1937 widget->motion_handler = handler;
1938}
1939
1940void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001941widget_set_button_handler(struct widget *widget,
1942 widget_button_handler_t handler)
1943{
1944 widget->button_handler = handler;
1945}
1946
1947void
Rusty Lynch041815a2013-08-08 21:20:38 -07001948widget_set_touch_up_handler(struct widget *widget,
1949 widget_touch_up_handler_t handler)
1950{
1951 widget->touch_up_handler = handler;
1952}
1953
1954void
1955widget_set_touch_down_handler(struct widget *widget,
1956 widget_touch_down_handler_t handler)
1957{
1958 widget->touch_down_handler = handler;
1959}
1960
1961void
1962widget_set_touch_motion_handler(struct widget *widget,
1963 widget_touch_motion_handler_t handler)
1964{
1965 widget->touch_motion_handler = handler;
1966}
1967
1968void
1969widget_set_touch_frame_handler(struct widget *widget,
1970 widget_touch_frame_handler_t handler)
1971{
1972 widget->touch_frame_handler = handler;
1973}
1974
1975void
1976widget_set_touch_cancel_handler(struct widget *widget,
1977 widget_touch_cancel_handler_t handler)
1978{
1979 widget->touch_cancel_handler = handler;
1980}
1981
1982void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001983widget_set_axis_handler(struct widget *widget,
1984 widget_axis_handler_t handler)
1985{
1986 widget->axis_handler = handler;
1987}
1988
Peter Hutterer87743e92016-01-18 16:38:22 +10001989void
1990widget_set_pointer_frame_handler(struct widget *widget,
1991 widget_pointer_frame_handler_t handler)
1992{
1993 widget->pointer_frame_handler = handler;
1994}
1995
1996void
1997widget_set_axis_handlers(struct widget *widget,
1998 widget_axis_handler_t axis_handler,
1999 widget_axis_source_handler_t axis_source_handler,
2000 widget_axis_stop_handler_t axis_stop_handler,
2001 widget_axis_discrete_handler_t axis_discrete_handler)
2002{
2003 widget->axis_handler = axis_handler;
2004 widget->axis_source_handler = axis_source_handler;
2005 widget->axis_stop_handler = axis_stop_handler;
2006 widget->axis_discrete_handler = axis_discrete_handler;
2007}
2008
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002009static void
2010window_schedule_redraw_task(struct window *window);
2011
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002012void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002013widget_schedule_redraw(struct widget *widget)
2014{
Pekka Paalanen71233882013-04-25 13:57:53 +03002015 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002016 widget->surface->redraw_needed = 1;
2017 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002018}
2019
Neil Roberts97b747c2013-12-19 16:17:12 +00002020void
2021widget_set_use_cairo(struct widget *widget,
2022 int use_cairo)
2023{
2024 widget->use_cairo = use_cairo;
2025}
2026
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002027cairo_surface_t *
2028window_get_surface(struct window *window)
2029{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002030 cairo_surface_t *cairo_surface;
2031
2032 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2033
2034 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002035}
2036
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002037struct wl_surface *
2038window_get_wl_surface(struct window *window)
2039{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002040 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002041}
2042
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002043static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002044tooltip_redraw_handler(struct widget *widget, void *data)
2045{
2046 cairo_t *cr;
2047 const int32_t r = 3;
2048 struct tooltip *tooltip = data;
2049 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002050
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002051 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002052 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002053 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2054 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2055 cairo_paint(cr);
2056
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002057 width = widget->allocation.width;
2058 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002059 rounded_rect(cr, 0, 0, width, height, r);
2060
2061 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2062 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2063 cairo_fill(cr);
2064
2065 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2066 cairo_move_to(cr, 10, 16);
2067 cairo_show_text(cr, tooltip->entry);
2068 cairo_destroy(cr);
2069}
2070
2071static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002072get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002073{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002074 cairo_t *cr;
2075 cairo_text_extents_t extents;
2076
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002077 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002078 * created yet, and parent does not have a valid surface
2079 * outside repaint, either.
2080 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002081 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002082 cairo_text_extents(cr, tooltip->entry, &extents);
2083 cairo_destroy(cr);
2084
2085 return extents;
2086}
2087
2088static int
2089window_create_tooltip(struct tooltip *tooltip)
2090{
2091 struct widget *parent = tooltip->parent;
2092 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002093 const int offset_y = 27;
2094 const int margin = 3;
2095 cairo_text_extents_t extents;
2096
2097 if (tooltip->widget)
2098 return 0;
2099
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002100 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002101
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002102 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002103 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002104 widget_set_allocation(tooltip->widget,
2105 tooltip->x, tooltip->y + offset_y,
2106 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002107
2108 return 0;
2109}
2110
2111void
2112widget_destroy_tooltip(struct widget *parent)
2113{
2114 struct tooltip *tooltip = parent->tooltip;
2115
2116 parent->tooltip_count = 0;
2117 if (!tooltip)
2118 return;
2119
2120 if (tooltip->widget) {
2121 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002122 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002123 }
2124
2125 close(tooltip->tooltip_fd);
2126 free(tooltip->entry);
2127 free(tooltip);
2128 parent->tooltip = NULL;
2129}
2130
2131static void
2132tooltip_func(struct task *task, uint32_t events)
2133{
2134 struct tooltip *tooltip =
2135 container_of(task, struct tooltip, tooltip_task);
2136 uint64_t exp;
2137
Martin Olsson8df662a2012-07-08 03:03:47 +02002138 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2139 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002140 window_create_tooltip(tooltip);
2141}
2142
2143#define TOOLTIP_TIMEOUT 500
2144static int
2145tooltip_timer_reset(struct tooltip *tooltip)
2146{
2147 struct itimerspec its;
2148
2149 its.it_interval.tv_sec = 0;
2150 its.it_interval.tv_nsec = 0;
2151 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2152 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2153 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2154 fprintf(stderr, "could not set timerfd\n: %m");
2155 return -1;
2156 }
2157
2158 return 0;
2159}
2160
2161int
2162widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2163{
2164 struct tooltip *tooltip = parent->tooltip;
2165
2166 parent->tooltip_count++;
2167 if (tooltip) {
2168 tooltip->x = x;
2169 tooltip->y = y;
2170 tooltip_timer_reset(tooltip);
2171 return 0;
2172 }
2173
2174 /* the handler might be triggered too fast via input device motion, so
2175 * we need this check here to make sure tooltip is fully initialized */
2176 if (parent->tooltip_count > 1)
2177 return 0;
2178
2179 tooltip = malloc(sizeof *tooltip);
2180 if (!tooltip)
2181 return -1;
2182
2183 parent->tooltip = tooltip;
2184 tooltip->parent = parent;
2185 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002186 tooltip->x = x;
2187 tooltip->y = y;
2188 tooltip->entry = strdup(entry);
2189 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2190 if (tooltip->tooltip_fd < 0) {
2191 fprintf(stderr, "could not create timerfd\n: %m");
2192 return -1;
2193 }
2194
2195 tooltip->tooltip_task.run = tooltip_func;
2196 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2197 EPOLLIN, &tooltip->tooltip_task);
2198 tooltip_timer_reset(tooltip);
2199
2200 return 0;
2201}
2202
2203static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002204frame_resize_handler(struct widget *widget,
2205 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002206{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002207 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002208 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002209 struct rectangle interior;
2210 struct rectangle input;
2211 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002213 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002214 interior.x = 0;
2215 interior.y = 0;
2216 interior.width = width;
2217 interior.height = height;
2218 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002219 frame_resize(frame->frame, width, height);
2220 frame_interior(frame->frame, &interior.x, &interior.y,
2221 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002222 }
2223
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002224 widget_set_allocation(child, interior.x, interior.y,
2225 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002227 if (child->resize_handler) {
2228 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002229 child->user_data);
2230
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002231 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002232 width = child->allocation.width;
2233 height = child->allocation.height;
2234 } else {
2235 frame_resize_inside(frame->frame,
2236 child->allocation.width,
2237 child->allocation.height);
2238 width = frame_width(frame->frame);
2239 height = frame_height(frame->frame);
2240 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002241 }
2242
Scott Moreauf7e498c2012-05-14 11:39:29 -06002243 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002244
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002245 widget->surface->input_region =
2246 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002247 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002248 frame_input_rect(frame->frame, &input.x, &input.y,
2249 &input.width, &input.height);
2250 wl_region_add(widget->surface->input_region,
2251 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002252 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002253 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002254 }
2255
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002256 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002257
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002258 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002259 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002260 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2261 &opaque.width, &opaque.height);
2262
2263 wl_region_add(widget->surface->opaque_region,
2264 opaque.x, opaque.y,
2265 opaque.width, opaque.height);
2266 } else {
2267 wl_region_add(widget->surface->opaque_region,
2268 0, 0, width, height);
2269 }
Martin Minarik1998b152012-05-10 02:04:35 +02002270 }
2271
Martin Minarik1998b152012-05-10 02:04:35 +02002272
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002273 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002274}
2275
2276static void
2277frame_redraw_handler(struct widget *widget, void *data)
2278{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002279 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002280 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002281 struct window *window = widget->window;
2282
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002283 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002284 return;
2285
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002286 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002287
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002288 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002289
2290 cairo_destroy(cr);
2291}
2292
2293static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002294frame_get_pointer_image_for_location(struct window_frame *frame,
2295 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002296{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002297 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002298
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002299 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002300 return CURSOR_LEFT_PTR;
2301
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002302 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002303 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002304 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002305 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002306 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002307 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002308 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002309 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002310 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002311 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002312 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002313 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002314 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002315 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002316 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002317 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002318 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002319 case THEME_LOCATION_EXTERIOR:
2320 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002321 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002322 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002323 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002324}
2325
Pekka Paalanen26237862014-09-10 15:10:30 +03002326static void
2327frame_menu_func(void *data, struct input *input, int index)
2328{
2329 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002330
2331 switch (index) {
2332 case 0: /* close */
2333 window_close(window);
2334 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002335 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002336 /* we don't have a way to get out of fullscreen for now */
2337 if (window->fullscreen_handler)
2338 window->fullscreen_handler(window, window->user_data);
2339 break;
2340 }
2341}
2342
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002343void
2344window_show_frame_menu(struct window *window,
2345 struct input *input, uint32_t time)
2346{
2347 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002348 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002349
Pekka Paalanen26237862014-09-10 15:10:30 +03002350 static const char *entries[] = {
2351 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002352 "Fullscreen"
2353 };
2354
2355 if (window->fullscreen_handler)
2356 count = ARRAY_LENGTH(entries);
2357 else
2358 count = ARRAY_LENGTH(entries) - 1;
2359
2360 input_get_position(input, &x, &y);
2361 window_show_menu(window->display, input, time, window,
2362 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002363}
2364
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002365static int
2366frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002367 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002368{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002369 struct window_frame *frame = data;
2370 enum theme_location location;
2371
2372 location = frame_pointer_enter(frame->frame, input, x, y);
2373 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2374 widget_schedule_redraw(frame->widget);
2375
2376 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002377}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002378
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002379static int
2380frame_motion_handler(struct widget *widget,
2381 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002382 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002383{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002384 struct window_frame *frame = data;
2385 enum theme_location location;
2386
2387 location = frame_pointer_motion(frame->frame, input, x, y);
2388 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2389 widget_schedule_redraw(frame->widget);
2390
2391 return frame_get_pointer_image_for_location(data, location);
2392}
2393
2394static void
2395frame_leave_handler(struct widget *widget,
2396 struct input *input, void *data)
2397{
2398 struct window_frame *frame = data;
2399
2400 frame_pointer_leave(frame->frame, input);
2401 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2402 widget_schedule_redraw(frame->widget);
2403}
2404
2405static void
2406frame_handle_status(struct window_frame *frame, struct input *input,
2407 uint32_t time, enum theme_location location)
2408{
2409 struct window *window = frame->widget->window;
2410 uint32_t status;
2411
2412 status = frame_status(frame->frame);
2413 if (status & FRAME_STATUS_REPAINT)
2414 widget_schedule_redraw(frame->widget);
2415
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002416 if (status & FRAME_STATUS_MINIMIZE) {
2417 window_set_minimized(window);
2418 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2419 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002420
2421 if (status & FRAME_STATUS_MENU) {
2422 window_show_frame_menu(window, input, time);
2423 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2424 }
2425
2426 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002427 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002428 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2429 }
2430
2431 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002432 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002433 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002434 }
2435
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002436 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002437 input_ungrab(input);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002438 zxdg_toplevel_v6_move(window->xdg_toplevel,
2439 input_get_seat(input),
2440 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002441
2442 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2443 }
2444
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002445 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002446 input_ungrab(input);
2447
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002448 zxdg_toplevel_v6_resize(window->xdg_toplevel,
2449 input_get_seat(input),
2450 window->display->serial,
2451 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002452
2453 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2454 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002455}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002456
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002457#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002458static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002459frame_button_handler(struct widget *widget,
2460 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002461 uint32_t button, enum wl_pointer_button_state state,
2462 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002463
2464{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002465 struct window_frame *frame = data;
2466 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002467
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002468 frame->double_click = 0;
2469 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2470 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2471 frame->double_click = 1;
2472 frame->did_double = 1;
2473 } else
2474 frame->did_double = 0;
2475
2476 frame->last_time = time;
2477 } else if (frame->did_double == 1) {
2478 frame->double_click = 1;
2479 frame->did_double = 0;
2480 }
2481
2482 if (frame->double_click)
2483 location = frame_double_click(frame->frame, input,
2484 button, state);
2485 else
2486 location = frame_pointer_button(frame->frame, input,
2487 button, state);
2488
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002489 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002490}
2491
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002492static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002493frame_touch_down_handler(struct widget *widget, struct input *input,
2494 uint32_t serial, uint32_t time, int32_t id,
2495 float x, float y, void *data)
2496{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002497 struct window_frame *frame = data;
2498
Xiong Zhang382de462014-06-12 11:06:26 +08002499 frame->double_click = 0;
2500 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2501 frame->last_id == id) {
2502 frame->double_click = 1;
2503 frame->did_double = 1;
2504 frame->double_id = id;
2505 } else
2506 frame->did_double = 0;
2507
2508 frame->last_time = time;
2509 frame->last_id = id;
2510
2511 if (frame->double_click)
2512 frame_double_touch_down(frame->frame, input, id, x, y);
2513 else
2514 frame_touch_down(frame->frame, input, id, x, y);
2515
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002516 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2517}
2518
2519static void
2520frame_touch_up_handler(struct widget *widget,
2521 struct input *input, uint32_t serial, uint32_t time,
2522 int32_t id, void *data)
2523{
2524 struct window_frame *frame = data;
2525
Xiong Zhang382de462014-06-12 11:06:26 +08002526 if (frame->double_id == id && frame->did_double) {
2527 frame->did_double = 0;
2528 frame->double_id = 0;
2529 frame_double_touch_up(frame->frame, input, id);
2530 } else
2531 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002532 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002533}
2534
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002535struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002536window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002537{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002538 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002539 uint32_t buttons;
2540
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002541 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002542 buttons = FRAME_BUTTON_NONE;
2543 } else {
2544 buttons = FRAME_BUTTON_ALL;
2545 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002546
Peter Huttererf3d62272013-08-08 11:57:05 +10002547 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002548 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002549 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002550
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002551 frame->widget = window_add_widget(window, frame);
2552 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002553
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002554 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2555 widget_set_resize_handler(frame->widget, frame_resize_handler);
2556 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002557 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002558 widget_set_motion_handler(frame->widget, frame_motion_handler);
2559 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002560 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002561 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002562
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002563 window->frame = frame;
2564
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002565 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002566}
2567
Kristian Høgsberga1627922012-06-20 17:30:03 -04002568void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002569window_frame_set_child_size(struct widget *widget, int child_width,
2570 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002571{
2572 struct display *display = widget->window->display;
2573 struct theme *t = display->theme;
2574 int decoration_width, decoration_height;
2575 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002576 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002577
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002578 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002579 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002580 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002581 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002582
2583 width = child_width + decoration_width;
2584 height = child_height + decoration_height;
2585 } else {
2586 width = child_width;
2587 height = child_height;
2588 }
2589
2590 window_schedule_resize(widget->window, width, height);
2591}
2592
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002593static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002594window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002595{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002596 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002597
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002598 /* frame->child must be destroyed by the application */
2599 widget_destroy(frame->widget);
2600 free(frame);
2601}
2602
2603static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002604input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002605 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002606{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002607 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002608 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002609
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002610 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002611 return;
2612
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002613 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002614 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002615 widget = old;
2616 if (input->grab)
2617 widget = input->grab;
2618 if (widget->leave_handler)
2619 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002620 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002621 }
2622
2623 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002624 widget = focus;
2625 if (input->grab)
2626 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002627 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002628 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002629 cursor = widget->enter_handler(focus, input, x, y,
2630 widget->user_data);
2631 else
2632 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002633
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002634 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002635 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002636}
2637
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002638void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002639touch_grab(struct input *input, int32_t touch_id)
2640{
2641 input->touch_grab = 1;
2642 input->touch_grab_id = touch_id;
2643}
2644
2645void
2646touch_ungrab(struct input *input)
2647{
2648 struct touch_point *tp, *tmp;
2649
2650 input->touch_grab = 0;
2651
2652 wl_list_for_each_safe(tp, tmp,
2653 &input->touch_point_list, link) {
2654 if (tp->id != input->touch_grab_id)
2655 continue;
2656 wl_list_remove(&tp->link);
2657 free(tp);
2658
2659 return;
2660 }
2661}
2662
2663void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002664input_grab(struct input *input, struct widget *widget, uint32_t button)
2665{
2666 input->grab = widget;
2667 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002668
2669 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002670}
2671
2672void
2673input_ungrab(struct input *input)
2674{
2675 struct widget *widget;
2676
2677 input->grab = NULL;
2678 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002679 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002680 input->sx, input->sy);
2681 input_set_focus_widget(input, widget, input->sx, input->sy);
2682 }
2683}
2684
2685static void
Derek Foreman118a4292015-04-22 17:23:35 -05002686cursor_delay_timer_reset(struct input *input, uint32_t duration)
2687{
2688 struct itimerspec its;
2689
2690 if (!duration)
2691 input->cursor_timer_running = false;
2692 else
2693 input->cursor_timer_running = true;
2694
2695 its.it_interval.tv_sec = 0;
2696 its.it_interval.tv_nsec = 0;
2697 its.it_value.tv_sec = duration / 1000;
2698 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2699 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2700 fprintf(stderr, "could not set cursor timerfd\n: %m");
2701}
2702
2703static void cancel_pointer_image_update(struct input *input)
2704{
2705 if (input->cursor_timer_running)
2706 cursor_delay_timer_reset(input, 0);
2707}
2708
2709static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002710input_remove_pointer_focus(struct input *input)
2711{
2712 struct window *window = input->pointer_focus;
2713
2714 if (!window)
2715 return;
2716
2717 input_set_focus_widget(input, NULL, 0, 0);
2718
2719 input->pointer_focus = NULL;
2720 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002721 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002722}
2723
2724static void
2725pointer_handle_enter(void *data, struct wl_pointer *pointer,
2726 uint32_t serial, struct wl_surface *surface,
2727 wl_fixed_t sx_w, wl_fixed_t sy_w)
2728{
2729 struct input *input = data;
2730 struct window *window;
2731 struct widget *widget;
2732 float sx = wl_fixed_to_double(sx_w);
2733 float sy = wl_fixed_to_double(sy_w);
2734
2735 if (!surface) {
2736 /* enter event for a window we've just destroyed */
2737 return;
2738 }
2739
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002740 window = wl_surface_get_user_data(surface);
2741 if (surface != window->main_surface->surface) {
2742 DBG("Ignoring input event from subsurface %p\n", surface);
2743 return;
2744 }
2745
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002746 input->display->serial = serial;
2747 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002748 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002749
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002750 input->sx = sx;
2751 input->sy = sy;
2752
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002753 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002754 input_set_focus_widget(input, widget, sx, sy);
2755}
2756
2757static void
2758pointer_handle_leave(void *data, struct wl_pointer *pointer,
2759 uint32_t serial, struct wl_surface *surface)
2760{
2761 struct input *input = data;
2762
2763 input->display->serial = serial;
2764 input_remove_pointer_focus(input);
2765}
2766
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002767static void
Daniel Stone37816df2012-05-16 18:45:18 +01002768pointer_handle_motion(void *data, struct wl_pointer *pointer,
2769 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002770{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002771 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002772 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002773 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002774 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002775 float sx = wl_fixed_to_double(sx_w);
2776 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002777
Paul Winwoodb22bf572013-08-29 10:52:54 +01002778 if (!window)
2779 return;
2780
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002781 input->sx = sx;
2782 input->sy = sy;
2783
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05002784 /* when making the window smaller - e.g. after an unmaximise we might
Rob Bradford5f087742013-07-11 19:41:27 +01002785 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002786 * based on the old surface dimensions. However, if we have an active
2787 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002788 */
Derek Foreman46812b62015-08-26 17:13:27 -05002789 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002790 sy < window->main_surface->allocation.y ||
2791 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002792 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002793 return;
2794
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002795 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002796 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002797 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002798 }
2799
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002800 if (input->grab)
2801 widget = input->grab;
2802 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002803 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002804 if (widget) {
2805 if (widget->motion_handler)
2806 cursor = widget->motion_handler(input->focus_widget,
2807 input, time, sx, sy,
2808 widget->user_data);
2809 else
2810 cursor = widget->default_cursor;
2811 } else
2812 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002813
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002814 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002815}
2816
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002817static void
Daniel Stone37816df2012-05-16 18:45:18 +01002818pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002819 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002820{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002821 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002822 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002823 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002824
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002825 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002826 if (input->focus_widget && input->grab == NULL &&
2827 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002828 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002829
Neil Roberts6b28aad2012-01-23 19:11:18 +00002830 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002831 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002832 (*widget->button_handler)(widget,
2833 input, time,
2834 button, state,
2835 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002836
Daniel Stone4dbadb12012-05-30 16:31:51 +01002837 if (input->grab && input->grab_button == button &&
2838 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002839 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002840}
2841
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002842static void
Daniel Stone37816df2012-05-16 18:45:18 +01002843pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002844 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002845{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002846 struct input *input = data;
2847 struct widget *widget;
2848
2849 widget = input->focus_widget;
2850 if (input->grab)
2851 widget = input->grab;
2852 if (widget && widget->axis_handler)
2853 (*widget->axis_handler)(widget,
2854 input, time,
2855 axis, value,
2856 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002857}
2858
Peter Hutterer87743e92016-01-18 16:38:22 +10002859static void
2860pointer_handle_frame(void *data, struct wl_pointer *pointer)
2861{
2862 struct input *input = data;
2863 struct widget *widget;
2864
2865 widget = input->focus_widget;
2866 if (input->grab)
2867 widget = input->grab;
2868 if (widget && widget->pointer_frame_handler)
2869 (*widget->pointer_frame_handler)(widget,
2870 input,
2871 widget->user_data);
2872}
2873
2874static void
2875pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2876 uint32_t source)
2877{
2878 struct input *input = data;
2879 struct widget *widget;
2880
2881 widget = input->focus_widget;
2882 if (input->grab)
2883 widget = input->grab;
2884 if (widget && widget->axis_source_handler)
2885 (*widget->axis_source_handler)(widget,
2886 input,
2887 source,
2888 widget->user_data);
2889}
2890
2891static void
2892pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2893 uint32_t time, uint32_t axis)
2894{
2895 struct input *input = data;
2896 struct widget *widget;
2897
2898 widget = input->focus_widget;
2899 if (input->grab)
2900 widget = input->grab;
2901 if (widget && widget->axis_stop_handler)
2902 (*widget->axis_stop_handler)(widget,
2903 input, time,
2904 axis,
2905 widget->user_data);
2906}
2907
2908static void
2909pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2910 uint32_t axis, int32_t discrete)
2911{
2912 struct input *input = data;
2913 struct widget *widget;
2914
2915 widget = input->focus_widget;
2916 if (input->grab)
2917 widget = input->grab;
2918 if (widget && widget->axis_discrete_handler)
2919 (*widget->axis_discrete_handler)(widget,
2920 input,
2921 axis,
2922 discrete,
2923 widget->user_data);
2924}
2925
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002926static const struct wl_pointer_listener pointer_listener = {
2927 pointer_handle_enter,
2928 pointer_handle_leave,
2929 pointer_handle_motion,
2930 pointer_handle_button,
2931 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002932 pointer_handle_frame,
2933 pointer_handle_axis_source,
2934 pointer_handle_axis_stop,
2935 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002936};
2937
2938static void
2939input_remove_keyboard_focus(struct input *input)
2940{
2941 struct window *window = input->keyboard_focus;
2942 struct itimerspec its;
2943
2944 its.it_interval.tv_sec = 0;
2945 its.it_interval.tv_nsec = 0;
2946 its.it_value.tv_sec = 0;
2947 its.it_value.tv_nsec = 0;
2948 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2949
2950 if (!window)
2951 return;
2952
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002953 if (window->keyboard_focus_handler)
2954 (*window->keyboard_focus_handler)(window, NULL,
2955 window->user_data);
2956
2957 input->keyboard_focus = NULL;
2958}
2959
2960static void
2961keyboard_repeat_func(struct task *task, uint32_t events)
2962{
2963 struct input *input =
2964 container_of(task, struct input, repeat_task);
2965 struct window *window = input->keyboard_focus;
2966 uint64_t exp;
2967
2968 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2969 /* If we change the timer between the fd becoming
2970 * readable and getting here, there'll be nothing to
2971 * read and we get EAGAIN. */
2972 return;
2973
2974 if (window && window->key_handler) {
2975 (*window->key_handler)(window, input, input->repeat_time,
2976 input->repeat_key, input->repeat_sym,
2977 WL_KEYBOARD_KEY_STATE_PRESSED,
2978 window->user_data);
2979 }
2980}
2981
2982static void
2983keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2984 uint32_t format, int fd, uint32_t size)
2985{
2986 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002987 struct xkb_keymap *keymap;
2988 struct xkb_state *state;
Daniel Stone5b015962016-10-20 14:45:58 +01002989#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002990 struct xkb_compose_table *compose_table;
2991 struct xkb_compose_state *compose_state;
Daniel Stone5b015962016-10-20 14:45:58 +01002992#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002993 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002994 char *map_str;
2995
2996 if (!data) {
2997 close(fd);
2998 return;
2999 }
3000
3001 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3002 close(fd);
3003 return;
3004 }
3005
3006 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3007 if (map_str == MAP_FAILED) {
3008 close(fd);
3009 return;
3010 }
3011
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003012 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03003013 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
3014 map_str,
3015 XKB_KEYMAP_FORMAT_TEXT_V1,
3016 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003017 munmap(map_str, size);
3018 close(fd);
3019
Rui Matos3eccb862013-10-10 19:44:22 +02003020 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003021 fprintf(stderr, "failed to compile keymap\n");
3022 return;
3023 }
3024
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003025 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02003026 state = xkb_state_new(keymap);
3027 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003028 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003029 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003030 return;
3031 }
3032
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003033 /* Look up the preferred locale, falling back to "C" as default */
3034 if (!(locale = getenv("LC_ALL")))
3035 if (!(locale = getenv("LC_CTYPE")))
3036 if (!(locale = getenv("LANG")))
3037 locale = "C";
3038
3039 /* Set up XKB compose table */
Daniel Stone5b015962016-10-20 14:45:58 +01003040#ifdef HAVE_XKBCOMMON_COMPOSE
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003041 compose_table =
3042 xkb_compose_table_new_from_locale(input->display->xkb_context,
3043 locale,
3044 XKB_COMPOSE_COMPILE_NO_FLAGS);
3045 if (compose_table) {
3046 /* Set up XKB compose state */
3047 compose_state = xkb_compose_state_new(compose_table,
3048 XKB_COMPOSE_STATE_NO_FLAGS);
3049 if (compose_state) {
3050 xkb_compose_state_unref(input->xkb.compose_state);
3051 xkb_compose_table_unref(input->xkb.compose_table);
3052 input->xkb.compose_state = compose_state;
3053 input->xkb.compose_table = compose_table;
3054 } else {
3055 fprintf(stderr, "could not create XKB compose state. "
3056 "Disabiling compose.\n");
3057 xkb_compose_table_unref(compose_table);
3058 compose_table = NULL;
3059 }
3060 } else {
3061 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3062 "Disabiling compose\n", locale);
3063 }
Daniel Stone5b015962016-10-20 14:45:58 +01003064#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003065
Rui Matos3eccb862013-10-10 19:44:22 +02003066 xkb_keymap_unref(input->xkb.keymap);
3067 xkb_state_unref(input->xkb.state);
3068 input->xkb.keymap = keymap;
3069 input->xkb.state = state;
3070
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003071 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003072 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003073 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003074 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003075 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003076 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003077}
3078
3079static void
3080keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3081 uint32_t serial, struct wl_surface *surface,
3082 struct wl_array *keys)
3083{
3084 struct input *input = data;
3085 struct window *window;
3086
Dima Ryazanov3b7c2072016-11-30 12:10:55 -08003087 if (!surface) {
3088 /* enter event for a window we've just destroyed */
3089 return;
3090 }
3091
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003092 input->display->serial = serial;
3093 input->keyboard_focus = wl_surface_get_user_data(surface);
3094
3095 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003096 if (window->keyboard_focus_handler)
3097 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003098 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003099}
3100
3101static void
3102keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3103 uint32_t serial, struct wl_surface *surface)
3104{
3105 struct input *input = data;
3106
3107 input->display->serial = serial;
3108 input_remove_keyboard_focus(input);
3109}
3110
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003111/* Translate symbols appropriately if a compose sequence is being entered */
3112static xkb_keysym_t
3113process_key_press(xkb_keysym_t sym, struct input *input)
3114{
Daniel Stone5b015962016-10-20 14:45:58 +01003115#ifdef HAVE_XKBCOMMON_COMPOSE
Derek Foreman2b685d92016-11-22 13:00:38 -06003116 if (!input->xkb.compose_state)
3117 return sym;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003118 if (sym == XKB_KEY_NoSymbol)
3119 return sym;
3120 if (xkb_compose_state_feed(input->xkb.compose_state,
3121 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3122 return sym;
3123
3124 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3125 case XKB_COMPOSE_COMPOSING:
3126 return XKB_KEY_NoSymbol;
3127 case XKB_COMPOSE_COMPOSED:
3128 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3129 case XKB_COMPOSE_CANCELLED:
3130 return XKB_KEY_NoSymbol;
3131 case XKB_COMPOSE_NOTHING:
3132 return sym;
3133 default:
3134 return sym;
3135 }
Daniel Stone5b015962016-10-20 14:45:58 +01003136#else
3137 return sym;
3138#endif
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003139}
3140
Scott Moreau210d0792012-03-22 10:47:01 -06003141static void
Daniel Stone37816df2012-05-16 18:45:18 +01003142keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003143 uint32_t serial, uint32_t time, uint32_t key,
3144 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003145{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003146 struct input *input = data;
3147 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003148 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003149 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003150 const xkb_keysym_t *syms;
3151 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003152 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003153
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003154 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003155 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003156 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003157 return;
3158
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003159 /* We only use input grabs for pointer events for now, so just
3160 * ignore key presses if a grab is active. We expand the key
3161 * event delivery mechanism to route events to widgets to
3162 * properly handle key grabs. In the meantime, this prevents
Bryce Harringtone57b6a12016-10-11 16:06:44 -07003163 * key event delivery while a grab is active. */
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003164 if (input->grab && input->grab_button == 0)
3165 return;
3166
Ran Benita2e1968f2014-08-19 23:59:51 +03003167 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003168
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003169 sym = XKB_KEY_NoSymbol;
3170 if (num_syms == 1)
3171 sym = syms[0];
3172
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003173
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003174 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003175 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003176 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003177 } else if (sym == XKB_KEY_F11 &&
3178 window->fullscreen_handler &&
3179 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3180 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003181 } else if (sym == XKB_KEY_F4 &&
3182 input->modifiers == MOD_ALT_MASK &&
3183 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003184 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003185 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003186 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3187 sym = process_key_press(sym, input);
3188
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003189 (*window->key_handler)(window, input, time, key,
3190 sym, state, window->user_data);
3191 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003192
3193 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3194 key == input->repeat_key) {
3195 its.it_interval.tv_sec = 0;
3196 its.it_interval.tv_nsec = 0;
3197 its.it_value.tv_sec = 0;
3198 its.it_value.tv_nsec = 0;
3199 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003200 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3201 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003202 input->repeat_sym = sym;
3203 input->repeat_key = key;
3204 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003205 its.it_interval.tv_sec = input->repeat_rate_sec;
3206 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3207 its.it_value.tv_sec = input->repeat_delay_sec;
3208 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003209 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3210 }
3211}
3212
3213static void
Daniel Stone351eb612012-05-31 15:27:47 -04003214keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3215 uint32_t serial, uint32_t mods_depressed,
3216 uint32_t mods_latched, uint32_t mods_locked,
3217 uint32_t group)
3218{
3219 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003220 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003221
Matt Ropere61561f2013-06-24 16:52:43 +01003222 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3223 if (!input->xkb.keymap)
3224 return;
3225
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003226 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3227 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003228 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003229 XKB_STATE_MODS_DEPRESSED |
3230 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003231 input->modifiers = 0;
3232 if (mask & input->xkb.control_mask)
3233 input->modifiers |= MOD_CONTROL_MASK;
3234 if (mask & input->xkb.alt_mask)
3235 input->modifiers |= MOD_ALT_MASK;
3236 if (mask & input->xkb.shift_mask)
3237 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003238}
3239
Jonny Lamb06959082014-08-12 14:58:27 +02003240static void
3241set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3242{
3243 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3244 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3245
3246 /* a rate of zero disables any repeating, regardless of the delay's
3247 * value */
3248 if (rate == 0)
3249 return;
3250
3251 if (rate == 1)
3252 input->repeat_rate_sec = 1;
3253 else
3254 input->repeat_rate_nsec = 1000000000 / rate;
3255
3256 input->repeat_delay_sec = delay / 1000;
3257 delay -= (input->repeat_delay_sec * 1000);
3258 input->repeat_delay_nsec = delay * 1000 * 1000;
3259}
3260
3261static void
3262keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3263 int32_t rate, int32_t delay)
3264{
3265 struct input *input = data;
3266
3267 set_repeat_info(input, rate, delay);
3268}
3269
Daniel Stone37816df2012-05-16 18:45:18 +01003270static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003271 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003272 keyboard_handle_enter,
3273 keyboard_handle_leave,
3274 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003275 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003276 keyboard_handle_repeat_info
3277
Daniel Stone37816df2012-05-16 18:45:18 +01003278};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003279
3280static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003281touch_handle_down(void *data, struct wl_touch *wl_touch,
3282 uint32_t serial, uint32_t time, struct wl_surface *surface,
3283 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3284{
3285 struct input *input = data;
3286 struct widget *widget;
3287 float sx = wl_fixed_to_double(x_w);
3288 float sy = wl_fixed_to_double(y_w);
3289
Rusty Lynch1084da52013-08-15 09:10:08 -07003290 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003291 input->touch_focus = wl_surface_get_user_data(surface);
3292 if (!input->touch_focus) {
3293 DBG("Failed to find to touch focus for surface %p\n", surface);
3294 return;
3295 }
3296
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003297 if (surface != input->touch_focus->main_surface->surface) {
3298 DBG("Ignoring input event from subsurface %p\n", surface);
3299 input->touch_focus = NULL;
3300 return;
3301 }
3302
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003303 if (input->grab)
3304 widget = input->grab;
3305 else
3306 widget = window_find_widget(input->touch_focus,
3307 wl_fixed_to_double(x_w),
3308 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003309 if (widget) {
3310 struct touch_point *tp = xmalloc(sizeof *tp);
3311 if (tp) {
3312 tp->id = id;
3313 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003314 tp->x = sx;
3315 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003316 wl_list_insert(&input->touch_point_list, &tp->link);
3317
3318 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003319 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003320 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003321 sx, sy,
3322 widget->user_data);
3323 }
3324 }
3325}
3326
3327static void
3328touch_handle_up(void *data, struct wl_touch *wl_touch,
3329 uint32_t serial, uint32_t time, int32_t id)
3330{
3331 struct input *input = data;
3332 struct touch_point *tp, *tmp;
3333
Rusty Lynch041815a2013-08-08 21:20:38 -07003334 if (!input->touch_focus) {
3335 DBG("No touch focus found for touch up event!\n");
3336 return;
3337 }
3338
3339 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3340 if (tp->id != id)
3341 continue;
3342
3343 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003344 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003345 time, id,
3346 tp->widget->user_data);
3347
3348 wl_list_remove(&tp->link);
3349 free(tp);
3350
3351 return;
3352 }
3353}
3354
3355static void
3356touch_handle_motion(void *data, struct wl_touch *wl_touch,
3357 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3358{
3359 struct input *input = data;
3360 struct touch_point *tp;
3361 float sx = wl_fixed_to_double(x_w);
3362 float sy = wl_fixed_to_double(y_w);
3363
3364 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3365
3366 if (!input->touch_focus) {
3367 DBG("No touch focus found for touch motion event!\n");
3368 return;
3369 }
3370
3371 wl_list_for_each(tp, &input->touch_point_list, link) {
3372 if (tp->id != id)
3373 continue;
3374
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003375 tp->x = sx;
3376 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003377 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003378 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003379 id, sx, sy,
3380 tp->widget->user_data);
3381 return;
3382 }
3383}
3384
3385static void
3386touch_handle_frame(void *data, struct wl_touch *wl_touch)
3387{
3388 struct input *input = data;
3389 struct touch_point *tp, *tmp;
3390
3391 DBG("touch_handle_frame\n");
3392
3393 if (!input->touch_focus) {
3394 DBG("No touch focus found for touch frame event!\n");
3395 return;
3396 }
3397
3398 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3399 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003400 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003401 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003402 }
3403}
3404
3405static void
3406touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3407{
3408 struct input *input = data;
3409 struct touch_point *tp, *tmp;
3410
3411 DBG("touch_handle_cancel\n");
3412
3413 if (!input->touch_focus) {
3414 DBG("No touch focus found for touch cancel event!\n");
3415 return;
3416 }
3417
3418 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3419 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003420 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3421 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003422
3423 wl_list_remove(&tp->link);
3424 free(tp);
3425 }
3426}
3427
3428static const struct wl_touch_listener touch_listener = {
3429 touch_handle_down,
3430 touch_handle_up,
3431 touch_handle_motion,
3432 touch_handle_frame,
3433 touch_handle_cancel,
3434};
3435
3436static void
Daniel Stone37816df2012-05-16 18:45:18 +01003437seat_handle_capabilities(void *data, struct wl_seat *seat,
3438 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003439{
Daniel Stone37816df2012-05-16 18:45:18 +01003440 struct input *input = data;
3441
3442 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3443 input->pointer = wl_seat_get_pointer(seat);
3444 wl_pointer_set_user_data(input->pointer, input);
3445 wl_pointer_add_listener(input->pointer, &pointer_listener,
3446 input);
3447 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003448 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003449 wl_pointer_release(input->pointer);
3450 else
3451 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003452 input->pointer = NULL;
3453 }
3454
3455 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3456 input->keyboard = wl_seat_get_keyboard(seat);
3457 wl_keyboard_set_user_data(input->keyboard, input);
3458 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3459 input);
3460 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003461 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003462 wl_keyboard_release(input->keyboard);
3463 else
3464 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003465 input->keyboard = NULL;
3466 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003467
3468 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3469 input->touch = wl_seat_get_touch(seat);
3470 wl_touch_set_user_data(input->touch, input);
3471 wl_touch_add_listener(input->touch, &touch_listener, input);
3472 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003473 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003474 wl_touch_release(input->touch);
3475 else
3476 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003477 input->touch = NULL;
3478 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003479}
3480
Rob Bradford08031182013-08-13 20:11:03 +01003481static void
3482seat_handle_name(void *data, struct wl_seat *seat,
3483 const char *name)
3484{
3485
3486}
3487
Daniel Stone37816df2012-05-16 18:45:18 +01003488static const struct wl_seat_listener seat_listener = {
3489 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003490 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003491};
3492
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003493void
3494input_get_position(struct input *input, int32_t *x, int32_t *y)
3495{
3496 *x = input->sx;
3497 *y = input->sy;
3498}
3499
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003500int
3501input_get_touch(struct input *input, int32_t id, float *x, float *y)
3502{
3503 struct touch_point *tp;
3504
3505 wl_list_for_each(tp, &input->touch_point_list, link) {
3506 if (tp->id != id)
3507 continue;
3508
3509 *x = tp->x;
3510 *y = tp->y;
3511 return 0;
3512 }
3513
3514 return -1;
3515}
3516
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003517struct display *
3518input_get_display(struct input *input)
3519{
3520 return input->display;
3521}
3522
Daniel Stone37816df2012-05-16 18:45:18 +01003523struct wl_seat *
3524input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003525{
Daniel Stone37816df2012-05-16 18:45:18 +01003526 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003527}
3528
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003529uint32_t
3530input_get_modifiers(struct input *input)
3531{
3532 return input->modifiers;
3533}
3534
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003535struct widget *
3536input_get_focus_widget(struct input *input)
3537{
3538 return input->focus_widget;
3539}
3540
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003541struct data_offer {
3542 struct wl_data_offer *offer;
3543 struct input *input;
3544 struct wl_array types;
3545 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003546
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003547 struct task io_task;
3548 int fd;
3549 data_func_t func;
3550 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003551 uint32_t dnd_action;
3552 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003553 void *user_data;
3554};
3555
3556static void
3557data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3558{
3559 struct data_offer *offer = data;
3560 char **p;
3561
3562 p = wl_array_add(&offer->types, sizeof *p);
3563 *p = strdup(type);
3564}
3565
Carlos Garnacho9c931792016-01-18 23:52:12 +01003566static void
3567data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3568{
3569 struct data_offer *offer = data;
3570
3571 offer->source_actions = source_actions;
3572}
3573
3574static void
3575data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3576{
3577 struct data_offer *offer = data;
3578
3579 offer->dnd_action = dnd_action;
3580}
3581
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003582static const struct wl_data_offer_listener data_offer_listener = {
3583 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003584 data_offer_source_actions,
3585 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003586};
3587
3588static void
3589data_offer_destroy(struct data_offer *offer)
3590{
3591 char **p;
3592
3593 offer->refcount--;
3594 if (offer->refcount == 0) {
3595 wl_data_offer_destroy(offer->offer);
3596 for (p = offer->types.data; *p; p++)
3597 free(*p);
3598 wl_array_release(&offer->types);
3599 free(offer);
3600 }
3601}
3602
3603static void
3604data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003605 struct wl_data_device *data_device,
3606 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003607{
3608 struct data_offer *offer;
3609
Brian Lovinbc919262013-08-07 15:34:59 -07003610 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003611
3612 wl_array_init(&offer->types);
3613 offer->refcount = 1;
3614 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003615 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003616 wl_data_offer_add_listener(offer->offer,
3617 &data_offer_listener, offer);
3618}
3619
3620static void
3621data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003622 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003623 wl_fixed_t x_w, wl_fixed_t y_w,
3624 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003625{
3626 struct input *input = data;
3627 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003628 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003629 float x = wl_fixed_to_double(x_w);
3630 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003631 char **p;
3632
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003633 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003634 input->drag_enter_serial = serial;
3635 input->drag_focus = window,
3636 input->drag_x = x;
3637 input->drag_y = y;
3638
3639 if (!input->touch_grab)
3640 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003641
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003642 if (offer) {
3643 input->drag_offer = wl_data_offer_get_user_data(offer);
3644
3645 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3646 *p = NULL;
3647
3648 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003649
3650 if (input->display->data_device_manager_version >=
3651 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3652 wl_data_offer_set_actions(offer,
3653 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3654 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3655 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3656 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003657 } else {
3658 input->drag_offer = NULL;
3659 types_data = NULL;
3660 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003661
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003662 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003663 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003664 window->user_data);
3665}
3666
3667static void
3668data_device_leave(void *data, struct wl_data_device *data_device)
3669{
3670 struct input *input = data;
3671
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003672 if (input->drag_offer) {
3673 data_offer_destroy(input->drag_offer);
3674 input->drag_offer = NULL;
3675 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003676}
3677
3678static void
3679data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003680 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003681{
3682 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003683 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003684 float x = wl_fixed_to_double(x_w);
3685 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003686 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003687
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003688 input->drag_x = x;
3689 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003690
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003691 if (input->drag_offer)
3692 types_data = input->drag_offer->types.data;
3693 else
3694 types_data = NULL;
3695
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003696 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003697 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003698 window->user_data);
3699}
3700
3701static void
3702data_device_drop(void *data, struct wl_data_device *data_device)
3703{
3704 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003705 struct window *window = input->drag_focus;
3706 float x, y;
3707
3708 x = input->drag_x;
3709 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003710
3711 if (window->drop_handler)
3712 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003713 x, y, window->user_data);
3714
3715 if (input->touch_grab)
3716 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003717}
3718
3719static void
3720data_device_selection(void *data,
3721 struct wl_data_device *wl_data_device,
3722 struct wl_data_offer *offer)
3723{
3724 struct input *input = data;
3725 char **p;
3726
3727 if (input->selection_offer)
3728 data_offer_destroy(input->selection_offer);
3729
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003730 if (offer) {
3731 input->selection_offer = wl_data_offer_get_user_data(offer);
3732 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3733 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003734 } else {
3735 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003736 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003737}
3738
3739static const struct wl_data_device_listener data_device_listener = {
3740 data_device_data_offer,
3741 data_device_enter,
3742 data_device_leave,
3743 data_device_motion,
3744 data_device_drop,
3745 data_device_selection
3746};
3747
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003748static void
3749input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003750{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003751 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003752 struct wl_cursor *cursor;
3753 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003754
Daniel Stone80972742012-11-07 17:51:39 +11003755 if (!input->pointer)
3756 return;
3757
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003758 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003759 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003760 return;
3761
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003762 if (index >= (int) cursor->image_count) {
3763 fprintf(stderr, "cursor index out of range\n");
3764 return;
3765 }
3766
3767 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003768 buffer = wl_cursor_image_get_buffer(image);
3769 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003770 return;
3771
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003772 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3773 wl_surface_damage(input->pointer_surface, 0, 0,
3774 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003775 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003776 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3777 input->pointer_surface,
3778 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003779}
3780
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003781static const struct wl_callback_listener pointer_surface_listener;
3782
Derek Foreman493d9792015-03-04 16:26:25 -06003783static bool
3784input_set_pointer_special(struct input *input)
3785{
3786 if (input->current_cursor == CURSOR_BLANK) {
3787 wl_pointer_set_cursor(input->pointer,
3788 input->pointer_enter_serial,
3789 NULL, 0, 0);
3790 return true;
3791 }
3792
3793 if (input->current_cursor == CURSOR_UNSET)
3794 return true;
3795
3796 return false;
3797}
3798
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003799static void
Derek Foreman118a4292015-04-22 17:23:35 -05003800schedule_pointer_image_update(struct input *input,
3801 struct wl_cursor *cursor,
3802 uint32_t duration,
3803 bool force_frame)
3804{
3805 /* Some silly cursor sets have enormous pauses in them. In these
3806 * cases it's better to use a timer even if it results in less
3807 * accurate presentation, since it will save us having to set the
3808 * same cursor image over and over again.
3809 *
3810 * This is really not the way we're supposed to time any kind of
3811 * animation, but we're pretending it's OK here because we don't
3812 * want animated cursors with long delays to needlessly hog CPU.
3813 *
3814 * We use force_frame to ensure we don't accumulate large timing
3815 * errors by running off the wrong clock.
3816 */
3817 if (!force_frame && duration > 100) {
3818 struct timespec tp;
3819
3820 clock_gettime(CLOCK_MONOTONIC, &tp);
3821 input->cursor_timer_start = tp.tv_sec * 1000
3822 + tp.tv_nsec / 1000000;
3823 cursor_delay_timer_reset(input, duration);
3824 return;
3825 }
3826
3827 /* for short durations we'll just spin on frame callbacks for
3828 * accurate timing - the way any kind of timing sensitive animation
3829 * should really be done. */
3830 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3831 wl_callback_add_listener(input->cursor_frame_cb,
3832 &pointer_surface_listener, input);
3833
3834}
3835
3836static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003837pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3838 uint32_t time)
3839{
3840 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003841 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003842 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003843 uint32_t duration;
3844 bool force_frame = true;
3845
3846 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003847
3848 if (callback) {
3849 assert(callback == input->cursor_frame_cb);
3850 wl_callback_destroy(callback);
3851 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003852 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003853 }
3854
Daniel Stone80972742012-11-07 17:51:39 +11003855 if (!input->pointer)
3856 return;
3857
Derek Foreman493d9792015-03-04 16:26:25 -06003858 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003859 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003860
Daniel Stonea494f1d2012-06-18 19:31:12 +01003861 cursor = input->display->cursors[input->current_cursor];
3862 if (!cursor)
3863 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003864
3865 /* FIXME We don't have the current time on the first call so we set
3866 * the animation start to the time of the first frame callback. */
3867 if (time == 0)
3868 input->cursor_anim_start = 0;
3869 else if (input->cursor_anim_start == 0)
3870 input->cursor_anim_start = time;
3871
Derek Foreman118a4292015-04-22 17:23:35 -05003872 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003873
Derek Foreman118a4292015-04-22 17:23:35 -05003874 if (time == 0 || input->cursor_anim_start == 0) {
3875 duration = 0;
3876 i = 0;
3877 } else
3878 i = wl_cursor_frame_and_duration(
3879 cursor,
3880 time - input->cursor_anim_start,
3881 &duration);
3882
3883 if (cursor->image_count > 1)
3884 schedule_pointer_image_update(input, cursor, duration,
3885 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003886
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003887 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003888}
3889
Derek Foreman118a4292015-04-22 17:23:35 -05003890static void
3891cursor_timer_func(struct task *task, uint32_t events)
3892{
3893 struct input *input = container_of(task, struct input, cursor_task);
3894 struct timespec tp;
3895 struct wl_cursor *cursor;
3896 uint32_t time;
3897 uint64_t exp;
3898
3899 if (!input->cursor_timer_running)
3900 return;
3901
3902 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3903 return;
3904
3905 cursor = input->display->cursors[input->current_cursor];
3906 if (!cursor)
3907 return;
3908
3909 clock_gettime(CLOCK_MONOTONIC, &tp);
3910 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3911 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3912}
3913
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003914static const struct wl_callback_listener pointer_surface_listener = {
3915 pointer_surface_frame_callback
3916};
3917
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003918void
3919input_set_pointer_image(struct input *input, int pointer)
3920{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003921 int force = 0;
3922
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003923 if (!input->pointer)
3924 return;
3925
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003926 if (input->pointer_enter_serial > input->cursor_serial)
3927 force = 1;
3928
3929 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003930 return;
3931
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003932 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003933 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003934 if (!input->cursor_frame_cb)
3935 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003936 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003937 /* The current frame callback may be stuck if, for instance,
3938 * the set cursor request was processed by the server after
3939 * this client lost the focus. In this case the cursor surface
3940 * might not be mapped and the frame callback wouldn't ever
3941 * complete. Send a set_cursor and attach to try to map the
3942 * cursor surface again so that the callback will finish */
3943 input_set_pointer_image_index(input, 0);
3944 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003945}
3946
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003947struct wl_data_device *
3948input_get_data_device(struct input *input)
3949{
3950 return input->data_device;
3951}
3952
3953void
3954input_set_selection(struct input *input,
3955 struct wl_data_source *source, uint32_t time)
3956{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003957 if (input->data_device)
3958 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003959}
3960
3961void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003962input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003963{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003964 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003965 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003966}
3967
3968static void
3969offer_io_func(struct task *task, uint32_t events)
3970{
3971 struct data_offer *offer =
3972 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003973 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003974 unsigned int len;
3975 char buffer[4096];
3976
3977 len = read(offer->fd, buffer, sizeof buffer);
3978 offer->func(buffer, len,
3979 offer->x, offer->y, offer->user_data);
3980
3981 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003982 if (display->data_device_manager_version >=
3983 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3984 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003985 close(offer->fd);
3986 data_offer_destroy(offer);
3987 }
3988}
3989
3990static void
3991data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3992 data_func_t func, void *user_data)
3993{
3994 int p[2];
3995
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003996 if (pipe2(p, O_CLOEXEC) == -1)
3997 return;
3998
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003999 wl_data_offer_receive(offer->offer, mime_type, p[1]);
4000 close(p[1]);
4001
4002 offer->io_task.run = offer_io_func;
4003 offer->fd = p[0];
4004 offer->func = func;
4005 offer->refcount++;
4006 offer->user_data = user_data;
4007
4008 display_watch_fd(offer->input->display,
4009 offer->fd, EPOLLIN, &offer->io_task);
4010}
4011
4012void
4013input_receive_drag_data(struct input *input, const char *mime_type,
4014 data_func_t func, void *data)
4015{
4016 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08004017 input->drag_offer->x = input->drag_x;
4018 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004019}
4020
4021int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004022input_receive_drag_data_to_fd(struct input *input,
4023 const char *mime_type, int fd)
4024{
4025 if (input->drag_offer)
4026 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4027
4028 return 0;
4029}
4030
4031int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004032input_receive_selection_data(struct input *input, const char *mime_type,
4033 data_func_t func, void *data)
4034{
4035 char **p;
4036
4037 if (input->selection_offer == NULL)
4038 return -1;
4039
4040 for (p = input->selection_offer->types.data; *p; p++)
4041 if (strcmp(mime_type, *p) == 0)
4042 break;
4043
4044 if (*p == NULL)
4045 return -1;
4046
4047 data_offer_receive_data(input->selection_offer,
4048 mime_type, func, data);
4049 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004050}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004051
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004052int
4053input_receive_selection_data_to_fd(struct input *input,
4054 const char *mime_type, int fd)
4055{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004056 if (input->selection_offer)
4057 wl_data_offer_receive(input->selection_offer->offer,
4058 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004059
4060 return 0;
4061}
4062
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004063void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004064window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004065{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004066 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004067 return;
4068
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004069 zxdg_toplevel_v6_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004070}
4071
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004072static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004073surface_set_synchronized(struct surface *surface)
4074{
4075 if (!surface->subsurface)
4076 return;
4077
4078 if (surface->synchronized)
4079 return;
4080
4081 wl_subsurface_set_sync(surface->subsurface);
4082 surface->synchronized = 1;
4083}
4084
4085static void
4086surface_set_synchronized_default(struct surface *surface)
4087{
4088 if (!surface->subsurface)
4089 return;
4090
4091 if (surface->synchronized == surface->synchronized_default)
4092 return;
4093
4094 if (surface->synchronized_default)
4095 wl_subsurface_set_sync(surface->subsurface);
4096 else
4097 wl_subsurface_set_desync(surface->subsurface);
4098
4099 surface->synchronized = surface->synchronized_default;
4100}
4101
4102static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004103surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004104{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004105 struct widget *widget = surface->widget;
4106 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004107
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004108 if (surface->input_region) {
4109 wl_region_destroy(surface->input_region);
4110 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004111 }
4112
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004113 if (surface->opaque_region)
4114 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004115
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004116 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004117
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004118 if (widget->resize_handler)
4119 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004120 widget->allocation.width,
4121 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004122 widget->user_data);
4123
Pekka Paalanen35e82632013-04-25 13:57:48 +03004124 if (surface->subsurface &&
4125 (surface->allocation.x != widget->allocation.x ||
4126 surface->allocation.y != widget->allocation.y)) {
4127 wl_subsurface_set_position(surface->subsurface,
4128 widget->allocation.x,
4129 widget->allocation.y);
4130 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004131 if (surface->allocation.width != widget->allocation.width ||
4132 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004133 window_schedule_redraw(widget->window);
4134 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004135 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004136
4137 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004138 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004139 widget->allocation.width,
4140 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004141}
4142
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004143static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004144window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004145{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004146 struct surface *surface;
4147
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004148 widget_set_allocation(window->main_surface->widget,
4149 window->pending_allocation.x,
4150 window->pending_allocation.y,
4151 window->pending_allocation.width,
4152 window->pending_allocation.height);
4153
4154 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004155
4156 /* The main surface is in the list, too. Main surface's
4157 * resize_handler is responsible for calling widget_set_allocation()
4158 * on all sub-surface root widgets, so they will be resized
4159 * properly.
4160 */
4161 wl_list_for_each(surface, &window->subsurface_list, link) {
4162 if (surface == window->main_surface)
4163 continue;
4164
4165 surface_set_synchronized(surface);
4166 surface_resize(surface);
4167 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004168
4169 if (!window->fullscreen && !window->maximized)
4170 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004171
4172 if (window->confined && window->confined_widget) {
4173 struct wl_compositor *compositor = window->display->compositor;
4174 struct wl_region *region;
4175 struct widget *widget = window->confined_widget;
4176
4177 region = wl_compositor_create_region(compositor);
4178 wl_region_add(region,
4179 widget->allocation.x,
4180 widget->allocation.y,
4181 widget->allocation.width,
4182 widget->allocation.height);
4183 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4184 region);
4185 wl_region_destroy(region);
4186 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004187}
4188
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004189static void
4190idle_resize(struct window *window)
4191{
4192 window->resize_needed = 0;
4193 window->redraw_needed = 1;
4194
4195 DBG("from %dx%d to %dx%d\n",
4196 window->main_surface->server_allocation.width,
4197 window->main_surface->server_allocation.height,
4198 window->pending_allocation.width,
4199 window->pending_allocation.height);
4200
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004201 window_do_resize(window);
4202}
4203
4204static void
4205undo_resize(struct window *window)
4206{
4207 window->pending_allocation.width =
4208 window->main_surface->server_allocation.width;
4209 window->pending_allocation.height =
4210 window->main_surface->server_allocation.height;
4211
4212 DBG("back to %dx%d\n",
4213 window->main_surface->server_allocation.width,
4214 window->main_surface->server_allocation.height);
4215
4216 window_do_resize(window);
4217
4218 if (window->pending_allocation.width == 0 &&
4219 window->pending_allocation.height == 0) {
4220 fprintf(stderr, "Error: Could not draw a surface, "
4221 "most likely due to insufficient disk space in "
4222 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4223 exit(EXIT_FAILURE);
4224 }
4225}
4226
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004227void
4228window_schedule_resize(struct window *window, int width, int height)
4229{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004230 /* We should probably get these numbers from the theme. */
4231 const int min_width = 200, min_height = 200;
4232
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004233 window->pending_allocation.x = 0;
4234 window->pending_allocation.y = 0;
4235 window->pending_allocation.width = width;
4236 window->pending_allocation.height = height;
4237
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004238 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004239 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004240 window->min_allocation.width = min_width;
4241 else
4242 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004243 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004244 window->min_allocation.height = min_height;
4245 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004246 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004247 }
4248
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004249 if (window->pending_allocation.width < window->min_allocation.width)
4250 window->pending_allocation.width = window->min_allocation.width;
4251 if (window->pending_allocation.height < window->min_allocation.height)
4252 window->pending_allocation.height = window->min_allocation.height;
4253
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004254 window->resize_needed = 1;
4255 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004256}
4257
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004258void
4259widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4260{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004261 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004262}
4263
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004264static int
4265window_get_shadow_margin(struct window *window)
4266{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004267 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004268 return frame_get_shadow_margin(window->frame->frame);
4269 else
4270 return 0;
4271}
4272
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004273static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004274window_inhibit_redraw(struct window *window)
4275{
4276 window->redraw_inhibited = 1;
4277 wl_list_remove(&window->redraw_task.link);
4278 wl_list_init(&window->redraw_task.link);
4279 window->redraw_task_scheduled = 0;
4280}
4281
4282static void
4283window_uninhibit_redraw(struct window *window)
4284{
4285 window->redraw_inhibited = 0;
4286 if (window->redraw_needed || window->resize_needed)
4287 window_schedule_redraw_task(window);
4288}
4289
4290static void
4291xdg_surface_handle_configure(void *data,
4292 struct zxdg_surface_v6 *zxdg_surface_v6,
4293 uint32_t serial)
4294{
4295 struct window *window = data;
4296
4297 zxdg_surface_v6_ack_configure(window->xdg_surface, serial);
4298
4299 if (window->state_changed_handler)
4300 window->state_changed_handler(window, window->user_data);
4301
4302 window_uninhibit_redraw(window);
4303}
4304
4305static const struct zxdg_surface_v6_listener xdg_surface_listener = {
4306 xdg_surface_handle_configure
4307};
4308
4309static void
4310xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
4311 int32_t width, int32_t height,
4312 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004313{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004314 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004315 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004316
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004317 window->maximized = 0;
4318 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004319 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004320 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004321
4322 wl_array_for_each(p, states) {
4323 uint32_t state = *p;
4324 switch (state) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004325 case ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004326 window->maximized = 1;
4327 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004328 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004329 window->fullscreen = 1;
4330 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004331 case ZXDG_TOPLEVEL_V6_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004332 window->resizing = 1;
4333 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004334 case ZXDG_TOPLEVEL_V6_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004335 window->focused = 1;
4336 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004337 default:
4338 /* Unknown state */
4339 break;
4340 }
4341 }
4342
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004343 if (window->frame) {
4344 if (window->maximized) {
4345 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4346 } else {
4347 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4348 }
4349
4350 if (window->focused) {
4351 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4352 } else {
4353 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4354 }
4355 }
4356
Jasper St. Pierref184c382014-05-06 08:33:27 -04004357 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004358 /* The width / height params are for window geometry,
4359 * but window_schedule_resize takes allocation. Add
4360 * on the shadow margin to get the difference. */
4361 int margin = window_get_shadow_margin(window);
4362
4363 window_schedule_resize(window,
4364 width + margin * 2,
4365 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004366 } else if (window->saved_allocation.width > 0 &&
4367 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004368 window_schedule_resize(window,
4369 window->saved_allocation.width,
4370 window->saved_allocation.height);
4371 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004372}
4373
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004374static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004375xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004376{
4377 struct window *window = data;
4378 window_close(window);
4379}
4380
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004381static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
4382 xdg_toplevel_handle_configure,
4383 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004384};
4385
4386static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004387window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004388{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004389 struct zxdg_toplevel_v6 *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004390
4391 if (!window->xdg_surface)
4392 return;
4393
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004394 if (window->parent == window->last_parent)
4395 return;
4396
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004397 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004398 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004399 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004400 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004401
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004402 zxdg_toplevel_v6_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004403 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004404}
4405
4406static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004407window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004408{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004409 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004410 frame_input_rect(window->frame->frame,
4411 &geometry->x,
4412 &geometry->y,
4413 &geometry->width,
4414 &geometry->height);
4415 else
4416 window_get_allocation(window, geometry);
4417}
4418
4419static void
4420window_sync_geometry(struct window *window)
4421{
4422 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004423
4424 if (!window->xdg_surface)
4425 return;
4426
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004427 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004428 if (geometry.x == window->last_geometry.x &&
4429 geometry.y == window->last_geometry.y &&
4430 geometry.width == window->last_geometry.width &&
4431 geometry.height == window->last_geometry.height)
4432 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004433
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004434 zxdg_surface_v6_set_window_geometry(window->xdg_surface,
4435 geometry.x,
4436 geometry.y,
4437 geometry.width,
4438 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004439 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004440}
4441
4442static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004443window_flush(struct window *window)
4444{
4445 struct surface *surface;
4446
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004447 assert(!window->redraw_inhibited);
4448
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004449 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004450 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004451 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004452 if (window->xdg_toplevel)
4453 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004454 }
4455
4456 wl_list_for_each(surface, &window->subsurface_list, link) {
4457 if (surface == window->main_surface)
4458 continue;
4459
4460 surface_flush(surface);
4461 }
4462
4463 surface_flush(window->main_surface);
4464}
4465
4466static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004467menu_destroy(struct menu *menu)
4468{
4469 widget_destroy(menu->widget);
4470 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004471 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004472 free(menu);
4473}
4474
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004475void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004476window_get_allocation(struct window *window,
4477 struct rectangle *allocation)
4478{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004479 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004480}
4481
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004482static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004483widget_redraw(struct widget *widget)
4484{
4485 struct widget *child;
4486
4487 if (widget->redraw_handler)
4488 widget->redraw_handler(widget, widget->user_data);
4489 wl_list_for_each(child, &widget->child_list, link)
4490 widget_redraw(child);
4491}
4492
4493static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004494frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4495{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004496 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004497
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004498 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004499 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004500 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004501 surface->frame_cb = NULL;
4502
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004503 surface->last_time = time;
4504
Pekka Paalanen71233882013-04-25 13:57:53 +03004505 if (surface->redraw_needed || surface->window->redraw_needed) {
4506 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004507 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004508 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004509}
4510
4511static const struct wl_callback_listener listener = {
4512 frame_callback
4513};
4514
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004515static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004516surface_redraw(struct surface *surface)
4517{
Pekka Paalanen71233882013-04-25 13:57:53 +03004518 DBG_OBJ(surface->surface, "begin\n");
4519
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004520 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004521 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004522
4523 /* Whole-window redraw forces a redraw even if the previous has
4524 * not yet hit the screen.
4525 */
4526 if (surface->frame_cb) {
4527 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004528 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004529
Pekka Paalanen71233882013-04-25 13:57:53 +03004530 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004531 wl_callback_destroy(surface->frame_cb);
4532 }
4533
Neil Roberts97b747c2013-12-19 16:17:12 +00004534 if (surface->widget->use_cairo &&
4535 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004536 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004537 return -1;
4538 }
4539
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004540 surface->frame_cb = wl_surface_frame(surface->surface);
4541 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004542 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004543
4544 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004545 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004546 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004547 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004548 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004549}
4550
4551static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004552idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004553{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004554 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004555 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004556 int failed = 0;
4557 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004558
Pekka Paalanen71233882013-04-25 13:57:53 +03004559 DBG(" --------- \n");
4560
Pekka Paalaneneebff542013-04-25 13:57:52 +03004561 wl_list_init(&window->redraw_task.link);
4562 window->redraw_task_scheduled = 0;
4563
4564 if (window->resize_needed) {
4565 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004566 if (window->main_surface->frame_cb) {
4567 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004568 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004569 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004570
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004571 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004572 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004573 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004574
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004575 if (surface_redraw(window->main_surface) < 0) {
4576 /*
4577 * Only main_surface failure will cause us to undo the resize.
4578 * If sub-surfaces fail, they will just be broken with old
4579 * content.
4580 */
4581 failed = 1;
4582 } else {
4583 wl_list_for_each(surface, &window->subsurface_list, link) {
4584 if (surface == window->main_surface)
4585 continue;
4586
4587 surface_redraw(surface);
4588 }
4589 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004590
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004591 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004592 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004593
4594 wl_list_for_each(surface, &window->subsurface_list, link)
4595 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004596
4597 if (resized && failed) {
4598 /* Restore widget tree to correspond to what is on screen. */
4599 undo_resize(window);
4600 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004601}
4602
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004603static void
4604window_schedule_redraw_task(struct window *window)
4605{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004606 if (window->redraw_inhibited)
4607 return;
4608
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004609 if (!window->redraw_task_scheduled) {
4610 window->redraw_task.run = idle_redraw;
4611 display_defer(window->display, &window->redraw_task);
4612 window->redraw_task_scheduled = 1;
4613 }
4614}
4615
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004616void
4617window_schedule_redraw(struct window *window)
4618{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004619 struct surface *surface;
4620
Pekka Paalanen71233882013-04-25 13:57:53 +03004621 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4622
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004623 wl_list_for_each(surface, &window->subsurface_list, link)
4624 surface->redraw_needed = 1;
4625
4626 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004627}
4628
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004629int
4630window_is_fullscreen(struct window *window)
4631{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004632 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004633}
4634
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004635void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004636window_set_fullscreen(struct window *window, int fullscreen)
4637{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004638 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004639 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004640
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004641 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004642 return;
4643
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004644 if (fullscreen)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004645 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004646 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004647 zxdg_toplevel_v6_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004648}
4649
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004650int
4651window_is_maximized(struct window *window)
4652{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004653 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004654}
4655
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004656void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004657window_set_maximized(struct window *window, int maximized)
4658{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004659 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004660 return;
4661
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004662 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004663 return;
4664
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004665 if (maximized)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004666 zxdg_toplevel_v6_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004667 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004668 zxdg_toplevel_v6_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004669}
4670
Jasper St. Pierrede680992014-04-10 17:23:49 -07004671int
4672window_is_resizing(struct window *window)
4673{
4674 return window->resizing;
4675}
4676
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004677void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004678window_set_minimized(struct window *window)
4679{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004680 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004681 return;
4682
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004683 zxdg_toplevel_v6_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004684}
4685
4686void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004687window_set_user_data(struct window *window, void *data)
4688{
4689 window->user_data = data;
4690}
4691
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004692void *
4693window_get_user_data(struct window *window)
4694{
4695 return window->user_data;
4696}
4697
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004698void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004699window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004700 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004701{
4702 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004703}
4704
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004705void
4706window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004707 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004708{
4709 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004710}
4711
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004712void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004713window_set_data_handler(struct window *window, window_data_handler_t handler)
4714{
4715 window->data_handler = handler;
4716}
4717
4718void
4719window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4720{
4721 window->drop_handler = handler;
4722}
4723
4724void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004725window_set_close_handler(struct window *window,
4726 window_close_handler_t handler)
4727{
4728 window->close_handler = handler;
4729}
4730
4731void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004732window_set_fullscreen_handler(struct window *window,
4733 window_fullscreen_handler_t handler)
4734{
4735 window->fullscreen_handler = handler;
4736}
4737
4738void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004739window_set_output_handler(struct window *window,
4740 window_output_handler_t handler)
4741{
4742 window->output_handler = handler;
4743}
4744
4745void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004746window_set_state_changed_handler(struct window *window,
4747 window_state_changed_handler_t handler)
4748{
4749 window->state_changed_handler = handler;
4750}
4751
4752void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004753window_set_pointer_locked_handler(struct window *window,
4754 locked_pointer_locked_handler_t locked,
4755 locked_pointer_unlocked_handler_t unlocked)
4756{
4757 window->pointer_unlocked_handler = unlocked;
4758 window->pointer_locked_handler = locked;
4759}
4760
4761void
4762window_set_pointer_confined_handler(struct window *window,
4763 confined_pointer_confined_handler_t confined,
4764 confined_pointer_unconfined_handler_t unconfined)
4765{
4766 window->pointer_confined_handler = confined;
4767 window->pointer_unconfined_handler = unconfined;
4768}
4769
4770void
4771window_set_locked_pointer_motion_handler(struct window *window,
4772 window_locked_pointer_motion_handler_t handler)
4773{
4774 window->locked_pointer_motion_handler = handler;
4775}
4776
4777void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004778window_set_title(struct window *window, const char *title)
4779{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004780 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004781 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004782 if (window->frame) {
4783 frame_set_title(window->frame->frame, title);
4784 widget_schedule_redraw(window->frame->widget);
4785 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004786 if (window->xdg_toplevel)
4787 zxdg_toplevel_v6_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004788}
4789
4790const char *
4791window_get_title(struct window *window)
4792{
4793 return window->title;
4794}
4795
4796void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004797window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4798{
4799 struct text_cursor_position *text_cursor_position =
4800 window->display->text_cursor_position;
4801
Scott Moreau9295ce02012-06-01 12:46:10 -06004802 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004803 return;
4804
4805 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004806 window->main_surface->surface,
4807 wl_fixed_from_int(x),
4808 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004809}
4810
Casey Dahlin9074db52012-04-19 22:50:09 -04004811static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004812relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4813 uint32_t utime_hi,
4814 uint32_t utime_lo,
4815 wl_fixed_t dx,
4816 wl_fixed_t dy,
4817 wl_fixed_t dx_unaccel,
4818 wl_fixed_t dy_unaccel)
4819{
4820 struct input *input = data;
4821 struct window *window = input->pointer_focus;
4822 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4823
4824 if (window->locked_pointer_motion_handler &&
4825 window->pointer_locked) {
4826 window->locked_pointer_motion_handler(
4827 window, input, ms,
4828 wl_fixed_to_double(dx),
4829 wl_fixed_to_double(dy),
4830 window->user_data);
4831 }
4832}
4833
4834static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4835 relative_pointer_handle_motion,
4836};
4837
4838static void
4839locked_pointer_locked(void *data,
4840 struct zwp_locked_pointer_v1 *locked_pointer)
4841{
4842 struct input *input = data;
4843 struct window *window = input->pointer_focus;
4844
4845 window->pointer_locked = true;
4846
4847 if (window->pointer_locked_handler) {
4848 window->pointer_locked_handler(window,
4849 input,
4850 window->user_data);
4851 }
4852}
4853
4854static void
4855locked_pointer_unlocked(void *data,
4856 struct zwp_locked_pointer_v1 *locked_pointer)
4857{
4858 struct input *input = data;
4859 struct window *window = input->pointer_focus;
4860
4861 window_unlock_pointer(window);
4862
4863 if (window->pointer_unlocked_handler) {
4864 window->pointer_unlocked_handler(window,
4865 input,
4866 window->user_data);
4867 }
4868}
4869
4870static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4871 locked_pointer_locked,
4872 locked_pointer_unlocked,
4873};
4874
4875int
4876window_lock_pointer(struct window *window, struct input *input)
4877{
4878 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4879 window->display->relative_pointer_manager;
4880 struct zwp_pointer_constraints_v1 *pointer_constraints =
4881 window->display->pointer_constraints;
4882 struct zwp_relative_pointer_v1 *relative_pointer;
4883 struct zwp_locked_pointer_v1 *locked_pointer;
4884
4885 if (!window->display->relative_pointer_manager)
4886 return -1;
4887
4888 if (!window->display->pointer_constraints)
4889 return -1;
4890
4891 if (window->locked_pointer)
4892 return -1;
4893
4894 if (window->confined_pointer)
4895 return -1;
4896
4897 if (!input->pointer)
4898 return -1;
4899
4900 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4901 relative_pointer_manager, input->pointer);
4902 zwp_relative_pointer_v1_add_listener(relative_pointer,
4903 &relative_pointer_listener,
4904 input);
4905
4906 locked_pointer =
4907 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4908 window->main_surface->surface,
4909 input->pointer,
4910 NULL,
4911 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4912 zwp_locked_pointer_v1_add_listener(locked_pointer,
4913 &locked_pointer_listener,
4914 input);
4915
4916 window->locked_input = input;
4917 window->locked_pointer = locked_pointer;
4918 window->relative_pointer = relative_pointer;
4919
4920 return 0;
4921}
4922
4923void
4924window_unlock_pointer(struct window *window)
4925{
4926 if (!window->locked_pointer)
4927 return;
4928
4929 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4930 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4931 window->locked_pointer = NULL;
4932 window->relative_pointer = NULL;
4933 window->pointer_locked = false;
4934 window->locked_input = NULL;
4935}
4936
4937void
4938widget_set_locked_pointer_cursor_hint(struct widget *widget,
4939 float x, float y)
4940{
4941 struct window *window = widget->window;
4942
4943 if (!window->locked_pointer)
4944 return;
4945
4946 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4947 wl_fixed_from_double(x),
4948 wl_fixed_from_double(y));
4949 wl_surface_commit(window->main_surface->surface);
4950}
4951
4952static void
4953confined_pointer_confined(void *data,
4954 struct zwp_confined_pointer_v1 *confined_pointer)
4955{
4956 struct input *input = data;
4957 struct window *window = input->pointer_focus;
4958
4959 window->confined = true;
4960
4961 if (window->pointer_confined_handler) {
4962 window->pointer_confined_handler(window,
4963 input,
4964 window->user_data);
4965 }
4966}
4967
4968static void
4969confined_pointer_unconfined(void *data,
4970 struct zwp_confined_pointer_v1 *confined_pointer)
4971{
4972 struct input *input = data;
4973 struct window *window = input->pointer_focus;
4974
4975 window_unconfine_pointer(window);
4976
4977 window->confined = false;
4978
4979 if (window->pointer_unconfined_handler) {
4980 window->pointer_unconfined_handler(window,
4981 input,
4982 window->user_data);
4983 }
4984}
4985
4986static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4987 confined_pointer_confined,
4988 confined_pointer_unconfined,
4989};
4990
4991int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004992window_confine_pointer_to_rectangles(struct window *window,
4993 struct input *input,
4994 struct rectangle *rectangles,
4995 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004996{
4997 struct zwp_pointer_constraints_v1 *pointer_constraints =
4998 window->display->pointer_constraints;
4999 struct zwp_confined_pointer_v1 *confined_pointer;
5000 struct wl_compositor *compositor = window->display->compositor;
5001 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005002 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005003
5004 if (!window->display->pointer_constraints)
5005 return -1;
5006
5007 if (window->locked_pointer)
5008 return -1;
5009
5010 if (window->confined_pointer)
5011 return -1;
5012
5013 if (!input->pointer)
5014 return -1;
5015
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005016 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005017 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005018 for (i = 0; i < num_rectangles; i++) {
5019 wl_region_add(region,
5020 rectangles[i].x,
5021 rectangles[i].y,
5022 rectangles[i].width,
5023 rectangles[i].height);
5024 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005025 }
5026
5027 confined_pointer =
5028 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5029 window->main_surface->surface,
5030 input->pointer,
5031 region,
5032 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5033 if (region)
5034 wl_region_destroy(region);
5035
5036 zwp_confined_pointer_v1_add_listener(confined_pointer,
5037 &confined_pointer_listener,
5038 input);
5039
5040 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005041 window->confined_widget = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005042
5043 return 0;
5044}
5045
5046void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005047window_update_confine_rectangles(struct window *window,
5048 struct rectangle *rectangles,
5049 int num_rectangles)
5050{
5051 struct wl_compositor *compositor = window->display->compositor;
5052 struct wl_region *region;
5053 int i;
5054
5055 region = wl_compositor_create_region(compositor);
5056 for (i = 0; i < num_rectangles; i++) {
5057 wl_region_add(region,
5058 rectangles[i].x,
5059 rectangles[i].y,
5060 rectangles[i].width,
5061 rectangles[i].height);
5062 }
5063
5064 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5065
5066 wl_region_destroy(region);
5067}
5068
5069int
5070window_confine_pointer_to_widget(struct window *window,
5071 struct widget *widget,
5072 struct input *input)
5073{
5074 int ret;
5075
5076 if (widget) {
5077 ret = window_confine_pointer_to_rectangles(window,
5078 input,
5079 &widget->allocation,
5080 1);
5081 window->confined_widget = widget;
5082 return ret;
5083 } else {
5084 return window_confine_pointer_to_rectangles(window,
5085 input,
5086 NULL,
5087 0);
5088 }
5089}
5090
5091void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005092window_unconfine_pointer(struct window *window)
5093{
5094 if (!window->confined_pointer)
5095 return;
5096
5097 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5098 window->confined_pointer = NULL;
5099 window->confined = false;
5100}
5101
5102static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005103surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005104 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005105{
Rob Bradford7507b572012-05-15 17:55:34 +01005106 struct window *window = data;
5107 struct output *output;
5108 struct output *output_found = NULL;
5109 struct window_output *window_output;
5110
5111 wl_list_for_each(output, &window->display->output_list, link) {
5112 if (output->output == wl_output) {
5113 output_found = output;
5114 break;
5115 }
5116 }
5117
5118 if (!output_found)
5119 return;
5120
Brian Lovinbc919262013-08-07 15:34:59 -07005121 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005122 window_output->output = output_found;
5123
5124 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005125
5126 if (window->output_handler)
5127 window->output_handler(window, output_found, 1,
5128 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005129}
5130
5131static void
5132surface_leave(void *data,
5133 struct wl_surface *wl_surface, struct wl_output *output)
5134{
Rob Bradford7507b572012-05-15 17:55:34 +01005135 struct window *window = data;
5136 struct window_output *window_output;
5137 struct window_output *window_output_found = NULL;
5138
5139 wl_list_for_each(window_output, &window->window_output_list, link) {
5140 if (window_output->output->output == output) {
5141 window_output_found = window_output;
5142 break;
5143 }
5144 }
5145
5146 if (window_output_found) {
5147 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005148
5149 if (window->output_handler)
5150 window->output_handler(window, window_output->output,
5151 0, window->user_data);
5152
Rob Bradford7507b572012-05-15 17:55:34 +01005153 free(window_output_found);
5154 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005155}
5156
5157static const struct wl_surface_listener surface_listener = {
5158 surface_enter,
5159 surface_leave
5160};
5161
Pekka Paalanen4e373742013-02-13 16:17:13 +02005162static struct surface *
5163surface_create(struct window *window)
5164{
5165 struct display *display = window->display;
5166 struct surface *surface;
5167
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005168 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005169 surface->window = window;
5170 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005171 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005172 wl_surface_add_listener(surface->surface, &surface_listener, window);
5173
Pekka Paalanen35e82632013-04-25 13:57:48 +03005174 wl_list_insert(&window->subsurface_list, &surface->link);
5175
Pekka Paalanen4e373742013-02-13 16:17:13 +02005176 return surface;
5177}
5178
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005179static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005180get_preferred_buffer_type(struct display *display)
5181{
5182#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005183 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005184 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5185#endif
5186
5187 return WINDOW_BUFFER_TYPE_SHM;
5188}
5189
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005190static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005191window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005192{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005193 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005194 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005195
Peter Huttererf3d62272013-08-08 11:57:05 +10005196 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005197 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005198 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005199
5200 surface = surface_create(window);
5201 window->main_surface = surface;
5202
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005203 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005204
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005205 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005206 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005207
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005208 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005209
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005210 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005211 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005212 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005213
Rob Bradford7507b572012-05-15 17:55:34 +01005214 wl_list_init (&window->window_output_list);
5215
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005216 return window;
5217}
5218
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005219struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005220window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005221{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005222 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005223 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005224
5225 window = window_create_internal(display, 0);
5226
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005227 if (window->display->xdg_shell) {
5228 window->xdg_surface =
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005229 zxdg_shell_v6_get_xdg_surface(window->display->xdg_shell,
5230 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005231 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005232
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005233 zxdg_surface_v6_add_listener(window->xdg_surface,
5234 &xdg_surface_listener, window);
5235
5236 window->xdg_toplevel =
5237 zxdg_surface_v6_get_toplevel(window->xdg_surface);
5238 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5239
5240 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
5241 &xdg_toplevel_listener, window);
5242
5243 window_inhibit_redraw(window);
5244
5245 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005246 } else if (display->ivi_application) {
5247 /* auto generation of ivi_id based on process id + basement of id */
5248 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5249 window->ivi_surface =
5250 ivi_application_surface_create(display->ivi_application,
5251 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005252 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005253
5254 ivi_surface_add_listener(window->ivi_surface,
5255 &ivi_surface_listener, window);
5256 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005257
5258 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005259}
5260
5261struct window *
5262window_create_custom(struct display *display)
5263{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005264 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005265}
5266
Jasper St. Pierre53686042013-12-09 15:26:25 -05005267void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005268window_set_parent(struct window *window,
5269 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005270{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005271 window->parent = parent_window;
5272 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005273}
5274
5275struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005276window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005277{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005278 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005279}
5280
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005281static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005282menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005283{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005284 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005285 int next;
5286
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005287 frame_interior(menu->frame, &x, &y, &width, &height);
5288 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005289 if (menu->current != next) {
5290 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005291 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005292 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005293}
5294
5295static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005296menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005297 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005298 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005299{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005300 struct menu *menu = data;
5301
5302 if (widget == menu->widget)
5303 menu_set_item(data, y);
5304
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005305 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005306}
5307
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005308static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005309menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005310 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005311{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005312 struct menu *menu = data;
5313
5314 if (widget == menu->widget)
5315 menu_set_item(data, y);
5316
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005317 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005318}
5319
5320static void
5321menu_leave_handler(struct widget *widget, struct input *input, void *data)
5322{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005323 struct menu *menu = data;
5324
5325 if (widget == menu->widget)
5326 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005327}
5328
5329static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005330menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005331 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005332 uint32_t button, enum wl_pointer_button_state state,
5333 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005334
5335{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005336 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005337
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005338 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5339 (menu->release_count > 0 || time - menu->time > 500)) {
Abdur Rehman6c1c0dd2017-01-01 19:46:31 +05005340 /* Either release after press-drag-release or
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005341 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005342 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005343 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005344 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005345 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005346 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005347 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005348}
5349
5350static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005351menu_touch_up_handler(struct widget *widget,
5352 struct input *input,
5353 uint32_t serial,
5354 uint32_t time,
5355 int32_t id,
5356 void *data)
5357{
5358 struct menu *menu = data;
5359
5360 input_ungrab(input);
5361 menu_destroy(menu);
5362}
5363
5364static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005365menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005366{
5367 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005368 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005369 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005370
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005371 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005372
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005373 frame_repaint(menu->frame, cr);
5374 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005375
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005376 theme_set_background_source(menu->window->display->theme,
5377 cr, THEME_FRAME_ACTIVE);
5378 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005379 cairo_fill(cr);
5380
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005381 cairo_select_font_face(cr, "sans",
5382 CAIRO_FONT_SLANT_NORMAL,
5383 CAIRO_FONT_WEIGHT_NORMAL);
5384 cairo_set_font_size(cr, 12);
5385
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005386 for (i = 0; i < menu->count; i++) {
5387 if (i == menu->current) {
5388 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005389 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005390 cairo_fill(cr);
5391 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005392 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005393 cairo_show_text(cr, menu->entries[i]);
5394 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005395 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5396 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005397 cairo_show_text(cr, menu->entries[i]);
5398 }
5399 }
5400
5401 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005402}
5403
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005404static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005405xdg_popup_handle_configure(void *data,
5406 struct zxdg_popup_v6 *zxdg_popup_v6,
5407 int32_t x,
5408 int32_t y,
5409 int32_t width,
5410 int32_t height)
5411{
5412}
5413
5414static void
5415xdg_popup_handle_popup_done(void *data, struct zxdg_popup_v6 *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005416{
5417 struct window *window = data;
5418 struct menu *menu = window->main_surface->widget->user_data;
5419
5420 input_ungrab(menu->input);
5421 menu_destroy(menu);
5422}
5423
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005424static const struct zxdg_popup_v6_listener xdg_popup_listener = {
5425 xdg_popup_handle_configure,
5426 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005427};
5428
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005429static struct menu *
5430create_menu(struct display *display,
5431 struct input *input, uint32_t time,
5432 menu_func_t func, const char **entries, int count,
5433 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005434{
5435 struct window *window;
5436 struct menu *menu;
5437
5438 menu = malloc(sizeof *menu);
5439 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005440 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005441
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005442 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005443 if (!window) {
5444 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005445 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005446 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005447
5448 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005449 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005450 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005451 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07005452 FRAME_BUTTON_NONE, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005453 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005454 menu->entries = entries;
5455 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005456 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005457 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005458 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005459 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005460 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005461
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005462 input_ungrab(input);
5463
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005464 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005465 widget_set_enter_handler(menu->widget, menu_enter_handler);
5466 widget_set_leave_handler(menu->widget, menu_leave_handler);
5467 widget_set_motion_handler(menu->widget, menu_motion_handler);
5468 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005469 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005470
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005471 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005472 frame_resize_inside(menu->frame, 200, count * 20);
5473 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5474 window_schedule_resize(window, frame_width(menu->frame),
5475 frame_height(menu->frame));
5476
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005477 return menu;
5478}
5479
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005480static struct zxdg_positioner_v6 *
5481create_simple_positioner(struct display *display,
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005482 int x, int y, int w, int h)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005483{
5484 struct zxdg_positioner_v6 *positioner;
5485
5486 positioner = zxdg_shell_v6_create_positioner(display->xdg_shell);
5487 fail_on_null(positioner, 0, __FILE__, __LINE__);
5488 zxdg_positioner_v6_set_anchor_rect(positioner, x, y, 1, 1);
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005489 zxdg_positioner_v6_set_size(positioner, w, h);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005490 zxdg_positioner_v6_set_anchor(positioner,
5491 ZXDG_POSITIONER_V6_ANCHOR_TOP |
5492 ZXDG_POSITIONER_V6_ANCHOR_LEFT);
5493 zxdg_positioner_v6_set_gravity(positioner,
5494 ZXDG_POSITIONER_V6_ANCHOR_BOTTOM |
5495 ZXDG_POSITIONER_V6_ANCHOR_RIGHT);
5496
5497 return positioner;
5498}
5499
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005500void
5501window_show_menu(struct display *display,
5502 struct input *input, uint32_t time, struct window *parent,
5503 int32_t x, int32_t y,
5504 menu_func_t func, const char **entries, int count)
5505{
5506 struct menu *menu;
5507 struct window *window;
5508 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005509 struct rectangle parent_geometry;
5510 struct zxdg_positioner_v6 *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005511
5512 menu = create_menu(display, input, time, func, entries, count, parent);
5513
5514 if (menu == NULL)
5515 return;
5516
5517 window = menu->window;
5518
5519 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5520 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5521
5522 window->x = x;
5523 window->y = y;
5524
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005525 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005526 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005527
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005528 if (!display->xdg_shell)
5529 return;
5530
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005531 window->xdg_surface =
5532 zxdg_shell_v6_get_xdg_surface(display->xdg_shell,
5533 window->main_surface->surface);
5534 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005535
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005536 zxdg_surface_v6_add_listener(window->xdg_surface,
5537 &xdg_surface_listener, window);
5538
5539 positioner = create_simple_positioner(display,
5540 window->x - (ix + parent_geometry.x),
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02005541 window->y - (iy + parent_geometry.y),
5542 frame_width(menu->frame),
5543 frame_height(menu->frame));
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005544 window->xdg_popup =
5545 zxdg_surface_v6_get_popup(window->xdg_surface,
5546 parent->xdg_surface,
5547 positioner);
5548 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
5549 zxdg_positioner_v6_destroy(positioner);
5550 zxdg_popup_v6_grab(window->xdg_popup,
5551 input->seat,
5552 display_get_serial(window->display));
5553 zxdg_popup_v6_add_listener(window->xdg_popup,
5554 &xdg_popup_listener, window);
5555
5556 window_inhibit_redraw(window);
5557
5558 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005559}
5560
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005561void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005562window_set_buffer_type(struct window *window, enum window_buffer_type type)
5563{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005564 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005565}
5566
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005567enum window_buffer_type
5568window_get_buffer_type(struct window *window)
5569{
5570 return window->main_surface->buffer_type;
5571}
5572
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005573void
5574window_set_preferred_format(struct window *window,
5575 enum preferred_format format)
5576{
5577 window->preferred_format = format;
5578}
5579
Pekka Paalanen35e82632013-04-25 13:57:48 +03005580struct widget *
5581window_add_subsurface(struct window *window, void *data,
5582 enum subsurface_mode default_mode)
5583{
5584 struct widget *widget;
5585 struct surface *surface;
5586 struct wl_surface *parent;
5587 struct wl_subcompositor *subcompo = window->display->subcompositor;
5588
Pekka Paalanen35e82632013-04-25 13:57:48 +03005589 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005590 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005591 widget = widget_create(window, surface, data);
5592 wl_list_init(&widget->link);
5593 surface->widget = widget;
5594
5595 parent = window->main_surface->surface;
5596 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5597 surface->surface,
5598 parent);
5599 surface->synchronized = 1;
5600
5601 switch (default_mode) {
5602 case SUBSURFACE_SYNCHRONIZED:
5603 surface->synchronized_default = 1;
5604 break;
5605 case SUBSURFACE_DESYNCHRONIZED:
5606 surface->synchronized_default = 0;
5607 break;
5608 default:
5609 assert(!"bad enum subsurface_mode");
5610 }
5611
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005612 window->resize_needed = 1;
5613 window_schedule_redraw(window);
5614
Pekka Paalanen35e82632013-04-25 13:57:48 +03005615 return widget;
5616}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005617
5618static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005619display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005620 struct wl_output *wl_output,
5621 int x, int y,
5622 int physical_width,
5623 int physical_height,
5624 int subpixel,
5625 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005626 const char *model,
5627 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005628{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005629 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005630
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005631 output->allocation.x = x;
5632 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005633 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005634
5635 if (output->make)
5636 free(output->make);
5637 output->make = strdup(make);
5638
5639 if (output->model)
5640 free(output->model);
5641 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005642}
5643
5644static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005645display_handle_done(void *data,
5646 struct wl_output *wl_output)
5647{
5648}
5649
5650static void
5651display_handle_scale(void *data,
5652 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005653 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005654{
5655 struct output *output = data;
5656
5657 output->scale = scale;
5658}
5659
5660static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005661display_handle_mode(void *data,
5662 struct wl_output *wl_output,
5663 uint32_t flags,
5664 int width,
5665 int height,
5666 int refresh)
5667{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005668 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005669 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005670
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005671 if (flags & WL_OUTPUT_MODE_CURRENT) {
5672 output->allocation.width = width;
5673 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005674 if (display->output_configure_handler)
5675 (*display->output_configure_handler)(
5676 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005677 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005678}
5679
5680static const struct wl_output_listener output_listener = {
5681 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005682 display_handle_mode,
5683 display_handle_done,
5684 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005685};
5686
5687static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005688display_add_output(struct display *d, uint32_t id)
5689{
5690 struct output *output;
5691
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005692 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005693 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005694 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005695 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005696 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005697 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005698 wl_list_insert(d->output_list.prev, &output->link);
5699
5700 wl_output_add_listener(output->output, &output_listener, output);
5701}
5702
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005703static void
5704output_destroy(struct output *output)
5705{
5706 if (output->destroy_handler)
5707 (*output->destroy_handler)(output, output->user_data);
5708
5709 wl_output_destroy(output->output);
5710 wl_list_remove(&output->link);
5711 free(output);
5712}
5713
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005714static void
5715display_destroy_output(struct display *d, uint32_t id)
5716{
5717 struct output *output;
5718
5719 wl_list_for_each(output, &d->output_list, link) {
5720 if (output->server_output_id == id) {
5721 output_destroy(output);
5722 break;
5723 }
5724 }
5725}
5726
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005727void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005728display_set_global_handler(struct display *display,
5729 display_global_handler_t handler)
5730{
5731 struct global *global;
5732
5733 display->global_handler = handler;
5734 if (!handler)
5735 return;
5736
5737 wl_list_for_each(global, &display->global_list, link)
5738 display->global_handler(display,
5739 global->name, global->interface,
5740 global->version, display->user_data);
5741}
5742
5743void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005744display_set_global_handler_remove(struct display *display,
5745 display_global_handler_t remove_handler)
5746{
5747 display->global_handler_remove = remove_handler;
5748 if (!remove_handler)
5749 return;
5750}
5751
5752void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005753display_set_output_configure_handler(struct display *display,
5754 display_output_handler_t handler)
5755{
5756 struct output *output;
5757
5758 display->output_configure_handler = handler;
5759 if (!handler)
5760 return;
5761
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005762 wl_list_for_each(output, &display->output_list, link) {
5763 if (output->allocation.width == 0 &&
5764 output->allocation.height == 0)
5765 continue;
5766
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005767 (*display->output_configure_handler)(output,
5768 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005769 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005770}
5771
5772void
5773output_set_user_data(struct output *output, void *data)
5774{
5775 output->user_data = data;
5776}
5777
5778void *
5779output_get_user_data(struct output *output)
5780{
5781 return output->user_data;
5782}
5783
5784void
5785output_set_destroy_handler(struct output *output,
5786 display_output_handler_t handler)
5787{
5788 output->destroy_handler = handler;
5789 /* FIXME: implement this, once we have way to remove outputs */
5790}
5791
5792void
Scott Moreau4e072362012-09-29 02:03:11 -06005793output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005794{
Scott Moreau4e072362012-09-29 02:03:11 -06005795 struct rectangle allocation = output->allocation;
5796
5797 switch (output->transform) {
5798 case WL_OUTPUT_TRANSFORM_90:
5799 case WL_OUTPUT_TRANSFORM_270:
5800 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5801 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5802 /* Swap width and height */
5803 allocation.width = output->allocation.height;
5804 allocation.height = output->allocation.width;
5805 break;
5806 }
5807
5808 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005809}
5810
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005811struct wl_output *
5812output_get_wl_output(struct output *output)
5813{
5814 return output->output;
5815}
5816
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005817enum wl_output_transform
5818output_get_transform(struct output *output)
5819{
5820 return output->transform;
5821}
5822
Alexander Larssonafd319a2013-05-22 14:41:27 +02005823uint32_t
5824output_get_scale(struct output *output)
5825{
5826 return output->scale;
5827}
5828
Jason Ekstrand738715d2014-04-02 19:53:50 -05005829const char *
5830output_get_make(struct output *output)
5831{
5832 return output->make;
5833}
5834
5835const char *
5836output_get_model(struct output *output)
5837{
5838 return output->model;
5839}
5840
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005841static void
Daniel Stone97f68542012-05-30 16:32:01 +01005842fini_xkb(struct input *input)
5843{
5844 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005845 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005846}
5847
5848static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005849display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005850{
5851 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005852 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005853
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005854 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005855 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005856 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005857 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005858 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005859 input->pointer_focus = NULL;
5860 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005861 input->seat_version = seat_version;
5862
Rusty Lynch041815a2013-08-08 21:20:38 -07005863 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005864 wl_list_insert(d->input_list.prev, &input->link);
5865
Daniel Stone37816df2012-05-16 18:45:18 +01005866 wl_seat_add_listener(input->seat, &seat_listener, input);
5867 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005868
Jason Ekstranda669bd52014-04-02 19:53:51 -05005869 if (d->data_device_manager) {
5870 input->data_device =
5871 wl_data_device_manager_get_data_device(d->data_device_manager,
5872 input->seat);
5873 wl_data_device_add_listener(input->data_device,
5874 &data_device_listener,
5875 input);
5876 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005877
5878 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005879 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005880
Derek Foreman118a4292015-04-22 17:23:35 -05005881 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5882 TFD_CLOEXEC | TFD_NONBLOCK);
5883 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5884 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005885 set_repeat_info(input, 40, 400);
5886
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005887 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5888 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005889 input->repeat_task.run = keyboard_repeat_func;
5890 display_watch_fd(d, input->repeat_timer_fd,
5891 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005892}
5893
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005894static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005895input_destroy(struct input *input)
5896{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005897 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005898 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005899
5900 if (input->drag_offer)
5901 data_offer_destroy(input->drag_offer);
5902
5903 if (input->selection_offer)
5904 data_offer_destroy(input->selection_offer);
5905
kabeer khan6ce67ec2014-10-20 11:55:29 +05305906 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005907 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305908 wl_data_device_release(input->data_device);
5909 else
5910 wl_data_device_destroy(input->data_device);
5911 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005912 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005913 if (input->touch)
5914 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005915 if (input->pointer)
5916 wl_pointer_release(input->pointer);
5917 if (input->keyboard)
5918 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005919 } else {
5920 if (input->touch)
5921 wl_touch_destroy(input->touch);
5922 if (input->pointer)
5923 wl_pointer_destroy(input->pointer);
5924 if (input->keyboard)
5925 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005926 }
5927
Daniel Stone97f68542012-05-30 16:32:01 +01005928 fini_xkb(input);
5929
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005930 wl_surface_destroy(input->pointer_surface);
5931
Pekka Paalanene1207c72011-12-16 12:02:09 +02005932 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005933 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005934 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005935 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005936 free(input);
5937}
5938
5939static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005940shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5941{
5942 struct display *d = data;
5943
5944 if (format == WL_SHM_FORMAT_RGB565)
5945 d->has_rgb565 = 1;
5946}
5947
5948struct wl_shm_listener shm_listener = {
5949 shm_format
5950};
5951
5952static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005953xdg_shell_handle_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005954{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005955 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005956}
5957
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005958static const struct zxdg_shell_v6_listener xdg_shell_listener = {
5959 xdg_shell_handle_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005960};
5961
5962static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005963registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5964 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005965{
5966 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005967 struct global *global;
5968
Brian Lovinbc919262013-08-07 15:34:59 -07005969 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005970 global->name = id;
5971 global->interface = strdup(interface);
5972 global->version = version;
5973 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005974
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005975 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005976 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005977 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005978 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005979 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005980 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005981 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005982 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5983 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5984 d->relative_pointer_manager =
5985 wl_registry_bind(registry, id,
5986 &zwp_relative_pointer_manager_v1_interface,
5987 1);
5988 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5989 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5990 d->pointer_constraints =
5991 wl_registry_bind(registry, id,
5992 &zwp_pointer_constraints_v1_interface,
5993 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005994 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005995 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005996 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005997 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005998 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005999 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006000 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05306001 &wl_data_device_manager_interface,
6002 d->data_device_manager_version);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006003 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006004 d->xdg_shell = wl_registry_bind(registry, id,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006005 &zxdg_shell_v6_interface, 1);
6006 zxdg_shell_v6_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06006007 } else if (strcmp(interface, "text_cursor_position") == 0) {
6008 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006009 wl_registry_bind(registry, id,
6010 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006011 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6012 d->subcompositor =
6013 wl_registry_bind(registry, id,
6014 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006015 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006016 else if (strcmp(interface, "ivi_application") == 0) {
6017 d->ivi_application =
6018 wl_registry_bind(registry, id,
6019 &ivi_application_interface, 1);
6020 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006021
6022 if (d->global_handler)
6023 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006024}
6025
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006026static void
6027registry_handle_global_remove(void *data, struct wl_registry *registry,
6028 uint32_t name)
6029{
6030 struct display *d = data;
6031 struct global *global;
6032 struct global *tmp;
6033
6034 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6035 if (global->name != name)
6036 continue;
6037
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006038 if (strcmp(global->interface, "wl_output") == 0)
6039 display_destroy_output(d, name);
6040
6041 /* XXX: Should destroy remaining bound globals */
6042
6043 if (d->global_handler_remove)
6044 d->global_handler_remove(d, name, global->interface,
6045 global->version, d->user_data);
6046
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006047 wl_list_remove(&global->link);
6048 free(global->interface);
6049 free(global);
6050 }
6051}
6052
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006053void *
6054display_bind(struct display *display, uint32_t name,
6055 const struct wl_interface *interface, uint32_t version)
6056{
6057 return wl_registry_bind(display->registry, name, interface, version);
6058}
6059
6060static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006061 registry_handle_global,
6062 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006063};
6064
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006065#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006066static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006067init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006068{
6069 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006070 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006071
Rob Clark6396ed32012-03-11 19:48:41 -05006072#ifdef USE_CAIRO_GLESV2
6073# define GL_BIT EGL_OPENGL_ES2_BIT
6074#else
6075# define GL_BIT EGL_OPENGL_BIT
6076#endif
6077
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006078 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006079 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006080 EGL_RED_SIZE, 1,
6081 EGL_GREEN_SIZE, 1,
6082 EGL_BLUE_SIZE, 1,
6083 EGL_ALPHA_SIZE, 1,
6084 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006085 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006086 EGL_NONE
6087 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006088
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006089#ifdef USE_CAIRO_GLESV2
6090 static const EGLint context_attribs[] = {
6091 EGL_CONTEXT_CLIENT_VERSION, 2,
6092 EGL_NONE
6093 };
6094 EGLint api = EGL_OPENGL_ES_API;
6095#else
6096 EGLint *context_attribs = NULL;
6097 EGLint api = EGL_OPENGL_API;
6098#endif
6099
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006100 d->dpy =
6101 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6102 d->display, NULL);
6103
Yuval Fledel45568f62010-12-06 09:18:12 -05006104 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006105 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006106 return -1;
6107 }
6108
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006109 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006110 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006111 return -1;
6112 }
6113
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006114 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6115 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006116 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006117 return -1;
6118 }
6119
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006120 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006121 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006122 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006123 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006124 return -1;
6125 }
6126
Benjamin Franzke0c991632011-09-27 21:57:31 +02006127 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6128 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006129 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006130 return -1;
6131 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006132
6133 return 0;
6134}
6135
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006136static void
6137fini_egl(struct display *display)
6138{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006139 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006140
6141 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6142 EGL_NO_CONTEXT);
6143
6144 eglTerminate(display->dpy);
6145 eglReleaseThread();
6146}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006147#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006148
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006149static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006150init_dummy_surface(struct display *display)
6151{
6152 int len;
6153 void *data;
6154
6155 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006156 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006157 display->dummy_surface =
6158 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6159 1, 1, len);
6160 display->dummy_surface_data = data;
6161}
6162
6163static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006164handle_display_data(struct task *task, uint32_t events)
6165{
6166 struct display *display =
6167 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006168 struct epoll_event ep;
6169 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006170
6171 display->display_fd_events = events;
6172
6173 if (events & EPOLLERR || events & EPOLLHUP) {
6174 display_exit(display);
6175 return;
6176 }
6177
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006178 if (events & EPOLLIN) {
6179 ret = wl_display_dispatch(display->display);
6180 if (ret == -1) {
6181 display_exit(display);
6182 return;
6183 }
6184 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006185
6186 if (events & EPOLLOUT) {
6187 ret = wl_display_flush(display->display);
6188 if (ret == 0) {
6189 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6190 ep.data.ptr = &display->display_task;
6191 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6192 display->display_fd, &ep);
6193 } else if (ret == -1 && errno != EAGAIN) {
6194 display_exit(display);
6195 return;
6196 }
6197 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006198}
6199
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006200static void
6201log_handler(const char *format, va_list args)
6202{
6203 vfprintf(stderr, format, args);
6204}
6205
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006206struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006207display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006208{
6209 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006210
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006211 wl_log_set_handler_client(log_handler);
6212
Peter Huttererf3d62272013-08-08 11:57:05 +10006213 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006214 if (d == NULL)
6215 return NULL;
6216
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006217 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006218 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006219 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006220 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006221 return NULL;
6222 }
6223
Rob Bradford5ab9c752013-07-26 16:29:43 +01006224 d->xkb_context = xkb_context_new(0);
6225 if (d->xkb_context == NULL) {
6226 fprintf(stderr, "Failed to create XKB context\n");
6227 free(d);
6228 return NULL;
6229 }
6230
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006231 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006232 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006233 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006234 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6235 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006236
6237 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006238 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006239 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006240 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006241
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006242 d->registry = wl_display_get_registry(d->display);
6243 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006244
Marek Chalupaa519d062014-12-05 13:49:40 +01006245 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006246 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6247 return NULL;
6248 }
6249
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006250#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006251 if (init_egl(d) < 0)
6252 fprintf(stderr, "EGL does not seem to work, "
6253 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006254#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006255
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006256 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006257
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006258 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006259
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006260 wl_list_init(&d->window_list);
6261
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006262 init_dummy_surface(d);
6263
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006264 return d;
6265}
6266
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006267static void
6268display_destroy_outputs(struct display *display)
6269{
6270 struct output *tmp;
6271 struct output *output;
6272
6273 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6274 output_destroy(output);
6275}
6276
Pekka Paalanene1207c72011-12-16 12:02:09 +02006277static void
6278display_destroy_inputs(struct display *display)
6279{
6280 struct input *tmp;
6281 struct input *input;
6282
6283 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6284 input_destroy(input);
6285}
6286
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006287void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006288display_destroy(struct display *display)
6289{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006290 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006291 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6292 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006293
6294 if (!wl_list_empty(&display->deferred_list))
6295 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6296
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006297 cairo_surface_destroy(display->dummy_surface);
6298 free(display->dummy_surface_data);
6299
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006300 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006301 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006302
Daniel Stone97f68542012-05-30 16:32:01 +01006303 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006304
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006305 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006306 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006307
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006308#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006309 if (display->argb_device)
6310 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006311#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006312
Pekka Paalanen35e82632013-04-25 13:57:48 +03006313 if (display->subcompositor)
6314 wl_subcompositor_destroy(display->subcompositor);
6315
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006316 if (display->xdg_shell)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006317 zxdg_shell_v6_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006318
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006319 if (display->ivi_application)
6320 ivi_application_destroy(display->ivi_application);
6321
Pekka Paalanenc2052982011-12-16 11:41:32 +02006322 if (display->shm)
6323 wl_shm_destroy(display->shm);
6324
6325 if (display->data_device_manager)
6326 wl_data_device_manager_destroy(display->data_device_manager);
6327
6328 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006329 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006330
6331 close(display->epoll_fd);
6332
U. Artie Eoff44874d92012-10-02 21:12:35 -07006333 if (!(display->display_fd_events & EPOLLERR) &&
6334 !(display->display_fd_events & EPOLLHUP))
6335 wl_display_flush(display->display);
6336
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006337 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006338 free(display);
6339}
6340
6341void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006342display_set_user_data(struct display *display, void *data)
6343{
6344 display->user_data = data;
6345}
6346
6347void *
6348display_get_user_data(struct display *display)
6349{
6350 return display->user_data;
6351}
6352
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006353struct wl_display *
6354display_get_display(struct display *display)
6355{
6356 return display->display;
6357}
6358
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006359int
6360display_has_subcompositor(struct display *display)
6361{
6362 if (display->subcompositor)
6363 return 1;
6364
6365 wl_display_roundtrip(display->display);
6366
6367 return display->subcompositor != NULL;
6368}
6369
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006370cairo_device_t *
6371display_get_cairo_device(struct display *display)
6372{
6373 return display->argb_device;
6374}
6375
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006376struct output *
6377display_get_output(struct display *display)
6378{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006379 if (wl_list_empty(&display->output_list))
6380 return NULL;
6381
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006382 return container_of(display->output_list.next, struct output, link);
6383}
6384
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006385struct wl_compositor *
6386display_get_compositor(struct display *display)
6387{
6388 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006389}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006390
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006391uint32_t
6392display_get_serial(struct display *display)
6393{
6394 return display->serial;
6395}
6396
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006397EGLDisplay
6398display_get_egl_display(struct display *d)
6399{
6400 return d->dpy;
6401}
6402
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006403struct wl_data_source *
6404display_create_data_source(struct display *display)
6405{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006406 if (display->data_device_manager)
6407 return wl_data_device_manager_create_data_source(display->data_device_manager);
6408 else
6409 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006410}
6411
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006412EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006413display_get_argb_egl_config(struct display *d)
6414{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006415 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006416}
6417
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006418int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006419display_acquire_window_surface(struct display *display,
6420 struct window *window,
6421 EGLContext ctx)
6422{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006423 struct surface *surface = window->main_surface;
6424
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006425 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006426 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006427
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006428 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006429 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006430}
6431
6432void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006433display_release_window_surface(struct display *display,
6434 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006435{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006436 struct surface *surface = window->main_surface;
6437
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006438 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006439 return;
6440
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006441 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006442}
6443
6444void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006445display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006446{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006447 wl_list_insert(&display->deferred_list, &task->link);
6448}
6449
6450void
6451display_watch_fd(struct display *display,
6452 int fd, uint32_t events, struct task *task)
6453{
6454 struct epoll_event ep;
6455
6456 ep.events = events;
6457 ep.data.ptr = task;
6458 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6459}
6460
6461void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006462display_unwatch_fd(struct display *display, int fd)
6463{
6464 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6465}
6466
6467void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006468display_run(struct display *display)
6469{
6470 struct task *task;
6471 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006472 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006473
Pekka Paalanen826d7952011-12-15 10:14:07 +02006474 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006475 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006476 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006477 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006478 struct task, link);
6479 wl_list_remove(&task->link);
6480 task->run(task, 0);
6481 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006482
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006483 wl_display_dispatch_pending(display->display);
6484
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006485 if (!display->running)
6486 break;
6487
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006488 ret = wl_display_flush(display->display);
6489 if (ret < 0 && errno == EAGAIN) {
6490 ep[0].events =
6491 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6492 ep[0].data.ptr = &display->display_task;
6493
6494 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6495 display->display_fd, &ep[0]);
6496 } else if (ret < 0) {
6497 break;
6498 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006499
6500 count = epoll_wait(display->epoll_fd,
6501 ep, ARRAY_LENGTH(ep), -1);
6502 for (i = 0; i < count; i++) {
6503 task = ep[i].data.ptr;
6504 task->run(task, ep[i].events);
6505 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006506 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006507}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006508
6509void
6510display_exit(struct display *display)
6511{
6512 display->running = 0;
6513}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006514
Carlos Garnacho9c931792016-01-18 23:52:12 +01006515int
6516display_get_data_device_manager_version(struct display *display)
6517{
6518 return display->data_device_manager_version;
6519}
6520
Jan Arne Petersencd997062012-11-18 19:06:44 +01006521void
6522keysym_modifiers_add(struct wl_array *modifiers_map,
6523 const char *name)
6524{
6525 size_t len = strlen(name) + 1;
6526 char *p;
6527
6528 p = wl_array_add(modifiers_map, len);
6529
6530 if (p == NULL)
6531 return;
6532
6533 strncpy(p, name, len);
6534}
6535
6536static xkb_mod_index_t
6537keysym_modifiers_get_index(struct wl_array *modifiers_map,
6538 const char *name)
6539{
6540 xkb_mod_index_t index = 0;
6541 char *p = modifiers_map->data;
6542
6543 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6544 if (strcmp(p, name) == 0)
6545 return index;
6546
6547 index++;
6548 p += strlen(p) + 1;
6549 }
6550
6551 return XKB_MOD_INVALID;
6552}
6553
6554xkb_mod_mask_t
6555keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6556 const char *name)
6557{
6558 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6559
6560 if (index == XKB_MOD_INVALID)
6561 return XKB_MOD_INVALID;
6562
6563 return 1 << index;
6564}