blob: 1c53b5f569a7bb22460fa2f0a49e427cf0b6b5b9 [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>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030058#else /* HAVE_CAIRO_EGL */
59typedef 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>
Bryce Harrington894b3ec2016-10-10 15:31:47 -070066#include <xkbcommon/xkbcommon-compose.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030067#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040068
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050069#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020070#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070071#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070072#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070073#include "shared/xalloc.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080074#include "shared/zalloc.h"
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +080075#include "xdg-shell-unstable-v6-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060076#include "text-cursor-position-client-protocol.h"
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080077#include "pointer-constraints-unstable-v1-client-protocol.h"
78#include "relative-pointer-unstable-v1-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070079#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050080
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050081#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050082
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090083#include <sys/types.h>
84#include "ivi-application-client-protocol.h"
85#define IVI_SURFACE_ID 9000
86
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080087#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
88#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
89
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070090struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030091
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040092struct global {
93 uint32_t name;
94 char *interface;
95 uint32_t version;
96 struct wl_list link;
97};
98
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050099struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -0500100 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400101 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500102 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300103 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400104 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400105 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600106 struct text_cursor_position *text_cursor_position;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800107 struct zxdg_shell_v6 *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900108 struct ivi_application *ivi_application; /* ivi style shell */
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800109 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
110 struct zwp_pointer_constraints_v1 *pointer_constraints;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400111 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500112 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200113 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200114 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400115 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400116
117 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700118 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400119 struct task display_task;
120
121 int epoll_fd;
122 struct wl_list deferred_list;
123
Pekka Paalanen826d7952011-12-15 10:14:07 +0200124 int running;
125
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400126 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400127 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500129 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400130
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400131 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400132
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300133 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300134 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400135
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200136 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400137 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800138 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200139
140 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100141
142 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200143
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200144 /* A hack to get text extents for tooltips */
145 cairo_surface_t *dummy_surface;
146 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200147
148 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530149 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500150};
151
Rob Bradford7507b572012-05-15 17:55:34 +0100152struct window_output {
153 struct output *output;
154 struct wl_list link;
155};
156
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200157struct toysurface {
158 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500159 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800160 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200162 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200163 * If flags has SURFACE_HINT_RESIZE set, the user is
164 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200165 * Returns the Cairo surface to draw to.
166 */
167 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200168 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200169 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200170
171 /*
172 * Post the surface to the server, returning the server allocation
173 * rectangle. The Cairo surface from prepare() must be destroyed
174 * after calling this.
175 */
176 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200177 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200178 struct rectangle *server_allocation);
179
180 /*
181 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800182 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200183 */
184 int (*acquire)(struct toysurface *base, EGLContext ctx);
185
186 /*
187 * Release the toysurface from the EGL context, returning control
188 * to Cairo.
189 */
190 void (*release)(struct toysurface *base);
191
192 /*
193 * Destroy the toysurface, including the Cairo surface, any
194 * backing storage, and the Wayland protocol objects.
195 */
196 void (*destroy)(struct toysurface *base);
197};
198
Pekka Paalanen4e373742013-02-13 16:17:13 +0200199struct surface {
200 struct window *window;
201
202 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300203 struct wl_subsurface *subsurface;
204 int synchronized;
205 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200206 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200207 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300208 int redraw_needed;
209 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300210 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200211
212 struct rectangle allocation;
213 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200214
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200215 struct wl_region *input_region;
216 struct wl_region *opaque_region;
217
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200218 enum window_buffer_type buffer_type;
219 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200220 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200221
222 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300223
224 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200225};
226
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500227struct window {
228 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100229 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500230 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200231 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400232 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500233 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200234 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500235 int x, y;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800236 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400237 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300238 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400239 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400240 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500241 int custom;
242 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400243
Pekka Paalanen99436862012-11-19 17:15:59 +0200244 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400245
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500246 int fullscreen;
247 int maximized;
248
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200249 enum preferred_format preferred_format;
250
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500251 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500252 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400253 window_data_handler_t data_handler;
254 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500255 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400256 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200257 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700258 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400259
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800260 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
261
Pekka Paalanen4e373742013-02-13 16:17:13 +0200262 struct surface *main_surface;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800263 struct zxdg_surface_v6 *xdg_surface;
264 struct zxdg_toplevel_v6 *xdg_toplevel;
265 struct zxdg_popup_v6 *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300266
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700267 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800268 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500269
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900270 struct ivi_surface *ivi_surface;
271
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500272 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500273
Pekka Paalanen35e82632013-04-25 13:57:48 +0300274 /* struct surface::link, contains also main_surface */
275 struct wl_list subsurface_list;
276
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800277 struct zwp_relative_pointer_v1 *relative_pointer;
278 struct zwp_locked_pointer_v1 *locked_pointer;
279 struct input *locked_input;
280 bool pointer_locked;
281 locked_pointer_locked_handler_t pointer_locked_handler;
282 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
283 confined_pointer_confined_handler_t pointer_confined_handler;
284 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
285
286 struct zwp_confined_pointer_v1 *confined_pointer;
287 struct widget *confined_widget;
288 bool confined;
289
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500290 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400291 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500292};
293
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500294struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500295 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200296 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300297 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500298 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400299 struct wl_list link;
300 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500301 widget_resize_handler_t resize_handler;
302 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500303 widget_enter_handler_t enter_handler;
304 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500305 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500306 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700307 widget_touch_down_handler_t touch_down_handler;
308 widget_touch_up_handler_t touch_up_handler;
309 widget_touch_motion_handler_t touch_motion_handler;
310 widget_touch_frame_handler_t touch_frame_handler;
311 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200312 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000313 widget_pointer_frame_handler_t pointer_frame_handler;
314 widget_axis_source_handler_t axis_source_handler;
315 widget_axis_stop_handler_t axis_stop_handler;
316 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400317 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500318 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300319 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500320 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000321 /* If this is set to false then no cairo surface will be
322 * created before redrawing the surface. This is useful if the
323 * redraw handler is going to do completely custom rendering
324 * such as using EGL directly */
325 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400326};
327
Rusty Lynch041815a2013-08-08 21:20:38 -0700328struct touch_point {
329 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800330 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700331 struct widget *widget;
332 struct wl_list link;
333};
334
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400335struct input {
336 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100337 struct wl_seat *seat;
338 struct wl_pointer *pointer;
339 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700340 struct wl_touch *touch;
341 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400342 struct window *pointer_focus;
343 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700344 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300345 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300346 uint32_t cursor_anim_start;
347 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500348 uint32_t cursor_timer_start;
349 uint32_t cursor_anim_current;
350 int cursor_delay_fd;
351 bool cursor_timer_running;
352 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300353 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400354 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400355 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400356 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400357 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400358 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400359
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500360 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500361 struct widget *grab;
362 uint32_t grab_button;
363
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400364 struct wl_data_device *data_device;
365 struct data_offer *drag_offer;
366 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800367 uint32_t touch_grab;
368 int32_t touch_grab_id;
369 float drag_x, drag_y;
370 struct window *drag_focus;
371 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100372
373 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100374 struct xkb_keymap *keymap;
375 struct xkb_state *state;
Bryce Harrington894b3ec2016-10-10 15:31:47 -0700376 struct xkb_compose_table *compose_table;
377 struct xkb_compose_state *compose_state;
Daniel Stone97f68542012-05-30 16:32:01 +0100378 xkb_mod_mask_t control_mask;
379 xkb_mod_mask_t alt_mask;
380 xkb_mod_mask_t shift_mask;
381 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400382
Jonny Lamb06959082014-08-12 14:58:27 +0200383 int32_t repeat_rate_sec;
384 int32_t repeat_rate_nsec;
385 int32_t repeat_delay_sec;
386 int32_t repeat_delay_nsec;
387
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400388 struct task repeat_task;
389 int repeat_timer_fd;
390 uint32_t repeat_sym;
391 uint32_t repeat_key;
392 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500393 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400394};
395
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500396struct output {
397 struct display *display;
398 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800399 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500400 struct rectangle allocation;
401 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600402 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200403 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500404 char *make;
405 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200406
407 display_output_handler_t destroy_handler;
408 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500409};
410
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500411struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500412 struct widget *widget;
413 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500414 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800415
416 uint32_t last_time;
417 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800418 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500419};
420
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500421struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400422 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500423 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500424 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500425 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700426 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500427 const char **entries;
428 uint32_t time;
429 int current;
430 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400431 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500432 menu_func_t func;
433};
434
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300435struct tooltip {
436 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300437 struct widget *widget;
438 char *entry;
439 struct task tooltip_task;
440 int tooltip_fd;
441 float x, y;
442};
443
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700444struct shm_pool {
445 struct wl_shm_pool *pool;
446 size_t size;
447 size_t used;
448 void *data;
449};
450
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400451enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300452 CURSOR_DEFAULT = 100,
453 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400454};
455
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200456static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400457
Pekka Paalanen97777442013-05-22 10:20:05 +0300458/* #define DEBUG */
459
460#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300461
462static void
463debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
464__attribute__ ((format (printf, 4, 5)));
465
466static void
467debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
468{
469 va_list ap;
470 struct timeval tv;
471
472 gettimeofday(&tv, NULL);
473 fprintf(stderr, "%8ld.%03ld ",
474 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
475
476 if (proxy)
477 fprintf(stderr, "%s@%d ",
478 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
479
480 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
481 fprintf(stderr, "%s ", func);
482
483 va_start(ap, fmt);
484 vfprintf(stderr, fmt, ap);
485 va_end(ap);
486}
487
488#define DBG(fmt, ...) \
489 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
490
491#define DBG_OBJ(obj, fmt, ...) \
492 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
493
494#else
495
496#define DBG(...) do {} while (0)
497#define DBG_OBJ(...) do {} while (0)
498
499#endif
500
Alexander Larsson1818e312013-05-22 14:41:31 +0200501static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200502surface_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 +0200503{
504 int32_t tmp;
505
506 switch (buffer_transform) {
507 case WL_OUTPUT_TRANSFORM_90:
508 case WL_OUTPUT_TRANSFORM_270:
509 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
510 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
511 tmp = *width;
512 *width = *height;
513 *height = tmp;
514 break;
515 default:
516 break;
517 }
518
519 *width *= buffer_scale;
520 *height *= buffer_scale;
521}
522
523static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200524buffer_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 +0200525{
526 int32_t tmp;
527
528 switch (buffer_transform) {
529 case WL_OUTPUT_TRANSFORM_90:
530 case WL_OUTPUT_TRANSFORM_270:
531 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
532 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
533 tmp = *width;
534 *width = *height;
535 *height = tmp;
536 break;
537 default:
538 break;
539 }
540
541 *width /= buffer_scale;
542 *height /= buffer_scale;
543}
544
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500545#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400546
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547struct egl_window_surface {
548 struct toysurface base;
549 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100550 struct display *display;
551 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200552 struct wl_egl_window *egl_window;
553 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100554};
555
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556static struct egl_window_surface *
557to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100560}
561
562static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200564 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200565 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100566{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200567 struct egl_window_surface *surface = to_egl_window_surface(base);
568
Alexander Larsson1818e312013-05-22 14:41:31 +0200569 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
570
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
572 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
573
574 return cairo_surface_reference(surface->cairo_surface);
575}
576
577static void
578egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200579 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200580 struct rectangle *server_allocation)
581{
582 struct egl_window_surface *surface = to_egl_window_surface(base);
583
584 cairo_gl_surface_swapbuffers(surface->cairo_surface);
585 wl_egl_window_get_attached_size(surface->egl_window,
586 &server_allocation->width,
587 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200588
589 buffer_to_surface_size (buffer_transform, buffer_scale,
590 &server_allocation->width,
591 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200592}
593
594static int
595egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
596{
597 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200598 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100599
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200600 device = cairo_surface_get_device(surface->cairo_surface);
601 if (!device)
602 return -1;
603
604 if (!ctx) {
605 if (device == surface->display->argb_device)
606 ctx = surface->display->argb_ctx;
607 else
608 assert(0);
609 }
610
611 cairo_device_flush(device);
612 cairo_device_acquire(device);
613 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
614 surface->egl_surface, ctx))
615 fprintf(stderr, "failed to make surface current\n");
616
617 return 0;
618}
619
620static void
621egl_window_surface_release(struct toysurface *base)
622{
623 struct egl_window_surface *surface = to_egl_window_surface(base);
624 cairo_device_t *device;
625
626 device = cairo_surface_get_device(surface->cairo_surface);
627 if (!device)
628 return;
629
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200630 if (!eglMakeCurrent(surface->display->dpy,
631 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200632 fprintf(stderr, "failed to make context current\n");
633
634 cairo_device_release(device);
635}
636
637static void
638egl_window_surface_destroy(struct toysurface *base)
639{
640 struct egl_window_surface *surface = to_egl_window_surface(base);
641 struct display *d = surface->display;
642
643 cairo_surface_destroy(surface->cairo_surface);
644 eglDestroySurface(d->dpy, surface->egl_surface);
645 wl_egl_window_destroy(surface->egl_window);
646 surface->surface = NULL;
647
648 free(surface);
649}
650
651static struct toysurface *
652egl_window_surface_create(struct display *display,
653 struct wl_surface *wl_surface,
654 uint32_t flags,
655 struct rectangle *rectangle)
656{
657 struct egl_window_surface *surface;
658
Pekka Paalanenb3627362012-11-19 17:16:00 +0200659 if (display->dpy == EGL_NO_DISPLAY)
660 return NULL;
661
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800662 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200663 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100664 return NULL;
665
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200666 surface->base.prepare = egl_window_surface_prepare;
667 surface->base.swap = egl_window_surface_swap;
668 surface->base.acquire = egl_window_surface_acquire;
669 surface->base.release = egl_window_surface_release;
670 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100671
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200672 surface->display = display;
673 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400674
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200675 surface->egl_window = wl_egl_window_create(surface->surface,
676 rectangle->width,
677 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100678
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100679 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100680 weston_platform_create_egl_surface(display->dpy,
681 display->argb_config,
682 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100683
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200684 surface->cairo_surface =
685 cairo_gl_surface_create_for_egl(display->argb_device,
686 surface->egl_surface,
687 rectangle->width,
688 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100689
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200690 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100691}
692
Pekka Paalanenb3627362012-11-19 17:16:00 +0200693#else
694
695static struct toysurface *
696egl_window_surface_create(struct display *display,
697 struct wl_surface *wl_surface,
698 uint32_t flags,
699 struct rectangle *rectangle)
700{
701 return NULL;
702}
703
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400704#endif
705
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200706struct shm_surface_data {
707 struct wl_buffer *buffer;
708 struct shm_pool *pool;
709};
710
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400711struct wl_buffer *
712display_get_buffer_for_surface(struct display *display,
713 cairo_surface_t *surface)
714{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200715 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400716
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200717 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400718
719 return data->buffer;
720}
721
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500722static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723shm_pool_destroy(struct shm_pool *pool);
724
725static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400726shm_surface_data_destroy(void *p)
727{
728 struct shm_surface_data *data = p;
729
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200730 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731 if (data->pool)
732 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300733
734 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400735}
736
Kristian Høgsberg16626282012-04-03 11:21:27 -0400737static struct wl_shm_pool *
738make_shm_pool(struct display *display, int size, void **data)
739{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400740 struct wl_shm_pool *pool;
741 int fd;
742
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300743 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400744 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300745 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
746 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400747 return NULL;
748 }
749
750 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400751 if (*data == MAP_FAILED) {
752 fprintf(stderr, "mmap failed: %m\n");
753 close(fd);
754 return NULL;
755 }
756
757 pool = wl_shm_create_pool(display->shm, fd, size);
758
759 close(fd);
760
761 return pool;
762}
763
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700764static struct shm_pool *
765shm_pool_create(struct display *display, size_t size)
766{
767 struct shm_pool *pool = malloc(sizeof *pool);
768
769 if (!pool)
770 return NULL;
771
772 pool->pool = make_shm_pool(display, size, &pool->data);
773 if (!pool->pool) {
774 free(pool);
775 return NULL;
776 }
777
778 pool->size = size;
779 pool->used = 0;
780
781 return pool;
782}
783
784static void *
785shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
786{
787 if (pool->used + size > pool->size)
788 return NULL;
789
790 *offset = pool->used;
791 pool->used += size;
792
793 return (char *) pool->data + *offset;
794}
795
796/* destroy the pool. this does not unmap the memory though */
797static void
798shm_pool_destroy(struct shm_pool *pool)
799{
800 munmap(pool->data, pool->size);
801 wl_shm_pool_destroy(pool->pool);
802 free(pool);
803}
804
805/* Start allocating from the beginning of the pool again */
806static void
807shm_pool_reset(struct shm_pool *pool)
808{
809 pool->used = 0;
810}
811
812static int
813data_length_for_shm_surface(struct rectangle *rect)
814{
815 int stride;
816
817 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
818 rect->width);
819 return stride * rect->height;
820}
821
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500822static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700823display_create_shm_surface_from_pool(struct display *display,
824 struct rectangle *rectangle,
825 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826{
827 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400828 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400829 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200830 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700831 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400832 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833
834 data = malloc(sizeof *data);
835 if (data == NULL)
836 return NULL;
837
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200838 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
839 cairo_format = CAIRO_FORMAT_RGB16_565;
840 else
841 cairo_format = CAIRO_FORMAT_ARGB32;
842
843 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700844 length = stride * rectangle->height;
845 data->pool = NULL;
846 map = shm_pool_allocate(pool, length, &offset);
847
848 if (!map) {
849 free(data);
850 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400851 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400852
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400853 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200854 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400855 rectangle->width,
856 rectangle->height,
857 stride);
858
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200859 cairo_surface_set_user_data(surface, &shm_surface_data_key,
860 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400861
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200862 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
863 format = WL_SHM_FORMAT_RGB565;
864 else {
865 if (flags & SURFACE_OPAQUE)
866 format = WL_SHM_FORMAT_XRGB8888;
867 else
868 format = WL_SHM_FORMAT_ARGB8888;
869 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400870
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200871 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
872 rectangle->width,
873 rectangle->height,
874 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400875
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700876 return surface;
877}
878
879static cairo_surface_t *
880display_create_shm_surface(struct display *display,
881 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200882 struct shm_pool *alternate_pool,
883 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700884{
885 struct shm_surface_data *data;
886 struct shm_pool *pool;
887 cairo_surface_t *surface;
888
Pekka Paalanen99436862012-11-19 17:15:59 +0200889 if (alternate_pool) {
890 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700891 surface = display_create_shm_surface_from_pool(display,
892 rectangle,
893 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200894 alternate_pool);
895 if (surface) {
896 data = cairo_surface_get_user_data(surface,
897 &shm_surface_data_key);
898 goto out;
899 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700900 }
901
902 pool = shm_pool_create(display,
903 data_length_for_shm_surface(rectangle));
904 if (!pool)
905 return NULL;
906
907 surface =
908 display_create_shm_surface_from_pool(display, rectangle,
909 flags, pool);
910
911 if (!surface) {
912 shm_pool_destroy(pool);
913 return NULL;
914 }
915
916 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200917 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700918 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400919
Pekka Paalanen99436862012-11-19 17:15:59 +0200920out:
921 if (data_ret)
922 *data_ret = data;
923
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400924 return surface;
925}
926
nobled7b87cb02011-02-01 18:51:47 +0000927static int
928check_size(struct rectangle *rect)
929{
930 if (rect->width && rect->height)
931 return 0;
932
933 fprintf(stderr, "tried to create surface of "
934 "width: %d, height: %d\n", rect->width, rect->height);
935 return -1;
936}
937
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400938cairo_surface_t *
939display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100940 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400941 struct rectangle *rectangle,
942 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400943{
nobled7b87cb02011-02-01 18:51:47 +0000944 if (check_size(rectangle) < 0)
945 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200946
947 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200948 return display_create_shm_surface(display, rectangle, flags,
949 NULL, NULL);
950}
951
Pekka Paalanena4eda732012-11-19 17:16:02 +0200952struct shm_surface_leaf {
953 cairo_surface_t *cairo_surface;
954 /* 'data' is automatically destroyed, when 'cairo_surface' is */
955 struct shm_surface_data *data;
956
957 struct shm_pool *resize_pool;
958 int busy;
959};
960
961static void
962shm_surface_leaf_release(struct shm_surface_leaf *leaf)
963{
964 if (leaf->cairo_surface)
965 cairo_surface_destroy(leaf->cairo_surface);
966 /* leaf->data already destroyed via cairo private */
967
968 if (leaf->resize_pool)
969 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200970
971 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200972}
973
Pekka Paalanenaef02542013-04-25 13:57:47 +0300974#define MAX_LEAVES 3
975
Pekka Paalanen99436862012-11-19 17:15:59 +0200976struct shm_surface {
977 struct toysurface base;
978 struct display *display;
979 struct wl_surface *surface;
980 uint32_t flags;
981 int dx, dy;
982
Pekka Paalanenaef02542013-04-25 13:57:47 +0300983 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200984 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200985};
986
987static struct shm_surface *
988to_shm_surface(struct toysurface *base)
989{
990 return container_of(base, struct shm_surface, base);
991}
992
Pekka Paalanena4eda732012-11-19 17:16:02 +0200993static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300994shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
995{
996#ifdef DEBUG
997 struct shm_surface_leaf *leaf;
998 char bufs[MAX_LEAVES + 1];
999 int i;
1000
1001 for (i = 0; i < MAX_LEAVES; i++) {
1002 leaf = &surface->leaf[i];
1003
1004 if (leaf->busy)
1005 bufs[i] = 'b';
1006 else if (leaf->cairo_surface)
1007 bufs[i] = 'a';
1008 else
1009 bufs[i] = ' ';
1010 }
1011
1012 bufs[MAX_LEAVES] = '\0';
1013 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1014#endif
1015}
1016
1017static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001018shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1019{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001020 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001021 struct shm_surface_leaf *leaf;
1022 int i;
1023 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001024
1025 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001026
Pekka Paalanenaef02542013-04-25 13:57:47 +03001027 for (i = 0; i < MAX_LEAVES; i++) {
1028 leaf = &surface->leaf[i];
1029 if (leaf->data && leaf->data->buffer == buffer) {
1030 leaf->busy = 0;
1031 break;
1032 }
1033 }
1034 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001035
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 /* Leave one free leaf with storage, release others */
1037 free_found = 0;
1038 for (i = 0; i < MAX_LEAVES; i++) {
1039 leaf = &surface->leaf[i];
1040
1041 if (!leaf->cairo_surface || leaf->busy)
1042 continue;
1043
1044 if (!free_found)
1045 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001046 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001047 shm_surface_leaf_release(leaf);
1048 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001049
Pekka Paalanen97777442013-05-22 10:20:05 +03001050 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001051}
1052
1053static const struct wl_buffer_listener shm_surface_buffer_listener = {
1054 shm_surface_buffer_release
1055};
1056
Pekka Paalanen99436862012-11-19 17:15:59 +02001057static cairo_surface_t *
1058shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001059 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001060 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001061{
Pekka Paalanenec076692012-11-30 13:37:27 +02001062 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001063 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001064 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001065 struct shm_surface_leaf *leaf = NULL;
1066 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001067
1068 surface->dx = dx;
1069 surface->dy = dy;
1070
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001071 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001072 for (i = 0; i < MAX_LEAVES; i++) {
1073 if (surface->leaf[i].busy)
1074 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001075
Pekka Paalanenaef02542013-04-25 13:57:47 +03001076 if (!leaf || surface->leaf[i].cairo_surface)
1077 leaf = &surface->leaf[i];
1078 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001079 DBG_OBJ(surface->surface, "pick leaf %d\n",
1080 (int)(leaf - &surface->leaf[0]));
1081
Pekka Paalanenaef02542013-04-25 13:57:47 +03001082 if (!leaf) {
1083 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001085 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001086 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001087 }
1088
Pekka Paalanena4eda732012-11-19 17:16:02 +02001089 if (!resize_hint && leaf->resize_pool) {
1090 cairo_surface_destroy(leaf->cairo_surface);
1091 leaf->cairo_surface = NULL;
1092 shm_pool_destroy(leaf->resize_pool);
1093 leaf->resize_pool = NULL;
1094 }
1095
Alexander Larsson1818e312013-05-22 14:41:31 +02001096 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1097
Pekka Paalanena4eda732012-11-19 17:16:02 +02001098 if (leaf->cairo_surface &&
1099 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1100 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001101 goto out;
1102
Pekka Paalanena4eda732012-11-19 17:16:02 +02001103 if (leaf->cairo_surface)
1104 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001105
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001106#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001107 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001108 /* Create a big pool to allocate from, while continuously
1109 * resizing. Mmapping a new pool in the server
1110 * is relatively expensive, so reusing a pool performs
1111 * better, but may temporarily reserve unneeded memory.
1112 */
1113 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001114 leaf->resize_pool = shm_pool_create(surface->display,
1115 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001116 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001117#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001118
Alexander Larsson1818e312013-05-22 14:41:31 +02001119 rect.width = width;
1120 rect.height = height;
1121
Pekka Paalanena4eda732012-11-19 17:16:02 +02001122 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001123 display_create_shm_surface(surface->display, &rect,
1124 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001125 leaf->resize_pool,
1126 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001127 if (!leaf->cairo_surface)
1128 return NULL;
1129
Pekka Paalanena4eda732012-11-19 17:16:02 +02001130 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001131 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001132
1133out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001134 surface->current = leaf;
1135
1136 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001137}
1138
1139static void
1140shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001141 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001142 struct rectangle *server_allocation)
1143{
1144 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001145 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001146
1147 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001148 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001149 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001150 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001151
Alexander Larsson1818e312013-05-22 14:41:31 +02001152 buffer_to_surface_size (buffer_transform, buffer_scale,
1153 &server_allocation->width,
1154 &server_allocation->height);
1155
Pekka Paalanena4eda732012-11-19 17:16:02 +02001156 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001157 surface->dx, surface->dy);
1158 wl_surface_damage(surface->surface, 0, 0,
1159 server_allocation->width, server_allocation->height);
1160 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001161
Pekka Paalanen71233882013-04-25 13:57:53 +03001162 DBG_OBJ(surface->surface, "leaf %d busy\n",
1163 (int)(leaf - &surface->leaf[0]));
1164
Pekka Paalanena4eda732012-11-19 17:16:02 +02001165 leaf->busy = 1;
1166 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001167}
1168
1169static int
1170shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1171{
1172 return -1;
1173}
1174
1175static void
1176shm_surface_release(struct toysurface *base)
1177{
1178}
1179
1180static void
1181shm_surface_destroy(struct toysurface *base)
1182{
1183 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001184 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001185
Pekka Paalanenaef02542013-04-25 13:57:47 +03001186 for (i = 0; i < MAX_LEAVES; i++)
1187 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001188
1189 free(surface);
1190}
1191
1192static struct toysurface *
1193shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1194 uint32_t flags, struct rectangle *rectangle)
1195{
1196 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001197 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001198
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001199 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001200 surface->base.prepare = shm_surface_prepare;
1201 surface->base.swap = shm_surface_swap;
1202 surface->base.acquire = shm_surface_acquire;
1203 surface->base.release = shm_surface_release;
1204 surface->base.destroy = shm_surface_destroy;
1205
1206 surface->display = display;
1207 surface->surface = wl_surface;
1208 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001209
1210 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001211}
1212
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001213/*
1214 * The following correspondences between file names and cursors was copied
1215 * from: https://bugs.kde.org/attachment.cgi?id=67313
1216 */
1217
1218static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001219 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001220 "sw-resize",
1221 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001222};
1223
1224static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001225 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001226 "se-resize",
1227 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001228};
1229
1230static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001232 "s-resize",
1233 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001234};
1235
1236static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001237 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001238 "closedhand",
1239 "208530c400c041818281048008011002"
1240};
1241
1242static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001243 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001244 "default",
1245 "top_left_arrow",
1246 "left-arrow"
1247};
1248
1249static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001250 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001251 "w-resize",
1252 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001253};
1254
1255static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001256 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001257 "e-resize",
1258 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001259};
1260
1261static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001262 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001263 "nw-resize",
1264 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001265};
1266
1267static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001268 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001269 "ne-resize",
1270 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001271};
1272
1273static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001274 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001275 "n-resize",
1276 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001277};
1278
1279static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001280 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001281 "ibeam",
1282 "text"
1283};
1284
1285static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001286 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001287 "pointer",
1288 "pointing_hand",
1289 "e29285e634086352946a0e7090d73106"
1290};
1291
1292static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001293 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001294 "wait",
1295 "0426c94ea35c87780ff01dc239897213"
1296};
1297
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001298static const char *move_draggings[] = {
1299 "dnd-move"
1300};
1301
1302static const char *copy_draggings[] = {
1303 "dnd-copy"
1304};
1305
1306static const char *forbidden_draggings[] = {
1307 "dnd-none",
1308 "dnd-no-drop"
1309};
1310
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001311struct cursor_alternatives {
1312 const char **names;
1313 size_t count;
1314};
1315
1316static const struct cursor_alternatives cursors[] = {
1317 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1318 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1319 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1320 {grabbings, ARRAY_LENGTH(grabbings)},
1321 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1322 {left_sides, ARRAY_LENGTH(left_sides)},
1323 {right_sides, ARRAY_LENGTH(right_sides)},
1324 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1325 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1326 {top_sides, ARRAY_LENGTH(top_sides)},
1327 {xterms, ARRAY_LENGTH(xterms)},
1328 {hand1s, ARRAY_LENGTH(hand1s)},
1329 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001330 {move_draggings, ARRAY_LENGTH(move_draggings)},
1331 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1332 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001333};
1334
1335static void
1336create_cursors(struct display *display)
1337{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001338 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001339 struct weston_config *config;
1340 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001341 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001342 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001343 unsigned int i, j;
1344 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001345
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001346 config_file = weston_config_get_name_from_env();
1347 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001348 s = weston_config_get_section(config, "shell", NULL, NULL);
1349 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1350 weston_config_section_get_int(s, "cursor-size", &size, 32);
1351 weston_config_destroy(config);
1352
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001353 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001354 if (!display->cursor_theme) {
1355 fprintf(stderr, "could not load theme '%s'\n", theme);
1356 return;
1357 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001358 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001359 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001360 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001361
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001362 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001363 cursor = NULL;
1364 for (j = 0; !cursor && j < cursors[i].count; ++j)
1365 cursor = wl_cursor_theme_get_cursor(
1366 display->cursor_theme, cursors[i].names[j]);
1367
1368 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001369 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001370 cursors[i].names[0]);
1371
1372 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001373 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001374}
1375
1376static void
1377destroy_cursors(struct display *display)
1378{
1379 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001380 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001381}
1382
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001383struct wl_cursor_image *
1384display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001385{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001386 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001387
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001388 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001389}
1390
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001391static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001392surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001393{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001394 if (!surface->cairo_surface)
1395 return;
1396
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001397 if (surface->opaque_region) {
1398 wl_surface_set_opaque_region(surface->surface,
1399 surface->opaque_region);
1400 wl_region_destroy(surface->opaque_region);
1401 surface->opaque_region = NULL;
1402 }
1403
1404 if (surface->input_region) {
1405 wl_surface_set_input_region(surface->surface,
1406 surface->input_region);
1407 wl_region_destroy(surface->input_region);
1408 surface->input_region = NULL;
1409 }
1410
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001411 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001412 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001413 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001414
Pekka Paalanen89dee002013-02-13 16:17:20 +02001415 cairo_surface_destroy(surface->cairo_surface);
1416 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001417}
1418
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001419int
1420window_has_focus(struct window *window)
1421{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001422 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001423}
1424
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001425static void
1426window_close(struct window *window)
1427{
1428 if (window->close_handler)
1429 window->close_handler(window->user_data);
1430 else
1431 display_exit(window->display);
1432}
1433
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001434struct display *
1435window_get_display(struct window *window)
1436{
1437 return window->display;
1438}
1439
Pekka Paalanen89dee002013-02-13 16:17:20 +02001440static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001441handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1442 int32_t width, int32_t height)
1443{
1444 struct window *window = data;
1445
1446 window_schedule_resize(window, width, height);
1447}
1448
1449static const struct ivi_surface_listener ivi_surface_listener = {
1450 handle_ivi_surface_configure,
1451};
1452
1453static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001454surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001455{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001457 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001458
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001459 if (!surface->toysurface && display->dpy &&
1460 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001461 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001462 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001463 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001464 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001465 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001466 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001467
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001468 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001469 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001470 surface->surface,
1471 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001472
Pekka Paalanen89dee002013-02-13 16:17:20 +02001473 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001474 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001475 allocation.width, allocation.height, flags,
1476 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001477}
1478
1479static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001480window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001481{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001482 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001483 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001484
Pekka Paalanenec076692012-11-30 13:37:27 +02001485 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001486 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001487
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001488 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1489 flags |= SURFACE_HINT_RGB565;
1490
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001491 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001492}
1493
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001494int
1495window_get_buffer_transform(struct window *window)
1496{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001497 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001498}
1499
1500void
1501window_set_buffer_transform(struct window *window,
1502 enum wl_output_transform transform)
1503{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001504 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001505 wl_surface_set_buffer_transform(window->main_surface->surface,
1506 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001507}
1508
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001509void
1510window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001511 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001512{
1513 window->main_surface->buffer_scale = scale;
1514 wl_surface_set_buffer_scale(window->main_surface->surface,
1515 scale);
1516}
1517
1518uint32_t
1519window_get_buffer_scale(struct window *window)
1520{
1521 return window->main_surface->buffer_scale;
1522}
1523
Alexander Larssond68f5232013-05-22 14:41:33 +02001524uint32_t
1525window_get_output_scale(struct window *window)
1526{
1527 struct window_output *window_output;
1528 struct window_output *window_output_tmp;
1529 int scale = 1;
1530
1531 wl_list_for_each_safe(window_output, window_output_tmp,
1532 &window->window_output_list, link) {
1533 if (window_output->output->scale > scale)
1534 scale = window_output->output->scale;
1535 }
1536
1537 return scale;
1538}
1539
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001540static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001541
Pekka Paalanen4e373742013-02-13 16:17:13 +02001542static void
1543surface_destroy(struct surface *surface)
1544{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001545 if (surface->frame_cb)
1546 wl_callback_destroy(surface->frame_cb);
1547
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001548 if (surface->input_region)
1549 wl_region_destroy(surface->input_region);
1550
1551 if (surface->opaque_region)
1552 wl_region_destroy(surface->opaque_region);
1553
Pekka Paalanen35e82632013-04-25 13:57:48 +03001554 if (surface->subsurface)
1555 wl_subsurface_destroy(surface->subsurface);
1556
Pekka Paalanen4e373742013-02-13 16:17:13 +02001557 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001558
1559 if (surface->toysurface)
1560 surface->toysurface->destroy(surface->toysurface);
1561
Pekka Paalanen35e82632013-04-25 13:57:48 +03001562 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001563 free(surface);
1564}
1565
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001566void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001567window_destroy(struct window *window)
1568{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001569 struct display *display = window->display;
1570 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001571 struct window_output *window_output;
1572 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001573
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001574 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001575
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001576 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001577 if (input->touch_focus == window)
1578 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001579 if (input->pointer_focus == window)
1580 input->pointer_focus = NULL;
1581 if (input->keyboard_focus == window)
1582 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001583 if (input->focus_widget &&
1584 input->focus_widget->window == window)
1585 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001586 }
1587
Rob Bradford7507b572012-05-15 17:55:34 +01001588 wl_list_for_each_safe(window_output, window_output_tmp,
1589 &window->window_output_list, link) {
1590 free (window_output);
1591 }
1592
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001593 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001594 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001595
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001596 if (window->xdg_toplevel)
1597 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001598 if (window->xdg_popup)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001599 zxdg_popup_v6_destroy(window->xdg_popup);
1600 if (window->xdg_surface)
1601 zxdg_surface_v6_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001602
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001603 if (window->ivi_surface)
1604 ivi_surface_destroy(window->ivi_surface);
1605
Pekka Paalanen4e373742013-02-13 16:17:13 +02001606 surface_destroy(window->main_surface);
1607
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001608 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001609
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001610 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001611 free(window);
1612}
1613
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001614static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001615widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001616{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001617 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001618
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619 wl_list_for_each(child, &widget->child_list, link) {
1620 target = widget_find_widget(child, x, y);
1621 if (target)
1622 return target;
1623 }
1624
1625 if (widget->allocation.x <= x &&
1626 x < widget->allocation.x + widget->allocation.width &&
1627 widget->allocation.y <= y &&
1628 y < widget->allocation.y + widget->allocation.height) {
1629 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001630 }
1631
1632 return NULL;
1633}
1634
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001635static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001636window_find_widget(struct window *window, int32_t x, int32_t y)
1637{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001638 struct surface *surface;
1639 struct widget *widget;
1640
1641 wl_list_for_each(surface, &window->subsurface_list, link) {
1642 widget = widget_find_widget(surface->widget, x, y);
1643 if (widget)
1644 return widget;
1645 }
1646
1647 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001648}
1649
1650static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001651widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001652{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001653 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001654
Peter Huttererf3d62272013-08-08 11:57:05 +10001655 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001656 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001657 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001658 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001659 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001660 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001661 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001662 widget->tooltip = NULL;
1663 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001664 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001665 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001666
1667 return widget;
1668}
1669
1670struct widget *
1671window_add_widget(struct window *window, void *data)
1672{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001673 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001674
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001675 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001676 wl_list_init(&widget->link);
1677 window->main_surface->widget = widget;
1678
1679 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001680}
1681
1682struct widget *
1683widget_add_widget(struct widget *parent, void *data)
1684{
1685 struct widget *widget;
1686
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001687 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001688 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001689
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001690 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001691}
1692
1693void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001694widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001696 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001697 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001698 struct input *input;
1699
Pekka Paalanen35e82632013-04-25 13:57:48 +03001700 /* Destroy the sub-surface along with the root widget */
1701 if (surface->widget == widget && surface->subsurface)
1702 surface_destroy(widget->surface);
1703
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001704 if (widget->tooltip)
1705 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001706
Pekka Paalanene156fb62012-01-19 13:51:38 +02001707 wl_list_for_each(input, &display->input_list, link) {
1708 if (input->focus_widget == widget)
1709 input->focus_widget = NULL;
1710 }
1711
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001712 wl_list_remove(&widget->link);
1713 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001714}
1715
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001716void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001717widget_set_default_cursor(struct widget *widget, int cursor)
1718{
1719 widget->default_cursor = cursor;
1720}
1721
1722void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001723widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001724{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001725 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001726}
1727
1728void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001729widget_set_size(struct widget *widget, int32_t width, int32_t height)
1730{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001731 widget->allocation.width = width;
1732 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001733}
1734
1735void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001736widget_set_allocation(struct widget *widget,
1737 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001738{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001739 widget->allocation.x = x;
1740 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001741 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001742}
1743
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001744void
1745widget_set_transparent(struct widget *widget, int transparent)
1746{
1747 widget->opaque = !transparent;
1748}
1749
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001750void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001751widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001752{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001753 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001754}
1755
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001756static cairo_surface_t *
1757widget_get_cairo_surface(struct widget *widget)
1758{
1759 struct surface *surface = widget->surface;
1760 struct window *window = widget->window;
1761
Neil Roberts97b747c2013-12-19 16:17:12 +00001762 assert(widget->use_cairo);
1763
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001764 if (!surface->cairo_surface) {
1765 if (surface == window->main_surface)
1766 window_create_main_surface(window);
1767 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001768 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001769 }
1770
1771 return surface->cairo_surface;
1772}
1773
Alexander Larsson15901f02013-05-22 14:41:25 +02001774static void
1775widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1776{
1777 struct surface *surface = widget->surface;
1778 double angle;
1779 cairo_matrix_t m;
1780 enum wl_output_transform transform;
1781 int surface_width, surface_height;
1782 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001783 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001784
1785 surface_width = surface->allocation.width;
1786 surface_height = surface->allocation.height;
1787
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001788 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001789 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001790
Alexander Larsson15901f02013-05-22 14:41:25 +02001791 switch (transform) {
1792 case WL_OUTPUT_TRANSFORM_FLIPPED:
1793 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1794 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1795 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1796 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1797 break;
1798 default:
1799 cairo_matrix_init_identity(&m);
1800 break;
1801 }
1802
1803 switch (transform) {
1804 case WL_OUTPUT_TRANSFORM_NORMAL:
1805 default:
1806 angle = 0;
1807 translate_x = 0;
1808 translate_y = 0;
1809 break;
1810 case WL_OUTPUT_TRANSFORM_FLIPPED:
1811 angle = 0;
1812 translate_x = surface_width;
1813 translate_y = 0;
1814 break;
1815 case WL_OUTPUT_TRANSFORM_90:
1816 angle = M_PI_2;
1817 translate_x = surface_height;
1818 translate_y = 0;
1819 break;
1820 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1821 angle = M_PI_2;
1822 translate_x = surface_height;
1823 translate_y = surface_width;
1824 break;
1825 case WL_OUTPUT_TRANSFORM_180:
1826 angle = M_PI;
1827 translate_x = surface_width;
1828 translate_y = surface_height;
1829 break;
1830 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1831 angle = M_PI;
1832 translate_x = 0;
1833 translate_y = surface_height;
1834 break;
1835 case WL_OUTPUT_TRANSFORM_270:
1836 angle = M_PI + M_PI_2;
1837 translate_x = 0;
1838 translate_y = surface_width;
1839 break;
1840 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1841 angle = M_PI + M_PI_2;
1842 translate_x = 0;
1843 translate_y = 0;
1844 break;
1845 }
1846
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001847 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001848 cairo_translate(cr, translate_x, translate_y);
1849 cairo_rotate(cr, angle);
1850 cairo_transform(cr, &m);
1851}
1852
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001853cairo_t *
1854widget_cairo_create(struct widget *widget)
1855{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001856 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001857 cairo_surface_t *cairo_surface;
1858 cairo_t *cr;
1859
1860 cairo_surface = widget_get_cairo_surface(widget);
1861 cr = cairo_create(cairo_surface);
1862
Alexander Larsson15901f02013-05-22 14:41:25 +02001863 widget_cairo_update_transform(widget, cr);
1864
Pekka Paalanen35e82632013-04-25 13:57:48 +03001865 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1866
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001867 return cr;
1868}
1869
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001870struct wl_surface *
1871widget_get_wl_surface(struct widget *widget)
1872{
1873 return widget->surface->surface;
1874}
1875
Neil Roberts5e10a042013-09-09 00:40:17 +01001876struct wl_subsurface *
1877widget_get_wl_subsurface(struct widget *widget)
1878{
1879 return widget->surface->subsurface;
1880}
1881
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001882uint32_t
1883widget_get_last_time(struct widget *widget)
1884{
1885 return widget->surface->last_time;
1886}
1887
1888void
1889widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1890{
1891 struct wl_compositor *comp = widget->window->display->compositor;
1892 struct surface *surface = widget->surface;
1893
1894 if (!surface->input_region)
1895 surface->input_region = wl_compositor_create_region(comp);
1896
1897 if (rect) {
1898 wl_region_add(surface->input_region,
1899 rect->x, rect->y, rect->width, rect->height);
1900 }
1901}
1902
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001903void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001904widget_set_resize_handler(struct widget *widget,
1905 widget_resize_handler_t handler)
1906{
1907 widget->resize_handler = handler;
1908}
1909
1910void
1911widget_set_redraw_handler(struct widget *widget,
1912 widget_redraw_handler_t handler)
1913{
1914 widget->redraw_handler = handler;
1915}
1916
1917void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001918widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001919{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001920 widget->enter_handler = handler;
1921}
1922
1923void
1924widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1925{
1926 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001927}
1928
1929void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001930widget_set_motion_handler(struct widget *widget,
1931 widget_motion_handler_t handler)
1932{
1933 widget->motion_handler = handler;
1934}
1935
1936void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001937widget_set_button_handler(struct widget *widget,
1938 widget_button_handler_t handler)
1939{
1940 widget->button_handler = handler;
1941}
1942
1943void
Rusty Lynch041815a2013-08-08 21:20:38 -07001944widget_set_touch_up_handler(struct widget *widget,
1945 widget_touch_up_handler_t handler)
1946{
1947 widget->touch_up_handler = handler;
1948}
1949
1950void
1951widget_set_touch_down_handler(struct widget *widget,
1952 widget_touch_down_handler_t handler)
1953{
1954 widget->touch_down_handler = handler;
1955}
1956
1957void
1958widget_set_touch_motion_handler(struct widget *widget,
1959 widget_touch_motion_handler_t handler)
1960{
1961 widget->touch_motion_handler = handler;
1962}
1963
1964void
1965widget_set_touch_frame_handler(struct widget *widget,
1966 widget_touch_frame_handler_t handler)
1967{
1968 widget->touch_frame_handler = handler;
1969}
1970
1971void
1972widget_set_touch_cancel_handler(struct widget *widget,
1973 widget_touch_cancel_handler_t handler)
1974{
1975 widget->touch_cancel_handler = handler;
1976}
1977
1978void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001979widget_set_axis_handler(struct widget *widget,
1980 widget_axis_handler_t handler)
1981{
1982 widget->axis_handler = handler;
1983}
1984
Peter Hutterer87743e92016-01-18 16:38:22 +10001985void
1986widget_set_pointer_frame_handler(struct widget *widget,
1987 widget_pointer_frame_handler_t handler)
1988{
1989 widget->pointer_frame_handler = handler;
1990}
1991
1992void
1993widget_set_axis_handlers(struct widget *widget,
1994 widget_axis_handler_t axis_handler,
1995 widget_axis_source_handler_t axis_source_handler,
1996 widget_axis_stop_handler_t axis_stop_handler,
1997 widget_axis_discrete_handler_t axis_discrete_handler)
1998{
1999 widget->axis_handler = axis_handler;
2000 widget->axis_source_handler = axis_source_handler;
2001 widget->axis_stop_handler = axis_stop_handler;
2002 widget->axis_discrete_handler = axis_discrete_handler;
2003}
2004
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002005static void
2006window_schedule_redraw_task(struct window *window);
2007
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002008void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002009widget_schedule_redraw(struct widget *widget)
2010{
Pekka Paalanen71233882013-04-25 13:57:53 +03002011 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002012 widget->surface->redraw_needed = 1;
2013 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002014}
2015
Neil Roberts97b747c2013-12-19 16:17:12 +00002016void
2017widget_set_use_cairo(struct widget *widget,
2018 int use_cairo)
2019{
2020 widget->use_cairo = use_cairo;
2021}
2022
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002023cairo_surface_t *
2024window_get_surface(struct window *window)
2025{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002026 cairo_surface_t *cairo_surface;
2027
2028 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2029
2030 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002031}
2032
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002033struct wl_surface *
2034window_get_wl_surface(struct window *window)
2035{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002036 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002037}
2038
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002039static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002040tooltip_redraw_handler(struct widget *widget, void *data)
2041{
2042 cairo_t *cr;
2043 const int32_t r = 3;
2044 struct tooltip *tooltip = data;
2045 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002046
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002047 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002048 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002049 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2050 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2051 cairo_paint(cr);
2052
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002053 width = widget->allocation.width;
2054 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002055 rounded_rect(cr, 0, 0, width, height, r);
2056
2057 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2058 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2059 cairo_fill(cr);
2060
2061 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2062 cairo_move_to(cr, 10, 16);
2063 cairo_show_text(cr, tooltip->entry);
2064 cairo_destroy(cr);
2065}
2066
2067static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002068get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002069{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002070 cairo_t *cr;
2071 cairo_text_extents_t extents;
2072
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002073 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002074 * created yet, and parent does not have a valid surface
2075 * outside repaint, either.
2076 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002077 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002078 cairo_text_extents(cr, tooltip->entry, &extents);
2079 cairo_destroy(cr);
2080
2081 return extents;
2082}
2083
2084static int
2085window_create_tooltip(struct tooltip *tooltip)
2086{
2087 struct widget *parent = tooltip->parent;
2088 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002089 const int offset_y = 27;
2090 const int margin = 3;
2091 cairo_text_extents_t extents;
2092
2093 if (tooltip->widget)
2094 return 0;
2095
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002096 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002097
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002098 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002099 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002100 widget_set_allocation(tooltip->widget,
2101 tooltip->x, tooltip->y + offset_y,
2102 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002103
2104 return 0;
2105}
2106
2107void
2108widget_destroy_tooltip(struct widget *parent)
2109{
2110 struct tooltip *tooltip = parent->tooltip;
2111
2112 parent->tooltip_count = 0;
2113 if (!tooltip)
2114 return;
2115
2116 if (tooltip->widget) {
2117 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002118 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002119 }
2120
2121 close(tooltip->tooltip_fd);
2122 free(tooltip->entry);
2123 free(tooltip);
2124 parent->tooltip = NULL;
2125}
2126
2127static void
2128tooltip_func(struct task *task, uint32_t events)
2129{
2130 struct tooltip *tooltip =
2131 container_of(task, struct tooltip, tooltip_task);
2132 uint64_t exp;
2133
Martin Olsson8df662a2012-07-08 03:03:47 +02002134 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2135 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002136 window_create_tooltip(tooltip);
2137}
2138
2139#define TOOLTIP_TIMEOUT 500
2140static int
2141tooltip_timer_reset(struct tooltip *tooltip)
2142{
2143 struct itimerspec its;
2144
2145 its.it_interval.tv_sec = 0;
2146 its.it_interval.tv_nsec = 0;
2147 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2148 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2149 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2150 fprintf(stderr, "could not set timerfd\n: %m");
2151 return -1;
2152 }
2153
2154 return 0;
2155}
2156
2157int
2158widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2159{
2160 struct tooltip *tooltip = parent->tooltip;
2161
2162 parent->tooltip_count++;
2163 if (tooltip) {
2164 tooltip->x = x;
2165 tooltip->y = y;
2166 tooltip_timer_reset(tooltip);
2167 return 0;
2168 }
2169
2170 /* the handler might be triggered too fast via input device motion, so
2171 * we need this check here to make sure tooltip is fully initialized */
2172 if (parent->tooltip_count > 1)
2173 return 0;
2174
2175 tooltip = malloc(sizeof *tooltip);
2176 if (!tooltip)
2177 return -1;
2178
2179 parent->tooltip = tooltip;
2180 tooltip->parent = parent;
2181 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002182 tooltip->x = x;
2183 tooltip->y = y;
2184 tooltip->entry = strdup(entry);
2185 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2186 if (tooltip->tooltip_fd < 0) {
2187 fprintf(stderr, "could not create timerfd\n: %m");
2188 return -1;
2189 }
2190
2191 tooltip->tooltip_task.run = tooltip_func;
2192 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2193 EPOLLIN, &tooltip->tooltip_task);
2194 tooltip_timer_reset(tooltip);
2195
2196 return 0;
2197}
2198
2199static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200frame_resize_handler(struct widget *widget,
2201 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002202{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002204 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002205 struct rectangle interior;
2206 struct rectangle input;
2207 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002208
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002209 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002210 interior.x = 0;
2211 interior.y = 0;
2212 interior.width = width;
2213 interior.height = height;
2214 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002215 frame_resize(frame->frame, width, height);
2216 frame_interior(frame->frame, &interior.x, &interior.y,
2217 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002218 }
2219
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002220 widget_set_allocation(child, interior.x, interior.y,
2221 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002223 if (child->resize_handler) {
2224 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225 child->user_data);
2226
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002227 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002228 width = child->allocation.width;
2229 height = child->allocation.height;
2230 } else {
2231 frame_resize_inside(frame->frame,
2232 child->allocation.width,
2233 child->allocation.height);
2234 width = frame_width(frame->frame);
2235 height = frame_height(frame->frame);
2236 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002237 }
2238
Scott Moreauf7e498c2012-05-14 11:39:29 -06002239 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002240
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002241 widget->surface->input_region =
2242 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002243 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002244 frame_input_rect(frame->frame, &input.x, &input.y,
2245 &input.width, &input.height);
2246 wl_region_add(widget->surface->input_region,
2247 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002248 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002249 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002250 }
2251
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002252 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002253
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002254 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002255 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002256 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2257 &opaque.width, &opaque.height);
2258
2259 wl_region_add(widget->surface->opaque_region,
2260 opaque.x, opaque.y,
2261 opaque.width, opaque.height);
2262 } else {
2263 wl_region_add(widget->surface->opaque_region,
2264 0, 0, width, height);
2265 }
Martin Minarik1998b152012-05-10 02:04:35 +02002266 }
2267
Martin Minarik1998b152012-05-10 02:04:35 +02002268
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002269 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002270}
2271
2272static void
2273frame_redraw_handler(struct widget *widget, void *data)
2274{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002275 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002276 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002277 struct window *window = widget->window;
2278
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002279 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002280 return;
2281
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002282 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002283
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002284 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002285
2286 cairo_destroy(cr);
2287}
2288
2289static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002290frame_get_pointer_image_for_location(struct window_frame *frame,
2291 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002292{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002293 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002294
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002295 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002296 return CURSOR_LEFT_PTR;
2297
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002298 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002299 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002300 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002301 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002302 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002303 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002304 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002305 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002306 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002307 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002308 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002309 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002310 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002311 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002312 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002313 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002314 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002315 case THEME_LOCATION_EXTERIOR:
2316 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002317 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002318 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002319 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002320}
2321
Pekka Paalanen26237862014-09-10 15:10:30 +03002322static void
2323frame_menu_func(void *data, struct input *input, int index)
2324{
2325 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002326
2327 switch (index) {
2328 case 0: /* close */
2329 window_close(window);
2330 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002331 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002332 /* we don't have a way to get out of fullscreen for now */
2333 if (window->fullscreen_handler)
2334 window->fullscreen_handler(window, window->user_data);
2335 break;
2336 }
2337}
2338
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002339void
2340window_show_frame_menu(struct window *window,
2341 struct input *input, uint32_t time)
2342{
2343 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002344 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002345
Pekka Paalanen26237862014-09-10 15:10:30 +03002346 static const char *entries[] = {
2347 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002348 "Fullscreen"
2349 };
2350
2351 if (window->fullscreen_handler)
2352 count = ARRAY_LENGTH(entries);
2353 else
2354 count = ARRAY_LENGTH(entries) - 1;
2355
2356 input_get_position(input, &x, &y);
2357 window_show_menu(window->display, input, time, window,
2358 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002359}
2360
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002361static int
2362frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002363 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002364{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002365 struct window_frame *frame = data;
2366 enum theme_location location;
2367
2368 location = frame_pointer_enter(frame->frame, input, x, y);
2369 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2370 widget_schedule_redraw(frame->widget);
2371
2372 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002373}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002374
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002375static int
2376frame_motion_handler(struct widget *widget,
2377 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002378 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002379{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002380 struct window_frame *frame = data;
2381 enum theme_location location;
2382
2383 location = frame_pointer_motion(frame->frame, input, x, y);
2384 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2385 widget_schedule_redraw(frame->widget);
2386
2387 return frame_get_pointer_image_for_location(data, location);
2388}
2389
2390static void
2391frame_leave_handler(struct widget *widget,
2392 struct input *input, void *data)
2393{
2394 struct window_frame *frame = data;
2395
2396 frame_pointer_leave(frame->frame, input);
2397 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2398 widget_schedule_redraw(frame->widget);
2399}
2400
2401static void
2402frame_handle_status(struct window_frame *frame, struct input *input,
2403 uint32_t time, enum theme_location location)
2404{
2405 struct window *window = frame->widget->window;
2406 uint32_t status;
2407
2408 status = frame_status(frame->frame);
2409 if (status & FRAME_STATUS_REPAINT)
2410 widget_schedule_redraw(frame->widget);
2411
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002412 if (status & FRAME_STATUS_MINIMIZE) {
2413 window_set_minimized(window);
2414 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2415 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002416
2417 if (status & FRAME_STATUS_MENU) {
2418 window_show_frame_menu(window, input, time);
2419 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2420 }
2421
2422 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002423 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002424 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2425 }
2426
2427 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002428 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002429 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002430 }
2431
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002432 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002433 input_ungrab(input);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002434 zxdg_toplevel_v6_move(window->xdg_toplevel,
2435 input_get_seat(input),
2436 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002437
2438 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2439 }
2440
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002441 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002442 input_ungrab(input);
2443
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002444 zxdg_toplevel_v6_resize(window->xdg_toplevel,
2445 input_get_seat(input),
2446 window->display->serial,
2447 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002448
2449 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2450 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002451}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002452
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002453#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002454static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002455frame_button_handler(struct widget *widget,
2456 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002457 uint32_t button, enum wl_pointer_button_state state,
2458 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002459
2460{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002461 struct window_frame *frame = data;
2462 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002463
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002464 frame->double_click = 0;
2465 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2466 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2467 frame->double_click = 1;
2468 frame->did_double = 1;
2469 } else
2470 frame->did_double = 0;
2471
2472 frame->last_time = time;
2473 } else if (frame->did_double == 1) {
2474 frame->double_click = 1;
2475 frame->did_double = 0;
2476 }
2477
2478 if (frame->double_click)
2479 location = frame_double_click(frame->frame, input,
2480 button, state);
2481 else
2482 location = frame_pointer_button(frame->frame, input,
2483 button, state);
2484
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002485 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002486}
2487
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002488static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002489frame_touch_down_handler(struct widget *widget, struct input *input,
2490 uint32_t serial, uint32_t time, int32_t id,
2491 float x, float y, void *data)
2492{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002493 struct window_frame *frame = data;
2494
Xiong Zhang382de462014-06-12 11:06:26 +08002495 frame->double_click = 0;
2496 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2497 frame->last_id == id) {
2498 frame->double_click = 1;
2499 frame->did_double = 1;
2500 frame->double_id = id;
2501 } else
2502 frame->did_double = 0;
2503
2504 frame->last_time = time;
2505 frame->last_id = id;
2506
2507 if (frame->double_click)
2508 frame_double_touch_down(frame->frame, input, id, x, y);
2509 else
2510 frame_touch_down(frame->frame, input, id, x, y);
2511
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002512 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2513}
2514
2515static void
2516frame_touch_up_handler(struct widget *widget,
2517 struct input *input, uint32_t serial, uint32_t time,
2518 int32_t id, void *data)
2519{
2520 struct window_frame *frame = data;
2521
Xiong Zhang382de462014-06-12 11:06:26 +08002522 if (frame->double_id == id && frame->did_double) {
2523 frame->did_double = 0;
2524 frame->double_id = 0;
2525 frame_double_touch_up(frame->frame, input, id);
2526 } else
2527 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002528 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002529}
2530
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002531struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002532window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002533{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002534 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002535 uint32_t buttons;
2536
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002537 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002538 buttons = FRAME_BUTTON_NONE;
2539 } else {
2540 buttons = FRAME_BUTTON_ALL;
2541 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002542
Peter Huttererf3d62272013-08-08 11:57:05 +10002543 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002544 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002545 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002546
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002547 frame->widget = window_add_widget(window, frame);
2548 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002549
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002550 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2551 widget_set_resize_handler(frame->widget, frame_resize_handler);
2552 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002553 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002554 widget_set_motion_handler(frame->widget, frame_motion_handler);
2555 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002556 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002557 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002558
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002559 window->frame = frame;
2560
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002561 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002562}
2563
Kristian Høgsberga1627922012-06-20 17:30:03 -04002564void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002565window_frame_set_child_size(struct widget *widget, int child_width,
2566 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002567{
2568 struct display *display = widget->window->display;
2569 struct theme *t = display->theme;
2570 int decoration_width, decoration_height;
2571 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002572 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002573
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002574 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002575 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002576 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002577 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002578
2579 width = child_width + decoration_width;
2580 height = child_height + decoration_height;
2581 } else {
2582 width = child_width;
2583 height = child_height;
2584 }
2585
2586 window_schedule_resize(widget->window, width, height);
2587}
2588
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002589static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002590window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002591{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002592 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002593
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002594 /* frame->child must be destroyed by the application */
2595 widget_destroy(frame->widget);
2596 free(frame);
2597}
2598
2599static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002600input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002601 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002602{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002603 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002604 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002605
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002606 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002607 return;
2608
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002609 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002610 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002611 widget = old;
2612 if (input->grab)
2613 widget = input->grab;
2614 if (widget->leave_handler)
2615 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002616 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002617 }
2618
2619 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002620 widget = focus;
2621 if (input->grab)
2622 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002623 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002624 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002625 cursor = widget->enter_handler(focus, input, x, y,
2626 widget->user_data);
2627 else
2628 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002629
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002630 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002631 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002632}
2633
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002634void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002635touch_grab(struct input *input, int32_t touch_id)
2636{
2637 input->touch_grab = 1;
2638 input->touch_grab_id = touch_id;
2639}
2640
2641void
2642touch_ungrab(struct input *input)
2643{
2644 struct touch_point *tp, *tmp;
2645
2646 input->touch_grab = 0;
2647
2648 wl_list_for_each_safe(tp, tmp,
2649 &input->touch_point_list, link) {
2650 if (tp->id != input->touch_grab_id)
2651 continue;
2652 wl_list_remove(&tp->link);
2653 free(tp);
2654
2655 return;
2656 }
2657}
2658
2659void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002660input_grab(struct input *input, struct widget *widget, uint32_t button)
2661{
2662 input->grab = widget;
2663 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002664
2665 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002666}
2667
2668void
2669input_ungrab(struct input *input)
2670{
2671 struct widget *widget;
2672
2673 input->grab = NULL;
2674 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002675 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002676 input->sx, input->sy);
2677 input_set_focus_widget(input, widget, input->sx, input->sy);
2678 }
2679}
2680
2681static void
Derek Foreman118a4292015-04-22 17:23:35 -05002682cursor_delay_timer_reset(struct input *input, uint32_t duration)
2683{
2684 struct itimerspec its;
2685
2686 if (!duration)
2687 input->cursor_timer_running = false;
2688 else
2689 input->cursor_timer_running = true;
2690
2691 its.it_interval.tv_sec = 0;
2692 its.it_interval.tv_nsec = 0;
2693 its.it_value.tv_sec = duration / 1000;
2694 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2695 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2696 fprintf(stderr, "could not set cursor timerfd\n: %m");
2697}
2698
2699static void cancel_pointer_image_update(struct input *input)
2700{
2701 if (input->cursor_timer_running)
2702 cursor_delay_timer_reset(input, 0);
2703}
2704
2705static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002706input_remove_pointer_focus(struct input *input)
2707{
2708 struct window *window = input->pointer_focus;
2709
2710 if (!window)
2711 return;
2712
2713 input_set_focus_widget(input, NULL, 0, 0);
2714
2715 input->pointer_focus = NULL;
2716 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002717 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002718}
2719
2720static void
2721pointer_handle_enter(void *data, struct wl_pointer *pointer,
2722 uint32_t serial, struct wl_surface *surface,
2723 wl_fixed_t sx_w, wl_fixed_t sy_w)
2724{
2725 struct input *input = data;
2726 struct window *window;
2727 struct widget *widget;
2728 float sx = wl_fixed_to_double(sx_w);
2729 float sy = wl_fixed_to_double(sy_w);
2730
2731 if (!surface) {
2732 /* enter event for a window we've just destroyed */
2733 return;
2734 }
2735
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002736 window = wl_surface_get_user_data(surface);
2737 if (surface != window->main_surface->surface) {
2738 DBG("Ignoring input event from subsurface %p\n", surface);
2739 return;
2740 }
2741
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002742 input->display->serial = serial;
2743 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002744 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002745
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002746 input->sx = sx;
2747 input->sy = sy;
2748
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002749 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002750 input_set_focus_widget(input, widget, sx, sy);
2751}
2752
2753static void
2754pointer_handle_leave(void *data, struct wl_pointer *pointer,
2755 uint32_t serial, struct wl_surface *surface)
2756{
2757 struct input *input = data;
2758
2759 input->display->serial = serial;
2760 input_remove_pointer_focus(input);
2761}
2762
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002763static void
Daniel Stone37816df2012-05-16 18:45:18 +01002764pointer_handle_motion(void *data, struct wl_pointer *pointer,
2765 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002766{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002767 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002768 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002769 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002770 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002771 float sx = wl_fixed_to_double(sx_w);
2772 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002773
Paul Winwoodb22bf572013-08-29 10:52:54 +01002774 if (!window)
2775 return;
2776
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002777 input->sx = sx;
2778 input->sy = sy;
2779
Rob Bradford5f087742013-07-11 19:41:27 +01002780 /* when making the window smaller - e.g. after a unmaximise we might
2781 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002782 * based on the old surface dimensions. However, if we have an active
2783 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002784 */
Derek Foreman46812b62015-08-26 17:13:27 -05002785 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002786 sy < window->main_surface->allocation.y ||
2787 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002788 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002789 return;
2790
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002791 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002792 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002793 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002794 }
2795
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002796 if (input->grab)
2797 widget = input->grab;
2798 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002799 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002800 if (widget) {
2801 if (widget->motion_handler)
2802 cursor = widget->motion_handler(input->focus_widget,
2803 input, time, sx, sy,
2804 widget->user_data);
2805 else
2806 cursor = widget->default_cursor;
2807 } else
2808 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002809
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002810 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002811}
2812
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002813static void
Daniel Stone37816df2012-05-16 18:45:18 +01002814pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002815 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002816{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002817 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002818 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002819 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002820
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002821 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002822 if (input->focus_widget && input->grab == NULL &&
2823 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002824 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002825
Neil Roberts6b28aad2012-01-23 19:11:18 +00002826 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002827 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002828 (*widget->button_handler)(widget,
2829 input, time,
2830 button, state,
2831 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002832
Daniel Stone4dbadb12012-05-30 16:31:51 +01002833 if (input->grab && input->grab_button == button &&
2834 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002835 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002836}
2837
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002838static void
Daniel Stone37816df2012-05-16 18:45:18 +01002839pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002840 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002841{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002842 struct input *input = data;
2843 struct widget *widget;
2844
2845 widget = input->focus_widget;
2846 if (input->grab)
2847 widget = input->grab;
2848 if (widget && widget->axis_handler)
2849 (*widget->axis_handler)(widget,
2850 input, time,
2851 axis, value,
2852 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002853}
2854
Peter Hutterer87743e92016-01-18 16:38:22 +10002855static void
2856pointer_handle_frame(void *data, struct wl_pointer *pointer)
2857{
2858 struct input *input = data;
2859 struct widget *widget;
2860
2861 widget = input->focus_widget;
2862 if (input->grab)
2863 widget = input->grab;
2864 if (widget && widget->pointer_frame_handler)
2865 (*widget->pointer_frame_handler)(widget,
2866 input,
2867 widget->user_data);
2868}
2869
2870static void
2871pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2872 uint32_t source)
2873{
2874 struct input *input = data;
2875 struct widget *widget;
2876
2877 widget = input->focus_widget;
2878 if (input->grab)
2879 widget = input->grab;
2880 if (widget && widget->axis_source_handler)
2881 (*widget->axis_source_handler)(widget,
2882 input,
2883 source,
2884 widget->user_data);
2885}
2886
2887static void
2888pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2889 uint32_t time, uint32_t axis)
2890{
2891 struct input *input = data;
2892 struct widget *widget;
2893
2894 widget = input->focus_widget;
2895 if (input->grab)
2896 widget = input->grab;
2897 if (widget && widget->axis_stop_handler)
2898 (*widget->axis_stop_handler)(widget,
2899 input, time,
2900 axis,
2901 widget->user_data);
2902}
2903
2904static void
2905pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2906 uint32_t axis, int32_t discrete)
2907{
2908 struct input *input = data;
2909 struct widget *widget;
2910
2911 widget = input->focus_widget;
2912 if (input->grab)
2913 widget = input->grab;
2914 if (widget && widget->axis_discrete_handler)
2915 (*widget->axis_discrete_handler)(widget,
2916 input,
2917 axis,
2918 discrete,
2919 widget->user_data);
2920}
2921
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002922static const struct wl_pointer_listener pointer_listener = {
2923 pointer_handle_enter,
2924 pointer_handle_leave,
2925 pointer_handle_motion,
2926 pointer_handle_button,
2927 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002928 pointer_handle_frame,
2929 pointer_handle_axis_source,
2930 pointer_handle_axis_stop,
2931 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002932};
2933
2934static void
2935input_remove_keyboard_focus(struct input *input)
2936{
2937 struct window *window = input->keyboard_focus;
2938 struct itimerspec its;
2939
2940 its.it_interval.tv_sec = 0;
2941 its.it_interval.tv_nsec = 0;
2942 its.it_value.tv_sec = 0;
2943 its.it_value.tv_nsec = 0;
2944 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2945
2946 if (!window)
2947 return;
2948
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002949 if (window->keyboard_focus_handler)
2950 (*window->keyboard_focus_handler)(window, NULL,
2951 window->user_data);
2952
2953 input->keyboard_focus = NULL;
2954}
2955
2956static void
2957keyboard_repeat_func(struct task *task, uint32_t events)
2958{
2959 struct input *input =
2960 container_of(task, struct input, repeat_task);
2961 struct window *window = input->keyboard_focus;
2962 uint64_t exp;
2963
2964 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2965 /* If we change the timer between the fd becoming
2966 * readable and getting here, there'll be nothing to
2967 * read and we get EAGAIN. */
2968 return;
2969
2970 if (window && window->key_handler) {
2971 (*window->key_handler)(window, input, input->repeat_time,
2972 input->repeat_key, input->repeat_sym,
2973 WL_KEYBOARD_KEY_STATE_PRESSED,
2974 window->user_data);
2975 }
2976}
2977
2978static void
2979keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2980 uint32_t format, int fd, uint32_t size)
2981{
2982 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002983 struct xkb_keymap *keymap;
2984 struct xkb_state *state;
Bryce Harrington894b3ec2016-10-10 15:31:47 -07002985 struct xkb_compose_table *compose_table;
2986 struct xkb_compose_state *compose_state;
2987 char *locale;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002988 char *map_str;
2989
2990 if (!data) {
2991 close(fd);
2992 return;
2993 }
2994
2995 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2996 close(fd);
2997 return;
2998 }
2999
3000 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3001 if (map_str == MAP_FAILED) {
3002 close(fd);
3003 return;
3004 }
3005
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003006 /* Set up XKB keymap */
Ran Benita2e1968f2014-08-19 23:59:51 +03003007 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
3008 map_str,
3009 XKB_KEYMAP_FORMAT_TEXT_V1,
3010 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003011 munmap(map_str, size);
3012 close(fd);
3013
Rui Matos3eccb862013-10-10 19:44:22 +02003014 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003015 fprintf(stderr, "failed to compile keymap\n");
3016 return;
3017 }
3018
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003019 /* Set up XKB state */
Rui Matos3eccb862013-10-10 19:44:22 +02003020 state = xkb_state_new(keymap);
3021 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003022 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003023 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003024 return;
3025 }
3026
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003027 /* Look up the preferred locale, falling back to "C" as default */
3028 if (!(locale = getenv("LC_ALL")))
3029 if (!(locale = getenv("LC_CTYPE")))
3030 if (!(locale = getenv("LANG")))
3031 locale = "C";
3032
3033 /* Set up XKB compose table */
3034 compose_table =
3035 xkb_compose_table_new_from_locale(input->display->xkb_context,
3036 locale,
3037 XKB_COMPOSE_COMPILE_NO_FLAGS);
3038 if (compose_table) {
3039 /* Set up XKB compose state */
3040 compose_state = xkb_compose_state_new(compose_table,
3041 XKB_COMPOSE_STATE_NO_FLAGS);
3042 if (compose_state) {
3043 xkb_compose_state_unref(input->xkb.compose_state);
3044 xkb_compose_table_unref(input->xkb.compose_table);
3045 input->xkb.compose_state = compose_state;
3046 input->xkb.compose_table = compose_table;
3047 } else {
3048 fprintf(stderr, "could not create XKB compose state. "
3049 "Disabiling compose.\n");
3050 xkb_compose_table_unref(compose_table);
3051 compose_table = NULL;
3052 }
3053 } else {
3054 fprintf(stderr, "could not create XKB compose table for locale '%s'. "
3055 "Disabiling compose\n", locale);
3056 }
3057
Rui Matos3eccb862013-10-10 19:44:22 +02003058 xkb_keymap_unref(input->xkb.keymap);
3059 xkb_state_unref(input->xkb.state);
3060 input->xkb.keymap = keymap;
3061 input->xkb.state = state;
3062
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003063 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003064 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003065 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003066 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003067 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003068 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003069}
3070
3071static void
3072keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3073 uint32_t serial, struct wl_surface *surface,
3074 struct wl_array *keys)
3075{
3076 struct input *input = data;
3077 struct window *window;
3078
3079 input->display->serial = serial;
3080 input->keyboard_focus = wl_surface_get_user_data(surface);
3081
3082 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003083 if (window->keyboard_focus_handler)
3084 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003085 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003086}
3087
3088static void
3089keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3090 uint32_t serial, struct wl_surface *surface)
3091{
3092 struct input *input = data;
3093
3094 input->display->serial = serial;
3095 input_remove_keyboard_focus(input);
3096}
3097
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003098/* Translate symbols appropriately if a compose sequence is being entered */
3099static xkb_keysym_t
3100process_key_press(xkb_keysym_t sym, struct input *input)
3101{
3102 if (sym == XKB_KEY_NoSymbol)
3103 return sym;
3104 if (xkb_compose_state_feed(input->xkb.compose_state,
3105 sym) != XKB_COMPOSE_FEED_ACCEPTED)
3106 return sym;
3107
3108 switch (xkb_compose_state_get_status(input->xkb.compose_state)) {
3109 case XKB_COMPOSE_COMPOSING:
3110 return XKB_KEY_NoSymbol;
3111 case XKB_COMPOSE_COMPOSED:
3112 return xkb_compose_state_get_one_sym(input->xkb.compose_state);
3113 case XKB_COMPOSE_CANCELLED:
3114 return XKB_KEY_NoSymbol;
3115 case XKB_COMPOSE_NOTHING:
3116 return sym;
3117 default:
3118 return sym;
3119 }
3120}
3121
Scott Moreau210d0792012-03-22 10:47:01 -06003122static void
Daniel Stone37816df2012-05-16 18:45:18 +01003123keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003124 uint32_t serial, uint32_t time, uint32_t key,
3125 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003126{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003127 struct input *input = data;
3128 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003129 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003130 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003131 const xkb_keysym_t *syms;
3132 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003133 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003134
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003135 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003136 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003137 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003138 return;
3139
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003140 /* We only use input grabs for pointer events for now, so just
3141 * ignore key presses if a grab is active. We expand the key
3142 * event delivery mechanism to route events to widgets to
3143 * properly handle key grabs. In the meantime, this prevents
3144 * key event devlivery while a grab is active. */
3145 if (input->grab && input->grab_button == 0)
3146 return;
3147
Ran Benita2e1968f2014-08-19 23:59:51 +03003148 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003149
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003150 sym = XKB_KEY_NoSymbol;
3151 if (num_syms == 1)
3152 sym = syms[0];
3153
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003154
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003155 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003156 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003157 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003158 } else if (sym == XKB_KEY_F11 &&
3159 window->fullscreen_handler &&
3160 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3161 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003162 } else if (sym == XKB_KEY_F4 &&
3163 input->modifiers == MOD_ALT_MASK &&
3164 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003165 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003166 } else if (window->key_handler) {
Bryce Harrington894b3ec2016-10-10 15:31:47 -07003167 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3168 sym = process_key_press(sym, input);
3169
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003170 (*window->key_handler)(window, input, time, key,
3171 sym, state, window->user_data);
3172 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003173
3174 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3175 key == input->repeat_key) {
3176 its.it_interval.tv_sec = 0;
3177 its.it_interval.tv_nsec = 0;
3178 its.it_value.tv_sec = 0;
3179 its.it_value.tv_nsec = 0;
3180 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003181 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3182 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003183 input->repeat_sym = sym;
3184 input->repeat_key = key;
3185 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003186 its.it_interval.tv_sec = input->repeat_rate_sec;
3187 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3188 its.it_value.tv_sec = input->repeat_delay_sec;
3189 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003190 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3191 }
3192}
3193
3194static void
Daniel Stone351eb612012-05-31 15:27:47 -04003195keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3196 uint32_t serial, uint32_t mods_depressed,
3197 uint32_t mods_latched, uint32_t mods_locked,
3198 uint32_t group)
3199{
3200 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003201 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003202
Matt Ropere61561f2013-06-24 16:52:43 +01003203 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3204 if (!input->xkb.keymap)
3205 return;
3206
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003207 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3208 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003209 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003210 XKB_STATE_MODS_DEPRESSED |
3211 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003212 input->modifiers = 0;
3213 if (mask & input->xkb.control_mask)
3214 input->modifiers |= MOD_CONTROL_MASK;
3215 if (mask & input->xkb.alt_mask)
3216 input->modifiers |= MOD_ALT_MASK;
3217 if (mask & input->xkb.shift_mask)
3218 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003219}
3220
Jonny Lamb06959082014-08-12 14:58:27 +02003221static void
3222set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3223{
3224 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3225 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3226
3227 /* a rate of zero disables any repeating, regardless of the delay's
3228 * value */
3229 if (rate == 0)
3230 return;
3231
3232 if (rate == 1)
3233 input->repeat_rate_sec = 1;
3234 else
3235 input->repeat_rate_nsec = 1000000000 / rate;
3236
3237 input->repeat_delay_sec = delay / 1000;
3238 delay -= (input->repeat_delay_sec * 1000);
3239 input->repeat_delay_nsec = delay * 1000 * 1000;
3240}
3241
3242static void
3243keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3244 int32_t rate, int32_t delay)
3245{
3246 struct input *input = data;
3247
3248 set_repeat_info(input, rate, delay);
3249}
3250
Daniel Stone37816df2012-05-16 18:45:18 +01003251static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003252 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003253 keyboard_handle_enter,
3254 keyboard_handle_leave,
3255 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003256 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003257 keyboard_handle_repeat_info
3258
Daniel Stone37816df2012-05-16 18:45:18 +01003259};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003260
3261static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003262touch_handle_down(void *data, struct wl_touch *wl_touch,
3263 uint32_t serial, uint32_t time, struct wl_surface *surface,
3264 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3265{
3266 struct input *input = data;
3267 struct widget *widget;
3268 float sx = wl_fixed_to_double(x_w);
3269 float sy = wl_fixed_to_double(y_w);
3270
Rusty Lynch1084da52013-08-15 09:10:08 -07003271 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003272 input->touch_focus = wl_surface_get_user_data(surface);
3273 if (!input->touch_focus) {
3274 DBG("Failed to find to touch focus for surface %p\n", surface);
3275 return;
3276 }
3277
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003278 if (surface != input->touch_focus->main_surface->surface) {
3279 DBG("Ignoring input event from subsurface %p\n", surface);
3280 input->touch_focus = NULL;
3281 return;
3282 }
3283
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003284 if (input->grab)
3285 widget = input->grab;
3286 else
3287 widget = window_find_widget(input->touch_focus,
3288 wl_fixed_to_double(x_w),
3289 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003290 if (widget) {
3291 struct touch_point *tp = xmalloc(sizeof *tp);
3292 if (tp) {
3293 tp->id = id;
3294 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003295 tp->x = sx;
3296 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003297 wl_list_insert(&input->touch_point_list, &tp->link);
3298
3299 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003300 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003301 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003302 sx, sy,
3303 widget->user_data);
3304 }
3305 }
3306}
3307
3308static void
3309touch_handle_up(void *data, struct wl_touch *wl_touch,
3310 uint32_t serial, uint32_t time, int32_t id)
3311{
3312 struct input *input = data;
3313 struct touch_point *tp, *tmp;
3314
Rusty Lynch041815a2013-08-08 21:20:38 -07003315 if (!input->touch_focus) {
3316 DBG("No touch focus found for touch up event!\n");
3317 return;
3318 }
3319
3320 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3321 if (tp->id != id)
3322 continue;
3323
3324 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003325 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003326 time, id,
3327 tp->widget->user_data);
3328
3329 wl_list_remove(&tp->link);
3330 free(tp);
3331
3332 return;
3333 }
3334}
3335
3336static void
3337touch_handle_motion(void *data, struct wl_touch *wl_touch,
3338 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3339{
3340 struct input *input = data;
3341 struct touch_point *tp;
3342 float sx = wl_fixed_to_double(x_w);
3343 float sy = wl_fixed_to_double(y_w);
3344
3345 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3346
3347 if (!input->touch_focus) {
3348 DBG("No touch focus found for touch motion event!\n");
3349 return;
3350 }
3351
3352 wl_list_for_each(tp, &input->touch_point_list, link) {
3353 if (tp->id != id)
3354 continue;
3355
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003356 tp->x = sx;
3357 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003358 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003359 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003360 id, sx, sy,
3361 tp->widget->user_data);
3362 return;
3363 }
3364}
3365
3366static void
3367touch_handle_frame(void *data, struct wl_touch *wl_touch)
3368{
3369 struct input *input = data;
3370 struct touch_point *tp, *tmp;
3371
3372 DBG("touch_handle_frame\n");
3373
3374 if (!input->touch_focus) {
3375 DBG("No touch focus found for touch frame event!\n");
3376 return;
3377 }
3378
3379 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3380 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003381 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003382 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003383 }
3384}
3385
3386static void
3387touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3388{
3389 struct input *input = data;
3390 struct touch_point *tp, *tmp;
3391
3392 DBG("touch_handle_cancel\n");
3393
3394 if (!input->touch_focus) {
3395 DBG("No touch focus found for touch cancel event!\n");
3396 return;
3397 }
3398
3399 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3400 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003401 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3402 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003403
3404 wl_list_remove(&tp->link);
3405 free(tp);
3406 }
3407}
3408
3409static const struct wl_touch_listener touch_listener = {
3410 touch_handle_down,
3411 touch_handle_up,
3412 touch_handle_motion,
3413 touch_handle_frame,
3414 touch_handle_cancel,
3415};
3416
3417static void
Daniel Stone37816df2012-05-16 18:45:18 +01003418seat_handle_capabilities(void *data, struct wl_seat *seat,
3419 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003420{
Daniel Stone37816df2012-05-16 18:45:18 +01003421 struct input *input = data;
3422
3423 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3424 input->pointer = wl_seat_get_pointer(seat);
3425 wl_pointer_set_user_data(input->pointer, input);
3426 wl_pointer_add_listener(input->pointer, &pointer_listener,
3427 input);
3428 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003429 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003430 wl_pointer_release(input->pointer);
3431 else
3432 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003433 input->pointer = NULL;
3434 }
3435
3436 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3437 input->keyboard = wl_seat_get_keyboard(seat);
3438 wl_keyboard_set_user_data(input->keyboard, input);
3439 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3440 input);
3441 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003442 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003443 wl_keyboard_release(input->keyboard);
3444 else
3445 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003446 input->keyboard = NULL;
3447 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003448
3449 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3450 input->touch = wl_seat_get_touch(seat);
3451 wl_touch_set_user_data(input->touch, input);
3452 wl_touch_add_listener(input->touch, &touch_listener, input);
3453 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003454 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003455 wl_touch_release(input->touch);
3456 else
3457 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003458 input->touch = NULL;
3459 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003460}
3461
Rob Bradford08031182013-08-13 20:11:03 +01003462static void
3463seat_handle_name(void *data, struct wl_seat *seat,
3464 const char *name)
3465{
3466
3467}
3468
Daniel Stone37816df2012-05-16 18:45:18 +01003469static const struct wl_seat_listener seat_listener = {
3470 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003471 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003472};
3473
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003474void
3475input_get_position(struct input *input, int32_t *x, int32_t *y)
3476{
3477 *x = input->sx;
3478 *y = input->sy;
3479}
3480
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003481int
3482input_get_touch(struct input *input, int32_t id, float *x, float *y)
3483{
3484 struct touch_point *tp;
3485
3486 wl_list_for_each(tp, &input->touch_point_list, link) {
3487 if (tp->id != id)
3488 continue;
3489
3490 *x = tp->x;
3491 *y = tp->y;
3492 return 0;
3493 }
3494
3495 return -1;
3496}
3497
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003498struct display *
3499input_get_display(struct input *input)
3500{
3501 return input->display;
3502}
3503
Daniel Stone37816df2012-05-16 18:45:18 +01003504struct wl_seat *
3505input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003506{
Daniel Stone37816df2012-05-16 18:45:18 +01003507 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003508}
3509
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003510uint32_t
3511input_get_modifiers(struct input *input)
3512{
3513 return input->modifiers;
3514}
3515
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003516struct widget *
3517input_get_focus_widget(struct input *input)
3518{
3519 return input->focus_widget;
3520}
3521
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003522struct data_offer {
3523 struct wl_data_offer *offer;
3524 struct input *input;
3525 struct wl_array types;
3526 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003527
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003528 struct task io_task;
3529 int fd;
3530 data_func_t func;
3531 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003532 uint32_t dnd_action;
3533 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003534 void *user_data;
3535};
3536
3537static void
3538data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3539{
3540 struct data_offer *offer = data;
3541 char **p;
3542
3543 p = wl_array_add(&offer->types, sizeof *p);
3544 *p = strdup(type);
3545}
3546
Carlos Garnacho9c931792016-01-18 23:52:12 +01003547static void
3548data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3549{
3550 struct data_offer *offer = data;
3551
3552 offer->source_actions = source_actions;
3553}
3554
3555static void
3556data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3557{
3558 struct data_offer *offer = data;
3559
3560 offer->dnd_action = dnd_action;
3561}
3562
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003563static const struct wl_data_offer_listener data_offer_listener = {
3564 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003565 data_offer_source_actions,
3566 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003567};
3568
3569static void
3570data_offer_destroy(struct data_offer *offer)
3571{
3572 char **p;
3573
3574 offer->refcount--;
3575 if (offer->refcount == 0) {
3576 wl_data_offer_destroy(offer->offer);
3577 for (p = offer->types.data; *p; p++)
3578 free(*p);
3579 wl_array_release(&offer->types);
3580 free(offer);
3581 }
3582}
3583
3584static void
3585data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003586 struct wl_data_device *data_device,
3587 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003588{
3589 struct data_offer *offer;
3590
Brian Lovinbc919262013-08-07 15:34:59 -07003591 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003592
3593 wl_array_init(&offer->types);
3594 offer->refcount = 1;
3595 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003596 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003597 wl_data_offer_add_listener(offer->offer,
3598 &data_offer_listener, offer);
3599}
3600
3601static void
3602data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003603 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003604 wl_fixed_t x_w, wl_fixed_t y_w,
3605 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003606{
3607 struct input *input = data;
3608 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003609 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003610 float x = wl_fixed_to_double(x_w);
3611 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003612 char **p;
3613
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003614 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003615 input->drag_enter_serial = serial;
3616 input->drag_focus = window,
3617 input->drag_x = x;
3618 input->drag_y = y;
3619
3620 if (!input->touch_grab)
3621 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003622
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003623 if (offer) {
3624 input->drag_offer = wl_data_offer_get_user_data(offer);
3625
3626 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3627 *p = NULL;
3628
3629 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003630
3631 if (input->display->data_device_manager_version >=
3632 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3633 wl_data_offer_set_actions(offer,
3634 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3635 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3636 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3637 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003638 } else {
3639 input->drag_offer = NULL;
3640 types_data = NULL;
3641 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003642
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003643 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003644 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003645 window->user_data);
3646}
3647
3648static void
3649data_device_leave(void *data, struct wl_data_device *data_device)
3650{
3651 struct input *input = data;
3652
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003653 if (input->drag_offer) {
3654 data_offer_destroy(input->drag_offer);
3655 input->drag_offer = NULL;
3656 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003657}
3658
3659static void
3660data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003661 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003662{
3663 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003664 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003665 float x = wl_fixed_to_double(x_w);
3666 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003667 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003668
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003669 input->drag_x = x;
3670 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003671
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003672 if (input->drag_offer)
3673 types_data = input->drag_offer->types.data;
3674 else
3675 types_data = NULL;
3676
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003677 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003678 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003679 window->user_data);
3680}
3681
3682static void
3683data_device_drop(void *data, struct wl_data_device *data_device)
3684{
3685 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003686 struct window *window = input->drag_focus;
3687 float x, y;
3688
3689 x = input->drag_x;
3690 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003691
3692 if (window->drop_handler)
3693 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003694 x, y, window->user_data);
3695
3696 if (input->touch_grab)
3697 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003698}
3699
3700static void
3701data_device_selection(void *data,
3702 struct wl_data_device *wl_data_device,
3703 struct wl_data_offer *offer)
3704{
3705 struct input *input = data;
3706 char **p;
3707
3708 if (input->selection_offer)
3709 data_offer_destroy(input->selection_offer);
3710
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003711 if (offer) {
3712 input->selection_offer = wl_data_offer_get_user_data(offer);
3713 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3714 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003715 } else {
3716 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003717 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003718}
3719
3720static const struct wl_data_device_listener data_device_listener = {
3721 data_device_data_offer,
3722 data_device_enter,
3723 data_device_leave,
3724 data_device_motion,
3725 data_device_drop,
3726 data_device_selection
3727};
3728
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003729static void
3730input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003731{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003732 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003733 struct wl_cursor *cursor;
3734 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003735
Daniel Stone80972742012-11-07 17:51:39 +11003736 if (!input->pointer)
3737 return;
3738
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003739 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003740 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003741 return;
3742
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003743 if (index >= (int) cursor->image_count) {
3744 fprintf(stderr, "cursor index out of range\n");
3745 return;
3746 }
3747
3748 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003749 buffer = wl_cursor_image_get_buffer(image);
3750 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003751 return;
3752
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003753 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3754 wl_surface_damage(input->pointer_surface, 0, 0,
3755 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003756 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003757 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3758 input->pointer_surface,
3759 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003760}
3761
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003762static const struct wl_callback_listener pointer_surface_listener;
3763
Derek Foreman493d9792015-03-04 16:26:25 -06003764static bool
3765input_set_pointer_special(struct input *input)
3766{
3767 if (input->current_cursor == CURSOR_BLANK) {
3768 wl_pointer_set_cursor(input->pointer,
3769 input->pointer_enter_serial,
3770 NULL, 0, 0);
3771 return true;
3772 }
3773
3774 if (input->current_cursor == CURSOR_UNSET)
3775 return true;
3776
3777 return false;
3778}
3779
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003780static void
Derek Foreman118a4292015-04-22 17:23:35 -05003781schedule_pointer_image_update(struct input *input,
3782 struct wl_cursor *cursor,
3783 uint32_t duration,
3784 bool force_frame)
3785{
3786 /* Some silly cursor sets have enormous pauses in them. In these
3787 * cases it's better to use a timer even if it results in less
3788 * accurate presentation, since it will save us having to set the
3789 * same cursor image over and over again.
3790 *
3791 * This is really not the way we're supposed to time any kind of
3792 * animation, but we're pretending it's OK here because we don't
3793 * want animated cursors with long delays to needlessly hog CPU.
3794 *
3795 * We use force_frame to ensure we don't accumulate large timing
3796 * errors by running off the wrong clock.
3797 */
3798 if (!force_frame && duration > 100) {
3799 struct timespec tp;
3800
3801 clock_gettime(CLOCK_MONOTONIC, &tp);
3802 input->cursor_timer_start = tp.tv_sec * 1000
3803 + tp.tv_nsec / 1000000;
3804 cursor_delay_timer_reset(input, duration);
3805 return;
3806 }
3807
3808 /* for short durations we'll just spin on frame callbacks for
3809 * accurate timing - the way any kind of timing sensitive animation
3810 * should really be done. */
3811 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3812 wl_callback_add_listener(input->cursor_frame_cb,
3813 &pointer_surface_listener, input);
3814
3815}
3816
3817static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003818pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3819 uint32_t time)
3820{
3821 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003822 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003823 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003824 uint32_t duration;
3825 bool force_frame = true;
3826
3827 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003828
3829 if (callback) {
3830 assert(callback == input->cursor_frame_cb);
3831 wl_callback_destroy(callback);
3832 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003833 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003834 }
3835
Daniel Stone80972742012-11-07 17:51:39 +11003836 if (!input->pointer)
3837 return;
3838
Derek Foreman493d9792015-03-04 16:26:25 -06003839 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003840 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003841
Daniel Stonea494f1d2012-06-18 19:31:12 +01003842 cursor = input->display->cursors[input->current_cursor];
3843 if (!cursor)
3844 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003845
3846 /* FIXME We don't have the current time on the first call so we set
3847 * the animation start to the time of the first frame callback. */
3848 if (time == 0)
3849 input->cursor_anim_start = 0;
3850 else if (input->cursor_anim_start == 0)
3851 input->cursor_anim_start = time;
3852
Derek Foreman118a4292015-04-22 17:23:35 -05003853 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003854
Derek Foreman118a4292015-04-22 17:23:35 -05003855 if (time == 0 || input->cursor_anim_start == 0) {
3856 duration = 0;
3857 i = 0;
3858 } else
3859 i = wl_cursor_frame_and_duration(
3860 cursor,
3861 time - input->cursor_anim_start,
3862 &duration);
3863
3864 if (cursor->image_count > 1)
3865 schedule_pointer_image_update(input, cursor, duration,
3866 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003867
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003868 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003869}
3870
Derek Foreman118a4292015-04-22 17:23:35 -05003871static void
3872cursor_timer_func(struct task *task, uint32_t events)
3873{
3874 struct input *input = container_of(task, struct input, cursor_task);
3875 struct timespec tp;
3876 struct wl_cursor *cursor;
3877 uint32_t time;
3878 uint64_t exp;
3879
3880 if (!input->cursor_timer_running)
3881 return;
3882
3883 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3884 return;
3885
3886 cursor = input->display->cursors[input->current_cursor];
3887 if (!cursor)
3888 return;
3889
3890 clock_gettime(CLOCK_MONOTONIC, &tp);
3891 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3892 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3893}
3894
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003895static const struct wl_callback_listener pointer_surface_listener = {
3896 pointer_surface_frame_callback
3897};
3898
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003899void
3900input_set_pointer_image(struct input *input, int pointer)
3901{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003902 int force = 0;
3903
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003904 if (!input->pointer)
3905 return;
3906
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003907 if (input->pointer_enter_serial > input->cursor_serial)
3908 force = 1;
3909
3910 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003911 return;
3912
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003913 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003914 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003915 if (!input->cursor_frame_cb)
3916 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003917 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003918 /* The current frame callback may be stuck if, for instance,
3919 * the set cursor request was processed by the server after
3920 * this client lost the focus. In this case the cursor surface
3921 * might not be mapped and the frame callback wouldn't ever
3922 * complete. Send a set_cursor and attach to try to map the
3923 * cursor surface again so that the callback will finish */
3924 input_set_pointer_image_index(input, 0);
3925 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003926}
3927
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003928struct wl_data_device *
3929input_get_data_device(struct input *input)
3930{
3931 return input->data_device;
3932}
3933
3934void
3935input_set_selection(struct input *input,
3936 struct wl_data_source *source, uint32_t time)
3937{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003938 if (input->data_device)
3939 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003940}
3941
3942void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003943input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003944{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003945 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003946 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003947}
3948
3949static void
3950offer_io_func(struct task *task, uint32_t events)
3951{
3952 struct data_offer *offer =
3953 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003954 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003955 unsigned int len;
3956 char buffer[4096];
3957
3958 len = read(offer->fd, buffer, sizeof buffer);
3959 offer->func(buffer, len,
3960 offer->x, offer->y, offer->user_data);
3961
3962 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003963 if (display->data_device_manager_version >=
3964 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3965 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003966 close(offer->fd);
3967 data_offer_destroy(offer);
3968 }
3969}
3970
3971static void
3972data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3973 data_func_t func, void *user_data)
3974{
3975 int p[2];
3976
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003977 if (pipe2(p, O_CLOEXEC) == -1)
3978 return;
3979
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003980 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3981 close(p[1]);
3982
3983 offer->io_task.run = offer_io_func;
3984 offer->fd = p[0];
3985 offer->func = func;
3986 offer->refcount++;
3987 offer->user_data = user_data;
3988
3989 display_watch_fd(offer->input->display,
3990 offer->fd, EPOLLIN, &offer->io_task);
3991}
3992
3993void
3994input_receive_drag_data(struct input *input, const char *mime_type,
3995 data_func_t func, void *data)
3996{
3997 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003998 input->drag_offer->x = input->drag_x;
3999 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004000}
4001
4002int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07004003input_receive_drag_data_to_fd(struct input *input,
4004 const char *mime_type, int fd)
4005{
4006 if (input->drag_offer)
4007 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
4008
4009 return 0;
4010}
4011
4012int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004013input_receive_selection_data(struct input *input, const char *mime_type,
4014 data_func_t func, void *data)
4015{
4016 char **p;
4017
4018 if (input->selection_offer == NULL)
4019 return -1;
4020
4021 for (p = input->selection_offer->types.data; *p; p++)
4022 if (strcmp(mime_type, *p) == 0)
4023 break;
4024
4025 if (*p == NULL)
4026 return -1;
4027
4028 data_offer_receive_data(input->selection_offer,
4029 mime_type, func, data);
4030 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004031}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004032
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004033int
4034input_receive_selection_data_to_fd(struct input *input,
4035 const char *mime_type, int fd)
4036{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04004037 if (input->selection_offer)
4038 wl_data_offer_receive(input->selection_offer->offer,
4039 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05004040
4041 return 0;
4042}
4043
Kristian Høgsberg41da9082010-11-30 14:01:07 -05004044void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004045window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004046{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004047 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004048 return;
4049
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004050 zxdg_toplevel_v6_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05004051}
4052
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004053static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03004054surface_set_synchronized(struct surface *surface)
4055{
4056 if (!surface->subsurface)
4057 return;
4058
4059 if (surface->synchronized)
4060 return;
4061
4062 wl_subsurface_set_sync(surface->subsurface);
4063 surface->synchronized = 1;
4064}
4065
4066static void
4067surface_set_synchronized_default(struct surface *surface)
4068{
4069 if (!surface->subsurface)
4070 return;
4071
4072 if (surface->synchronized == surface->synchronized_default)
4073 return;
4074
4075 if (surface->synchronized_default)
4076 wl_subsurface_set_sync(surface->subsurface);
4077 else
4078 wl_subsurface_set_desync(surface->subsurface);
4079
4080 surface->synchronized = surface->synchronized_default;
4081}
4082
4083static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004084surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004085{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004086 struct widget *widget = surface->widget;
4087 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004088
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004089 if (surface->input_region) {
4090 wl_region_destroy(surface->input_region);
4091 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004092 }
4093
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004094 if (surface->opaque_region)
4095 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004096
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004097 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004098
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004099 if (widget->resize_handler)
4100 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004101 widget->allocation.width,
4102 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004103 widget->user_data);
4104
Pekka Paalanen35e82632013-04-25 13:57:48 +03004105 if (surface->subsurface &&
4106 (surface->allocation.x != widget->allocation.x ||
4107 surface->allocation.y != widget->allocation.y)) {
4108 wl_subsurface_set_position(surface->subsurface,
4109 widget->allocation.x,
4110 widget->allocation.y);
4111 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004112 if (surface->allocation.width != widget->allocation.width ||
4113 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004114 window_schedule_redraw(widget->window);
4115 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004116 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004117
4118 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004119 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004120 widget->allocation.width,
4121 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004122}
4123
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004124static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004125window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004126{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004127 struct surface *surface;
4128
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004129 widget_set_allocation(window->main_surface->widget,
4130 window->pending_allocation.x,
4131 window->pending_allocation.y,
4132 window->pending_allocation.width,
4133 window->pending_allocation.height);
4134
4135 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004136
4137 /* The main surface is in the list, too. Main surface's
4138 * resize_handler is responsible for calling widget_set_allocation()
4139 * on all sub-surface root widgets, so they will be resized
4140 * properly.
4141 */
4142 wl_list_for_each(surface, &window->subsurface_list, link) {
4143 if (surface == window->main_surface)
4144 continue;
4145
4146 surface_set_synchronized(surface);
4147 surface_resize(surface);
4148 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004149
4150 if (!window->fullscreen && !window->maximized)
4151 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004152
4153 if (window->confined && window->confined_widget) {
4154 struct wl_compositor *compositor = window->display->compositor;
4155 struct wl_region *region;
4156 struct widget *widget = window->confined_widget;
4157
4158 region = wl_compositor_create_region(compositor);
4159 wl_region_add(region,
4160 widget->allocation.x,
4161 widget->allocation.y,
4162 widget->allocation.width,
4163 widget->allocation.height);
4164 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4165 region);
4166 wl_region_destroy(region);
4167 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004168}
4169
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004170static void
4171idle_resize(struct window *window)
4172{
4173 window->resize_needed = 0;
4174 window->redraw_needed = 1;
4175
4176 DBG("from %dx%d to %dx%d\n",
4177 window->main_surface->server_allocation.width,
4178 window->main_surface->server_allocation.height,
4179 window->pending_allocation.width,
4180 window->pending_allocation.height);
4181
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004182 window_do_resize(window);
4183}
4184
4185static void
4186undo_resize(struct window *window)
4187{
4188 window->pending_allocation.width =
4189 window->main_surface->server_allocation.width;
4190 window->pending_allocation.height =
4191 window->main_surface->server_allocation.height;
4192
4193 DBG("back to %dx%d\n",
4194 window->main_surface->server_allocation.width,
4195 window->main_surface->server_allocation.height);
4196
4197 window_do_resize(window);
4198
4199 if (window->pending_allocation.width == 0 &&
4200 window->pending_allocation.height == 0) {
4201 fprintf(stderr, "Error: Could not draw a surface, "
4202 "most likely due to insufficient disk space in "
4203 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4204 exit(EXIT_FAILURE);
4205 }
4206}
4207
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004208void
4209window_schedule_resize(struct window *window, int width, int height)
4210{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004211 /* We should probably get these numbers from the theme. */
4212 const int min_width = 200, min_height = 200;
4213
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004214 window->pending_allocation.x = 0;
4215 window->pending_allocation.y = 0;
4216 window->pending_allocation.width = width;
4217 window->pending_allocation.height = height;
4218
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004219 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004220 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004221 window->min_allocation.width = min_width;
4222 else
4223 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004224 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004225 window->min_allocation.height = min_height;
4226 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004227 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004228 }
4229
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004230 if (window->pending_allocation.width < window->min_allocation.width)
4231 window->pending_allocation.width = window->min_allocation.width;
4232 if (window->pending_allocation.height < window->min_allocation.height)
4233 window->pending_allocation.height = window->min_allocation.height;
4234
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004235 window->resize_needed = 1;
4236 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004237}
4238
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004239void
4240widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4241{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004242 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004243}
4244
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004245static int
4246window_get_shadow_margin(struct window *window)
4247{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004248 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004249 return frame_get_shadow_margin(window->frame->frame);
4250 else
4251 return 0;
4252}
4253
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004254static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004255window_inhibit_redraw(struct window *window)
4256{
4257 window->redraw_inhibited = 1;
4258 wl_list_remove(&window->redraw_task.link);
4259 wl_list_init(&window->redraw_task.link);
4260 window->redraw_task_scheduled = 0;
4261}
4262
4263static void
4264window_uninhibit_redraw(struct window *window)
4265{
4266 window->redraw_inhibited = 0;
4267 if (window->redraw_needed || window->resize_needed)
4268 window_schedule_redraw_task(window);
4269}
4270
4271static void
4272xdg_surface_handle_configure(void *data,
4273 struct zxdg_surface_v6 *zxdg_surface_v6,
4274 uint32_t serial)
4275{
4276 struct window *window = data;
4277
4278 zxdg_surface_v6_ack_configure(window->xdg_surface, serial);
4279
4280 if (window->state_changed_handler)
4281 window->state_changed_handler(window, window->user_data);
4282
4283 window_uninhibit_redraw(window);
4284}
4285
4286static const struct zxdg_surface_v6_listener xdg_surface_listener = {
4287 xdg_surface_handle_configure
4288};
4289
4290static void
4291xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
4292 int32_t width, int32_t height,
4293 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004294{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004295 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004296 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004297
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004298 window->maximized = 0;
4299 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004300 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004301 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004302
4303 wl_array_for_each(p, states) {
4304 uint32_t state = *p;
4305 switch (state) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004306 case ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004307 window->maximized = 1;
4308 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004309 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004310 window->fullscreen = 1;
4311 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004312 case ZXDG_TOPLEVEL_V6_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004313 window->resizing = 1;
4314 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004315 case ZXDG_TOPLEVEL_V6_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004316 window->focused = 1;
4317 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004318 default:
4319 /* Unknown state */
4320 break;
4321 }
4322 }
4323
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004324 if (window->frame) {
4325 if (window->maximized) {
4326 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4327 } else {
4328 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4329 }
4330
4331 if (window->focused) {
4332 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4333 } else {
4334 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4335 }
4336 }
4337
Jasper St. Pierref184c382014-05-06 08:33:27 -04004338 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004339 /* The width / height params are for window geometry,
4340 * but window_schedule_resize takes allocation. Add
4341 * on the shadow margin to get the difference. */
4342 int margin = window_get_shadow_margin(window);
4343
4344 window_schedule_resize(window,
4345 width + margin * 2,
4346 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004347 } else if (window->saved_allocation.width > 0 &&
4348 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004349 window_schedule_resize(window,
4350 window->saved_allocation.width,
4351 window->saved_allocation.height);
4352 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004353}
4354
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004355static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004356xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004357{
4358 struct window *window = data;
4359 window_close(window);
4360}
4361
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004362static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
4363 xdg_toplevel_handle_configure,
4364 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004365};
4366
4367static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004368window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004369{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004370 struct zxdg_toplevel_v6 *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004371
4372 if (!window->xdg_surface)
4373 return;
4374
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004375 if (window->parent == window->last_parent)
4376 return;
4377
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004378 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004379 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004380 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004381 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004382
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004383 zxdg_toplevel_v6_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004384 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004385}
4386
4387static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004388window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004389{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004390 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004391 frame_input_rect(window->frame->frame,
4392 &geometry->x,
4393 &geometry->y,
4394 &geometry->width,
4395 &geometry->height);
4396 else
4397 window_get_allocation(window, geometry);
4398}
4399
4400static void
4401window_sync_geometry(struct window *window)
4402{
4403 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004404
4405 if (!window->xdg_surface)
4406 return;
4407
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004408 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004409 if (geometry.x == window->last_geometry.x &&
4410 geometry.y == window->last_geometry.y &&
4411 geometry.width == window->last_geometry.width &&
4412 geometry.height == window->last_geometry.height)
4413 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004414
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004415 zxdg_surface_v6_set_window_geometry(window->xdg_surface,
4416 geometry.x,
4417 geometry.y,
4418 geometry.width,
4419 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004420 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004421}
4422
4423static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004424window_flush(struct window *window)
4425{
4426 struct surface *surface;
4427
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004428 assert(!window->redraw_inhibited);
4429
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004430 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004431 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004432 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004433 if (window->xdg_toplevel)
4434 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004435 }
4436
4437 wl_list_for_each(surface, &window->subsurface_list, link) {
4438 if (surface == window->main_surface)
4439 continue;
4440
4441 surface_flush(surface);
4442 }
4443
4444 surface_flush(window->main_surface);
4445}
4446
4447static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004448menu_destroy(struct menu *menu)
4449{
4450 widget_destroy(menu->widget);
4451 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004452 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004453 free(menu);
4454}
4455
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004456void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004457window_get_allocation(struct window *window,
4458 struct rectangle *allocation)
4459{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004460 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004461}
4462
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004463static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004464widget_redraw(struct widget *widget)
4465{
4466 struct widget *child;
4467
4468 if (widget->redraw_handler)
4469 widget->redraw_handler(widget, widget->user_data);
4470 wl_list_for_each(child, &widget->child_list, link)
4471 widget_redraw(child);
4472}
4473
4474static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004475frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4476{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004477 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004478
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004479 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004480 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004481 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004482 surface->frame_cb = NULL;
4483
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004484 surface->last_time = time;
4485
Pekka Paalanen71233882013-04-25 13:57:53 +03004486 if (surface->redraw_needed || surface->window->redraw_needed) {
4487 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004488 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004489 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004490}
4491
4492static const struct wl_callback_listener listener = {
4493 frame_callback
4494};
4495
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004496static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004497surface_redraw(struct surface *surface)
4498{
Pekka Paalanen71233882013-04-25 13:57:53 +03004499 DBG_OBJ(surface->surface, "begin\n");
4500
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004501 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004502 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004503
4504 /* Whole-window redraw forces a redraw even if the previous has
4505 * not yet hit the screen.
4506 */
4507 if (surface->frame_cb) {
4508 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004509 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004510
Pekka Paalanen71233882013-04-25 13:57:53 +03004511 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004512 wl_callback_destroy(surface->frame_cb);
4513 }
4514
Neil Roberts97b747c2013-12-19 16:17:12 +00004515 if (surface->widget->use_cairo &&
4516 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004517 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004518 return -1;
4519 }
4520
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004521 surface->frame_cb = wl_surface_frame(surface->surface);
4522 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004523 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004524
4525 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004526 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004527 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004528 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004529 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004530}
4531
4532static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004533idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004534{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004535 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004536 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004537 int failed = 0;
4538 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004539
Pekka Paalanen71233882013-04-25 13:57:53 +03004540 DBG(" --------- \n");
4541
Pekka Paalaneneebff542013-04-25 13:57:52 +03004542 wl_list_init(&window->redraw_task.link);
4543 window->redraw_task_scheduled = 0;
4544
4545 if (window->resize_needed) {
4546 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004547 if (window->main_surface->frame_cb) {
4548 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004549 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004550 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004551
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004552 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004553 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004554 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004555
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004556 if (surface_redraw(window->main_surface) < 0) {
4557 /*
4558 * Only main_surface failure will cause us to undo the resize.
4559 * If sub-surfaces fail, they will just be broken with old
4560 * content.
4561 */
4562 failed = 1;
4563 } else {
4564 wl_list_for_each(surface, &window->subsurface_list, link) {
4565 if (surface == window->main_surface)
4566 continue;
4567
4568 surface_redraw(surface);
4569 }
4570 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004571
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004572 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004573 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004574
4575 wl_list_for_each(surface, &window->subsurface_list, link)
4576 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004577
4578 if (resized && failed) {
4579 /* Restore widget tree to correspond to what is on screen. */
4580 undo_resize(window);
4581 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004582}
4583
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004584static void
4585window_schedule_redraw_task(struct window *window)
4586{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004587 if (window->redraw_inhibited)
4588 return;
4589
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004590 if (!window->redraw_task_scheduled) {
4591 window->redraw_task.run = idle_redraw;
4592 display_defer(window->display, &window->redraw_task);
4593 window->redraw_task_scheduled = 1;
4594 }
4595}
4596
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004597void
4598window_schedule_redraw(struct window *window)
4599{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004600 struct surface *surface;
4601
Pekka Paalanen71233882013-04-25 13:57:53 +03004602 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4603
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004604 wl_list_for_each(surface, &window->subsurface_list, link)
4605 surface->redraw_needed = 1;
4606
4607 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004608}
4609
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004610int
4611window_is_fullscreen(struct window *window)
4612{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004613 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004614}
4615
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004616void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004617window_set_fullscreen(struct window *window, int fullscreen)
4618{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004619 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004620 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004621
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004622 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004623 return;
4624
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004625 if (fullscreen)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004626 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004627 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004628 zxdg_toplevel_v6_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004629}
4630
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004631int
4632window_is_maximized(struct window *window)
4633{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004634 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004635}
4636
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004637void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004638window_set_maximized(struct window *window, int maximized)
4639{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004640 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004641 return;
4642
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004643 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004644 return;
4645
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004646 if (maximized)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004647 zxdg_toplevel_v6_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004648 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004649 zxdg_toplevel_v6_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004650}
4651
Jasper St. Pierrede680992014-04-10 17:23:49 -07004652int
4653window_is_resizing(struct window *window)
4654{
4655 return window->resizing;
4656}
4657
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004658void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004659window_set_minimized(struct window *window)
4660{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004661 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004662 return;
4663
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004664 zxdg_toplevel_v6_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004665}
4666
4667void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004668window_set_user_data(struct window *window, void *data)
4669{
4670 window->user_data = data;
4671}
4672
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004673void *
4674window_get_user_data(struct window *window)
4675{
4676 return window->user_data;
4677}
4678
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004679void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004680window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004681 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004682{
4683 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004684}
4685
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004686void
4687window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004688 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004689{
4690 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004691}
4692
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004693void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004694window_set_data_handler(struct window *window, window_data_handler_t handler)
4695{
4696 window->data_handler = handler;
4697}
4698
4699void
4700window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4701{
4702 window->drop_handler = handler;
4703}
4704
4705void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004706window_set_close_handler(struct window *window,
4707 window_close_handler_t handler)
4708{
4709 window->close_handler = handler;
4710}
4711
4712void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004713window_set_fullscreen_handler(struct window *window,
4714 window_fullscreen_handler_t handler)
4715{
4716 window->fullscreen_handler = handler;
4717}
4718
4719void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004720window_set_output_handler(struct window *window,
4721 window_output_handler_t handler)
4722{
4723 window->output_handler = handler;
4724}
4725
4726void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004727window_set_state_changed_handler(struct window *window,
4728 window_state_changed_handler_t handler)
4729{
4730 window->state_changed_handler = handler;
4731}
4732
4733void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004734window_set_pointer_locked_handler(struct window *window,
4735 locked_pointer_locked_handler_t locked,
4736 locked_pointer_unlocked_handler_t unlocked)
4737{
4738 window->pointer_unlocked_handler = unlocked;
4739 window->pointer_locked_handler = locked;
4740}
4741
4742void
4743window_set_pointer_confined_handler(struct window *window,
4744 confined_pointer_confined_handler_t confined,
4745 confined_pointer_unconfined_handler_t unconfined)
4746{
4747 window->pointer_confined_handler = confined;
4748 window->pointer_unconfined_handler = unconfined;
4749}
4750
4751void
4752window_set_locked_pointer_motion_handler(struct window *window,
4753 window_locked_pointer_motion_handler_t handler)
4754{
4755 window->locked_pointer_motion_handler = handler;
4756}
4757
4758void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004759window_set_title(struct window *window, const char *title)
4760{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004761 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004762 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004763 if (window->frame) {
4764 frame_set_title(window->frame->frame, title);
4765 widget_schedule_redraw(window->frame->widget);
4766 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004767 if (window->xdg_toplevel)
4768 zxdg_toplevel_v6_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004769}
4770
4771const char *
4772window_get_title(struct window *window)
4773{
4774 return window->title;
4775}
4776
4777void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004778window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4779{
4780 struct text_cursor_position *text_cursor_position =
4781 window->display->text_cursor_position;
4782
Scott Moreau9295ce02012-06-01 12:46:10 -06004783 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004784 return;
4785
4786 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004787 window->main_surface->surface,
4788 wl_fixed_from_int(x),
4789 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004790}
4791
Casey Dahlin9074db52012-04-19 22:50:09 -04004792static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004793relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4794 uint32_t utime_hi,
4795 uint32_t utime_lo,
4796 wl_fixed_t dx,
4797 wl_fixed_t dy,
4798 wl_fixed_t dx_unaccel,
4799 wl_fixed_t dy_unaccel)
4800{
4801 struct input *input = data;
4802 struct window *window = input->pointer_focus;
4803 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4804
4805 if (window->locked_pointer_motion_handler &&
4806 window->pointer_locked) {
4807 window->locked_pointer_motion_handler(
4808 window, input, ms,
4809 wl_fixed_to_double(dx),
4810 wl_fixed_to_double(dy),
4811 window->user_data);
4812 }
4813}
4814
4815static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4816 relative_pointer_handle_motion,
4817};
4818
4819static void
4820locked_pointer_locked(void *data,
4821 struct zwp_locked_pointer_v1 *locked_pointer)
4822{
4823 struct input *input = data;
4824 struct window *window = input->pointer_focus;
4825
4826 window->pointer_locked = true;
4827
4828 if (window->pointer_locked_handler) {
4829 window->pointer_locked_handler(window,
4830 input,
4831 window->user_data);
4832 }
4833}
4834
4835static void
4836locked_pointer_unlocked(void *data,
4837 struct zwp_locked_pointer_v1 *locked_pointer)
4838{
4839 struct input *input = data;
4840 struct window *window = input->pointer_focus;
4841
4842 window_unlock_pointer(window);
4843
4844 if (window->pointer_unlocked_handler) {
4845 window->pointer_unlocked_handler(window,
4846 input,
4847 window->user_data);
4848 }
4849}
4850
4851static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4852 locked_pointer_locked,
4853 locked_pointer_unlocked,
4854};
4855
4856int
4857window_lock_pointer(struct window *window, struct input *input)
4858{
4859 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4860 window->display->relative_pointer_manager;
4861 struct zwp_pointer_constraints_v1 *pointer_constraints =
4862 window->display->pointer_constraints;
4863 struct zwp_relative_pointer_v1 *relative_pointer;
4864 struct zwp_locked_pointer_v1 *locked_pointer;
4865
4866 if (!window->display->relative_pointer_manager)
4867 return -1;
4868
4869 if (!window->display->pointer_constraints)
4870 return -1;
4871
4872 if (window->locked_pointer)
4873 return -1;
4874
4875 if (window->confined_pointer)
4876 return -1;
4877
4878 if (!input->pointer)
4879 return -1;
4880
4881 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4882 relative_pointer_manager, input->pointer);
4883 zwp_relative_pointer_v1_add_listener(relative_pointer,
4884 &relative_pointer_listener,
4885 input);
4886
4887 locked_pointer =
4888 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4889 window->main_surface->surface,
4890 input->pointer,
4891 NULL,
4892 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4893 zwp_locked_pointer_v1_add_listener(locked_pointer,
4894 &locked_pointer_listener,
4895 input);
4896
4897 window->locked_input = input;
4898 window->locked_pointer = locked_pointer;
4899 window->relative_pointer = relative_pointer;
4900
4901 return 0;
4902}
4903
4904void
4905window_unlock_pointer(struct window *window)
4906{
4907 if (!window->locked_pointer)
4908 return;
4909
4910 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4911 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4912 window->locked_pointer = NULL;
4913 window->relative_pointer = NULL;
4914 window->pointer_locked = false;
4915 window->locked_input = NULL;
4916}
4917
4918void
4919widget_set_locked_pointer_cursor_hint(struct widget *widget,
4920 float x, float y)
4921{
4922 struct window *window = widget->window;
4923
4924 if (!window->locked_pointer)
4925 return;
4926
4927 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4928 wl_fixed_from_double(x),
4929 wl_fixed_from_double(y));
4930 wl_surface_commit(window->main_surface->surface);
4931}
4932
4933static void
4934confined_pointer_confined(void *data,
4935 struct zwp_confined_pointer_v1 *confined_pointer)
4936{
4937 struct input *input = data;
4938 struct window *window = input->pointer_focus;
4939
4940 window->confined = true;
4941
4942 if (window->pointer_confined_handler) {
4943 window->pointer_confined_handler(window,
4944 input,
4945 window->user_data);
4946 }
4947}
4948
4949static void
4950confined_pointer_unconfined(void *data,
4951 struct zwp_confined_pointer_v1 *confined_pointer)
4952{
4953 struct input *input = data;
4954 struct window *window = input->pointer_focus;
4955
4956 window_unconfine_pointer(window);
4957
4958 window->confined = false;
4959
4960 if (window->pointer_unconfined_handler) {
4961 window->pointer_unconfined_handler(window,
4962 input,
4963 window->user_data);
4964 }
4965}
4966
4967static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4968 confined_pointer_confined,
4969 confined_pointer_unconfined,
4970};
4971
4972int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004973window_confine_pointer_to_rectangles(struct window *window,
4974 struct input *input,
4975 struct rectangle *rectangles,
4976 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004977{
4978 struct zwp_pointer_constraints_v1 *pointer_constraints =
4979 window->display->pointer_constraints;
4980 struct zwp_confined_pointer_v1 *confined_pointer;
4981 struct wl_compositor *compositor = window->display->compositor;
4982 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004983 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004984
4985 if (!window->display->pointer_constraints)
4986 return -1;
4987
4988 if (window->locked_pointer)
4989 return -1;
4990
4991 if (window->confined_pointer)
4992 return -1;
4993
4994 if (!input->pointer)
4995 return -1;
4996
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004997 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004998 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004999 for (i = 0; i < num_rectangles; i++) {
5000 wl_region_add(region,
5001 rectangles[i].x,
5002 rectangles[i].y,
5003 rectangles[i].width,
5004 rectangles[i].height);
5005 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005006 }
5007
5008 confined_pointer =
5009 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
5010 window->main_surface->surface,
5011 input->pointer,
5012 region,
5013 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
5014 if (region)
5015 wl_region_destroy(region);
5016
5017 zwp_confined_pointer_v1_add_listener(confined_pointer,
5018 &confined_pointer_listener,
5019 input);
5020
5021 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005022 window->confined_widget = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005023
5024 return 0;
5025}
5026
5027void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08005028window_update_confine_rectangles(struct window *window,
5029 struct rectangle *rectangles,
5030 int num_rectangles)
5031{
5032 struct wl_compositor *compositor = window->display->compositor;
5033 struct wl_region *region;
5034 int i;
5035
5036 region = wl_compositor_create_region(compositor);
5037 for (i = 0; i < num_rectangles; i++) {
5038 wl_region_add(region,
5039 rectangles[i].x,
5040 rectangles[i].y,
5041 rectangles[i].width,
5042 rectangles[i].height);
5043 }
5044
5045 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
5046
5047 wl_region_destroy(region);
5048}
5049
5050int
5051window_confine_pointer_to_widget(struct window *window,
5052 struct widget *widget,
5053 struct input *input)
5054{
5055 int ret;
5056
5057 if (widget) {
5058 ret = window_confine_pointer_to_rectangles(window,
5059 input,
5060 &widget->allocation,
5061 1);
5062 window->confined_widget = widget;
5063 return ret;
5064 } else {
5065 return window_confine_pointer_to_rectangles(window,
5066 input,
5067 NULL,
5068 0);
5069 }
5070}
5071
5072void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005073window_unconfine_pointer(struct window *window)
5074{
5075 if (!window->confined_pointer)
5076 return;
5077
5078 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5079 window->confined_pointer = NULL;
5080 window->confined = false;
5081}
5082
5083static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005084surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005085 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005086{
Rob Bradford7507b572012-05-15 17:55:34 +01005087 struct window *window = data;
5088 struct output *output;
5089 struct output *output_found = NULL;
5090 struct window_output *window_output;
5091
5092 wl_list_for_each(output, &window->display->output_list, link) {
5093 if (output->output == wl_output) {
5094 output_found = output;
5095 break;
5096 }
5097 }
5098
5099 if (!output_found)
5100 return;
5101
Brian Lovinbc919262013-08-07 15:34:59 -07005102 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005103 window_output->output = output_found;
5104
5105 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005106
5107 if (window->output_handler)
5108 window->output_handler(window, output_found, 1,
5109 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005110}
5111
5112static void
5113surface_leave(void *data,
5114 struct wl_surface *wl_surface, struct wl_output *output)
5115{
Rob Bradford7507b572012-05-15 17:55:34 +01005116 struct window *window = data;
5117 struct window_output *window_output;
5118 struct window_output *window_output_found = NULL;
5119
5120 wl_list_for_each(window_output, &window->window_output_list, link) {
5121 if (window_output->output->output == output) {
5122 window_output_found = window_output;
5123 break;
5124 }
5125 }
5126
5127 if (window_output_found) {
5128 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005129
5130 if (window->output_handler)
5131 window->output_handler(window, window_output->output,
5132 0, window->user_data);
5133
Rob Bradford7507b572012-05-15 17:55:34 +01005134 free(window_output_found);
5135 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005136}
5137
5138static const struct wl_surface_listener surface_listener = {
5139 surface_enter,
5140 surface_leave
5141};
5142
Pekka Paalanen4e373742013-02-13 16:17:13 +02005143static struct surface *
5144surface_create(struct window *window)
5145{
5146 struct display *display = window->display;
5147 struct surface *surface;
5148
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005149 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005150 surface->window = window;
5151 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005152 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005153 wl_surface_add_listener(surface->surface, &surface_listener, window);
5154
Pekka Paalanen35e82632013-04-25 13:57:48 +03005155 wl_list_insert(&window->subsurface_list, &surface->link);
5156
Pekka Paalanen4e373742013-02-13 16:17:13 +02005157 return surface;
5158}
5159
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005160static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005161get_preferred_buffer_type(struct display *display)
5162{
5163#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005164 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005165 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5166#endif
5167
5168 return WINDOW_BUFFER_TYPE_SHM;
5169}
5170
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005171static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005172window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005173{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005174 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005175 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005176
Peter Huttererf3d62272013-08-08 11:57:05 +10005177 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005178 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005179 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005180
5181 surface = surface_create(window);
5182 window->main_surface = surface;
5183
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005184 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005185
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005186 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005187 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005188
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005189 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005190
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005191 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005192 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005193 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005194
Rob Bradford7507b572012-05-15 17:55:34 +01005195 wl_list_init (&window->window_output_list);
5196
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005197 return window;
5198}
5199
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005200struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005201window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005202{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005203 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005204 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005205
5206 window = window_create_internal(display, 0);
5207
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005208 if (window->display->xdg_shell) {
5209 window->xdg_surface =
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005210 zxdg_shell_v6_get_xdg_surface(window->display->xdg_shell,
5211 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005212 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005213
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005214 zxdg_surface_v6_add_listener(window->xdg_surface,
5215 &xdg_surface_listener, window);
5216
5217 window->xdg_toplevel =
5218 zxdg_surface_v6_get_toplevel(window->xdg_surface);
5219 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5220
5221 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
5222 &xdg_toplevel_listener, window);
5223
5224 window_inhibit_redraw(window);
5225
5226 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005227 } else if (display->ivi_application) {
5228 /* auto generation of ivi_id based on process id + basement of id */
5229 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5230 window->ivi_surface =
5231 ivi_application_surface_create(display->ivi_application,
5232 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005233 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005234
5235 ivi_surface_add_listener(window->ivi_surface,
5236 &ivi_surface_listener, window);
5237 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005238
5239 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005240}
5241
5242struct window *
5243window_create_custom(struct display *display)
5244{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005245 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005246}
5247
Jasper St. Pierre53686042013-12-09 15:26:25 -05005248void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005249window_set_parent(struct window *window,
5250 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005251{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005252 window->parent = parent_window;
5253 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005254}
5255
5256struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005257window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005258{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005259 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005260}
5261
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005262static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005263menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005264{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005265 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005266 int next;
5267
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005268 frame_interior(menu->frame, &x, &y, &width, &height);
5269 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005270 if (menu->current != next) {
5271 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005272 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005273 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005274}
5275
5276static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005277menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005278 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005279 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005280{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005281 struct menu *menu = data;
5282
5283 if (widget == menu->widget)
5284 menu_set_item(data, y);
5285
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005286 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005287}
5288
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005289static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005290menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005291 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005292{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005293 struct menu *menu = data;
5294
5295 if (widget == menu->widget)
5296 menu_set_item(data, y);
5297
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005298 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005299}
5300
5301static void
5302menu_leave_handler(struct widget *widget, struct input *input, void *data)
5303{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005304 struct menu *menu = data;
5305
5306 if (widget == menu->widget)
5307 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005308}
5309
5310static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005311menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005312 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005313 uint32_t button, enum wl_pointer_button_state state,
5314 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005315
5316{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005317 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005318
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005319 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5320 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005321 /* Either relase after press-drag-release or
5322 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005323 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005324 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005325 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005326 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005327 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005328 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005329}
5330
5331static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005332menu_touch_up_handler(struct widget *widget,
5333 struct input *input,
5334 uint32_t serial,
5335 uint32_t time,
5336 int32_t id,
5337 void *data)
5338{
5339 struct menu *menu = data;
5340
5341 input_ungrab(input);
5342 menu_destroy(menu);
5343}
5344
5345static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005346menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005347{
5348 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005349 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005350 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005351
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005352 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005353
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005354 frame_repaint(menu->frame, cr);
5355 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005356
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005357 theme_set_background_source(menu->window->display->theme,
5358 cr, THEME_FRAME_ACTIVE);
5359 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005360 cairo_fill(cr);
5361
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005362 cairo_select_font_face(cr, "sans",
5363 CAIRO_FONT_SLANT_NORMAL,
5364 CAIRO_FONT_WEIGHT_NORMAL);
5365 cairo_set_font_size(cr, 12);
5366
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005367 for (i = 0; i < menu->count; i++) {
5368 if (i == menu->current) {
5369 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005370 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005371 cairo_fill(cr);
5372 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005373 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005374 cairo_show_text(cr, menu->entries[i]);
5375 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005376 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5377 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005378 cairo_show_text(cr, menu->entries[i]);
5379 }
5380 }
5381
5382 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005383}
5384
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005385static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005386xdg_popup_handle_configure(void *data,
5387 struct zxdg_popup_v6 *zxdg_popup_v6,
5388 int32_t x,
5389 int32_t y,
5390 int32_t width,
5391 int32_t height)
5392{
5393}
5394
5395static void
5396xdg_popup_handle_popup_done(void *data, struct zxdg_popup_v6 *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005397{
5398 struct window *window = data;
5399 struct menu *menu = window->main_surface->widget->user_data;
5400
5401 input_ungrab(menu->input);
5402 menu_destroy(menu);
5403}
5404
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005405static const struct zxdg_popup_v6_listener xdg_popup_listener = {
5406 xdg_popup_handle_configure,
5407 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005408};
5409
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005410static struct menu *
5411create_menu(struct display *display,
5412 struct input *input, uint32_t time,
5413 menu_func_t func, const char **entries, int count,
5414 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005415{
5416 struct window *window;
5417 struct menu *menu;
5418
5419 menu = malloc(sizeof *menu);
5420 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005421 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005422
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005423 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005424 if (!window) {
5425 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005426 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005427 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005428
5429 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005430 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005431 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005432 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07005433 FRAME_BUTTON_NONE, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005434 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005435 menu->entries = entries;
5436 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005437 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005438 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005439 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005440 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005441 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005442
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005443 input_ungrab(input);
5444
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005445 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005446 widget_set_enter_handler(menu->widget, menu_enter_handler);
5447 widget_set_leave_handler(menu->widget, menu_leave_handler);
5448 widget_set_motion_handler(menu->widget, menu_motion_handler);
5449 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005450 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005451
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005452 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005453 frame_resize_inside(menu->frame, 200, count * 20);
5454 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5455 window_schedule_resize(window, frame_width(menu->frame),
5456 frame_height(menu->frame));
5457
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005458 return menu;
5459}
5460
5461struct window *
5462window_create_menu(struct display *display,
5463 struct input *input, uint32_t time,
5464 menu_func_t func, const char **entries, int count,
5465 void *user_data)
5466{
5467 struct menu *menu;
5468 menu = create_menu(display, input, time, func, entries, count, user_data);
5469
5470 if (menu == NULL)
5471 return NULL;
5472
5473 return menu->window;
5474}
5475
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005476static struct zxdg_positioner_v6 *
5477create_simple_positioner(struct display *display,
5478 int x, int y)
5479{
5480 struct zxdg_positioner_v6 *positioner;
5481
5482 positioner = zxdg_shell_v6_create_positioner(display->xdg_shell);
5483 fail_on_null(positioner, 0, __FILE__, __LINE__);
5484 zxdg_positioner_v6_set_anchor_rect(positioner, x, y, 1, 1);
5485 zxdg_positioner_v6_set_anchor(positioner,
5486 ZXDG_POSITIONER_V6_ANCHOR_TOP |
5487 ZXDG_POSITIONER_V6_ANCHOR_LEFT);
5488 zxdg_positioner_v6_set_gravity(positioner,
5489 ZXDG_POSITIONER_V6_ANCHOR_BOTTOM |
5490 ZXDG_POSITIONER_V6_ANCHOR_RIGHT);
5491
5492 return positioner;
5493}
5494
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005495void
5496window_show_menu(struct display *display,
5497 struct input *input, uint32_t time, struct window *parent,
5498 int32_t x, int32_t y,
5499 menu_func_t func, const char **entries, int count)
5500{
5501 struct menu *menu;
5502 struct window *window;
5503 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005504 struct rectangle parent_geometry;
5505 struct zxdg_positioner_v6 *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005506
5507 menu = create_menu(display, input, time, func, entries, count, parent);
5508
5509 if (menu == NULL)
5510 return;
5511
5512 window = menu->window;
5513
5514 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5515 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5516
5517 window->x = x;
5518 window->y = y;
5519
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005520 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005521 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005522
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005523 if (!display->xdg_shell)
5524 return;
5525
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005526 window->xdg_surface =
5527 zxdg_shell_v6_get_xdg_surface(display->xdg_shell,
5528 window->main_surface->surface);
5529 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005530
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005531 zxdg_surface_v6_add_listener(window->xdg_surface,
5532 &xdg_surface_listener, window);
5533
5534 positioner = create_simple_positioner(display,
5535 window->x - (ix + parent_geometry.x),
5536 window->y - (iy + parent_geometry.y));
5537 window->xdg_popup =
5538 zxdg_surface_v6_get_popup(window->xdg_surface,
5539 parent->xdg_surface,
5540 positioner);
5541 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
5542 zxdg_positioner_v6_destroy(positioner);
5543 zxdg_popup_v6_grab(window->xdg_popup,
5544 input->seat,
5545 display_get_serial(window->display));
5546 zxdg_popup_v6_add_listener(window->xdg_popup,
5547 &xdg_popup_listener, window);
5548
5549 window_inhibit_redraw(window);
5550
5551 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005552}
5553
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005554void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005555window_set_buffer_type(struct window *window, enum window_buffer_type type)
5556{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005557 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005558}
5559
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005560enum window_buffer_type
5561window_get_buffer_type(struct window *window)
5562{
5563 return window->main_surface->buffer_type;
5564}
5565
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005566void
5567window_set_preferred_format(struct window *window,
5568 enum preferred_format format)
5569{
5570 window->preferred_format = format;
5571}
5572
Pekka Paalanen35e82632013-04-25 13:57:48 +03005573struct widget *
5574window_add_subsurface(struct window *window, void *data,
5575 enum subsurface_mode default_mode)
5576{
5577 struct widget *widget;
5578 struct surface *surface;
5579 struct wl_surface *parent;
5580 struct wl_subcompositor *subcompo = window->display->subcompositor;
5581
Pekka Paalanen35e82632013-04-25 13:57:48 +03005582 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005583 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005584 widget = widget_create(window, surface, data);
5585 wl_list_init(&widget->link);
5586 surface->widget = widget;
5587
5588 parent = window->main_surface->surface;
5589 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5590 surface->surface,
5591 parent);
5592 surface->synchronized = 1;
5593
5594 switch (default_mode) {
5595 case SUBSURFACE_SYNCHRONIZED:
5596 surface->synchronized_default = 1;
5597 break;
5598 case SUBSURFACE_DESYNCHRONIZED:
5599 surface->synchronized_default = 0;
5600 break;
5601 default:
5602 assert(!"bad enum subsurface_mode");
5603 }
5604
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005605 window->resize_needed = 1;
5606 window_schedule_redraw(window);
5607
Pekka Paalanen35e82632013-04-25 13:57:48 +03005608 return widget;
5609}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005610
5611static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005612display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005613 struct wl_output *wl_output,
5614 int x, int y,
5615 int physical_width,
5616 int physical_height,
5617 int subpixel,
5618 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005619 const char *model,
5620 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005621{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005622 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005623
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005624 output->allocation.x = x;
5625 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005626 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005627
5628 if (output->make)
5629 free(output->make);
5630 output->make = strdup(make);
5631
5632 if (output->model)
5633 free(output->model);
5634 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005635}
5636
5637static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005638display_handle_done(void *data,
5639 struct wl_output *wl_output)
5640{
5641}
5642
5643static void
5644display_handle_scale(void *data,
5645 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005646 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005647{
5648 struct output *output = data;
5649
5650 output->scale = scale;
5651}
5652
5653static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005654display_handle_mode(void *data,
5655 struct wl_output *wl_output,
5656 uint32_t flags,
5657 int width,
5658 int height,
5659 int refresh)
5660{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005661 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005662 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005663
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005664 if (flags & WL_OUTPUT_MODE_CURRENT) {
5665 output->allocation.width = width;
5666 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005667 if (display->output_configure_handler)
5668 (*display->output_configure_handler)(
5669 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005670 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005671}
5672
5673static const struct wl_output_listener output_listener = {
5674 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005675 display_handle_mode,
5676 display_handle_done,
5677 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005678};
5679
5680static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005681display_add_output(struct display *d, uint32_t id)
5682{
5683 struct output *output;
5684
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005685 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005686 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005687 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005688 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005689 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005690 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005691 wl_list_insert(d->output_list.prev, &output->link);
5692
5693 wl_output_add_listener(output->output, &output_listener, output);
5694}
5695
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005696static void
5697output_destroy(struct output *output)
5698{
5699 if (output->destroy_handler)
5700 (*output->destroy_handler)(output, output->user_data);
5701
5702 wl_output_destroy(output->output);
5703 wl_list_remove(&output->link);
5704 free(output);
5705}
5706
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005707static void
5708display_destroy_output(struct display *d, uint32_t id)
5709{
5710 struct output *output;
5711
5712 wl_list_for_each(output, &d->output_list, link) {
5713 if (output->server_output_id == id) {
5714 output_destroy(output);
5715 break;
5716 }
5717 }
5718}
5719
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005720void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005721display_set_global_handler(struct display *display,
5722 display_global_handler_t handler)
5723{
5724 struct global *global;
5725
5726 display->global_handler = handler;
5727 if (!handler)
5728 return;
5729
5730 wl_list_for_each(global, &display->global_list, link)
5731 display->global_handler(display,
5732 global->name, global->interface,
5733 global->version, display->user_data);
5734}
5735
5736void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005737display_set_global_handler_remove(struct display *display,
5738 display_global_handler_t remove_handler)
5739{
5740 display->global_handler_remove = remove_handler;
5741 if (!remove_handler)
5742 return;
5743}
5744
5745void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005746display_set_output_configure_handler(struct display *display,
5747 display_output_handler_t handler)
5748{
5749 struct output *output;
5750
5751 display->output_configure_handler = handler;
5752 if (!handler)
5753 return;
5754
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005755 wl_list_for_each(output, &display->output_list, link) {
5756 if (output->allocation.width == 0 &&
5757 output->allocation.height == 0)
5758 continue;
5759
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005760 (*display->output_configure_handler)(output,
5761 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005762 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005763}
5764
5765void
5766output_set_user_data(struct output *output, void *data)
5767{
5768 output->user_data = data;
5769}
5770
5771void *
5772output_get_user_data(struct output *output)
5773{
5774 return output->user_data;
5775}
5776
5777void
5778output_set_destroy_handler(struct output *output,
5779 display_output_handler_t handler)
5780{
5781 output->destroy_handler = handler;
5782 /* FIXME: implement this, once we have way to remove outputs */
5783}
5784
5785void
Scott Moreau4e072362012-09-29 02:03:11 -06005786output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005787{
Scott Moreau4e072362012-09-29 02:03:11 -06005788 struct rectangle allocation = output->allocation;
5789
5790 switch (output->transform) {
5791 case WL_OUTPUT_TRANSFORM_90:
5792 case WL_OUTPUT_TRANSFORM_270:
5793 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5794 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5795 /* Swap width and height */
5796 allocation.width = output->allocation.height;
5797 allocation.height = output->allocation.width;
5798 break;
5799 }
5800
5801 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005802}
5803
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005804struct wl_output *
5805output_get_wl_output(struct output *output)
5806{
5807 return output->output;
5808}
5809
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005810enum wl_output_transform
5811output_get_transform(struct output *output)
5812{
5813 return output->transform;
5814}
5815
Alexander Larssonafd319a2013-05-22 14:41:27 +02005816uint32_t
5817output_get_scale(struct output *output)
5818{
5819 return output->scale;
5820}
5821
Jason Ekstrand738715d2014-04-02 19:53:50 -05005822const char *
5823output_get_make(struct output *output)
5824{
5825 return output->make;
5826}
5827
5828const char *
5829output_get_model(struct output *output)
5830{
5831 return output->model;
5832}
5833
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005834static void
Daniel Stone97f68542012-05-30 16:32:01 +01005835fini_xkb(struct input *input)
5836{
5837 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005838 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005839}
5840
5841static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005842display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005843{
5844 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005845 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005846
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005847 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005848 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005849 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005850 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005851 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005852 input->pointer_focus = NULL;
5853 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005854 input->seat_version = seat_version;
5855
Rusty Lynch041815a2013-08-08 21:20:38 -07005856 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005857 wl_list_insert(d->input_list.prev, &input->link);
5858
Daniel Stone37816df2012-05-16 18:45:18 +01005859 wl_seat_add_listener(input->seat, &seat_listener, input);
5860 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005861
Jason Ekstranda669bd52014-04-02 19:53:51 -05005862 if (d->data_device_manager) {
5863 input->data_device =
5864 wl_data_device_manager_get_data_device(d->data_device_manager,
5865 input->seat);
5866 wl_data_device_add_listener(input->data_device,
5867 &data_device_listener,
5868 input);
5869 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005870
5871 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005872 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005873
Derek Foreman118a4292015-04-22 17:23:35 -05005874 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5875 TFD_CLOEXEC | TFD_NONBLOCK);
5876 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5877 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005878 set_repeat_info(input, 40, 400);
5879
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005880 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5881 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005882 input->repeat_task.run = keyboard_repeat_func;
5883 display_watch_fd(d, input->repeat_timer_fd,
5884 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005885}
5886
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005887static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005888input_destroy(struct input *input)
5889{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005890 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005891 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005892
5893 if (input->drag_offer)
5894 data_offer_destroy(input->drag_offer);
5895
5896 if (input->selection_offer)
5897 data_offer_destroy(input->selection_offer);
5898
kabeer khan6ce67ec2014-10-20 11:55:29 +05305899 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005900 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305901 wl_data_device_release(input->data_device);
5902 else
5903 wl_data_device_destroy(input->data_device);
5904 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005905 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005906 if (input->touch)
5907 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005908 if (input->pointer)
5909 wl_pointer_release(input->pointer);
5910 if (input->keyboard)
5911 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005912 } else {
5913 if (input->touch)
5914 wl_touch_destroy(input->touch);
5915 if (input->pointer)
5916 wl_pointer_destroy(input->pointer);
5917 if (input->keyboard)
5918 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005919 }
5920
Daniel Stone97f68542012-05-30 16:32:01 +01005921 fini_xkb(input);
5922
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005923 wl_surface_destroy(input->pointer_surface);
5924
Pekka Paalanene1207c72011-12-16 12:02:09 +02005925 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005926 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005927 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005928 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005929 free(input);
5930}
5931
5932static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005933shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5934{
5935 struct display *d = data;
5936
5937 if (format == WL_SHM_FORMAT_RGB565)
5938 d->has_rgb565 = 1;
5939}
5940
5941struct wl_shm_listener shm_listener = {
5942 shm_format
5943};
5944
5945static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005946xdg_shell_handle_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005947{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005948 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005949}
5950
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005951static const struct zxdg_shell_v6_listener xdg_shell_listener = {
5952 xdg_shell_handle_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005953};
5954
5955static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005956registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5957 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005958{
5959 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005960 struct global *global;
5961
Brian Lovinbc919262013-08-07 15:34:59 -07005962 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005963 global->name = id;
5964 global->interface = strdup(interface);
5965 global->version = version;
5966 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005967
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005968 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005969 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005970 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005971 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005972 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005973 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005974 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005975 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5976 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5977 d->relative_pointer_manager =
5978 wl_registry_bind(registry, id,
5979 &zwp_relative_pointer_manager_v1_interface,
5980 1);
5981 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5982 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5983 d->pointer_constraints =
5984 wl_registry_bind(registry, id,
5985 &zwp_pointer_constraints_v1_interface,
5986 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005987 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005988 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005989 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005990 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005991 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005992 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005993 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305994 &wl_data_device_manager_interface,
5995 d->data_device_manager_version);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005996 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005997 d->xdg_shell = wl_registry_bind(registry, id,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005998 &zxdg_shell_v6_interface, 1);
5999 zxdg_shell_v6_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06006000 } else if (strcmp(interface, "text_cursor_position") == 0) {
6001 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006002 wl_registry_bind(registry, id,
6003 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03006004 } else if (strcmp(interface, "wl_subcompositor") == 0) {
6005 d->subcompositor =
6006 wl_registry_bind(registry, id,
6007 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006008 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006009 else if (strcmp(interface, "ivi_application") == 0) {
6010 d->ivi_application =
6011 wl_registry_bind(registry, id,
6012 &ivi_application_interface, 1);
6013 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006014
6015 if (d->global_handler)
6016 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006017}
6018
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006019static void
6020registry_handle_global_remove(void *data, struct wl_registry *registry,
6021 uint32_t name)
6022{
6023 struct display *d = data;
6024 struct global *global;
6025 struct global *tmp;
6026
6027 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
6028 if (global->name != name)
6029 continue;
6030
Xiong Zhang83d8ee72013-10-23 13:58:35 +08006031 if (strcmp(global->interface, "wl_output") == 0)
6032 display_destroy_output(d, name);
6033
6034 /* XXX: Should destroy remaining bound globals */
6035
6036 if (d->global_handler_remove)
6037 d->global_handler_remove(d, name, global->interface,
6038 global->version, d->user_data);
6039
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006040 wl_list_remove(&global->link);
6041 free(global->interface);
6042 free(global);
6043 }
6044}
6045
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006046void *
6047display_bind(struct display *display, uint32_t name,
6048 const struct wl_interface *interface, uint32_t version)
6049{
6050 return wl_registry_bind(display->registry, name, interface, version);
6051}
6052
6053static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02006054 registry_handle_global,
6055 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006056};
6057
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006058#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05006059static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05006060init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05006061{
6062 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006063 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04006064
Rob Clark6396ed32012-03-11 19:48:41 -05006065#ifdef USE_CAIRO_GLESV2
6066# define GL_BIT EGL_OPENGL_ES2_BIT
6067#else
6068# define GL_BIT EGL_OPENGL_BIT
6069#endif
6070
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006071 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006072 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006073 EGL_RED_SIZE, 1,
6074 EGL_GREEN_SIZE, 1,
6075 EGL_BLUE_SIZE, 1,
6076 EGL_ALPHA_SIZE, 1,
6077 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006078 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006079 EGL_NONE
6080 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006081
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006082#ifdef USE_CAIRO_GLESV2
6083 static const EGLint context_attribs[] = {
6084 EGL_CONTEXT_CLIENT_VERSION, 2,
6085 EGL_NONE
6086 };
6087 EGLint api = EGL_OPENGL_ES_API;
6088#else
6089 EGLint *context_attribs = NULL;
6090 EGLint api = EGL_OPENGL_API;
6091#endif
6092
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006093 d->dpy =
6094 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6095 d->display, NULL);
6096
Yuval Fledel45568f62010-12-06 09:18:12 -05006097 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006098 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006099 return -1;
6100 }
6101
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006102 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006103 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006104 return -1;
6105 }
6106
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006107 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6108 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006109 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006110 return -1;
6111 }
6112
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006113 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006114 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006115 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006116 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006117 return -1;
6118 }
6119
Benjamin Franzke0c991632011-09-27 21:57:31 +02006120 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6121 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006122 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006123 return -1;
6124 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006125
6126 return 0;
6127}
6128
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006129static void
6130fini_egl(struct display *display)
6131{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006132 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006133
6134 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6135 EGL_NO_CONTEXT);
6136
6137 eglTerminate(display->dpy);
6138 eglReleaseThread();
6139}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006140#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006141
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006142static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006143init_dummy_surface(struct display *display)
6144{
6145 int len;
6146 void *data;
6147
6148 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006149 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006150 display->dummy_surface =
6151 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6152 1, 1, len);
6153 display->dummy_surface_data = data;
6154}
6155
6156static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006157handle_display_data(struct task *task, uint32_t events)
6158{
6159 struct display *display =
6160 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006161 struct epoll_event ep;
6162 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006163
6164 display->display_fd_events = events;
6165
6166 if (events & EPOLLERR || events & EPOLLHUP) {
6167 display_exit(display);
6168 return;
6169 }
6170
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006171 if (events & EPOLLIN) {
6172 ret = wl_display_dispatch(display->display);
6173 if (ret == -1) {
6174 display_exit(display);
6175 return;
6176 }
6177 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006178
6179 if (events & EPOLLOUT) {
6180 ret = wl_display_flush(display->display);
6181 if (ret == 0) {
6182 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6183 ep.data.ptr = &display->display_task;
6184 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6185 display->display_fd, &ep);
6186 } else if (ret == -1 && errno != EAGAIN) {
6187 display_exit(display);
6188 return;
6189 }
6190 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006191}
6192
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006193static void
6194log_handler(const char *format, va_list args)
6195{
6196 vfprintf(stderr, format, args);
6197}
6198
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006199struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006200display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006201{
6202 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006203
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006204 wl_log_set_handler_client(log_handler);
6205
Peter Huttererf3d62272013-08-08 11:57:05 +10006206 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006207 if (d == NULL)
6208 return NULL;
6209
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006210 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006211 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006212 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006213 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006214 return NULL;
6215 }
6216
Rob Bradford5ab9c752013-07-26 16:29:43 +01006217 d->xkb_context = xkb_context_new(0);
6218 if (d->xkb_context == NULL) {
6219 fprintf(stderr, "Failed to create XKB context\n");
6220 free(d);
6221 return NULL;
6222 }
6223
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006224 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006225 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006226 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006227 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6228 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006229
6230 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006231 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006232 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006233 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006234
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006235 d->registry = wl_display_get_registry(d->display);
6236 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006237
Marek Chalupaa519d062014-12-05 13:49:40 +01006238 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006239 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6240 return NULL;
6241 }
6242
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006243#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006244 if (init_egl(d) < 0)
6245 fprintf(stderr, "EGL does not seem to work, "
6246 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006247#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006248
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006249 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006250
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006251 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006252
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006253 wl_list_init(&d->window_list);
6254
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006255 init_dummy_surface(d);
6256
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006257 return d;
6258}
6259
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006260static void
6261display_destroy_outputs(struct display *display)
6262{
6263 struct output *tmp;
6264 struct output *output;
6265
6266 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6267 output_destroy(output);
6268}
6269
Pekka Paalanene1207c72011-12-16 12:02:09 +02006270static void
6271display_destroy_inputs(struct display *display)
6272{
6273 struct input *tmp;
6274 struct input *input;
6275
6276 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6277 input_destroy(input);
6278}
6279
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006280void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006281display_destroy(struct display *display)
6282{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006283 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006284 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6285 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006286
6287 if (!wl_list_empty(&display->deferred_list))
6288 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6289
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006290 cairo_surface_destroy(display->dummy_surface);
6291 free(display->dummy_surface_data);
6292
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006293 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006294 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006295
Daniel Stone97f68542012-05-30 16:32:01 +01006296 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006297
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006298 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006299 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006300
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006301#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006302 if (display->argb_device)
6303 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006304#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006305
Pekka Paalanen35e82632013-04-25 13:57:48 +03006306 if (display->subcompositor)
6307 wl_subcompositor_destroy(display->subcompositor);
6308
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006309 if (display->xdg_shell)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006310 zxdg_shell_v6_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006311
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006312 if (display->ivi_application)
6313 ivi_application_destroy(display->ivi_application);
6314
Pekka Paalanenc2052982011-12-16 11:41:32 +02006315 if (display->shm)
6316 wl_shm_destroy(display->shm);
6317
6318 if (display->data_device_manager)
6319 wl_data_device_manager_destroy(display->data_device_manager);
6320
6321 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006322 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006323
6324 close(display->epoll_fd);
6325
U. Artie Eoff44874d92012-10-02 21:12:35 -07006326 if (!(display->display_fd_events & EPOLLERR) &&
6327 !(display->display_fd_events & EPOLLHUP))
6328 wl_display_flush(display->display);
6329
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006330 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006331 free(display);
6332}
6333
6334void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006335display_set_user_data(struct display *display, void *data)
6336{
6337 display->user_data = data;
6338}
6339
6340void *
6341display_get_user_data(struct display *display)
6342{
6343 return display->user_data;
6344}
6345
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006346struct wl_display *
6347display_get_display(struct display *display)
6348{
6349 return display->display;
6350}
6351
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006352int
6353display_has_subcompositor(struct display *display)
6354{
6355 if (display->subcompositor)
6356 return 1;
6357
6358 wl_display_roundtrip(display->display);
6359
6360 return display->subcompositor != NULL;
6361}
6362
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006363cairo_device_t *
6364display_get_cairo_device(struct display *display)
6365{
6366 return display->argb_device;
6367}
6368
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006369struct output *
6370display_get_output(struct display *display)
6371{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006372 if (wl_list_empty(&display->output_list))
6373 return NULL;
6374
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006375 return container_of(display->output_list.next, struct output, link);
6376}
6377
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006378struct wl_compositor *
6379display_get_compositor(struct display *display)
6380{
6381 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006382}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006383
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006384uint32_t
6385display_get_serial(struct display *display)
6386{
6387 return display->serial;
6388}
6389
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006390EGLDisplay
6391display_get_egl_display(struct display *d)
6392{
6393 return d->dpy;
6394}
6395
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006396struct wl_data_source *
6397display_create_data_source(struct display *display)
6398{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006399 if (display->data_device_manager)
6400 return wl_data_device_manager_create_data_source(display->data_device_manager);
6401 else
6402 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006403}
6404
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006405EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006406display_get_argb_egl_config(struct display *d)
6407{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006408 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006409}
6410
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006411int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006412display_acquire_window_surface(struct display *display,
6413 struct window *window,
6414 EGLContext ctx)
6415{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006416 struct surface *surface = window->main_surface;
6417
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006418 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006419 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006420
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006421 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006422 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006423}
6424
6425void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006426display_release_window_surface(struct display *display,
6427 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006428{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006429 struct surface *surface = window->main_surface;
6430
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006431 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006432 return;
6433
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006434 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006435}
6436
6437void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006438display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006439{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006440 wl_list_insert(&display->deferred_list, &task->link);
6441}
6442
6443void
6444display_watch_fd(struct display *display,
6445 int fd, uint32_t events, struct task *task)
6446{
6447 struct epoll_event ep;
6448
6449 ep.events = events;
6450 ep.data.ptr = task;
6451 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6452}
6453
6454void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006455display_unwatch_fd(struct display *display, int fd)
6456{
6457 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6458}
6459
6460void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006461display_run(struct display *display)
6462{
6463 struct task *task;
6464 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006465 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006466
Pekka Paalanen826d7952011-12-15 10:14:07 +02006467 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006468 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006469 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006470 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006471 struct task, link);
6472 wl_list_remove(&task->link);
6473 task->run(task, 0);
6474 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006475
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006476 wl_display_dispatch_pending(display->display);
6477
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006478 if (!display->running)
6479 break;
6480
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006481 ret = wl_display_flush(display->display);
6482 if (ret < 0 && errno == EAGAIN) {
6483 ep[0].events =
6484 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6485 ep[0].data.ptr = &display->display_task;
6486
6487 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6488 display->display_fd, &ep[0]);
6489 } else if (ret < 0) {
6490 break;
6491 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006492
6493 count = epoll_wait(display->epoll_fd,
6494 ep, ARRAY_LENGTH(ep), -1);
6495 for (i = 0; i < count; i++) {
6496 task = ep[i].data.ptr;
6497 task->run(task, ep[i].events);
6498 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006499 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006500}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006501
6502void
6503display_exit(struct display *display)
6504{
6505 display->running = 0;
6506}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006507
Carlos Garnacho9c931792016-01-18 23:52:12 +01006508int
6509display_get_data_device_manager_version(struct display *display)
6510{
6511 return display->data_device_manager_version;
6512}
6513
Jan Arne Petersencd997062012-11-18 19:06:44 +01006514void
6515keysym_modifiers_add(struct wl_array *modifiers_map,
6516 const char *name)
6517{
6518 size_t len = strlen(name) + 1;
6519 char *p;
6520
6521 p = wl_array_add(modifiers_map, len);
6522
6523 if (p == NULL)
6524 return;
6525
6526 strncpy(p, name, len);
6527}
6528
6529static xkb_mod_index_t
6530keysym_modifiers_get_index(struct wl_array *modifiers_map,
6531 const char *name)
6532{
6533 xkb_mod_index_t index = 0;
6534 char *p = modifiers_map->data;
6535
6536 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6537 if (strcmp(p, name) == 0)
6538 return index;
6539
6540 index++;
6541 p += strlen(p) + 1;
6542 }
6543
6544 return XKB_MOD_INVALID;
6545}
6546
6547xkb_mod_mask_t
6548keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6549 const char *name)
6550{
6551 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6552
6553 if (index == XKB_MOD_INVALID)
6554 return XKB_MOD_INVALID;
6555
6556 return 1 << index;
6557}