blob: 6410a596cabca59d349917205b3dcc005d417ac1 [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>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030066#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040067
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050068#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020069#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070070#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070071#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070072#include "shared/xalloc.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080073#include "shared/zalloc.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080074#include "xdg-shell-unstable-v5-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060075#include "text-cursor-position-client-protocol.h"
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080076#include "pointer-constraints-unstable-v1-client-protocol.h"
77#include "relative-pointer-unstable-v1-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070078#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050079
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050080#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050081
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090082#include <sys/types.h>
83#include "ivi-application-client-protocol.h"
84#define IVI_SURFACE_ID 9000
85
Jonas Ådahle5a1bb42014-11-25 10:25:27 +080086#define ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION 1
87#define ZWP_POINTER_CONSTRAINTS_V1_VERSION 1
88
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070089struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030090
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040091struct global {
92 uint32_t name;
93 char *interface;
94 uint32_t version;
95 struct wl_list link;
96};
97
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050098struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050099 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400100 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500101 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300102 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400103 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400104 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600105 struct text_cursor_position *text_cursor_position;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500106 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900107 struct ivi_application *ivi_application; /* ivi style shell */
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800108 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
109 struct zwp_pointer_constraints_v1 *pointer_constraints;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400110 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500111 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200112 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200113 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400114 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400115
116 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700117 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400118 struct task display_task;
119
120 int epoll_fd;
121 struct wl_list deferred_list;
122
Pekka Paalanen826d7952011-12-15 10:14:07 +0200123 int running;
124
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400125 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400126 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400127 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500128 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400129
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400130 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400131
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300132 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300133 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400134
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200135 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400136 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800137 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200138
139 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100140
141 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200142
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200143 /* A hack to get text extents for tooltips */
144 cairo_surface_t *dummy_surface;
145 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200146
147 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530148 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500149};
150
Rob Bradford7507b572012-05-15 17:55:34 +0100151struct window_output {
152 struct output *output;
153 struct wl_list link;
154};
155
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156struct toysurface {
157 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500158 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800159 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200161 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200162 * If flags has SURFACE_HINT_RESIZE set, the user is
163 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164 * Returns the Cairo surface to draw to.
165 */
166 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200167 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200168 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200169
170 /*
171 * Post the surface to the server, returning the server allocation
172 * rectangle. The Cairo surface from prepare() must be destroyed
173 * after calling this.
174 */
175 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200176 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200177 struct rectangle *server_allocation);
178
179 /*
180 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800181 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200182 */
183 int (*acquire)(struct toysurface *base, EGLContext ctx);
184
185 /*
186 * Release the toysurface from the EGL context, returning control
187 * to Cairo.
188 */
189 void (*release)(struct toysurface *base);
190
191 /*
192 * Destroy the toysurface, including the Cairo surface, any
193 * backing storage, and the Wayland protocol objects.
194 */
195 void (*destroy)(struct toysurface *base);
196};
197
Pekka Paalanen4e373742013-02-13 16:17:13 +0200198struct surface {
199 struct window *window;
200
201 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300202 struct wl_subsurface *subsurface;
203 int synchronized;
204 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200205 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200206 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300207 int redraw_needed;
208 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300209 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200210
211 struct rectangle allocation;
212 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200213
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200214 struct wl_region *input_region;
215 struct wl_region *opaque_region;
216
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200217 enum window_buffer_type buffer_type;
218 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200219 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200220
221 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300222
223 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200224};
225
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500226struct window {
227 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100228 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500229 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200230 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400231 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500232 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200233 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500234 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400235 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300236 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400237 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400238 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500239 int custom;
240 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400241
Pekka Paalanen99436862012-11-19 17:15:59 +0200242 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400243
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500244 int fullscreen;
245 int maximized;
246
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200247 enum preferred_format preferred_format;
248
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500249 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500250 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400251 window_data_handler_t data_handler;
252 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500253 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400254 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200255 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700256 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400257
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800258 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
259
Pekka Paalanen4e373742013-02-13 16:17:13 +0200260 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500261 struct xdg_surface *xdg_surface;
262 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300263
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700264 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800265 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500266
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900267 struct ivi_surface *ivi_surface;
268
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500269 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500270
Pekka Paalanen35e82632013-04-25 13:57:48 +0300271 /* struct surface::link, contains also main_surface */
272 struct wl_list subsurface_list;
273
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800274 struct zwp_relative_pointer_v1 *relative_pointer;
275 struct zwp_locked_pointer_v1 *locked_pointer;
276 struct input *locked_input;
277 bool pointer_locked;
278 locked_pointer_locked_handler_t pointer_locked_handler;
279 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
280 confined_pointer_confined_handler_t pointer_confined_handler;
281 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
282
283 struct zwp_confined_pointer_v1 *confined_pointer;
284 struct widget *confined_widget;
285 bool confined;
286
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500287 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400288 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500289};
290
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500291struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500292 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200293 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300294 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500295 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400296 struct wl_list link;
297 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500298 widget_resize_handler_t resize_handler;
299 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500300 widget_enter_handler_t enter_handler;
301 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500302 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500303 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 widget_touch_down_handler_t touch_down_handler;
305 widget_touch_up_handler_t touch_up_handler;
306 widget_touch_motion_handler_t touch_motion_handler;
307 widget_touch_frame_handler_t touch_frame_handler;
308 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200309 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000310 widget_pointer_frame_handler_t pointer_frame_handler;
311 widget_axis_source_handler_t axis_source_handler;
312 widget_axis_stop_handler_t axis_stop_handler;
313 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400314 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500315 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300316 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500317 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000318 /* If this is set to false then no cairo surface will be
319 * created before redrawing the surface. This is useful if the
320 * redraw handler is going to do completely custom rendering
321 * such as using EGL directly */
322 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400323};
324
Rusty Lynch041815a2013-08-08 21:20:38 -0700325struct touch_point {
326 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800327 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700328 struct widget *widget;
329 struct wl_list link;
330};
331
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400332struct input {
333 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100334 struct wl_seat *seat;
335 struct wl_pointer *pointer;
336 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700337 struct wl_touch *touch;
338 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400339 struct window *pointer_focus;
340 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700341 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300342 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300343 uint32_t cursor_anim_start;
344 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500345 uint32_t cursor_timer_start;
346 uint32_t cursor_anim_current;
347 int cursor_delay_fd;
348 bool cursor_timer_running;
349 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300350 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400351 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400352 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400353 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400354 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400355 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400356
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500357 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500358 struct widget *grab;
359 uint32_t grab_button;
360
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400361 struct wl_data_device *data_device;
362 struct data_offer *drag_offer;
363 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800364 uint32_t touch_grab;
365 int32_t touch_grab_id;
366 float drag_x, drag_y;
367 struct window *drag_focus;
368 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100369
370 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100371 struct xkb_keymap *keymap;
372 struct xkb_state *state;
373 xkb_mod_mask_t control_mask;
374 xkb_mod_mask_t alt_mask;
375 xkb_mod_mask_t shift_mask;
376 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400377
Jonny Lamb06959082014-08-12 14:58:27 +0200378 int32_t repeat_rate_sec;
379 int32_t repeat_rate_nsec;
380 int32_t repeat_delay_sec;
381 int32_t repeat_delay_nsec;
382
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400383 struct task repeat_task;
384 int repeat_timer_fd;
385 uint32_t repeat_sym;
386 uint32_t repeat_key;
387 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500388 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400389};
390
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500391struct output {
392 struct display *display;
393 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800394 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500395 struct rectangle allocation;
396 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600397 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200398 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500399 char *make;
400 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200401
402 display_output_handler_t destroy_handler;
403 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500404};
405
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500406struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500407 struct widget *widget;
408 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500409 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800410
411 uint32_t last_time;
412 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800413 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500414};
415
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500416struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400417 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500418 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500419 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500420 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700421 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500422 const char **entries;
423 uint32_t time;
424 int current;
425 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400426 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500427 menu_func_t func;
428};
429
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300430struct tooltip {
431 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300432 struct widget *widget;
433 char *entry;
434 struct task tooltip_task;
435 int tooltip_fd;
436 float x, y;
437};
438
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700439struct shm_pool {
440 struct wl_shm_pool *pool;
441 size_t size;
442 size_t used;
443 void *data;
444};
445
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400446enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300447 CURSOR_DEFAULT = 100,
448 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400449};
450
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200451static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400452
Pekka Paalanen97777442013-05-22 10:20:05 +0300453/* #define DEBUG */
454
455#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300456
457static void
458debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
459__attribute__ ((format (printf, 4, 5)));
460
461static void
462debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
463{
464 va_list ap;
465 struct timeval tv;
466
467 gettimeofday(&tv, NULL);
468 fprintf(stderr, "%8ld.%03ld ",
469 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
470
471 if (proxy)
472 fprintf(stderr, "%s@%d ",
473 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
474
475 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
476 fprintf(stderr, "%s ", func);
477
478 va_start(ap, fmt);
479 vfprintf(stderr, fmt, ap);
480 va_end(ap);
481}
482
483#define DBG(fmt, ...) \
484 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
485
486#define DBG_OBJ(obj, fmt, ...) \
487 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
488
489#else
490
491#define DBG(...) do {} while (0)
492#define DBG_OBJ(...) do {} while (0)
493
494#endif
495
Alexander Larsson1818e312013-05-22 14:41:31 +0200496static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200497surface_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 +0200498{
499 int32_t tmp;
500
501 switch (buffer_transform) {
502 case WL_OUTPUT_TRANSFORM_90:
503 case WL_OUTPUT_TRANSFORM_270:
504 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
505 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
506 tmp = *width;
507 *width = *height;
508 *height = tmp;
509 break;
510 default:
511 break;
512 }
513
514 *width *= buffer_scale;
515 *height *= buffer_scale;
516}
517
518static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200519buffer_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 +0200520{
521 int32_t tmp;
522
523 switch (buffer_transform) {
524 case WL_OUTPUT_TRANSFORM_90:
525 case WL_OUTPUT_TRANSFORM_270:
526 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
527 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
528 tmp = *width;
529 *width = *height;
530 *height = tmp;
531 break;
532 default:
533 break;
534 }
535
536 *width /= buffer_scale;
537 *height /= buffer_scale;
538}
539
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500540#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400541
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200542struct egl_window_surface {
543 struct toysurface base;
544 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100545 struct display *display;
546 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547 struct wl_egl_window *egl_window;
548 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100549};
550
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551static struct egl_window_surface *
552to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100553{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200554 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100555}
556
557static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200558egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200559 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200560 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100561{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200562 struct egl_window_surface *surface = to_egl_window_surface(base);
563
Alexander Larsson1818e312013-05-22 14:41:31 +0200564 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
565
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200566 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
567 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
568
569 return cairo_surface_reference(surface->cairo_surface);
570}
571
572static void
573egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200574 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200575 struct rectangle *server_allocation)
576{
577 struct egl_window_surface *surface = to_egl_window_surface(base);
578
579 cairo_gl_surface_swapbuffers(surface->cairo_surface);
580 wl_egl_window_get_attached_size(surface->egl_window,
581 &server_allocation->width,
582 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200583
584 buffer_to_surface_size (buffer_transform, buffer_scale,
585 &server_allocation->width,
586 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200587}
588
589static int
590egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
591{
592 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200593 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100594
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200595 device = cairo_surface_get_device(surface->cairo_surface);
596 if (!device)
597 return -1;
598
599 if (!ctx) {
600 if (device == surface->display->argb_device)
601 ctx = surface->display->argb_ctx;
602 else
603 assert(0);
604 }
605
606 cairo_device_flush(device);
607 cairo_device_acquire(device);
608 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
609 surface->egl_surface, ctx))
610 fprintf(stderr, "failed to make surface current\n");
611
612 return 0;
613}
614
615static void
616egl_window_surface_release(struct toysurface *base)
617{
618 struct egl_window_surface *surface = to_egl_window_surface(base);
619 cairo_device_t *device;
620
621 device = cairo_surface_get_device(surface->cairo_surface);
622 if (!device)
623 return;
624
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200625 if (!eglMakeCurrent(surface->display->dpy,
626 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200627 fprintf(stderr, "failed to make context current\n");
628
629 cairo_device_release(device);
630}
631
632static void
633egl_window_surface_destroy(struct toysurface *base)
634{
635 struct egl_window_surface *surface = to_egl_window_surface(base);
636 struct display *d = surface->display;
637
638 cairo_surface_destroy(surface->cairo_surface);
639 eglDestroySurface(d->dpy, surface->egl_surface);
640 wl_egl_window_destroy(surface->egl_window);
641 surface->surface = NULL;
642
643 free(surface);
644}
645
646static struct toysurface *
647egl_window_surface_create(struct display *display,
648 struct wl_surface *wl_surface,
649 uint32_t flags,
650 struct rectangle *rectangle)
651{
652 struct egl_window_surface *surface;
653
Pekka Paalanenb3627362012-11-19 17:16:00 +0200654 if (display->dpy == EGL_NO_DISPLAY)
655 return NULL;
656
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800657 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200658 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100659 return NULL;
660
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200661 surface->base.prepare = egl_window_surface_prepare;
662 surface->base.swap = egl_window_surface_swap;
663 surface->base.acquire = egl_window_surface_acquire;
664 surface->base.release = egl_window_surface_release;
665 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100666
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200667 surface->display = display;
668 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400669
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200670 surface->egl_window = wl_egl_window_create(surface->surface,
671 rectangle->width,
672 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100673
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100674 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100675 weston_platform_create_egl_surface(display->dpy,
676 display->argb_config,
677 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100678
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200679 surface->cairo_surface =
680 cairo_gl_surface_create_for_egl(display->argb_device,
681 surface->egl_surface,
682 rectangle->width,
683 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100684
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200685 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100686}
687
Pekka Paalanenb3627362012-11-19 17:16:00 +0200688#else
689
690static struct toysurface *
691egl_window_surface_create(struct display *display,
692 struct wl_surface *wl_surface,
693 uint32_t flags,
694 struct rectangle *rectangle)
695{
696 return NULL;
697}
698
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400699#endif
700
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200701struct shm_surface_data {
702 struct wl_buffer *buffer;
703 struct shm_pool *pool;
704};
705
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706struct wl_buffer *
707display_get_buffer_for_surface(struct display *display,
708 cairo_surface_t *surface)
709{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200710 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400711
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200712 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400713
714 return data->buffer;
715}
716
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500717static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700718shm_pool_destroy(struct shm_pool *pool);
719
720static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400721shm_surface_data_destroy(void *p)
722{
723 struct shm_surface_data *data = p;
724
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200725 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700726 if (data->pool)
727 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300728
729 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400730}
731
Kristian Høgsberg16626282012-04-03 11:21:27 -0400732static struct wl_shm_pool *
733make_shm_pool(struct display *display, int size, void **data)
734{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400735 struct wl_shm_pool *pool;
736 int fd;
737
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300738 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400739 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300740 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
741 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400742 return NULL;
743 }
744
745 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400746 if (*data == MAP_FAILED) {
747 fprintf(stderr, "mmap failed: %m\n");
748 close(fd);
749 return NULL;
750 }
751
752 pool = wl_shm_create_pool(display->shm, fd, size);
753
754 close(fd);
755
756 return pool;
757}
758
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700759static struct shm_pool *
760shm_pool_create(struct display *display, size_t size)
761{
762 struct shm_pool *pool = malloc(sizeof *pool);
763
764 if (!pool)
765 return NULL;
766
767 pool->pool = make_shm_pool(display, size, &pool->data);
768 if (!pool->pool) {
769 free(pool);
770 return NULL;
771 }
772
773 pool->size = size;
774 pool->used = 0;
775
776 return pool;
777}
778
779static void *
780shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
781{
782 if (pool->used + size > pool->size)
783 return NULL;
784
785 *offset = pool->used;
786 pool->used += size;
787
788 return (char *) pool->data + *offset;
789}
790
791/* destroy the pool. this does not unmap the memory though */
792static void
793shm_pool_destroy(struct shm_pool *pool)
794{
795 munmap(pool->data, pool->size);
796 wl_shm_pool_destroy(pool->pool);
797 free(pool);
798}
799
800/* Start allocating from the beginning of the pool again */
801static void
802shm_pool_reset(struct shm_pool *pool)
803{
804 pool->used = 0;
805}
806
807static int
808data_length_for_shm_surface(struct rectangle *rect)
809{
810 int stride;
811
812 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
813 rect->width);
814 return stride * rect->height;
815}
816
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500817static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700818display_create_shm_surface_from_pool(struct display *display,
819 struct rectangle *rectangle,
820 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821{
822 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400823 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200825 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700826 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400827 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400828
829 data = malloc(sizeof *data);
830 if (data == NULL)
831 return NULL;
832
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200833 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
834 cairo_format = CAIRO_FORMAT_RGB16_565;
835 else
836 cairo_format = CAIRO_FORMAT_ARGB32;
837
838 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700839 length = stride * rectangle->height;
840 data->pool = NULL;
841 map = shm_pool_allocate(pool, length, &offset);
842
843 if (!map) {
844 free(data);
845 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400846 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400848 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200849 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400850 rectangle->width,
851 rectangle->height,
852 stride);
853
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200854 cairo_surface_set_user_data(surface, &shm_surface_data_key,
855 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400856
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200857 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
858 format = WL_SHM_FORMAT_RGB565;
859 else {
860 if (flags & SURFACE_OPAQUE)
861 format = WL_SHM_FORMAT_XRGB8888;
862 else
863 format = WL_SHM_FORMAT_ARGB8888;
864 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400865
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200866 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
867 rectangle->width,
868 rectangle->height,
869 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400870
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700871 return surface;
872}
873
874static cairo_surface_t *
875display_create_shm_surface(struct display *display,
876 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200877 struct shm_pool *alternate_pool,
878 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700879{
880 struct shm_surface_data *data;
881 struct shm_pool *pool;
882 cairo_surface_t *surface;
883
Pekka Paalanen99436862012-11-19 17:15:59 +0200884 if (alternate_pool) {
885 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700886 surface = display_create_shm_surface_from_pool(display,
887 rectangle,
888 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200889 alternate_pool);
890 if (surface) {
891 data = cairo_surface_get_user_data(surface,
892 &shm_surface_data_key);
893 goto out;
894 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700895 }
896
897 pool = shm_pool_create(display,
898 data_length_for_shm_surface(rectangle));
899 if (!pool)
900 return NULL;
901
902 surface =
903 display_create_shm_surface_from_pool(display, rectangle,
904 flags, pool);
905
906 if (!surface) {
907 shm_pool_destroy(pool);
908 return NULL;
909 }
910
911 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200912 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700913 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400914
Pekka Paalanen99436862012-11-19 17:15:59 +0200915out:
916 if (data_ret)
917 *data_ret = data;
918
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400919 return surface;
920}
921
nobled7b87cb02011-02-01 18:51:47 +0000922static int
923check_size(struct rectangle *rect)
924{
925 if (rect->width && rect->height)
926 return 0;
927
928 fprintf(stderr, "tried to create surface of "
929 "width: %d, height: %d\n", rect->width, rect->height);
930 return -1;
931}
932
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400933cairo_surface_t *
934display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100935 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400936 struct rectangle *rectangle,
937 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400938{
nobled7b87cb02011-02-01 18:51:47 +0000939 if (check_size(rectangle) < 0)
940 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200941
942 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200943 return display_create_shm_surface(display, rectangle, flags,
944 NULL, NULL);
945}
946
Pekka Paalanena4eda732012-11-19 17:16:02 +0200947struct shm_surface_leaf {
948 cairo_surface_t *cairo_surface;
949 /* 'data' is automatically destroyed, when 'cairo_surface' is */
950 struct shm_surface_data *data;
951
952 struct shm_pool *resize_pool;
953 int busy;
954};
955
956static void
957shm_surface_leaf_release(struct shm_surface_leaf *leaf)
958{
959 if (leaf->cairo_surface)
960 cairo_surface_destroy(leaf->cairo_surface);
961 /* leaf->data already destroyed via cairo private */
962
963 if (leaf->resize_pool)
964 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200965
966 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200967}
968
Pekka Paalanenaef02542013-04-25 13:57:47 +0300969#define MAX_LEAVES 3
970
Pekka Paalanen99436862012-11-19 17:15:59 +0200971struct shm_surface {
972 struct toysurface base;
973 struct display *display;
974 struct wl_surface *surface;
975 uint32_t flags;
976 int dx, dy;
977
Pekka Paalanenaef02542013-04-25 13:57:47 +0300978 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200979 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200980};
981
982static struct shm_surface *
983to_shm_surface(struct toysurface *base)
984{
985 return container_of(base, struct shm_surface, base);
986}
987
Pekka Paalanena4eda732012-11-19 17:16:02 +0200988static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300989shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
990{
991#ifdef DEBUG
992 struct shm_surface_leaf *leaf;
993 char bufs[MAX_LEAVES + 1];
994 int i;
995
996 for (i = 0; i < MAX_LEAVES; i++) {
997 leaf = &surface->leaf[i];
998
999 if (leaf->busy)
1000 bufs[i] = 'b';
1001 else if (leaf->cairo_surface)
1002 bufs[i] = 'a';
1003 else
1004 bufs[i] = ' ';
1005 }
1006
1007 bufs[MAX_LEAVES] = '\0';
1008 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1009#endif
1010}
1011
1012static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001013shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1014{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001015 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001016 struct shm_surface_leaf *leaf;
1017 int i;
1018 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001019
1020 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001021
Pekka Paalanenaef02542013-04-25 13:57:47 +03001022 for (i = 0; i < MAX_LEAVES; i++) {
1023 leaf = &surface->leaf[i];
1024 if (leaf->data && leaf->data->buffer == buffer) {
1025 leaf->busy = 0;
1026 break;
1027 }
1028 }
1029 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001030
Pekka Paalanenaef02542013-04-25 13:57:47 +03001031 /* Leave one free leaf with storage, release others */
1032 free_found = 0;
1033 for (i = 0; i < MAX_LEAVES; i++) {
1034 leaf = &surface->leaf[i];
1035
1036 if (!leaf->cairo_surface || leaf->busy)
1037 continue;
1038
1039 if (!free_found)
1040 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001041 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001042 shm_surface_leaf_release(leaf);
1043 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001044
Pekka Paalanen97777442013-05-22 10:20:05 +03001045 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001046}
1047
1048static const struct wl_buffer_listener shm_surface_buffer_listener = {
1049 shm_surface_buffer_release
1050};
1051
Pekka Paalanen99436862012-11-19 17:15:59 +02001052static cairo_surface_t *
1053shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001054 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001055 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001056{
Pekka Paalanenec076692012-11-30 13:37:27 +02001057 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001058 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001059 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001060 struct shm_surface_leaf *leaf = NULL;
1061 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001062
1063 surface->dx = dx;
1064 surface->dy = dy;
1065
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001066 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001067 for (i = 0; i < MAX_LEAVES; i++) {
1068 if (surface->leaf[i].busy)
1069 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001070
Pekka Paalanenaef02542013-04-25 13:57:47 +03001071 if (!leaf || surface->leaf[i].cairo_surface)
1072 leaf = &surface->leaf[i];
1073 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001074 DBG_OBJ(surface->surface, "pick leaf %d\n",
1075 (int)(leaf - &surface->leaf[0]));
1076
Pekka Paalanenaef02542013-04-25 13:57:47 +03001077 if (!leaf) {
1078 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001080 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001081 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001082 }
1083
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 if (!resize_hint && leaf->resize_pool) {
1085 cairo_surface_destroy(leaf->cairo_surface);
1086 leaf->cairo_surface = NULL;
1087 shm_pool_destroy(leaf->resize_pool);
1088 leaf->resize_pool = NULL;
1089 }
1090
Alexander Larsson1818e312013-05-22 14:41:31 +02001091 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 if (leaf->cairo_surface &&
1094 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1095 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001096 goto out;
1097
Pekka Paalanena4eda732012-11-19 17:16:02 +02001098 if (leaf->cairo_surface)
1099 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001100
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001101#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001102 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001103 /* Create a big pool to allocate from, while continuously
1104 * resizing. Mmapping a new pool in the server
1105 * is relatively expensive, so reusing a pool performs
1106 * better, but may temporarily reserve unneeded memory.
1107 */
1108 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001109 leaf->resize_pool = shm_pool_create(surface->display,
1110 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001111 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001112#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001113
Alexander Larsson1818e312013-05-22 14:41:31 +02001114 rect.width = width;
1115 rect.height = height;
1116
Pekka Paalanena4eda732012-11-19 17:16:02 +02001117 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001118 display_create_shm_surface(surface->display, &rect,
1119 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 leaf->resize_pool,
1121 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001122 if (!leaf->cairo_surface)
1123 return NULL;
1124
Pekka Paalanena4eda732012-11-19 17:16:02 +02001125 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001126 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001127
1128out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001129 surface->current = leaf;
1130
1131 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001132}
1133
1134static void
1135shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001136 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001137 struct rectangle *server_allocation)
1138{
1139 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001140 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001141
1142 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001143 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001144 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001145 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001146
Alexander Larsson1818e312013-05-22 14:41:31 +02001147 buffer_to_surface_size (buffer_transform, buffer_scale,
1148 &server_allocation->width,
1149 &server_allocation->height);
1150
Pekka Paalanena4eda732012-11-19 17:16:02 +02001151 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001152 surface->dx, surface->dy);
1153 wl_surface_damage(surface->surface, 0, 0,
1154 server_allocation->width, server_allocation->height);
1155 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001156
Pekka Paalanen71233882013-04-25 13:57:53 +03001157 DBG_OBJ(surface->surface, "leaf %d busy\n",
1158 (int)(leaf - &surface->leaf[0]));
1159
Pekka Paalanena4eda732012-11-19 17:16:02 +02001160 leaf->busy = 1;
1161 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001162}
1163
1164static int
1165shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1166{
1167 return -1;
1168}
1169
1170static void
1171shm_surface_release(struct toysurface *base)
1172{
1173}
1174
1175static void
1176shm_surface_destroy(struct toysurface *base)
1177{
1178 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001179 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001180
Pekka Paalanenaef02542013-04-25 13:57:47 +03001181 for (i = 0; i < MAX_LEAVES; i++)
1182 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001183
1184 free(surface);
1185}
1186
1187static struct toysurface *
1188shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1189 uint32_t flags, struct rectangle *rectangle)
1190{
1191 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001192 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001193
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001194 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001195 surface->base.prepare = shm_surface_prepare;
1196 surface->base.swap = shm_surface_swap;
1197 surface->base.acquire = shm_surface_acquire;
1198 surface->base.release = shm_surface_release;
1199 surface->base.destroy = shm_surface_destroy;
1200
1201 surface->display = display;
1202 surface->surface = wl_surface;
1203 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001204
1205 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001206}
1207
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001208/*
1209 * The following correspondences between file names and cursors was copied
1210 * from: https://bugs.kde.org/attachment.cgi?id=67313
1211 */
1212
1213static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001215 "sw-resize",
1216 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001217};
1218
1219static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001220 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001221 "se-resize",
1222 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001223};
1224
1225static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001227 "s-resize",
1228 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229};
1230
1231static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001233 "closedhand",
1234 "208530c400c041818281048008011002"
1235};
1236
1237static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001239 "default",
1240 "top_left_arrow",
1241 "left-arrow"
1242};
1243
1244static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001246 "w-resize",
1247 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248};
1249
1250static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001251 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001252 "e-resize",
1253 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001254};
1255
1256static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001257 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001258 "nw-resize",
1259 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001260};
1261
1262static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001263 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001264 "ne-resize",
1265 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001266};
1267
1268static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001269 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001270 "n-resize",
1271 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001272};
1273
1274static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001275 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001276 "ibeam",
1277 "text"
1278};
1279
1280static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001281 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001282 "pointer",
1283 "pointing_hand",
1284 "e29285e634086352946a0e7090d73106"
1285};
1286
1287static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001288 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001289 "wait",
1290 "0426c94ea35c87780ff01dc239897213"
1291};
1292
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001293static const char *move_draggings[] = {
1294 "dnd-move"
1295};
1296
1297static const char *copy_draggings[] = {
1298 "dnd-copy"
1299};
1300
1301static const char *forbidden_draggings[] = {
1302 "dnd-none",
1303 "dnd-no-drop"
1304};
1305
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001306struct cursor_alternatives {
1307 const char **names;
1308 size_t count;
1309};
1310
1311static const struct cursor_alternatives cursors[] = {
1312 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1313 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1314 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1315 {grabbings, ARRAY_LENGTH(grabbings)},
1316 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1317 {left_sides, ARRAY_LENGTH(left_sides)},
1318 {right_sides, ARRAY_LENGTH(right_sides)},
1319 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1320 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1321 {top_sides, ARRAY_LENGTH(top_sides)},
1322 {xterms, ARRAY_LENGTH(xterms)},
1323 {hand1s, ARRAY_LENGTH(hand1s)},
1324 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001325 {move_draggings, ARRAY_LENGTH(move_draggings)},
1326 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1327 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001328};
1329
1330static void
1331create_cursors(struct display *display)
1332{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001333 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001334 struct weston_config *config;
1335 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001336 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001337 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001338 unsigned int i, j;
1339 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001340
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001341 config_file = weston_config_get_name_from_env();
1342 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001343 s = weston_config_get_section(config, "shell", NULL, NULL);
1344 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1345 weston_config_section_get_int(s, "cursor-size", &size, 32);
1346 weston_config_destroy(config);
1347
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001348 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001349 if (!display->cursor_theme) {
1350 fprintf(stderr, "could not load theme '%s'\n", theme);
1351 return;
1352 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001353 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001354 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001355 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001356
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001357 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001358 cursor = NULL;
1359 for (j = 0; !cursor && j < cursors[i].count; ++j)
1360 cursor = wl_cursor_theme_get_cursor(
1361 display->cursor_theme, cursors[i].names[j]);
1362
1363 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001364 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001365 cursors[i].names[0]);
1366
1367 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001368 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001369}
1370
1371static void
1372destroy_cursors(struct display *display)
1373{
1374 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001375 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001376}
1377
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001378struct wl_cursor_image *
1379display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001380{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001381 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001382
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001383 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001384}
1385
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001386static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001387surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001388{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001389 if (!surface->cairo_surface)
1390 return;
1391
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001392 if (surface->opaque_region) {
1393 wl_surface_set_opaque_region(surface->surface,
1394 surface->opaque_region);
1395 wl_region_destroy(surface->opaque_region);
1396 surface->opaque_region = NULL;
1397 }
1398
1399 if (surface->input_region) {
1400 wl_surface_set_input_region(surface->surface,
1401 surface->input_region);
1402 wl_region_destroy(surface->input_region);
1403 surface->input_region = NULL;
1404 }
1405
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001406 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001407 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001409
Pekka Paalanen89dee002013-02-13 16:17:20 +02001410 cairo_surface_destroy(surface->cairo_surface);
1411 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001412}
1413
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001414int
1415window_has_focus(struct window *window)
1416{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001417 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001418}
1419
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001420static void
1421window_close(struct window *window)
1422{
1423 if (window->close_handler)
1424 window->close_handler(window->user_data);
1425 else
1426 display_exit(window->display);
1427}
1428
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001429struct display *
1430window_get_display(struct window *window)
1431{
1432 return window->display;
1433}
1434
Pekka Paalanen89dee002013-02-13 16:17:20 +02001435static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001436handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1437 int32_t width, int32_t height)
1438{
1439 struct window *window = data;
1440
1441 window_schedule_resize(window, width, height);
1442}
1443
1444static const struct ivi_surface_listener ivi_surface_listener = {
1445 handle_ivi_surface_configure,
1446};
1447
1448static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001449surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001450{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001452 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001453
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001454 if (!surface->toysurface && display->dpy &&
1455 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001456 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001457 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001458 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001459 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001460 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001461 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001462
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001463 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001464 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001465 surface->surface,
1466 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001467
Pekka Paalanen89dee002013-02-13 16:17:20 +02001468 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001469 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001470 allocation.width, allocation.height, flags,
1471 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001472}
1473
1474static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001475window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001476{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001477 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001478 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001479
Pekka Paalanenec076692012-11-30 13:37:27 +02001480 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001481 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001482
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001483 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1484 flags |= SURFACE_HINT_RGB565;
1485
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001486 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001487}
1488
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001489int
1490window_get_buffer_transform(struct window *window)
1491{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001492 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001493}
1494
1495void
1496window_set_buffer_transform(struct window *window,
1497 enum wl_output_transform transform)
1498{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001499 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001500 wl_surface_set_buffer_transform(window->main_surface->surface,
1501 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001502}
1503
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001504void
1505window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001506 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001507{
1508 window->main_surface->buffer_scale = scale;
1509 wl_surface_set_buffer_scale(window->main_surface->surface,
1510 scale);
1511}
1512
1513uint32_t
1514window_get_buffer_scale(struct window *window)
1515{
1516 return window->main_surface->buffer_scale;
1517}
1518
Alexander Larssond68f5232013-05-22 14:41:33 +02001519uint32_t
1520window_get_output_scale(struct window *window)
1521{
1522 struct window_output *window_output;
1523 struct window_output *window_output_tmp;
1524 int scale = 1;
1525
1526 wl_list_for_each_safe(window_output, window_output_tmp,
1527 &window->window_output_list, link) {
1528 if (window_output->output->scale > scale)
1529 scale = window_output->output->scale;
1530 }
1531
1532 return scale;
1533}
1534
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001535static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001536
Pekka Paalanen4e373742013-02-13 16:17:13 +02001537static void
1538surface_destroy(struct surface *surface)
1539{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001540 if (surface->frame_cb)
1541 wl_callback_destroy(surface->frame_cb);
1542
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001543 if (surface->input_region)
1544 wl_region_destroy(surface->input_region);
1545
1546 if (surface->opaque_region)
1547 wl_region_destroy(surface->opaque_region);
1548
Pekka Paalanen35e82632013-04-25 13:57:48 +03001549 if (surface->subsurface)
1550 wl_subsurface_destroy(surface->subsurface);
1551
Pekka Paalanen4e373742013-02-13 16:17:13 +02001552 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001553
1554 if (surface->toysurface)
1555 surface->toysurface->destroy(surface->toysurface);
1556
Pekka Paalanen35e82632013-04-25 13:57:48 +03001557 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001558 free(surface);
1559}
1560
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001561void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001562window_destroy(struct window *window)
1563{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001564 struct display *display = window->display;
1565 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001566 struct window_output *window_output;
1567 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001568
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001569 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001570
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001571 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001572 if (input->touch_focus == window)
1573 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001574 if (input->pointer_focus == window)
1575 input->pointer_focus = NULL;
1576 if (input->keyboard_focus == window)
1577 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001578 if (input->focus_widget &&
1579 input->focus_widget->window == window)
1580 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001581 }
1582
Rob Bradford7507b572012-05-15 17:55:34 +01001583 wl_list_for_each_safe(window_output, window_output_tmp,
1584 &window->window_output_list, link) {
1585 free (window_output);
1586 }
1587
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001588 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001589 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001590
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001591 if (window->xdg_surface)
1592 xdg_surface_destroy(window->xdg_surface);
1593 if (window->xdg_popup)
1594 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001595
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001596 if (window->ivi_surface)
1597 ivi_surface_destroy(window->ivi_surface);
1598
Pekka Paalanen4e373742013-02-13 16:17:13 +02001599 surface_destroy(window->main_surface);
1600
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001601 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001602
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001603 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001604 free(window);
1605}
1606
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001607static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001608widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001609{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001610 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001611
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612 wl_list_for_each(child, &widget->child_list, link) {
1613 target = widget_find_widget(child, x, y);
1614 if (target)
1615 return target;
1616 }
1617
1618 if (widget->allocation.x <= x &&
1619 x < widget->allocation.x + widget->allocation.width &&
1620 widget->allocation.y <= y &&
1621 y < widget->allocation.y + widget->allocation.height) {
1622 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001623 }
1624
1625 return NULL;
1626}
1627
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001628static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001629window_find_widget(struct window *window, int32_t x, int32_t y)
1630{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001631 struct surface *surface;
1632 struct widget *widget;
1633
1634 wl_list_for_each(surface, &window->subsurface_list, link) {
1635 widget = widget_find_widget(surface->widget, x, y);
1636 if (widget)
1637 return widget;
1638 }
1639
1640 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001641}
1642
1643static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001644widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001645{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001646 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647
Peter Huttererf3d62272013-08-08 11:57:05 +10001648 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001649 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001650 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001651 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001652 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001653 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001654 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001655 widget->tooltip = NULL;
1656 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001657 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001658 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001659
1660 return widget;
1661}
1662
1663struct widget *
1664window_add_widget(struct window *window, void *data)
1665{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001666 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001667
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001668 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001669 wl_list_init(&widget->link);
1670 window->main_surface->widget = widget;
1671
1672 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001673}
1674
1675struct widget *
1676widget_add_widget(struct widget *parent, void *data)
1677{
1678 struct widget *widget;
1679
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001680 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001681 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001682
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001683 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001684}
1685
1686void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001687widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001688{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001689 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001690 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001691 struct input *input;
1692
Pekka Paalanen35e82632013-04-25 13:57:48 +03001693 /* Destroy the sub-surface along with the root widget */
1694 if (surface->widget == widget && surface->subsurface)
1695 surface_destroy(widget->surface);
1696
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001697 if (widget->tooltip)
1698 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001699
Pekka Paalanene156fb62012-01-19 13:51:38 +02001700 wl_list_for_each(input, &display->input_list, link) {
1701 if (input->focus_widget == widget)
1702 input->focus_widget = NULL;
1703 }
1704
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001705 wl_list_remove(&widget->link);
1706 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001707}
1708
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001709void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001710widget_set_default_cursor(struct widget *widget, int cursor)
1711{
1712 widget->default_cursor = cursor;
1713}
1714
1715void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001716widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001717{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001718 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001719}
1720
1721void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001722widget_set_size(struct widget *widget, int32_t width, int32_t height)
1723{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001724 widget->allocation.width = width;
1725 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001726}
1727
1728void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001729widget_set_allocation(struct widget *widget,
1730 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001731{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001732 widget->allocation.x = x;
1733 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001734 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001735}
1736
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001737void
1738widget_set_transparent(struct widget *widget, int transparent)
1739{
1740 widget->opaque = !transparent;
1741}
1742
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001743void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001744widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001745{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001746 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747}
1748
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001749static cairo_surface_t *
1750widget_get_cairo_surface(struct widget *widget)
1751{
1752 struct surface *surface = widget->surface;
1753 struct window *window = widget->window;
1754
Neil Roberts97b747c2013-12-19 16:17:12 +00001755 assert(widget->use_cairo);
1756
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001757 if (!surface->cairo_surface) {
1758 if (surface == window->main_surface)
1759 window_create_main_surface(window);
1760 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001761 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001762 }
1763
1764 return surface->cairo_surface;
1765}
1766
Alexander Larsson15901f02013-05-22 14:41:25 +02001767static void
1768widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1769{
1770 struct surface *surface = widget->surface;
1771 double angle;
1772 cairo_matrix_t m;
1773 enum wl_output_transform transform;
1774 int surface_width, surface_height;
1775 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001776 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001777
1778 surface_width = surface->allocation.width;
1779 surface_height = surface->allocation.height;
1780
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001781 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001782 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001783
Alexander Larsson15901f02013-05-22 14:41:25 +02001784 switch (transform) {
1785 case WL_OUTPUT_TRANSFORM_FLIPPED:
1786 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1787 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1789 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1790 break;
1791 default:
1792 cairo_matrix_init_identity(&m);
1793 break;
1794 }
1795
1796 switch (transform) {
1797 case WL_OUTPUT_TRANSFORM_NORMAL:
1798 default:
1799 angle = 0;
1800 translate_x = 0;
1801 translate_y = 0;
1802 break;
1803 case WL_OUTPUT_TRANSFORM_FLIPPED:
1804 angle = 0;
1805 translate_x = surface_width;
1806 translate_y = 0;
1807 break;
1808 case WL_OUTPUT_TRANSFORM_90:
1809 angle = M_PI_2;
1810 translate_x = surface_height;
1811 translate_y = 0;
1812 break;
1813 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1814 angle = M_PI_2;
1815 translate_x = surface_height;
1816 translate_y = surface_width;
1817 break;
1818 case WL_OUTPUT_TRANSFORM_180:
1819 angle = M_PI;
1820 translate_x = surface_width;
1821 translate_y = surface_height;
1822 break;
1823 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1824 angle = M_PI;
1825 translate_x = 0;
1826 translate_y = surface_height;
1827 break;
1828 case WL_OUTPUT_TRANSFORM_270:
1829 angle = M_PI + M_PI_2;
1830 translate_x = 0;
1831 translate_y = surface_width;
1832 break;
1833 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1834 angle = M_PI + M_PI_2;
1835 translate_x = 0;
1836 translate_y = 0;
1837 break;
1838 }
1839
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001840 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001841 cairo_translate(cr, translate_x, translate_y);
1842 cairo_rotate(cr, angle);
1843 cairo_transform(cr, &m);
1844}
1845
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001846cairo_t *
1847widget_cairo_create(struct widget *widget)
1848{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001849 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001850 cairo_surface_t *cairo_surface;
1851 cairo_t *cr;
1852
1853 cairo_surface = widget_get_cairo_surface(widget);
1854 cr = cairo_create(cairo_surface);
1855
Alexander Larsson15901f02013-05-22 14:41:25 +02001856 widget_cairo_update_transform(widget, cr);
1857
Pekka Paalanen35e82632013-04-25 13:57:48 +03001858 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1859
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001860 return cr;
1861}
1862
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001863struct wl_surface *
1864widget_get_wl_surface(struct widget *widget)
1865{
1866 return widget->surface->surface;
1867}
1868
Neil Roberts5e10a042013-09-09 00:40:17 +01001869struct wl_subsurface *
1870widget_get_wl_subsurface(struct widget *widget)
1871{
1872 return widget->surface->subsurface;
1873}
1874
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001875uint32_t
1876widget_get_last_time(struct widget *widget)
1877{
1878 return widget->surface->last_time;
1879}
1880
1881void
1882widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1883{
1884 struct wl_compositor *comp = widget->window->display->compositor;
1885 struct surface *surface = widget->surface;
1886
1887 if (!surface->input_region)
1888 surface->input_region = wl_compositor_create_region(comp);
1889
1890 if (rect) {
1891 wl_region_add(surface->input_region,
1892 rect->x, rect->y, rect->width, rect->height);
1893 }
1894}
1895
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001896void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001897widget_set_resize_handler(struct widget *widget,
1898 widget_resize_handler_t handler)
1899{
1900 widget->resize_handler = handler;
1901}
1902
1903void
1904widget_set_redraw_handler(struct widget *widget,
1905 widget_redraw_handler_t handler)
1906{
1907 widget->redraw_handler = handler;
1908}
1909
1910void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001911widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001912{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001913 widget->enter_handler = handler;
1914}
1915
1916void
1917widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1918{
1919 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001920}
1921
1922void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001923widget_set_motion_handler(struct widget *widget,
1924 widget_motion_handler_t handler)
1925{
1926 widget->motion_handler = handler;
1927}
1928
1929void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001930widget_set_button_handler(struct widget *widget,
1931 widget_button_handler_t handler)
1932{
1933 widget->button_handler = handler;
1934}
1935
1936void
Rusty Lynch041815a2013-08-08 21:20:38 -07001937widget_set_touch_up_handler(struct widget *widget,
1938 widget_touch_up_handler_t handler)
1939{
1940 widget->touch_up_handler = handler;
1941}
1942
1943void
1944widget_set_touch_down_handler(struct widget *widget,
1945 widget_touch_down_handler_t handler)
1946{
1947 widget->touch_down_handler = handler;
1948}
1949
1950void
1951widget_set_touch_motion_handler(struct widget *widget,
1952 widget_touch_motion_handler_t handler)
1953{
1954 widget->touch_motion_handler = handler;
1955}
1956
1957void
1958widget_set_touch_frame_handler(struct widget *widget,
1959 widget_touch_frame_handler_t handler)
1960{
1961 widget->touch_frame_handler = handler;
1962}
1963
1964void
1965widget_set_touch_cancel_handler(struct widget *widget,
1966 widget_touch_cancel_handler_t handler)
1967{
1968 widget->touch_cancel_handler = handler;
1969}
1970
1971void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001972widget_set_axis_handler(struct widget *widget,
1973 widget_axis_handler_t handler)
1974{
1975 widget->axis_handler = handler;
1976}
1977
Peter Hutterer87743e92016-01-18 16:38:22 +10001978void
1979widget_set_pointer_frame_handler(struct widget *widget,
1980 widget_pointer_frame_handler_t handler)
1981{
1982 widget->pointer_frame_handler = handler;
1983}
1984
1985void
1986widget_set_axis_handlers(struct widget *widget,
1987 widget_axis_handler_t axis_handler,
1988 widget_axis_source_handler_t axis_source_handler,
1989 widget_axis_stop_handler_t axis_stop_handler,
1990 widget_axis_discrete_handler_t axis_discrete_handler)
1991{
1992 widget->axis_handler = axis_handler;
1993 widget->axis_source_handler = axis_source_handler;
1994 widget->axis_stop_handler = axis_stop_handler;
1995 widget->axis_discrete_handler = axis_discrete_handler;
1996}
1997
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001998static void
1999window_schedule_redraw_task(struct window *window);
2000
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002001void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002002widget_schedule_redraw(struct widget *widget)
2003{
Pekka Paalanen71233882013-04-25 13:57:53 +03002004 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002005 widget->surface->redraw_needed = 1;
2006 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002007}
2008
Neil Roberts97b747c2013-12-19 16:17:12 +00002009void
2010widget_set_use_cairo(struct widget *widget,
2011 int use_cairo)
2012{
2013 widget->use_cairo = use_cairo;
2014}
2015
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002016cairo_surface_t *
2017window_get_surface(struct window *window)
2018{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002019 cairo_surface_t *cairo_surface;
2020
2021 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2022
2023 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002024}
2025
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002026struct wl_surface *
2027window_get_wl_surface(struct window *window)
2028{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002029 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002030}
2031
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002032static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002033tooltip_redraw_handler(struct widget *widget, void *data)
2034{
2035 cairo_t *cr;
2036 const int32_t r = 3;
2037 struct tooltip *tooltip = data;
2038 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002039
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002040 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002041 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002042 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2043 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2044 cairo_paint(cr);
2045
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002046 width = widget->allocation.width;
2047 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002048 rounded_rect(cr, 0, 0, width, height, r);
2049
2050 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2051 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2052 cairo_fill(cr);
2053
2054 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2055 cairo_move_to(cr, 10, 16);
2056 cairo_show_text(cr, tooltip->entry);
2057 cairo_destroy(cr);
2058}
2059
2060static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002061get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002062{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002063 cairo_t *cr;
2064 cairo_text_extents_t extents;
2065
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002066 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002067 * created yet, and parent does not have a valid surface
2068 * outside repaint, either.
2069 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002070 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002071 cairo_text_extents(cr, tooltip->entry, &extents);
2072 cairo_destroy(cr);
2073
2074 return extents;
2075}
2076
2077static int
2078window_create_tooltip(struct tooltip *tooltip)
2079{
2080 struct widget *parent = tooltip->parent;
2081 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002082 const int offset_y = 27;
2083 const int margin = 3;
2084 cairo_text_extents_t extents;
2085
2086 if (tooltip->widget)
2087 return 0;
2088
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002089 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002090
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002091 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002092 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002093 widget_set_allocation(tooltip->widget,
2094 tooltip->x, tooltip->y + offset_y,
2095 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002096
2097 return 0;
2098}
2099
2100void
2101widget_destroy_tooltip(struct widget *parent)
2102{
2103 struct tooltip *tooltip = parent->tooltip;
2104
2105 parent->tooltip_count = 0;
2106 if (!tooltip)
2107 return;
2108
2109 if (tooltip->widget) {
2110 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002111 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002112 }
2113
2114 close(tooltip->tooltip_fd);
2115 free(tooltip->entry);
2116 free(tooltip);
2117 parent->tooltip = NULL;
2118}
2119
2120static void
2121tooltip_func(struct task *task, uint32_t events)
2122{
2123 struct tooltip *tooltip =
2124 container_of(task, struct tooltip, tooltip_task);
2125 uint64_t exp;
2126
Martin Olsson8df662a2012-07-08 03:03:47 +02002127 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2128 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002129 window_create_tooltip(tooltip);
2130}
2131
2132#define TOOLTIP_TIMEOUT 500
2133static int
2134tooltip_timer_reset(struct tooltip *tooltip)
2135{
2136 struct itimerspec its;
2137
2138 its.it_interval.tv_sec = 0;
2139 its.it_interval.tv_nsec = 0;
2140 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2141 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2142 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2143 fprintf(stderr, "could not set timerfd\n: %m");
2144 return -1;
2145 }
2146
2147 return 0;
2148}
2149
2150int
2151widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2152{
2153 struct tooltip *tooltip = parent->tooltip;
2154
2155 parent->tooltip_count++;
2156 if (tooltip) {
2157 tooltip->x = x;
2158 tooltip->y = y;
2159 tooltip_timer_reset(tooltip);
2160 return 0;
2161 }
2162
2163 /* the handler might be triggered too fast via input device motion, so
2164 * we need this check here to make sure tooltip is fully initialized */
2165 if (parent->tooltip_count > 1)
2166 return 0;
2167
2168 tooltip = malloc(sizeof *tooltip);
2169 if (!tooltip)
2170 return -1;
2171
2172 parent->tooltip = tooltip;
2173 tooltip->parent = parent;
2174 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002175 tooltip->x = x;
2176 tooltip->y = y;
2177 tooltip->entry = strdup(entry);
2178 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2179 if (tooltip->tooltip_fd < 0) {
2180 fprintf(stderr, "could not create timerfd\n: %m");
2181 return -1;
2182 }
2183
2184 tooltip->tooltip_task.run = tooltip_func;
2185 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2186 EPOLLIN, &tooltip->tooltip_task);
2187 tooltip_timer_reset(tooltip);
2188
2189 return 0;
2190}
2191
2192static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002193frame_resize_handler(struct widget *widget,
2194 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002195{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002196 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 struct rectangle interior;
2199 struct rectangle input;
2200 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002201
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002202 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 interior.x = 0;
2204 interior.y = 0;
2205 interior.width = width;
2206 interior.height = height;
2207 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002208 frame_resize(frame->frame, width, height);
2209 frame_interior(frame->frame, &interior.x, &interior.y,
2210 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002211 }
2212
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002213 widget_set_allocation(child, interior.x, interior.y,
2214 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002215
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002216 if (child->resize_handler) {
2217 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002218 child->user_data);
2219
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002220 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002221 width = child->allocation.width;
2222 height = child->allocation.height;
2223 } else {
2224 frame_resize_inside(frame->frame,
2225 child->allocation.width,
2226 child->allocation.height);
2227 width = frame_width(frame->frame);
2228 height = frame_height(frame->frame);
2229 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002230 }
2231
Scott Moreauf7e498c2012-05-14 11:39:29 -06002232 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002233
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002234 widget->surface->input_region =
2235 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002236 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002237 frame_input_rect(frame->frame, &input.x, &input.y,
2238 &input.width, &input.height);
2239 wl_region_add(widget->surface->input_region,
2240 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002241 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002242 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002243 }
2244
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002245 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002246
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002247 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002248 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002249 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2250 &opaque.width, &opaque.height);
2251
2252 wl_region_add(widget->surface->opaque_region,
2253 opaque.x, opaque.y,
2254 opaque.width, opaque.height);
2255 } else {
2256 wl_region_add(widget->surface->opaque_region,
2257 0, 0, width, height);
2258 }
Martin Minarik1998b152012-05-10 02:04:35 +02002259 }
2260
Martin Minarik1998b152012-05-10 02:04:35 +02002261
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002262 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002263}
2264
2265static void
2266frame_redraw_handler(struct widget *widget, void *data)
2267{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002268 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002269 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002270 struct window *window = widget->window;
2271
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002272 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002273 return;
2274
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002275 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002276
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002277 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002278
2279 cairo_destroy(cr);
2280}
2281
2282static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002283frame_get_pointer_image_for_location(struct window_frame *frame,
2284 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002285{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002286 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002287
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002288 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002289 return CURSOR_LEFT_PTR;
2290
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002291 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002292 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002293 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002294 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002295 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002296 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002297 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002298 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002299 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002300 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002301 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002302 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002303 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002304 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002305 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002306 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002307 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002308 case THEME_LOCATION_EXTERIOR:
2309 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002310 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002311 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002312 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002313}
2314
Pekka Paalanen26237862014-09-10 15:10:30 +03002315static void
2316frame_menu_func(void *data, struct input *input, int index)
2317{
2318 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002319
2320 switch (index) {
2321 case 0: /* close */
2322 window_close(window);
2323 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002324 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002325 /* we don't have a way to get out of fullscreen for now */
2326 if (window->fullscreen_handler)
2327 window->fullscreen_handler(window, window->user_data);
2328 break;
2329 }
2330}
2331
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002332void
2333window_show_frame_menu(struct window *window,
2334 struct input *input, uint32_t time)
2335{
2336 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002337 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002338
Pekka Paalanen26237862014-09-10 15:10:30 +03002339 static const char *entries[] = {
2340 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002341 "Fullscreen"
2342 };
2343
2344 if (window->fullscreen_handler)
2345 count = ARRAY_LENGTH(entries);
2346 else
2347 count = ARRAY_LENGTH(entries) - 1;
2348
2349 input_get_position(input, &x, &y);
2350 window_show_menu(window->display, input, time, window,
2351 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002352}
2353
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002354static int
2355frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002356 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002357{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002358 struct window_frame *frame = data;
2359 enum theme_location location;
2360
2361 location = frame_pointer_enter(frame->frame, input, x, y);
2362 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2363 widget_schedule_redraw(frame->widget);
2364
2365 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002366}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002367
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002368static int
2369frame_motion_handler(struct widget *widget,
2370 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002371 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002372{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002373 struct window_frame *frame = data;
2374 enum theme_location location;
2375
2376 location = frame_pointer_motion(frame->frame, input, x, y);
2377 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2378 widget_schedule_redraw(frame->widget);
2379
2380 return frame_get_pointer_image_for_location(data, location);
2381}
2382
2383static void
2384frame_leave_handler(struct widget *widget,
2385 struct input *input, void *data)
2386{
2387 struct window_frame *frame = data;
2388
2389 frame_pointer_leave(frame->frame, input);
2390 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2391 widget_schedule_redraw(frame->widget);
2392}
2393
2394static void
2395frame_handle_status(struct window_frame *frame, struct input *input,
2396 uint32_t time, enum theme_location location)
2397{
2398 struct window *window = frame->widget->window;
2399 uint32_t status;
2400
2401 status = frame_status(frame->frame);
2402 if (status & FRAME_STATUS_REPAINT)
2403 widget_schedule_redraw(frame->widget);
2404
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002405 if (status & FRAME_STATUS_MINIMIZE) {
2406 window_set_minimized(window);
2407 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2408 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002409
2410 if (status & FRAME_STATUS_MENU) {
2411 window_show_frame_menu(window, input, time);
2412 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2413 }
2414
2415 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002416 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002417 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2418 }
2419
2420 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002421 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002422 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002423 }
2424
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002425 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002426 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002427 xdg_surface_move(window->xdg_surface,
2428 input_get_seat(input),
2429 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002430
2431 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2432 }
2433
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002434 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002435 input_ungrab(input);
2436
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002437 xdg_surface_resize(window->xdg_surface,
2438 input_get_seat(input),
2439 window->display->serial,
2440 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002441
2442 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2443 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002444}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002445
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002446#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002447static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002448frame_button_handler(struct widget *widget,
2449 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002450 uint32_t button, enum wl_pointer_button_state state,
2451 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002452
2453{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002454 struct window_frame *frame = data;
2455 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002456
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002457 frame->double_click = 0;
2458 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2459 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2460 frame->double_click = 1;
2461 frame->did_double = 1;
2462 } else
2463 frame->did_double = 0;
2464
2465 frame->last_time = time;
2466 } else if (frame->did_double == 1) {
2467 frame->double_click = 1;
2468 frame->did_double = 0;
2469 }
2470
2471 if (frame->double_click)
2472 location = frame_double_click(frame->frame, input,
2473 button, state);
2474 else
2475 location = frame_pointer_button(frame->frame, input,
2476 button, state);
2477
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002478 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002479}
2480
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002481static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002482frame_touch_down_handler(struct widget *widget, struct input *input,
2483 uint32_t serial, uint32_t time, int32_t id,
2484 float x, float y, void *data)
2485{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002486 struct window_frame *frame = data;
2487
Xiong Zhang382de462014-06-12 11:06:26 +08002488 frame->double_click = 0;
2489 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2490 frame->last_id == id) {
2491 frame->double_click = 1;
2492 frame->did_double = 1;
2493 frame->double_id = id;
2494 } else
2495 frame->did_double = 0;
2496
2497 frame->last_time = time;
2498 frame->last_id = id;
2499
2500 if (frame->double_click)
2501 frame_double_touch_down(frame->frame, input, id, x, y);
2502 else
2503 frame_touch_down(frame->frame, input, id, x, y);
2504
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002505 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2506}
2507
2508static void
2509frame_touch_up_handler(struct widget *widget,
2510 struct input *input, uint32_t serial, uint32_t time,
2511 int32_t id, void *data)
2512{
2513 struct window_frame *frame = data;
2514
Xiong Zhang382de462014-06-12 11:06:26 +08002515 if (frame->double_id == id && frame->did_double) {
2516 frame->did_double = 0;
2517 frame->double_id = 0;
2518 frame_double_touch_up(frame->frame, input, id);
2519 } else
2520 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002521 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002522}
2523
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002525window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002526{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002527 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002528 uint32_t buttons;
2529
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002530 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002531 buttons = FRAME_BUTTON_NONE;
2532 } else {
2533 buttons = FRAME_BUTTON_ALL;
2534 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002535
Peter Huttererf3d62272013-08-08 11:57:05 +10002536 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002537 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002538 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002539
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002540 frame->widget = window_add_widget(window, frame);
2541 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002542
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002543 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2544 widget_set_resize_handler(frame->widget, frame_resize_handler);
2545 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002546 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002547 widget_set_motion_handler(frame->widget, frame_motion_handler);
2548 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002549 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002550 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002551
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002552 window->frame = frame;
2553
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002554 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002555}
2556
Kristian Høgsberga1627922012-06-20 17:30:03 -04002557void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002558window_frame_set_child_size(struct widget *widget, int child_width,
2559 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002560{
2561 struct display *display = widget->window->display;
2562 struct theme *t = display->theme;
2563 int decoration_width, decoration_height;
2564 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002565 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002566
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002567 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002568 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002569 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002570 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002571
2572 width = child_width + decoration_width;
2573 height = child_height + decoration_height;
2574 } else {
2575 width = child_width;
2576 height = child_height;
2577 }
2578
2579 window_schedule_resize(widget->window, width, height);
2580}
2581
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002582static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002583window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002584{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002585 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002586
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002587 /* frame->child must be destroyed by the application */
2588 widget_destroy(frame->widget);
2589 free(frame);
2590}
2591
2592static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002593input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002594 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002595{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002596 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002597 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002598
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002599 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002600 return;
2601
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002602 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002603 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002604 widget = old;
2605 if (input->grab)
2606 widget = input->grab;
2607 if (widget->leave_handler)
2608 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002609 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002610 }
2611
2612 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002613 widget = focus;
2614 if (input->grab)
2615 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002616 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002617 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002618 cursor = widget->enter_handler(focus, input, x, y,
2619 widget->user_data);
2620 else
2621 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002622
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002623 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002624 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002625}
2626
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002627void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002628touch_grab(struct input *input, int32_t touch_id)
2629{
2630 input->touch_grab = 1;
2631 input->touch_grab_id = touch_id;
2632}
2633
2634void
2635touch_ungrab(struct input *input)
2636{
2637 struct touch_point *tp, *tmp;
2638
2639 input->touch_grab = 0;
2640
2641 wl_list_for_each_safe(tp, tmp,
2642 &input->touch_point_list, link) {
2643 if (tp->id != input->touch_grab_id)
2644 continue;
2645 wl_list_remove(&tp->link);
2646 free(tp);
2647
2648 return;
2649 }
2650}
2651
2652void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002653input_grab(struct input *input, struct widget *widget, uint32_t button)
2654{
2655 input->grab = widget;
2656 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002657
2658 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002659}
2660
2661void
2662input_ungrab(struct input *input)
2663{
2664 struct widget *widget;
2665
2666 input->grab = NULL;
2667 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002668 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002669 input->sx, input->sy);
2670 input_set_focus_widget(input, widget, input->sx, input->sy);
2671 }
2672}
2673
2674static void
Derek Foreman118a4292015-04-22 17:23:35 -05002675cursor_delay_timer_reset(struct input *input, uint32_t duration)
2676{
2677 struct itimerspec its;
2678
2679 if (!duration)
2680 input->cursor_timer_running = false;
2681 else
2682 input->cursor_timer_running = true;
2683
2684 its.it_interval.tv_sec = 0;
2685 its.it_interval.tv_nsec = 0;
2686 its.it_value.tv_sec = duration / 1000;
2687 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2688 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2689 fprintf(stderr, "could not set cursor timerfd\n: %m");
2690}
2691
2692static void cancel_pointer_image_update(struct input *input)
2693{
2694 if (input->cursor_timer_running)
2695 cursor_delay_timer_reset(input, 0);
2696}
2697
2698static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002699input_remove_pointer_focus(struct input *input)
2700{
2701 struct window *window = input->pointer_focus;
2702
2703 if (!window)
2704 return;
2705
2706 input_set_focus_widget(input, NULL, 0, 0);
2707
2708 input->pointer_focus = NULL;
2709 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002710 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002711}
2712
2713static void
2714pointer_handle_enter(void *data, struct wl_pointer *pointer,
2715 uint32_t serial, struct wl_surface *surface,
2716 wl_fixed_t sx_w, wl_fixed_t sy_w)
2717{
2718 struct input *input = data;
2719 struct window *window;
2720 struct widget *widget;
2721 float sx = wl_fixed_to_double(sx_w);
2722 float sy = wl_fixed_to_double(sy_w);
2723
2724 if (!surface) {
2725 /* enter event for a window we've just destroyed */
2726 return;
2727 }
2728
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002729 window = wl_surface_get_user_data(surface);
2730 if (surface != window->main_surface->surface) {
2731 DBG("Ignoring input event from subsurface %p\n", surface);
2732 return;
2733 }
2734
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002735 input->display->serial = serial;
2736 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002737 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002738
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002739 input->sx = sx;
2740 input->sy = sy;
2741
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002742 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002743 input_set_focus_widget(input, widget, sx, sy);
2744}
2745
2746static void
2747pointer_handle_leave(void *data, struct wl_pointer *pointer,
2748 uint32_t serial, struct wl_surface *surface)
2749{
2750 struct input *input = data;
2751
2752 input->display->serial = serial;
2753 input_remove_pointer_focus(input);
2754}
2755
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002756static void
Daniel Stone37816df2012-05-16 18:45:18 +01002757pointer_handle_motion(void *data, struct wl_pointer *pointer,
2758 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002759{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002760 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002761 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002762 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002763 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002764 float sx = wl_fixed_to_double(sx_w);
2765 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002766
Paul Winwoodb22bf572013-08-29 10:52:54 +01002767 if (!window)
2768 return;
2769
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002770 input->sx = sx;
2771 input->sy = sy;
2772
Rob Bradford5f087742013-07-11 19:41:27 +01002773 /* when making the window smaller - e.g. after a unmaximise we might
2774 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002775 * based on the old surface dimensions. However, if we have an active
2776 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002777 */
Derek Foreman46812b62015-08-26 17:13:27 -05002778 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002779 sy < window->main_surface->allocation.y ||
2780 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002781 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002782 return;
2783
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002784 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002785 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002786 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002787 }
2788
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002789 if (input->grab)
2790 widget = input->grab;
2791 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002792 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002793 if (widget) {
2794 if (widget->motion_handler)
2795 cursor = widget->motion_handler(input->focus_widget,
2796 input, time, sx, sy,
2797 widget->user_data);
2798 else
2799 cursor = widget->default_cursor;
2800 } else
2801 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002802
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002803 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002804}
2805
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002806static void
Daniel Stone37816df2012-05-16 18:45:18 +01002807pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002808 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002809{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002810 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002811 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002812 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002813
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002814 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002815 if (input->focus_widget && input->grab == NULL &&
2816 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002817 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002818
Neil Roberts6b28aad2012-01-23 19:11:18 +00002819 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002820 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002821 (*widget->button_handler)(widget,
2822 input, time,
2823 button, state,
2824 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002825
Daniel Stone4dbadb12012-05-30 16:31:51 +01002826 if (input->grab && input->grab_button == button &&
2827 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002828 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002829}
2830
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002831static void
Daniel Stone37816df2012-05-16 18:45:18 +01002832pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002833 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002834{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002835 struct input *input = data;
2836 struct widget *widget;
2837
2838 widget = input->focus_widget;
2839 if (input->grab)
2840 widget = input->grab;
2841 if (widget && widget->axis_handler)
2842 (*widget->axis_handler)(widget,
2843 input, time,
2844 axis, value,
2845 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002846}
2847
Peter Hutterer87743e92016-01-18 16:38:22 +10002848static void
2849pointer_handle_frame(void *data, struct wl_pointer *pointer)
2850{
2851 struct input *input = data;
2852 struct widget *widget;
2853
2854 widget = input->focus_widget;
2855 if (input->grab)
2856 widget = input->grab;
2857 if (widget && widget->pointer_frame_handler)
2858 (*widget->pointer_frame_handler)(widget,
2859 input,
2860 widget->user_data);
2861}
2862
2863static void
2864pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2865 uint32_t source)
2866{
2867 struct input *input = data;
2868 struct widget *widget;
2869
2870 widget = input->focus_widget;
2871 if (input->grab)
2872 widget = input->grab;
2873 if (widget && widget->axis_source_handler)
2874 (*widget->axis_source_handler)(widget,
2875 input,
2876 source,
2877 widget->user_data);
2878}
2879
2880static void
2881pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2882 uint32_t time, uint32_t axis)
2883{
2884 struct input *input = data;
2885 struct widget *widget;
2886
2887 widget = input->focus_widget;
2888 if (input->grab)
2889 widget = input->grab;
2890 if (widget && widget->axis_stop_handler)
2891 (*widget->axis_stop_handler)(widget,
2892 input, time,
2893 axis,
2894 widget->user_data);
2895}
2896
2897static void
2898pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2899 uint32_t axis, int32_t discrete)
2900{
2901 struct input *input = data;
2902 struct widget *widget;
2903
2904 widget = input->focus_widget;
2905 if (input->grab)
2906 widget = input->grab;
2907 if (widget && widget->axis_discrete_handler)
2908 (*widget->axis_discrete_handler)(widget,
2909 input,
2910 axis,
2911 discrete,
2912 widget->user_data);
2913}
2914
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002915static const struct wl_pointer_listener pointer_listener = {
2916 pointer_handle_enter,
2917 pointer_handle_leave,
2918 pointer_handle_motion,
2919 pointer_handle_button,
2920 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002921 pointer_handle_frame,
2922 pointer_handle_axis_source,
2923 pointer_handle_axis_stop,
2924 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002925};
2926
2927static void
2928input_remove_keyboard_focus(struct input *input)
2929{
2930 struct window *window = input->keyboard_focus;
2931 struct itimerspec its;
2932
2933 its.it_interval.tv_sec = 0;
2934 its.it_interval.tv_nsec = 0;
2935 its.it_value.tv_sec = 0;
2936 its.it_value.tv_nsec = 0;
2937 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2938
2939 if (!window)
2940 return;
2941
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002942 if (window->keyboard_focus_handler)
2943 (*window->keyboard_focus_handler)(window, NULL,
2944 window->user_data);
2945
2946 input->keyboard_focus = NULL;
2947}
2948
2949static void
2950keyboard_repeat_func(struct task *task, uint32_t events)
2951{
2952 struct input *input =
2953 container_of(task, struct input, repeat_task);
2954 struct window *window = input->keyboard_focus;
2955 uint64_t exp;
2956
2957 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2958 /* If we change the timer between the fd becoming
2959 * readable and getting here, there'll be nothing to
2960 * read and we get EAGAIN. */
2961 return;
2962
2963 if (window && window->key_handler) {
2964 (*window->key_handler)(window, input, input->repeat_time,
2965 input->repeat_key, input->repeat_sym,
2966 WL_KEYBOARD_KEY_STATE_PRESSED,
2967 window->user_data);
2968 }
2969}
2970
2971static void
2972keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2973 uint32_t format, int fd, uint32_t size)
2974{
2975 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002976 struct xkb_keymap *keymap;
2977 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002978 char *map_str;
2979
2980 if (!data) {
2981 close(fd);
2982 return;
2983 }
2984
2985 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2986 close(fd);
2987 return;
2988 }
2989
2990 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2991 if (map_str == MAP_FAILED) {
2992 close(fd);
2993 return;
2994 }
2995
Ran Benita2e1968f2014-08-19 23:59:51 +03002996 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2997 map_str,
2998 XKB_KEYMAP_FORMAT_TEXT_V1,
2999 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003000 munmap(map_str, size);
3001 close(fd);
3002
Rui Matos3eccb862013-10-10 19:44:22 +02003003 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003004 fprintf(stderr, "failed to compile keymap\n");
3005 return;
3006 }
3007
Rui Matos3eccb862013-10-10 19:44:22 +02003008 state = xkb_state_new(keymap);
3009 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003010 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003011 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003012 return;
3013 }
3014
Rui Matos3eccb862013-10-10 19:44:22 +02003015 xkb_keymap_unref(input->xkb.keymap);
3016 xkb_state_unref(input->xkb.state);
3017 input->xkb.keymap = keymap;
3018 input->xkb.state = state;
3019
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003020 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003021 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003022 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003023 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003024 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003025 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003026}
3027
3028static void
3029keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3030 uint32_t serial, struct wl_surface *surface,
3031 struct wl_array *keys)
3032{
3033 struct input *input = data;
3034 struct window *window;
3035
3036 input->display->serial = serial;
3037 input->keyboard_focus = wl_surface_get_user_data(surface);
3038
3039 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003040 if (window->keyboard_focus_handler)
3041 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003042 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003043}
3044
3045static void
3046keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3047 uint32_t serial, struct wl_surface *surface)
3048{
3049 struct input *input = data;
3050
3051 input->display->serial = serial;
3052 input_remove_keyboard_focus(input);
3053}
3054
Scott Moreau210d0792012-03-22 10:47:01 -06003055static void
Daniel Stone37816df2012-05-16 18:45:18 +01003056keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003057 uint32_t serial, uint32_t time, uint32_t key,
3058 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003059{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003060 struct input *input = data;
3061 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003062 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003063 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003064 const xkb_keysym_t *syms;
3065 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003066 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003067
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003068 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003069 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003070 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003071 return;
3072
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003073 /* We only use input grabs for pointer events for now, so just
3074 * ignore key presses if a grab is active. We expand the key
3075 * event delivery mechanism to route events to widgets to
3076 * properly handle key grabs. In the meantime, this prevents
3077 * key event devlivery while a grab is active. */
3078 if (input->grab && input->grab_button == 0)
3079 return;
3080
Ran Benita2e1968f2014-08-19 23:59:51 +03003081 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003082
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003083 sym = XKB_KEY_NoSymbol;
3084 if (num_syms == 1)
3085 sym = syms[0];
3086
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003087
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003088 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003089 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003090 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003091 } else if (sym == XKB_KEY_F11 &&
3092 window->fullscreen_handler &&
3093 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3094 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003095 } else if (sym == XKB_KEY_F4 &&
3096 input->modifiers == MOD_ALT_MASK &&
3097 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003098 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003099 } else if (window->key_handler) {
3100 (*window->key_handler)(window, input, time, key,
3101 sym, state, window->user_data);
3102 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003103
3104 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3105 key == input->repeat_key) {
3106 its.it_interval.tv_sec = 0;
3107 its.it_interval.tv_nsec = 0;
3108 its.it_value.tv_sec = 0;
3109 its.it_value.tv_nsec = 0;
3110 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003111 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3112 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003113 input->repeat_sym = sym;
3114 input->repeat_key = key;
3115 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003116 its.it_interval.tv_sec = input->repeat_rate_sec;
3117 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3118 its.it_value.tv_sec = input->repeat_delay_sec;
3119 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003120 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3121 }
3122}
3123
3124static void
Daniel Stone351eb612012-05-31 15:27:47 -04003125keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3126 uint32_t serial, uint32_t mods_depressed,
3127 uint32_t mods_latched, uint32_t mods_locked,
3128 uint32_t group)
3129{
3130 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003131 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003132
Matt Ropere61561f2013-06-24 16:52:43 +01003133 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3134 if (!input->xkb.keymap)
3135 return;
3136
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003137 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3138 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003139 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003140 XKB_STATE_MODS_DEPRESSED |
3141 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003142 input->modifiers = 0;
3143 if (mask & input->xkb.control_mask)
3144 input->modifiers |= MOD_CONTROL_MASK;
3145 if (mask & input->xkb.alt_mask)
3146 input->modifiers |= MOD_ALT_MASK;
3147 if (mask & input->xkb.shift_mask)
3148 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003149}
3150
Jonny Lamb06959082014-08-12 14:58:27 +02003151static void
3152set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3153{
3154 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3155 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3156
3157 /* a rate of zero disables any repeating, regardless of the delay's
3158 * value */
3159 if (rate == 0)
3160 return;
3161
3162 if (rate == 1)
3163 input->repeat_rate_sec = 1;
3164 else
3165 input->repeat_rate_nsec = 1000000000 / rate;
3166
3167 input->repeat_delay_sec = delay / 1000;
3168 delay -= (input->repeat_delay_sec * 1000);
3169 input->repeat_delay_nsec = delay * 1000 * 1000;
3170}
3171
3172static void
3173keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3174 int32_t rate, int32_t delay)
3175{
3176 struct input *input = data;
3177
3178 set_repeat_info(input, rate, delay);
3179}
3180
Daniel Stone37816df2012-05-16 18:45:18 +01003181static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003182 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003183 keyboard_handle_enter,
3184 keyboard_handle_leave,
3185 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003186 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003187 keyboard_handle_repeat_info
3188
Daniel Stone37816df2012-05-16 18:45:18 +01003189};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003190
3191static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003192touch_handle_down(void *data, struct wl_touch *wl_touch,
3193 uint32_t serial, uint32_t time, struct wl_surface *surface,
3194 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3195{
3196 struct input *input = data;
3197 struct widget *widget;
3198 float sx = wl_fixed_to_double(x_w);
3199 float sy = wl_fixed_to_double(y_w);
3200
Rusty Lynch1084da52013-08-15 09:10:08 -07003201 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003202 input->touch_focus = wl_surface_get_user_data(surface);
3203 if (!input->touch_focus) {
3204 DBG("Failed to find to touch focus for surface %p\n", surface);
3205 return;
3206 }
3207
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003208 if (surface != input->touch_focus->main_surface->surface) {
3209 DBG("Ignoring input event from subsurface %p\n", surface);
3210 input->touch_focus = NULL;
3211 return;
3212 }
3213
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003214 if (input->grab)
3215 widget = input->grab;
3216 else
3217 widget = window_find_widget(input->touch_focus,
3218 wl_fixed_to_double(x_w),
3219 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003220 if (widget) {
3221 struct touch_point *tp = xmalloc(sizeof *tp);
3222 if (tp) {
3223 tp->id = id;
3224 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003225 tp->x = sx;
3226 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003227 wl_list_insert(&input->touch_point_list, &tp->link);
3228
3229 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003230 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003231 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003232 sx, sy,
3233 widget->user_data);
3234 }
3235 }
3236}
3237
3238static void
3239touch_handle_up(void *data, struct wl_touch *wl_touch,
3240 uint32_t serial, uint32_t time, int32_t id)
3241{
3242 struct input *input = data;
3243 struct touch_point *tp, *tmp;
3244
Rusty Lynch041815a2013-08-08 21:20:38 -07003245 if (!input->touch_focus) {
3246 DBG("No touch focus found for touch up event!\n");
3247 return;
3248 }
3249
3250 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3251 if (tp->id != id)
3252 continue;
3253
3254 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003255 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003256 time, id,
3257 tp->widget->user_data);
3258
3259 wl_list_remove(&tp->link);
3260 free(tp);
3261
3262 return;
3263 }
3264}
3265
3266static void
3267touch_handle_motion(void *data, struct wl_touch *wl_touch,
3268 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3269{
3270 struct input *input = data;
3271 struct touch_point *tp;
3272 float sx = wl_fixed_to_double(x_w);
3273 float sy = wl_fixed_to_double(y_w);
3274
3275 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3276
3277 if (!input->touch_focus) {
3278 DBG("No touch focus found for touch motion event!\n");
3279 return;
3280 }
3281
3282 wl_list_for_each(tp, &input->touch_point_list, link) {
3283 if (tp->id != id)
3284 continue;
3285
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003286 tp->x = sx;
3287 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003288 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003289 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003290 id, sx, sy,
3291 tp->widget->user_data);
3292 return;
3293 }
3294}
3295
3296static void
3297touch_handle_frame(void *data, struct wl_touch *wl_touch)
3298{
3299 struct input *input = data;
3300 struct touch_point *tp, *tmp;
3301
3302 DBG("touch_handle_frame\n");
3303
3304 if (!input->touch_focus) {
3305 DBG("No touch focus found for touch frame event!\n");
3306 return;
3307 }
3308
3309 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3310 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003311 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003312 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003313 }
3314}
3315
3316static void
3317touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3318{
3319 struct input *input = data;
3320 struct touch_point *tp, *tmp;
3321
3322 DBG("touch_handle_cancel\n");
3323
3324 if (!input->touch_focus) {
3325 DBG("No touch focus found for touch cancel event!\n");
3326 return;
3327 }
3328
3329 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3330 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003331 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3332 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003333
3334 wl_list_remove(&tp->link);
3335 free(tp);
3336 }
3337}
3338
3339static const struct wl_touch_listener touch_listener = {
3340 touch_handle_down,
3341 touch_handle_up,
3342 touch_handle_motion,
3343 touch_handle_frame,
3344 touch_handle_cancel,
3345};
3346
3347static void
Daniel Stone37816df2012-05-16 18:45:18 +01003348seat_handle_capabilities(void *data, struct wl_seat *seat,
3349 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003350{
Daniel Stone37816df2012-05-16 18:45:18 +01003351 struct input *input = data;
3352
3353 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3354 input->pointer = wl_seat_get_pointer(seat);
3355 wl_pointer_set_user_data(input->pointer, input);
3356 wl_pointer_add_listener(input->pointer, &pointer_listener,
3357 input);
3358 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003359 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003360 wl_pointer_release(input->pointer);
3361 else
3362 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003363 input->pointer = NULL;
3364 }
3365
3366 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3367 input->keyboard = wl_seat_get_keyboard(seat);
3368 wl_keyboard_set_user_data(input->keyboard, input);
3369 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3370 input);
3371 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003372 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003373 wl_keyboard_release(input->keyboard);
3374 else
3375 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003376 input->keyboard = NULL;
3377 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003378
3379 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3380 input->touch = wl_seat_get_touch(seat);
3381 wl_touch_set_user_data(input->touch, input);
3382 wl_touch_add_listener(input->touch, &touch_listener, input);
3383 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003384 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003385 wl_touch_release(input->touch);
3386 else
3387 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003388 input->touch = NULL;
3389 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003390}
3391
Rob Bradford08031182013-08-13 20:11:03 +01003392static void
3393seat_handle_name(void *data, struct wl_seat *seat,
3394 const char *name)
3395{
3396
3397}
3398
Daniel Stone37816df2012-05-16 18:45:18 +01003399static const struct wl_seat_listener seat_listener = {
3400 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003401 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003402};
3403
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003404void
3405input_get_position(struct input *input, int32_t *x, int32_t *y)
3406{
3407 *x = input->sx;
3408 *y = input->sy;
3409}
3410
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003411int
3412input_get_touch(struct input *input, int32_t id, float *x, float *y)
3413{
3414 struct touch_point *tp;
3415
3416 wl_list_for_each(tp, &input->touch_point_list, link) {
3417 if (tp->id != id)
3418 continue;
3419
3420 *x = tp->x;
3421 *y = tp->y;
3422 return 0;
3423 }
3424
3425 return -1;
3426}
3427
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003428struct display *
3429input_get_display(struct input *input)
3430{
3431 return input->display;
3432}
3433
Daniel Stone37816df2012-05-16 18:45:18 +01003434struct wl_seat *
3435input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003436{
Daniel Stone37816df2012-05-16 18:45:18 +01003437 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003438}
3439
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003440uint32_t
3441input_get_modifiers(struct input *input)
3442{
3443 return input->modifiers;
3444}
3445
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003446struct widget *
3447input_get_focus_widget(struct input *input)
3448{
3449 return input->focus_widget;
3450}
3451
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003452struct data_offer {
3453 struct wl_data_offer *offer;
3454 struct input *input;
3455 struct wl_array types;
3456 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003457
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003458 struct task io_task;
3459 int fd;
3460 data_func_t func;
3461 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003462 uint32_t dnd_action;
3463 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003464 void *user_data;
3465};
3466
3467static void
3468data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3469{
3470 struct data_offer *offer = data;
3471 char **p;
3472
3473 p = wl_array_add(&offer->types, sizeof *p);
3474 *p = strdup(type);
3475}
3476
Carlos Garnacho9c931792016-01-18 23:52:12 +01003477static void
3478data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3479{
3480 struct data_offer *offer = data;
3481
3482 offer->source_actions = source_actions;
3483}
3484
3485static void
3486data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3487{
3488 struct data_offer *offer = data;
3489
3490 offer->dnd_action = dnd_action;
3491}
3492
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003493static const struct wl_data_offer_listener data_offer_listener = {
3494 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003495 data_offer_source_actions,
3496 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003497};
3498
3499static void
3500data_offer_destroy(struct data_offer *offer)
3501{
3502 char **p;
3503
3504 offer->refcount--;
3505 if (offer->refcount == 0) {
3506 wl_data_offer_destroy(offer->offer);
3507 for (p = offer->types.data; *p; p++)
3508 free(*p);
3509 wl_array_release(&offer->types);
3510 free(offer);
3511 }
3512}
3513
3514static void
3515data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003516 struct wl_data_device *data_device,
3517 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003518{
3519 struct data_offer *offer;
3520
Brian Lovinbc919262013-08-07 15:34:59 -07003521 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003522
3523 wl_array_init(&offer->types);
3524 offer->refcount = 1;
3525 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003526 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003527 wl_data_offer_add_listener(offer->offer,
3528 &data_offer_listener, offer);
3529}
3530
3531static void
3532data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003533 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003534 wl_fixed_t x_w, wl_fixed_t y_w,
3535 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003536{
3537 struct input *input = data;
3538 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003539 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003540 float x = wl_fixed_to_double(x_w);
3541 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003542 char **p;
3543
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003544 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003545 input->drag_enter_serial = serial;
3546 input->drag_focus = window,
3547 input->drag_x = x;
3548 input->drag_y = y;
3549
3550 if (!input->touch_grab)
3551 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003552
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003553 if (offer) {
3554 input->drag_offer = wl_data_offer_get_user_data(offer);
3555
3556 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3557 *p = NULL;
3558
3559 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003560
3561 if (input->display->data_device_manager_version >=
3562 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3563 wl_data_offer_set_actions(offer,
3564 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3565 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3566 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3567 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003568 } else {
3569 input->drag_offer = NULL;
3570 types_data = NULL;
3571 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003572
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003573 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003574 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003575 window->user_data);
3576}
3577
3578static void
3579data_device_leave(void *data, struct wl_data_device *data_device)
3580{
3581 struct input *input = data;
3582
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003583 if (input->drag_offer) {
3584 data_offer_destroy(input->drag_offer);
3585 input->drag_offer = NULL;
3586 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003587}
3588
3589static void
3590data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003591 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003592{
3593 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003594 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003595 float x = wl_fixed_to_double(x_w);
3596 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003597 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003598
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003599 input->drag_x = x;
3600 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003601
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003602 if (input->drag_offer)
3603 types_data = input->drag_offer->types.data;
3604 else
3605 types_data = NULL;
3606
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003607 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003608 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003609 window->user_data);
3610}
3611
3612static void
3613data_device_drop(void *data, struct wl_data_device *data_device)
3614{
3615 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003616 struct window *window = input->drag_focus;
3617 float x, y;
3618
3619 x = input->drag_x;
3620 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003621
3622 if (window->drop_handler)
3623 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003624 x, y, window->user_data);
3625
3626 if (input->touch_grab)
3627 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003628}
3629
3630static void
3631data_device_selection(void *data,
3632 struct wl_data_device *wl_data_device,
3633 struct wl_data_offer *offer)
3634{
3635 struct input *input = data;
3636 char **p;
3637
3638 if (input->selection_offer)
3639 data_offer_destroy(input->selection_offer);
3640
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003641 if (offer) {
3642 input->selection_offer = wl_data_offer_get_user_data(offer);
3643 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3644 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003645 } else {
3646 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003647 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003648}
3649
3650static const struct wl_data_device_listener data_device_listener = {
3651 data_device_data_offer,
3652 data_device_enter,
3653 data_device_leave,
3654 data_device_motion,
3655 data_device_drop,
3656 data_device_selection
3657};
3658
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003659static void
3660input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003661{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003662 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003663 struct wl_cursor *cursor;
3664 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003665
Daniel Stone80972742012-11-07 17:51:39 +11003666 if (!input->pointer)
3667 return;
3668
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003669 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003670 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003671 return;
3672
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003673 if (index >= (int) cursor->image_count) {
3674 fprintf(stderr, "cursor index out of range\n");
3675 return;
3676 }
3677
3678 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003679 buffer = wl_cursor_image_get_buffer(image);
3680 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003681 return;
3682
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003683 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3684 wl_surface_damage(input->pointer_surface, 0, 0,
3685 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003686 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003687 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3688 input->pointer_surface,
3689 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003690}
3691
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003692static const struct wl_callback_listener pointer_surface_listener;
3693
Derek Foreman493d9792015-03-04 16:26:25 -06003694static bool
3695input_set_pointer_special(struct input *input)
3696{
3697 if (input->current_cursor == CURSOR_BLANK) {
3698 wl_pointer_set_cursor(input->pointer,
3699 input->pointer_enter_serial,
3700 NULL, 0, 0);
3701 return true;
3702 }
3703
3704 if (input->current_cursor == CURSOR_UNSET)
3705 return true;
3706
3707 return false;
3708}
3709
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003710static void
Derek Foreman118a4292015-04-22 17:23:35 -05003711schedule_pointer_image_update(struct input *input,
3712 struct wl_cursor *cursor,
3713 uint32_t duration,
3714 bool force_frame)
3715{
3716 /* Some silly cursor sets have enormous pauses in them. In these
3717 * cases it's better to use a timer even if it results in less
3718 * accurate presentation, since it will save us having to set the
3719 * same cursor image over and over again.
3720 *
3721 * This is really not the way we're supposed to time any kind of
3722 * animation, but we're pretending it's OK here because we don't
3723 * want animated cursors with long delays to needlessly hog CPU.
3724 *
3725 * We use force_frame to ensure we don't accumulate large timing
3726 * errors by running off the wrong clock.
3727 */
3728 if (!force_frame && duration > 100) {
3729 struct timespec tp;
3730
3731 clock_gettime(CLOCK_MONOTONIC, &tp);
3732 input->cursor_timer_start = tp.tv_sec * 1000
3733 + tp.tv_nsec / 1000000;
3734 cursor_delay_timer_reset(input, duration);
3735 return;
3736 }
3737
3738 /* for short durations we'll just spin on frame callbacks for
3739 * accurate timing - the way any kind of timing sensitive animation
3740 * should really be done. */
3741 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3742 wl_callback_add_listener(input->cursor_frame_cb,
3743 &pointer_surface_listener, input);
3744
3745}
3746
3747static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003748pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3749 uint32_t time)
3750{
3751 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003752 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003753 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003754 uint32_t duration;
3755 bool force_frame = true;
3756
3757 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003758
3759 if (callback) {
3760 assert(callback == input->cursor_frame_cb);
3761 wl_callback_destroy(callback);
3762 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003763 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003764 }
3765
Daniel Stone80972742012-11-07 17:51:39 +11003766 if (!input->pointer)
3767 return;
3768
Derek Foreman493d9792015-03-04 16:26:25 -06003769 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003770 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003771
Daniel Stonea494f1d2012-06-18 19:31:12 +01003772 cursor = input->display->cursors[input->current_cursor];
3773 if (!cursor)
3774 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003775
3776 /* FIXME We don't have the current time on the first call so we set
3777 * the animation start to the time of the first frame callback. */
3778 if (time == 0)
3779 input->cursor_anim_start = 0;
3780 else if (input->cursor_anim_start == 0)
3781 input->cursor_anim_start = time;
3782
Derek Foreman118a4292015-04-22 17:23:35 -05003783 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003784
Derek Foreman118a4292015-04-22 17:23:35 -05003785 if (time == 0 || input->cursor_anim_start == 0) {
3786 duration = 0;
3787 i = 0;
3788 } else
3789 i = wl_cursor_frame_and_duration(
3790 cursor,
3791 time - input->cursor_anim_start,
3792 &duration);
3793
3794 if (cursor->image_count > 1)
3795 schedule_pointer_image_update(input, cursor, duration,
3796 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003797
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003798 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003799}
3800
Derek Foreman118a4292015-04-22 17:23:35 -05003801static void
3802cursor_timer_func(struct task *task, uint32_t events)
3803{
3804 struct input *input = container_of(task, struct input, cursor_task);
3805 struct timespec tp;
3806 struct wl_cursor *cursor;
3807 uint32_t time;
3808 uint64_t exp;
3809
3810 if (!input->cursor_timer_running)
3811 return;
3812
3813 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3814 return;
3815
3816 cursor = input->display->cursors[input->current_cursor];
3817 if (!cursor)
3818 return;
3819
3820 clock_gettime(CLOCK_MONOTONIC, &tp);
3821 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3822 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3823}
3824
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003825static const struct wl_callback_listener pointer_surface_listener = {
3826 pointer_surface_frame_callback
3827};
3828
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003829void
3830input_set_pointer_image(struct input *input, int pointer)
3831{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003832 int force = 0;
3833
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003834 if (!input->pointer)
3835 return;
3836
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003837 if (input->pointer_enter_serial > input->cursor_serial)
3838 force = 1;
3839
3840 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003841 return;
3842
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003843 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003844 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003845 if (!input->cursor_frame_cb)
3846 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003847 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003848 /* The current frame callback may be stuck if, for instance,
3849 * the set cursor request was processed by the server after
3850 * this client lost the focus. In this case the cursor surface
3851 * might not be mapped and the frame callback wouldn't ever
3852 * complete. Send a set_cursor and attach to try to map the
3853 * cursor surface again so that the callback will finish */
3854 input_set_pointer_image_index(input, 0);
3855 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003856}
3857
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003858struct wl_data_device *
3859input_get_data_device(struct input *input)
3860{
3861 return input->data_device;
3862}
3863
3864void
3865input_set_selection(struct input *input,
3866 struct wl_data_source *source, uint32_t time)
3867{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003868 if (input->data_device)
3869 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003870}
3871
3872void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003873input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003874{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003875 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003876 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003877}
3878
3879static void
3880offer_io_func(struct task *task, uint32_t events)
3881{
3882 struct data_offer *offer =
3883 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003884 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003885 unsigned int len;
3886 char buffer[4096];
3887
3888 len = read(offer->fd, buffer, sizeof buffer);
3889 offer->func(buffer, len,
3890 offer->x, offer->y, offer->user_data);
3891
3892 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003893 if (display->data_device_manager_version >=
3894 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3895 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003896 close(offer->fd);
3897 data_offer_destroy(offer);
3898 }
3899}
3900
3901static void
3902data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3903 data_func_t func, void *user_data)
3904{
3905 int p[2];
3906
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003907 if (pipe2(p, O_CLOEXEC) == -1)
3908 return;
3909
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003910 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3911 close(p[1]);
3912
3913 offer->io_task.run = offer_io_func;
3914 offer->fd = p[0];
3915 offer->func = func;
3916 offer->refcount++;
3917 offer->user_data = user_data;
3918
3919 display_watch_fd(offer->input->display,
3920 offer->fd, EPOLLIN, &offer->io_task);
3921}
3922
3923void
3924input_receive_drag_data(struct input *input, const char *mime_type,
3925 data_func_t func, void *data)
3926{
3927 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003928 input->drag_offer->x = input->drag_x;
3929 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003930}
3931
3932int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003933input_receive_drag_data_to_fd(struct input *input,
3934 const char *mime_type, int fd)
3935{
3936 if (input->drag_offer)
3937 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3938
3939 return 0;
3940}
3941
3942int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003943input_receive_selection_data(struct input *input, const char *mime_type,
3944 data_func_t func, void *data)
3945{
3946 char **p;
3947
3948 if (input->selection_offer == NULL)
3949 return -1;
3950
3951 for (p = input->selection_offer->types.data; *p; p++)
3952 if (strcmp(mime_type, *p) == 0)
3953 break;
3954
3955 if (*p == NULL)
3956 return -1;
3957
3958 data_offer_receive_data(input->selection_offer,
3959 mime_type, func, data);
3960 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003961}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003962
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003963int
3964input_receive_selection_data_to_fd(struct input *input,
3965 const char *mime_type, int fd)
3966{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003967 if (input->selection_offer)
3968 wl_data_offer_receive(input->selection_offer->offer,
3969 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003970
3971 return 0;
3972}
3973
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003974void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003975window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003976{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003977 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003978 return;
3979
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003980 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003981}
3982
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003983static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003984surface_set_synchronized(struct surface *surface)
3985{
3986 if (!surface->subsurface)
3987 return;
3988
3989 if (surface->synchronized)
3990 return;
3991
3992 wl_subsurface_set_sync(surface->subsurface);
3993 surface->synchronized = 1;
3994}
3995
3996static void
3997surface_set_synchronized_default(struct surface *surface)
3998{
3999 if (!surface->subsurface)
4000 return;
4001
4002 if (surface->synchronized == surface->synchronized_default)
4003 return;
4004
4005 if (surface->synchronized_default)
4006 wl_subsurface_set_sync(surface->subsurface);
4007 else
4008 wl_subsurface_set_desync(surface->subsurface);
4009
4010 surface->synchronized = surface->synchronized_default;
4011}
4012
4013static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004014surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004015{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004016 struct widget *widget = surface->widget;
4017 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004018
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004019 if (surface->input_region) {
4020 wl_region_destroy(surface->input_region);
4021 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004022 }
4023
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004024 if (surface->opaque_region)
4025 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004026
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004027 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004028
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004029 if (widget->resize_handler)
4030 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004031 widget->allocation.width,
4032 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004033 widget->user_data);
4034
Pekka Paalanen35e82632013-04-25 13:57:48 +03004035 if (surface->subsurface &&
4036 (surface->allocation.x != widget->allocation.x ||
4037 surface->allocation.y != widget->allocation.y)) {
4038 wl_subsurface_set_position(surface->subsurface,
4039 widget->allocation.x,
4040 widget->allocation.y);
4041 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004042 if (surface->allocation.width != widget->allocation.width ||
4043 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004044 window_schedule_redraw(widget->window);
4045 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004046 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004047
4048 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004049 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004050 widget->allocation.width,
4051 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004052}
4053
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004054static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004055window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004056{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004057 struct surface *surface;
4058
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004059 widget_set_allocation(window->main_surface->widget,
4060 window->pending_allocation.x,
4061 window->pending_allocation.y,
4062 window->pending_allocation.width,
4063 window->pending_allocation.height);
4064
4065 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004066
4067 /* The main surface is in the list, too. Main surface's
4068 * resize_handler is responsible for calling widget_set_allocation()
4069 * on all sub-surface root widgets, so they will be resized
4070 * properly.
4071 */
4072 wl_list_for_each(surface, &window->subsurface_list, link) {
4073 if (surface == window->main_surface)
4074 continue;
4075
4076 surface_set_synchronized(surface);
4077 surface_resize(surface);
4078 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004079
4080 if (!window->fullscreen && !window->maximized)
4081 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004082
4083 if (window->confined && window->confined_widget) {
4084 struct wl_compositor *compositor = window->display->compositor;
4085 struct wl_region *region;
4086 struct widget *widget = window->confined_widget;
4087
4088 region = wl_compositor_create_region(compositor);
4089 wl_region_add(region,
4090 widget->allocation.x,
4091 widget->allocation.y,
4092 widget->allocation.width,
4093 widget->allocation.height);
4094 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4095 region);
4096 wl_region_destroy(region);
4097 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004098}
4099
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004100static void
4101idle_resize(struct window *window)
4102{
4103 window->resize_needed = 0;
4104 window->redraw_needed = 1;
4105
4106 DBG("from %dx%d to %dx%d\n",
4107 window->main_surface->server_allocation.width,
4108 window->main_surface->server_allocation.height,
4109 window->pending_allocation.width,
4110 window->pending_allocation.height);
4111
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004112 window_do_resize(window);
4113}
4114
4115static void
4116undo_resize(struct window *window)
4117{
4118 window->pending_allocation.width =
4119 window->main_surface->server_allocation.width;
4120 window->pending_allocation.height =
4121 window->main_surface->server_allocation.height;
4122
4123 DBG("back to %dx%d\n",
4124 window->main_surface->server_allocation.width,
4125 window->main_surface->server_allocation.height);
4126
4127 window_do_resize(window);
4128
4129 if (window->pending_allocation.width == 0 &&
4130 window->pending_allocation.height == 0) {
4131 fprintf(stderr, "Error: Could not draw a surface, "
4132 "most likely due to insufficient disk space in "
4133 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4134 exit(EXIT_FAILURE);
4135 }
4136}
4137
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004138void
4139window_schedule_resize(struct window *window, int width, int height)
4140{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004141 /* We should probably get these numbers from the theme. */
4142 const int min_width = 200, min_height = 200;
4143
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004144 window->pending_allocation.x = 0;
4145 window->pending_allocation.y = 0;
4146 window->pending_allocation.width = width;
4147 window->pending_allocation.height = height;
4148
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004149 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004150 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004151 window->min_allocation.width = min_width;
4152 else
4153 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004154 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004155 window->min_allocation.height = min_height;
4156 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004157 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004158 }
4159
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004160 if (window->pending_allocation.width < window->min_allocation.width)
4161 window->pending_allocation.width = window->min_allocation.width;
4162 if (window->pending_allocation.height < window->min_allocation.height)
4163 window->pending_allocation.height = window->min_allocation.height;
4164
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004165 window->resize_needed = 1;
4166 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004167}
4168
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004169void
4170widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4171{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004172 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004173}
4174
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004175static int
4176window_get_shadow_margin(struct window *window)
4177{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004178 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004179 return frame_get_shadow_margin(window->frame->frame);
4180 else
4181 return 0;
4182}
4183
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004184static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004185handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004186 int32_t width, int32_t height,
4187 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004188{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004189 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004190 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004191
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004192 window->maximized = 0;
4193 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004194 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004195 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004196
4197 wl_array_for_each(p, states) {
4198 uint32_t state = *p;
4199 switch (state) {
4200 case XDG_SURFACE_STATE_MAXIMIZED:
4201 window->maximized = 1;
4202 break;
4203 case XDG_SURFACE_STATE_FULLSCREEN:
4204 window->fullscreen = 1;
4205 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004206 case XDG_SURFACE_STATE_RESIZING:
4207 window->resizing = 1;
4208 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004209 case XDG_SURFACE_STATE_ACTIVATED:
4210 window->focused = 1;
4211 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004212 default:
4213 /* Unknown state */
4214 break;
4215 }
4216 }
4217
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004218 if (window->frame) {
4219 if (window->maximized) {
4220 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4221 } else {
4222 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4223 }
4224
4225 if (window->focused) {
4226 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4227 } else {
4228 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4229 }
4230 }
4231
Jasper St. Pierref184c382014-05-06 08:33:27 -04004232 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004233 /* The width / height params are for window geometry,
4234 * but window_schedule_resize takes allocation. Add
4235 * on the shadow margin to get the difference. */
4236 int margin = window_get_shadow_margin(window);
4237
4238 window_schedule_resize(window,
4239 width + margin * 2,
4240 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004241 } else {
4242 window_schedule_resize(window,
4243 window->saved_allocation.width,
4244 window->saved_allocation.height);
4245 }
4246
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004247 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004248
4249 if (window->state_changed_handler)
4250 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004251}
4252
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004253static void
4254handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4255{
4256 struct window *window = data;
4257 window_close(window);
4258}
4259
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004260static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004261 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004262 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004263};
4264
4265static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004266window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004267{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004268 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004269
4270 if (!window->xdg_surface)
4271 return;
4272
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004273 if (window->parent == window->last_parent)
4274 return;
4275
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004276 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004277 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004278 else
4279 parent_surface = NULL;
4280
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004281 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004282 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004283}
4284
4285static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004286window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004287{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004288 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004289 frame_input_rect(window->frame->frame,
4290 &geometry->x,
4291 &geometry->y,
4292 &geometry->width,
4293 &geometry->height);
4294 else
4295 window_get_allocation(window, geometry);
4296}
4297
4298static void
4299window_sync_geometry(struct window *window)
4300{
4301 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004302
4303 if (!window->xdg_surface)
4304 return;
4305
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004306 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004307 if (geometry.x == window->last_geometry.x &&
4308 geometry.y == window->last_geometry.y &&
4309 geometry.width == window->last_geometry.width &&
4310 geometry.height == window->last_geometry.height)
4311 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004312
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004313 xdg_surface_set_window_geometry(window->xdg_surface,
4314 geometry.x,
4315 geometry.y,
4316 geometry.width,
4317 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004318 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004319}
4320
4321static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004322window_flush(struct window *window)
4323{
4324 struct surface *surface;
4325
4326 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004327 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004328 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004329 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004330 }
4331 }
4332
4333 wl_list_for_each(surface, &window->subsurface_list, link) {
4334 if (surface == window->main_surface)
4335 continue;
4336
4337 surface_flush(surface);
4338 }
4339
4340 surface_flush(window->main_surface);
4341}
4342
4343static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004344menu_destroy(struct menu *menu)
4345{
4346 widget_destroy(menu->widget);
4347 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004348 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004349 free(menu);
4350}
4351
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004352void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004353window_get_allocation(struct window *window,
4354 struct rectangle *allocation)
4355{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004356 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004357}
4358
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004359static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004360widget_redraw(struct widget *widget)
4361{
4362 struct widget *child;
4363
4364 if (widget->redraw_handler)
4365 widget->redraw_handler(widget, widget->user_data);
4366 wl_list_for_each(child, &widget->child_list, link)
4367 widget_redraw(child);
4368}
4369
4370static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004371frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4372{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004373 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004374
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004375 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004376 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004377 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004378 surface->frame_cb = NULL;
4379
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004380 surface->last_time = time;
4381
Pekka Paalanen71233882013-04-25 13:57:53 +03004382 if (surface->redraw_needed || surface->window->redraw_needed) {
4383 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004384 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004385 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004386}
4387
4388static const struct wl_callback_listener listener = {
4389 frame_callback
4390};
4391
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004392static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004393surface_redraw(struct surface *surface)
4394{
Pekka Paalanen71233882013-04-25 13:57:53 +03004395 DBG_OBJ(surface->surface, "begin\n");
4396
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004397 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004398 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004399
4400 /* Whole-window redraw forces a redraw even if the previous has
4401 * not yet hit the screen.
4402 */
4403 if (surface->frame_cb) {
4404 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004405 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004406
Pekka Paalanen71233882013-04-25 13:57:53 +03004407 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004408 wl_callback_destroy(surface->frame_cb);
4409 }
4410
Neil Roberts97b747c2013-12-19 16:17:12 +00004411 if (surface->widget->use_cairo &&
4412 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004413 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004414 return -1;
4415 }
4416
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004417 surface->frame_cb = wl_surface_frame(surface->surface);
4418 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004419 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004420
4421 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004422 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004423 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004424 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004425 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004426}
4427
4428static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004429idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004430{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004431 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004432 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004433 int failed = 0;
4434 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004435
Pekka Paalanen71233882013-04-25 13:57:53 +03004436 DBG(" --------- \n");
4437
Pekka Paalaneneebff542013-04-25 13:57:52 +03004438 wl_list_init(&window->redraw_task.link);
4439 window->redraw_task_scheduled = 0;
4440
4441 if (window->resize_needed) {
4442 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004443 if (window->main_surface->frame_cb) {
4444 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004445 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004446 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004447
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004448 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004449 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004450 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004451
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004452 if (surface_redraw(window->main_surface) < 0) {
4453 /*
4454 * Only main_surface failure will cause us to undo the resize.
4455 * If sub-surfaces fail, they will just be broken with old
4456 * content.
4457 */
4458 failed = 1;
4459 } else {
4460 wl_list_for_each(surface, &window->subsurface_list, link) {
4461 if (surface == window->main_surface)
4462 continue;
4463
4464 surface_redraw(surface);
4465 }
4466 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004467
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004468 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004469 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004470
4471 wl_list_for_each(surface, &window->subsurface_list, link)
4472 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004473
4474 if (resized && failed) {
4475 /* Restore widget tree to correspond to what is on screen. */
4476 undo_resize(window);
4477 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004478}
4479
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004480static void
4481window_schedule_redraw_task(struct window *window)
4482{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004483 if (!window->redraw_task_scheduled) {
4484 window->redraw_task.run = idle_redraw;
4485 display_defer(window->display, &window->redraw_task);
4486 window->redraw_task_scheduled = 1;
4487 }
4488}
4489
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004490void
4491window_schedule_redraw(struct window *window)
4492{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004493 struct surface *surface;
4494
Pekka Paalanen71233882013-04-25 13:57:53 +03004495 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4496
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004497 wl_list_for_each(surface, &window->subsurface_list, link)
4498 surface->redraw_needed = 1;
4499
4500 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004501}
4502
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004503int
4504window_is_fullscreen(struct window *window)
4505{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004506 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004507}
4508
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004509void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004510window_set_fullscreen(struct window *window, int fullscreen)
4511{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004512 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004513 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004514
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004515 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004516 return;
4517
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004518 if (fullscreen)
4519 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4520 else
4521 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004522}
4523
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004524int
4525window_is_maximized(struct window *window)
4526{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004527 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004528}
4529
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004530void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004531window_set_maximized(struct window *window, int maximized)
4532{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004533 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004534 return;
4535
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004536 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004537 return;
4538
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004539 if (maximized)
4540 xdg_surface_set_maximized(window->xdg_surface);
4541 else
4542 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004543}
4544
Jasper St. Pierrede680992014-04-10 17:23:49 -07004545int
4546window_is_resizing(struct window *window)
4547{
4548 return window->resizing;
4549}
4550
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004551void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004552window_set_minimized(struct window *window)
4553{
4554 if (!window->xdg_surface)
4555 return;
4556
4557 xdg_surface_set_minimized(window->xdg_surface);
4558}
4559
4560void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004561window_set_user_data(struct window *window, void *data)
4562{
4563 window->user_data = data;
4564}
4565
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004566void *
4567window_get_user_data(struct window *window)
4568{
4569 return window->user_data;
4570}
4571
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004572void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004573window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004574 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004575{
4576 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004577}
4578
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004579void
4580window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004581 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004582{
4583 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004584}
4585
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004586void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004587window_set_data_handler(struct window *window, window_data_handler_t handler)
4588{
4589 window->data_handler = handler;
4590}
4591
4592void
4593window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4594{
4595 window->drop_handler = handler;
4596}
4597
4598void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004599window_set_close_handler(struct window *window,
4600 window_close_handler_t handler)
4601{
4602 window->close_handler = handler;
4603}
4604
4605void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004606window_set_fullscreen_handler(struct window *window,
4607 window_fullscreen_handler_t handler)
4608{
4609 window->fullscreen_handler = handler;
4610}
4611
4612void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004613window_set_output_handler(struct window *window,
4614 window_output_handler_t handler)
4615{
4616 window->output_handler = handler;
4617}
4618
4619void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004620window_set_state_changed_handler(struct window *window,
4621 window_state_changed_handler_t handler)
4622{
4623 window->state_changed_handler = handler;
4624}
4625
4626void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004627window_set_pointer_locked_handler(struct window *window,
4628 locked_pointer_locked_handler_t locked,
4629 locked_pointer_unlocked_handler_t unlocked)
4630{
4631 window->pointer_unlocked_handler = unlocked;
4632 window->pointer_locked_handler = locked;
4633}
4634
4635void
4636window_set_pointer_confined_handler(struct window *window,
4637 confined_pointer_confined_handler_t confined,
4638 confined_pointer_unconfined_handler_t unconfined)
4639{
4640 window->pointer_confined_handler = confined;
4641 window->pointer_unconfined_handler = unconfined;
4642}
4643
4644void
4645window_set_locked_pointer_motion_handler(struct window *window,
4646 window_locked_pointer_motion_handler_t handler)
4647{
4648 window->locked_pointer_motion_handler = handler;
4649}
4650
4651void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004652window_set_title(struct window *window, const char *title)
4653{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004654 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004655 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004656 if (window->frame) {
4657 frame_set_title(window->frame->frame, title);
4658 widget_schedule_redraw(window->frame->widget);
4659 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004660 if (window->xdg_surface)
4661 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004662}
4663
4664const char *
4665window_get_title(struct window *window)
4666{
4667 return window->title;
4668}
4669
4670void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004671window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4672{
4673 struct text_cursor_position *text_cursor_position =
4674 window->display->text_cursor_position;
4675
Scott Moreau9295ce02012-06-01 12:46:10 -06004676 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004677 return;
4678
4679 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004680 window->main_surface->surface,
4681 wl_fixed_from_int(x),
4682 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004683}
4684
Casey Dahlin9074db52012-04-19 22:50:09 -04004685static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004686relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4687 uint32_t utime_hi,
4688 uint32_t utime_lo,
4689 wl_fixed_t dx,
4690 wl_fixed_t dy,
4691 wl_fixed_t dx_unaccel,
4692 wl_fixed_t dy_unaccel)
4693{
4694 struct input *input = data;
4695 struct window *window = input->pointer_focus;
4696 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4697
4698 if (window->locked_pointer_motion_handler &&
4699 window->pointer_locked) {
4700 window->locked_pointer_motion_handler(
4701 window, input, ms,
4702 wl_fixed_to_double(dx),
4703 wl_fixed_to_double(dy),
4704 window->user_data);
4705 }
4706}
4707
4708static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4709 relative_pointer_handle_motion,
4710};
4711
4712static void
4713locked_pointer_locked(void *data,
4714 struct zwp_locked_pointer_v1 *locked_pointer)
4715{
4716 struct input *input = data;
4717 struct window *window = input->pointer_focus;
4718
4719 window->pointer_locked = true;
4720
4721 if (window->pointer_locked_handler) {
4722 window->pointer_locked_handler(window,
4723 input,
4724 window->user_data);
4725 }
4726}
4727
4728static void
4729locked_pointer_unlocked(void *data,
4730 struct zwp_locked_pointer_v1 *locked_pointer)
4731{
4732 struct input *input = data;
4733 struct window *window = input->pointer_focus;
4734
4735 window_unlock_pointer(window);
4736
4737 if (window->pointer_unlocked_handler) {
4738 window->pointer_unlocked_handler(window,
4739 input,
4740 window->user_data);
4741 }
4742}
4743
4744static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4745 locked_pointer_locked,
4746 locked_pointer_unlocked,
4747};
4748
4749int
4750window_lock_pointer(struct window *window, struct input *input)
4751{
4752 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4753 window->display->relative_pointer_manager;
4754 struct zwp_pointer_constraints_v1 *pointer_constraints =
4755 window->display->pointer_constraints;
4756 struct zwp_relative_pointer_v1 *relative_pointer;
4757 struct zwp_locked_pointer_v1 *locked_pointer;
4758
4759 if (!window->display->relative_pointer_manager)
4760 return -1;
4761
4762 if (!window->display->pointer_constraints)
4763 return -1;
4764
4765 if (window->locked_pointer)
4766 return -1;
4767
4768 if (window->confined_pointer)
4769 return -1;
4770
4771 if (!input->pointer)
4772 return -1;
4773
4774 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4775 relative_pointer_manager, input->pointer);
4776 zwp_relative_pointer_v1_add_listener(relative_pointer,
4777 &relative_pointer_listener,
4778 input);
4779
4780 locked_pointer =
4781 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4782 window->main_surface->surface,
4783 input->pointer,
4784 NULL,
4785 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4786 zwp_locked_pointer_v1_add_listener(locked_pointer,
4787 &locked_pointer_listener,
4788 input);
4789
4790 window->locked_input = input;
4791 window->locked_pointer = locked_pointer;
4792 window->relative_pointer = relative_pointer;
4793
4794 return 0;
4795}
4796
4797void
4798window_unlock_pointer(struct window *window)
4799{
4800 if (!window->locked_pointer)
4801 return;
4802
4803 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4804 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4805 window->locked_pointer = NULL;
4806 window->relative_pointer = NULL;
4807 window->pointer_locked = false;
4808 window->locked_input = NULL;
4809}
4810
4811void
4812widget_set_locked_pointer_cursor_hint(struct widget *widget,
4813 float x, float y)
4814{
4815 struct window *window = widget->window;
4816
4817 if (!window->locked_pointer)
4818 return;
4819
4820 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4821 wl_fixed_from_double(x),
4822 wl_fixed_from_double(y));
4823 wl_surface_commit(window->main_surface->surface);
4824}
4825
4826static void
4827confined_pointer_confined(void *data,
4828 struct zwp_confined_pointer_v1 *confined_pointer)
4829{
4830 struct input *input = data;
4831 struct window *window = input->pointer_focus;
4832
4833 window->confined = true;
4834
4835 if (window->pointer_confined_handler) {
4836 window->pointer_confined_handler(window,
4837 input,
4838 window->user_data);
4839 }
4840}
4841
4842static void
4843confined_pointer_unconfined(void *data,
4844 struct zwp_confined_pointer_v1 *confined_pointer)
4845{
4846 struct input *input = data;
4847 struct window *window = input->pointer_focus;
4848
4849 window_unconfine_pointer(window);
4850
4851 window->confined = false;
4852
4853 if (window->pointer_unconfined_handler) {
4854 window->pointer_unconfined_handler(window,
4855 input,
4856 window->user_data);
4857 }
4858}
4859
4860static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4861 confined_pointer_confined,
4862 confined_pointer_unconfined,
4863};
4864
4865int
4866window_confine_pointer_to_widget(struct window *window,
4867 struct widget *widget,
4868 struct input *input)
4869{
4870 struct zwp_pointer_constraints_v1 *pointer_constraints =
4871 window->display->pointer_constraints;
4872 struct zwp_confined_pointer_v1 *confined_pointer;
4873 struct wl_compositor *compositor = window->display->compositor;
4874 struct wl_region *region = NULL;
4875
4876 if (!window->display->pointer_constraints)
4877 return -1;
4878
4879 if (window->locked_pointer)
4880 return -1;
4881
4882 if (window->confined_pointer)
4883 return -1;
4884
4885 if (!input->pointer)
4886 return -1;
4887
4888 if (widget) {
4889 region = wl_compositor_create_region(compositor);
4890 wl_region_add(region,
4891 widget->allocation.x,
4892 widget->allocation.y,
4893 widget->allocation.width,
4894 widget->allocation.height);
4895 }
4896
4897 confined_pointer =
4898 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
4899 window->main_surface->surface,
4900 input->pointer,
4901 region,
4902 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4903 if (region)
4904 wl_region_destroy(region);
4905
4906 zwp_confined_pointer_v1_add_listener(confined_pointer,
4907 &confined_pointer_listener,
4908 input);
4909
4910 window->confined_pointer = confined_pointer;
4911 window->confined_widget = widget;
4912
4913 return 0;
4914}
4915
4916void
4917window_unconfine_pointer(struct window *window)
4918{
4919 if (!window->confined_pointer)
4920 return;
4921
4922 zwp_confined_pointer_v1_destroy(window->confined_pointer);
4923 window->confined_pointer = NULL;
4924 window->confined = false;
4925}
4926
4927static void
Casey Dahlin9074db52012-04-19 22:50:09 -04004928surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004929 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004930{
Rob Bradford7507b572012-05-15 17:55:34 +01004931 struct window *window = data;
4932 struct output *output;
4933 struct output *output_found = NULL;
4934 struct window_output *window_output;
4935
4936 wl_list_for_each(output, &window->display->output_list, link) {
4937 if (output->output == wl_output) {
4938 output_found = output;
4939 break;
4940 }
4941 }
4942
4943 if (!output_found)
4944 return;
4945
Brian Lovinbc919262013-08-07 15:34:59 -07004946 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004947 window_output->output = output_found;
4948
4949 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004950
4951 if (window->output_handler)
4952 window->output_handler(window, output_found, 1,
4953 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004954}
4955
4956static void
4957surface_leave(void *data,
4958 struct wl_surface *wl_surface, struct wl_output *output)
4959{
Rob Bradford7507b572012-05-15 17:55:34 +01004960 struct window *window = data;
4961 struct window_output *window_output;
4962 struct window_output *window_output_found = NULL;
4963
4964 wl_list_for_each(window_output, &window->window_output_list, link) {
4965 if (window_output->output->output == output) {
4966 window_output_found = window_output;
4967 break;
4968 }
4969 }
4970
4971 if (window_output_found) {
4972 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004973
4974 if (window->output_handler)
4975 window->output_handler(window, window_output->output,
4976 0, window->user_data);
4977
Rob Bradford7507b572012-05-15 17:55:34 +01004978 free(window_output_found);
4979 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004980}
4981
4982static const struct wl_surface_listener surface_listener = {
4983 surface_enter,
4984 surface_leave
4985};
4986
Pekka Paalanen4e373742013-02-13 16:17:13 +02004987static struct surface *
4988surface_create(struct window *window)
4989{
4990 struct display *display = window->display;
4991 struct surface *surface;
4992
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004993 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004994 surface->window = window;
4995 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004996 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004997 wl_surface_add_listener(surface->surface, &surface_listener, window);
4998
Pekka Paalanen35e82632013-04-25 13:57:48 +03004999 wl_list_insert(&window->subsurface_list, &surface->link);
5000
Pekka Paalanen4e373742013-02-13 16:17:13 +02005001 return surface;
5002}
5003
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005004static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005005get_preferred_buffer_type(struct display *display)
5006{
5007#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005008 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005009 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5010#endif
5011
5012 return WINDOW_BUFFER_TYPE_SHM;
5013}
5014
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005015static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005016window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005017{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005018 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005019 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005020
Peter Huttererf3d62272013-08-08 11:57:05 +10005021 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005022 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005023 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005024
5025 surface = surface_create(window);
5026 window->main_surface = surface;
5027
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005028 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005029
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005030 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005031 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005032
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005033 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005034
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005035 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005036 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005037 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005038
Rob Bradford7507b572012-05-15 17:55:34 +01005039 wl_list_init (&window->window_output_list);
5040
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005041 return window;
5042}
5043
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005044struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005045window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005046{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005047 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005048 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005049
5050 window = window_create_internal(display, 0);
5051
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005052 if (window->display->xdg_shell) {
5053 window->xdg_surface =
5054 xdg_shell_get_xdg_surface(window->display->xdg_shell,
5055 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005056 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005057
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005058 xdg_surface_set_user_data(window->xdg_surface, window);
5059 xdg_surface_add_listener(window->xdg_surface,
5060 &xdg_surface_listener, window);
5061 } else if (display->ivi_application) {
5062 /* auto generation of ivi_id based on process id + basement of id */
5063 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5064 window->ivi_surface =
5065 ivi_application_surface_create(display->ivi_application,
5066 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005067 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005068
5069 ivi_surface_add_listener(window->ivi_surface,
5070 &ivi_surface_listener, window);
5071 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005072
5073 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005074}
5075
5076struct window *
5077window_create_custom(struct display *display)
5078{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005079 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005080}
5081
Jasper St. Pierre53686042013-12-09 15:26:25 -05005082void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005083window_set_parent(struct window *window,
5084 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005085{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005086 window->parent = parent_window;
5087 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005088}
5089
5090struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005091window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005092{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005093 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005094}
5095
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005096static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005097menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005098{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005099 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005100 int next;
5101
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005102 frame_interior(menu->frame, &x, &y, &width, &height);
5103 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005104 if (menu->current != next) {
5105 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005106 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005107 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005108}
5109
5110static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005111menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005112 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005113 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005114{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005115 struct menu *menu = data;
5116
5117 if (widget == menu->widget)
5118 menu_set_item(data, y);
5119
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005120 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005121}
5122
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005123static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005124menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005125 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005126{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005127 struct menu *menu = data;
5128
5129 if (widget == menu->widget)
5130 menu_set_item(data, y);
5131
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005132 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005133}
5134
5135static void
5136menu_leave_handler(struct widget *widget, struct input *input, void *data)
5137{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005138 struct menu *menu = data;
5139
5140 if (widget == menu->widget)
5141 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005142}
5143
5144static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005145menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005146 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005147 uint32_t button, enum wl_pointer_button_state state,
5148 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005149
5150{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005151 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005152
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005153 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5154 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005155 /* Either relase after press-drag-release or
5156 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005157 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005158 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005159 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005160 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005161 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005162 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005163}
5164
5165static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005166menu_touch_up_handler(struct widget *widget,
5167 struct input *input,
5168 uint32_t serial,
5169 uint32_t time,
5170 int32_t id,
5171 void *data)
5172{
5173 struct menu *menu = data;
5174
5175 input_ungrab(input);
5176 menu_destroy(menu);
5177}
5178
5179static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005180menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005181{
5182 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005183 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005184 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005185
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005186 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005187
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005188 frame_repaint(menu->frame, cr);
5189 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005190
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005191 theme_set_background_source(menu->window->display->theme,
5192 cr, THEME_FRAME_ACTIVE);
5193 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005194 cairo_fill(cr);
5195
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005196 cairo_select_font_face(cr, "sans",
5197 CAIRO_FONT_SLANT_NORMAL,
5198 CAIRO_FONT_WEIGHT_NORMAL);
5199 cairo_set_font_size(cr, 12);
5200
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005201 for (i = 0; i < menu->count; i++) {
5202 if (i == menu->current) {
5203 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005204 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005205 cairo_fill(cr);
5206 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005207 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005208 cairo_show_text(cr, menu->entries[i]);
5209 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005210 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5211 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005212 cairo_show_text(cr, menu->entries[i]);
5213 }
5214 }
5215
5216 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005217}
5218
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005219static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08005220handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005221{
5222 struct window *window = data;
5223 struct menu *menu = window->main_surface->widget->user_data;
5224
5225 input_ungrab(menu->input);
5226 menu_destroy(menu);
5227}
5228
5229static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005230 handle_popup_popup_done,
5231};
5232
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005233static struct menu *
5234create_menu(struct display *display,
5235 struct input *input, uint32_t time,
5236 menu_func_t func, const char **entries, int count,
5237 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005238{
5239 struct window *window;
5240 struct menu *menu;
5241
5242 menu = malloc(sizeof *menu);
5243 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005244 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005245
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005246 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005247 if (!window) {
5248 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005249 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005250 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005251
5252 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005253 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005254 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005255 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07005256 FRAME_BUTTON_NONE, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005257 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005258 menu->entries = entries;
5259 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005260 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005261 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005262 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005263 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005264 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005265
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005266 input_ungrab(input);
5267
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005268 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005269 widget_set_enter_handler(menu->widget, menu_enter_handler);
5270 widget_set_leave_handler(menu->widget, menu_leave_handler);
5271 widget_set_motion_handler(menu->widget, menu_motion_handler);
5272 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005273 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005274
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005275 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005276 frame_resize_inside(menu->frame, 200, count * 20);
5277 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5278 window_schedule_resize(window, frame_width(menu->frame),
5279 frame_height(menu->frame));
5280
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005281 return menu;
5282}
5283
5284struct window *
5285window_create_menu(struct display *display,
5286 struct input *input, uint32_t time,
5287 menu_func_t func, const char **entries, int count,
5288 void *user_data)
5289{
5290 struct menu *menu;
5291 menu = create_menu(display, input, time, func, entries, count, user_data);
5292
5293 if (menu == NULL)
5294 return NULL;
5295
5296 return menu->window;
5297}
5298
5299void
5300window_show_menu(struct display *display,
5301 struct input *input, uint32_t time, struct window *parent,
5302 int32_t x, int32_t y,
5303 menu_func_t func, const char **entries, int count)
5304{
5305 struct menu *menu;
5306 struct window *window;
5307 int32_t ix, iy;
5308
5309 menu = create_menu(display, input, time, func, entries, count, parent);
5310
5311 if (menu == NULL)
5312 return;
5313
5314 window = menu->window;
5315
5316 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5317 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5318
5319 window->x = x;
5320 window->y = y;
5321
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005322 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005323
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005324 if (!display->xdg_shell)
5325 return;
5326
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005327 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
5328 window->main_surface->surface,
5329 parent->main_surface->surface,
5330 input->seat,
5331 display_get_serial(window->display),
5332 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08005333 window->y - iy);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005334 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005335
5336 xdg_popup_set_user_data(window->xdg_popup, window);
5337 xdg_popup_add_listener(window->xdg_popup,
5338 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005339}
5340
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005341void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005342window_set_buffer_type(struct window *window, enum window_buffer_type type)
5343{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005344 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005345}
5346
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005347enum window_buffer_type
5348window_get_buffer_type(struct window *window)
5349{
5350 return window->main_surface->buffer_type;
5351}
5352
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005353void
5354window_set_preferred_format(struct window *window,
5355 enum preferred_format format)
5356{
5357 window->preferred_format = format;
5358}
5359
Pekka Paalanen35e82632013-04-25 13:57:48 +03005360struct widget *
5361window_add_subsurface(struct window *window, void *data,
5362 enum subsurface_mode default_mode)
5363{
5364 struct widget *widget;
5365 struct surface *surface;
5366 struct wl_surface *parent;
5367 struct wl_subcompositor *subcompo = window->display->subcompositor;
5368
Pekka Paalanen35e82632013-04-25 13:57:48 +03005369 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005370 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005371 widget = widget_create(window, surface, data);
5372 wl_list_init(&widget->link);
5373 surface->widget = widget;
5374
5375 parent = window->main_surface->surface;
5376 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5377 surface->surface,
5378 parent);
5379 surface->synchronized = 1;
5380
5381 switch (default_mode) {
5382 case SUBSURFACE_SYNCHRONIZED:
5383 surface->synchronized_default = 1;
5384 break;
5385 case SUBSURFACE_DESYNCHRONIZED:
5386 surface->synchronized_default = 0;
5387 break;
5388 default:
5389 assert(!"bad enum subsurface_mode");
5390 }
5391
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005392 window->resize_needed = 1;
5393 window_schedule_redraw(window);
5394
Pekka Paalanen35e82632013-04-25 13:57:48 +03005395 return widget;
5396}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005397
5398static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005399display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005400 struct wl_output *wl_output,
5401 int x, int y,
5402 int physical_width,
5403 int physical_height,
5404 int subpixel,
5405 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005406 const char *model,
5407 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005408{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005409 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005410
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005411 output->allocation.x = x;
5412 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005413 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005414
5415 if (output->make)
5416 free(output->make);
5417 output->make = strdup(make);
5418
5419 if (output->model)
5420 free(output->model);
5421 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005422}
5423
5424static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005425display_handle_done(void *data,
5426 struct wl_output *wl_output)
5427{
5428}
5429
5430static void
5431display_handle_scale(void *data,
5432 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005433 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005434{
5435 struct output *output = data;
5436
5437 output->scale = scale;
5438}
5439
5440static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005441display_handle_mode(void *data,
5442 struct wl_output *wl_output,
5443 uint32_t flags,
5444 int width,
5445 int height,
5446 int refresh)
5447{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005448 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005449 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005450
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005451 if (flags & WL_OUTPUT_MODE_CURRENT) {
5452 output->allocation.width = width;
5453 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005454 if (display->output_configure_handler)
5455 (*display->output_configure_handler)(
5456 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005457 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005458}
5459
5460static const struct wl_output_listener output_listener = {
5461 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005462 display_handle_mode,
5463 display_handle_done,
5464 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005465};
5466
5467static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005468display_add_output(struct display *d, uint32_t id)
5469{
5470 struct output *output;
5471
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005472 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005473 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005474 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005475 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005476 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005477 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005478 wl_list_insert(d->output_list.prev, &output->link);
5479
5480 wl_output_add_listener(output->output, &output_listener, output);
5481}
5482
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005483static void
5484output_destroy(struct output *output)
5485{
5486 if (output->destroy_handler)
5487 (*output->destroy_handler)(output, output->user_data);
5488
5489 wl_output_destroy(output->output);
5490 wl_list_remove(&output->link);
5491 free(output);
5492}
5493
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005494static void
5495display_destroy_output(struct display *d, uint32_t id)
5496{
5497 struct output *output;
5498
5499 wl_list_for_each(output, &d->output_list, link) {
5500 if (output->server_output_id == id) {
5501 output_destroy(output);
5502 break;
5503 }
5504 }
5505}
5506
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005507void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005508display_set_global_handler(struct display *display,
5509 display_global_handler_t handler)
5510{
5511 struct global *global;
5512
5513 display->global_handler = handler;
5514 if (!handler)
5515 return;
5516
5517 wl_list_for_each(global, &display->global_list, link)
5518 display->global_handler(display,
5519 global->name, global->interface,
5520 global->version, display->user_data);
5521}
5522
5523void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005524display_set_global_handler_remove(struct display *display,
5525 display_global_handler_t remove_handler)
5526{
5527 display->global_handler_remove = remove_handler;
5528 if (!remove_handler)
5529 return;
5530}
5531
5532void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005533display_set_output_configure_handler(struct display *display,
5534 display_output_handler_t handler)
5535{
5536 struct output *output;
5537
5538 display->output_configure_handler = handler;
5539 if (!handler)
5540 return;
5541
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005542 wl_list_for_each(output, &display->output_list, link) {
5543 if (output->allocation.width == 0 &&
5544 output->allocation.height == 0)
5545 continue;
5546
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005547 (*display->output_configure_handler)(output,
5548 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005549 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005550}
5551
5552void
5553output_set_user_data(struct output *output, void *data)
5554{
5555 output->user_data = data;
5556}
5557
5558void *
5559output_get_user_data(struct output *output)
5560{
5561 return output->user_data;
5562}
5563
5564void
5565output_set_destroy_handler(struct output *output,
5566 display_output_handler_t handler)
5567{
5568 output->destroy_handler = handler;
5569 /* FIXME: implement this, once we have way to remove outputs */
5570}
5571
5572void
Scott Moreau4e072362012-09-29 02:03:11 -06005573output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005574{
Scott Moreau4e072362012-09-29 02:03:11 -06005575 struct rectangle allocation = output->allocation;
5576
5577 switch (output->transform) {
5578 case WL_OUTPUT_TRANSFORM_90:
5579 case WL_OUTPUT_TRANSFORM_270:
5580 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5581 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5582 /* Swap width and height */
5583 allocation.width = output->allocation.height;
5584 allocation.height = output->allocation.width;
5585 break;
5586 }
5587
5588 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005589}
5590
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005591struct wl_output *
5592output_get_wl_output(struct output *output)
5593{
5594 return output->output;
5595}
5596
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005597enum wl_output_transform
5598output_get_transform(struct output *output)
5599{
5600 return output->transform;
5601}
5602
Alexander Larssonafd319a2013-05-22 14:41:27 +02005603uint32_t
5604output_get_scale(struct output *output)
5605{
5606 return output->scale;
5607}
5608
Jason Ekstrand738715d2014-04-02 19:53:50 -05005609const char *
5610output_get_make(struct output *output)
5611{
5612 return output->make;
5613}
5614
5615const char *
5616output_get_model(struct output *output)
5617{
5618 return output->model;
5619}
5620
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005621static void
Daniel Stone97f68542012-05-30 16:32:01 +01005622fini_xkb(struct input *input)
5623{
5624 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005625 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005626}
5627
5628static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005629display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005630{
5631 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005632 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005633
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005634 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005635 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005636 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005637 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005638 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005639 input->pointer_focus = NULL;
5640 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005641 input->seat_version = seat_version;
5642
Rusty Lynch041815a2013-08-08 21:20:38 -07005643 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005644 wl_list_insert(d->input_list.prev, &input->link);
5645
Daniel Stone37816df2012-05-16 18:45:18 +01005646 wl_seat_add_listener(input->seat, &seat_listener, input);
5647 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005648
Jason Ekstranda669bd52014-04-02 19:53:51 -05005649 if (d->data_device_manager) {
5650 input->data_device =
5651 wl_data_device_manager_get_data_device(d->data_device_manager,
5652 input->seat);
5653 wl_data_device_add_listener(input->data_device,
5654 &data_device_listener,
5655 input);
5656 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005657
5658 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005659 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005660
Derek Foreman118a4292015-04-22 17:23:35 -05005661 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5662 TFD_CLOEXEC | TFD_NONBLOCK);
5663 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5664 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005665 set_repeat_info(input, 40, 400);
5666
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005667 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5668 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005669 input->repeat_task.run = keyboard_repeat_func;
5670 display_watch_fd(d, input->repeat_timer_fd,
5671 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005672}
5673
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005674static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005675input_destroy(struct input *input)
5676{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005677 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005678 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005679
5680 if (input->drag_offer)
5681 data_offer_destroy(input->drag_offer);
5682
5683 if (input->selection_offer)
5684 data_offer_destroy(input->selection_offer);
5685
kabeer khan6ce67ec2014-10-20 11:55:29 +05305686 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005687 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305688 wl_data_device_release(input->data_device);
5689 else
5690 wl_data_device_destroy(input->data_device);
5691 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005692 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005693 if (input->touch)
5694 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005695 if (input->pointer)
5696 wl_pointer_release(input->pointer);
5697 if (input->keyboard)
5698 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005699 } else {
5700 if (input->touch)
5701 wl_touch_destroy(input->touch);
5702 if (input->pointer)
5703 wl_pointer_destroy(input->pointer);
5704 if (input->keyboard)
5705 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005706 }
5707
Daniel Stone97f68542012-05-30 16:32:01 +01005708 fini_xkb(input);
5709
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005710 wl_surface_destroy(input->pointer_surface);
5711
Pekka Paalanene1207c72011-12-16 12:02:09 +02005712 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005713 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005714 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005715 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005716 free(input);
5717}
5718
5719static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005720shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5721{
5722 struct display *d = data;
5723
5724 if (format == WL_SHM_FORMAT_RGB565)
5725 d->has_rgb565 = 1;
5726}
5727
5728struct wl_shm_listener shm_listener = {
5729 shm_format
5730};
5731
5732static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005733xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5734{
5735 xdg_shell_pong(shell, serial);
5736}
5737
5738static const struct xdg_shell_listener xdg_shell_listener = {
5739 xdg_shell_ping,
5740};
5741
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005742#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005743#ifdef static_assert
5744static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5745 "Interface version doesn't match implementation version");
5746#endif
5747
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005748static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005749registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5750 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005751{
5752 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005753 struct global *global;
5754
Brian Lovinbc919262013-08-07 15:34:59 -07005755 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005756 global->name = id;
5757 global->interface = strdup(interface);
5758 global->version = version;
5759 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005760
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005761 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005762 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005763 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005764 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005765 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005766 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005767 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005768 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5769 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5770 d->relative_pointer_manager =
5771 wl_registry_bind(registry, id,
5772 &zwp_relative_pointer_manager_v1_interface,
5773 1);
5774 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5775 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5776 d->pointer_constraints =
5777 wl_registry_bind(registry, id,
5778 &zwp_pointer_constraints_v1_interface,
5779 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005780 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005781 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005782 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005783 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005784 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005785 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005786 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305787 &wl_data_device_manager_interface,
5788 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005789 } else if (strcmp(interface, "xdg_shell") == 0) {
5790 d->xdg_shell = wl_registry_bind(registry, id,
5791 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005792 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005793 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005794 } else if (strcmp(interface, "text_cursor_position") == 0) {
5795 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005796 wl_registry_bind(registry, id,
5797 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005798 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5799 d->subcompositor =
5800 wl_registry_bind(registry, id,
5801 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005802 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005803 else if (strcmp(interface, "ivi_application") == 0) {
5804 d->ivi_application =
5805 wl_registry_bind(registry, id,
5806 &ivi_application_interface, 1);
5807 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005808
5809 if (d->global_handler)
5810 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005811}
5812
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005813static void
5814registry_handle_global_remove(void *data, struct wl_registry *registry,
5815 uint32_t name)
5816{
5817 struct display *d = data;
5818 struct global *global;
5819 struct global *tmp;
5820
5821 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5822 if (global->name != name)
5823 continue;
5824
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005825 if (strcmp(global->interface, "wl_output") == 0)
5826 display_destroy_output(d, name);
5827
5828 /* XXX: Should destroy remaining bound globals */
5829
5830 if (d->global_handler_remove)
5831 d->global_handler_remove(d, name, global->interface,
5832 global->version, d->user_data);
5833
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005834 wl_list_remove(&global->link);
5835 free(global->interface);
5836 free(global);
5837 }
5838}
5839
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005840void *
5841display_bind(struct display *display, uint32_t name,
5842 const struct wl_interface *interface, uint32_t version)
5843{
5844 return wl_registry_bind(display->registry, name, interface, version);
5845}
5846
5847static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005848 registry_handle_global,
5849 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005850};
5851
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005852#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005853static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005854init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005855{
5856 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005857 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005858
Rob Clark6396ed32012-03-11 19:48:41 -05005859#ifdef USE_CAIRO_GLESV2
5860# define GL_BIT EGL_OPENGL_ES2_BIT
5861#else
5862# define GL_BIT EGL_OPENGL_BIT
5863#endif
5864
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005865 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005866 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005867 EGL_RED_SIZE, 1,
5868 EGL_GREEN_SIZE, 1,
5869 EGL_BLUE_SIZE, 1,
5870 EGL_ALPHA_SIZE, 1,
5871 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005872 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005873 EGL_NONE
5874 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005875
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005876#ifdef USE_CAIRO_GLESV2
5877 static const EGLint context_attribs[] = {
5878 EGL_CONTEXT_CLIENT_VERSION, 2,
5879 EGL_NONE
5880 };
5881 EGLint api = EGL_OPENGL_ES_API;
5882#else
5883 EGLint *context_attribs = NULL;
5884 EGLint api = EGL_OPENGL_API;
5885#endif
5886
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005887 d->dpy =
5888 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5889 d->display, NULL);
5890
Yuval Fledel45568f62010-12-06 09:18:12 -05005891 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005892 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005893 return -1;
5894 }
5895
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005896 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005897 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005898 return -1;
5899 }
5900
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005901 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5902 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005903 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005904 return -1;
5905 }
5906
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005907 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005908 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005909 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005910 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005911 return -1;
5912 }
5913
Benjamin Franzke0c991632011-09-27 21:57:31 +02005914 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5915 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005916 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005917 return -1;
5918 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005919
5920 return 0;
5921}
5922
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005923static void
5924fini_egl(struct display *display)
5925{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005926 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005927
5928 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5929 EGL_NO_CONTEXT);
5930
5931 eglTerminate(display->dpy);
5932 eglReleaseThread();
5933}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005934#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005935
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005936static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005937init_dummy_surface(struct display *display)
5938{
5939 int len;
5940 void *data;
5941
5942 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005943 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005944 display->dummy_surface =
5945 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5946 1, 1, len);
5947 display->dummy_surface_data = data;
5948}
5949
5950static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005951handle_display_data(struct task *task, uint32_t events)
5952{
5953 struct display *display =
5954 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005955 struct epoll_event ep;
5956 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005957
5958 display->display_fd_events = events;
5959
5960 if (events & EPOLLERR || events & EPOLLHUP) {
5961 display_exit(display);
5962 return;
5963 }
5964
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005965 if (events & EPOLLIN) {
5966 ret = wl_display_dispatch(display->display);
5967 if (ret == -1) {
5968 display_exit(display);
5969 return;
5970 }
5971 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005972
5973 if (events & EPOLLOUT) {
5974 ret = wl_display_flush(display->display);
5975 if (ret == 0) {
5976 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5977 ep.data.ptr = &display->display_task;
5978 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5979 display->display_fd, &ep);
5980 } else if (ret == -1 && errno != EAGAIN) {
5981 display_exit(display);
5982 return;
5983 }
5984 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005985}
5986
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005987static void
5988log_handler(const char *format, va_list args)
5989{
5990 vfprintf(stderr, format, args);
5991}
5992
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005993struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005994display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005995{
5996 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005997
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005998 wl_log_set_handler_client(log_handler);
5999
Peter Huttererf3d62272013-08-08 11:57:05 +10006000 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006001 if (d == NULL)
6002 return NULL;
6003
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006004 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006005 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006006 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006007 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006008 return NULL;
6009 }
6010
Rob Bradford5ab9c752013-07-26 16:29:43 +01006011 d->xkb_context = xkb_context_new(0);
6012 if (d->xkb_context == NULL) {
6013 fprintf(stderr, "Failed to create XKB context\n");
6014 free(d);
6015 return NULL;
6016 }
6017
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006018 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006019 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006020 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006021 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6022 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006023
6024 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006025 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006026 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006027 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006028
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006029 d->registry = wl_display_get_registry(d->display);
6030 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006031
Marek Chalupaa519d062014-12-05 13:49:40 +01006032 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006033 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6034 return NULL;
6035 }
6036
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006037#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006038 if (init_egl(d) < 0)
6039 fprintf(stderr, "EGL does not seem to work, "
6040 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006041#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006042
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006043 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006044
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006045 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006046
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006047 wl_list_init(&d->window_list);
6048
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006049 init_dummy_surface(d);
6050
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006051 return d;
6052}
6053
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006054static void
6055display_destroy_outputs(struct display *display)
6056{
6057 struct output *tmp;
6058 struct output *output;
6059
6060 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6061 output_destroy(output);
6062}
6063
Pekka Paalanene1207c72011-12-16 12:02:09 +02006064static void
6065display_destroy_inputs(struct display *display)
6066{
6067 struct input *tmp;
6068 struct input *input;
6069
6070 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6071 input_destroy(input);
6072}
6073
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006074void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006075display_destroy(struct display *display)
6076{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006077 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006078 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6079 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006080
6081 if (!wl_list_empty(&display->deferred_list))
6082 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6083
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006084 cairo_surface_destroy(display->dummy_surface);
6085 free(display->dummy_surface_data);
6086
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006087 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006088 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006089
Daniel Stone97f68542012-05-30 16:32:01 +01006090 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006091
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006092 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006093 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006094
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006095#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006096 if (display->argb_device)
6097 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006098#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006099
Pekka Paalanen35e82632013-04-25 13:57:48 +03006100 if (display->subcompositor)
6101 wl_subcompositor_destroy(display->subcompositor);
6102
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006103 if (display->xdg_shell)
6104 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006105
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006106 if (display->ivi_application)
6107 ivi_application_destroy(display->ivi_application);
6108
Pekka Paalanenc2052982011-12-16 11:41:32 +02006109 if (display->shm)
6110 wl_shm_destroy(display->shm);
6111
6112 if (display->data_device_manager)
6113 wl_data_device_manager_destroy(display->data_device_manager);
6114
6115 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006116 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006117
6118 close(display->epoll_fd);
6119
U. Artie Eoff44874d92012-10-02 21:12:35 -07006120 if (!(display->display_fd_events & EPOLLERR) &&
6121 !(display->display_fd_events & EPOLLHUP))
6122 wl_display_flush(display->display);
6123
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006124 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006125 free(display);
6126}
6127
6128void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006129display_set_user_data(struct display *display, void *data)
6130{
6131 display->user_data = data;
6132}
6133
6134void *
6135display_get_user_data(struct display *display)
6136{
6137 return display->user_data;
6138}
6139
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006140struct wl_display *
6141display_get_display(struct display *display)
6142{
6143 return display->display;
6144}
6145
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006146int
6147display_has_subcompositor(struct display *display)
6148{
6149 if (display->subcompositor)
6150 return 1;
6151
6152 wl_display_roundtrip(display->display);
6153
6154 return display->subcompositor != NULL;
6155}
6156
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006157cairo_device_t *
6158display_get_cairo_device(struct display *display)
6159{
6160 return display->argb_device;
6161}
6162
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006163struct output *
6164display_get_output(struct display *display)
6165{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006166 if (wl_list_empty(&display->output_list))
6167 return NULL;
6168
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006169 return container_of(display->output_list.next, struct output, link);
6170}
6171
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006172struct wl_compositor *
6173display_get_compositor(struct display *display)
6174{
6175 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006176}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006177
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006178uint32_t
6179display_get_serial(struct display *display)
6180{
6181 return display->serial;
6182}
6183
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006184EGLDisplay
6185display_get_egl_display(struct display *d)
6186{
6187 return d->dpy;
6188}
6189
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006190struct wl_data_source *
6191display_create_data_source(struct display *display)
6192{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006193 if (display->data_device_manager)
6194 return wl_data_device_manager_create_data_source(display->data_device_manager);
6195 else
6196 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006197}
6198
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006199EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006200display_get_argb_egl_config(struct display *d)
6201{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006202 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006203}
6204
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006205int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006206display_acquire_window_surface(struct display *display,
6207 struct window *window,
6208 EGLContext ctx)
6209{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006210 struct surface *surface = window->main_surface;
6211
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006212 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006213 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006214
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006215 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006216 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006217}
6218
6219void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006220display_release_window_surface(struct display *display,
6221 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006222{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006223 struct surface *surface = window->main_surface;
6224
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006225 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006226 return;
6227
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006228 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006229}
6230
6231void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006232display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006233{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006234 wl_list_insert(&display->deferred_list, &task->link);
6235}
6236
6237void
6238display_watch_fd(struct display *display,
6239 int fd, uint32_t events, struct task *task)
6240{
6241 struct epoll_event ep;
6242
6243 ep.events = events;
6244 ep.data.ptr = task;
6245 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6246}
6247
6248void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006249display_unwatch_fd(struct display *display, int fd)
6250{
6251 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6252}
6253
6254void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006255display_run(struct display *display)
6256{
6257 struct task *task;
6258 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006259 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006260
Pekka Paalanen826d7952011-12-15 10:14:07 +02006261 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006262 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006263 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006264 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006265 struct task, link);
6266 wl_list_remove(&task->link);
6267 task->run(task, 0);
6268 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006269
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006270 wl_display_dispatch_pending(display->display);
6271
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006272 if (!display->running)
6273 break;
6274
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006275 ret = wl_display_flush(display->display);
6276 if (ret < 0 && errno == EAGAIN) {
6277 ep[0].events =
6278 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6279 ep[0].data.ptr = &display->display_task;
6280
6281 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6282 display->display_fd, &ep[0]);
6283 } else if (ret < 0) {
6284 break;
6285 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006286
6287 count = epoll_wait(display->epoll_fd,
6288 ep, ARRAY_LENGTH(ep), -1);
6289 for (i = 0; i < count; i++) {
6290 task = ep[i].data.ptr;
6291 task->run(task, ep[i].events);
6292 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006293 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006294}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006295
6296void
6297display_exit(struct display *display)
6298{
6299 display->running = 0;
6300}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006301
Carlos Garnacho9c931792016-01-18 23:52:12 +01006302int
6303display_get_data_device_manager_version(struct display *display)
6304{
6305 return display->data_device_manager_version;
6306}
6307
Jan Arne Petersencd997062012-11-18 19:06:44 +01006308void
6309keysym_modifiers_add(struct wl_array *modifiers_map,
6310 const char *name)
6311{
6312 size_t len = strlen(name) + 1;
6313 char *p;
6314
6315 p = wl_array_add(modifiers_map, len);
6316
6317 if (p == NULL)
6318 return;
6319
6320 strncpy(p, name, len);
6321}
6322
6323static xkb_mod_index_t
6324keysym_modifiers_get_index(struct wl_array *modifiers_map,
6325 const char *name)
6326{
6327 xkb_mod_index_t index = 0;
6328 char *p = modifiers_map->data;
6329
6330 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6331 if (strcmp(p, name) == 0)
6332 return index;
6333
6334 index++;
6335 p += strlen(p) + 1;
6336 }
6337
6338 return XKB_MOD_INVALID;
6339}
6340
6341xkb_mod_mask_t
6342keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6343 const char *name)
6344{
6345 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6346
6347 if (index == XKB_MOD_INVALID)
6348 return XKB_MOD_INVALID;
6349
6350 return 1 << index;
6351}