blob: 216ef968fea03271aed399947f97d2cba0b55f90 [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 Ådahlaf7f98b2016-08-09 18:15:43 +080074#include "xdg-shell-unstable-v6-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;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800106 struct zxdg_shell_v6 *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;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800235 int redraw_inhibited;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400236 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300237 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400238 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400239 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500240 int custom;
241 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400242
Pekka Paalanen99436862012-11-19 17:15:59 +0200243 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400244
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500245 int fullscreen;
246 int maximized;
247
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200248 enum preferred_format preferred_format;
249
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500250 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500251 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400252 window_data_handler_t data_handler;
253 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500254 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400255 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200256 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700257 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400258
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800259 window_locked_pointer_motion_handler_t locked_pointer_motion_handler;
260
Pekka Paalanen4e373742013-02-13 16:17:13 +0200261 struct surface *main_surface;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +0800262 struct zxdg_surface_v6 *xdg_surface;
263 struct zxdg_toplevel_v6 *xdg_toplevel;
264 struct zxdg_popup_v6 *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300265
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700266 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800267 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500268
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900269 struct ivi_surface *ivi_surface;
270
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500271 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500272
Pekka Paalanen35e82632013-04-25 13:57:48 +0300273 /* struct surface::link, contains also main_surface */
274 struct wl_list subsurface_list;
275
Jonas Ådahle5a1bb42014-11-25 10:25:27 +0800276 struct zwp_relative_pointer_v1 *relative_pointer;
277 struct zwp_locked_pointer_v1 *locked_pointer;
278 struct input *locked_input;
279 bool pointer_locked;
280 locked_pointer_locked_handler_t pointer_locked_handler;
281 locked_pointer_unlocked_handler_t pointer_unlocked_handler;
282 confined_pointer_confined_handler_t pointer_confined_handler;
283 confined_pointer_unconfined_handler_t pointer_unconfined_handler;
284
285 struct zwp_confined_pointer_v1 *confined_pointer;
286 struct widget *confined_widget;
287 bool confined;
288
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500289 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400290 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500291};
292
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500293struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500294 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200295 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300296 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500297 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400298 struct wl_list link;
299 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500300 widget_resize_handler_t resize_handler;
301 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500302 widget_enter_handler_t enter_handler;
303 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500304 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500305 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700306 widget_touch_down_handler_t touch_down_handler;
307 widget_touch_up_handler_t touch_up_handler;
308 widget_touch_motion_handler_t touch_motion_handler;
309 widget_touch_frame_handler_t touch_frame_handler;
310 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200311 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000312 widget_pointer_frame_handler_t pointer_frame_handler;
313 widget_axis_source_handler_t axis_source_handler;
314 widget_axis_stop_handler_t axis_stop_handler;
315 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400316 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500317 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300318 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500319 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000320 /* If this is set to false then no cairo surface will be
321 * created before redrawing the surface. This is useful if the
322 * redraw handler is going to do completely custom rendering
323 * such as using EGL directly */
324 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400325};
326
Rusty Lynch041815a2013-08-08 21:20:38 -0700327struct touch_point {
328 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800329 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700330 struct widget *widget;
331 struct wl_list link;
332};
333
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400334struct input {
335 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100336 struct wl_seat *seat;
337 struct wl_pointer *pointer;
338 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700339 struct wl_touch *touch;
340 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400341 struct window *pointer_focus;
342 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700343 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300344 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300345 uint32_t cursor_anim_start;
346 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500347 uint32_t cursor_timer_start;
348 uint32_t cursor_anim_current;
349 int cursor_delay_fd;
350 bool cursor_timer_running;
351 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300352 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400353 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400354 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400355 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400356 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400357 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400358
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500359 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500360 struct widget *grab;
361 uint32_t grab_button;
362
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400363 struct wl_data_device *data_device;
364 struct data_offer *drag_offer;
365 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800366 uint32_t touch_grab;
367 int32_t touch_grab_id;
368 float drag_x, drag_y;
369 struct window *drag_focus;
370 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100371
372 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100373 struct xkb_keymap *keymap;
374 struct xkb_state *state;
375 xkb_mod_mask_t control_mask;
376 xkb_mod_mask_t alt_mask;
377 xkb_mod_mask_t shift_mask;
378 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400379
Jonny Lamb06959082014-08-12 14:58:27 +0200380 int32_t repeat_rate_sec;
381 int32_t repeat_rate_nsec;
382 int32_t repeat_delay_sec;
383 int32_t repeat_delay_nsec;
384
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400385 struct task repeat_task;
386 int repeat_timer_fd;
387 uint32_t repeat_sym;
388 uint32_t repeat_key;
389 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500390 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400391};
392
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500393struct output {
394 struct display *display;
395 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800396 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500397 struct rectangle allocation;
398 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600399 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200400 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500401 char *make;
402 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200403
404 display_output_handler_t destroy_handler;
405 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500406};
407
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500408struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500409 struct widget *widget;
410 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500411 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800412
413 uint32_t last_time;
414 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800415 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500416};
417
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500418struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400419 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500420 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500421 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500422 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700423 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500424 const char **entries;
425 uint32_t time;
426 int current;
427 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400428 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500429 menu_func_t func;
430};
431
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300432struct tooltip {
433 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300434 struct widget *widget;
435 char *entry;
436 struct task tooltip_task;
437 int tooltip_fd;
438 float x, y;
439};
440
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700441struct shm_pool {
442 struct wl_shm_pool *pool;
443 size_t size;
444 size_t used;
445 void *data;
446};
447
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400448enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300449 CURSOR_DEFAULT = 100,
450 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400451};
452
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200453static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454
Pekka Paalanen97777442013-05-22 10:20:05 +0300455/* #define DEBUG */
456
457#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300458
459static void
460debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
461__attribute__ ((format (printf, 4, 5)));
462
463static void
464debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
465{
466 va_list ap;
467 struct timeval tv;
468
469 gettimeofday(&tv, NULL);
470 fprintf(stderr, "%8ld.%03ld ",
471 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
472
473 if (proxy)
474 fprintf(stderr, "%s@%d ",
475 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
476
477 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
478 fprintf(stderr, "%s ", func);
479
480 va_start(ap, fmt);
481 vfprintf(stderr, fmt, ap);
482 va_end(ap);
483}
484
485#define DBG(fmt, ...) \
486 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
487
488#define DBG_OBJ(obj, fmt, ...) \
489 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
490
491#else
492
493#define DBG(...) do {} while (0)
494#define DBG_OBJ(...) do {} while (0)
495
496#endif
497
Alexander Larsson1818e312013-05-22 14:41:31 +0200498static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200499surface_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 +0200500{
501 int32_t tmp;
502
503 switch (buffer_transform) {
504 case WL_OUTPUT_TRANSFORM_90:
505 case WL_OUTPUT_TRANSFORM_270:
506 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
507 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
508 tmp = *width;
509 *width = *height;
510 *height = tmp;
511 break;
512 default:
513 break;
514 }
515
516 *width *= buffer_scale;
517 *height *= buffer_scale;
518}
519
520static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200521buffer_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 +0200522{
523 int32_t tmp;
524
525 switch (buffer_transform) {
526 case WL_OUTPUT_TRANSFORM_90:
527 case WL_OUTPUT_TRANSFORM_270:
528 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
529 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
530 tmp = *width;
531 *width = *height;
532 *height = tmp;
533 break;
534 default:
535 break;
536 }
537
538 *width /= buffer_scale;
539 *height /= buffer_scale;
540}
541
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500542#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400543
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200544struct egl_window_surface {
545 struct toysurface base;
546 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100547 struct display *display;
548 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200549 struct wl_egl_window *egl_window;
550 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100551};
552
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200553static struct egl_window_surface *
554to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100555{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200556 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100557}
558
559static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200560egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200561 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200562 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100563{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200564 struct egl_window_surface *surface = to_egl_window_surface(base);
565
Alexander Larsson1818e312013-05-22 14:41:31 +0200566 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
567
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200568 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
569 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
570
571 return cairo_surface_reference(surface->cairo_surface);
572}
573
574static void
575egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200576 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200577 struct rectangle *server_allocation)
578{
579 struct egl_window_surface *surface = to_egl_window_surface(base);
580
581 cairo_gl_surface_swapbuffers(surface->cairo_surface);
582 wl_egl_window_get_attached_size(surface->egl_window,
583 &server_allocation->width,
584 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200585
586 buffer_to_surface_size (buffer_transform, buffer_scale,
587 &server_allocation->width,
588 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200589}
590
591static int
592egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
593{
594 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200595 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100596
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200597 device = cairo_surface_get_device(surface->cairo_surface);
598 if (!device)
599 return -1;
600
601 if (!ctx) {
602 if (device == surface->display->argb_device)
603 ctx = surface->display->argb_ctx;
604 else
605 assert(0);
606 }
607
608 cairo_device_flush(device);
609 cairo_device_acquire(device);
610 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
611 surface->egl_surface, ctx))
612 fprintf(stderr, "failed to make surface current\n");
613
614 return 0;
615}
616
617static void
618egl_window_surface_release(struct toysurface *base)
619{
620 struct egl_window_surface *surface = to_egl_window_surface(base);
621 cairo_device_t *device;
622
623 device = cairo_surface_get_device(surface->cairo_surface);
624 if (!device)
625 return;
626
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200627 if (!eglMakeCurrent(surface->display->dpy,
628 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200629 fprintf(stderr, "failed to make context current\n");
630
631 cairo_device_release(device);
632}
633
634static void
635egl_window_surface_destroy(struct toysurface *base)
636{
637 struct egl_window_surface *surface = to_egl_window_surface(base);
638 struct display *d = surface->display;
639
640 cairo_surface_destroy(surface->cairo_surface);
641 eglDestroySurface(d->dpy, surface->egl_surface);
642 wl_egl_window_destroy(surface->egl_window);
643 surface->surface = NULL;
644
645 free(surface);
646}
647
648static struct toysurface *
649egl_window_surface_create(struct display *display,
650 struct wl_surface *wl_surface,
651 uint32_t flags,
652 struct rectangle *rectangle)
653{
654 struct egl_window_surface *surface;
655
Pekka Paalanenb3627362012-11-19 17:16:00 +0200656 if (display->dpy == EGL_NO_DISPLAY)
657 return NULL;
658
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800659 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200660 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100661 return NULL;
662
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200663 surface->base.prepare = egl_window_surface_prepare;
664 surface->base.swap = egl_window_surface_swap;
665 surface->base.acquire = egl_window_surface_acquire;
666 surface->base.release = egl_window_surface_release;
667 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100668
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200669 surface->display = display;
670 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400671
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200672 surface->egl_window = wl_egl_window_create(surface->surface,
673 rectangle->width,
674 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100675
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100676 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100677 weston_platform_create_egl_surface(display->dpy,
678 display->argb_config,
679 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100680
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200681 surface->cairo_surface =
682 cairo_gl_surface_create_for_egl(display->argb_device,
683 surface->egl_surface,
684 rectangle->width,
685 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100686
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200687 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100688}
689
Pekka Paalanenb3627362012-11-19 17:16:00 +0200690#else
691
692static struct toysurface *
693egl_window_surface_create(struct display *display,
694 struct wl_surface *wl_surface,
695 uint32_t flags,
696 struct rectangle *rectangle)
697{
698 return NULL;
699}
700
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400701#endif
702
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200703struct shm_surface_data {
704 struct wl_buffer *buffer;
705 struct shm_pool *pool;
706};
707
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400708struct wl_buffer *
709display_get_buffer_for_surface(struct display *display,
710 cairo_surface_t *surface)
711{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200712 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400713
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200714 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400715
716 return data->buffer;
717}
718
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500719static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700720shm_pool_destroy(struct shm_pool *pool);
721
722static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400723shm_surface_data_destroy(void *p)
724{
725 struct shm_surface_data *data = p;
726
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200727 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700728 if (data->pool)
729 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300730
731 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400732}
733
Kristian Høgsberg16626282012-04-03 11:21:27 -0400734static struct wl_shm_pool *
735make_shm_pool(struct display *display, int size, void **data)
736{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400737 struct wl_shm_pool *pool;
738 int fd;
739
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300740 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400741 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300742 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
743 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400744 return NULL;
745 }
746
747 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400748 if (*data == MAP_FAILED) {
749 fprintf(stderr, "mmap failed: %m\n");
750 close(fd);
751 return NULL;
752 }
753
754 pool = wl_shm_create_pool(display->shm, fd, size);
755
756 close(fd);
757
758 return pool;
759}
760
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700761static struct shm_pool *
762shm_pool_create(struct display *display, size_t size)
763{
764 struct shm_pool *pool = malloc(sizeof *pool);
765
766 if (!pool)
767 return NULL;
768
769 pool->pool = make_shm_pool(display, size, &pool->data);
770 if (!pool->pool) {
771 free(pool);
772 return NULL;
773 }
774
775 pool->size = size;
776 pool->used = 0;
777
778 return pool;
779}
780
781static void *
782shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
783{
784 if (pool->used + size > pool->size)
785 return NULL;
786
787 *offset = pool->used;
788 pool->used += size;
789
790 return (char *) pool->data + *offset;
791}
792
793/* destroy the pool. this does not unmap the memory though */
794static void
795shm_pool_destroy(struct shm_pool *pool)
796{
797 munmap(pool->data, pool->size);
798 wl_shm_pool_destroy(pool->pool);
799 free(pool);
800}
801
802/* Start allocating from the beginning of the pool again */
803static void
804shm_pool_reset(struct shm_pool *pool)
805{
806 pool->used = 0;
807}
808
809static int
810data_length_for_shm_surface(struct rectangle *rect)
811{
812 int stride;
813
814 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
815 rect->width);
816 return stride * rect->height;
817}
818
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500819static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700820display_create_shm_surface_from_pool(struct display *display,
821 struct rectangle *rectangle,
822 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823{
824 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400825 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400826 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200827 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700828 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400829 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830
831 data = malloc(sizeof *data);
832 if (data == NULL)
833 return NULL;
834
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200835 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
836 cairo_format = CAIRO_FORMAT_RGB16_565;
837 else
838 cairo_format = CAIRO_FORMAT_ARGB32;
839
840 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700841 length = stride * rectangle->height;
842 data->pool = NULL;
843 map = shm_pool_allocate(pool, length, &offset);
844
845 if (!map) {
846 free(data);
847 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400848 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400849
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400850 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200851 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400852 rectangle->width,
853 rectangle->height,
854 stride);
855
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200856 cairo_surface_set_user_data(surface, &shm_surface_data_key,
857 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400858
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200859 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
860 format = WL_SHM_FORMAT_RGB565;
861 else {
862 if (flags & SURFACE_OPAQUE)
863 format = WL_SHM_FORMAT_XRGB8888;
864 else
865 format = WL_SHM_FORMAT_ARGB8888;
866 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400867
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200868 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
869 rectangle->width,
870 rectangle->height,
871 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400872
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700873 return surface;
874}
875
876static cairo_surface_t *
877display_create_shm_surface(struct display *display,
878 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200879 struct shm_pool *alternate_pool,
880 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700881{
882 struct shm_surface_data *data;
883 struct shm_pool *pool;
884 cairo_surface_t *surface;
885
Pekka Paalanen99436862012-11-19 17:15:59 +0200886 if (alternate_pool) {
887 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700888 surface = display_create_shm_surface_from_pool(display,
889 rectangle,
890 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200891 alternate_pool);
892 if (surface) {
893 data = cairo_surface_get_user_data(surface,
894 &shm_surface_data_key);
895 goto out;
896 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700897 }
898
899 pool = shm_pool_create(display,
900 data_length_for_shm_surface(rectangle));
901 if (!pool)
902 return NULL;
903
904 surface =
905 display_create_shm_surface_from_pool(display, rectangle,
906 flags, pool);
907
908 if (!surface) {
909 shm_pool_destroy(pool);
910 return NULL;
911 }
912
913 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200914 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700915 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400916
Pekka Paalanen99436862012-11-19 17:15:59 +0200917out:
918 if (data_ret)
919 *data_ret = data;
920
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400921 return surface;
922}
923
nobled7b87cb02011-02-01 18:51:47 +0000924static int
925check_size(struct rectangle *rect)
926{
927 if (rect->width && rect->height)
928 return 0;
929
930 fprintf(stderr, "tried to create surface of "
931 "width: %d, height: %d\n", rect->width, rect->height);
932 return -1;
933}
934
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400935cairo_surface_t *
936display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100937 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400938 struct rectangle *rectangle,
939 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400940{
nobled7b87cb02011-02-01 18:51:47 +0000941 if (check_size(rectangle) < 0)
942 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200943
944 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200945 return display_create_shm_surface(display, rectangle, flags,
946 NULL, NULL);
947}
948
Pekka Paalanena4eda732012-11-19 17:16:02 +0200949struct shm_surface_leaf {
950 cairo_surface_t *cairo_surface;
951 /* 'data' is automatically destroyed, when 'cairo_surface' is */
952 struct shm_surface_data *data;
953
954 struct shm_pool *resize_pool;
955 int busy;
956};
957
958static void
959shm_surface_leaf_release(struct shm_surface_leaf *leaf)
960{
961 if (leaf->cairo_surface)
962 cairo_surface_destroy(leaf->cairo_surface);
963 /* leaf->data already destroyed via cairo private */
964
965 if (leaf->resize_pool)
966 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200967
968 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200969}
970
Pekka Paalanenaef02542013-04-25 13:57:47 +0300971#define MAX_LEAVES 3
972
Pekka Paalanen99436862012-11-19 17:15:59 +0200973struct shm_surface {
974 struct toysurface base;
975 struct display *display;
976 struct wl_surface *surface;
977 uint32_t flags;
978 int dx, dy;
979
Pekka Paalanenaef02542013-04-25 13:57:47 +0300980 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200981 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200982};
983
984static struct shm_surface *
985to_shm_surface(struct toysurface *base)
986{
987 return container_of(base, struct shm_surface, base);
988}
989
Pekka Paalanena4eda732012-11-19 17:16:02 +0200990static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300991shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
992{
993#ifdef DEBUG
994 struct shm_surface_leaf *leaf;
995 char bufs[MAX_LEAVES + 1];
996 int i;
997
998 for (i = 0; i < MAX_LEAVES; i++) {
999 leaf = &surface->leaf[i];
1000
1001 if (leaf->busy)
1002 bufs[i] = 'b';
1003 else if (leaf->cairo_surface)
1004 bufs[i] = 'a';
1005 else
1006 bufs[i] = ' ';
1007 }
1008
1009 bufs[MAX_LEAVES] = '\0';
1010 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1011#endif
1012}
1013
1014static void
Pekka Paalanena4eda732012-11-19 17:16:02 +02001015shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1016{
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001017 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +03001018 struct shm_surface_leaf *leaf;
1019 int i;
1020 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +03001021
1022 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001023
Pekka Paalanenaef02542013-04-25 13:57:47 +03001024 for (i = 0; i < MAX_LEAVES; i++) {
1025 leaf = &surface->leaf[i];
1026 if (leaf->data && leaf->data->buffer == buffer) {
1027 leaf->busy = 0;
1028 break;
1029 }
1030 }
1031 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001032
Pekka Paalanenaef02542013-04-25 13:57:47 +03001033 /* Leave one free leaf with storage, release others */
1034 free_found = 0;
1035 for (i = 0; i < MAX_LEAVES; i++) {
1036 leaf = &surface->leaf[i];
1037
1038 if (!leaf->cairo_surface || leaf->busy)
1039 continue;
1040
1041 if (!free_found)
1042 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001043 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001044 shm_surface_leaf_release(leaf);
1045 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001046
Pekka Paalanen97777442013-05-22 10:20:05 +03001047 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001048}
1049
1050static const struct wl_buffer_listener shm_surface_buffer_listener = {
1051 shm_surface_buffer_release
1052};
1053
Pekka Paalanen99436862012-11-19 17:15:59 +02001054static cairo_surface_t *
1055shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001056 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001057 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001058{
Pekka Paalanenec076692012-11-30 13:37:27 +02001059 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001060 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001061 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001062 struct shm_surface_leaf *leaf = NULL;
1063 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001064
1065 surface->dx = dx;
1066 surface->dy = dy;
1067
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001068 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001069 for (i = 0; i < MAX_LEAVES; i++) {
1070 if (surface->leaf[i].busy)
1071 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001072
Pekka Paalanenaef02542013-04-25 13:57:47 +03001073 if (!leaf || surface->leaf[i].cairo_surface)
1074 leaf = &surface->leaf[i];
1075 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001076 DBG_OBJ(surface->surface, "pick leaf %d\n",
1077 (int)(leaf - &surface->leaf[0]));
1078
Pekka Paalanenaef02542013-04-25 13:57:47 +03001079 if (!leaf) {
1080 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001081 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001082 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001083 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001084 }
1085
Pekka Paalanena4eda732012-11-19 17:16:02 +02001086 if (!resize_hint && leaf->resize_pool) {
1087 cairo_surface_destroy(leaf->cairo_surface);
1088 leaf->cairo_surface = NULL;
1089 shm_pool_destroy(leaf->resize_pool);
1090 leaf->resize_pool = NULL;
1091 }
1092
Alexander Larsson1818e312013-05-22 14:41:31 +02001093 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1094
Pekka Paalanena4eda732012-11-19 17:16:02 +02001095 if (leaf->cairo_surface &&
1096 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1097 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001098 goto out;
1099
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 if (leaf->cairo_surface)
1101 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001102
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001103#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001105 /* Create a big pool to allocate from, while continuously
1106 * resizing. Mmapping a new pool in the server
1107 * is relatively expensive, so reusing a pool performs
1108 * better, but may temporarily reserve unneeded memory.
1109 */
1110 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 leaf->resize_pool = shm_pool_create(surface->display,
1112 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001114#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001115
Alexander Larsson1818e312013-05-22 14:41:31 +02001116 rect.width = width;
1117 rect.height = height;
1118
Pekka Paalanena4eda732012-11-19 17:16:02 +02001119 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001120 display_create_shm_surface(surface->display, &rect,
1121 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001122 leaf->resize_pool,
1123 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001124 if (!leaf->cairo_surface)
1125 return NULL;
1126
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001128 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001129
1130out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001131 surface->current = leaf;
1132
1133 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001134}
1135
1136static void
1137shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001138 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001139 struct rectangle *server_allocation)
1140{
1141 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001142 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001143
1144 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001145 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001146 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001147 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001148
Alexander Larsson1818e312013-05-22 14:41:31 +02001149 buffer_to_surface_size (buffer_transform, buffer_scale,
1150 &server_allocation->width,
1151 &server_allocation->height);
1152
Pekka Paalanena4eda732012-11-19 17:16:02 +02001153 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001154 surface->dx, surface->dy);
1155 wl_surface_damage(surface->surface, 0, 0,
1156 server_allocation->width, server_allocation->height);
1157 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001158
Pekka Paalanen71233882013-04-25 13:57:53 +03001159 DBG_OBJ(surface->surface, "leaf %d busy\n",
1160 (int)(leaf - &surface->leaf[0]));
1161
Pekka Paalanena4eda732012-11-19 17:16:02 +02001162 leaf->busy = 1;
1163 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001164}
1165
1166static int
1167shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1168{
1169 return -1;
1170}
1171
1172static void
1173shm_surface_release(struct toysurface *base)
1174{
1175}
1176
1177static void
1178shm_surface_destroy(struct toysurface *base)
1179{
1180 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001181 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001182
Pekka Paalanenaef02542013-04-25 13:57:47 +03001183 for (i = 0; i < MAX_LEAVES; i++)
1184 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001185
1186 free(surface);
1187}
1188
1189static struct toysurface *
1190shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1191 uint32_t flags, struct rectangle *rectangle)
1192{
1193 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001194 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001195
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001196 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001197 surface->base.prepare = shm_surface_prepare;
1198 surface->base.swap = shm_surface_swap;
1199 surface->base.acquire = shm_surface_acquire;
1200 surface->base.release = shm_surface_release;
1201 surface->base.destroy = shm_surface_destroy;
1202
1203 surface->display = display;
1204 surface->surface = wl_surface;
1205 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001206
1207 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001208}
1209
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001210/*
1211 * The following correspondences between file names and cursors was copied
1212 * from: https://bugs.kde.org/attachment.cgi?id=67313
1213 */
1214
1215static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001216 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001217 "sw-resize",
1218 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001219};
1220
1221static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001222 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001223 "se-resize",
1224 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001225};
1226
1227static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001228 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001229 "s-resize",
1230 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001231};
1232
1233static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001234 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235 "closedhand",
1236 "208530c400c041818281048008011002"
1237};
1238
1239static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001240 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241 "default",
1242 "top_left_arrow",
1243 "left-arrow"
1244};
1245
1246static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001247 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001248 "w-resize",
1249 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001250};
1251
1252static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001253 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001254 "e-resize",
1255 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001256};
1257
1258static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001259 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001260 "nw-resize",
1261 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001262};
1263
1264static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001265 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001266 "ne-resize",
1267 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001268};
1269
1270static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001271 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001272 "n-resize",
1273 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001274};
1275
1276static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001277 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001278 "ibeam",
1279 "text"
1280};
1281
1282static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001283 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001284 "pointer",
1285 "pointing_hand",
1286 "e29285e634086352946a0e7090d73106"
1287};
1288
1289static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001290 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001291 "wait",
1292 "0426c94ea35c87780ff01dc239897213"
1293};
1294
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001295static const char *move_draggings[] = {
1296 "dnd-move"
1297};
1298
1299static const char *copy_draggings[] = {
1300 "dnd-copy"
1301};
1302
1303static const char *forbidden_draggings[] = {
1304 "dnd-none",
1305 "dnd-no-drop"
1306};
1307
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001308struct cursor_alternatives {
1309 const char **names;
1310 size_t count;
1311};
1312
1313static const struct cursor_alternatives cursors[] = {
1314 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1315 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1316 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1317 {grabbings, ARRAY_LENGTH(grabbings)},
1318 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1319 {left_sides, ARRAY_LENGTH(left_sides)},
1320 {right_sides, ARRAY_LENGTH(right_sides)},
1321 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1322 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1323 {top_sides, ARRAY_LENGTH(top_sides)},
1324 {xterms, ARRAY_LENGTH(xterms)},
1325 {hand1s, ARRAY_LENGTH(hand1s)},
1326 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001327 {move_draggings, ARRAY_LENGTH(move_draggings)},
1328 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1329 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001330};
1331
1332static void
1333create_cursors(struct display *display)
1334{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001335 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001336 struct weston_config *config;
1337 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001338 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001339 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001340 unsigned int i, j;
1341 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001342
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001343 config_file = weston_config_get_name_from_env();
1344 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001345 s = weston_config_get_section(config, "shell", NULL, NULL);
1346 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1347 weston_config_section_get_int(s, "cursor-size", &size, 32);
1348 weston_config_destroy(config);
1349
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001350 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001351 if (!display->cursor_theme) {
1352 fprintf(stderr, "could not load theme '%s'\n", theme);
1353 return;
1354 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001355 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001356 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001357 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001358
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001359 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001360 cursor = NULL;
1361 for (j = 0; !cursor && j < cursors[i].count; ++j)
1362 cursor = wl_cursor_theme_get_cursor(
1363 display->cursor_theme, cursors[i].names[j]);
1364
1365 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001366 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001367 cursors[i].names[0]);
1368
1369 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001370 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001371}
1372
1373static void
1374destroy_cursors(struct display *display)
1375{
1376 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001377 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001378}
1379
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001380struct wl_cursor_image *
1381display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001382{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001383 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001384
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001385 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001386}
1387
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001388static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001389surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001390{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001391 if (!surface->cairo_surface)
1392 return;
1393
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001394 if (surface->opaque_region) {
1395 wl_surface_set_opaque_region(surface->surface,
1396 surface->opaque_region);
1397 wl_region_destroy(surface->opaque_region);
1398 surface->opaque_region = NULL;
1399 }
1400
1401 if (surface->input_region) {
1402 wl_surface_set_input_region(surface->surface,
1403 surface->input_region);
1404 wl_region_destroy(surface->input_region);
1405 surface->input_region = NULL;
1406 }
1407
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001409 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001410 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001411
Pekka Paalanen89dee002013-02-13 16:17:20 +02001412 cairo_surface_destroy(surface->cairo_surface);
1413 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001414}
1415
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001416int
1417window_has_focus(struct window *window)
1418{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001419 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001420}
1421
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001422static void
1423window_close(struct window *window)
1424{
1425 if (window->close_handler)
1426 window->close_handler(window->user_data);
1427 else
1428 display_exit(window->display);
1429}
1430
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001431struct display *
1432window_get_display(struct window *window)
1433{
1434 return window->display;
1435}
1436
Pekka Paalanen89dee002013-02-13 16:17:20 +02001437static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001438handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1439 int32_t width, int32_t height)
1440{
1441 struct window *window = data;
1442
1443 window_schedule_resize(window, width, height);
1444}
1445
1446static const struct ivi_surface_listener ivi_surface_listener = {
1447 handle_ivi_surface_configure,
1448};
1449
1450static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001451surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001452{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001453 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001454 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001455
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456 if (!surface->toysurface && display->dpy &&
1457 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001458 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001459 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001460 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001461 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001462 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001463 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001464
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001465 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001466 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001467 surface->surface,
1468 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001469
Pekka Paalanen89dee002013-02-13 16:17:20 +02001470 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001471 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001472 allocation.width, allocation.height, flags,
1473 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001474}
1475
1476static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001477window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001478{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001479 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001480 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001481
Pekka Paalanenec076692012-11-30 13:37:27 +02001482 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001483 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001484
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001485 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1486 flags |= SURFACE_HINT_RGB565;
1487
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001488 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001489}
1490
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001491int
1492window_get_buffer_transform(struct window *window)
1493{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001494 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001495}
1496
1497void
1498window_set_buffer_transform(struct window *window,
1499 enum wl_output_transform transform)
1500{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001501 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001502 wl_surface_set_buffer_transform(window->main_surface->surface,
1503 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001504}
1505
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001506void
1507window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001508 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001509{
1510 window->main_surface->buffer_scale = scale;
1511 wl_surface_set_buffer_scale(window->main_surface->surface,
1512 scale);
1513}
1514
1515uint32_t
1516window_get_buffer_scale(struct window *window)
1517{
1518 return window->main_surface->buffer_scale;
1519}
1520
Alexander Larssond68f5232013-05-22 14:41:33 +02001521uint32_t
1522window_get_output_scale(struct window *window)
1523{
1524 struct window_output *window_output;
1525 struct window_output *window_output_tmp;
1526 int scale = 1;
1527
1528 wl_list_for_each_safe(window_output, window_output_tmp,
1529 &window->window_output_list, link) {
1530 if (window_output->output->scale > scale)
1531 scale = window_output->output->scale;
1532 }
1533
1534 return scale;
1535}
1536
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001537static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001538
Pekka Paalanen4e373742013-02-13 16:17:13 +02001539static void
1540surface_destroy(struct surface *surface)
1541{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001542 if (surface->frame_cb)
1543 wl_callback_destroy(surface->frame_cb);
1544
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001545 if (surface->input_region)
1546 wl_region_destroy(surface->input_region);
1547
1548 if (surface->opaque_region)
1549 wl_region_destroy(surface->opaque_region);
1550
Pekka Paalanen35e82632013-04-25 13:57:48 +03001551 if (surface->subsurface)
1552 wl_subsurface_destroy(surface->subsurface);
1553
Pekka Paalanen4e373742013-02-13 16:17:13 +02001554 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001555
1556 if (surface->toysurface)
1557 surface->toysurface->destroy(surface->toysurface);
1558
Pekka Paalanen35e82632013-04-25 13:57:48 +03001559 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001560 free(surface);
1561}
1562
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001563void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001564window_destroy(struct window *window)
1565{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001566 struct display *display = window->display;
1567 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001568 struct window_output *window_output;
1569 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001570
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001571 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001572
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001573 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001574 if (input->touch_focus == window)
1575 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001576 if (input->pointer_focus == window)
1577 input->pointer_focus = NULL;
1578 if (input->keyboard_focus == window)
1579 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001580 if (input->focus_widget &&
1581 input->focus_widget->window == window)
1582 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001583 }
1584
Rob Bradford7507b572012-05-15 17:55:34 +01001585 wl_list_for_each_safe(window_output, window_output_tmp,
1586 &window->window_output_list, link) {
1587 free (window_output);
1588 }
1589
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001590 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001591 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001592
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001593 if (window->xdg_toplevel)
1594 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001595 if (window->xdg_popup)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08001596 zxdg_popup_v6_destroy(window->xdg_popup);
1597 if (window->xdg_surface)
1598 zxdg_surface_v6_destroy(window->xdg_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001599
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001600 if (window->ivi_surface)
1601 ivi_surface_destroy(window->ivi_surface);
1602
Pekka Paalanen4e373742013-02-13 16:17:13 +02001603 surface_destroy(window->main_surface);
1604
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001605 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001606
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001607 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001608 free(window);
1609}
1610
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001611static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001613{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001614 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001615
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001616 wl_list_for_each(child, &widget->child_list, link) {
1617 target = widget_find_widget(child, x, y);
1618 if (target)
1619 return target;
1620 }
1621
1622 if (widget->allocation.x <= x &&
1623 x < widget->allocation.x + widget->allocation.width &&
1624 widget->allocation.y <= y &&
1625 y < widget->allocation.y + widget->allocation.height) {
1626 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001627 }
1628
1629 return NULL;
1630}
1631
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001632static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001633window_find_widget(struct window *window, int32_t x, int32_t y)
1634{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001635 struct surface *surface;
1636 struct widget *widget;
1637
1638 wl_list_for_each(surface, &window->subsurface_list, link) {
1639 widget = widget_find_widget(surface->widget, x, y);
1640 if (widget)
1641 return widget;
1642 }
1643
1644 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001645}
1646
1647static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001648widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001649{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001650 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001651
Peter Huttererf3d62272013-08-08 11:57:05 +10001652 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001653 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001654 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001655 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001656 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001657 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001658 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001659 widget->tooltip = NULL;
1660 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001661 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001662 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001663
1664 return widget;
1665}
1666
1667struct widget *
1668window_add_widget(struct window *window, void *data)
1669{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001670 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001671
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001672 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001673 wl_list_init(&widget->link);
1674 window->main_surface->widget = widget;
1675
1676 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001677}
1678
1679struct widget *
1680widget_add_widget(struct widget *parent, void *data)
1681{
1682 struct widget *widget;
1683
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001684 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001685 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001687 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001688}
1689
1690void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001691widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001692{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001693 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001694 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001695 struct input *input;
1696
Pekka Paalanen35e82632013-04-25 13:57:48 +03001697 /* Destroy the sub-surface along with the root widget */
1698 if (surface->widget == widget && surface->subsurface)
1699 surface_destroy(widget->surface);
1700
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001701 if (widget->tooltip)
1702 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001703
Pekka Paalanene156fb62012-01-19 13:51:38 +02001704 wl_list_for_each(input, &display->input_list, link) {
1705 if (input->focus_widget == widget)
1706 input->focus_widget = NULL;
1707 }
1708
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001709 wl_list_remove(&widget->link);
1710 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001711}
1712
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001713void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001714widget_set_default_cursor(struct widget *widget, int cursor)
1715{
1716 widget->default_cursor = cursor;
1717}
1718
1719void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001720widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001721{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001722 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001723}
1724
1725void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001726widget_set_size(struct widget *widget, int32_t width, int32_t height)
1727{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001728 widget->allocation.width = width;
1729 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001730}
1731
1732void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001733widget_set_allocation(struct widget *widget,
1734 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001735{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001736 widget->allocation.x = x;
1737 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001738 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001739}
1740
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001741void
1742widget_set_transparent(struct widget *widget, int transparent)
1743{
1744 widget->opaque = !transparent;
1745}
1746
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001747void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001748widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001749{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001750 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001751}
1752
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001753static cairo_surface_t *
1754widget_get_cairo_surface(struct widget *widget)
1755{
1756 struct surface *surface = widget->surface;
1757 struct window *window = widget->window;
1758
Neil Roberts97b747c2013-12-19 16:17:12 +00001759 assert(widget->use_cairo);
1760
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001761 if (!surface->cairo_surface) {
1762 if (surface == window->main_surface)
1763 window_create_main_surface(window);
1764 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001765 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001766 }
1767
1768 return surface->cairo_surface;
1769}
1770
Alexander Larsson15901f02013-05-22 14:41:25 +02001771static void
1772widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1773{
1774 struct surface *surface = widget->surface;
1775 double angle;
1776 cairo_matrix_t m;
1777 enum wl_output_transform transform;
1778 int surface_width, surface_height;
1779 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001780 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001781
1782 surface_width = surface->allocation.width;
1783 surface_height = surface->allocation.height;
1784
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001785 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001786 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001787
Alexander Larsson15901f02013-05-22 14:41:25 +02001788 switch (transform) {
1789 case WL_OUTPUT_TRANSFORM_FLIPPED:
1790 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1791 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1792 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1793 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1794 break;
1795 default:
1796 cairo_matrix_init_identity(&m);
1797 break;
1798 }
1799
1800 switch (transform) {
1801 case WL_OUTPUT_TRANSFORM_NORMAL:
1802 default:
1803 angle = 0;
1804 translate_x = 0;
1805 translate_y = 0;
1806 break;
1807 case WL_OUTPUT_TRANSFORM_FLIPPED:
1808 angle = 0;
1809 translate_x = surface_width;
1810 translate_y = 0;
1811 break;
1812 case WL_OUTPUT_TRANSFORM_90:
1813 angle = M_PI_2;
1814 translate_x = surface_height;
1815 translate_y = 0;
1816 break;
1817 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1818 angle = M_PI_2;
1819 translate_x = surface_height;
1820 translate_y = surface_width;
1821 break;
1822 case WL_OUTPUT_TRANSFORM_180:
1823 angle = M_PI;
1824 translate_x = surface_width;
1825 translate_y = surface_height;
1826 break;
1827 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1828 angle = M_PI;
1829 translate_x = 0;
1830 translate_y = surface_height;
1831 break;
1832 case WL_OUTPUT_TRANSFORM_270:
1833 angle = M_PI + M_PI_2;
1834 translate_x = 0;
1835 translate_y = surface_width;
1836 break;
1837 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1838 angle = M_PI + M_PI_2;
1839 translate_x = 0;
1840 translate_y = 0;
1841 break;
1842 }
1843
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001844 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001845 cairo_translate(cr, translate_x, translate_y);
1846 cairo_rotate(cr, angle);
1847 cairo_transform(cr, &m);
1848}
1849
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001850cairo_t *
1851widget_cairo_create(struct widget *widget)
1852{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001853 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001854 cairo_surface_t *cairo_surface;
1855 cairo_t *cr;
1856
1857 cairo_surface = widget_get_cairo_surface(widget);
1858 cr = cairo_create(cairo_surface);
1859
Alexander Larsson15901f02013-05-22 14:41:25 +02001860 widget_cairo_update_transform(widget, cr);
1861
Pekka Paalanen35e82632013-04-25 13:57:48 +03001862 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1863
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001864 return cr;
1865}
1866
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001867struct wl_surface *
1868widget_get_wl_surface(struct widget *widget)
1869{
1870 return widget->surface->surface;
1871}
1872
Neil Roberts5e10a042013-09-09 00:40:17 +01001873struct wl_subsurface *
1874widget_get_wl_subsurface(struct widget *widget)
1875{
1876 return widget->surface->subsurface;
1877}
1878
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001879uint32_t
1880widget_get_last_time(struct widget *widget)
1881{
1882 return widget->surface->last_time;
1883}
1884
1885void
1886widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1887{
1888 struct wl_compositor *comp = widget->window->display->compositor;
1889 struct surface *surface = widget->surface;
1890
1891 if (!surface->input_region)
1892 surface->input_region = wl_compositor_create_region(comp);
1893
1894 if (rect) {
1895 wl_region_add(surface->input_region,
1896 rect->x, rect->y, rect->width, rect->height);
1897 }
1898}
1899
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001900void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001901widget_set_resize_handler(struct widget *widget,
1902 widget_resize_handler_t handler)
1903{
1904 widget->resize_handler = handler;
1905}
1906
1907void
1908widget_set_redraw_handler(struct widget *widget,
1909 widget_redraw_handler_t handler)
1910{
1911 widget->redraw_handler = handler;
1912}
1913
1914void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001915widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001916{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001917 widget->enter_handler = handler;
1918}
1919
1920void
1921widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1922{
1923 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001924}
1925
1926void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001927widget_set_motion_handler(struct widget *widget,
1928 widget_motion_handler_t handler)
1929{
1930 widget->motion_handler = handler;
1931}
1932
1933void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001934widget_set_button_handler(struct widget *widget,
1935 widget_button_handler_t handler)
1936{
1937 widget->button_handler = handler;
1938}
1939
1940void
Rusty Lynch041815a2013-08-08 21:20:38 -07001941widget_set_touch_up_handler(struct widget *widget,
1942 widget_touch_up_handler_t handler)
1943{
1944 widget->touch_up_handler = handler;
1945}
1946
1947void
1948widget_set_touch_down_handler(struct widget *widget,
1949 widget_touch_down_handler_t handler)
1950{
1951 widget->touch_down_handler = handler;
1952}
1953
1954void
1955widget_set_touch_motion_handler(struct widget *widget,
1956 widget_touch_motion_handler_t handler)
1957{
1958 widget->touch_motion_handler = handler;
1959}
1960
1961void
1962widget_set_touch_frame_handler(struct widget *widget,
1963 widget_touch_frame_handler_t handler)
1964{
1965 widget->touch_frame_handler = handler;
1966}
1967
1968void
1969widget_set_touch_cancel_handler(struct widget *widget,
1970 widget_touch_cancel_handler_t handler)
1971{
1972 widget->touch_cancel_handler = handler;
1973}
1974
1975void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001976widget_set_axis_handler(struct widget *widget,
1977 widget_axis_handler_t handler)
1978{
1979 widget->axis_handler = handler;
1980}
1981
Peter Hutterer87743e92016-01-18 16:38:22 +10001982void
1983widget_set_pointer_frame_handler(struct widget *widget,
1984 widget_pointer_frame_handler_t handler)
1985{
1986 widget->pointer_frame_handler = handler;
1987}
1988
1989void
1990widget_set_axis_handlers(struct widget *widget,
1991 widget_axis_handler_t axis_handler,
1992 widget_axis_source_handler_t axis_source_handler,
1993 widget_axis_stop_handler_t axis_stop_handler,
1994 widget_axis_discrete_handler_t axis_discrete_handler)
1995{
1996 widget->axis_handler = axis_handler;
1997 widget->axis_source_handler = axis_source_handler;
1998 widget->axis_stop_handler = axis_stop_handler;
1999 widget->axis_discrete_handler = axis_discrete_handler;
2000}
2001
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002002static void
2003window_schedule_redraw_task(struct window *window);
2004
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002005void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002006widget_schedule_redraw(struct widget *widget)
2007{
Pekka Paalanen71233882013-04-25 13:57:53 +03002008 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03002009 widget->surface->redraw_needed = 1;
2010 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05002011}
2012
Neil Roberts97b747c2013-12-19 16:17:12 +00002013void
2014widget_set_use_cairo(struct widget *widget,
2015 int use_cairo)
2016{
2017 widget->use_cairo = use_cairo;
2018}
2019
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002020cairo_surface_t *
2021window_get_surface(struct window *window)
2022{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002023 cairo_surface_t *cairo_surface;
2024
2025 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2026
2027 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002028}
2029
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002030struct wl_surface *
2031window_get_wl_surface(struct window *window)
2032{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002033 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002034}
2035
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002036static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002037tooltip_redraw_handler(struct widget *widget, void *data)
2038{
2039 cairo_t *cr;
2040 const int32_t r = 3;
2041 struct tooltip *tooltip = data;
2042 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002043
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002044 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002045 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002046 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2047 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2048 cairo_paint(cr);
2049
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002050 width = widget->allocation.width;
2051 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002052 rounded_rect(cr, 0, 0, width, height, r);
2053
2054 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2055 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2056 cairo_fill(cr);
2057
2058 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2059 cairo_move_to(cr, 10, 16);
2060 cairo_show_text(cr, tooltip->entry);
2061 cairo_destroy(cr);
2062}
2063
2064static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002065get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002066{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002067 cairo_t *cr;
2068 cairo_text_extents_t extents;
2069
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002070 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002071 * created yet, and parent does not have a valid surface
2072 * outside repaint, either.
2073 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002074 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002075 cairo_text_extents(cr, tooltip->entry, &extents);
2076 cairo_destroy(cr);
2077
2078 return extents;
2079}
2080
2081static int
2082window_create_tooltip(struct tooltip *tooltip)
2083{
2084 struct widget *parent = tooltip->parent;
2085 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002086 const int offset_y = 27;
2087 const int margin = 3;
2088 cairo_text_extents_t extents;
2089
2090 if (tooltip->widget)
2091 return 0;
2092
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002093 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002094
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002095 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002096 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002097 widget_set_allocation(tooltip->widget,
2098 tooltip->x, tooltip->y + offset_y,
2099 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002100
2101 return 0;
2102}
2103
2104void
2105widget_destroy_tooltip(struct widget *parent)
2106{
2107 struct tooltip *tooltip = parent->tooltip;
2108
2109 parent->tooltip_count = 0;
2110 if (!tooltip)
2111 return;
2112
2113 if (tooltip->widget) {
2114 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002115 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002116 }
2117
2118 close(tooltip->tooltip_fd);
2119 free(tooltip->entry);
2120 free(tooltip);
2121 parent->tooltip = NULL;
2122}
2123
2124static void
2125tooltip_func(struct task *task, uint32_t events)
2126{
2127 struct tooltip *tooltip =
2128 container_of(task, struct tooltip, tooltip_task);
2129 uint64_t exp;
2130
Martin Olsson8df662a2012-07-08 03:03:47 +02002131 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2132 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002133 window_create_tooltip(tooltip);
2134}
2135
2136#define TOOLTIP_TIMEOUT 500
2137static int
2138tooltip_timer_reset(struct tooltip *tooltip)
2139{
2140 struct itimerspec its;
2141
2142 its.it_interval.tv_sec = 0;
2143 its.it_interval.tv_nsec = 0;
2144 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2145 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2146 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2147 fprintf(stderr, "could not set timerfd\n: %m");
2148 return -1;
2149 }
2150
2151 return 0;
2152}
2153
2154int
2155widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2156{
2157 struct tooltip *tooltip = parent->tooltip;
2158
2159 parent->tooltip_count++;
2160 if (tooltip) {
2161 tooltip->x = x;
2162 tooltip->y = y;
2163 tooltip_timer_reset(tooltip);
2164 return 0;
2165 }
2166
2167 /* the handler might be triggered too fast via input device motion, so
2168 * we need this check here to make sure tooltip is fully initialized */
2169 if (parent->tooltip_count > 1)
2170 return 0;
2171
2172 tooltip = malloc(sizeof *tooltip);
2173 if (!tooltip)
2174 return -1;
2175
2176 parent->tooltip = tooltip;
2177 tooltip->parent = parent;
2178 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002179 tooltip->x = x;
2180 tooltip->y = y;
2181 tooltip->entry = strdup(entry);
2182 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2183 if (tooltip->tooltip_fd < 0) {
2184 fprintf(stderr, "could not create timerfd\n: %m");
2185 return -1;
2186 }
2187
2188 tooltip->tooltip_task.run = tooltip_func;
2189 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2190 EPOLLIN, &tooltip->tooltip_task);
2191 tooltip_timer_reset(tooltip);
2192
2193 return 0;
2194}
2195
2196static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197frame_resize_handler(struct widget *widget,
2198 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002199{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002200 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002201 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002202 struct rectangle interior;
2203 struct rectangle input;
2204 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002205
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002206 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002207 interior.x = 0;
2208 interior.y = 0;
2209 interior.width = width;
2210 interior.height = height;
2211 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002212 frame_resize(frame->frame, width, height);
2213 frame_interior(frame->frame, &interior.x, &interior.y,
2214 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002215 }
2216
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002217 widget_set_allocation(child, interior.x, interior.y,
2218 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002219
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002220 if (child->resize_handler) {
2221 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002222 child->user_data);
2223
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002224 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002225 width = child->allocation.width;
2226 height = child->allocation.height;
2227 } else {
2228 frame_resize_inside(frame->frame,
2229 child->allocation.width,
2230 child->allocation.height);
2231 width = frame_width(frame->frame);
2232 height = frame_height(frame->frame);
2233 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002234 }
2235
Scott Moreauf7e498c2012-05-14 11:39:29 -06002236 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002237
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002238 widget->surface->input_region =
2239 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002240 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002241 frame_input_rect(frame->frame, &input.x, &input.y,
2242 &input.width, &input.height);
2243 wl_region_add(widget->surface->input_region,
2244 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002245 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002246 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002247 }
2248
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002249 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002250
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002251 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002252 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002253 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2254 &opaque.width, &opaque.height);
2255
2256 wl_region_add(widget->surface->opaque_region,
2257 opaque.x, opaque.y,
2258 opaque.width, opaque.height);
2259 } else {
2260 wl_region_add(widget->surface->opaque_region,
2261 0, 0, width, height);
2262 }
Martin Minarik1998b152012-05-10 02:04:35 +02002263 }
2264
Martin Minarik1998b152012-05-10 02:04:35 +02002265
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002266 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002267}
2268
2269static void
2270frame_redraw_handler(struct widget *widget, void *data)
2271{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002272 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002273 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002274 struct window *window = widget->window;
2275
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002276 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002277 return;
2278
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002279 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002280
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002281 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002282
2283 cairo_destroy(cr);
2284}
2285
2286static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002287frame_get_pointer_image_for_location(struct window_frame *frame,
2288 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002289{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002290 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002291
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002292 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002293 return CURSOR_LEFT_PTR;
2294
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002295 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002296 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002297 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002298 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002299 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002300 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002301 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002302 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002303 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002304 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002305 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002306 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002307 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002308 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002309 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002310 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002311 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002312 case THEME_LOCATION_EXTERIOR:
2313 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002314 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002315 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002316 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002317}
2318
Pekka Paalanen26237862014-09-10 15:10:30 +03002319static void
2320frame_menu_func(void *data, struct input *input, int index)
2321{
2322 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002323
2324 switch (index) {
2325 case 0: /* close */
2326 window_close(window);
2327 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002328 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002329 /* we don't have a way to get out of fullscreen for now */
2330 if (window->fullscreen_handler)
2331 window->fullscreen_handler(window, window->user_data);
2332 break;
2333 }
2334}
2335
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002336void
2337window_show_frame_menu(struct window *window,
2338 struct input *input, uint32_t time)
2339{
2340 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002341 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002342
Pekka Paalanen26237862014-09-10 15:10:30 +03002343 static const char *entries[] = {
2344 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002345 "Fullscreen"
2346 };
2347
2348 if (window->fullscreen_handler)
2349 count = ARRAY_LENGTH(entries);
2350 else
2351 count = ARRAY_LENGTH(entries) - 1;
2352
2353 input_get_position(input, &x, &y);
2354 window_show_menu(window->display, input, time, window,
2355 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002356}
2357
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002358static int
2359frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002360 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002361{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002362 struct window_frame *frame = data;
2363 enum theme_location location;
2364
2365 location = frame_pointer_enter(frame->frame, input, x, y);
2366 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2367 widget_schedule_redraw(frame->widget);
2368
2369 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002370}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002371
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002372static int
2373frame_motion_handler(struct widget *widget,
2374 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002375 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002376{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002377 struct window_frame *frame = data;
2378 enum theme_location location;
2379
2380 location = frame_pointer_motion(frame->frame, input, x, y);
2381 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2382 widget_schedule_redraw(frame->widget);
2383
2384 return frame_get_pointer_image_for_location(data, location);
2385}
2386
2387static void
2388frame_leave_handler(struct widget *widget,
2389 struct input *input, void *data)
2390{
2391 struct window_frame *frame = data;
2392
2393 frame_pointer_leave(frame->frame, input);
2394 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2395 widget_schedule_redraw(frame->widget);
2396}
2397
2398static void
2399frame_handle_status(struct window_frame *frame, struct input *input,
2400 uint32_t time, enum theme_location location)
2401{
2402 struct window *window = frame->widget->window;
2403 uint32_t status;
2404
2405 status = frame_status(frame->frame);
2406 if (status & FRAME_STATUS_REPAINT)
2407 widget_schedule_redraw(frame->widget);
2408
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002409 if (status & FRAME_STATUS_MINIMIZE) {
2410 window_set_minimized(window);
2411 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2412 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002413
2414 if (status & FRAME_STATUS_MENU) {
2415 window_show_frame_menu(window, input, time);
2416 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2417 }
2418
2419 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002420 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002421 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2422 }
2423
2424 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002425 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002426 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002427 }
2428
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002429 if ((status & FRAME_STATUS_MOVE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002430 input_ungrab(input);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002431 zxdg_toplevel_v6_move(window->xdg_toplevel,
2432 input_get_seat(input),
2433 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002434
2435 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2436 }
2437
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002438 if ((status & FRAME_STATUS_RESIZE) && window->xdg_toplevel) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002439 input_ungrab(input);
2440
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08002441 zxdg_toplevel_v6_resize(window->xdg_toplevel,
2442 input_get_seat(input),
2443 window->display->serial,
2444 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002445
2446 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2447 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002448}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002449
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002450#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002451static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002452frame_button_handler(struct widget *widget,
2453 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002454 uint32_t button, enum wl_pointer_button_state state,
2455 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002456
2457{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002458 struct window_frame *frame = data;
2459 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002460
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002461 frame->double_click = 0;
2462 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2463 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2464 frame->double_click = 1;
2465 frame->did_double = 1;
2466 } else
2467 frame->did_double = 0;
2468
2469 frame->last_time = time;
2470 } else if (frame->did_double == 1) {
2471 frame->double_click = 1;
2472 frame->did_double = 0;
2473 }
2474
2475 if (frame->double_click)
2476 location = frame_double_click(frame->frame, input,
2477 button, state);
2478 else
2479 location = frame_pointer_button(frame->frame, input,
2480 button, state);
2481
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002482 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002483}
2484
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002485static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002486frame_touch_down_handler(struct widget *widget, struct input *input,
2487 uint32_t serial, uint32_t time, int32_t id,
2488 float x, float y, void *data)
2489{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002490 struct window_frame *frame = data;
2491
Xiong Zhang382de462014-06-12 11:06:26 +08002492 frame->double_click = 0;
2493 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2494 frame->last_id == id) {
2495 frame->double_click = 1;
2496 frame->did_double = 1;
2497 frame->double_id = id;
2498 } else
2499 frame->did_double = 0;
2500
2501 frame->last_time = time;
2502 frame->last_id = id;
2503
2504 if (frame->double_click)
2505 frame_double_touch_down(frame->frame, input, id, x, y);
2506 else
2507 frame_touch_down(frame->frame, input, id, x, y);
2508
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002509 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2510}
2511
2512static void
2513frame_touch_up_handler(struct widget *widget,
2514 struct input *input, uint32_t serial, uint32_t time,
2515 int32_t id, void *data)
2516{
2517 struct window_frame *frame = data;
2518
Xiong Zhang382de462014-06-12 11:06:26 +08002519 if (frame->double_id == id && frame->did_double) {
2520 frame->did_double = 0;
2521 frame->double_id = 0;
2522 frame_double_touch_up(frame->frame, input, id);
2523 } else
2524 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002525 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002526}
2527
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002528struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002529window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002530{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002531 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002532 uint32_t buttons;
2533
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002534 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002535 buttons = FRAME_BUTTON_NONE;
2536 } else {
2537 buttons = FRAME_BUTTON_ALL;
2538 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002539
Peter Huttererf3d62272013-08-08 11:57:05 +10002540 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002541 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002542 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002543
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002544 frame->widget = window_add_widget(window, frame);
2545 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002546
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002547 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2548 widget_set_resize_handler(frame->widget, frame_resize_handler);
2549 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002550 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002551 widget_set_motion_handler(frame->widget, frame_motion_handler);
2552 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002553 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002554 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002555
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002556 window->frame = frame;
2557
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002558 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002559}
2560
Kristian Høgsberga1627922012-06-20 17:30:03 -04002561void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002562window_frame_set_child_size(struct widget *widget, int child_width,
2563 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002564{
2565 struct display *display = widget->window->display;
2566 struct theme *t = display->theme;
2567 int decoration_width, decoration_height;
2568 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002569 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002570
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002571 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002572 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002573 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002574 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002575
2576 width = child_width + decoration_width;
2577 height = child_height + decoration_height;
2578 } else {
2579 width = child_width;
2580 height = child_height;
2581 }
2582
2583 window_schedule_resize(widget->window, width, height);
2584}
2585
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002586static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002587window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002588{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002589 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002590
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002591 /* frame->child must be destroyed by the application */
2592 widget_destroy(frame->widget);
2593 free(frame);
2594}
2595
2596static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002597input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002598 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002599{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002600 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002601 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002602
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002603 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002604 return;
2605
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002606 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002607 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002608 widget = old;
2609 if (input->grab)
2610 widget = input->grab;
2611 if (widget->leave_handler)
2612 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002613 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002614 }
2615
2616 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002617 widget = focus;
2618 if (input->grab)
2619 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002620 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002621 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002622 cursor = widget->enter_handler(focus, input, x, y,
2623 widget->user_data);
2624 else
2625 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002626
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002627 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002628 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002629}
2630
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002631void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002632touch_grab(struct input *input, int32_t touch_id)
2633{
2634 input->touch_grab = 1;
2635 input->touch_grab_id = touch_id;
2636}
2637
2638void
2639touch_ungrab(struct input *input)
2640{
2641 struct touch_point *tp, *tmp;
2642
2643 input->touch_grab = 0;
2644
2645 wl_list_for_each_safe(tp, tmp,
2646 &input->touch_point_list, link) {
2647 if (tp->id != input->touch_grab_id)
2648 continue;
2649 wl_list_remove(&tp->link);
2650 free(tp);
2651
2652 return;
2653 }
2654}
2655
2656void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002657input_grab(struct input *input, struct widget *widget, uint32_t button)
2658{
2659 input->grab = widget;
2660 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002661
2662 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002663}
2664
2665void
2666input_ungrab(struct input *input)
2667{
2668 struct widget *widget;
2669
2670 input->grab = NULL;
2671 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002672 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002673 input->sx, input->sy);
2674 input_set_focus_widget(input, widget, input->sx, input->sy);
2675 }
2676}
2677
2678static void
Derek Foreman118a4292015-04-22 17:23:35 -05002679cursor_delay_timer_reset(struct input *input, uint32_t duration)
2680{
2681 struct itimerspec its;
2682
2683 if (!duration)
2684 input->cursor_timer_running = false;
2685 else
2686 input->cursor_timer_running = true;
2687
2688 its.it_interval.tv_sec = 0;
2689 its.it_interval.tv_nsec = 0;
2690 its.it_value.tv_sec = duration / 1000;
2691 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2692 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2693 fprintf(stderr, "could not set cursor timerfd\n: %m");
2694}
2695
2696static void cancel_pointer_image_update(struct input *input)
2697{
2698 if (input->cursor_timer_running)
2699 cursor_delay_timer_reset(input, 0);
2700}
2701
2702static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002703input_remove_pointer_focus(struct input *input)
2704{
2705 struct window *window = input->pointer_focus;
2706
2707 if (!window)
2708 return;
2709
2710 input_set_focus_widget(input, NULL, 0, 0);
2711
2712 input->pointer_focus = NULL;
2713 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002714 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002715}
2716
2717static void
2718pointer_handle_enter(void *data, struct wl_pointer *pointer,
2719 uint32_t serial, struct wl_surface *surface,
2720 wl_fixed_t sx_w, wl_fixed_t sy_w)
2721{
2722 struct input *input = data;
2723 struct window *window;
2724 struct widget *widget;
2725 float sx = wl_fixed_to_double(sx_w);
2726 float sy = wl_fixed_to_double(sy_w);
2727
2728 if (!surface) {
2729 /* enter event for a window we've just destroyed */
2730 return;
2731 }
2732
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002733 window = wl_surface_get_user_data(surface);
2734 if (surface != window->main_surface->surface) {
2735 DBG("Ignoring input event from subsurface %p\n", surface);
2736 return;
2737 }
2738
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002739 input->display->serial = serial;
2740 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002741 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002742
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002743 input->sx = sx;
2744 input->sy = sy;
2745
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002746 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002747 input_set_focus_widget(input, widget, sx, sy);
2748}
2749
2750static void
2751pointer_handle_leave(void *data, struct wl_pointer *pointer,
2752 uint32_t serial, struct wl_surface *surface)
2753{
2754 struct input *input = data;
2755
2756 input->display->serial = serial;
2757 input_remove_pointer_focus(input);
2758}
2759
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002760static void
Daniel Stone37816df2012-05-16 18:45:18 +01002761pointer_handle_motion(void *data, struct wl_pointer *pointer,
2762 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002763{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002764 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002765 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002766 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002767 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002768 float sx = wl_fixed_to_double(sx_w);
2769 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002770
Paul Winwoodb22bf572013-08-29 10:52:54 +01002771 if (!window)
2772 return;
2773
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002774 input->sx = sx;
2775 input->sy = sy;
2776
Rob Bradford5f087742013-07-11 19:41:27 +01002777 /* when making the window smaller - e.g. after a unmaximise we might
2778 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002779 * based on the old surface dimensions. However, if we have an active
2780 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002781 */
Derek Foreman46812b62015-08-26 17:13:27 -05002782 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002783 sy < window->main_surface->allocation.y ||
2784 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002785 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002786 return;
2787
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002788 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002789 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002790 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002791 }
2792
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002793 if (input->grab)
2794 widget = input->grab;
2795 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002796 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002797 if (widget) {
2798 if (widget->motion_handler)
2799 cursor = widget->motion_handler(input->focus_widget,
2800 input, time, sx, sy,
2801 widget->user_data);
2802 else
2803 cursor = widget->default_cursor;
2804 } else
2805 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002806
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002807 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002808}
2809
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002810static void
Daniel Stone37816df2012-05-16 18:45:18 +01002811pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002812 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002813{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002814 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002815 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002816 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002817
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002818 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002819 if (input->focus_widget && input->grab == NULL &&
2820 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002821 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002822
Neil Roberts6b28aad2012-01-23 19:11:18 +00002823 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002824 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002825 (*widget->button_handler)(widget,
2826 input, time,
2827 button, state,
2828 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002829
Daniel Stone4dbadb12012-05-30 16:31:51 +01002830 if (input->grab && input->grab_button == button &&
2831 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002832 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002833}
2834
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002835static void
Daniel Stone37816df2012-05-16 18:45:18 +01002836pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002837 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002838{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002839 struct input *input = data;
2840 struct widget *widget;
2841
2842 widget = input->focus_widget;
2843 if (input->grab)
2844 widget = input->grab;
2845 if (widget && widget->axis_handler)
2846 (*widget->axis_handler)(widget,
2847 input, time,
2848 axis, value,
2849 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002850}
2851
Peter Hutterer87743e92016-01-18 16:38:22 +10002852static void
2853pointer_handle_frame(void *data, struct wl_pointer *pointer)
2854{
2855 struct input *input = data;
2856 struct widget *widget;
2857
2858 widget = input->focus_widget;
2859 if (input->grab)
2860 widget = input->grab;
2861 if (widget && widget->pointer_frame_handler)
2862 (*widget->pointer_frame_handler)(widget,
2863 input,
2864 widget->user_data);
2865}
2866
2867static void
2868pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2869 uint32_t source)
2870{
2871 struct input *input = data;
2872 struct widget *widget;
2873
2874 widget = input->focus_widget;
2875 if (input->grab)
2876 widget = input->grab;
2877 if (widget && widget->axis_source_handler)
2878 (*widget->axis_source_handler)(widget,
2879 input,
2880 source,
2881 widget->user_data);
2882}
2883
2884static void
2885pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2886 uint32_t time, uint32_t axis)
2887{
2888 struct input *input = data;
2889 struct widget *widget;
2890
2891 widget = input->focus_widget;
2892 if (input->grab)
2893 widget = input->grab;
2894 if (widget && widget->axis_stop_handler)
2895 (*widget->axis_stop_handler)(widget,
2896 input, time,
2897 axis,
2898 widget->user_data);
2899}
2900
2901static void
2902pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2903 uint32_t axis, int32_t discrete)
2904{
2905 struct input *input = data;
2906 struct widget *widget;
2907
2908 widget = input->focus_widget;
2909 if (input->grab)
2910 widget = input->grab;
2911 if (widget && widget->axis_discrete_handler)
2912 (*widget->axis_discrete_handler)(widget,
2913 input,
2914 axis,
2915 discrete,
2916 widget->user_data);
2917}
2918
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002919static const struct wl_pointer_listener pointer_listener = {
2920 pointer_handle_enter,
2921 pointer_handle_leave,
2922 pointer_handle_motion,
2923 pointer_handle_button,
2924 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002925 pointer_handle_frame,
2926 pointer_handle_axis_source,
2927 pointer_handle_axis_stop,
2928 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002929};
2930
2931static void
2932input_remove_keyboard_focus(struct input *input)
2933{
2934 struct window *window = input->keyboard_focus;
2935 struct itimerspec its;
2936
2937 its.it_interval.tv_sec = 0;
2938 its.it_interval.tv_nsec = 0;
2939 its.it_value.tv_sec = 0;
2940 its.it_value.tv_nsec = 0;
2941 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2942
2943 if (!window)
2944 return;
2945
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002946 if (window->keyboard_focus_handler)
2947 (*window->keyboard_focus_handler)(window, NULL,
2948 window->user_data);
2949
2950 input->keyboard_focus = NULL;
2951}
2952
2953static void
2954keyboard_repeat_func(struct task *task, uint32_t events)
2955{
2956 struct input *input =
2957 container_of(task, struct input, repeat_task);
2958 struct window *window = input->keyboard_focus;
2959 uint64_t exp;
2960
2961 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2962 /* If we change the timer between the fd becoming
2963 * readable and getting here, there'll be nothing to
2964 * read and we get EAGAIN. */
2965 return;
2966
2967 if (window && window->key_handler) {
2968 (*window->key_handler)(window, input, input->repeat_time,
2969 input->repeat_key, input->repeat_sym,
2970 WL_KEYBOARD_KEY_STATE_PRESSED,
2971 window->user_data);
2972 }
2973}
2974
2975static void
2976keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2977 uint32_t format, int fd, uint32_t size)
2978{
2979 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002980 struct xkb_keymap *keymap;
2981 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002982 char *map_str;
2983
2984 if (!data) {
2985 close(fd);
2986 return;
2987 }
2988
2989 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2990 close(fd);
2991 return;
2992 }
2993
2994 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2995 if (map_str == MAP_FAILED) {
2996 close(fd);
2997 return;
2998 }
2999
Ran Benita2e1968f2014-08-19 23:59:51 +03003000 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
3001 map_str,
3002 XKB_KEYMAP_FORMAT_TEXT_V1,
3003 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003004 munmap(map_str, size);
3005 close(fd);
3006
Rui Matos3eccb862013-10-10 19:44:22 +02003007 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003008 fprintf(stderr, "failed to compile keymap\n");
3009 return;
3010 }
3011
Rui Matos3eccb862013-10-10 19:44:22 +02003012 state = xkb_state_new(keymap);
3013 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003014 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03003015 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003016 return;
3017 }
3018
Rui Matos3eccb862013-10-10 19:44:22 +02003019 xkb_keymap_unref(input->xkb.keymap);
3020 xkb_state_unref(input->xkb.state);
3021 input->xkb.keymap = keymap;
3022 input->xkb.state = state;
3023
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003024 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003025 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003026 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003027 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003028 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003029 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003030}
3031
3032static void
3033keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3034 uint32_t serial, struct wl_surface *surface,
3035 struct wl_array *keys)
3036{
3037 struct input *input = data;
3038 struct window *window;
3039
3040 input->display->serial = serial;
3041 input->keyboard_focus = wl_surface_get_user_data(surface);
3042
3043 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003044 if (window->keyboard_focus_handler)
3045 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003046 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003047}
3048
3049static void
3050keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3051 uint32_t serial, struct wl_surface *surface)
3052{
3053 struct input *input = data;
3054
3055 input->display->serial = serial;
3056 input_remove_keyboard_focus(input);
3057}
3058
Scott Moreau210d0792012-03-22 10:47:01 -06003059static void
Daniel Stone37816df2012-05-16 18:45:18 +01003060keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003061 uint32_t serial, uint32_t time, uint32_t key,
3062 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003063{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003064 struct input *input = data;
3065 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003066 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003067 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003068 const xkb_keysym_t *syms;
3069 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003070 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003071
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003072 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003073 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003074 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003075 return;
3076
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003077 /* We only use input grabs for pointer events for now, so just
3078 * ignore key presses if a grab is active. We expand the key
3079 * event delivery mechanism to route events to widgets to
3080 * properly handle key grabs. In the meantime, this prevents
3081 * key event devlivery while a grab is active. */
3082 if (input->grab && input->grab_button == 0)
3083 return;
3084
Ran Benita2e1968f2014-08-19 23:59:51 +03003085 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003086
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003087 sym = XKB_KEY_NoSymbol;
3088 if (num_syms == 1)
3089 sym = syms[0];
3090
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003091
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003092 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003093 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003094 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003095 } else if (sym == XKB_KEY_F11 &&
3096 window->fullscreen_handler &&
3097 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3098 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003099 } else if (sym == XKB_KEY_F4 &&
3100 input->modifiers == MOD_ALT_MASK &&
3101 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003102 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003103 } else if (window->key_handler) {
3104 (*window->key_handler)(window, input, time, key,
3105 sym, state, window->user_data);
3106 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003107
3108 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3109 key == input->repeat_key) {
3110 its.it_interval.tv_sec = 0;
3111 its.it_interval.tv_nsec = 0;
3112 its.it_value.tv_sec = 0;
3113 its.it_value.tv_nsec = 0;
3114 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003115 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3116 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003117 input->repeat_sym = sym;
3118 input->repeat_key = key;
3119 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003120 its.it_interval.tv_sec = input->repeat_rate_sec;
3121 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3122 its.it_value.tv_sec = input->repeat_delay_sec;
3123 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003124 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3125 }
3126}
3127
3128static void
Daniel Stone351eb612012-05-31 15:27:47 -04003129keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3130 uint32_t serial, uint32_t mods_depressed,
3131 uint32_t mods_latched, uint32_t mods_locked,
3132 uint32_t group)
3133{
3134 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003135 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003136
Matt Ropere61561f2013-06-24 16:52:43 +01003137 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3138 if (!input->xkb.keymap)
3139 return;
3140
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003141 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3142 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003143 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003144 XKB_STATE_MODS_DEPRESSED |
3145 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003146 input->modifiers = 0;
3147 if (mask & input->xkb.control_mask)
3148 input->modifiers |= MOD_CONTROL_MASK;
3149 if (mask & input->xkb.alt_mask)
3150 input->modifiers |= MOD_ALT_MASK;
3151 if (mask & input->xkb.shift_mask)
3152 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003153}
3154
Jonny Lamb06959082014-08-12 14:58:27 +02003155static void
3156set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3157{
3158 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3159 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3160
3161 /* a rate of zero disables any repeating, regardless of the delay's
3162 * value */
3163 if (rate == 0)
3164 return;
3165
3166 if (rate == 1)
3167 input->repeat_rate_sec = 1;
3168 else
3169 input->repeat_rate_nsec = 1000000000 / rate;
3170
3171 input->repeat_delay_sec = delay / 1000;
3172 delay -= (input->repeat_delay_sec * 1000);
3173 input->repeat_delay_nsec = delay * 1000 * 1000;
3174}
3175
3176static void
3177keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3178 int32_t rate, int32_t delay)
3179{
3180 struct input *input = data;
3181
3182 set_repeat_info(input, rate, delay);
3183}
3184
Daniel Stone37816df2012-05-16 18:45:18 +01003185static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003186 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003187 keyboard_handle_enter,
3188 keyboard_handle_leave,
3189 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003190 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003191 keyboard_handle_repeat_info
3192
Daniel Stone37816df2012-05-16 18:45:18 +01003193};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003194
3195static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003196touch_handle_down(void *data, struct wl_touch *wl_touch,
3197 uint32_t serial, uint32_t time, struct wl_surface *surface,
3198 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3199{
3200 struct input *input = data;
3201 struct widget *widget;
3202 float sx = wl_fixed_to_double(x_w);
3203 float sy = wl_fixed_to_double(y_w);
3204
Rusty Lynch1084da52013-08-15 09:10:08 -07003205 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003206 input->touch_focus = wl_surface_get_user_data(surface);
3207 if (!input->touch_focus) {
3208 DBG("Failed to find to touch focus for surface %p\n", surface);
3209 return;
3210 }
3211
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003212 if (surface != input->touch_focus->main_surface->surface) {
3213 DBG("Ignoring input event from subsurface %p\n", surface);
3214 input->touch_focus = NULL;
3215 return;
3216 }
3217
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003218 if (input->grab)
3219 widget = input->grab;
3220 else
3221 widget = window_find_widget(input->touch_focus,
3222 wl_fixed_to_double(x_w),
3223 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003224 if (widget) {
3225 struct touch_point *tp = xmalloc(sizeof *tp);
3226 if (tp) {
3227 tp->id = id;
3228 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003229 tp->x = sx;
3230 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003231 wl_list_insert(&input->touch_point_list, &tp->link);
3232
3233 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003234 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003235 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003236 sx, sy,
3237 widget->user_data);
3238 }
3239 }
3240}
3241
3242static void
3243touch_handle_up(void *data, struct wl_touch *wl_touch,
3244 uint32_t serial, uint32_t time, int32_t id)
3245{
3246 struct input *input = data;
3247 struct touch_point *tp, *tmp;
3248
Rusty Lynch041815a2013-08-08 21:20:38 -07003249 if (!input->touch_focus) {
3250 DBG("No touch focus found for touch up event!\n");
3251 return;
3252 }
3253
3254 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3255 if (tp->id != id)
3256 continue;
3257
3258 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003259 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003260 time, id,
3261 tp->widget->user_data);
3262
3263 wl_list_remove(&tp->link);
3264 free(tp);
3265
3266 return;
3267 }
3268}
3269
3270static void
3271touch_handle_motion(void *data, struct wl_touch *wl_touch,
3272 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3273{
3274 struct input *input = data;
3275 struct touch_point *tp;
3276 float sx = wl_fixed_to_double(x_w);
3277 float sy = wl_fixed_to_double(y_w);
3278
3279 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3280
3281 if (!input->touch_focus) {
3282 DBG("No touch focus found for touch motion event!\n");
3283 return;
3284 }
3285
3286 wl_list_for_each(tp, &input->touch_point_list, link) {
3287 if (tp->id != id)
3288 continue;
3289
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003290 tp->x = sx;
3291 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003292 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003293 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003294 id, sx, sy,
3295 tp->widget->user_data);
3296 return;
3297 }
3298}
3299
3300static void
3301touch_handle_frame(void *data, struct wl_touch *wl_touch)
3302{
3303 struct input *input = data;
3304 struct touch_point *tp, *tmp;
3305
3306 DBG("touch_handle_frame\n");
3307
3308 if (!input->touch_focus) {
3309 DBG("No touch focus found for touch frame event!\n");
3310 return;
3311 }
3312
3313 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3314 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003315 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003316 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003317 }
3318}
3319
3320static void
3321touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3322{
3323 struct input *input = data;
3324 struct touch_point *tp, *tmp;
3325
3326 DBG("touch_handle_cancel\n");
3327
3328 if (!input->touch_focus) {
3329 DBG("No touch focus found for touch cancel event!\n");
3330 return;
3331 }
3332
3333 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3334 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003335 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3336 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003337
3338 wl_list_remove(&tp->link);
3339 free(tp);
3340 }
3341}
3342
3343static const struct wl_touch_listener touch_listener = {
3344 touch_handle_down,
3345 touch_handle_up,
3346 touch_handle_motion,
3347 touch_handle_frame,
3348 touch_handle_cancel,
3349};
3350
3351static void
Daniel Stone37816df2012-05-16 18:45:18 +01003352seat_handle_capabilities(void *data, struct wl_seat *seat,
3353 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003354{
Daniel Stone37816df2012-05-16 18:45:18 +01003355 struct input *input = data;
3356
3357 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3358 input->pointer = wl_seat_get_pointer(seat);
3359 wl_pointer_set_user_data(input->pointer, input);
3360 wl_pointer_add_listener(input->pointer, &pointer_listener,
3361 input);
3362 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003363 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003364 wl_pointer_release(input->pointer);
3365 else
3366 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003367 input->pointer = NULL;
3368 }
3369
3370 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3371 input->keyboard = wl_seat_get_keyboard(seat);
3372 wl_keyboard_set_user_data(input->keyboard, input);
3373 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3374 input);
3375 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003376 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003377 wl_keyboard_release(input->keyboard);
3378 else
3379 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003380 input->keyboard = NULL;
3381 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003382
3383 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3384 input->touch = wl_seat_get_touch(seat);
3385 wl_touch_set_user_data(input->touch, input);
3386 wl_touch_add_listener(input->touch, &touch_listener, input);
3387 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003388 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003389 wl_touch_release(input->touch);
3390 else
3391 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003392 input->touch = NULL;
3393 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003394}
3395
Rob Bradford08031182013-08-13 20:11:03 +01003396static void
3397seat_handle_name(void *data, struct wl_seat *seat,
3398 const char *name)
3399{
3400
3401}
3402
Daniel Stone37816df2012-05-16 18:45:18 +01003403static const struct wl_seat_listener seat_listener = {
3404 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003405 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003406};
3407
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003408void
3409input_get_position(struct input *input, int32_t *x, int32_t *y)
3410{
3411 *x = input->sx;
3412 *y = input->sy;
3413}
3414
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003415int
3416input_get_touch(struct input *input, int32_t id, float *x, float *y)
3417{
3418 struct touch_point *tp;
3419
3420 wl_list_for_each(tp, &input->touch_point_list, link) {
3421 if (tp->id != id)
3422 continue;
3423
3424 *x = tp->x;
3425 *y = tp->y;
3426 return 0;
3427 }
3428
3429 return -1;
3430}
3431
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003432struct display *
3433input_get_display(struct input *input)
3434{
3435 return input->display;
3436}
3437
Daniel Stone37816df2012-05-16 18:45:18 +01003438struct wl_seat *
3439input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003440{
Daniel Stone37816df2012-05-16 18:45:18 +01003441 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003442}
3443
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003444uint32_t
3445input_get_modifiers(struct input *input)
3446{
3447 return input->modifiers;
3448}
3449
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003450struct widget *
3451input_get_focus_widget(struct input *input)
3452{
3453 return input->focus_widget;
3454}
3455
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003456struct data_offer {
3457 struct wl_data_offer *offer;
3458 struct input *input;
3459 struct wl_array types;
3460 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003461
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003462 struct task io_task;
3463 int fd;
3464 data_func_t func;
3465 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003466 uint32_t dnd_action;
3467 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003468 void *user_data;
3469};
3470
3471static void
3472data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3473{
3474 struct data_offer *offer = data;
3475 char **p;
3476
3477 p = wl_array_add(&offer->types, sizeof *p);
3478 *p = strdup(type);
3479}
3480
Carlos Garnacho9c931792016-01-18 23:52:12 +01003481static void
3482data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3483{
3484 struct data_offer *offer = data;
3485
3486 offer->source_actions = source_actions;
3487}
3488
3489static void
3490data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3491{
3492 struct data_offer *offer = data;
3493
3494 offer->dnd_action = dnd_action;
3495}
3496
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003497static const struct wl_data_offer_listener data_offer_listener = {
3498 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003499 data_offer_source_actions,
3500 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003501};
3502
3503static void
3504data_offer_destroy(struct data_offer *offer)
3505{
3506 char **p;
3507
3508 offer->refcount--;
3509 if (offer->refcount == 0) {
3510 wl_data_offer_destroy(offer->offer);
3511 for (p = offer->types.data; *p; p++)
3512 free(*p);
3513 wl_array_release(&offer->types);
3514 free(offer);
3515 }
3516}
3517
3518static void
3519data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003520 struct wl_data_device *data_device,
3521 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003522{
3523 struct data_offer *offer;
3524
Brian Lovinbc919262013-08-07 15:34:59 -07003525 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003526
3527 wl_array_init(&offer->types);
3528 offer->refcount = 1;
3529 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003530 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003531 wl_data_offer_add_listener(offer->offer,
3532 &data_offer_listener, offer);
3533}
3534
3535static void
3536data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003537 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003538 wl_fixed_t x_w, wl_fixed_t y_w,
3539 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003540{
3541 struct input *input = data;
3542 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003543 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003544 float x = wl_fixed_to_double(x_w);
3545 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003546 char **p;
3547
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003548 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003549 input->drag_enter_serial = serial;
3550 input->drag_focus = window,
3551 input->drag_x = x;
3552 input->drag_y = y;
3553
3554 if (!input->touch_grab)
3555 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003556
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003557 if (offer) {
3558 input->drag_offer = wl_data_offer_get_user_data(offer);
3559
3560 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3561 *p = NULL;
3562
3563 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003564
3565 if (input->display->data_device_manager_version >=
3566 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3567 wl_data_offer_set_actions(offer,
3568 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3569 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3570 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3571 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003572 } else {
3573 input->drag_offer = NULL;
3574 types_data = NULL;
3575 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003576
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003577 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003578 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003579 window->user_data);
3580}
3581
3582static void
3583data_device_leave(void *data, struct wl_data_device *data_device)
3584{
3585 struct input *input = data;
3586
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003587 if (input->drag_offer) {
3588 data_offer_destroy(input->drag_offer);
3589 input->drag_offer = NULL;
3590 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003591}
3592
3593static void
3594data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003595 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003596{
3597 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003598 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003599 float x = wl_fixed_to_double(x_w);
3600 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003601 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003602
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003603 input->drag_x = x;
3604 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003605
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003606 if (input->drag_offer)
3607 types_data = input->drag_offer->types.data;
3608 else
3609 types_data = NULL;
3610
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003611 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003612 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003613 window->user_data);
3614}
3615
3616static void
3617data_device_drop(void *data, struct wl_data_device *data_device)
3618{
3619 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003620 struct window *window = input->drag_focus;
3621 float x, y;
3622
3623 x = input->drag_x;
3624 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003625
3626 if (window->drop_handler)
3627 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003628 x, y, window->user_data);
3629
3630 if (input->touch_grab)
3631 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003632}
3633
3634static void
3635data_device_selection(void *data,
3636 struct wl_data_device *wl_data_device,
3637 struct wl_data_offer *offer)
3638{
3639 struct input *input = data;
3640 char **p;
3641
3642 if (input->selection_offer)
3643 data_offer_destroy(input->selection_offer);
3644
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003645 if (offer) {
3646 input->selection_offer = wl_data_offer_get_user_data(offer);
3647 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3648 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003649 } else {
3650 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003651 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003652}
3653
3654static const struct wl_data_device_listener data_device_listener = {
3655 data_device_data_offer,
3656 data_device_enter,
3657 data_device_leave,
3658 data_device_motion,
3659 data_device_drop,
3660 data_device_selection
3661};
3662
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003663static void
3664input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003665{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003666 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003667 struct wl_cursor *cursor;
3668 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003669
Daniel Stone80972742012-11-07 17:51:39 +11003670 if (!input->pointer)
3671 return;
3672
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003673 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003674 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003675 return;
3676
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003677 if (index >= (int) cursor->image_count) {
3678 fprintf(stderr, "cursor index out of range\n");
3679 return;
3680 }
3681
3682 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003683 buffer = wl_cursor_image_get_buffer(image);
3684 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003685 return;
3686
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003687 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3688 wl_surface_damage(input->pointer_surface, 0, 0,
3689 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003690 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003691 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3692 input->pointer_surface,
3693 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003694}
3695
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003696static const struct wl_callback_listener pointer_surface_listener;
3697
Derek Foreman493d9792015-03-04 16:26:25 -06003698static bool
3699input_set_pointer_special(struct input *input)
3700{
3701 if (input->current_cursor == CURSOR_BLANK) {
3702 wl_pointer_set_cursor(input->pointer,
3703 input->pointer_enter_serial,
3704 NULL, 0, 0);
3705 return true;
3706 }
3707
3708 if (input->current_cursor == CURSOR_UNSET)
3709 return true;
3710
3711 return false;
3712}
3713
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003714static void
Derek Foreman118a4292015-04-22 17:23:35 -05003715schedule_pointer_image_update(struct input *input,
3716 struct wl_cursor *cursor,
3717 uint32_t duration,
3718 bool force_frame)
3719{
3720 /* Some silly cursor sets have enormous pauses in them. In these
3721 * cases it's better to use a timer even if it results in less
3722 * accurate presentation, since it will save us having to set the
3723 * same cursor image over and over again.
3724 *
3725 * This is really not the way we're supposed to time any kind of
3726 * animation, but we're pretending it's OK here because we don't
3727 * want animated cursors with long delays to needlessly hog CPU.
3728 *
3729 * We use force_frame to ensure we don't accumulate large timing
3730 * errors by running off the wrong clock.
3731 */
3732 if (!force_frame && duration > 100) {
3733 struct timespec tp;
3734
3735 clock_gettime(CLOCK_MONOTONIC, &tp);
3736 input->cursor_timer_start = tp.tv_sec * 1000
3737 + tp.tv_nsec / 1000000;
3738 cursor_delay_timer_reset(input, duration);
3739 return;
3740 }
3741
3742 /* for short durations we'll just spin on frame callbacks for
3743 * accurate timing - the way any kind of timing sensitive animation
3744 * should really be done. */
3745 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3746 wl_callback_add_listener(input->cursor_frame_cb,
3747 &pointer_surface_listener, input);
3748
3749}
3750
3751static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003752pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3753 uint32_t time)
3754{
3755 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003756 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003757 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003758 uint32_t duration;
3759 bool force_frame = true;
3760
3761 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003762
3763 if (callback) {
3764 assert(callback == input->cursor_frame_cb);
3765 wl_callback_destroy(callback);
3766 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003767 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003768 }
3769
Daniel Stone80972742012-11-07 17:51:39 +11003770 if (!input->pointer)
3771 return;
3772
Derek Foreman493d9792015-03-04 16:26:25 -06003773 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003774 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003775
Daniel Stonea494f1d2012-06-18 19:31:12 +01003776 cursor = input->display->cursors[input->current_cursor];
3777 if (!cursor)
3778 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003779
3780 /* FIXME We don't have the current time on the first call so we set
3781 * the animation start to the time of the first frame callback. */
3782 if (time == 0)
3783 input->cursor_anim_start = 0;
3784 else if (input->cursor_anim_start == 0)
3785 input->cursor_anim_start = time;
3786
Derek Foreman118a4292015-04-22 17:23:35 -05003787 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003788
Derek Foreman118a4292015-04-22 17:23:35 -05003789 if (time == 0 || input->cursor_anim_start == 0) {
3790 duration = 0;
3791 i = 0;
3792 } else
3793 i = wl_cursor_frame_and_duration(
3794 cursor,
3795 time - input->cursor_anim_start,
3796 &duration);
3797
3798 if (cursor->image_count > 1)
3799 schedule_pointer_image_update(input, cursor, duration,
3800 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003801
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003802 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003803}
3804
Derek Foreman118a4292015-04-22 17:23:35 -05003805static void
3806cursor_timer_func(struct task *task, uint32_t events)
3807{
3808 struct input *input = container_of(task, struct input, cursor_task);
3809 struct timespec tp;
3810 struct wl_cursor *cursor;
3811 uint32_t time;
3812 uint64_t exp;
3813
3814 if (!input->cursor_timer_running)
3815 return;
3816
3817 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3818 return;
3819
3820 cursor = input->display->cursors[input->current_cursor];
3821 if (!cursor)
3822 return;
3823
3824 clock_gettime(CLOCK_MONOTONIC, &tp);
3825 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3826 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3827}
3828
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003829static const struct wl_callback_listener pointer_surface_listener = {
3830 pointer_surface_frame_callback
3831};
3832
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003833void
3834input_set_pointer_image(struct input *input, int pointer)
3835{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003836 int force = 0;
3837
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003838 if (!input->pointer)
3839 return;
3840
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003841 if (input->pointer_enter_serial > input->cursor_serial)
3842 force = 1;
3843
3844 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003845 return;
3846
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003847 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003848 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003849 if (!input->cursor_frame_cb)
3850 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003851 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003852 /* The current frame callback may be stuck if, for instance,
3853 * the set cursor request was processed by the server after
3854 * this client lost the focus. In this case the cursor surface
3855 * might not be mapped and the frame callback wouldn't ever
3856 * complete. Send a set_cursor and attach to try to map the
3857 * cursor surface again so that the callback will finish */
3858 input_set_pointer_image_index(input, 0);
3859 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003860}
3861
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003862struct wl_data_device *
3863input_get_data_device(struct input *input)
3864{
3865 return input->data_device;
3866}
3867
3868void
3869input_set_selection(struct input *input,
3870 struct wl_data_source *source, uint32_t time)
3871{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003872 if (input->data_device)
3873 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003874}
3875
3876void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003877input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003878{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003879 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003880 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003881}
3882
3883static void
3884offer_io_func(struct task *task, uint32_t events)
3885{
3886 struct data_offer *offer =
3887 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003888 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003889 unsigned int len;
3890 char buffer[4096];
3891
3892 len = read(offer->fd, buffer, sizeof buffer);
3893 offer->func(buffer, len,
3894 offer->x, offer->y, offer->user_data);
3895
3896 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003897 if (display->data_device_manager_version >=
3898 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3899 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003900 close(offer->fd);
3901 data_offer_destroy(offer);
3902 }
3903}
3904
3905static void
3906data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3907 data_func_t func, void *user_data)
3908{
3909 int p[2];
3910
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003911 if (pipe2(p, O_CLOEXEC) == -1)
3912 return;
3913
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003914 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3915 close(p[1]);
3916
3917 offer->io_task.run = offer_io_func;
3918 offer->fd = p[0];
3919 offer->func = func;
3920 offer->refcount++;
3921 offer->user_data = user_data;
3922
3923 display_watch_fd(offer->input->display,
3924 offer->fd, EPOLLIN, &offer->io_task);
3925}
3926
3927void
3928input_receive_drag_data(struct input *input, const char *mime_type,
3929 data_func_t func, void *data)
3930{
3931 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003932 input->drag_offer->x = input->drag_x;
3933 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003934}
3935
3936int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003937input_receive_drag_data_to_fd(struct input *input,
3938 const char *mime_type, int fd)
3939{
3940 if (input->drag_offer)
3941 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3942
3943 return 0;
3944}
3945
3946int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003947input_receive_selection_data(struct input *input, const char *mime_type,
3948 data_func_t func, void *data)
3949{
3950 char **p;
3951
3952 if (input->selection_offer == NULL)
3953 return -1;
3954
3955 for (p = input->selection_offer->types.data; *p; p++)
3956 if (strcmp(mime_type, *p) == 0)
3957 break;
3958
3959 if (*p == NULL)
3960 return -1;
3961
3962 data_offer_receive_data(input->selection_offer,
3963 mime_type, func, data);
3964 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003965}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003966
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003967int
3968input_receive_selection_data_to_fd(struct input *input,
3969 const char *mime_type, int fd)
3970{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003971 if (input->selection_offer)
3972 wl_data_offer_receive(input->selection_offer->offer,
3973 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003974
3975 return 0;
3976}
3977
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003978void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003979window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003980{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08003981 if (!window->xdg_toplevel)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003982 return;
3983
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08003984 zxdg_toplevel_v6_move(window->xdg_toplevel, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003985}
3986
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003987static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003988surface_set_synchronized(struct surface *surface)
3989{
3990 if (!surface->subsurface)
3991 return;
3992
3993 if (surface->synchronized)
3994 return;
3995
3996 wl_subsurface_set_sync(surface->subsurface);
3997 surface->synchronized = 1;
3998}
3999
4000static void
4001surface_set_synchronized_default(struct surface *surface)
4002{
4003 if (!surface->subsurface)
4004 return;
4005
4006 if (surface->synchronized == surface->synchronized_default)
4007 return;
4008
4009 if (surface->synchronized_default)
4010 wl_subsurface_set_sync(surface->subsurface);
4011 else
4012 wl_subsurface_set_desync(surface->subsurface);
4013
4014 surface->synchronized = surface->synchronized_default;
4015}
4016
4017static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004018surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004019{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004020 struct widget *widget = surface->widget;
4021 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004022
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004023 if (surface->input_region) {
4024 wl_region_destroy(surface->input_region);
4025 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004026 }
4027
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004028 if (surface->opaque_region)
4029 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004030
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004031 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004032
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004033 if (widget->resize_handler)
4034 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004035 widget->allocation.width,
4036 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004037 widget->user_data);
4038
Pekka Paalanen35e82632013-04-25 13:57:48 +03004039 if (surface->subsurface &&
4040 (surface->allocation.x != widget->allocation.x ||
4041 surface->allocation.y != widget->allocation.y)) {
4042 wl_subsurface_set_position(surface->subsurface,
4043 widget->allocation.x,
4044 widget->allocation.y);
4045 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004046 if (surface->allocation.width != widget->allocation.width ||
4047 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004048 window_schedule_redraw(widget->window);
4049 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004050 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004051
4052 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004053 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004054 widget->allocation.width,
4055 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004056}
4057
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004058static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004059window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004060{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004061 struct surface *surface;
4062
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004063 widget_set_allocation(window->main_surface->widget,
4064 window->pending_allocation.x,
4065 window->pending_allocation.y,
4066 window->pending_allocation.width,
4067 window->pending_allocation.height);
4068
4069 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004070
4071 /* The main surface is in the list, too. Main surface's
4072 * resize_handler is responsible for calling widget_set_allocation()
4073 * on all sub-surface root widgets, so they will be resized
4074 * properly.
4075 */
4076 wl_list_for_each(surface, &window->subsurface_list, link) {
4077 if (surface == window->main_surface)
4078 continue;
4079
4080 surface_set_synchronized(surface);
4081 surface_resize(surface);
4082 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004083
4084 if (!window->fullscreen && !window->maximized)
4085 window->saved_allocation = window->pending_allocation;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004086
4087 if (window->confined && window->confined_widget) {
4088 struct wl_compositor *compositor = window->display->compositor;
4089 struct wl_region *region;
4090 struct widget *widget = window->confined_widget;
4091
4092 region = wl_compositor_create_region(compositor);
4093 wl_region_add(region,
4094 widget->allocation.x,
4095 widget->allocation.y,
4096 widget->allocation.width,
4097 widget->allocation.height);
4098 zwp_confined_pointer_v1_set_region(window->confined_pointer,
4099 region);
4100 wl_region_destroy(region);
4101 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004102}
4103
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004104static void
4105idle_resize(struct window *window)
4106{
4107 window->resize_needed = 0;
4108 window->redraw_needed = 1;
4109
4110 DBG("from %dx%d to %dx%d\n",
4111 window->main_surface->server_allocation.width,
4112 window->main_surface->server_allocation.height,
4113 window->pending_allocation.width,
4114 window->pending_allocation.height);
4115
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004116 window_do_resize(window);
4117}
4118
4119static void
4120undo_resize(struct window *window)
4121{
4122 window->pending_allocation.width =
4123 window->main_surface->server_allocation.width;
4124 window->pending_allocation.height =
4125 window->main_surface->server_allocation.height;
4126
4127 DBG("back to %dx%d\n",
4128 window->main_surface->server_allocation.width,
4129 window->main_surface->server_allocation.height);
4130
4131 window_do_resize(window);
4132
4133 if (window->pending_allocation.width == 0 &&
4134 window->pending_allocation.height == 0) {
4135 fprintf(stderr, "Error: Could not draw a surface, "
4136 "most likely due to insufficient disk space in "
4137 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4138 exit(EXIT_FAILURE);
4139 }
4140}
4141
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004142void
4143window_schedule_resize(struct window *window, int width, int height)
4144{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004145 /* We should probably get these numbers from the theme. */
4146 const int min_width = 200, min_height = 200;
4147
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004148 window->pending_allocation.x = 0;
4149 window->pending_allocation.y = 0;
4150 window->pending_allocation.width = width;
4151 window->pending_allocation.height = height;
4152
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004153 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004154 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004155 window->min_allocation.width = min_width;
4156 else
4157 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004158 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004159 window->min_allocation.height = min_height;
4160 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004161 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004162 }
4163
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004164 if (window->pending_allocation.width < window->min_allocation.width)
4165 window->pending_allocation.width = window->min_allocation.width;
4166 if (window->pending_allocation.height < window->min_allocation.height)
4167 window->pending_allocation.height = window->min_allocation.height;
4168
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004169 window->resize_needed = 1;
4170 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004171}
4172
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004173void
4174widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4175{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004176 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004177}
4178
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004179static int
4180window_get_shadow_margin(struct window *window)
4181{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004182 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004183 return frame_get_shadow_margin(window->frame->frame);
4184 else
4185 return 0;
4186}
4187
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004188static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004189window_inhibit_redraw(struct window *window)
4190{
4191 window->redraw_inhibited = 1;
4192 wl_list_remove(&window->redraw_task.link);
4193 wl_list_init(&window->redraw_task.link);
4194 window->redraw_task_scheduled = 0;
4195}
4196
4197static void
4198window_uninhibit_redraw(struct window *window)
4199{
4200 window->redraw_inhibited = 0;
4201 if (window->redraw_needed || window->resize_needed)
4202 window_schedule_redraw_task(window);
4203}
4204
4205static void
4206xdg_surface_handle_configure(void *data,
4207 struct zxdg_surface_v6 *zxdg_surface_v6,
4208 uint32_t serial)
4209{
4210 struct window *window = data;
4211
4212 zxdg_surface_v6_ack_configure(window->xdg_surface, serial);
4213
4214 if (window->state_changed_handler)
4215 window->state_changed_handler(window, window->user_data);
4216
4217 window_uninhibit_redraw(window);
4218}
4219
4220static const struct zxdg_surface_v6_listener xdg_surface_listener = {
4221 xdg_surface_handle_configure
4222};
4223
4224static void
4225xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *xdg_toplevel,
4226 int32_t width, int32_t height,
4227 struct wl_array *states)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004228{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004229 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004230 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004231
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004232 window->maximized = 0;
4233 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004234 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004235 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004236
4237 wl_array_for_each(p, states) {
4238 uint32_t state = *p;
4239 switch (state) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004240 case ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004241 window->maximized = 1;
4242 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004243 case ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN:
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004244 window->fullscreen = 1;
4245 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004246 case ZXDG_TOPLEVEL_V6_STATE_RESIZING:
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004247 window->resizing = 1;
4248 break;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004249 case ZXDG_TOPLEVEL_V6_STATE_ACTIVATED:
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004250 window->focused = 1;
4251 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004252 default:
4253 /* Unknown state */
4254 break;
4255 }
4256 }
4257
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004258 if (window->frame) {
4259 if (window->maximized) {
4260 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4261 } else {
4262 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4263 }
4264
4265 if (window->focused) {
4266 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4267 } else {
4268 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4269 }
4270 }
4271
Jasper St. Pierref184c382014-05-06 08:33:27 -04004272 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004273 /* The width / height params are for window geometry,
4274 * but window_schedule_resize takes allocation. Add
4275 * on the shadow margin to get the difference. */
4276 int margin = window_get_shadow_margin(window);
4277
4278 window_schedule_resize(window,
4279 width + margin * 2,
4280 height + margin * 2);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004281 } else if (window->saved_allocation.width > 0 &&
4282 window->saved_allocation.height > 0) {
Jasper St. Pierref184c382014-05-06 08:33:27 -04004283 window_schedule_resize(window,
4284 window->saved_allocation.width,
4285 window->saved_allocation.height);
4286 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004287}
4288
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004289static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004290xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_surface)
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004291{
4292 struct window *window = data;
4293 window_close(window);
4294}
4295
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004296static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
4297 xdg_toplevel_handle_configure,
4298 xdg_toplevel_handle_close,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004299};
4300
4301static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004302window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004303{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004304 struct zxdg_toplevel_v6 *parent_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004305
4306 if (!window->xdg_surface)
4307 return;
4308
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004309 if (window->parent == window->last_parent)
4310 return;
4311
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004312 if (window->parent)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004313 parent_toplevel = window->parent->xdg_toplevel;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004314 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004315 parent_toplevel = NULL;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004316
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004317 zxdg_toplevel_v6_set_parent(window->xdg_toplevel, parent_toplevel);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004318 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004319}
4320
4321static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004322window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004323{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004324 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004325 frame_input_rect(window->frame->frame,
4326 &geometry->x,
4327 &geometry->y,
4328 &geometry->width,
4329 &geometry->height);
4330 else
4331 window_get_allocation(window, geometry);
4332}
4333
4334static void
4335window_sync_geometry(struct window *window)
4336{
4337 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004338
4339 if (!window->xdg_surface)
4340 return;
4341
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004342 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004343 if (geometry.x == window->last_geometry.x &&
4344 geometry.y == window->last_geometry.y &&
4345 geometry.width == window->last_geometry.width &&
4346 geometry.height == window->last_geometry.height)
4347 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004348
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004349 zxdg_surface_v6_set_window_geometry(window->xdg_surface,
4350 geometry.x,
4351 geometry.y,
4352 geometry.width,
4353 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004354 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004355}
4356
4357static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004358window_flush(struct window *window)
4359{
4360 struct surface *surface;
4361
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004362 assert(!window->redraw_inhibited);
4363
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004364 if (!window->custom) {
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004365 if (window->xdg_surface)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004366 window_sync_geometry(window);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004367 if (window->xdg_toplevel)
4368 window_sync_parent(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004369 }
4370
4371 wl_list_for_each(surface, &window->subsurface_list, link) {
4372 if (surface == window->main_surface)
4373 continue;
4374
4375 surface_flush(surface);
4376 }
4377
4378 surface_flush(window->main_surface);
4379}
4380
4381static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004382menu_destroy(struct menu *menu)
4383{
4384 widget_destroy(menu->widget);
4385 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004386 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004387 free(menu);
4388}
4389
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004390void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004391window_get_allocation(struct window *window,
4392 struct rectangle *allocation)
4393{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004394 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004395}
4396
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004397static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004398widget_redraw(struct widget *widget)
4399{
4400 struct widget *child;
4401
4402 if (widget->redraw_handler)
4403 widget->redraw_handler(widget, widget->user_data);
4404 wl_list_for_each(child, &widget->child_list, link)
4405 widget_redraw(child);
4406}
4407
4408static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004409frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4410{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004411 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004412
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004413 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004414 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004415 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004416 surface->frame_cb = NULL;
4417
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004418 surface->last_time = time;
4419
Pekka Paalanen71233882013-04-25 13:57:53 +03004420 if (surface->redraw_needed || surface->window->redraw_needed) {
4421 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004422 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004423 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004424}
4425
4426static const struct wl_callback_listener listener = {
4427 frame_callback
4428};
4429
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004430static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004431surface_redraw(struct surface *surface)
4432{
Pekka Paalanen71233882013-04-25 13:57:53 +03004433 DBG_OBJ(surface->surface, "begin\n");
4434
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004435 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004436 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004437
4438 /* Whole-window redraw forces a redraw even if the previous has
4439 * not yet hit the screen.
4440 */
4441 if (surface->frame_cb) {
4442 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004443 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004444
Pekka Paalanen71233882013-04-25 13:57:53 +03004445 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004446 wl_callback_destroy(surface->frame_cb);
4447 }
4448
Neil Roberts97b747c2013-12-19 16:17:12 +00004449 if (surface->widget->use_cairo &&
4450 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004451 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004452 return -1;
4453 }
4454
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004455 surface->frame_cb = wl_surface_frame(surface->surface);
4456 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004457 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004458
4459 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004460 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004461 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004462 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004463 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004464}
4465
4466static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004467idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004468{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004469 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004470 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004471 int failed = 0;
4472 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004473
Pekka Paalanen71233882013-04-25 13:57:53 +03004474 DBG(" --------- \n");
4475
Pekka Paalaneneebff542013-04-25 13:57:52 +03004476 wl_list_init(&window->redraw_task.link);
4477 window->redraw_task_scheduled = 0;
4478
4479 if (window->resize_needed) {
4480 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004481 if (window->main_surface->frame_cb) {
4482 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004483 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004484 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004485
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004486 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004487 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004488 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004489
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004490 if (surface_redraw(window->main_surface) < 0) {
4491 /*
4492 * Only main_surface failure will cause us to undo the resize.
4493 * If sub-surfaces fail, they will just be broken with old
4494 * content.
4495 */
4496 failed = 1;
4497 } else {
4498 wl_list_for_each(surface, &window->subsurface_list, link) {
4499 if (surface == window->main_surface)
4500 continue;
4501
4502 surface_redraw(surface);
4503 }
4504 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004505
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004506 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004507 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004508
4509 wl_list_for_each(surface, &window->subsurface_list, link)
4510 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004511
4512 if (resized && failed) {
4513 /* Restore widget tree to correspond to what is on screen. */
4514 undo_resize(window);
4515 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004516}
4517
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004518static void
4519window_schedule_redraw_task(struct window *window)
4520{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004521 if (window->redraw_inhibited)
4522 return;
4523
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004524 if (!window->redraw_task_scheduled) {
4525 window->redraw_task.run = idle_redraw;
4526 display_defer(window->display, &window->redraw_task);
4527 window->redraw_task_scheduled = 1;
4528 }
4529}
4530
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004531void
4532window_schedule_redraw(struct window *window)
4533{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004534 struct surface *surface;
4535
Pekka Paalanen71233882013-04-25 13:57:53 +03004536 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4537
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004538 wl_list_for_each(surface, &window->subsurface_list, link)
4539 surface->redraw_needed = 1;
4540
4541 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004542}
4543
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004544int
4545window_is_fullscreen(struct window *window)
4546{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004547 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004548}
4549
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004550void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004551window_set_fullscreen(struct window *window, int fullscreen)
4552{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004553 if (!window->xdg_toplevel)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004554 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004555
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004556 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004557 return;
4558
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004559 if (fullscreen)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004560 zxdg_toplevel_v6_set_fullscreen(window->xdg_toplevel, NULL);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004561 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004562 zxdg_toplevel_v6_unset_fullscreen(window->xdg_toplevel);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004563}
4564
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004565int
4566window_is_maximized(struct window *window)
4567{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004568 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004569}
4570
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004571void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004572window_set_maximized(struct window *window, int maximized)
4573{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004574 if (!window->xdg_toplevel)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004575 return;
4576
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004577 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004578 return;
4579
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004580 if (maximized)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004581 zxdg_toplevel_v6_set_maximized(window->xdg_toplevel);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004582 else
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004583 zxdg_toplevel_v6_unset_maximized(window->xdg_toplevel);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004584}
4585
Jasper St. Pierrede680992014-04-10 17:23:49 -07004586int
4587window_is_resizing(struct window *window)
4588{
4589 return window->resizing;
4590}
4591
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004592void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004593window_set_minimized(struct window *window)
4594{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004595 if (!window->xdg_toplevel)
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004596 return;
4597
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004598 zxdg_toplevel_v6_set_minimized(window->xdg_toplevel);
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004599}
4600
4601void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004602window_set_user_data(struct window *window, void *data)
4603{
4604 window->user_data = data;
4605}
4606
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004607void *
4608window_get_user_data(struct window *window)
4609{
4610 return window->user_data;
4611}
4612
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004613void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004614window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004615 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004616{
4617 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004618}
4619
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004620void
4621window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004622 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004623{
4624 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004625}
4626
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004627void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004628window_set_data_handler(struct window *window, window_data_handler_t handler)
4629{
4630 window->data_handler = handler;
4631}
4632
4633void
4634window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4635{
4636 window->drop_handler = handler;
4637}
4638
4639void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004640window_set_close_handler(struct window *window,
4641 window_close_handler_t handler)
4642{
4643 window->close_handler = handler;
4644}
4645
4646void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004647window_set_fullscreen_handler(struct window *window,
4648 window_fullscreen_handler_t handler)
4649{
4650 window->fullscreen_handler = handler;
4651}
4652
4653void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004654window_set_output_handler(struct window *window,
4655 window_output_handler_t handler)
4656{
4657 window->output_handler = handler;
4658}
4659
4660void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004661window_set_state_changed_handler(struct window *window,
4662 window_state_changed_handler_t handler)
4663{
4664 window->state_changed_handler = handler;
4665}
4666
4667void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004668window_set_pointer_locked_handler(struct window *window,
4669 locked_pointer_locked_handler_t locked,
4670 locked_pointer_unlocked_handler_t unlocked)
4671{
4672 window->pointer_unlocked_handler = unlocked;
4673 window->pointer_locked_handler = locked;
4674}
4675
4676void
4677window_set_pointer_confined_handler(struct window *window,
4678 confined_pointer_confined_handler_t confined,
4679 confined_pointer_unconfined_handler_t unconfined)
4680{
4681 window->pointer_confined_handler = confined;
4682 window->pointer_unconfined_handler = unconfined;
4683}
4684
4685void
4686window_set_locked_pointer_motion_handler(struct window *window,
4687 window_locked_pointer_motion_handler_t handler)
4688{
4689 window->locked_pointer_motion_handler = handler;
4690}
4691
4692void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004693window_set_title(struct window *window, const char *title)
4694{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004695 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004696 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004697 if (window->frame) {
4698 frame_set_title(window->frame->frame, title);
4699 widget_schedule_redraw(window->frame->widget);
4700 }
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08004701 if (window->xdg_toplevel)
4702 zxdg_toplevel_v6_set_title(window->xdg_toplevel, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004703}
4704
4705const char *
4706window_get_title(struct window *window)
4707{
4708 return window->title;
4709}
4710
4711void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004712window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4713{
4714 struct text_cursor_position *text_cursor_position =
4715 window->display->text_cursor_position;
4716
Scott Moreau9295ce02012-06-01 12:46:10 -06004717 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004718 return;
4719
4720 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004721 window->main_surface->surface,
4722 wl_fixed_from_int(x),
4723 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004724}
4725
Casey Dahlin9074db52012-04-19 22:50:09 -04004726static void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004727relative_pointer_handle_motion(void *data, struct zwp_relative_pointer_v1 *pointer,
4728 uint32_t utime_hi,
4729 uint32_t utime_lo,
4730 wl_fixed_t dx,
4731 wl_fixed_t dy,
4732 wl_fixed_t dx_unaccel,
4733 wl_fixed_t dy_unaccel)
4734{
4735 struct input *input = data;
4736 struct window *window = input->pointer_focus;
4737 uint32_t ms = (((uint64_t) utime_hi) << 32 | utime_lo) / 1000;
4738
4739 if (window->locked_pointer_motion_handler &&
4740 window->pointer_locked) {
4741 window->locked_pointer_motion_handler(
4742 window, input, ms,
4743 wl_fixed_to_double(dx),
4744 wl_fixed_to_double(dy),
4745 window->user_data);
4746 }
4747}
4748
4749static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
4750 relative_pointer_handle_motion,
4751};
4752
4753static void
4754locked_pointer_locked(void *data,
4755 struct zwp_locked_pointer_v1 *locked_pointer)
4756{
4757 struct input *input = data;
4758 struct window *window = input->pointer_focus;
4759
4760 window->pointer_locked = true;
4761
4762 if (window->pointer_locked_handler) {
4763 window->pointer_locked_handler(window,
4764 input,
4765 window->user_data);
4766 }
4767}
4768
4769static void
4770locked_pointer_unlocked(void *data,
4771 struct zwp_locked_pointer_v1 *locked_pointer)
4772{
4773 struct input *input = data;
4774 struct window *window = input->pointer_focus;
4775
4776 window_unlock_pointer(window);
4777
4778 if (window->pointer_unlocked_handler) {
4779 window->pointer_unlocked_handler(window,
4780 input,
4781 window->user_data);
4782 }
4783}
4784
4785static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
4786 locked_pointer_locked,
4787 locked_pointer_unlocked,
4788};
4789
4790int
4791window_lock_pointer(struct window *window, struct input *input)
4792{
4793 struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
4794 window->display->relative_pointer_manager;
4795 struct zwp_pointer_constraints_v1 *pointer_constraints =
4796 window->display->pointer_constraints;
4797 struct zwp_relative_pointer_v1 *relative_pointer;
4798 struct zwp_locked_pointer_v1 *locked_pointer;
4799
4800 if (!window->display->relative_pointer_manager)
4801 return -1;
4802
4803 if (!window->display->pointer_constraints)
4804 return -1;
4805
4806 if (window->locked_pointer)
4807 return -1;
4808
4809 if (window->confined_pointer)
4810 return -1;
4811
4812 if (!input->pointer)
4813 return -1;
4814
4815 relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
4816 relative_pointer_manager, input->pointer);
4817 zwp_relative_pointer_v1_add_listener(relative_pointer,
4818 &relative_pointer_listener,
4819 input);
4820
4821 locked_pointer =
4822 zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
4823 window->main_surface->surface,
4824 input->pointer,
4825 NULL,
4826 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4827 zwp_locked_pointer_v1_add_listener(locked_pointer,
4828 &locked_pointer_listener,
4829 input);
4830
4831 window->locked_input = input;
4832 window->locked_pointer = locked_pointer;
4833 window->relative_pointer = relative_pointer;
4834
4835 return 0;
4836}
4837
4838void
4839window_unlock_pointer(struct window *window)
4840{
4841 if (!window->locked_pointer)
4842 return;
4843
4844 zwp_locked_pointer_v1_destroy(window->locked_pointer);
4845 zwp_relative_pointer_v1_destroy(window->relative_pointer);
4846 window->locked_pointer = NULL;
4847 window->relative_pointer = NULL;
4848 window->pointer_locked = false;
4849 window->locked_input = NULL;
4850}
4851
4852void
4853widget_set_locked_pointer_cursor_hint(struct widget *widget,
4854 float x, float y)
4855{
4856 struct window *window = widget->window;
4857
4858 if (!window->locked_pointer)
4859 return;
4860
4861 zwp_locked_pointer_v1_set_cursor_position_hint(window->locked_pointer,
4862 wl_fixed_from_double(x),
4863 wl_fixed_from_double(y));
4864 wl_surface_commit(window->main_surface->surface);
4865}
4866
4867static void
4868confined_pointer_confined(void *data,
4869 struct zwp_confined_pointer_v1 *confined_pointer)
4870{
4871 struct input *input = data;
4872 struct window *window = input->pointer_focus;
4873
4874 window->confined = true;
4875
4876 if (window->pointer_confined_handler) {
4877 window->pointer_confined_handler(window,
4878 input,
4879 window->user_data);
4880 }
4881}
4882
4883static void
4884confined_pointer_unconfined(void *data,
4885 struct zwp_confined_pointer_v1 *confined_pointer)
4886{
4887 struct input *input = data;
4888 struct window *window = input->pointer_focus;
4889
4890 window_unconfine_pointer(window);
4891
4892 window->confined = false;
4893
4894 if (window->pointer_unconfined_handler) {
4895 window->pointer_unconfined_handler(window,
4896 input,
4897 window->user_data);
4898 }
4899}
4900
4901static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
4902 confined_pointer_confined,
4903 confined_pointer_unconfined,
4904};
4905
4906int
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004907window_confine_pointer_to_rectangles(struct window *window,
4908 struct input *input,
4909 struct rectangle *rectangles,
4910 int num_rectangles)
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004911{
4912 struct zwp_pointer_constraints_v1 *pointer_constraints =
4913 window->display->pointer_constraints;
4914 struct zwp_confined_pointer_v1 *confined_pointer;
4915 struct wl_compositor *compositor = window->display->compositor;
4916 struct wl_region *region = NULL;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004917 int i;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004918
4919 if (!window->display->pointer_constraints)
4920 return -1;
4921
4922 if (window->locked_pointer)
4923 return -1;
4924
4925 if (window->confined_pointer)
4926 return -1;
4927
4928 if (!input->pointer)
4929 return -1;
4930
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004931 if (num_rectangles >= 1) {
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004932 region = wl_compositor_create_region(compositor);
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004933 for (i = 0; i < num_rectangles; i++) {
4934 wl_region_add(region,
4935 rectangles[i].x,
4936 rectangles[i].y,
4937 rectangles[i].width,
4938 rectangles[i].height);
4939 }
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004940 }
4941
4942 confined_pointer =
4943 zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
4944 window->main_surface->surface,
4945 input->pointer,
4946 region,
4947 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
4948 if (region)
4949 wl_region_destroy(region);
4950
4951 zwp_confined_pointer_v1_add_listener(confined_pointer,
4952 &confined_pointer_listener,
4953 input);
4954
4955 window->confined_pointer = confined_pointer;
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004956 window->confined_widget = NULL;
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08004957
4958 return 0;
4959}
4960
4961void
Jonas Ådahlfdcdda32015-05-08 14:17:24 +08004962window_update_confine_rectangles(struct window *window,
4963 struct rectangle *rectangles,
4964 int num_rectangles)
4965{
4966 struct wl_compositor *compositor = window->display->compositor;
4967 struct wl_region *region;
4968 int i;
4969
4970 region = wl_compositor_create_region(compositor);
4971 for (i = 0; i < num_rectangles; i++) {
4972 wl_region_add(region,
4973 rectangles[i].x,
4974 rectangles[i].y,
4975 rectangles[i].width,
4976 rectangles[i].height);
4977 }
4978
4979 zwp_confined_pointer_v1_set_region(window->confined_pointer, region);
4980
4981 wl_region_destroy(region);
4982}
4983
4984int
4985window_confine_pointer_to_widget(struct window *window,
4986 struct widget *widget,
4987 struct input *input)
4988{
4989 int ret;
4990
4991 if (widget) {
4992 ret = window_confine_pointer_to_rectangles(window,
4993 input,
4994 &widget->allocation,
4995 1);
4996 window->confined_widget = widget;
4997 return ret;
4998 } else {
4999 return window_confine_pointer_to_rectangles(window,
5000 input,
5001 NULL,
5002 0);
5003 }
5004}
5005
5006void
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005007window_unconfine_pointer(struct window *window)
5008{
5009 if (!window->confined_pointer)
5010 return;
5011
5012 zwp_confined_pointer_v1_destroy(window->confined_pointer);
5013 window->confined_pointer = NULL;
5014 window->confined = false;
5015}
5016
5017static void
Casey Dahlin9074db52012-04-19 22:50:09 -04005018surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01005019 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04005020{
Rob Bradford7507b572012-05-15 17:55:34 +01005021 struct window *window = data;
5022 struct output *output;
5023 struct output *output_found = NULL;
5024 struct window_output *window_output;
5025
5026 wl_list_for_each(output, &window->display->output_list, link) {
5027 if (output->output == wl_output) {
5028 output_found = output;
5029 break;
5030 }
5031 }
5032
5033 if (!output_found)
5034 return;
5035
Brian Lovinbc919262013-08-07 15:34:59 -07005036 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01005037 window_output->output = output_found;
5038
5039 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005040
5041 if (window->output_handler)
5042 window->output_handler(window, output_found, 1,
5043 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04005044}
5045
5046static void
5047surface_leave(void *data,
5048 struct wl_surface *wl_surface, struct wl_output *output)
5049{
Rob Bradford7507b572012-05-15 17:55:34 +01005050 struct window *window = data;
5051 struct window_output *window_output;
5052 struct window_output *window_output_found = NULL;
5053
5054 wl_list_for_each(window_output, &window->window_output_list, link) {
5055 if (window_output->output->output == output) {
5056 window_output_found = window_output;
5057 break;
5058 }
5059 }
5060
5061 if (window_output_found) {
5062 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005063
5064 if (window->output_handler)
5065 window->output_handler(window, window_output->output,
5066 0, window->user_data);
5067
Rob Bradford7507b572012-05-15 17:55:34 +01005068 free(window_output_found);
5069 }
Casey Dahlin9074db52012-04-19 22:50:09 -04005070}
5071
5072static const struct wl_surface_listener surface_listener = {
5073 surface_enter,
5074 surface_leave
5075};
5076
Pekka Paalanen4e373742013-02-13 16:17:13 +02005077static struct surface *
5078surface_create(struct window *window)
5079{
5080 struct display *display = window->display;
5081 struct surface *surface;
5082
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07005083 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02005084 surface->window = window;
5085 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02005086 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02005087 wl_surface_add_listener(surface->surface, &surface_listener, window);
5088
Pekka Paalanen35e82632013-04-25 13:57:48 +03005089 wl_list_insert(&window->subsurface_list, &surface->link);
5090
Pekka Paalanen4e373742013-02-13 16:17:13 +02005091 return surface;
5092}
5093
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005094static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005095get_preferred_buffer_type(struct display *display)
5096{
5097#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04005098 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005099 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
5100#endif
5101
5102 return WINDOW_BUFFER_TYPE_SHM;
5103}
5104
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005105static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005106window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05005107{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005108 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005109 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05005110
Peter Huttererf3d62272013-08-08 11:57:05 +10005111 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005112 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05005113 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005114
5115 surface = surface_create(window);
5116 window->main_surface = surface;
5117
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005118 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005119
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005120 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005121 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05005122
Jasper St. Pierrebd600772014-04-28 11:19:31 -04005123 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005124
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005125 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005126 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04005127 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005128
Rob Bradford7507b572012-05-15 17:55:34 +01005129 wl_list_init (&window->window_output_list);
5130
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005131 return window;
5132}
5133
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005134struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05005135window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005136{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005137 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005138 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005139
5140 window = window_create_internal(display, 0);
5141
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005142 if (window->display->xdg_shell) {
5143 window->xdg_surface =
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005144 zxdg_shell_v6_get_xdg_surface(window->display->xdg_shell,
5145 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005146 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005147
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005148 zxdg_surface_v6_add_listener(window->xdg_surface,
5149 &xdg_surface_listener, window);
5150
5151 window->xdg_toplevel =
5152 zxdg_surface_v6_get_toplevel(window->xdg_surface);
5153 fail_on_null(window->xdg_toplevel, 0, __FILE__, __LINE__);
5154
5155 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
5156 &xdg_toplevel_listener, window);
5157
5158 window_inhibit_redraw(window);
5159
5160 wl_surface_commit(window->main_surface->surface);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005161 } else if (display->ivi_application) {
5162 /* auto generation of ivi_id based on process id + basement of id */
5163 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
5164 window->ivi_surface =
5165 ivi_application_surface_create(display->ivi_application,
5166 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005167 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005168
5169 ivi_surface_add_listener(window->ivi_surface,
5170 &ivi_surface_listener, window);
5171 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07005172
5173 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04005174}
5175
5176struct window *
5177window_create_custom(struct display *display)
5178{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005179 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005180}
5181
Jasper St. Pierre53686042013-12-09 15:26:25 -05005182void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005183window_set_parent(struct window *window,
5184 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005185{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005186 window->parent = parent_window;
5187 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05005188}
5189
5190struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005191window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05005192{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07005193 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05005194}
5195
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005196static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05005197menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005198{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005199 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005200 int next;
5201
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005202 frame_interior(menu->frame, &x, &y, &width, &height);
5203 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005204 if (menu->current != next) {
5205 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005206 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005207 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005208}
5209
5210static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05005211menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005212 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005213 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005214{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005215 struct menu *menu = data;
5216
5217 if (widget == menu->widget)
5218 menu_set_item(data, y);
5219
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005220 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005221}
5222
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05005223static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005224menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04005225 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005226{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005227 struct menu *menu = data;
5228
5229 if (widget == menu->widget)
5230 menu_set_item(data, y);
5231
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03005232 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005233}
5234
5235static void
5236menu_leave_handler(struct widget *widget, struct input *input, void *data)
5237{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005238 struct menu *menu = data;
5239
5240 if (widget == menu->widget)
5241 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005242}
5243
5244static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05005245menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005246 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01005247 uint32_t button, enum wl_pointer_button_state state,
5248 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005249
5250{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005251 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005252
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005253 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
5254 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005255 /* Either relase after press-drag-release or
5256 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04005257 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05005258 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02005259 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005260 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005261 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04005262 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005263}
5264
5265static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005266menu_touch_up_handler(struct widget *widget,
5267 struct input *input,
5268 uint32_t serial,
5269 uint32_t time,
5270 int32_t id,
5271 void *data)
5272{
5273 struct menu *menu = data;
5274
5275 input_ungrab(input);
5276 menu_destroy(menu);
5277}
5278
5279static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005280menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005281{
5282 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005283 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005284 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005285
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02005286 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005287
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005288 frame_repaint(menu->frame, cr);
5289 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05005290
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005291 theme_set_background_source(menu->window->display->theme,
5292 cr, THEME_FRAME_ACTIVE);
5293 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005294 cairo_fill(cr);
5295
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005296 cairo_select_font_face(cr, "sans",
5297 CAIRO_FONT_SLANT_NORMAL,
5298 CAIRO_FONT_WEIGHT_NORMAL);
5299 cairo_set_font_size(cr, 12);
5300
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005301 for (i = 0; i < menu->count; i++) {
5302 if (i == menu->current) {
5303 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005304 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005305 cairo_fill(cr);
5306 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005307 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005308 cairo_show_text(cr, menu->entries[i]);
5309 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005310 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
5311 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005312 cairo_show_text(cr, menu->entries[i]);
5313 }
5314 }
5315
5316 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005317}
5318
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005319static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005320xdg_popup_handle_configure(void *data,
5321 struct zxdg_popup_v6 *zxdg_popup_v6,
5322 int32_t x,
5323 int32_t y,
5324 int32_t width,
5325 int32_t height)
5326{
5327}
5328
5329static void
5330xdg_popup_handle_popup_done(void *data, struct zxdg_popup_v6 *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005331{
5332 struct window *window = data;
5333 struct menu *menu = window->main_surface->widget->user_data;
5334
5335 input_ungrab(menu->input);
5336 menu_destroy(menu);
5337}
5338
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005339static const struct zxdg_popup_v6_listener xdg_popup_listener = {
5340 xdg_popup_handle_configure,
5341 xdg_popup_handle_popup_done,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005342};
5343
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005344static struct menu *
5345create_menu(struct display *display,
5346 struct input *input, uint32_t time,
5347 menu_func_t func, const char **entries, int count,
5348 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005349{
5350 struct window *window;
5351 struct menu *menu;
5352
5353 menu = malloc(sizeof *menu);
5354 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005355 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005356
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005357 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02005358 if (!window) {
5359 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005360 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02005361 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005362
5363 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005364 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005365 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005366 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07005367 FRAME_BUTTON_NONE, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005368 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005369 menu->entries = entries;
5370 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04005371 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005372 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05005373 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05005374 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005375 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005376
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07005377 input_ungrab(input);
5378
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05005379 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05005380 widget_set_enter_handler(menu->widget, menu_enter_handler);
5381 widget_set_leave_handler(menu->widget, menu_leave_handler);
5382 widget_set_motion_handler(menu->widget, menu_motion_handler);
5383 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07005384 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05005385
Kristian Høgsberg831dd522012-01-10 23:46:33 -05005386 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005387 frame_resize_inside(menu->frame, 200, count * 20);
5388 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
5389 window_schedule_resize(window, frame_width(menu->frame),
5390 frame_height(menu->frame));
5391
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005392 return menu;
5393}
5394
5395struct window *
5396window_create_menu(struct display *display,
5397 struct input *input, uint32_t time,
5398 menu_func_t func, const char **entries, int count,
5399 void *user_data)
5400{
5401 struct menu *menu;
5402 menu = create_menu(display, input, time, func, entries, count, user_data);
5403
5404 if (menu == NULL)
5405 return NULL;
5406
5407 return menu->window;
5408}
5409
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005410static struct zxdg_positioner_v6 *
5411create_simple_positioner(struct display *display,
5412 int x, int y)
5413{
5414 struct zxdg_positioner_v6 *positioner;
5415
5416 positioner = zxdg_shell_v6_create_positioner(display->xdg_shell);
5417 fail_on_null(positioner, 0, __FILE__, __LINE__);
5418 zxdg_positioner_v6_set_anchor_rect(positioner, x, y, 1, 1);
5419 zxdg_positioner_v6_set_anchor(positioner,
5420 ZXDG_POSITIONER_V6_ANCHOR_TOP |
5421 ZXDG_POSITIONER_V6_ANCHOR_LEFT);
5422 zxdg_positioner_v6_set_gravity(positioner,
5423 ZXDG_POSITIONER_V6_ANCHOR_BOTTOM |
5424 ZXDG_POSITIONER_V6_ANCHOR_RIGHT);
5425
5426 return positioner;
5427}
5428
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005429void
5430window_show_menu(struct display *display,
5431 struct input *input, uint32_t time, struct window *parent,
5432 int32_t x, int32_t y,
5433 menu_func_t func, const char **entries, int count)
5434{
5435 struct menu *menu;
5436 struct window *window;
5437 int32_t ix, iy;
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005438 struct rectangle parent_geometry;
5439 struct zxdg_positioner_v6 *positioner;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04005440
5441 menu = create_menu(display, input, time, func, entries, count, parent);
5442
5443 if (menu == NULL)
5444 return;
5445
5446 window = menu->window;
5447
5448 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5449 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5450
5451 window->x = x;
5452 window->y = y;
5453
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005454 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005455 window_get_geometry(parent, &parent_geometry);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005456
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005457 if (!display->xdg_shell)
5458 return;
5459
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005460 window->xdg_surface =
5461 zxdg_shell_v6_get_xdg_surface(display->xdg_shell,
5462 window->main_surface->surface);
5463 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005464
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005465 zxdg_surface_v6_add_listener(window->xdg_surface,
5466 &xdg_surface_listener, window);
5467
5468 positioner = create_simple_positioner(display,
5469 window->x - (ix + parent_geometry.x),
5470 window->y - (iy + parent_geometry.y));
5471 window->xdg_popup =
5472 zxdg_surface_v6_get_popup(window->xdg_surface,
5473 parent->xdg_surface,
5474 positioner);
5475 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
5476 zxdg_positioner_v6_destroy(positioner);
5477 zxdg_popup_v6_grab(window->xdg_popup,
5478 input->seat,
5479 display_get_serial(window->display));
5480 zxdg_popup_v6_add_listener(window->xdg_popup,
5481 &xdg_popup_listener, window);
5482
5483 window_inhibit_redraw(window);
5484
5485 wl_surface_commit(window->main_surface->surface);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005486}
5487
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005488void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005489window_set_buffer_type(struct window *window, enum window_buffer_type type)
5490{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005491 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005492}
5493
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005494enum window_buffer_type
5495window_get_buffer_type(struct window *window)
5496{
5497 return window->main_surface->buffer_type;
5498}
5499
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005500void
5501window_set_preferred_format(struct window *window,
5502 enum preferred_format format)
5503{
5504 window->preferred_format = format;
5505}
5506
Pekka Paalanen35e82632013-04-25 13:57:48 +03005507struct widget *
5508window_add_subsurface(struct window *window, void *data,
5509 enum subsurface_mode default_mode)
5510{
5511 struct widget *widget;
5512 struct surface *surface;
5513 struct wl_surface *parent;
5514 struct wl_subcompositor *subcompo = window->display->subcompositor;
5515
Pekka Paalanen35e82632013-04-25 13:57:48 +03005516 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005517 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005518 widget = widget_create(window, surface, data);
5519 wl_list_init(&widget->link);
5520 surface->widget = widget;
5521
5522 parent = window->main_surface->surface;
5523 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5524 surface->surface,
5525 parent);
5526 surface->synchronized = 1;
5527
5528 switch (default_mode) {
5529 case SUBSURFACE_SYNCHRONIZED:
5530 surface->synchronized_default = 1;
5531 break;
5532 case SUBSURFACE_DESYNCHRONIZED:
5533 surface->synchronized_default = 0;
5534 break;
5535 default:
5536 assert(!"bad enum subsurface_mode");
5537 }
5538
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005539 window->resize_needed = 1;
5540 window_schedule_redraw(window);
5541
Pekka Paalanen35e82632013-04-25 13:57:48 +03005542 return widget;
5543}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005544
5545static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005546display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005547 struct wl_output *wl_output,
5548 int x, int y,
5549 int physical_width,
5550 int physical_height,
5551 int subpixel,
5552 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005553 const char *model,
5554 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005555{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005556 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005557
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005558 output->allocation.x = x;
5559 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005560 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005561
5562 if (output->make)
5563 free(output->make);
5564 output->make = strdup(make);
5565
5566 if (output->model)
5567 free(output->model);
5568 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005569}
5570
5571static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005572display_handle_done(void *data,
5573 struct wl_output *wl_output)
5574{
5575}
5576
5577static void
5578display_handle_scale(void *data,
5579 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005580 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005581{
5582 struct output *output = data;
5583
5584 output->scale = scale;
5585}
5586
5587static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005588display_handle_mode(void *data,
5589 struct wl_output *wl_output,
5590 uint32_t flags,
5591 int width,
5592 int height,
5593 int refresh)
5594{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005595 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005596 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005597
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005598 if (flags & WL_OUTPUT_MODE_CURRENT) {
5599 output->allocation.width = width;
5600 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005601 if (display->output_configure_handler)
5602 (*display->output_configure_handler)(
5603 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005604 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005605}
5606
5607static const struct wl_output_listener output_listener = {
5608 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005609 display_handle_mode,
5610 display_handle_done,
5611 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005612};
5613
5614static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005615display_add_output(struct display *d, uint32_t id)
5616{
5617 struct output *output;
5618
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005619 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005620 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005621 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005622 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005623 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005624 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005625 wl_list_insert(d->output_list.prev, &output->link);
5626
5627 wl_output_add_listener(output->output, &output_listener, output);
5628}
5629
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005630static void
5631output_destroy(struct output *output)
5632{
5633 if (output->destroy_handler)
5634 (*output->destroy_handler)(output, output->user_data);
5635
5636 wl_output_destroy(output->output);
5637 wl_list_remove(&output->link);
5638 free(output);
5639}
5640
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005641static void
5642display_destroy_output(struct display *d, uint32_t id)
5643{
5644 struct output *output;
5645
5646 wl_list_for_each(output, &d->output_list, link) {
5647 if (output->server_output_id == id) {
5648 output_destroy(output);
5649 break;
5650 }
5651 }
5652}
5653
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005654void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005655display_set_global_handler(struct display *display,
5656 display_global_handler_t handler)
5657{
5658 struct global *global;
5659
5660 display->global_handler = handler;
5661 if (!handler)
5662 return;
5663
5664 wl_list_for_each(global, &display->global_list, link)
5665 display->global_handler(display,
5666 global->name, global->interface,
5667 global->version, display->user_data);
5668}
5669
5670void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005671display_set_global_handler_remove(struct display *display,
5672 display_global_handler_t remove_handler)
5673{
5674 display->global_handler_remove = remove_handler;
5675 if (!remove_handler)
5676 return;
5677}
5678
5679void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005680display_set_output_configure_handler(struct display *display,
5681 display_output_handler_t handler)
5682{
5683 struct output *output;
5684
5685 display->output_configure_handler = handler;
5686 if (!handler)
5687 return;
5688
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005689 wl_list_for_each(output, &display->output_list, link) {
5690 if (output->allocation.width == 0 &&
5691 output->allocation.height == 0)
5692 continue;
5693
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005694 (*display->output_configure_handler)(output,
5695 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005696 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005697}
5698
5699void
5700output_set_user_data(struct output *output, void *data)
5701{
5702 output->user_data = data;
5703}
5704
5705void *
5706output_get_user_data(struct output *output)
5707{
5708 return output->user_data;
5709}
5710
5711void
5712output_set_destroy_handler(struct output *output,
5713 display_output_handler_t handler)
5714{
5715 output->destroy_handler = handler;
5716 /* FIXME: implement this, once we have way to remove outputs */
5717}
5718
5719void
Scott Moreau4e072362012-09-29 02:03:11 -06005720output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005721{
Scott Moreau4e072362012-09-29 02:03:11 -06005722 struct rectangle allocation = output->allocation;
5723
5724 switch (output->transform) {
5725 case WL_OUTPUT_TRANSFORM_90:
5726 case WL_OUTPUT_TRANSFORM_270:
5727 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5728 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5729 /* Swap width and height */
5730 allocation.width = output->allocation.height;
5731 allocation.height = output->allocation.width;
5732 break;
5733 }
5734
5735 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005736}
5737
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005738struct wl_output *
5739output_get_wl_output(struct output *output)
5740{
5741 return output->output;
5742}
5743
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005744enum wl_output_transform
5745output_get_transform(struct output *output)
5746{
5747 return output->transform;
5748}
5749
Alexander Larssonafd319a2013-05-22 14:41:27 +02005750uint32_t
5751output_get_scale(struct output *output)
5752{
5753 return output->scale;
5754}
5755
Jason Ekstrand738715d2014-04-02 19:53:50 -05005756const char *
5757output_get_make(struct output *output)
5758{
5759 return output->make;
5760}
5761
5762const char *
5763output_get_model(struct output *output)
5764{
5765 return output->model;
5766}
5767
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005768static void
Daniel Stone97f68542012-05-30 16:32:01 +01005769fini_xkb(struct input *input)
5770{
5771 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005772 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005773}
5774
5775static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005776display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005777{
5778 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005779 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005780
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005781 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005782 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005783 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005784 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005785 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005786 input->pointer_focus = NULL;
5787 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005788 input->seat_version = seat_version;
5789
Rusty Lynch041815a2013-08-08 21:20:38 -07005790 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005791 wl_list_insert(d->input_list.prev, &input->link);
5792
Daniel Stone37816df2012-05-16 18:45:18 +01005793 wl_seat_add_listener(input->seat, &seat_listener, input);
5794 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005795
Jason Ekstranda669bd52014-04-02 19:53:51 -05005796 if (d->data_device_manager) {
5797 input->data_device =
5798 wl_data_device_manager_get_data_device(d->data_device_manager,
5799 input->seat);
5800 wl_data_device_add_listener(input->data_device,
5801 &data_device_listener,
5802 input);
5803 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005804
5805 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005806 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005807
Derek Foreman118a4292015-04-22 17:23:35 -05005808 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5809 TFD_CLOEXEC | TFD_NONBLOCK);
5810 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5811 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005812 set_repeat_info(input, 40, 400);
5813
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005814 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5815 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005816 input->repeat_task.run = keyboard_repeat_func;
5817 display_watch_fd(d, input->repeat_timer_fd,
5818 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005819}
5820
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005821static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005822input_destroy(struct input *input)
5823{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005824 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005825 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005826
5827 if (input->drag_offer)
5828 data_offer_destroy(input->drag_offer);
5829
5830 if (input->selection_offer)
5831 data_offer_destroy(input->selection_offer);
5832
kabeer khan6ce67ec2014-10-20 11:55:29 +05305833 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005834 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305835 wl_data_device_release(input->data_device);
5836 else
5837 wl_data_device_destroy(input->data_device);
5838 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005839 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005840 if (input->touch)
5841 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005842 if (input->pointer)
5843 wl_pointer_release(input->pointer);
5844 if (input->keyboard)
5845 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005846 } else {
5847 if (input->touch)
5848 wl_touch_destroy(input->touch);
5849 if (input->pointer)
5850 wl_pointer_destroy(input->pointer);
5851 if (input->keyboard)
5852 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005853 }
5854
Daniel Stone97f68542012-05-30 16:32:01 +01005855 fini_xkb(input);
5856
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005857 wl_surface_destroy(input->pointer_surface);
5858
Pekka Paalanene1207c72011-12-16 12:02:09 +02005859 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005860 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005861 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005862 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005863 free(input);
5864}
5865
5866static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005867shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5868{
5869 struct display *d = data;
5870
5871 if (format == WL_SHM_FORMAT_RGB565)
5872 d->has_rgb565 = 1;
5873}
5874
5875struct wl_shm_listener shm_listener = {
5876 shm_format
5877};
5878
5879static void
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005880xdg_shell_handle_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005881{
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005882 zxdg_shell_v6_pong(shell, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005883}
5884
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005885static const struct zxdg_shell_v6_listener xdg_shell_listener = {
5886 xdg_shell_handle_ping,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005887};
5888
5889static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005890registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5891 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005892{
5893 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005894 struct global *global;
5895
Brian Lovinbc919262013-08-07 15:34:59 -07005896 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005897 global->name = id;
5898 global->interface = strdup(interface);
5899 global->version = version;
5900 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005901
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005902 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005903 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005904 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005905 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005906 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005907 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005908 display_add_input(d, id, version);
Jonas Ådahle5a1bb42014-11-25 10:25:27 +08005909 } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0 &&
5910 version == ZWP_RELATIVE_POINTER_MANAGER_V1_VERSION) {
5911 d->relative_pointer_manager =
5912 wl_registry_bind(registry, id,
5913 &zwp_relative_pointer_manager_v1_interface,
5914 1);
5915 } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0 &&
5916 version == ZWP_POINTER_CONSTRAINTS_V1_VERSION) {
5917 d->pointer_constraints =
5918 wl_registry_bind(registry, id,
5919 &zwp_pointer_constraints_v1_interface,
5920 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005921 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005922 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005923 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005924 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005925 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005926 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005927 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305928 &wl_data_device_manager_interface,
5929 d->data_device_manager_version);
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005930 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005931 d->xdg_shell = wl_registry_bind(registry, id,
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08005932 &zxdg_shell_v6_interface, 1);
5933 zxdg_shell_v6_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005934 } else if (strcmp(interface, "text_cursor_position") == 0) {
5935 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005936 wl_registry_bind(registry, id,
5937 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005938 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5939 d->subcompositor =
5940 wl_registry_bind(registry, id,
5941 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005942 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005943 else if (strcmp(interface, "ivi_application") == 0) {
5944 d->ivi_application =
5945 wl_registry_bind(registry, id,
5946 &ivi_application_interface, 1);
5947 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005948
5949 if (d->global_handler)
5950 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005951}
5952
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005953static void
5954registry_handle_global_remove(void *data, struct wl_registry *registry,
5955 uint32_t name)
5956{
5957 struct display *d = data;
5958 struct global *global;
5959 struct global *tmp;
5960
5961 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5962 if (global->name != name)
5963 continue;
5964
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005965 if (strcmp(global->interface, "wl_output") == 0)
5966 display_destroy_output(d, name);
5967
5968 /* XXX: Should destroy remaining bound globals */
5969
5970 if (d->global_handler_remove)
5971 d->global_handler_remove(d, name, global->interface,
5972 global->version, d->user_data);
5973
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005974 wl_list_remove(&global->link);
5975 free(global->interface);
5976 free(global);
5977 }
5978}
5979
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005980void *
5981display_bind(struct display *display, uint32_t name,
5982 const struct wl_interface *interface, uint32_t version)
5983{
5984 return wl_registry_bind(display->registry, name, interface, version);
5985}
5986
5987static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005988 registry_handle_global,
5989 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005990};
5991
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005992#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005993static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005994init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005995{
5996 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005997 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005998
Rob Clark6396ed32012-03-11 19:48:41 -05005999#ifdef USE_CAIRO_GLESV2
6000# define GL_BIT EGL_OPENGL_ES2_BIT
6001#else
6002# define GL_BIT EGL_OPENGL_BIT
6003#endif
6004
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006005 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04006006 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006007 EGL_RED_SIZE, 1,
6008 EGL_GREEN_SIZE, 1,
6009 EGL_BLUE_SIZE, 1,
6010 EGL_ALPHA_SIZE, 1,
6011 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05006012 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006013 EGL_NONE
6014 };
Yuval Fledel45568f62010-12-06 09:18:12 -05006015
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006016#ifdef USE_CAIRO_GLESV2
6017 static const EGLint context_attribs[] = {
6018 EGL_CONTEXT_CLIENT_VERSION, 2,
6019 EGL_NONE
6020 };
6021 EGLint api = EGL_OPENGL_ES_API;
6022#else
6023 EGLint *context_attribs = NULL;
6024 EGLint api = EGL_OPENGL_API;
6025#endif
6026
Jonny Lamb51a7ae52015-03-20 15:26:51 +01006027 d->dpy =
6028 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
6029 d->display, NULL);
6030
Yuval Fledel45568f62010-12-06 09:18:12 -05006031 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006032 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006033 return -1;
6034 }
6035
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006036 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006037 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006038 return -1;
6039 }
6040
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006041 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
6042 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006043 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01006044 return -1;
6045 }
6046
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006047 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05006048 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02006049 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006050 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05006051 return -1;
6052 }
6053
Benjamin Franzke0c991632011-09-27 21:57:31 +02006054 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
6055 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006056 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02006057 return -1;
6058 }
Yuval Fledel45568f62010-12-06 09:18:12 -05006059
6060 return 0;
6061}
6062
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006063static void
6064fini_egl(struct display *display)
6065{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006066 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006067
6068 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
6069 EGL_NO_CONTEXT);
6070
6071 eglTerminate(display->dpy);
6072 eglReleaseThread();
6073}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006074#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006075
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006076static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006077init_dummy_surface(struct display *display)
6078{
6079 int len;
6080 void *data;
6081
6082 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06006083 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006084 display->dummy_surface =
6085 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
6086 1, 1, len);
6087 display->dummy_surface_data = data;
6088}
6089
6090static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006091handle_display_data(struct task *task, uint32_t events)
6092{
6093 struct display *display =
6094 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006095 struct epoll_event ep;
6096 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006097
6098 display->display_fd_events = events;
6099
6100 if (events & EPOLLERR || events & EPOLLHUP) {
6101 display_exit(display);
6102 return;
6103 }
6104
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04006105 if (events & EPOLLIN) {
6106 ret = wl_display_dispatch(display->display);
6107 if (ret == -1) {
6108 display_exit(display);
6109 return;
6110 }
6111 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006112
6113 if (events & EPOLLOUT) {
6114 ret = wl_display_flush(display->display);
6115 if (ret == 0) {
6116 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
6117 ep.data.ptr = &display->display_task;
6118 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6119 display->display_fd, &ep);
6120 } else if (ret == -1 && errno != EAGAIN) {
6121 display_exit(display);
6122 return;
6123 }
6124 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006125}
6126
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006127static void
6128log_handler(const char *format, va_list args)
6129{
6130 vfprintf(stderr, format, args);
6131}
6132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006133struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05006134display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006135{
6136 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04006137
Kristian Høgsberg2e437202013-04-16 11:21:48 -04006138 wl_log_set_handler_client(log_handler);
6139
Peter Huttererf3d62272013-08-08 11:57:05 +10006140 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006141 if (d == NULL)
6142 return NULL;
6143
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05006144 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006145 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02006146 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00006147 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006148 return NULL;
6149 }
6150
Rob Bradford5ab9c752013-07-26 16:29:43 +01006151 d->xkb_context = xkb_context_new(0);
6152 if (d->xkb_context == NULL) {
6153 fprintf(stderr, "Failed to create XKB context\n");
6154 free(d);
6155 return NULL;
6156 }
6157
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03006158 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006159 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006160 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07006161 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
6162 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006163
6164 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006165 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006166 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006167 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04006168
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006169 d->registry = wl_display_get_registry(d->display);
6170 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006171
Marek Chalupaa519d062014-12-05 13:49:40 +01006172 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02006173 fprintf(stderr, "Failed to process Wayland connection: %m\n");
6174 return NULL;
6175 }
6176
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006177#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02006178 if (init_egl(d) < 0)
6179 fprintf(stderr, "EGL does not seem to work, "
6180 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006181#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05006182
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006183 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04006184
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006185 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04006186
Kristian Høgsberg478d9262010-06-08 20:34:11 -04006187 wl_list_init(&d->window_list);
6188
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006189 init_dummy_surface(d);
6190
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006191 return d;
6192}
6193
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006194static void
6195display_destroy_outputs(struct display *display)
6196{
6197 struct output *tmp;
6198 struct output *output;
6199
6200 wl_list_for_each_safe(output, tmp, &display->output_list, link)
6201 output_destroy(output);
6202}
6203
Pekka Paalanene1207c72011-12-16 12:02:09 +02006204static void
6205display_destroy_inputs(struct display *display)
6206{
6207 struct input *tmp;
6208 struct input *input;
6209
6210 wl_list_for_each_safe(input, tmp, &display->input_list, link)
6211 input_destroy(input);
6212}
6213
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006214void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006215display_destroy(struct display *display)
6216{
Pekka Paalanenc2052982011-12-16 11:41:32 +02006217 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07006218 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
6219 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02006220
6221 if (!wl_list_empty(&display->deferred_list))
6222 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
6223
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02006224 cairo_surface_destroy(display->dummy_surface);
6225 free(display->dummy_surface_data);
6226
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006227 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02006228 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02006229
Daniel Stone97f68542012-05-30 16:32:01 +01006230 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006231
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04006232 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03006233 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02006234
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006235#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05006236 if (display->argb_device)
6237 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04006238#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006239
Pekka Paalanen35e82632013-04-25 13:57:48 +03006240 if (display->subcompositor)
6241 wl_subcompositor_destroy(display->subcompositor);
6242
Jasper St. Pierre0790e392013-12-09 14:58:00 -05006243 if (display->xdg_shell)
Jonas Ådahlaf7f98b2016-08-09 18:15:43 +08006244 zxdg_shell_v6_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006245
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09006246 if (display->ivi_application)
6247 ivi_application_destroy(display->ivi_application);
6248
Pekka Paalanenc2052982011-12-16 11:41:32 +02006249 if (display->shm)
6250 wl_shm_destroy(display->shm);
6251
6252 if (display->data_device_manager)
6253 wl_data_device_manager_destroy(display->data_device_manager);
6254
6255 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02006256 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02006257
6258 close(display->epoll_fd);
6259
U. Artie Eoff44874d92012-10-02 21:12:35 -07006260 if (!(display->display_fd_events & EPOLLERR) &&
6261 !(display->display_fd_events & EPOLLHUP))
6262 wl_display_flush(display->display);
6263
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05006264 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02006265 free(display);
6266}
6267
6268void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02006269display_set_user_data(struct display *display, void *data)
6270{
6271 display->user_data = data;
6272}
6273
6274void *
6275display_get_user_data(struct display *display)
6276{
6277 return display->user_data;
6278}
6279
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04006280struct wl_display *
6281display_get_display(struct display *display)
6282{
6283 return display->display;
6284}
6285
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07006286int
6287display_has_subcompositor(struct display *display)
6288{
6289 if (display->subcompositor)
6290 return 1;
6291
6292 wl_display_roundtrip(display->display);
6293
6294 return display->subcompositor != NULL;
6295}
6296
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04006297cairo_device_t *
6298display_get_cairo_device(struct display *display)
6299{
6300 return display->argb_device;
6301}
6302
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006303struct output *
6304display_get_output(struct display *display)
6305{
Armin Krezović7dda25b2016-06-23 11:59:31 +02006306 if (wl_list_empty(&display->output_list))
6307 return NULL;
6308
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05006309 return container_of(display->output_list.next, struct output, link);
6310}
6311
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05006312struct wl_compositor *
6313display_get_compositor(struct display *display)
6314{
6315 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05006316}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006317
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04006318uint32_t
6319display_get_serial(struct display *display)
6320{
6321 return display->serial;
6322}
6323
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006324EGLDisplay
6325display_get_egl_display(struct display *d)
6326{
6327 return d->dpy;
6328}
6329
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006330struct wl_data_source *
6331display_create_data_source(struct display *display)
6332{
Jason Ekstranda669bd52014-04-02 19:53:51 -05006333 if (display->data_device_manager)
6334 return wl_data_device_manager_create_data_source(display->data_device_manager);
6335 else
6336 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04006337}
6338
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006339EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02006340display_get_argb_egl_config(struct display *d)
6341{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05006342 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02006343}
6344
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006345int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006346display_acquire_window_surface(struct display *display,
6347 struct window *window,
6348 EGLContext ctx)
6349{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006350 struct surface *surface = window->main_surface;
6351
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006352 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02006353 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006354
Pekka Paalanen6f41b072013-02-20 13:39:17 +02006355 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006356 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006357}
6358
6359void
Benjamin Franzke0c991632011-09-27 21:57:31 +02006360display_release_window_surface(struct display *display,
6361 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006362{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006363 struct surface *surface = window->main_surface;
6364
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02006365 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02006366 return;
6367
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02006368 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01006369}
6370
6371void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006372display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006373{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006374 wl_list_insert(&display->deferred_list, &task->link);
6375}
6376
6377void
6378display_watch_fd(struct display *display,
6379 int fd, uint32_t events, struct task *task)
6380{
6381 struct epoll_event ep;
6382
6383 ep.events = events;
6384 ep.data.ptr = task;
6385 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
6386}
6387
6388void
Dima Ryazanova85292e2012-11-29 00:27:09 -08006389display_unwatch_fd(struct display *display, int fd)
6390{
6391 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
6392}
6393
6394void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006395display_run(struct display *display)
6396{
6397 struct task *task;
6398 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006399 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006400
Pekka Paalanen826d7952011-12-15 10:14:07 +02006401 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006402 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006403 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03006404 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006405 struct task, link);
6406 wl_list_remove(&task->link);
6407 task->run(task, 0);
6408 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006409
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04006410 wl_display_dispatch_pending(display->display);
6411
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006412 if (!display->running)
6413 break;
6414
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04006415 ret = wl_display_flush(display->display);
6416 if (ret < 0 && errno == EAGAIN) {
6417 ep[0].events =
6418 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
6419 ep[0].data.ptr = &display->display_task;
6420
6421 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
6422 display->display_fd, &ep[0]);
6423 } else if (ret < 0) {
6424 break;
6425 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05006426
6427 count = epoll_wait(display->epoll_fd,
6428 ep, ARRAY_LENGTH(ep), -1);
6429 for (i = 0; i < count; i++) {
6430 task = ep[i].data.ptr;
6431 task->run(task, ep[i].events);
6432 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04006433 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04006434}
Pekka Paalanen826d7952011-12-15 10:14:07 +02006435
6436void
6437display_exit(struct display *display)
6438{
6439 display->running = 0;
6440}
Jan Arne Petersencd997062012-11-18 19:06:44 +01006441
Carlos Garnacho9c931792016-01-18 23:52:12 +01006442int
6443display_get_data_device_manager_version(struct display *display)
6444{
6445 return display->data_device_manager_version;
6446}
6447
Jan Arne Petersencd997062012-11-18 19:06:44 +01006448void
6449keysym_modifiers_add(struct wl_array *modifiers_map,
6450 const char *name)
6451{
6452 size_t len = strlen(name) + 1;
6453 char *p;
6454
6455 p = wl_array_add(modifiers_map, len);
6456
6457 if (p == NULL)
6458 return;
6459
6460 strncpy(p, name, len);
6461}
6462
6463static xkb_mod_index_t
6464keysym_modifiers_get_index(struct wl_array *modifiers_map,
6465 const char *name)
6466{
6467 xkb_mod_index_t index = 0;
6468 char *p = modifiers_map->data;
6469
6470 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6471 if (strcmp(p, name) == 0)
6472 return index;
6473
6474 index++;
6475 p += strlen(p) + 1;
6476 }
6477
6478 return XKB_MOD_INVALID;
6479}
6480
6481xkb_mod_mask_t
6482keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6483 const char *name)
6484{
6485 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6486
6487 if (index == XKB_MOD_INVALID)
6488 return XKB_MOD_INVALID;
6489
6490 return 1 << index;
6491}