blob: 35261b6333c8d8320fc81169714e0e36f72c4c7a [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
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030030#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050032#include <fcntl.h>
33#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040034#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020036#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037#include <time.h>
38#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040039#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040040#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030041#include <sys/timerfd.h>
Derek Foreman493d9792015-03-04 16:26:25 -060042#include <stdbool.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040043
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030044#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050045#include <wayland-egl.h>
46
Rob Clark6396ed32012-03-11 19:48:41 -050047#ifdef USE_CAIRO_GLESV2
48#include <GLES2/gl2.h>
49#include <GLES2/gl2ext.h>
50#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050052#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040053#include <EGL/egl.h>
54#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040055
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040056#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030057#else /* HAVE_CAIRO_EGL */
58typedef void *EGLDisplay;
59typedef void *EGLConfig;
60typedef void *EGLContext;
61#define EGL_NO_DISPLAY ((EGLDisplay)0)
62#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050063
Daniel Stone9d4f0302012-02-15 16:33:21 +000064#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030065#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040066
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050067#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020068#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070069#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070070#include "shared/helpers.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080071#include "xdg-shell-unstable-v5-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060072#include "text-cursor-position-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070073#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050074
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050075#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050076
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090077#include <sys/types.h>
78#include "ivi-application-client-protocol.h"
79#define IVI_SURFACE_ID 9000
80
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070081struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030082
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040083struct global {
84 uint32_t name;
85 char *interface;
86 uint32_t version;
87 struct wl_list link;
88};
89
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050090struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050091 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040092 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050093 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030094 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040095 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040096 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060097 struct text_cursor_position *text_cursor_position;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050098 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090099 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400100 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500101 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200102 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200103 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400104 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400105
106 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700107 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400108 struct task display_task;
109
110 int epoll_fd;
111 struct wl_list deferred_list;
112
Pekka Paalanen826d7952011-12-15 10:14:07 +0200113 int running;
114
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400115 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400116 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400117 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500118 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400119
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400120 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400121
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300122 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300123 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400124
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200125 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400126 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800127 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200128
129 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100130
131 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200132
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200133 /* A hack to get text extents for tooltips */
134 cairo_surface_t *dummy_surface;
135 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200136
137 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530138 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500139};
140
Rob Bradford7507b572012-05-15 17:55:34 +0100141struct window_output {
142 struct output *output;
143 struct wl_list link;
144};
145
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200146struct toysurface {
147 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500148 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800149 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200150 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200151 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200152 * If flags has SURFACE_HINT_RESIZE set, the user is
153 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200154 * Returns the Cairo surface to draw to.
155 */
156 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200157 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200158 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200159
160 /*
161 * Post the surface to the server, returning the server allocation
162 * rectangle. The Cairo surface from prepare() must be destroyed
163 * after calling this.
164 */
165 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200166 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200167 struct rectangle *server_allocation);
168
169 /*
170 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800171 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200172 */
173 int (*acquire)(struct toysurface *base, EGLContext ctx);
174
175 /*
176 * Release the toysurface from the EGL context, returning control
177 * to Cairo.
178 */
179 void (*release)(struct toysurface *base);
180
181 /*
182 * Destroy the toysurface, including the Cairo surface, any
183 * backing storage, and the Wayland protocol objects.
184 */
185 void (*destroy)(struct toysurface *base);
186};
187
Pekka Paalanen4e373742013-02-13 16:17:13 +0200188struct surface {
189 struct window *window;
190
191 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300192 struct wl_subsurface *subsurface;
193 int synchronized;
194 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200195 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200196 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300197 int redraw_needed;
198 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300199 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200200
201 struct rectangle allocation;
202 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200203
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200204 struct wl_region *input_region;
205 struct wl_region *opaque_region;
206
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200207 enum window_buffer_type buffer_type;
208 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200209 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200210
211 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300212
213 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200214};
215
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500216struct window {
217 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100218 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500219 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200220 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400221 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500222 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200223 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500224 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400225 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300226 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400227 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400228 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500229 int custom;
230 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400231
Pekka Paalanen99436862012-11-19 17:15:59 +0200232 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400233
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500234 int fullscreen;
235 int maximized;
236
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200237 enum preferred_format preferred_format;
238
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500239 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500240 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400241 window_data_handler_t data_handler;
242 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500243 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400244 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200245 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700246 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400247
Pekka Paalanen4e373742013-02-13 16:17:13 +0200248 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500249 struct xdg_surface *xdg_surface;
250 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300251
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700252 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800253 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500254
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900255 struct ivi_surface *ivi_surface;
256
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500257 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500258
Pekka Paalanen35e82632013-04-25 13:57:48 +0300259 /* struct surface::link, contains also main_surface */
260 struct wl_list subsurface_list;
261
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500262 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400263 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500264};
265
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500266struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500267 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200268 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300269 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500270 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400271 struct wl_list link;
272 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500273 widget_resize_handler_t resize_handler;
274 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500275 widget_enter_handler_t enter_handler;
276 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500277 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500278 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700279 widget_touch_down_handler_t touch_down_handler;
280 widget_touch_up_handler_t touch_up_handler;
281 widget_touch_motion_handler_t touch_motion_handler;
282 widget_touch_frame_handler_t touch_frame_handler;
283 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200284 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000285 widget_pointer_frame_handler_t pointer_frame_handler;
286 widget_axis_source_handler_t axis_source_handler;
287 widget_axis_stop_handler_t axis_stop_handler;
288 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400289 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500290 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300291 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500292 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000293 /* If this is set to false then no cairo surface will be
294 * created before redrawing the surface. This is useful if the
295 * redraw handler is going to do completely custom rendering
296 * such as using EGL directly */
297 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400298};
299
Rusty Lynch041815a2013-08-08 21:20:38 -0700300struct touch_point {
301 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800302 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700303 struct widget *widget;
304 struct wl_list link;
305};
306
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400307struct input {
308 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100309 struct wl_seat *seat;
310 struct wl_pointer *pointer;
311 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700312 struct wl_touch *touch;
313 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct window *pointer_focus;
315 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700316 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300317 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300318 uint32_t cursor_anim_start;
319 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500320 uint32_t cursor_timer_start;
321 uint32_t cursor_anim_current;
322 int cursor_delay_fd;
323 bool cursor_timer_running;
324 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300325 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400326 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400327 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400328 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400329 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400330 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400331
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500332 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500333 struct widget *grab;
334 uint32_t grab_button;
335
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400336 struct wl_data_device *data_device;
337 struct data_offer *drag_offer;
338 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800339 uint32_t touch_grab;
340 int32_t touch_grab_id;
341 float drag_x, drag_y;
342 struct window *drag_focus;
343 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100344
345 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100346 struct xkb_keymap *keymap;
347 struct xkb_state *state;
348 xkb_mod_mask_t control_mask;
349 xkb_mod_mask_t alt_mask;
350 xkb_mod_mask_t shift_mask;
351 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400352
Jonny Lamb06959082014-08-12 14:58:27 +0200353 int32_t repeat_rate_sec;
354 int32_t repeat_rate_nsec;
355 int32_t repeat_delay_sec;
356 int32_t repeat_delay_nsec;
357
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400358 struct task repeat_task;
359 int repeat_timer_fd;
360 uint32_t repeat_sym;
361 uint32_t repeat_key;
362 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500363 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400364};
365
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500366struct output {
367 struct display *display;
368 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800369 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500370 struct rectangle allocation;
371 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600372 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200373 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500374 char *make;
375 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200376
377 display_output_handler_t destroy_handler;
378 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500379};
380
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500381struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500382 struct widget *widget;
383 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500384 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800385
386 uint32_t last_time;
387 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800388 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500389};
390
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500391struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400392 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500393 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500394 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500395 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700396 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500397 const char **entries;
398 uint32_t time;
399 int current;
400 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400401 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500402 menu_func_t func;
403};
404
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300405struct tooltip {
406 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300407 struct widget *widget;
408 char *entry;
409 struct task tooltip_task;
410 int tooltip_fd;
411 float x, y;
412};
413
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700414struct shm_pool {
415 struct wl_shm_pool *pool;
416 size_t size;
417 size_t used;
418 void *data;
419};
420
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400421enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300422 CURSOR_DEFAULT = 100,
423 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400424};
425
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200426static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400427
Pekka Paalanen97777442013-05-22 10:20:05 +0300428/* #define DEBUG */
429
430#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300431
432static void
433debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
434__attribute__ ((format (printf, 4, 5)));
435
436static void
437debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
438{
439 va_list ap;
440 struct timeval tv;
441
442 gettimeofday(&tv, NULL);
443 fprintf(stderr, "%8ld.%03ld ",
444 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
445
446 if (proxy)
447 fprintf(stderr, "%s@%d ",
448 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
449
450 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
451 fprintf(stderr, "%s ", func);
452
453 va_start(ap, fmt);
454 vfprintf(stderr, fmt, ap);
455 va_end(ap);
456}
457
458#define DBG(fmt, ...) \
459 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
460
461#define DBG_OBJ(obj, fmt, ...) \
462 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
463
464#else
465
466#define DBG(...) do {} while (0)
467#define DBG_OBJ(...) do {} while (0)
468
469#endif
470
Alexander Larsson1818e312013-05-22 14:41:31 +0200471static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200472surface_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 +0200473{
474 int32_t tmp;
475
476 switch (buffer_transform) {
477 case WL_OUTPUT_TRANSFORM_90:
478 case WL_OUTPUT_TRANSFORM_270:
479 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
481 tmp = *width;
482 *width = *height;
483 *height = tmp;
484 break;
485 default:
486 break;
487 }
488
489 *width *= buffer_scale;
490 *height *= buffer_scale;
491}
492
493static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200494buffer_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 +0200495{
496 int32_t tmp;
497
498 switch (buffer_transform) {
499 case WL_OUTPUT_TRANSFORM_90:
500 case WL_OUTPUT_TRANSFORM_270:
501 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
503 tmp = *width;
504 *width = *height;
505 *height = tmp;
506 break;
507 default:
508 break;
509 }
510
511 *width /= buffer_scale;
512 *height /= buffer_scale;
513}
514
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500515#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400516
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517struct egl_window_surface {
518 struct toysurface base;
519 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520 struct display *display;
521 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 struct wl_egl_window *egl_window;
523 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100524};
525
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526static struct egl_window_surface *
527to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100528{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100530}
531
532static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200533egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200534 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200535 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100536{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200537 struct egl_window_surface *surface = to_egl_window_surface(base);
538
Alexander Larsson1818e312013-05-22 14:41:31 +0200539 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
540
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200541 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
542 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
543
544 return cairo_surface_reference(surface->cairo_surface);
545}
546
547static void
548egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200549 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200550 struct rectangle *server_allocation)
551{
552 struct egl_window_surface *surface = to_egl_window_surface(base);
553
554 cairo_gl_surface_swapbuffers(surface->cairo_surface);
555 wl_egl_window_get_attached_size(surface->egl_window,
556 &server_allocation->width,
557 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200558
559 buffer_to_surface_size (buffer_transform, buffer_scale,
560 &server_allocation->width,
561 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200562}
563
564static int
565egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
566{
567 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200568 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100569
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200570 device = cairo_surface_get_device(surface->cairo_surface);
571 if (!device)
572 return -1;
573
574 if (!ctx) {
575 if (device == surface->display->argb_device)
576 ctx = surface->display->argb_ctx;
577 else
578 assert(0);
579 }
580
581 cairo_device_flush(device);
582 cairo_device_acquire(device);
583 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
584 surface->egl_surface, ctx))
585 fprintf(stderr, "failed to make surface current\n");
586
587 return 0;
588}
589
590static void
591egl_window_surface_release(struct toysurface *base)
592{
593 struct egl_window_surface *surface = to_egl_window_surface(base);
594 cairo_device_t *device;
595
596 device = cairo_surface_get_device(surface->cairo_surface);
597 if (!device)
598 return;
599
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200600 if (!eglMakeCurrent(surface->display->dpy,
601 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200602 fprintf(stderr, "failed to make context current\n");
603
604 cairo_device_release(device);
605}
606
607static void
608egl_window_surface_destroy(struct toysurface *base)
609{
610 struct egl_window_surface *surface = to_egl_window_surface(base);
611 struct display *d = surface->display;
612
613 cairo_surface_destroy(surface->cairo_surface);
614 eglDestroySurface(d->dpy, surface->egl_surface);
615 wl_egl_window_destroy(surface->egl_window);
616 surface->surface = NULL;
617
618 free(surface);
619}
620
621static struct toysurface *
622egl_window_surface_create(struct display *display,
623 struct wl_surface *wl_surface,
624 uint32_t flags,
625 struct rectangle *rectangle)
626{
627 struct egl_window_surface *surface;
628
Pekka Paalanenb3627362012-11-19 17:16:00 +0200629 if (display->dpy == EGL_NO_DISPLAY)
630 return NULL;
631
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200632 surface = calloc(1, sizeof *surface);
633 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634 return NULL;
635
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200636 surface->base.prepare = egl_window_surface_prepare;
637 surface->base.swap = egl_window_surface_swap;
638 surface->base.acquire = egl_window_surface_acquire;
639 surface->base.release = egl_window_surface_release;
640 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200642 surface->display = display;
643 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400644
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200645 surface->egl_window = wl_egl_window_create(surface->surface,
646 rectangle->width,
647 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100649 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100650 weston_platform_create_egl_surface(display->dpy,
651 display->argb_config,
652 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100653
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200654 surface->cairo_surface =
655 cairo_gl_surface_create_for_egl(display->argb_device,
656 surface->egl_surface,
657 rectangle->width,
658 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100659
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200660 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100661}
662
Pekka Paalanenb3627362012-11-19 17:16:00 +0200663#else
664
665static struct toysurface *
666egl_window_surface_create(struct display *display,
667 struct wl_surface *wl_surface,
668 uint32_t flags,
669 struct rectangle *rectangle)
670{
671 return NULL;
672}
673
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674#endif
675
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200676struct shm_surface_data {
677 struct wl_buffer *buffer;
678 struct shm_pool *pool;
679};
680
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681struct wl_buffer *
682display_get_buffer_for_surface(struct display *display,
683 cairo_surface_t *surface)
684{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200685 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400686
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200687 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400688
689 return data->buffer;
690}
691
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500692static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700693shm_pool_destroy(struct shm_pool *pool);
694
695static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400696shm_surface_data_destroy(void *p)
697{
698 struct shm_surface_data *data = p;
699
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200700 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700701 if (data->pool)
702 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300703
704 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400705}
706
Kristian Høgsberg16626282012-04-03 11:21:27 -0400707static struct wl_shm_pool *
708make_shm_pool(struct display *display, int size, void **data)
709{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400710 struct wl_shm_pool *pool;
711 int fd;
712
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300713 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400714 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300715 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
716 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400717 return NULL;
718 }
719
720 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400721 if (*data == MAP_FAILED) {
722 fprintf(stderr, "mmap failed: %m\n");
723 close(fd);
724 return NULL;
725 }
726
727 pool = wl_shm_create_pool(display->shm, fd, size);
728
729 close(fd);
730
731 return pool;
732}
733
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700734static struct shm_pool *
735shm_pool_create(struct display *display, size_t size)
736{
737 struct shm_pool *pool = malloc(sizeof *pool);
738
739 if (!pool)
740 return NULL;
741
742 pool->pool = make_shm_pool(display, size, &pool->data);
743 if (!pool->pool) {
744 free(pool);
745 return NULL;
746 }
747
748 pool->size = size;
749 pool->used = 0;
750
751 return pool;
752}
753
754static void *
755shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
756{
757 if (pool->used + size > pool->size)
758 return NULL;
759
760 *offset = pool->used;
761 pool->used += size;
762
763 return (char *) pool->data + *offset;
764}
765
766/* destroy the pool. this does not unmap the memory though */
767static void
768shm_pool_destroy(struct shm_pool *pool)
769{
770 munmap(pool->data, pool->size);
771 wl_shm_pool_destroy(pool->pool);
772 free(pool);
773}
774
775/* Start allocating from the beginning of the pool again */
776static void
777shm_pool_reset(struct shm_pool *pool)
778{
779 pool->used = 0;
780}
781
782static int
783data_length_for_shm_surface(struct rectangle *rect)
784{
785 int stride;
786
787 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
788 rect->width);
789 return stride * rect->height;
790}
791
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500792static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700793display_create_shm_surface_from_pool(struct display *display,
794 struct rectangle *rectangle,
795 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400796{
797 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400798 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400799 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200800 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700801 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400802 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400803
804 data = malloc(sizeof *data);
805 if (data == NULL)
806 return NULL;
807
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200808 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
809 cairo_format = CAIRO_FORMAT_RGB16_565;
810 else
811 cairo_format = CAIRO_FORMAT_ARGB32;
812
813 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700814 length = stride * rectangle->height;
815 data->pool = NULL;
816 map = shm_pool_allocate(pool, length, &offset);
817
818 if (!map) {
819 free(data);
820 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400821 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400822
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400823 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200824 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400825 rectangle->width,
826 rectangle->height,
827 stride);
828
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200829 cairo_surface_set_user_data(surface, &shm_surface_data_key,
830 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400831
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200832 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
833 format = WL_SHM_FORMAT_RGB565;
834 else {
835 if (flags & SURFACE_OPAQUE)
836 format = WL_SHM_FORMAT_XRGB8888;
837 else
838 format = WL_SHM_FORMAT_ARGB8888;
839 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400840
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200841 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
842 rectangle->width,
843 rectangle->height,
844 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400845
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700846 return surface;
847}
848
849static cairo_surface_t *
850display_create_shm_surface(struct display *display,
851 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200852 struct shm_pool *alternate_pool,
853 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700854{
855 struct shm_surface_data *data;
856 struct shm_pool *pool;
857 cairo_surface_t *surface;
858
Pekka Paalanen99436862012-11-19 17:15:59 +0200859 if (alternate_pool) {
860 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700861 surface = display_create_shm_surface_from_pool(display,
862 rectangle,
863 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200864 alternate_pool);
865 if (surface) {
866 data = cairo_surface_get_user_data(surface,
867 &shm_surface_data_key);
868 goto out;
869 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700870 }
871
872 pool = shm_pool_create(display,
873 data_length_for_shm_surface(rectangle));
874 if (!pool)
875 return NULL;
876
877 surface =
878 display_create_shm_surface_from_pool(display, rectangle,
879 flags, pool);
880
881 if (!surface) {
882 shm_pool_destroy(pool);
883 return NULL;
884 }
885
886 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200887 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700888 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889
Pekka Paalanen99436862012-11-19 17:15:59 +0200890out:
891 if (data_ret)
892 *data_ret = data;
893
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400894 return surface;
895}
896
nobled7b87cb02011-02-01 18:51:47 +0000897static int
898check_size(struct rectangle *rect)
899{
900 if (rect->width && rect->height)
901 return 0;
902
903 fprintf(stderr, "tried to create surface of "
904 "width: %d, height: %d\n", rect->width, rect->height);
905 return -1;
906}
907
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400908cairo_surface_t *
909display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100910 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400911 struct rectangle *rectangle,
912 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400913{
nobled7b87cb02011-02-01 18:51:47 +0000914 if (check_size(rectangle) < 0)
915 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200916
917 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200918 return display_create_shm_surface(display, rectangle, flags,
919 NULL, NULL);
920}
921
Pekka Paalanena4eda732012-11-19 17:16:02 +0200922struct shm_surface_leaf {
923 cairo_surface_t *cairo_surface;
924 /* 'data' is automatically destroyed, when 'cairo_surface' is */
925 struct shm_surface_data *data;
926
927 struct shm_pool *resize_pool;
928 int busy;
929};
930
931static void
932shm_surface_leaf_release(struct shm_surface_leaf *leaf)
933{
934 if (leaf->cairo_surface)
935 cairo_surface_destroy(leaf->cairo_surface);
936 /* leaf->data already destroyed via cairo private */
937
938 if (leaf->resize_pool)
939 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200940
941 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942}
943
Pekka Paalanenaef02542013-04-25 13:57:47 +0300944#define MAX_LEAVES 3
945
Pekka Paalanen99436862012-11-19 17:15:59 +0200946struct shm_surface {
947 struct toysurface base;
948 struct display *display;
949 struct wl_surface *surface;
950 uint32_t flags;
951 int dx, dy;
952
Pekka Paalanenaef02542013-04-25 13:57:47 +0300953 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200954 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200955};
956
957static struct shm_surface *
958to_shm_surface(struct toysurface *base)
959{
960 return container_of(base, struct shm_surface, base);
961}
962
Pekka Paalanena4eda732012-11-19 17:16:02 +0200963static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300964shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
965{
966#ifdef DEBUG
967 struct shm_surface_leaf *leaf;
968 char bufs[MAX_LEAVES + 1];
969 int i;
970
971 for (i = 0; i < MAX_LEAVES; i++) {
972 leaf = &surface->leaf[i];
973
974 if (leaf->busy)
975 bufs[i] = 'b';
976 else if (leaf->cairo_surface)
977 bufs[i] = 'a';
978 else
979 bufs[i] = ' ';
980 }
981
982 bufs[MAX_LEAVES] = '\0';
983 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
984#endif
985}
986
987static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200988shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
989{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200990 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300991 struct shm_surface_leaf *leaf;
992 int i;
993 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300994
995 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200996
Pekka Paalanenaef02542013-04-25 13:57:47 +0300997 for (i = 0; i < MAX_LEAVES; i++) {
998 leaf = &surface->leaf[i];
999 if (leaf->data && leaf->data->buffer == buffer) {
1000 leaf->busy = 0;
1001 break;
1002 }
1003 }
1004 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001005
Pekka Paalanenaef02542013-04-25 13:57:47 +03001006 /* Leave one free leaf with storage, release others */
1007 free_found = 0;
1008 for (i = 0; i < MAX_LEAVES; i++) {
1009 leaf = &surface->leaf[i];
1010
1011 if (!leaf->cairo_surface || leaf->busy)
1012 continue;
1013
1014 if (!free_found)
1015 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001016 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001017 shm_surface_leaf_release(leaf);
1018 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001019
Pekka Paalanen97777442013-05-22 10:20:05 +03001020 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001021}
1022
1023static const struct wl_buffer_listener shm_surface_buffer_listener = {
1024 shm_surface_buffer_release
1025};
1026
Pekka Paalanen99436862012-11-19 17:15:59 +02001027static cairo_surface_t *
1028shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001029 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001030 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001031{
Pekka Paalanenec076692012-11-30 13:37:27 +02001032 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001033 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001034 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001035 struct shm_surface_leaf *leaf = NULL;
1036 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001037
1038 surface->dx = dx;
1039 surface->dy = dy;
1040
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001041 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001042 for (i = 0; i < MAX_LEAVES; i++) {
1043 if (surface->leaf[i].busy)
1044 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001045
Pekka Paalanenaef02542013-04-25 13:57:47 +03001046 if (!leaf || surface->leaf[i].cairo_surface)
1047 leaf = &surface->leaf[i];
1048 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001049 DBG_OBJ(surface->surface, "pick leaf %d\n",
1050 (int)(leaf - &surface->leaf[0]));
1051
Pekka Paalanenaef02542013-04-25 13:57:47 +03001052 if (!leaf) {
1053 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001054 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001055 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001056 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001057 }
1058
Pekka Paalanena4eda732012-11-19 17:16:02 +02001059 if (!resize_hint && leaf->resize_pool) {
1060 cairo_surface_destroy(leaf->cairo_surface);
1061 leaf->cairo_surface = NULL;
1062 shm_pool_destroy(leaf->resize_pool);
1063 leaf->resize_pool = NULL;
1064 }
1065
Alexander Larsson1818e312013-05-22 14:41:31 +02001066 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1067
Pekka Paalanena4eda732012-11-19 17:16:02 +02001068 if (leaf->cairo_surface &&
1069 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1070 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001071 goto out;
1072
Pekka Paalanena4eda732012-11-19 17:16:02 +02001073 if (leaf->cairo_surface)
1074 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001075
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001076#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001077 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001078 /* Create a big pool to allocate from, while continuously
1079 * resizing. Mmapping a new pool in the server
1080 * is relatively expensive, so reusing a pool performs
1081 * better, but may temporarily reserve unneeded memory.
1082 */
1083 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 leaf->resize_pool = shm_pool_create(surface->display,
1085 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001086 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001087#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001088
Alexander Larsson1818e312013-05-22 14:41:31 +02001089 rect.width = width;
1090 rect.height = height;
1091
Pekka Paalanena4eda732012-11-19 17:16:02 +02001092 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001093 display_create_shm_surface(surface->display, &rect,
1094 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001095 leaf->resize_pool,
1096 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001097 if (!leaf->cairo_surface)
1098 return NULL;
1099
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001101 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001102
1103out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 surface->current = leaf;
1105
1106 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001107}
1108
1109static void
1110shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001111 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001112 struct rectangle *server_allocation)
1113{
1114 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001116
1117 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001118 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001119 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001121
Alexander Larsson1818e312013-05-22 14:41:31 +02001122 buffer_to_surface_size (buffer_transform, buffer_scale,
1123 &server_allocation->width,
1124 &server_allocation->height);
1125
Pekka Paalanena4eda732012-11-19 17:16:02 +02001126 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001127 surface->dx, surface->dy);
1128 wl_surface_damage(surface->surface, 0, 0,
1129 server_allocation->width, server_allocation->height);
1130 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001131
Pekka Paalanen71233882013-04-25 13:57:53 +03001132 DBG_OBJ(surface->surface, "leaf %d busy\n",
1133 (int)(leaf - &surface->leaf[0]));
1134
Pekka Paalanena4eda732012-11-19 17:16:02 +02001135 leaf->busy = 1;
1136 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001137}
1138
1139static int
1140shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1141{
1142 return -1;
1143}
1144
1145static void
1146shm_surface_release(struct toysurface *base)
1147{
1148}
1149
1150static void
1151shm_surface_destroy(struct toysurface *base)
1152{
1153 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001154 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001155
Pekka Paalanenaef02542013-04-25 13:57:47 +03001156 for (i = 0; i < MAX_LEAVES; i++)
1157 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001158
1159 free(surface);
1160}
1161
1162static struct toysurface *
1163shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1164 uint32_t flags, struct rectangle *rectangle)
1165{
1166 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001167 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001168
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001169 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001170 surface->base.prepare = shm_surface_prepare;
1171 surface->base.swap = shm_surface_swap;
1172 surface->base.acquire = shm_surface_acquire;
1173 surface->base.release = shm_surface_release;
1174 surface->base.destroy = shm_surface_destroy;
1175
1176 surface->display = display;
1177 surface->surface = wl_surface;
1178 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001179
1180 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001181}
1182
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001183/*
1184 * The following correspondences between file names and cursors was copied
1185 * from: https://bugs.kde.org/attachment.cgi?id=67313
1186 */
1187
1188static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001189 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001190 "sw-resize",
1191 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192};
1193
1194static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001195 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001196 "se-resize",
1197 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001198};
1199
1200static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001201 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001202 "s-resize",
1203 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001204};
1205
1206static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001207 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001208 "closedhand",
1209 "208530c400c041818281048008011002"
1210};
1211
1212static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001213 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001214 "default",
1215 "top_left_arrow",
1216 "left-arrow"
1217};
1218
1219static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001220 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001221 "w-resize",
1222 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001223};
1224
1225static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001227 "e-resize",
1228 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229};
1230
1231static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001233 "nw-resize",
1234 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235};
1236
1237static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001239 "ne-resize",
1240 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241};
1242
1243static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001244 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001245 "n-resize",
1246 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001247};
1248
1249static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001250 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001251 "ibeam",
1252 "text"
1253};
1254
1255static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001256 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257 "pointer",
1258 "pointing_hand",
1259 "e29285e634086352946a0e7090d73106"
1260};
1261
1262static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001263 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001264 "wait",
1265 "0426c94ea35c87780ff01dc239897213"
1266};
1267
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001268static const char *move_draggings[] = {
1269 "dnd-move"
1270};
1271
1272static const char *copy_draggings[] = {
1273 "dnd-copy"
1274};
1275
1276static const char *forbidden_draggings[] = {
1277 "dnd-none",
1278 "dnd-no-drop"
1279};
1280
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001281struct cursor_alternatives {
1282 const char **names;
1283 size_t count;
1284};
1285
1286static const struct cursor_alternatives cursors[] = {
1287 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1288 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1289 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1290 {grabbings, ARRAY_LENGTH(grabbings)},
1291 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1292 {left_sides, ARRAY_LENGTH(left_sides)},
1293 {right_sides, ARRAY_LENGTH(right_sides)},
1294 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1295 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1296 {top_sides, ARRAY_LENGTH(top_sides)},
1297 {xterms, ARRAY_LENGTH(xterms)},
1298 {hand1s, ARRAY_LENGTH(hand1s)},
1299 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001300 {move_draggings, ARRAY_LENGTH(move_draggings)},
1301 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1302 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001303};
1304
1305static void
1306create_cursors(struct display *display)
1307{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001308 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001309 struct weston_config *config;
1310 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001311 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001312 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001313 unsigned int i, j;
1314 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001315
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001316 config_file = weston_config_get_name_from_env();
1317 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001318 s = weston_config_get_section(config, "shell", NULL, NULL);
1319 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1320 weston_config_section_get_int(s, "cursor-size", &size, 32);
1321 weston_config_destroy(config);
1322
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001323 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001324 if (!display->cursor_theme) {
1325 fprintf(stderr, "could not load theme '%s'\n", theme);
1326 return;
1327 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001328 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001329 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001330 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001331
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001332 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001333 cursor = NULL;
1334 for (j = 0; !cursor && j < cursors[i].count; ++j)
1335 cursor = wl_cursor_theme_get_cursor(
1336 display->cursor_theme, cursors[i].names[j]);
1337
1338 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001339 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001340 cursors[i].names[0]);
1341
1342 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001343 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001344}
1345
1346static void
1347destroy_cursors(struct display *display)
1348{
1349 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001350 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001351}
1352
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001353struct wl_cursor_image *
1354display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001355{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001356 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001357
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001358 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001359}
1360
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001361static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001362surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001363{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001364 if (!surface->cairo_surface)
1365 return;
1366
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001367 if (surface->opaque_region) {
1368 wl_surface_set_opaque_region(surface->surface,
1369 surface->opaque_region);
1370 wl_region_destroy(surface->opaque_region);
1371 surface->opaque_region = NULL;
1372 }
1373
1374 if (surface->input_region) {
1375 wl_surface_set_input_region(surface->surface,
1376 surface->input_region);
1377 wl_region_destroy(surface->input_region);
1378 surface->input_region = NULL;
1379 }
1380
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001381 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001382 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001383 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001384
Pekka Paalanen89dee002013-02-13 16:17:20 +02001385 cairo_surface_destroy(surface->cairo_surface);
1386 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001387}
1388
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001389int
1390window_has_focus(struct window *window)
1391{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001392 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001393}
1394
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001395static void
1396window_close(struct window *window)
1397{
1398 if (window->close_handler)
1399 window->close_handler(window->user_data);
1400 else
1401 display_exit(window->display);
1402}
1403
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001404struct display *
1405window_get_display(struct window *window)
1406{
1407 return window->display;
1408}
1409
Pekka Paalanen89dee002013-02-13 16:17:20 +02001410static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001411handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1412 int32_t width, int32_t height)
1413{
1414 struct window *window = data;
1415
1416 window_schedule_resize(window, width, height);
1417}
1418
1419static const struct ivi_surface_listener ivi_surface_listener = {
1420 handle_ivi_surface_configure,
1421};
1422
1423static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001424surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001425{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001426 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001427 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001428
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001429 if (!surface->toysurface && display->dpy &&
1430 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001431 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001432 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001433 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001434 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001435 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001436 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001437
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001438 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001439 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001440 surface->surface,
1441 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001442
Pekka Paalanen89dee002013-02-13 16:17:20 +02001443 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001444 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001445 allocation.width, allocation.height, flags,
1446 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001447}
1448
1449static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001450window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001452 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001453 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001454
Pekka Paalanenec076692012-11-30 13:37:27 +02001455 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001457
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001458 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1459 flags |= SURFACE_HINT_RGB565;
1460
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001461 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001462}
1463
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001464int
1465window_get_buffer_transform(struct window *window)
1466{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001467 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001468}
1469
1470void
1471window_set_buffer_transform(struct window *window,
1472 enum wl_output_transform transform)
1473{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001474 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001475 wl_surface_set_buffer_transform(window->main_surface->surface,
1476 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001477}
1478
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001479void
1480window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001481 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001482{
1483 window->main_surface->buffer_scale = scale;
1484 wl_surface_set_buffer_scale(window->main_surface->surface,
1485 scale);
1486}
1487
1488uint32_t
1489window_get_buffer_scale(struct window *window)
1490{
1491 return window->main_surface->buffer_scale;
1492}
1493
Alexander Larssond68f5232013-05-22 14:41:33 +02001494uint32_t
1495window_get_output_scale(struct window *window)
1496{
1497 struct window_output *window_output;
1498 struct window_output *window_output_tmp;
1499 int scale = 1;
1500
1501 wl_list_for_each_safe(window_output, window_output_tmp,
1502 &window->window_output_list, link) {
1503 if (window_output->output->scale > scale)
1504 scale = window_output->output->scale;
1505 }
1506
1507 return scale;
1508}
1509
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001510static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001511
Pekka Paalanen4e373742013-02-13 16:17:13 +02001512static void
1513surface_destroy(struct surface *surface)
1514{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001515 if (surface->frame_cb)
1516 wl_callback_destroy(surface->frame_cb);
1517
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001518 if (surface->input_region)
1519 wl_region_destroy(surface->input_region);
1520
1521 if (surface->opaque_region)
1522 wl_region_destroy(surface->opaque_region);
1523
Pekka Paalanen35e82632013-04-25 13:57:48 +03001524 if (surface->subsurface)
1525 wl_subsurface_destroy(surface->subsurface);
1526
Pekka Paalanen4e373742013-02-13 16:17:13 +02001527 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001528
1529 if (surface->toysurface)
1530 surface->toysurface->destroy(surface->toysurface);
1531
Pekka Paalanen35e82632013-04-25 13:57:48 +03001532 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001533 free(surface);
1534}
1535
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001536void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001537window_destroy(struct window *window)
1538{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001539 struct display *display = window->display;
1540 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001541 struct window_output *window_output;
1542 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001543
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001544 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001545
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001546 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001547 if (input->touch_focus == window)
1548 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001549 if (input->pointer_focus == window)
1550 input->pointer_focus = NULL;
1551 if (input->keyboard_focus == window)
1552 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001553 if (input->focus_widget &&
1554 input->focus_widget->window == window)
1555 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001556 }
1557
Rob Bradford7507b572012-05-15 17:55:34 +01001558 wl_list_for_each_safe(window_output, window_output_tmp,
1559 &window->window_output_list, link) {
1560 free (window_output);
1561 }
1562
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001563 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001564 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001565
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001566 if (window->xdg_surface)
1567 xdg_surface_destroy(window->xdg_surface);
1568 if (window->xdg_popup)
1569 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001570
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001571 if (window->ivi_surface)
1572 ivi_surface_destroy(window->ivi_surface);
1573
Pekka Paalanen4e373742013-02-13 16:17:13 +02001574 surface_destroy(window->main_surface);
1575
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001576 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001577
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001578 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001579 free(window);
1580}
1581
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001582static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001583widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001584{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001585 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001586
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587 wl_list_for_each(child, &widget->child_list, link) {
1588 target = widget_find_widget(child, x, y);
1589 if (target)
1590 return target;
1591 }
1592
1593 if (widget->allocation.x <= x &&
1594 x < widget->allocation.x + widget->allocation.width &&
1595 widget->allocation.y <= y &&
1596 y < widget->allocation.y + widget->allocation.height) {
1597 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001598 }
1599
1600 return NULL;
1601}
1602
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001603static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001604window_find_widget(struct window *window, int32_t x, int32_t y)
1605{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001606 struct surface *surface;
1607 struct widget *widget;
1608
1609 wl_list_for_each(surface, &window->subsurface_list, link) {
1610 widget = widget_find_widget(surface->widget, x, y);
1611 if (widget)
1612 return widget;
1613 }
1614
1615 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001616}
1617
1618static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001619widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001620{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001621 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622
Peter Huttererf3d62272013-08-08 11:57:05 +10001623 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001624 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001625 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001626 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001627 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001628 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001629 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001630 widget->tooltip = NULL;
1631 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001632 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001633 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001634
1635 return widget;
1636}
1637
1638struct widget *
1639window_add_widget(struct window *window, void *data)
1640{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001641 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001642
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001643 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001644 wl_list_init(&widget->link);
1645 window->main_surface->widget = widget;
1646
1647 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001648}
1649
1650struct widget *
1651widget_add_widget(struct widget *parent, void *data)
1652{
1653 struct widget *widget;
1654
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001655 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001656 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001657
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001658 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001659}
1660
1661void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001662widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001664 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001665 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001666 struct input *input;
1667
Pekka Paalanen35e82632013-04-25 13:57:48 +03001668 /* Destroy the sub-surface along with the root widget */
1669 if (surface->widget == widget && surface->subsurface)
1670 surface_destroy(widget->surface);
1671
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001672 if (widget->tooltip)
1673 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001674
Pekka Paalanene156fb62012-01-19 13:51:38 +02001675 wl_list_for_each(input, &display->input_list, link) {
1676 if (input->focus_widget == widget)
1677 input->focus_widget = NULL;
1678 }
1679
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001680 wl_list_remove(&widget->link);
1681 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001682}
1683
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001684void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001685widget_set_default_cursor(struct widget *widget, int cursor)
1686{
1687 widget->default_cursor = cursor;
1688}
1689
1690void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001691widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001692{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001693 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694}
1695
1696void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001697widget_set_size(struct widget *widget, int32_t width, int32_t height)
1698{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001699 widget->allocation.width = width;
1700 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001701}
1702
1703void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001704widget_set_allocation(struct widget *widget,
1705 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001706{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001707 widget->allocation.x = x;
1708 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001709 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001710}
1711
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001712void
1713widget_set_transparent(struct widget *widget, int transparent)
1714{
1715 widget->opaque = !transparent;
1716}
1717
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001718void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001719widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001720{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001721 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001722}
1723
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001724static cairo_surface_t *
1725widget_get_cairo_surface(struct widget *widget)
1726{
1727 struct surface *surface = widget->surface;
1728 struct window *window = widget->window;
1729
Neil Roberts97b747c2013-12-19 16:17:12 +00001730 assert(widget->use_cairo);
1731
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001732 if (!surface->cairo_surface) {
1733 if (surface == window->main_surface)
1734 window_create_main_surface(window);
1735 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001736 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001737 }
1738
1739 return surface->cairo_surface;
1740}
1741
Alexander Larsson15901f02013-05-22 14:41:25 +02001742static void
1743widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1744{
1745 struct surface *surface = widget->surface;
1746 double angle;
1747 cairo_matrix_t m;
1748 enum wl_output_transform transform;
1749 int surface_width, surface_height;
1750 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001751 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001752
1753 surface_width = surface->allocation.width;
1754 surface_height = surface->allocation.height;
1755
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001756 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001757 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001758
Alexander Larsson15901f02013-05-22 14:41:25 +02001759 switch (transform) {
1760 case WL_OUTPUT_TRANSFORM_FLIPPED:
1761 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1764 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1765 break;
1766 default:
1767 cairo_matrix_init_identity(&m);
1768 break;
1769 }
1770
1771 switch (transform) {
1772 case WL_OUTPUT_TRANSFORM_NORMAL:
1773 default:
1774 angle = 0;
1775 translate_x = 0;
1776 translate_y = 0;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_FLIPPED:
1779 angle = 0;
1780 translate_x = surface_width;
1781 translate_y = 0;
1782 break;
1783 case WL_OUTPUT_TRANSFORM_90:
1784 angle = M_PI_2;
1785 translate_x = surface_height;
1786 translate_y = 0;
1787 break;
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1789 angle = M_PI_2;
1790 translate_x = surface_height;
1791 translate_y = surface_width;
1792 break;
1793 case WL_OUTPUT_TRANSFORM_180:
1794 angle = M_PI;
1795 translate_x = surface_width;
1796 translate_y = surface_height;
1797 break;
1798 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1799 angle = M_PI;
1800 translate_x = 0;
1801 translate_y = surface_height;
1802 break;
1803 case WL_OUTPUT_TRANSFORM_270:
1804 angle = M_PI + M_PI_2;
1805 translate_x = 0;
1806 translate_y = surface_width;
1807 break;
1808 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1809 angle = M_PI + M_PI_2;
1810 translate_x = 0;
1811 translate_y = 0;
1812 break;
1813 }
1814
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001815 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001816 cairo_translate(cr, translate_x, translate_y);
1817 cairo_rotate(cr, angle);
1818 cairo_transform(cr, &m);
1819}
1820
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001821cairo_t *
1822widget_cairo_create(struct widget *widget)
1823{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001824 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001825 cairo_surface_t *cairo_surface;
1826 cairo_t *cr;
1827
1828 cairo_surface = widget_get_cairo_surface(widget);
1829 cr = cairo_create(cairo_surface);
1830
Alexander Larsson15901f02013-05-22 14:41:25 +02001831 widget_cairo_update_transform(widget, cr);
1832
Pekka Paalanen35e82632013-04-25 13:57:48 +03001833 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1834
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001835 return cr;
1836}
1837
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001838struct wl_surface *
1839widget_get_wl_surface(struct widget *widget)
1840{
1841 return widget->surface->surface;
1842}
1843
Neil Roberts5e10a042013-09-09 00:40:17 +01001844struct wl_subsurface *
1845widget_get_wl_subsurface(struct widget *widget)
1846{
1847 return widget->surface->subsurface;
1848}
1849
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001850uint32_t
1851widget_get_last_time(struct widget *widget)
1852{
1853 return widget->surface->last_time;
1854}
1855
1856void
1857widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1858{
1859 struct wl_compositor *comp = widget->window->display->compositor;
1860 struct surface *surface = widget->surface;
1861
1862 if (!surface->input_region)
1863 surface->input_region = wl_compositor_create_region(comp);
1864
1865 if (rect) {
1866 wl_region_add(surface->input_region,
1867 rect->x, rect->y, rect->width, rect->height);
1868 }
1869}
1870
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001871void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001872widget_set_resize_handler(struct widget *widget,
1873 widget_resize_handler_t handler)
1874{
1875 widget->resize_handler = handler;
1876}
1877
1878void
1879widget_set_redraw_handler(struct widget *widget,
1880 widget_redraw_handler_t handler)
1881{
1882 widget->redraw_handler = handler;
1883}
1884
1885void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001886widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001887{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001888 widget->enter_handler = handler;
1889}
1890
1891void
1892widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1893{
1894 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001895}
1896
1897void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001898widget_set_motion_handler(struct widget *widget,
1899 widget_motion_handler_t handler)
1900{
1901 widget->motion_handler = handler;
1902}
1903
1904void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001905widget_set_button_handler(struct widget *widget,
1906 widget_button_handler_t handler)
1907{
1908 widget->button_handler = handler;
1909}
1910
1911void
Rusty Lynch041815a2013-08-08 21:20:38 -07001912widget_set_touch_up_handler(struct widget *widget,
1913 widget_touch_up_handler_t handler)
1914{
1915 widget->touch_up_handler = handler;
1916}
1917
1918void
1919widget_set_touch_down_handler(struct widget *widget,
1920 widget_touch_down_handler_t handler)
1921{
1922 widget->touch_down_handler = handler;
1923}
1924
1925void
1926widget_set_touch_motion_handler(struct widget *widget,
1927 widget_touch_motion_handler_t handler)
1928{
1929 widget->touch_motion_handler = handler;
1930}
1931
1932void
1933widget_set_touch_frame_handler(struct widget *widget,
1934 widget_touch_frame_handler_t handler)
1935{
1936 widget->touch_frame_handler = handler;
1937}
1938
1939void
1940widget_set_touch_cancel_handler(struct widget *widget,
1941 widget_touch_cancel_handler_t handler)
1942{
1943 widget->touch_cancel_handler = handler;
1944}
1945
1946void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001947widget_set_axis_handler(struct widget *widget,
1948 widget_axis_handler_t handler)
1949{
1950 widget->axis_handler = handler;
1951}
1952
Peter Hutterer87743e92016-01-18 16:38:22 +10001953void
1954widget_set_pointer_frame_handler(struct widget *widget,
1955 widget_pointer_frame_handler_t handler)
1956{
1957 widget->pointer_frame_handler = handler;
1958}
1959
1960void
1961widget_set_axis_handlers(struct widget *widget,
1962 widget_axis_handler_t axis_handler,
1963 widget_axis_source_handler_t axis_source_handler,
1964 widget_axis_stop_handler_t axis_stop_handler,
1965 widget_axis_discrete_handler_t axis_discrete_handler)
1966{
1967 widget->axis_handler = axis_handler;
1968 widget->axis_source_handler = axis_source_handler;
1969 widget->axis_stop_handler = axis_stop_handler;
1970 widget->axis_discrete_handler = axis_discrete_handler;
1971}
1972
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001973static void
1974window_schedule_redraw_task(struct window *window);
1975
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001976void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001977widget_schedule_redraw(struct widget *widget)
1978{
Pekka Paalanen71233882013-04-25 13:57:53 +03001979 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001980 widget->surface->redraw_needed = 1;
1981 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001982}
1983
Neil Roberts97b747c2013-12-19 16:17:12 +00001984void
1985widget_set_use_cairo(struct widget *widget,
1986 int use_cairo)
1987{
1988 widget->use_cairo = use_cairo;
1989}
1990
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001991cairo_surface_t *
1992window_get_surface(struct window *window)
1993{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001994 cairo_surface_t *cairo_surface;
1995
1996 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1997
1998 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001999}
2000
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002001struct wl_surface *
2002window_get_wl_surface(struct window *window)
2003{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002004 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002005}
2006
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002007static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002008tooltip_redraw_handler(struct widget *widget, void *data)
2009{
2010 cairo_t *cr;
2011 const int32_t r = 3;
2012 struct tooltip *tooltip = data;
2013 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002014
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002015 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002016 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002017 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2018 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2019 cairo_paint(cr);
2020
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002021 width = widget->allocation.width;
2022 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002023 rounded_rect(cr, 0, 0, width, height, r);
2024
2025 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2026 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2027 cairo_fill(cr);
2028
2029 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2030 cairo_move_to(cr, 10, 16);
2031 cairo_show_text(cr, tooltip->entry);
2032 cairo_destroy(cr);
2033}
2034
2035static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002036get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002037{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002038 cairo_t *cr;
2039 cairo_text_extents_t extents;
2040
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002041 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002042 * created yet, and parent does not have a valid surface
2043 * outside repaint, either.
2044 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002045 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002046 cairo_text_extents(cr, tooltip->entry, &extents);
2047 cairo_destroy(cr);
2048
2049 return extents;
2050}
2051
2052static int
2053window_create_tooltip(struct tooltip *tooltip)
2054{
2055 struct widget *parent = tooltip->parent;
2056 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002057 const int offset_y = 27;
2058 const int margin = 3;
2059 cairo_text_extents_t extents;
2060
2061 if (tooltip->widget)
2062 return 0;
2063
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002064 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002065
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002066 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002067 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002068 widget_set_allocation(tooltip->widget,
2069 tooltip->x, tooltip->y + offset_y,
2070 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002071
2072 return 0;
2073}
2074
2075void
2076widget_destroy_tooltip(struct widget *parent)
2077{
2078 struct tooltip *tooltip = parent->tooltip;
2079
2080 parent->tooltip_count = 0;
2081 if (!tooltip)
2082 return;
2083
2084 if (tooltip->widget) {
2085 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002086 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002087 }
2088
2089 close(tooltip->tooltip_fd);
2090 free(tooltip->entry);
2091 free(tooltip);
2092 parent->tooltip = NULL;
2093}
2094
2095static void
2096tooltip_func(struct task *task, uint32_t events)
2097{
2098 struct tooltip *tooltip =
2099 container_of(task, struct tooltip, tooltip_task);
2100 uint64_t exp;
2101
Martin Olsson8df662a2012-07-08 03:03:47 +02002102 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2103 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002104 window_create_tooltip(tooltip);
2105}
2106
2107#define TOOLTIP_TIMEOUT 500
2108static int
2109tooltip_timer_reset(struct tooltip *tooltip)
2110{
2111 struct itimerspec its;
2112
2113 its.it_interval.tv_sec = 0;
2114 its.it_interval.tv_nsec = 0;
2115 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2116 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2117 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2118 fprintf(stderr, "could not set timerfd\n: %m");
2119 return -1;
2120 }
2121
2122 return 0;
2123}
2124
2125int
2126widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2127{
2128 struct tooltip *tooltip = parent->tooltip;
2129
2130 parent->tooltip_count++;
2131 if (tooltip) {
2132 tooltip->x = x;
2133 tooltip->y = y;
2134 tooltip_timer_reset(tooltip);
2135 return 0;
2136 }
2137
2138 /* the handler might be triggered too fast via input device motion, so
2139 * we need this check here to make sure tooltip is fully initialized */
2140 if (parent->tooltip_count > 1)
2141 return 0;
2142
2143 tooltip = malloc(sizeof *tooltip);
2144 if (!tooltip)
2145 return -1;
2146
2147 parent->tooltip = tooltip;
2148 tooltip->parent = parent;
2149 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002150 tooltip->x = x;
2151 tooltip->y = y;
2152 tooltip->entry = strdup(entry);
2153 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2154 if (tooltip->tooltip_fd < 0) {
2155 fprintf(stderr, "could not create timerfd\n: %m");
2156 return -1;
2157 }
2158
2159 tooltip->tooltip_task.run = tooltip_func;
2160 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2161 EPOLLIN, &tooltip->tooltip_task);
2162 tooltip_timer_reset(tooltip);
2163
2164 return 0;
2165}
2166
2167static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002168frame_resize_handler(struct widget *widget,
2169 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002170{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002171 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002172 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002173 struct rectangle interior;
2174 struct rectangle input;
2175 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002176
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002177 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002178 interior.x = 0;
2179 interior.y = 0;
2180 interior.width = width;
2181 interior.height = height;
2182 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002183 frame_resize(frame->frame, width, height);
2184 frame_interior(frame->frame, &interior.x, &interior.y,
2185 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002186 }
2187
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002188 widget_set_allocation(child, interior.x, interior.y,
2189 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002190
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002191 if (child->resize_handler) {
2192 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002193 child->user_data);
2194
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002195 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002196 width = child->allocation.width;
2197 height = child->allocation.height;
2198 } else {
2199 frame_resize_inside(frame->frame,
2200 child->allocation.width,
2201 child->allocation.height);
2202 width = frame_width(frame->frame);
2203 height = frame_height(frame->frame);
2204 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002205 }
2206
Scott Moreauf7e498c2012-05-14 11:39:29 -06002207 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002208
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002209 widget->surface->input_region =
2210 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002211 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002212 frame_input_rect(frame->frame, &input.x, &input.y,
2213 &input.width, &input.height);
2214 wl_region_add(widget->surface->input_region,
2215 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002216 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002217 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002218 }
2219
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002220 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002221
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002222 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002223 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002224 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2225 &opaque.width, &opaque.height);
2226
2227 wl_region_add(widget->surface->opaque_region,
2228 opaque.x, opaque.y,
2229 opaque.width, opaque.height);
2230 } else {
2231 wl_region_add(widget->surface->opaque_region,
2232 0, 0, width, height);
2233 }
Martin Minarik1998b152012-05-10 02:04:35 +02002234 }
2235
Martin Minarik1998b152012-05-10 02:04:35 +02002236
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002237 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002238}
2239
2240static void
2241frame_redraw_handler(struct widget *widget, void *data)
2242{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002243 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002244 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002245 struct window *window = widget->window;
2246
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002247 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002248 return;
2249
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002250 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002251
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002252 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002253
2254 cairo_destroy(cr);
2255}
2256
2257static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002258frame_get_pointer_image_for_location(struct window_frame *frame,
2259 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002260{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002261 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002263 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002264 return CURSOR_LEFT_PTR;
2265
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002266 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002267 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002268 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002269 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002270 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002271 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002272 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002273 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002274 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002275 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002276 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002277 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002278 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002279 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002280 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002281 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002282 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002283 case THEME_LOCATION_EXTERIOR:
2284 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002285 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002286 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002287 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002288}
2289
Pekka Paalanen26237862014-09-10 15:10:30 +03002290static void
2291frame_menu_func(void *data, struct input *input, int index)
2292{
2293 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002294
2295 switch (index) {
2296 case 0: /* close */
2297 window_close(window);
2298 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002299 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002300 /* we don't have a way to get out of fullscreen for now */
2301 if (window->fullscreen_handler)
2302 window->fullscreen_handler(window, window->user_data);
2303 break;
2304 }
2305}
2306
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002307void
2308window_show_frame_menu(struct window *window,
2309 struct input *input, uint32_t time)
2310{
2311 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002312 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002313
Pekka Paalanen26237862014-09-10 15:10:30 +03002314 static const char *entries[] = {
2315 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002316 "Fullscreen"
2317 };
2318
2319 if (window->fullscreen_handler)
2320 count = ARRAY_LENGTH(entries);
2321 else
2322 count = ARRAY_LENGTH(entries) - 1;
2323
2324 input_get_position(input, &x, &y);
2325 window_show_menu(window->display, input, time, window,
2326 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002327}
2328
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002329static int
2330frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002331 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002332{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002333 struct window_frame *frame = data;
2334 enum theme_location location;
2335
2336 location = frame_pointer_enter(frame->frame, input, x, y);
2337 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2338 widget_schedule_redraw(frame->widget);
2339
2340 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002342
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002343static int
2344frame_motion_handler(struct widget *widget,
2345 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002346 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002347{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002348 struct window_frame *frame = data;
2349 enum theme_location location;
2350
2351 location = frame_pointer_motion(frame->frame, input, x, y);
2352 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2353 widget_schedule_redraw(frame->widget);
2354
2355 return frame_get_pointer_image_for_location(data, location);
2356}
2357
2358static void
2359frame_leave_handler(struct widget *widget,
2360 struct input *input, void *data)
2361{
2362 struct window_frame *frame = data;
2363
2364 frame_pointer_leave(frame->frame, input);
2365 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2366 widget_schedule_redraw(frame->widget);
2367}
2368
2369static void
2370frame_handle_status(struct window_frame *frame, struct input *input,
2371 uint32_t time, enum theme_location location)
2372{
2373 struct window *window = frame->widget->window;
2374 uint32_t status;
2375
2376 status = frame_status(frame->frame);
2377 if (status & FRAME_STATUS_REPAINT)
2378 widget_schedule_redraw(frame->widget);
2379
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002380 if (status & FRAME_STATUS_MINIMIZE) {
2381 window_set_minimized(window);
2382 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2383 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002384
2385 if (status & FRAME_STATUS_MENU) {
2386 window_show_frame_menu(window, input, time);
2387 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2388 }
2389
2390 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002391 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002392 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2393 }
2394
2395 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002396 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002397 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002398 }
2399
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002400 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002401 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002402 xdg_surface_move(window->xdg_surface,
2403 input_get_seat(input),
2404 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002405
2406 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2407 }
2408
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002409 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002410 input_ungrab(input);
2411
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002412 xdg_surface_resize(window->xdg_surface,
2413 input_get_seat(input),
2414 window->display->serial,
2415 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002416
2417 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2418 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002419}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002420
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002421#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002422static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002423frame_button_handler(struct widget *widget,
2424 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002425 uint32_t button, enum wl_pointer_button_state state,
2426 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002427
2428{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002429 struct window_frame *frame = data;
2430 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002431
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002432 frame->double_click = 0;
2433 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2434 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2435 frame->double_click = 1;
2436 frame->did_double = 1;
2437 } else
2438 frame->did_double = 0;
2439
2440 frame->last_time = time;
2441 } else if (frame->did_double == 1) {
2442 frame->double_click = 1;
2443 frame->did_double = 0;
2444 }
2445
2446 if (frame->double_click)
2447 location = frame_double_click(frame->frame, input,
2448 button, state);
2449 else
2450 location = frame_pointer_button(frame->frame, input,
2451 button, state);
2452
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002453 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002454}
2455
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002456static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002457frame_touch_down_handler(struct widget *widget, struct input *input,
2458 uint32_t serial, uint32_t time, int32_t id,
2459 float x, float y, void *data)
2460{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002461 struct window_frame *frame = data;
2462
Xiong Zhang382de462014-06-12 11:06:26 +08002463 frame->double_click = 0;
2464 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2465 frame->last_id == id) {
2466 frame->double_click = 1;
2467 frame->did_double = 1;
2468 frame->double_id = id;
2469 } else
2470 frame->did_double = 0;
2471
2472 frame->last_time = time;
2473 frame->last_id = id;
2474
2475 if (frame->double_click)
2476 frame_double_touch_down(frame->frame, input, id, x, y);
2477 else
2478 frame_touch_down(frame->frame, input, id, x, y);
2479
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002480 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2481}
2482
2483static void
2484frame_touch_up_handler(struct widget *widget,
2485 struct input *input, uint32_t serial, uint32_t time,
2486 int32_t id, void *data)
2487{
2488 struct window_frame *frame = data;
2489
Xiong Zhang382de462014-06-12 11:06:26 +08002490 if (frame->double_id == id && frame->did_double) {
2491 frame->did_double = 0;
2492 frame->double_id = 0;
2493 frame_double_touch_up(frame->frame, input, id);
2494 } else
2495 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002496 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002497}
2498
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002499struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002500window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002501{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002502 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002503 uint32_t buttons;
2504
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002505 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002506 buttons = FRAME_BUTTON_NONE;
2507 } else {
2508 buttons = FRAME_BUTTON_ALL;
2509 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002510
Peter Huttererf3d62272013-08-08 11:57:05 +10002511 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002512 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002513 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002514
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002515 frame->widget = window_add_widget(window, frame);
2516 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002517
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002518 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2519 widget_set_resize_handler(frame->widget, frame_resize_handler);
2520 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002521 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002522 widget_set_motion_handler(frame->widget, frame_motion_handler);
2523 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002524 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002525 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002526
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002527 window->frame = frame;
2528
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002529 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002530}
2531
Kristian Høgsberga1627922012-06-20 17:30:03 -04002532void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002533window_frame_set_child_size(struct widget *widget, int child_width,
2534 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002535{
2536 struct display *display = widget->window->display;
2537 struct theme *t = display->theme;
2538 int decoration_width, decoration_height;
2539 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002540 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002541
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002542 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002543 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002544 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002545 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002546
2547 width = child_width + decoration_width;
2548 height = child_height + decoration_height;
2549 } else {
2550 width = child_width;
2551 height = child_height;
2552 }
2553
2554 window_schedule_resize(widget->window, width, height);
2555}
2556
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002557static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002558window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002559{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002560 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002561
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002562 /* frame->child must be destroyed by the application */
2563 widget_destroy(frame->widget);
2564 free(frame);
2565}
2566
2567static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002568input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002569 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002570{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002571 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002572 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002573
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002574 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002575 return;
2576
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002577 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002578 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002579 widget = old;
2580 if (input->grab)
2581 widget = input->grab;
2582 if (widget->leave_handler)
2583 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002584 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002585 }
2586
2587 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002588 widget = focus;
2589 if (input->grab)
2590 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002591 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002592 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002593 cursor = widget->enter_handler(focus, input, x, y,
2594 widget->user_data);
2595 else
2596 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002597
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002598 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002599 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002600}
2601
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002602void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002603touch_grab(struct input *input, int32_t touch_id)
2604{
2605 input->touch_grab = 1;
2606 input->touch_grab_id = touch_id;
2607}
2608
2609void
2610touch_ungrab(struct input *input)
2611{
2612 struct touch_point *tp, *tmp;
2613
2614 input->touch_grab = 0;
2615
2616 wl_list_for_each_safe(tp, tmp,
2617 &input->touch_point_list, link) {
2618 if (tp->id != input->touch_grab_id)
2619 continue;
2620 wl_list_remove(&tp->link);
2621 free(tp);
2622
2623 return;
2624 }
2625}
2626
2627void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002628input_grab(struct input *input, struct widget *widget, uint32_t button)
2629{
2630 input->grab = widget;
2631 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002632
2633 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002634}
2635
2636void
2637input_ungrab(struct input *input)
2638{
2639 struct widget *widget;
2640
2641 input->grab = NULL;
2642 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002643 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002644 input->sx, input->sy);
2645 input_set_focus_widget(input, widget, input->sx, input->sy);
2646 }
2647}
2648
2649static void
Derek Foreman118a4292015-04-22 17:23:35 -05002650cursor_delay_timer_reset(struct input *input, uint32_t duration)
2651{
2652 struct itimerspec its;
2653
2654 if (!duration)
2655 input->cursor_timer_running = false;
2656 else
2657 input->cursor_timer_running = true;
2658
2659 its.it_interval.tv_sec = 0;
2660 its.it_interval.tv_nsec = 0;
2661 its.it_value.tv_sec = duration / 1000;
2662 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2663 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2664 fprintf(stderr, "could not set cursor timerfd\n: %m");
2665}
2666
2667static void cancel_pointer_image_update(struct input *input)
2668{
2669 if (input->cursor_timer_running)
2670 cursor_delay_timer_reset(input, 0);
2671}
2672
2673static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002674input_remove_pointer_focus(struct input *input)
2675{
2676 struct window *window = input->pointer_focus;
2677
2678 if (!window)
2679 return;
2680
2681 input_set_focus_widget(input, NULL, 0, 0);
2682
2683 input->pointer_focus = NULL;
2684 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002685 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002686}
2687
2688static void
2689pointer_handle_enter(void *data, struct wl_pointer *pointer,
2690 uint32_t serial, struct wl_surface *surface,
2691 wl_fixed_t sx_w, wl_fixed_t sy_w)
2692{
2693 struct input *input = data;
2694 struct window *window;
2695 struct widget *widget;
2696 float sx = wl_fixed_to_double(sx_w);
2697 float sy = wl_fixed_to_double(sy_w);
2698
2699 if (!surface) {
2700 /* enter event for a window we've just destroyed */
2701 return;
2702 }
2703
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002704 window = wl_surface_get_user_data(surface);
2705 if (surface != window->main_surface->surface) {
2706 DBG("Ignoring input event from subsurface %p\n", surface);
2707 return;
2708 }
2709
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002710 input->display->serial = serial;
2711 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002712 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002713
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002714 input->sx = sx;
2715 input->sy = sy;
2716
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002717 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002718 input_set_focus_widget(input, widget, sx, sy);
2719}
2720
2721static void
2722pointer_handle_leave(void *data, struct wl_pointer *pointer,
2723 uint32_t serial, struct wl_surface *surface)
2724{
2725 struct input *input = data;
2726
2727 input->display->serial = serial;
2728 input_remove_pointer_focus(input);
2729}
2730
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002731static void
Daniel Stone37816df2012-05-16 18:45:18 +01002732pointer_handle_motion(void *data, struct wl_pointer *pointer,
2733 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002734{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002735 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002736 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002737 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002738 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002739 float sx = wl_fixed_to_double(sx_w);
2740 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002741
Paul Winwoodb22bf572013-08-29 10:52:54 +01002742 if (!window)
2743 return;
2744
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002745 input->sx = sx;
2746 input->sy = sy;
2747
Rob Bradford5f087742013-07-11 19:41:27 +01002748 /* when making the window smaller - e.g. after a unmaximise we might
2749 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002750 * based on the old surface dimensions. However, if we have an active
2751 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002752 */
Derek Foreman46812b62015-08-26 17:13:27 -05002753 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002754 sy < window->main_surface->allocation.y ||
2755 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002756 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002757 return;
2758
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002759 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002760 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002761 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002762 }
2763
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002764 if (input->grab)
2765 widget = input->grab;
2766 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002767 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002768 if (widget) {
2769 if (widget->motion_handler)
2770 cursor = widget->motion_handler(input->focus_widget,
2771 input, time, sx, sy,
2772 widget->user_data);
2773 else
2774 cursor = widget->default_cursor;
2775 } else
2776 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002777
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002778 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002779}
2780
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002781static void
Daniel Stone37816df2012-05-16 18:45:18 +01002782pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002783 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002784{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002785 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002786 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002787 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002788
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002789 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002790 if (input->focus_widget && input->grab == NULL &&
2791 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002792 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002793
Neil Roberts6b28aad2012-01-23 19:11:18 +00002794 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002795 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002796 (*widget->button_handler)(widget,
2797 input, time,
2798 button, state,
2799 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002800
Daniel Stone4dbadb12012-05-30 16:31:51 +01002801 if (input->grab && input->grab_button == button &&
2802 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002803 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002804}
2805
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002806static void
Daniel Stone37816df2012-05-16 18:45:18 +01002807pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002808 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002809{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002810 struct input *input = data;
2811 struct widget *widget;
2812
2813 widget = input->focus_widget;
2814 if (input->grab)
2815 widget = input->grab;
2816 if (widget && widget->axis_handler)
2817 (*widget->axis_handler)(widget,
2818 input, time,
2819 axis, value,
2820 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002821}
2822
Peter Hutterer87743e92016-01-18 16:38:22 +10002823static void
2824pointer_handle_frame(void *data, struct wl_pointer *pointer)
2825{
2826 struct input *input = data;
2827 struct widget *widget;
2828
2829 widget = input->focus_widget;
2830 if (input->grab)
2831 widget = input->grab;
2832 if (widget && widget->pointer_frame_handler)
2833 (*widget->pointer_frame_handler)(widget,
2834 input,
2835 widget->user_data);
2836}
2837
2838static void
2839pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2840 uint32_t source)
2841{
2842 struct input *input = data;
2843 struct widget *widget;
2844
2845 widget = input->focus_widget;
2846 if (input->grab)
2847 widget = input->grab;
2848 if (widget && widget->axis_source_handler)
2849 (*widget->axis_source_handler)(widget,
2850 input,
2851 source,
2852 widget->user_data);
2853}
2854
2855static void
2856pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2857 uint32_t time, uint32_t axis)
2858{
2859 struct input *input = data;
2860 struct widget *widget;
2861
2862 widget = input->focus_widget;
2863 if (input->grab)
2864 widget = input->grab;
2865 if (widget && widget->axis_stop_handler)
2866 (*widget->axis_stop_handler)(widget,
2867 input, time,
2868 axis,
2869 widget->user_data);
2870}
2871
2872static void
2873pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2874 uint32_t axis, int32_t discrete)
2875{
2876 struct input *input = data;
2877 struct widget *widget;
2878
2879 widget = input->focus_widget;
2880 if (input->grab)
2881 widget = input->grab;
2882 if (widget && widget->axis_discrete_handler)
2883 (*widget->axis_discrete_handler)(widget,
2884 input,
2885 axis,
2886 discrete,
2887 widget->user_data);
2888}
2889
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002890static const struct wl_pointer_listener pointer_listener = {
2891 pointer_handle_enter,
2892 pointer_handle_leave,
2893 pointer_handle_motion,
2894 pointer_handle_button,
2895 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002896 pointer_handle_frame,
2897 pointer_handle_axis_source,
2898 pointer_handle_axis_stop,
2899 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002900};
2901
2902static void
2903input_remove_keyboard_focus(struct input *input)
2904{
2905 struct window *window = input->keyboard_focus;
2906 struct itimerspec its;
2907
2908 its.it_interval.tv_sec = 0;
2909 its.it_interval.tv_nsec = 0;
2910 its.it_value.tv_sec = 0;
2911 its.it_value.tv_nsec = 0;
2912 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2913
2914 if (!window)
2915 return;
2916
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002917 if (window->keyboard_focus_handler)
2918 (*window->keyboard_focus_handler)(window, NULL,
2919 window->user_data);
2920
2921 input->keyboard_focus = NULL;
2922}
2923
2924static void
2925keyboard_repeat_func(struct task *task, uint32_t events)
2926{
2927 struct input *input =
2928 container_of(task, struct input, repeat_task);
2929 struct window *window = input->keyboard_focus;
2930 uint64_t exp;
2931
2932 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2933 /* If we change the timer between the fd becoming
2934 * readable and getting here, there'll be nothing to
2935 * read and we get EAGAIN. */
2936 return;
2937
2938 if (window && window->key_handler) {
2939 (*window->key_handler)(window, input, input->repeat_time,
2940 input->repeat_key, input->repeat_sym,
2941 WL_KEYBOARD_KEY_STATE_PRESSED,
2942 window->user_data);
2943 }
2944}
2945
2946static void
2947keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2948 uint32_t format, int fd, uint32_t size)
2949{
2950 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002951 struct xkb_keymap *keymap;
2952 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002953 char *map_str;
2954
2955 if (!data) {
2956 close(fd);
2957 return;
2958 }
2959
2960 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2961 close(fd);
2962 return;
2963 }
2964
2965 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2966 if (map_str == MAP_FAILED) {
2967 close(fd);
2968 return;
2969 }
2970
Ran Benita2e1968f2014-08-19 23:59:51 +03002971 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2972 map_str,
2973 XKB_KEYMAP_FORMAT_TEXT_V1,
2974 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002975 munmap(map_str, size);
2976 close(fd);
2977
Rui Matos3eccb862013-10-10 19:44:22 +02002978 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002979 fprintf(stderr, "failed to compile keymap\n");
2980 return;
2981 }
2982
Rui Matos3eccb862013-10-10 19:44:22 +02002983 state = xkb_state_new(keymap);
2984 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002985 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002986 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002987 return;
2988 }
2989
Rui Matos3eccb862013-10-10 19:44:22 +02002990 xkb_keymap_unref(input->xkb.keymap);
2991 xkb_state_unref(input->xkb.state);
2992 input->xkb.keymap = keymap;
2993 input->xkb.state = state;
2994
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002995 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002996 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002997 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002998 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002999 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003000 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003001}
3002
3003static void
3004keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3005 uint32_t serial, struct wl_surface *surface,
3006 struct wl_array *keys)
3007{
3008 struct input *input = data;
3009 struct window *window;
3010
3011 input->display->serial = serial;
3012 input->keyboard_focus = wl_surface_get_user_data(surface);
3013
3014 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003015 if (window->keyboard_focus_handler)
3016 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003017 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003018}
3019
3020static void
3021keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3022 uint32_t serial, struct wl_surface *surface)
3023{
3024 struct input *input = data;
3025
3026 input->display->serial = serial;
3027 input_remove_keyboard_focus(input);
3028}
3029
Scott Moreau210d0792012-03-22 10:47:01 -06003030static void
Daniel Stone37816df2012-05-16 18:45:18 +01003031keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003032 uint32_t serial, uint32_t time, uint32_t key,
3033 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003034{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003035 struct input *input = data;
3036 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003037 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003038 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003039 const xkb_keysym_t *syms;
3040 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003041 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003042
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003043 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003044 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003045 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003046 return;
3047
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003048 /* We only use input grabs for pointer events for now, so just
3049 * ignore key presses if a grab is active. We expand the key
3050 * event delivery mechanism to route events to widgets to
3051 * properly handle key grabs. In the meantime, this prevents
3052 * key event devlivery while a grab is active. */
3053 if (input->grab && input->grab_button == 0)
3054 return;
3055
Ran Benita2e1968f2014-08-19 23:59:51 +03003056 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003057
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003058 sym = XKB_KEY_NoSymbol;
3059 if (num_syms == 1)
3060 sym = syms[0];
3061
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003062
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003063 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003064 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003065 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003066 } else if (sym == XKB_KEY_F11 &&
3067 window->fullscreen_handler &&
3068 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3069 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003070 } else if (sym == XKB_KEY_F4 &&
3071 input->modifiers == MOD_ALT_MASK &&
3072 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003073 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003074 } else if (window->key_handler) {
3075 (*window->key_handler)(window, input, time, key,
3076 sym, state, window->user_data);
3077 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003078
3079 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3080 key == input->repeat_key) {
3081 its.it_interval.tv_sec = 0;
3082 its.it_interval.tv_nsec = 0;
3083 its.it_value.tv_sec = 0;
3084 its.it_value.tv_nsec = 0;
3085 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003086 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3087 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003088 input->repeat_sym = sym;
3089 input->repeat_key = key;
3090 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003091 its.it_interval.tv_sec = input->repeat_rate_sec;
3092 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3093 its.it_value.tv_sec = input->repeat_delay_sec;
3094 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003095 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3096 }
3097}
3098
3099static void
Daniel Stone351eb612012-05-31 15:27:47 -04003100keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3101 uint32_t serial, uint32_t mods_depressed,
3102 uint32_t mods_latched, uint32_t mods_locked,
3103 uint32_t group)
3104{
3105 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003106 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003107
Matt Ropere61561f2013-06-24 16:52:43 +01003108 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3109 if (!input->xkb.keymap)
3110 return;
3111
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003112 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3113 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003114 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003115 XKB_STATE_MODS_DEPRESSED |
3116 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003117 input->modifiers = 0;
3118 if (mask & input->xkb.control_mask)
3119 input->modifiers |= MOD_CONTROL_MASK;
3120 if (mask & input->xkb.alt_mask)
3121 input->modifiers |= MOD_ALT_MASK;
3122 if (mask & input->xkb.shift_mask)
3123 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003124}
3125
Jonny Lamb06959082014-08-12 14:58:27 +02003126static void
3127set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3128{
3129 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3130 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3131
3132 /* a rate of zero disables any repeating, regardless of the delay's
3133 * value */
3134 if (rate == 0)
3135 return;
3136
3137 if (rate == 1)
3138 input->repeat_rate_sec = 1;
3139 else
3140 input->repeat_rate_nsec = 1000000000 / rate;
3141
3142 input->repeat_delay_sec = delay / 1000;
3143 delay -= (input->repeat_delay_sec * 1000);
3144 input->repeat_delay_nsec = delay * 1000 * 1000;
3145}
3146
3147static void
3148keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3149 int32_t rate, int32_t delay)
3150{
3151 struct input *input = data;
3152
3153 set_repeat_info(input, rate, delay);
3154}
3155
Daniel Stone37816df2012-05-16 18:45:18 +01003156static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003157 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003158 keyboard_handle_enter,
3159 keyboard_handle_leave,
3160 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003161 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003162 keyboard_handle_repeat_info
3163
Daniel Stone37816df2012-05-16 18:45:18 +01003164};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003165
3166static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003167touch_handle_down(void *data, struct wl_touch *wl_touch,
3168 uint32_t serial, uint32_t time, struct wl_surface *surface,
3169 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3170{
3171 struct input *input = data;
3172 struct widget *widget;
3173 float sx = wl_fixed_to_double(x_w);
3174 float sy = wl_fixed_to_double(y_w);
3175
Rusty Lynch1084da52013-08-15 09:10:08 -07003176 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003177 input->touch_focus = wl_surface_get_user_data(surface);
3178 if (!input->touch_focus) {
3179 DBG("Failed to find to touch focus for surface %p\n", surface);
3180 return;
3181 }
3182
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003183 if (surface != input->touch_focus->main_surface->surface) {
3184 DBG("Ignoring input event from subsurface %p\n", surface);
3185 input->touch_focus = NULL;
3186 return;
3187 }
3188
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003189 if (input->grab)
3190 widget = input->grab;
3191 else
3192 widget = window_find_widget(input->touch_focus,
3193 wl_fixed_to_double(x_w),
3194 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003195 if (widget) {
3196 struct touch_point *tp = xmalloc(sizeof *tp);
3197 if (tp) {
3198 tp->id = id;
3199 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003200 tp->x = sx;
3201 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003202 wl_list_insert(&input->touch_point_list, &tp->link);
3203
3204 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003205 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003206 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003207 sx, sy,
3208 widget->user_data);
3209 }
3210 }
3211}
3212
3213static void
3214touch_handle_up(void *data, struct wl_touch *wl_touch,
3215 uint32_t serial, uint32_t time, int32_t id)
3216{
3217 struct input *input = data;
3218 struct touch_point *tp, *tmp;
3219
Rusty Lynch041815a2013-08-08 21:20:38 -07003220 if (!input->touch_focus) {
3221 DBG("No touch focus found for touch up event!\n");
3222 return;
3223 }
3224
3225 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3226 if (tp->id != id)
3227 continue;
3228
3229 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003230 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003231 time, id,
3232 tp->widget->user_data);
3233
3234 wl_list_remove(&tp->link);
3235 free(tp);
3236
3237 return;
3238 }
3239}
3240
3241static void
3242touch_handle_motion(void *data, struct wl_touch *wl_touch,
3243 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3244{
3245 struct input *input = data;
3246 struct touch_point *tp;
3247 float sx = wl_fixed_to_double(x_w);
3248 float sy = wl_fixed_to_double(y_w);
3249
3250 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3251
3252 if (!input->touch_focus) {
3253 DBG("No touch focus found for touch motion event!\n");
3254 return;
3255 }
3256
3257 wl_list_for_each(tp, &input->touch_point_list, link) {
3258 if (tp->id != id)
3259 continue;
3260
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003261 tp->x = sx;
3262 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003263 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003264 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003265 id, sx, sy,
3266 tp->widget->user_data);
3267 return;
3268 }
3269}
3270
3271static void
3272touch_handle_frame(void *data, struct wl_touch *wl_touch)
3273{
3274 struct input *input = data;
3275 struct touch_point *tp, *tmp;
3276
3277 DBG("touch_handle_frame\n");
3278
3279 if (!input->touch_focus) {
3280 DBG("No touch focus found for touch frame event!\n");
3281 return;
3282 }
3283
3284 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3285 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003286 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003287 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003288 }
3289}
3290
3291static void
3292touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3293{
3294 struct input *input = data;
3295 struct touch_point *tp, *tmp;
3296
3297 DBG("touch_handle_cancel\n");
3298
3299 if (!input->touch_focus) {
3300 DBG("No touch focus found for touch cancel event!\n");
3301 return;
3302 }
3303
3304 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3305 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003306 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3307 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003308
3309 wl_list_remove(&tp->link);
3310 free(tp);
3311 }
3312}
3313
3314static const struct wl_touch_listener touch_listener = {
3315 touch_handle_down,
3316 touch_handle_up,
3317 touch_handle_motion,
3318 touch_handle_frame,
3319 touch_handle_cancel,
3320};
3321
3322static void
Daniel Stone37816df2012-05-16 18:45:18 +01003323seat_handle_capabilities(void *data, struct wl_seat *seat,
3324 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003325{
Daniel Stone37816df2012-05-16 18:45:18 +01003326 struct input *input = data;
3327
3328 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3329 input->pointer = wl_seat_get_pointer(seat);
3330 wl_pointer_set_user_data(input->pointer, input);
3331 wl_pointer_add_listener(input->pointer, &pointer_listener,
3332 input);
3333 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003334 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003335 wl_pointer_release(input->pointer);
3336 else
3337 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003338 input->pointer = NULL;
3339 }
3340
3341 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3342 input->keyboard = wl_seat_get_keyboard(seat);
3343 wl_keyboard_set_user_data(input->keyboard, input);
3344 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3345 input);
3346 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003347 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003348 wl_keyboard_release(input->keyboard);
3349 else
3350 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003351 input->keyboard = NULL;
3352 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003353
3354 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3355 input->touch = wl_seat_get_touch(seat);
3356 wl_touch_set_user_data(input->touch, input);
3357 wl_touch_add_listener(input->touch, &touch_listener, input);
3358 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003359 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003360 wl_touch_release(input->touch);
3361 else
3362 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003363 input->touch = NULL;
3364 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003365}
3366
Rob Bradford08031182013-08-13 20:11:03 +01003367static void
3368seat_handle_name(void *data, struct wl_seat *seat,
3369 const char *name)
3370{
3371
3372}
3373
Daniel Stone37816df2012-05-16 18:45:18 +01003374static const struct wl_seat_listener seat_listener = {
3375 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003376 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003377};
3378
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003379void
3380input_get_position(struct input *input, int32_t *x, int32_t *y)
3381{
3382 *x = input->sx;
3383 *y = input->sy;
3384}
3385
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003386int
3387input_get_touch(struct input *input, int32_t id, float *x, float *y)
3388{
3389 struct touch_point *tp;
3390
3391 wl_list_for_each(tp, &input->touch_point_list, link) {
3392 if (tp->id != id)
3393 continue;
3394
3395 *x = tp->x;
3396 *y = tp->y;
3397 return 0;
3398 }
3399
3400 return -1;
3401}
3402
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003403struct display *
3404input_get_display(struct input *input)
3405{
3406 return input->display;
3407}
3408
Daniel Stone37816df2012-05-16 18:45:18 +01003409struct wl_seat *
3410input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003411{
Daniel Stone37816df2012-05-16 18:45:18 +01003412 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003413}
3414
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003415uint32_t
3416input_get_modifiers(struct input *input)
3417{
3418 return input->modifiers;
3419}
3420
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003421struct widget *
3422input_get_focus_widget(struct input *input)
3423{
3424 return input->focus_widget;
3425}
3426
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003427struct data_offer {
3428 struct wl_data_offer *offer;
3429 struct input *input;
3430 struct wl_array types;
3431 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003432
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003433 struct task io_task;
3434 int fd;
3435 data_func_t func;
3436 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003437 uint32_t dnd_action;
3438 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003439 void *user_data;
3440};
3441
3442static void
3443data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3444{
3445 struct data_offer *offer = data;
3446 char **p;
3447
3448 p = wl_array_add(&offer->types, sizeof *p);
3449 *p = strdup(type);
3450}
3451
Carlos Garnacho9c931792016-01-18 23:52:12 +01003452static void
3453data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3454{
3455 struct data_offer *offer = data;
3456
3457 offer->source_actions = source_actions;
3458}
3459
3460static void
3461data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3462{
3463 struct data_offer *offer = data;
3464
3465 offer->dnd_action = dnd_action;
3466}
3467
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003468static const struct wl_data_offer_listener data_offer_listener = {
3469 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003470 data_offer_source_actions,
3471 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003472};
3473
3474static void
3475data_offer_destroy(struct data_offer *offer)
3476{
3477 char **p;
3478
3479 offer->refcount--;
3480 if (offer->refcount == 0) {
3481 wl_data_offer_destroy(offer->offer);
3482 for (p = offer->types.data; *p; p++)
3483 free(*p);
3484 wl_array_release(&offer->types);
3485 free(offer);
3486 }
3487}
3488
3489static void
3490data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003491 struct wl_data_device *data_device,
3492 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003493{
3494 struct data_offer *offer;
3495
Brian Lovinbc919262013-08-07 15:34:59 -07003496 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003497
3498 wl_array_init(&offer->types);
3499 offer->refcount = 1;
3500 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003501 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003502 wl_data_offer_add_listener(offer->offer,
3503 &data_offer_listener, offer);
3504}
3505
3506static void
3507data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003508 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003509 wl_fixed_t x_w, wl_fixed_t y_w,
3510 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003511{
3512 struct input *input = data;
3513 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003514 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003515 float x = wl_fixed_to_double(x_w);
3516 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003517 char **p;
3518
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003519 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003520 input->drag_enter_serial = serial;
3521 input->drag_focus = window,
3522 input->drag_x = x;
3523 input->drag_y = y;
3524
3525 if (!input->touch_grab)
3526 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003527
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003528 if (offer) {
3529 input->drag_offer = wl_data_offer_get_user_data(offer);
3530
3531 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3532 *p = NULL;
3533
3534 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003535
3536 if (input->display->data_device_manager_version >=
3537 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3538 wl_data_offer_set_actions(offer,
3539 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3540 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3541 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3542 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003543 } else {
3544 input->drag_offer = NULL;
3545 types_data = NULL;
3546 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003547
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003548 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003549 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003550 window->user_data);
3551}
3552
3553static void
3554data_device_leave(void *data, struct wl_data_device *data_device)
3555{
3556 struct input *input = data;
3557
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003558 if (input->drag_offer) {
3559 data_offer_destroy(input->drag_offer);
3560 input->drag_offer = NULL;
3561 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003562}
3563
3564static void
3565data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003566 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003567{
3568 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003569 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003570 float x = wl_fixed_to_double(x_w);
3571 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003572 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003573
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003574 input->drag_x = x;
3575 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003576
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003577 if (input->drag_offer)
3578 types_data = input->drag_offer->types.data;
3579 else
3580 types_data = NULL;
3581
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003582 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003583 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003584 window->user_data);
3585}
3586
3587static void
3588data_device_drop(void *data, struct wl_data_device *data_device)
3589{
3590 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003591 struct window *window = input->drag_focus;
3592 float x, y;
3593
3594 x = input->drag_x;
3595 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003596
3597 if (window->drop_handler)
3598 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003599 x, y, window->user_data);
3600
3601 if (input->touch_grab)
3602 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003603}
3604
3605static void
3606data_device_selection(void *data,
3607 struct wl_data_device *wl_data_device,
3608 struct wl_data_offer *offer)
3609{
3610 struct input *input = data;
3611 char **p;
3612
3613 if (input->selection_offer)
3614 data_offer_destroy(input->selection_offer);
3615
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003616 if (offer) {
3617 input->selection_offer = wl_data_offer_get_user_data(offer);
3618 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3619 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003620 } else {
3621 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003622 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003623}
3624
3625static const struct wl_data_device_listener data_device_listener = {
3626 data_device_data_offer,
3627 data_device_enter,
3628 data_device_leave,
3629 data_device_motion,
3630 data_device_drop,
3631 data_device_selection
3632};
3633
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003634static void
3635input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003636{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003637 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003638 struct wl_cursor *cursor;
3639 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003640
Daniel Stone80972742012-11-07 17:51:39 +11003641 if (!input->pointer)
3642 return;
3643
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003644 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003645 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003646 return;
3647
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003648 if (index >= (int) cursor->image_count) {
3649 fprintf(stderr, "cursor index out of range\n");
3650 return;
3651 }
3652
3653 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003654 buffer = wl_cursor_image_get_buffer(image);
3655 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003656 return;
3657
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003658 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3659 wl_surface_damage(input->pointer_surface, 0, 0,
3660 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003661 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003662 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3663 input->pointer_surface,
3664 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003665}
3666
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003667static const struct wl_callback_listener pointer_surface_listener;
3668
Derek Foreman493d9792015-03-04 16:26:25 -06003669static bool
3670input_set_pointer_special(struct input *input)
3671{
3672 if (input->current_cursor == CURSOR_BLANK) {
3673 wl_pointer_set_cursor(input->pointer,
3674 input->pointer_enter_serial,
3675 NULL, 0, 0);
3676 return true;
3677 }
3678
3679 if (input->current_cursor == CURSOR_UNSET)
3680 return true;
3681
3682 return false;
3683}
3684
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003685static void
Derek Foreman118a4292015-04-22 17:23:35 -05003686schedule_pointer_image_update(struct input *input,
3687 struct wl_cursor *cursor,
3688 uint32_t duration,
3689 bool force_frame)
3690{
3691 /* Some silly cursor sets have enormous pauses in them. In these
3692 * cases it's better to use a timer even if it results in less
3693 * accurate presentation, since it will save us having to set the
3694 * same cursor image over and over again.
3695 *
3696 * This is really not the way we're supposed to time any kind of
3697 * animation, but we're pretending it's OK here because we don't
3698 * want animated cursors with long delays to needlessly hog CPU.
3699 *
3700 * We use force_frame to ensure we don't accumulate large timing
3701 * errors by running off the wrong clock.
3702 */
3703 if (!force_frame && duration > 100) {
3704 struct timespec tp;
3705
3706 clock_gettime(CLOCK_MONOTONIC, &tp);
3707 input->cursor_timer_start = tp.tv_sec * 1000
3708 + tp.tv_nsec / 1000000;
3709 cursor_delay_timer_reset(input, duration);
3710 return;
3711 }
3712
3713 /* for short durations we'll just spin on frame callbacks for
3714 * accurate timing - the way any kind of timing sensitive animation
3715 * should really be done. */
3716 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3717 wl_callback_add_listener(input->cursor_frame_cb,
3718 &pointer_surface_listener, input);
3719
3720}
3721
3722static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003723pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3724 uint32_t time)
3725{
3726 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003727 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003728 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003729 uint32_t duration;
3730 bool force_frame = true;
3731
3732 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003733
3734 if (callback) {
3735 assert(callback == input->cursor_frame_cb);
3736 wl_callback_destroy(callback);
3737 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003738 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003739 }
3740
Daniel Stone80972742012-11-07 17:51:39 +11003741 if (!input->pointer)
3742 return;
3743
Derek Foreman493d9792015-03-04 16:26:25 -06003744 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003745 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003746
Daniel Stonea494f1d2012-06-18 19:31:12 +01003747 cursor = input->display->cursors[input->current_cursor];
3748 if (!cursor)
3749 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003750
3751 /* FIXME We don't have the current time on the first call so we set
3752 * the animation start to the time of the first frame callback. */
3753 if (time == 0)
3754 input->cursor_anim_start = 0;
3755 else if (input->cursor_anim_start == 0)
3756 input->cursor_anim_start = time;
3757
Derek Foreman118a4292015-04-22 17:23:35 -05003758 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003759
Derek Foreman118a4292015-04-22 17:23:35 -05003760 if (time == 0 || input->cursor_anim_start == 0) {
3761 duration = 0;
3762 i = 0;
3763 } else
3764 i = wl_cursor_frame_and_duration(
3765 cursor,
3766 time - input->cursor_anim_start,
3767 &duration);
3768
3769 if (cursor->image_count > 1)
3770 schedule_pointer_image_update(input, cursor, duration,
3771 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003772
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003773 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003774}
3775
Derek Foreman118a4292015-04-22 17:23:35 -05003776static void
3777cursor_timer_func(struct task *task, uint32_t events)
3778{
3779 struct input *input = container_of(task, struct input, cursor_task);
3780 struct timespec tp;
3781 struct wl_cursor *cursor;
3782 uint32_t time;
3783 uint64_t exp;
3784
3785 if (!input->cursor_timer_running)
3786 return;
3787
3788 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3789 return;
3790
3791 cursor = input->display->cursors[input->current_cursor];
3792 if (!cursor)
3793 return;
3794
3795 clock_gettime(CLOCK_MONOTONIC, &tp);
3796 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3797 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3798}
3799
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003800static const struct wl_callback_listener pointer_surface_listener = {
3801 pointer_surface_frame_callback
3802};
3803
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003804void
3805input_set_pointer_image(struct input *input, int pointer)
3806{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003807 int force = 0;
3808
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003809 if (!input->pointer)
3810 return;
3811
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003812 if (input->pointer_enter_serial > input->cursor_serial)
3813 force = 1;
3814
3815 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003816 return;
3817
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003818 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003819 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003820 if (!input->cursor_frame_cb)
3821 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003822 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003823 /* The current frame callback may be stuck if, for instance,
3824 * the set cursor request was processed by the server after
3825 * this client lost the focus. In this case the cursor surface
3826 * might not be mapped and the frame callback wouldn't ever
3827 * complete. Send a set_cursor and attach to try to map the
3828 * cursor surface again so that the callback will finish */
3829 input_set_pointer_image_index(input, 0);
3830 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003831}
3832
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003833struct wl_data_device *
3834input_get_data_device(struct input *input)
3835{
3836 return input->data_device;
3837}
3838
3839void
3840input_set_selection(struct input *input,
3841 struct wl_data_source *source, uint32_t time)
3842{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003843 if (input->data_device)
3844 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003845}
3846
3847void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003848input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003849{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003850 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003851 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003852}
3853
3854static void
3855offer_io_func(struct task *task, uint32_t events)
3856{
3857 struct data_offer *offer =
3858 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003859 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003860 unsigned int len;
3861 char buffer[4096];
3862
3863 len = read(offer->fd, buffer, sizeof buffer);
3864 offer->func(buffer, len,
3865 offer->x, offer->y, offer->user_data);
3866
3867 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003868 if (display->data_device_manager_version >=
3869 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3870 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003871 close(offer->fd);
3872 data_offer_destroy(offer);
3873 }
3874}
3875
3876static void
3877data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3878 data_func_t func, void *user_data)
3879{
3880 int p[2];
3881
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003882 if (pipe2(p, O_CLOEXEC) == -1)
3883 return;
3884
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003885 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3886 close(p[1]);
3887
3888 offer->io_task.run = offer_io_func;
3889 offer->fd = p[0];
3890 offer->func = func;
3891 offer->refcount++;
3892 offer->user_data = user_data;
3893
3894 display_watch_fd(offer->input->display,
3895 offer->fd, EPOLLIN, &offer->io_task);
3896}
3897
3898void
3899input_receive_drag_data(struct input *input, const char *mime_type,
3900 data_func_t func, void *data)
3901{
3902 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003903 input->drag_offer->x = input->drag_x;
3904 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003905}
3906
3907int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003908input_receive_drag_data_to_fd(struct input *input,
3909 const char *mime_type, int fd)
3910{
3911 if (input->drag_offer)
3912 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3913
3914 return 0;
3915}
3916
3917int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003918input_receive_selection_data(struct input *input, const char *mime_type,
3919 data_func_t func, void *data)
3920{
3921 char **p;
3922
3923 if (input->selection_offer == NULL)
3924 return -1;
3925
3926 for (p = input->selection_offer->types.data; *p; p++)
3927 if (strcmp(mime_type, *p) == 0)
3928 break;
3929
3930 if (*p == NULL)
3931 return -1;
3932
3933 data_offer_receive_data(input->selection_offer,
3934 mime_type, func, data);
3935 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003936}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003937
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003938int
3939input_receive_selection_data_to_fd(struct input *input,
3940 const char *mime_type, int fd)
3941{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003942 if (input->selection_offer)
3943 wl_data_offer_receive(input->selection_offer->offer,
3944 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003945
3946 return 0;
3947}
3948
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003949void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003950window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003951{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003952 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003953 return;
3954
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003955 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003956}
3957
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003958static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003959surface_set_synchronized(struct surface *surface)
3960{
3961 if (!surface->subsurface)
3962 return;
3963
3964 if (surface->synchronized)
3965 return;
3966
3967 wl_subsurface_set_sync(surface->subsurface);
3968 surface->synchronized = 1;
3969}
3970
3971static void
3972surface_set_synchronized_default(struct surface *surface)
3973{
3974 if (!surface->subsurface)
3975 return;
3976
3977 if (surface->synchronized == surface->synchronized_default)
3978 return;
3979
3980 if (surface->synchronized_default)
3981 wl_subsurface_set_sync(surface->subsurface);
3982 else
3983 wl_subsurface_set_desync(surface->subsurface);
3984
3985 surface->synchronized = surface->synchronized_default;
3986}
3987
3988static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003989surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003990{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003991 struct widget *widget = surface->widget;
3992 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003993
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003994 if (surface->input_region) {
3995 wl_region_destroy(surface->input_region);
3996 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003997 }
3998
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003999 if (surface->opaque_region)
4000 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004001
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004002 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004003
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004004 if (widget->resize_handler)
4005 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004006 widget->allocation.width,
4007 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004008 widget->user_data);
4009
Pekka Paalanen35e82632013-04-25 13:57:48 +03004010 if (surface->subsurface &&
4011 (surface->allocation.x != widget->allocation.x ||
4012 surface->allocation.y != widget->allocation.y)) {
4013 wl_subsurface_set_position(surface->subsurface,
4014 widget->allocation.x,
4015 widget->allocation.y);
4016 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004017 if (surface->allocation.width != widget->allocation.width ||
4018 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004019 window_schedule_redraw(widget->window);
4020 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004021 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004022
4023 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004024 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004025 widget->allocation.width,
4026 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004027}
4028
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004029static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004030window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004031{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004032 struct surface *surface;
4033
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004034 widget_set_allocation(window->main_surface->widget,
4035 window->pending_allocation.x,
4036 window->pending_allocation.y,
4037 window->pending_allocation.width,
4038 window->pending_allocation.height);
4039
4040 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004041
4042 /* The main surface is in the list, too. Main surface's
4043 * resize_handler is responsible for calling widget_set_allocation()
4044 * on all sub-surface root widgets, so they will be resized
4045 * properly.
4046 */
4047 wl_list_for_each(surface, &window->subsurface_list, link) {
4048 if (surface == window->main_surface)
4049 continue;
4050
4051 surface_set_synchronized(surface);
4052 surface_resize(surface);
4053 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004054
4055 if (!window->fullscreen && !window->maximized)
4056 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004057}
4058
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004059static void
4060idle_resize(struct window *window)
4061{
4062 window->resize_needed = 0;
4063 window->redraw_needed = 1;
4064
4065 DBG("from %dx%d to %dx%d\n",
4066 window->main_surface->server_allocation.width,
4067 window->main_surface->server_allocation.height,
4068 window->pending_allocation.width,
4069 window->pending_allocation.height);
4070
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004071 window_do_resize(window);
4072}
4073
4074static void
4075undo_resize(struct window *window)
4076{
4077 window->pending_allocation.width =
4078 window->main_surface->server_allocation.width;
4079 window->pending_allocation.height =
4080 window->main_surface->server_allocation.height;
4081
4082 DBG("back to %dx%d\n",
4083 window->main_surface->server_allocation.width,
4084 window->main_surface->server_allocation.height);
4085
4086 window_do_resize(window);
4087
4088 if (window->pending_allocation.width == 0 &&
4089 window->pending_allocation.height == 0) {
4090 fprintf(stderr, "Error: Could not draw a surface, "
4091 "most likely due to insufficient disk space in "
4092 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4093 exit(EXIT_FAILURE);
4094 }
4095}
4096
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004097void
4098window_schedule_resize(struct window *window, int width, int height)
4099{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004100 /* We should probably get these numbers from the theme. */
4101 const int min_width = 200, min_height = 200;
4102
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004103 window->pending_allocation.x = 0;
4104 window->pending_allocation.y = 0;
4105 window->pending_allocation.width = width;
4106 window->pending_allocation.height = height;
4107
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004108 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004109 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004110 window->min_allocation.width = min_width;
4111 else
4112 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004113 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004114 window->min_allocation.height = min_height;
4115 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004116 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004117 }
4118
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004119 if (window->pending_allocation.width < window->min_allocation.width)
4120 window->pending_allocation.width = window->min_allocation.width;
4121 if (window->pending_allocation.height < window->min_allocation.height)
4122 window->pending_allocation.height = window->min_allocation.height;
4123
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004124 window->resize_needed = 1;
4125 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004126}
4127
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004128void
4129widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4130{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004131 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004132}
4133
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004134static int
4135window_get_shadow_margin(struct window *window)
4136{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004137 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004138 return frame_get_shadow_margin(window->frame->frame);
4139 else
4140 return 0;
4141}
4142
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004143static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004144handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004145 int32_t width, int32_t height,
4146 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004147{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004148 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004149 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004150
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004151 window->maximized = 0;
4152 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004153 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004154 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004155
4156 wl_array_for_each(p, states) {
4157 uint32_t state = *p;
4158 switch (state) {
4159 case XDG_SURFACE_STATE_MAXIMIZED:
4160 window->maximized = 1;
4161 break;
4162 case XDG_SURFACE_STATE_FULLSCREEN:
4163 window->fullscreen = 1;
4164 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004165 case XDG_SURFACE_STATE_RESIZING:
4166 window->resizing = 1;
4167 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004168 case XDG_SURFACE_STATE_ACTIVATED:
4169 window->focused = 1;
4170 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004171 default:
4172 /* Unknown state */
4173 break;
4174 }
4175 }
4176
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004177 if (window->frame) {
4178 if (window->maximized) {
4179 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4180 } else {
4181 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4182 }
4183
4184 if (window->focused) {
4185 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4186 } else {
4187 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4188 }
4189 }
4190
Jasper St. Pierref184c382014-05-06 08:33:27 -04004191 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004192 /* The width / height params are for window geometry,
4193 * but window_schedule_resize takes allocation. Add
4194 * on the shadow margin to get the difference. */
4195 int margin = window_get_shadow_margin(window);
4196
4197 window_schedule_resize(window,
4198 width + margin * 2,
4199 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004200 } else {
4201 window_schedule_resize(window,
4202 window->saved_allocation.width,
4203 window->saved_allocation.height);
4204 }
4205
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004206 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004207
4208 if (window->state_changed_handler)
4209 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004210}
4211
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004212static void
4213handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4214{
4215 struct window *window = data;
4216 window_close(window);
4217}
4218
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004219static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004220 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004221 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004222};
4223
4224static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004225window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004226{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004227 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004228
4229 if (!window->xdg_surface)
4230 return;
4231
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004232 if (window->parent == window->last_parent)
4233 return;
4234
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004235 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004236 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004237 else
4238 parent_surface = NULL;
4239
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004240 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004241 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004242}
4243
4244static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004245window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004246{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004247 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004248 frame_input_rect(window->frame->frame,
4249 &geometry->x,
4250 &geometry->y,
4251 &geometry->width,
4252 &geometry->height);
4253 else
4254 window_get_allocation(window, geometry);
4255}
4256
4257static void
4258window_sync_geometry(struct window *window)
4259{
4260 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004261
4262 if (!window->xdg_surface)
4263 return;
4264
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004265 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004266 if (geometry.x == window->last_geometry.x &&
4267 geometry.y == window->last_geometry.y &&
4268 geometry.width == window->last_geometry.width &&
4269 geometry.height == window->last_geometry.height)
4270 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004271
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004272 xdg_surface_set_window_geometry(window->xdg_surface,
4273 geometry.x,
4274 geometry.y,
4275 geometry.width,
4276 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004277 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004278}
4279
4280static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004281window_flush(struct window *window)
4282{
4283 struct surface *surface;
4284
4285 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004286 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004287 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004288 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004289 }
4290 }
4291
4292 wl_list_for_each(surface, &window->subsurface_list, link) {
4293 if (surface == window->main_surface)
4294 continue;
4295
4296 surface_flush(surface);
4297 }
4298
4299 surface_flush(window->main_surface);
4300}
4301
4302static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004303menu_destroy(struct menu *menu)
4304{
4305 widget_destroy(menu->widget);
4306 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004307 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004308 free(menu);
4309}
4310
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004311void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004312window_get_allocation(struct window *window,
4313 struct rectangle *allocation)
4314{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004315 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004316}
4317
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004318static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004319widget_redraw(struct widget *widget)
4320{
4321 struct widget *child;
4322
4323 if (widget->redraw_handler)
4324 widget->redraw_handler(widget, widget->user_data);
4325 wl_list_for_each(child, &widget->child_list, link)
4326 widget_redraw(child);
4327}
4328
4329static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004330frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4331{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004332 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004333
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004334 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004335 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004336 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004337 surface->frame_cb = NULL;
4338
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004339 surface->last_time = time;
4340
Pekka Paalanen71233882013-04-25 13:57:53 +03004341 if (surface->redraw_needed || surface->window->redraw_needed) {
4342 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004343 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004344 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004345}
4346
4347static const struct wl_callback_listener listener = {
4348 frame_callback
4349};
4350
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004351static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004352surface_redraw(struct surface *surface)
4353{
Pekka Paalanen71233882013-04-25 13:57:53 +03004354 DBG_OBJ(surface->surface, "begin\n");
4355
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004356 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004357 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004358
4359 /* Whole-window redraw forces a redraw even if the previous has
4360 * not yet hit the screen.
4361 */
4362 if (surface->frame_cb) {
4363 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004364 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004365
Pekka Paalanen71233882013-04-25 13:57:53 +03004366 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004367 wl_callback_destroy(surface->frame_cb);
4368 }
4369
Neil Roberts97b747c2013-12-19 16:17:12 +00004370 if (surface->widget->use_cairo &&
4371 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004372 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004373 return -1;
4374 }
4375
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004376 surface->frame_cb = wl_surface_frame(surface->surface);
4377 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004378 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004379
4380 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004381 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004382 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004383 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004384 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004385}
4386
4387static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004388idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004389{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004390 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004391 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004392 int failed = 0;
4393 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004394
Pekka Paalanen71233882013-04-25 13:57:53 +03004395 DBG(" --------- \n");
4396
Pekka Paalaneneebff542013-04-25 13:57:52 +03004397 wl_list_init(&window->redraw_task.link);
4398 window->redraw_task_scheduled = 0;
4399
4400 if (window->resize_needed) {
4401 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004402 if (window->main_surface->frame_cb) {
4403 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004404 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004405 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004406
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004407 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004408 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004409 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004410
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004411 if (surface_redraw(window->main_surface) < 0) {
4412 /*
4413 * Only main_surface failure will cause us to undo the resize.
4414 * If sub-surfaces fail, they will just be broken with old
4415 * content.
4416 */
4417 failed = 1;
4418 } else {
4419 wl_list_for_each(surface, &window->subsurface_list, link) {
4420 if (surface == window->main_surface)
4421 continue;
4422
4423 surface_redraw(surface);
4424 }
4425 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004426
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004427 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004428 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004429
4430 wl_list_for_each(surface, &window->subsurface_list, link)
4431 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004432
4433 if (resized && failed) {
4434 /* Restore widget tree to correspond to what is on screen. */
4435 undo_resize(window);
4436 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004437}
4438
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004439static void
4440window_schedule_redraw_task(struct window *window)
4441{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004442 if (!window->redraw_task_scheduled) {
4443 window->redraw_task.run = idle_redraw;
4444 display_defer(window->display, &window->redraw_task);
4445 window->redraw_task_scheduled = 1;
4446 }
4447}
4448
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004449void
4450window_schedule_redraw(struct window *window)
4451{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004452 struct surface *surface;
4453
Pekka Paalanen71233882013-04-25 13:57:53 +03004454 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4455
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004456 wl_list_for_each(surface, &window->subsurface_list, link)
4457 surface->redraw_needed = 1;
4458
4459 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004460}
4461
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004462int
4463window_is_fullscreen(struct window *window)
4464{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004465 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004466}
4467
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004468void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004469window_set_fullscreen(struct window *window, int fullscreen)
4470{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004471 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004472 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004473
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004474 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004475 return;
4476
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004477 if (fullscreen)
4478 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4479 else
4480 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004481}
4482
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004483int
4484window_is_maximized(struct window *window)
4485{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004486 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004487}
4488
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004489void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004490window_set_maximized(struct window *window, int maximized)
4491{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004492 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004493 return;
4494
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004495 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004496 return;
4497
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004498 if (maximized)
4499 xdg_surface_set_maximized(window->xdg_surface);
4500 else
4501 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004502}
4503
Jasper St. Pierrede680992014-04-10 17:23:49 -07004504int
4505window_is_resizing(struct window *window)
4506{
4507 return window->resizing;
4508}
4509
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004510void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004511window_set_minimized(struct window *window)
4512{
4513 if (!window->xdg_surface)
4514 return;
4515
4516 xdg_surface_set_minimized(window->xdg_surface);
4517}
4518
4519void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004520window_set_user_data(struct window *window, void *data)
4521{
4522 window->user_data = data;
4523}
4524
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004525void *
4526window_get_user_data(struct window *window)
4527{
4528 return window->user_data;
4529}
4530
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004531void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004532window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004533 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004534{
4535 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004536}
4537
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004538void
4539window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004540 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004541{
4542 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004543}
4544
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004545void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004546window_set_data_handler(struct window *window, window_data_handler_t handler)
4547{
4548 window->data_handler = handler;
4549}
4550
4551void
4552window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4553{
4554 window->drop_handler = handler;
4555}
4556
4557void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004558window_set_close_handler(struct window *window,
4559 window_close_handler_t handler)
4560{
4561 window->close_handler = handler;
4562}
4563
4564void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004565window_set_fullscreen_handler(struct window *window,
4566 window_fullscreen_handler_t handler)
4567{
4568 window->fullscreen_handler = handler;
4569}
4570
4571void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004572window_set_output_handler(struct window *window,
4573 window_output_handler_t handler)
4574{
4575 window->output_handler = handler;
4576}
4577
4578void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004579window_set_state_changed_handler(struct window *window,
4580 window_state_changed_handler_t handler)
4581{
4582 window->state_changed_handler = handler;
4583}
4584
4585void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004586window_set_title(struct window *window, const char *title)
4587{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004588 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004589 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004590 if (window->frame) {
4591 frame_set_title(window->frame->frame, title);
4592 widget_schedule_redraw(window->frame->widget);
4593 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004594 if (window->xdg_surface)
4595 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004596}
4597
4598const char *
4599window_get_title(struct window *window)
4600{
4601 return window->title;
4602}
4603
4604void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004605window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4606{
4607 struct text_cursor_position *text_cursor_position =
4608 window->display->text_cursor_position;
4609
Scott Moreau9295ce02012-06-01 12:46:10 -06004610 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004611 return;
4612
4613 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004614 window->main_surface->surface,
4615 wl_fixed_from_int(x),
4616 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004617}
4618
Casey Dahlin9074db52012-04-19 22:50:09 -04004619static void
4620surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004621 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004622{
Rob Bradford7507b572012-05-15 17:55:34 +01004623 struct window *window = data;
4624 struct output *output;
4625 struct output *output_found = NULL;
4626 struct window_output *window_output;
4627
4628 wl_list_for_each(output, &window->display->output_list, link) {
4629 if (output->output == wl_output) {
4630 output_found = output;
4631 break;
4632 }
4633 }
4634
4635 if (!output_found)
4636 return;
4637
Brian Lovinbc919262013-08-07 15:34:59 -07004638 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004639 window_output->output = output_found;
4640
4641 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004642
4643 if (window->output_handler)
4644 window->output_handler(window, output_found, 1,
4645 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004646}
4647
4648static void
4649surface_leave(void *data,
4650 struct wl_surface *wl_surface, struct wl_output *output)
4651{
Rob Bradford7507b572012-05-15 17:55:34 +01004652 struct window *window = data;
4653 struct window_output *window_output;
4654 struct window_output *window_output_found = NULL;
4655
4656 wl_list_for_each(window_output, &window->window_output_list, link) {
4657 if (window_output->output->output == output) {
4658 window_output_found = window_output;
4659 break;
4660 }
4661 }
4662
4663 if (window_output_found) {
4664 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004665
4666 if (window->output_handler)
4667 window->output_handler(window, window_output->output,
4668 0, window->user_data);
4669
Rob Bradford7507b572012-05-15 17:55:34 +01004670 free(window_output_found);
4671 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004672}
4673
4674static const struct wl_surface_listener surface_listener = {
4675 surface_enter,
4676 surface_leave
4677};
4678
Pekka Paalanen4e373742013-02-13 16:17:13 +02004679static struct surface *
4680surface_create(struct window *window)
4681{
4682 struct display *display = window->display;
4683 struct surface *surface;
4684
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004685 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004686 surface->window = window;
4687 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004688 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004689 wl_surface_add_listener(surface->surface, &surface_listener, window);
4690
Pekka Paalanen35e82632013-04-25 13:57:48 +03004691 wl_list_insert(&window->subsurface_list, &surface->link);
4692
Pekka Paalanen4e373742013-02-13 16:17:13 +02004693 return surface;
4694}
4695
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004696static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004697get_preferred_buffer_type(struct display *display)
4698{
4699#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004700 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004701 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4702#endif
4703
4704 return WINDOW_BUFFER_TYPE_SHM;
4705}
4706
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004707static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004708window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004709{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004710 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004711 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004712
Peter Huttererf3d62272013-08-08 11:57:05 +10004713 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004714 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004715 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004716
4717 surface = surface_create(window);
4718 window->main_surface = surface;
4719
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004720 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004721
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004722 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004723 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004724
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004725 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004726
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004727 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004728 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004729 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004730
Rob Bradford7507b572012-05-15 17:55:34 +01004731 wl_list_init (&window->window_output_list);
4732
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004733 return window;
4734}
4735
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004736struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004737window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004738{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004739 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004740 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004741
4742 window = window_create_internal(display, 0);
4743
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004744 if (window->display->xdg_shell) {
4745 window->xdg_surface =
4746 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4747 window->main_surface->surface);
4748 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004749
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004750 xdg_surface_set_user_data(window->xdg_surface, window);
4751 xdg_surface_add_listener(window->xdg_surface,
4752 &xdg_surface_listener, window);
4753 } else if (display->ivi_application) {
4754 /* auto generation of ivi_id based on process id + basement of id */
4755 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4756 window->ivi_surface =
4757 ivi_application_surface_create(display->ivi_application,
4758 id_ivisurf, window->main_surface->surface);
4759 fail_on_null(window->ivi_surface);
4760
4761 ivi_surface_add_listener(window->ivi_surface,
4762 &ivi_surface_listener, window);
4763 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004764
4765 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004766}
4767
4768struct window *
4769window_create_custom(struct display *display)
4770{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004771 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004772}
4773
Jasper St. Pierre53686042013-12-09 15:26:25 -05004774void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004775window_set_parent(struct window *window,
4776 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004777{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004778 window->parent = parent_window;
4779 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004780}
4781
4782struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004783window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004784{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004785 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004786}
4787
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004788static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004789menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004790{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004791 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004792 int next;
4793
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004794 frame_interior(menu->frame, &x, &y, &width, &height);
4795 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004796 if (menu->current != next) {
4797 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004798 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004799 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004800}
4801
4802static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004803menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004804 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004805 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004806{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004807 struct menu *menu = data;
4808
4809 if (widget == menu->widget)
4810 menu_set_item(data, y);
4811
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004812 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004813}
4814
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004815static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004816menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004817 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004818{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004819 struct menu *menu = data;
4820
4821 if (widget == menu->widget)
4822 menu_set_item(data, y);
4823
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004824 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004825}
4826
4827static void
4828menu_leave_handler(struct widget *widget, struct input *input, void *data)
4829{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004830 struct menu *menu = data;
4831
4832 if (widget == menu->widget)
4833 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004834}
4835
4836static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004837menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004838 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004839 uint32_t button, enum wl_pointer_button_state state,
4840 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004841
4842{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004843 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004844
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004845 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4846 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004847 /* Either relase after press-drag-release or
4848 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004849 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05004850 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004851 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004852 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004853 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004854 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004855}
4856
4857static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004858menu_touch_up_handler(struct widget *widget,
4859 struct input *input,
4860 uint32_t serial,
4861 uint32_t time,
4862 int32_t id,
4863 void *data)
4864{
4865 struct menu *menu = data;
4866
4867 input_ungrab(input);
4868 menu_destroy(menu);
4869}
4870
4871static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004872menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004873{
4874 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004875 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004876 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004877
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004878 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004879
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004880 frame_repaint(menu->frame, cr);
4881 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004882
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004883 theme_set_background_source(menu->window->display->theme,
4884 cr, THEME_FRAME_ACTIVE);
4885 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004886 cairo_fill(cr);
4887
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004888 cairo_select_font_face(cr, "sans",
4889 CAIRO_FONT_SLANT_NORMAL,
4890 CAIRO_FONT_WEIGHT_NORMAL);
4891 cairo_set_font_size(cr, 12);
4892
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004893 for (i = 0; i < menu->count; i++) {
4894 if (i == menu->current) {
4895 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004896 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004897 cairo_fill(cr);
4898 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004899 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004900 cairo_show_text(cr, menu->entries[i]);
4901 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004902 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4903 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004904 cairo_show_text(cr, menu->entries[i]);
4905 }
4906 }
4907
4908 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004909}
4910
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004911static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004912handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004913{
4914 struct window *window = data;
4915 struct menu *menu = window->main_surface->widget->user_data;
4916
4917 input_ungrab(menu->input);
4918 menu_destroy(menu);
4919}
4920
4921static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004922 handle_popup_popup_done,
4923};
4924
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004925static struct menu *
4926create_menu(struct display *display,
4927 struct input *input, uint32_t time,
4928 menu_func_t func, const char **entries, int count,
4929 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004930{
4931 struct window *window;
4932 struct menu *menu;
4933
4934 menu = malloc(sizeof *menu);
4935 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004936 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004937
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004938 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004939 if (!window) {
4940 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004941 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004942 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004943
4944 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004945 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004946 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004947 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004948 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004949 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004950 menu->entries = entries;
4951 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004952 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004953 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004954 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004955 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004956 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004957
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004958 input_ungrab(input);
4959
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004960 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004961 widget_set_enter_handler(menu->widget, menu_enter_handler);
4962 widget_set_leave_handler(menu->widget, menu_leave_handler);
4963 widget_set_motion_handler(menu->widget, menu_motion_handler);
4964 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004965 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004966
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004967 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004968 frame_resize_inside(menu->frame, 200, count * 20);
4969 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4970 window_schedule_resize(window, frame_width(menu->frame),
4971 frame_height(menu->frame));
4972
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004973 return menu;
4974}
4975
4976struct window *
4977window_create_menu(struct display *display,
4978 struct input *input, uint32_t time,
4979 menu_func_t func, const char **entries, int count,
4980 void *user_data)
4981{
4982 struct menu *menu;
4983 menu = create_menu(display, input, time, func, entries, count, user_data);
4984
4985 if (menu == NULL)
4986 return NULL;
4987
4988 return menu->window;
4989}
4990
4991void
4992window_show_menu(struct display *display,
4993 struct input *input, uint32_t time, struct window *parent,
4994 int32_t x, int32_t y,
4995 menu_func_t func, const char **entries, int count)
4996{
4997 struct menu *menu;
4998 struct window *window;
4999 int32_t ix, iy;
5000
5001 menu = create_menu(display, input, time, func, entries, count, parent);
5002
5003 if (menu == NULL)
5004 return;
5005
5006 window = menu->window;
5007
5008 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5009 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5010
5011 window->x = x;
5012 window->y = y;
5013
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005014 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005015
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005016 if (!display->xdg_shell)
5017 return;
5018
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005019 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
5020 window->main_surface->surface,
5021 parent->main_surface->surface,
5022 input->seat,
5023 display_get_serial(window->display),
5024 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08005025 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005026 fail_on_null(window->xdg_popup);
5027
5028 xdg_popup_set_user_data(window->xdg_popup, window);
5029 xdg_popup_add_listener(window->xdg_popup,
5030 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005031}
5032
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005033void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005034window_set_buffer_type(struct window *window, enum window_buffer_type type)
5035{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005036 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005037}
5038
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005039enum window_buffer_type
5040window_get_buffer_type(struct window *window)
5041{
5042 return window->main_surface->buffer_type;
5043}
5044
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005045void
5046window_set_preferred_format(struct window *window,
5047 enum preferred_format format)
5048{
5049 window->preferred_format = format;
5050}
5051
Pekka Paalanen35e82632013-04-25 13:57:48 +03005052struct widget *
5053window_add_subsurface(struct window *window, void *data,
5054 enum subsurface_mode default_mode)
5055{
5056 struct widget *widget;
5057 struct surface *surface;
5058 struct wl_surface *parent;
5059 struct wl_subcompositor *subcompo = window->display->subcompositor;
5060
Pekka Paalanen35e82632013-04-25 13:57:48 +03005061 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005062 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005063 widget = widget_create(window, surface, data);
5064 wl_list_init(&widget->link);
5065 surface->widget = widget;
5066
5067 parent = window->main_surface->surface;
5068 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5069 surface->surface,
5070 parent);
5071 surface->synchronized = 1;
5072
5073 switch (default_mode) {
5074 case SUBSURFACE_SYNCHRONIZED:
5075 surface->synchronized_default = 1;
5076 break;
5077 case SUBSURFACE_DESYNCHRONIZED:
5078 surface->synchronized_default = 0;
5079 break;
5080 default:
5081 assert(!"bad enum subsurface_mode");
5082 }
5083
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005084 window->resize_needed = 1;
5085 window_schedule_redraw(window);
5086
Pekka Paalanen35e82632013-04-25 13:57:48 +03005087 return widget;
5088}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005089
5090static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005091display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005092 struct wl_output *wl_output,
5093 int x, int y,
5094 int physical_width,
5095 int physical_height,
5096 int subpixel,
5097 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005098 const char *model,
5099 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005100{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005101 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005102
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005103 output->allocation.x = x;
5104 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005105 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005106
5107 if (output->make)
5108 free(output->make);
5109 output->make = strdup(make);
5110
5111 if (output->model)
5112 free(output->model);
5113 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005114}
5115
5116static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005117display_handle_done(void *data,
5118 struct wl_output *wl_output)
5119{
5120}
5121
5122static void
5123display_handle_scale(void *data,
5124 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005125 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005126{
5127 struct output *output = data;
5128
5129 output->scale = scale;
5130}
5131
5132static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005133display_handle_mode(void *data,
5134 struct wl_output *wl_output,
5135 uint32_t flags,
5136 int width,
5137 int height,
5138 int refresh)
5139{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005140 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005141 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005142
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005143 if (flags & WL_OUTPUT_MODE_CURRENT) {
5144 output->allocation.width = width;
5145 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005146 if (display->output_configure_handler)
5147 (*display->output_configure_handler)(
5148 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005149 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005150}
5151
5152static const struct wl_output_listener output_listener = {
5153 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005154 display_handle_mode,
5155 display_handle_done,
5156 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005157};
5158
5159static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005160display_add_output(struct display *d, uint32_t id)
5161{
5162 struct output *output;
5163
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005164 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005165 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005166 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005167 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005168 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005169 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005170 wl_list_insert(d->output_list.prev, &output->link);
5171
5172 wl_output_add_listener(output->output, &output_listener, output);
5173}
5174
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005175static void
5176output_destroy(struct output *output)
5177{
5178 if (output->destroy_handler)
5179 (*output->destroy_handler)(output, output->user_data);
5180
5181 wl_output_destroy(output->output);
5182 wl_list_remove(&output->link);
5183 free(output);
5184}
5185
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005186static void
5187display_destroy_output(struct display *d, uint32_t id)
5188{
5189 struct output *output;
5190
5191 wl_list_for_each(output, &d->output_list, link) {
5192 if (output->server_output_id == id) {
5193 output_destroy(output);
5194 break;
5195 }
5196 }
5197}
5198
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005199void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005200display_set_global_handler(struct display *display,
5201 display_global_handler_t handler)
5202{
5203 struct global *global;
5204
5205 display->global_handler = handler;
5206 if (!handler)
5207 return;
5208
5209 wl_list_for_each(global, &display->global_list, link)
5210 display->global_handler(display,
5211 global->name, global->interface,
5212 global->version, display->user_data);
5213}
5214
5215void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005216display_set_global_handler_remove(struct display *display,
5217 display_global_handler_t remove_handler)
5218{
5219 display->global_handler_remove = remove_handler;
5220 if (!remove_handler)
5221 return;
5222}
5223
5224void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005225display_set_output_configure_handler(struct display *display,
5226 display_output_handler_t handler)
5227{
5228 struct output *output;
5229
5230 display->output_configure_handler = handler;
5231 if (!handler)
5232 return;
5233
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005234 wl_list_for_each(output, &display->output_list, link) {
5235 if (output->allocation.width == 0 &&
5236 output->allocation.height == 0)
5237 continue;
5238
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005239 (*display->output_configure_handler)(output,
5240 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005241 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005242}
5243
5244void
5245output_set_user_data(struct output *output, void *data)
5246{
5247 output->user_data = data;
5248}
5249
5250void *
5251output_get_user_data(struct output *output)
5252{
5253 return output->user_data;
5254}
5255
5256void
5257output_set_destroy_handler(struct output *output,
5258 display_output_handler_t handler)
5259{
5260 output->destroy_handler = handler;
5261 /* FIXME: implement this, once we have way to remove outputs */
5262}
5263
5264void
Scott Moreau4e072362012-09-29 02:03:11 -06005265output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005266{
Scott Moreau4e072362012-09-29 02:03:11 -06005267 struct rectangle allocation = output->allocation;
5268
5269 switch (output->transform) {
5270 case WL_OUTPUT_TRANSFORM_90:
5271 case WL_OUTPUT_TRANSFORM_270:
5272 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5273 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5274 /* Swap width and height */
5275 allocation.width = output->allocation.height;
5276 allocation.height = output->allocation.width;
5277 break;
5278 }
5279
5280 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005281}
5282
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005283struct wl_output *
5284output_get_wl_output(struct output *output)
5285{
5286 return output->output;
5287}
5288
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005289enum wl_output_transform
5290output_get_transform(struct output *output)
5291{
5292 return output->transform;
5293}
5294
Alexander Larssonafd319a2013-05-22 14:41:27 +02005295uint32_t
5296output_get_scale(struct output *output)
5297{
5298 return output->scale;
5299}
5300
Jason Ekstrand738715d2014-04-02 19:53:50 -05005301const char *
5302output_get_make(struct output *output)
5303{
5304 return output->make;
5305}
5306
5307const char *
5308output_get_model(struct output *output)
5309{
5310 return output->model;
5311}
5312
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005313static void
Daniel Stone97f68542012-05-30 16:32:01 +01005314fini_xkb(struct input *input)
5315{
5316 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005317 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005318}
5319
5320static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005321display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005322{
5323 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005324 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005325
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005326 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005327 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005328 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005329 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005330 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005331 input->pointer_focus = NULL;
5332 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005333 input->seat_version = seat_version;
5334
Rusty Lynch041815a2013-08-08 21:20:38 -07005335 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005336 wl_list_insert(d->input_list.prev, &input->link);
5337
Daniel Stone37816df2012-05-16 18:45:18 +01005338 wl_seat_add_listener(input->seat, &seat_listener, input);
5339 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005340
Jason Ekstranda669bd52014-04-02 19:53:51 -05005341 if (d->data_device_manager) {
5342 input->data_device =
5343 wl_data_device_manager_get_data_device(d->data_device_manager,
5344 input->seat);
5345 wl_data_device_add_listener(input->data_device,
5346 &data_device_listener,
5347 input);
5348 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005349
5350 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005351 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005352
Derek Foreman118a4292015-04-22 17:23:35 -05005353 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5354 TFD_CLOEXEC | TFD_NONBLOCK);
5355 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5356 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005357 set_repeat_info(input, 40, 400);
5358
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005359 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5360 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005361 input->repeat_task.run = keyboard_repeat_func;
5362 display_watch_fd(d, input->repeat_timer_fd,
5363 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005364}
5365
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005366static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005367input_destroy(struct input *input)
5368{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005369 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005370 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005371
5372 if (input->drag_offer)
5373 data_offer_destroy(input->drag_offer);
5374
5375 if (input->selection_offer)
5376 data_offer_destroy(input->selection_offer);
5377
kabeer khan6ce67ec2014-10-20 11:55:29 +05305378 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005379 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305380 wl_data_device_release(input->data_device);
5381 else
5382 wl_data_device_destroy(input->data_device);
5383 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005384 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005385 if (input->touch)
5386 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005387 if (input->pointer)
5388 wl_pointer_release(input->pointer);
5389 if (input->keyboard)
5390 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005391 } else {
5392 if (input->touch)
5393 wl_touch_destroy(input->touch);
5394 if (input->pointer)
5395 wl_pointer_destroy(input->pointer);
5396 if (input->keyboard)
5397 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005398 }
5399
Daniel Stone97f68542012-05-30 16:32:01 +01005400 fini_xkb(input);
5401
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005402 wl_surface_destroy(input->pointer_surface);
5403
Pekka Paalanene1207c72011-12-16 12:02:09 +02005404 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005405 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005406 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005407 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005408 free(input);
5409}
5410
5411static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005412shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5413{
5414 struct display *d = data;
5415
5416 if (format == WL_SHM_FORMAT_RGB565)
5417 d->has_rgb565 = 1;
5418}
5419
5420struct wl_shm_listener shm_listener = {
5421 shm_format
5422};
5423
5424static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005425xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5426{
5427 xdg_shell_pong(shell, serial);
5428}
5429
5430static const struct xdg_shell_listener xdg_shell_listener = {
5431 xdg_shell_ping,
5432};
5433
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005434#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005435#ifdef static_assert
5436static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5437 "Interface version doesn't match implementation version");
5438#endif
5439
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005440static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005441registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5442 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005443{
5444 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005445 struct global *global;
5446
Brian Lovinbc919262013-08-07 15:34:59 -07005447 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005448 global->name = id;
5449 global->interface = strdup(interface);
5450 global->version = version;
5451 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005452
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005453 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005454 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005455 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005456 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005457 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005458 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005459 display_add_input(d, id, version);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005460 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005461 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005462 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005463 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005464 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005465 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005466 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305467 &wl_data_device_manager_interface,
5468 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005469 } else if (strcmp(interface, "xdg_shell") == 0) {
5470 d->xdg_shell = wl_registry_bind(registry, id,
5471 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005472 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005473 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005474 } else if (strcmp(interface, "text_cursor_position") == 0) {
5475 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005476 wl_registry_bind(registry, id,
5477 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005478 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5479 d->subcompositor =
5480 wl_registry_bind(registry, id,
5481 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005482 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005483 else if (strcmp(interface, "ivi_application") == 0) {
5484 d->ivi_application =
5485 wl_registry_bind(registry, id,
5486 &ivi_application_interface, 1);
5487 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005488
5489 if (d->global_handler)
5490 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005491}
5492
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005493static void
5494registry_handle_global_remove(void *data, struct wl_registry *registry,
5495 uint32_t name)
5496{
5497 struct display *d = data;
5498 struct global *global;
5499 struct global *tmp;
5500
5501 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5502 if (global->name != name)
5503 continue;
5504
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005505 if (strcmp(global->interface, "wl_output") == 0)
5506 display_destroy_output(d, name);
5507
5508 /* XXX: Should destroy remaining bound globals */
5509
5510 if (d->global_handler_remove)
5511 d->global_handler_remove(d, name, global->interface,
5512 global->version, d->user_data);
5513
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005514 wl_list_remove(&global->link);
5515 free(global->interface);
5516 free(global);
5517 }
5518}
5519
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005520void *
5521display_bind(struct display *display, uint32_t name,
5522 const struct wl_interface *interface, uint32_t version)
5523{
5524 return wl_registry_bind(display->registry, name, interface, version);
5525}
5526
5527static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005528 registry_handle_global,
5529 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005530};
5531
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005532#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005533static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005534init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005535{
5536 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005537 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005538
Rob Clark6396ed32012-03-11 19:48:41 -05005539#ifdef USE_CAIRO_GLESV2
5540# define GL_BIT EGL_OPENGL_ES2_BIT
5541#else
5542# define GL_BIT EGL_OPENGL_BIT
5543#endif
5544
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005545 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005546 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005547 EGL_RED_SIZE, 1,
5548 EGL_GREEN_SIZE, 1,
5549 EGL_BLUE_SIZE, 1,
5550 EGL_ALPHA_SIZE, 1,
5551 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005552 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005553 EGL_NONE
5554 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005555
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005556#ifdef USE_CAIRO_GLESV2
5557 static const EGLint context_attribs[] = {
5558 EGL_CONTEXT_CLIENT_VERSION, 2,
5559 EGL_NONE
5560 };
5561 EGLint api = EGL_OPENGL_ES_API;
5562#else
5563 EGLint *context_attribs = NULL;
5564 EGLint api = EGL_OPENGL_API;
5565#endif
5566
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005567 d->dpy =
5568 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5569 d->display, NULL);
5570
Yuval Fledel45568f62010-12-06 09:18:12 -05005571 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005572 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005573 return -1;
5574 }
5575
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005576 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005577 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005578 return -1;
5579 }
5580
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005581 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5582 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005583 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005584 return -1;
5585 }
5586
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005587 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005588 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005589 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005590 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005591 return -1;
5592 }
5593
Benjamin Franzke0c991632011-09-27 21:57:31 +02005594 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5595 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005596 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005597 return -1;
5598 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005599
5600 return 0;
5601}
5602
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005603static void
5604fini_egl(struct display *display)
5605{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005606 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005607
5608 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5609 EGL_NO_CONTEXT);
5610
5611 eglTerminate(display->dpy);
5612 eglReleaseThread();
5613}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005614#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005615
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005616static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005617init_dummy_surface(struct display *display)
5618{
5619 int len;
5620 void *data;
5621
5622 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005623 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005624 display->dummy_surface =
5625 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5626 1, 1, len);
5627 display->dummy_surface_data = data;
5628}
5629
5630static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005631handle_display_data(struct task *task, uint32_t events)
5632{
5633 struct display *display =
5634 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005635 struct epoll_event ep;
5636 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005637
5638 display->display_fd_events = events;
5639
5640 if (events & EPOLLERR || events & EPOLLHUP) {
5641 display_exit(display);
5642 return;
5643 }
5644
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005645 if (events & EPOLLIN) {
5646 ret = wl_display_dispatch(display->display);
5647 if (ret == -1) {
5648 display_exit(display);
5649 return;
5650 }
5651 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005652
5653 if (events & EPOLLOUT) {
5654 ret = wl_display_flush(display->display);
5655 if (ret == 0) {
5656 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5657 ep.data.ptr = &display->display_task;
5658 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5659 display->display_fd, &ep);
5660 } else if (ret == -1 && errno != EAGAIN) {
5661 display_exit(display);
5662 return;
5663 }
5664 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005665}
5666
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005667static void
5668log_handler(const char *format, va_list args)
5669{
5670 vfprintf(stderr, format, args);
5671}
5672
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005673struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005674display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005675{
5676 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005677
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005678 wl_log_set_handler_client(log_handler);
5679
Peter Huttererf3d62272013-08-08 11:57:05 +10005680 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005681 if (d == NULL)
5682 return NULL;
5683
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005684 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005685 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005686 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005687 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005688 return NULL;
5689 }
5690
Rob Bradford5ab9c752013-07-26 16:29:43 +01005691 d->xkb_context = xkb_context_new(0);
5692 if (d->xkb_context == NULL) {
5693 fprintf(stderr, "Failed to create XKB context\n");
5694 free(d);
5695 return NULL;
5696 }
5697
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005698 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005699 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005700 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005701 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5702 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005703
5704 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005705 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005706 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005707 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005708
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005709 d->registry = wl_display_get_registry(d->display);
5710 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005711
Marek Chalupaa519d062014-12-05 13:49:40 +01005712 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005713 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5714 return NULL;
5715 }
5716
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005717#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005718 if (init_egl(d) < 0)
5719 fprintf(stderr, "EGL does not seem to work, "
5720 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005721#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005722
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005723 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005724
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005725 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005726
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005727 wl_list_init(&d->window_list);
5728
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005729 init_dummy_surface(d);
5730
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005731 return d;
5732}
5733
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005734static void
5735display_destroy_outputs(struct display *display)
5736{
5737 struct output *tmp;
5738 struct output *output;
5739
5740 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5741 output_destroy(output);
5742}
5743
Pekka Paalanene1207c72011-12-16 12:02:09 +02005744static void
5745display_destroy_inputs(struct display *display)
5746{
5747 struct input *tmp;
5748 struct input *input;
5749
5750 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5751 input_destroy(input);
5752}
5753
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005754void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005755display_destroy(struct display *display)
5756{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005757 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005758 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5759 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005760
5761 if (!wl_list_empty(&display->deferred_list))
5762 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5763
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005764 cairo_surface_destroy(display->dummy_surface);
5765 free(display->dummy_surface_data);
5766
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005767 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005768 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005769
Daniel Stone97f68542012-05-30 16:32:01 +01005770 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005771
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005772 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005773 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005774
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005775#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005776 if (display->argb_device)
5777 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005778#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005779
Pekka Paalanen35e82632013-04-25 13:57:48 +03005780 if (display->subcompositor)
5781 wl_subcompositor_destroy(display->subcompositor);
5782
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005783 if (display->xdg_shell)
5784 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005785
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005786 if (display->ivi_application)
5787 ivi_application_destroy(display->ivi_application);
5788
Pekka Paalanenc2052982011-12-16 11:41:32 +02005789 if (display->shm)
5790 wl_shm_destroy(display->shm);
5791
5792 if (display->data_device_manager)
5793 wl_data_device_manager_destroy(display->data_device_manager);
5794
5795 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005796 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005797
5798 close(display->epoll_fd);
5799
U. Artie Eoff44874d92012-10-02 21:12:35 -07005800 if (!(display->display_fd_events & EPOLLERR) &&
5801 !(display->display_fd_events & EPOLLHUP))
5802 wl_display_flush(display->display);
5803
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005804 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005805 free(display);
5806}
5807
5808void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005809display_set_user_data(struct display *display, void *data)
5810{
5811 display->user_data = data;
5812}
5813
5814void *
5815display_get_user_data(struct display *display)
5816{
5817 return display->user_data;
5818}
5819
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005820struct wl_display *
5821display_get_display(struct display *display)
5822{
5823 return display->display;
5824}
5825
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005826int
5827display_has_subcompositor(struct display *display)
5828{
5829 if (display->subcompositor)
5830 return 1;
5831
5832 wl_display_roundtrip(display->display);
5833
5834 return display->subcompositor != NULL;
5835}
5836
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005837cairo_device_t *
5838display_get_cairo_device(struct display *display)
5839{
5840 return display->argb_device;
5841}
5842
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005843struct output *
5844display_get_output(struct display *display)
5845{
5846 return container_of(display->output_list.next, struct output, link);
5847}
5848
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005849struct wl_compositor *
5850display_get_compositor(struct display *display)
5851{
5852 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005853}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005854
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005855uint32_t
5856display_get_serial(struct display *display)
5857{
5858 return display->serial;
5859}
5860
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005861EGLDisplay
5862display_get_egl_display(struct display *d)
5863{
5864 return d->dpy;
5865}
5866
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005867struct wl_data_source *
5868display_create_data_source(struct display *display)
5869{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005870 if (display->data_device_manager)
5871 return wl_data_device_manager_create_data_source(display->data_device_manager);
5872 else
5873 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005874}
5875
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005876EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005877display_get_argb_egl_config(struct display *d)
5878{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005879 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005880}
5881
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005882int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005883display_acquire_window_surface(struct display *display,
5884 struct window *window,
5885 EGLContext ctx)
5886{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005887 struct surface *surface = window->main_surface;
5888
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005889 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005890 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005891
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005892 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005893 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005894}
5895
5896void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005897display_release_window_surface(struct display *display,
5898 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005899{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005900 struct surface *surface = window->main_surface;
5901
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005902 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005903 return;
5904
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005905 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005906}
5907
5908void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005909display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005910{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005911 wl_list_insert(&display->deferred_list, &task->link);
5912}
5913
5914void
5915display_watch_fd(struct display *display,
5916 int fd, uint32_t events, struct task *task)
5917{
5918 struct epoll_event ep;
5919
5920 ep.events = events;
5921 ep.data.ptr = task;
5922 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5923}
5924
5925void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005926display_unwatch_fd(struct display *display, int fd)
5927{
5928 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5929}
5930
5931void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005932display_run(struct display *display)
5933{
5934 struct task *task;
5935 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005936 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005937
Pekka Paalanen826d7952011-12-15 10:14:07 +02005938 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005939 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005940 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005941 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005942 struct task, link);
5943 wl_list_remove(&task->link);
5944 task->run(task, 0);
5945 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005946
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005947 wl_display_dispatch_pending(display->display);
5948
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005949 if (!display->running)
5950 break;
5951
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005952 ret = wl_display_flush(display->display);
5953 if (ret < 0 && errno == EAGAIN) {
5954 ep[0].events =
5955 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5956 ep[0].data.ptr = &display->display_task;
5957
5958 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5959 display->display_fd, &ep[0]);
5960 } else if (ret < 0) {
5961 break;
5962 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005963
5964 count = epoll_wait(display->epoll_fd,
5965 ep, ARRAY_LENGTH(ep), -1);
5966 for (i = 0; i < count; i++) {
5967 task = ep[i].data.ptr;
5968 task->run(task, ep[i].events);
5969 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005970 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005971}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005972
5973void
5974display_exit(struct display *display)
5975{
5976 display->running = 0;
5977}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005978
Carlos Garnacho9c931792016-01-18 23:52:12 +01005979int
5980display_get_data_device_manager_version(struct display *display)
5981{
5982 return display->data_device_manager_version;
5983}
5984
Jan Arne Petersencd997062012-11-18 19:06:44 +01005985void
5986keysym_modifiers_add(struct wl_array *modifiers_map,
5987 const char *name)
5988{
5989 size_t len = strlen(name) + 1;
5990 char *p;
5991
5992 p = wl_array_add(modifiers_map, len);
5993
5994 if (p == NULL)
5995 return;
5996
5997 strncpy(p, name, len);
5998}
5999
6000static xkb_mod_index_t
6001keysym_modifiers_get_index(struct wl_array *modifiers_map,
6002 const char *name)
6003{
6004 xkb_mod_index_t index = 0;
6005 char *p = modifiers_map->data;
6006
6007 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6008 if (strcmp(p, name) == 0)
6009 return index;
6010
6011 index++;
6012 p += strlen(p) + 1;
6013 }
6014
6015 return XKB_MOD_INVALID;
6016}
6017
6018xkb_mod_mask_t
6019keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6020 const char *name)
6021{
6022 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6023
6024 if (index == XKB_MOD_INVALID)
6025 return XKB_MOD_INVALID;
6026
6027 return 1 << index;
6028}
Kristian Høgsbergce278412013-07-25 15:20:20 -07006029
6030void *
6031fail_on_null(void *p)
6032{
6033 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10006034 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07006035 exit(EXIT_FAILURE);
6036 }
6037
6038 return p;
6039}
6040
6041void *
6042xmalloc(size_t s)
6043{
6044 return fail_on_null(malloc(s));
6045}
6046
Peter Huttererf3d62272013-08-08 11:57:05 +10006047void *
6048xzalloc(size_t s)
6049{
6050 return fail_on_null(zalloc(s));
6051}
6052
Kristian Høgsbergce278412013-07-25 15:20:20 -07006053char *
6054xstrdup(const char *s)
6055{
6056 return fail_on_null(strdup(s));
6057}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08006058
6059void *
6060xrealloc(char *p, size_t s)
6061{
6062 return fail_on_null(realloc(p, s));
6063}