blob: ced867ef56326e8499836b3cd2cb775e6d51a66f [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"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080071#include "shared/zalloc.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080072#include "xdg-shell-unstable-v5-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060073#include "text-cursor-position-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070074#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050075
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050076#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050077
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090078#include <sys/types.h>
79#include "ivi-application-client-protocol.h"
80#define IVI_SURFACE_ID 9000
81
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070082struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030083
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040084struct global {
85 uint32_t name;
86 char *interface;
87 uint32_t version;
88 struct wl_list link;
89};
90
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050091struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050092 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040093 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050094 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030095 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040096 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040097 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060098 struct text_cursor_position *text_cursor_position;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050099 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900100 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400101 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500102 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200103 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200104 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400105 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400106
107 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700108 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400109 struct task display_task;
110
111 int epoll_fd;
112 struct wl_list deferred_list;
113
Pekka Paalanen826d7952011-12-15 10:14:07 +0200114 int running;
115
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400116 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400117 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400118 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500119 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400120
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400121 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400122
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300123 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300124 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400125
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200126 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400127 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800128 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200129
130 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100131
132 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200133
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200134 /* A hack to get text extents for tooltips */
135 cairo_surface_t *dummy_surface;
136 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200137
138 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530139 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500140};
141
Rob Bradford7507b572012-05-15 17:55:34 +0100142struct window_output {
143 struct output *output;
144 struct wl_list link;
145};
146
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200147struct toysurface {
148 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500149 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800150 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200152 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200153 * If flags has SURFACE_HINT_RESIZE set, the user is
154 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200155 * Returns the Cairo surface to draw to.
156 */
157 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200158 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200159 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160
161 /*
162 * Post the surface to the server, returning the server allocation
163 * rectangle. The Cairo surface from prepare() must be destroyed
164 * after calling this.
165 */
166 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200167 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200168 struct rectangle *server_allocation);
169
170 /*
171 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800172 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200173 */
174 int (*acquire)(struct toysurface *base, EGLContext ctx);
175
176 /*
177 * Release the toysurface from the EGL context, returning control
178 * to Cairo.
179 */
180 void (*release)(struct toysurface *base);
181
182 /*
183 * Destroy the toysurface, including the Cairo surface, any
184 * backing storage, and the Wayland protocol objects.
185 */
186 void (*destroy)(struct toysurface *base);
187};
188
Pekka Paalanen4e373742013-02-13 16:17:13 +0200189struct surface {
190 struct window *window;
191
192 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300193 struct wl_subsurface *subsurface;
194 int synchronized;
195 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200196 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200197 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300198 int redraw_needed;
199 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300200 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200201
202 struct rectangle allocation;
203 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200204
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200205 struct wl_region *input_region;
206 struct wl_region *opaque_region;
207
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200208 enum window_buffer_type buffer_type;
209 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200210 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200211
212 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300213
214 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200215};
216
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500217struct window {
218 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100219 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500220 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200221 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400222 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500223 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200224 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500225 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400226 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300227 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400228 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400229 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500230 int custom;
231 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400232
Pekka Paalanen99436862012-11-19 17:15:59 +0200233 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400234
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500235 int fullscreen;
236 int maximized;
237
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200238 enum preferred_format preferred_format;
239
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500240 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500241 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400242 window_data_handler_t data_handler;
243 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500244 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400245 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200246 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700247 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400248
Pekka Paalanen4e373742013-02-13 16:17:13 +0200249 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500250 struct xdg_surface *xdg_surface;
251 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300252
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700253 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800254 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500255
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900256 struct ivi_surface *ivi_surface;
257
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500258 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500259
Pekka Paalanen35e82632013-04-25 13:57:48 +0300260 /* struct surface::link, contains also main_surface */
261 struct wl_list subsurface_list;
262
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500263 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400264 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500265};
266
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500267struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500268 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200269 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300270 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500271 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400272 struct wl_list link;
273 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500274 widget_resize_handler_t resize_handler;
275 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500276 widget_enter_handler_t enter_handler;
277 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500278 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500279 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700280 widget_touch_down_handler_t touch_down_handler;
281 widget_touch_up_handler_t touch_up_handler;
282 widget_touch_motion_handler_t touch_motion_handler;
283 widget_touch_frame_handler_t touch_frame_handler;
284 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200285 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000286 widget_pointer_frame_handler_t pointer_frame_handler;
287 widget_axis_source_handler_t axis_source_handler;
288 widget_axis_stop_handler_t axis_stop_handler;
289 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400290 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500291 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300292 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500293 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000294 /* If this is set to false then no cairo surface will be
295 * created before redrawing the surface. This is useful if the
296 * redraw handler is going to do completely custom rendering
297 * such as using EGL directly */
298 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400299};
300
Rusty Lynch041815a2013-08-08 21:20:38 -0700301struct touch_point {
302 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800303 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 struct widget *widget;
305 struct wl_list link;
306};
307
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400308struct input {
309 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100310 struct wl_seat *seat;
311 struct wl_pointer *pointer;
312 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700313 struct wl_touch *touch;
314 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400315 struct window *pointer_focus;
316 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700317 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300318 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300319 uint32_t cursor_anim_start;
320 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500321 uint32_t cursor_timer_start;
322 uint32_t cursor_anim_current;
323 int cursor_delay_fd;
324 bool cursor_timer_running;
325 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300326 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400327 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400328 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400329 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400330 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400331 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400332
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500333 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500334 struct widget *grab;
335 uint32_t grab_button;
336
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400337 struct wl_data_device *data_device;
338 struct data_offer *drag_offer;
339 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800340 uint32_t touch_grab;
341 int32_t touch_grab_id;
342 float drag_x, drag_y;
343 struct window *drag_focus;
344 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100345
346 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100347 struct xkb_keymap *keymap;
348 struct xkb_state *state;
349 xkb_mod_mask_t control_mask;
350 xkb_mod_mask_t alt_mask;
351 xkb_mod_mask_t shift_mask;
352 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400353
Jonny Lamb06959082014-08-12 14:58:27 +0200354 int32_t repeat_rate_sec;
355 int32_t repeat_rate_nsec;
356 int32_t repeat_delay_sec;
357 int32_t repeat_delay_nsec;
358
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400359 struct task repeat_task;
360 int repeat_timer_fd;
361 uint32_t repeat_sym;
362 uint32_t repeat_key;
363 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500364 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400365};
366
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500367struct output {
368 struct display *display;
369 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800370 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500371 struct rectangle allocation;
372 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600373 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200374 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500375 char *make;
376 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200377
378 display_output_handler_t destroy_handler;
379 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500380};
381
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500382struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500383 struct widget *widget;
384 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500385 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800386
387 uint32_t last_time;
388 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800389 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500390};
391
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500392struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400393 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500394 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500395 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500396 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700397 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500398 const char **entries;
399 uint32_t time;
400 int current;
401 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400402 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500403 menu_func_t func;
404};
405
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300406struct tooltip {
407 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300408 struct widget *widget;
409 char *entry;
410 struct task tooltip_task;
411 int tooltip_fd;
412 float x, y;
413};
414
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700415struct shm_pool {
416 struct wl_shm_pool *pool;
417 size_t size;
418 size_t used;
419 void *data;
420};
421
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400422enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300423 CURSOR_DEFAULT = 100,
424 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400425};
426
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200427static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400428
Pekka Paalanen97777442013-05-22 10:20:05 +0300429/* #define DEBUG */
430
431#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300432
433static void
434debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435__attribute__ ((format (printf, 4, 5)));
436
437static void
438debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
439{
440 va_list ap;
441 struct timeval tv;
442
443 gettimeofday(&tv, NULL);
444 fprintf(stderr, "%8ld.%03ld ",
445 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
446
447 if (proxy)
448 fprintf(stderr, "%s@%d ",
449 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
450
451 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
452 fprintf(stderr, "%s ", func);
453
454 va_start(ap, fmt);
455 vfprintf(stderr, fmt, ap);
456 va_end(ap);
457}
458
459#define DBG(fmt, ...) \
460 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
461
462#define DBG_OBJ(obj, fmt, ...) \
463 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
464
465#else
466
467#define DBG(...) do {} while (0)
468#define DBG_OBJ(...) do {} while (0)
469
470#endif
471
Alexander Larsson1818e312013-05-22 14:41:31 +0200472static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200473surface_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 +0200474{
475 int32_t tmp;
476
477 switch (buffer_transform) {
478 case WL_OUTPUT_TRANSFORM_90:
479 case WL_OUTPUT_TRANSFORM_270:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
481 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
482 tmp = *width;
483 *width = *height;
484 *height = tmp;
485 break;
486 default:
487 break;
488 }
489
490 *width *= buffer_scale;
491 *height *= buffer_scale;
492}
493
494static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200495buffer_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 +0200496{
497 int32_t tmp;
498
499 switch (buffer_transform) {
500 case WL_OUTPUT_TRANSFORM_90:
501 case WL_OUTPUT_TRANSFORM_270:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
503 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
504 tmp = *width;
505 *width = *height;
506 *height = tmp;
507 break;
508 default:
509 break;
510 }
511
512 *width /= buffer_scale;
513 *height /= buffer_scale;
514}
515
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500516#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400517
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518struct egl_window_surface {
519 struct toysurface base;
520 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521 struct display *display;
522 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 struct wl_egl_window *egl_window;
524 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525};
526
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200527static struct egl_window_surface *
528to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100529{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100531}
532
533static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200535 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200536 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100537{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538 struct egl_window_surface *surface = to_egl_window_surface(base);
539
Alexander Larsson1818e312013-05-22 14:41:31 +0200540 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
541
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200542 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
543 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
544
545 return cairo_surface_reference(surface->cairo_surface);
546}
547
548static void
549egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200550 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551 struct rectangle *server_allocation)
552{
553 struct egl_window_surface *surface = to_egl_window_surface(base);
554
555 cairo_gl_surface_swapbuffers(surface->cairo_surface);
556 wl_egl_window_get_attached_size(surface->egl_window,
557 &server_allocation->width,
558 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200559
560 buffer_to_surface_size (buffer_transform, buffer_scale,
561 &server_allocation->width,
562 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200563}
564
565static int
566egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
567{
568 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200569 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100570
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200571 device = cairo_surface_get_device(surface->cairo_surface);
572 if (!device)
573 return -1;
574
575 if (!ctx) {
576 if (device == surface->display->argb_device)
577 ctx = surface->display->argb_ctx;
578 else
579 assert(0);
580 }
581
582 cairo_device_flush(device);
583 cairo_device_acquire(device);
584 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
585 surface->egl_surface, ctx))
586 fprintf(stderr, "failed to make surface current\n");
587
588 return 0;
589}
590
591static void
592egl_window_surface_release(struct toysurface *base)
593{
594 struct egl_window_surface *surface = to_egl_window_surface(base);
595 cairo_device_t *device;
596
597 device = cairo_surface_get_device(surface->cairo_surface);
598 if (!device)
599 return;
600
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200601 if (!eglMakeCurrent(surface->display->dpy,
602 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200603 fprintf(stderr, "failed to make context current\n");
604
605 cairo_device_release(device);
606}
607
608static void
609egl_window_surface_destroy(struct toysurface *base)
610{
611 struct egl_window_surface *surface = to_egl_window_surface(base);
612 struct display *d = surface->display;
613
614 cairo_surface_destroy(surface->cairo_surface);
615 eglDestroySurface(d->dpy, surface->egl_surface);
616 wl_egl_window_destroy(surface->egl_window);
617 surface->surface = NULL;
618
619 free(surface);
620}
621
622static struct toysurface *
623egl_window_surface_create(struct display *display,
624 struct wl_surface *wl_surface,
625 uint32_t flags,
626 struct rectangle *rectangle)
627{
628 struct egl_window_surface *surface;
629
Pekka Paalanenb3627362012-11-19 17:16:00 +0200630 if (display->dpy == EGL_NO_DISPLAY)
631 return NULL;
632
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800633 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200634 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635 return NULL;
636
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200637 surface->base.prepare = egl_window_surface_prepare;
638 surface->base.swap = egl_window_surface_swap;
639 surface->base.acquire = egl_window_surface_acquire;
640 surface->base.release = egl_window_surface_release;
641 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200643 surface->display = display;
644 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400645
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200646 surface->egl_window = wl_egl_window_create(surface->surface,
647 rectangle->width,
648 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100650 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100651 weston_platform_create_egl_surface(display->dpy,
652 display->argb_config,
653 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100654
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200655 surface->cairo_surface =
656 cairo_gl_surface_create_for_egl(display->argb_device,
657 surface->egl_surface,
658 rectangle->width,
659 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100660
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200661 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100662}
663
Pekka Paalanenb3627362012-11-19 17:16:00 +0200664#else
665
666static struct toysurface *
667egl_window_surface_create(struct display *display,
668 struct wl_surface *wl_surface,
669 uint32_t flags,
670 struct rectangle *rectangle)
671{
672 return NULL;
673}
674
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675#endif
676
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200677struct shm_surface_data {
678 struct wl_buffer *buffer;
679 struct shm_pool *pool;
680};
681
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400682struct wl_buffer *
683display_get_buffer_for_surface(struct display *display,
684 cairo_surface_t *surface)
685{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200686 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400687
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200688 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689
690 return data->buffer;
691}
692
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500693static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700694shm_pool_destroy(struct shm_pool *pool);
695
696static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400697shm_surface_data_destroy(void *p)
698{
699 struct shm_surface_data *data = p;
700
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200701 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700702 if (data->pool)
703 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300704
705 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400706}
707
Kristian Høgsberg16626282012-04-03 11:21:27 -0400708static struct wl_shm_pool *
709make_shm_pool(struct display *display, int size, void **data)
710{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400711 struct wl_shm_pool *pool;
712 int fd;
713
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300714 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400715 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300716 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
717 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400718 return NULL;
719 }
720
721 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400722 if (*data == MAP_FAILED) {
723 fprintf(stderr, "mmap failed: %m\n");
724 close(fd);
725 return NULL;
726 }
727
728 pool = wl_shm_create_pool(display->shm, fd, size);
729
730 close(fd);
731
732 return pool;
733}
734
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700735static struct shm_pool *
736shm_pool_create(struct display *display, size_t size)
737{
738 struct shm_pool *pool = malloc(sizeof *pool);
739
740 if (!pool)
741 return NULL;
742
743 pool->pool = make_shm_pool(display, size, &pool->data);
744 if (!pool->pool) {
745 free(pool);
746 return NULL;
747 }
748
749 pool->size = size;
750 pool->used = 0;
751
752 return pool;
753}
754
755static void *
756shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
757{
758 if (pool->used + size > pool->size)
759 return NULL;
760
761 *offset = pool->used;
762 pool->used += size;
763
764 return (char *) pool->data + *offset;
765}
766
767/* destroy the pool. this does not unmap the memory though */
768static void
769shm_pool_destroy(struct shm_pool *pool)
770{
771 munmap(pool->data, pool->size);
772 wl_shm_pool_destroy(pool->pool);
773 free(pool);
774}
775
776/* Start allocating from the beginning of the pool again */
777static void
778shm_pool_reset(struct shm_pool *pool)
779{
780 pool->used = 0;
781}
782
783static int
784data_length_for_shm_surface(struct rectangle *rect)
785{
786 int stride;
787
788 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
789 rect->width);
790 return stride * rect->height;
791}
792
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500793static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700794display_create_shm_surface_from_pool(struct display *display,
795 struct rectangle *rectangle,
796 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797{
798 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400799 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400800 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200801 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700802 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400803 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804
805 data = malloc(sizeof *data);
806 if (data == NULL)
807 return NULL;
808
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200809 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
810 cairo_format = CAIRO_FORMAT_RGB16_565;
811 else
812 cairo_format = CAIRO_FORMAT_ARGB32;
813
814 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700815 length = stride * rectangle->height;
816 data->pool = NULL;
817 map = shm_pool_allocate(pool, length, &offset);
818
819 if (!map) {
820 free(data);
821 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400822 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400824 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200825 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400826 rectangle->width,
827 rectangle->height,
828 stride);
829
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200830 cairo_surface_set_user_data(surface, &shm_surface_data_key,
831 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400832
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200833 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
834 format = WL_SHM_FORMAT_RGB565;
835 else {
836 if (flags & SURFACE_OPAQUE)
837 format = WL_SHM_FORMAT_XRGB8888;
838 else
839 format = WL_SHM_FORMAT_ARGB8888;
840 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400841
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200842 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
843 rectangle->width,
844 rectangle->height,
845 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400846
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700847 return surface;
848}
849
850static cairo_surface_t *
851display_create_shm_surface(struct display *display,
852 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200853 struct shm_pool *alternate_pool,
854 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700855{
856 struct shm_surface_data *data;
857 struct shm_pool *pool;
858 cairo_surface_t *surface;
859
Pekka Paalanen99436862012-11-19 17:15:59 +0200860 if (alternate_pool) {
861 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700862 surface = display_create_shm_surface_from_pool(display,
863 rectangle,
864 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200865 alternate_pool);
866 if (surface) {
867 data = cairo_surface_get_user_data(surface,
868 &shm_surface_data_key);
869 goto out;
870 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700871 }
872
873 pool = shm_pool_create(display,
874 data_length_for_shm_surface(rectangle));
875 if (!pool)
876 return NULL;
877
878 surface =
879 display_create_shm_surface_from_pool(display, rectangle,
880 flags, pool);
881
882 if (!surface) {
883 shm_pool_destroy(pool);
884 return NULL;
885 }
886
887 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200888 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700889 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890
Pekka Paalanen99436862012-11-19 17:15:59 +0200891out:
892 if (data_ret)
893 *data_ret = data;
894
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400895 return surface;
896}
897
nobled7b87cb02011-02-01 18:51:47 +0000898static int
899check_size(struct rectangle *rect)
900{
901 if (rect->width && rect->height)
902 return 0;
903
904 fprintf(stderr, "tried to create surface of "
905 "width: %d, height: %d\n", rect->width, rect->height);
906 return -1;
907}
908
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400909cairo_surface_t *
910display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100911 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400912 struct rectangle *rectangle,
913 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400914{
nobled7b87cb02011-02-01 18:51:47 +0000915 if (check_size(rectangle) < 0)
916 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200917
918 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200919 return display_create_shm_surface(display, rectangle, flags,
920 NULL, NULL);
921}
922
Pekka Paalanena4eda732012-11-19 17:16:02 +0200923struct shm_surface_leaf {
924 cairo_surface_t *cairo_surface;
925 /* 'data' is automatically destroyed, when 'cairo_surface' is */
926 struct shm_surface_data *data;
927
928 struct shm_pool *resize_pool;
929 int busy;
930};
931
932static void
933shm_surface_leaf_release(struct shm_surface_leaf *leaf)
934{
935 if (leaf->cairo_surface)
936 cairo_surface_destroy(leaf->cairo_surface);
937 /* leaf->data already destroyed via cairo private */
938
939 if (leaf->resize_pool)
940 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200941
942 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200943}
944
Pekka Paalanenaef02542013-04-25 13:57:47 +0300945#define MAX_LEAVES 3
946
Pekka Paalanen99436862012-11-19 17:15:59 +0200947struct shm_surface {
948 struct toysurface base;
949 struct display *display;
950 struct wl_surface *surface;
951 uint32_t flags;
952 int dx, dy;
953
Pekka Paalanenaef02542013-04-25 13:57:47 +0300954 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200955 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200956};
957
958static struct shm_surface *
959to_shm_surface(struct toysurface *base)
960{
961 return container_of(base, struct shm_surface, base);
962}
963
Pekka Paalanena4eda732012-11-19 17:16:02 +0200964static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300965shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
966{
967#ifdef DEBUG
968 struct shm_surface_leaf *leaf;
969 char bufs[MAX_LEAVES + 1];
970 int i;
971
972 for (i = 0; i < MAX_LEAVES; i++) {
973 leaf = &surface->leaf[i];
974
975 if (leaf->busy)
976 bufs[i] = 'b';
977 else if (leaf->cairo_surface)
978 bufs[i] = 'a';
979 else
980 bufs[i] = ' ';
981 }
982
983 bufs[MAX_LEAVES] = '\0';
984 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
985#endif
986}
987
988static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200989shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
990{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200991 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300992 struct shm_surface_leaf *leaf;
993 int i;
994 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300995
996 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200997
Pekka Paalanenaef02542013-04-25 13:57:47 +0300998 for (i = 0; i < MAX_LEAVES; i++) {
999 leaf = &surface->leaf[i];
1000 if (leaf->data && leaf->data->buffer == buffer) {
1001 leaf->busy = 0;
1002 break;
1003 }
1004 }
1005 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001006
Pekka Paalanenaef02542013-04-25 13:57:47 +03001007 /* Leave one free leaf with storage, release others */
1008 free_found = 0;
1009 for (i = 0; i < MAX_LEAVES; i++) {
1010 leaf = &surface->leaf[i];
1011
1012 if (!leaf->cairo_surface || leaf->busy)
1013 continue;
1014
1015 if (!free_found)
1016 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001017 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001018 shm_surface_leaf_release(leaf);
1019 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001020
Pekka Paalanen97777442013-05-22 10:20:05 +03001021 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001022}
1023
1024static const struct wl_buffer_listener shm_surface_buffer_listener = {
1025 shm_surface_buffer_release
1026};
1027
Pekka Paalanen99436862012-11-19 17:15:59 +02001028static cairo_surface_t *
1029shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001030 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001031 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001032{
Pekka Paalanenec076692012-11-30 13:37:27 +02001033 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001034 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001035 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 struct shm_surface_leaf *leaf = NULL;
1037 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001038
1039 surface->dx = dx;
1040 surface->dy = dy;
1041
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001042 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001043 for (i = 0; i < MAX_LEAVES; i++) {
1044 if (surface->leaf[i].busy)
1045 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001046
Pekka Paalanenaef02542013-04-25 13:57:47 +03001047 if (!leaf || surface->leaf[i].cairo_surface)
1048 leaf = &surface->leaf[i];
1049 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001050 DBG_OBJ(surface->surface, "pick leaf %d\n",
1051 (int)(leaf - &surface->leaf[0]));
1052
Pekka Paalanenaef02542013-04-25 13:57:47 +03001053 if (!leaf) {
1054 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001056 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001058 }
1059
Pekka Paalanena4eda732012-11-19 17:16:02 +02001060 if (!resize_hint && leaf->resize_pool) {
1061 cairo_surface_destroy(leaf->cairo_surface);
1062 leaf->cairo_surface = NULL;
1063 shm_pool_destroy(leaf->resize_pool);
1064 leaf->resize_pool = NULL;
1065 }
1066
Alexander Larsson1818e312013-05-22 14:41:31 +02001067 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1068
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 if (leaf->cairo_surface &&
1070 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1071 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001072 goto out;
1073
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 if (leaf->cairo_surface)
1075 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001077#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001078 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001079 /* Create a big pool to allocate from, while continuously
1080 * resizing. Mmapping a new pool in the server
1081 * is relatively expensive, so reusing a pool performs
1082 * better, but may temporarily reserve unneeded memory.
1083 */
1084 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001085 leaf->resize_pool = shm_pool_create(surface->display,
1086 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001087 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001088#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001089
Alexander Larsson1818e312013-05-22 14:41:31 +02001090 rect.width = width;
1091 rect.height = height;
1092
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001094 display_create_shm_surface(surface->display, &rect,
1095 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 leaf->resize_pool,
1097 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001098 if (!leaf->cairo_surface)
1099 return NULL;
1100
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001102 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103
1104out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 surface->current = leaf;
1106
1107 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001108}
1109
1110static void
1111shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001112 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001113 struct rectangle *server_allocation)
1114{
1115 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001117
1118 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001119 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001120 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001121 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001122
Alexander Larsson1818e312013-05-22 14:41:31 +02001123 buffer_to_surface_size (buffer_transform, buffer_scale,
1124 &server_allocation->width,
1125 &server_allocation->height);
1126
Pekka Paalanena4eda732012-11-19 17:16:02 +02001127 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001128 surface->dx, surface->dy);
1129 wl_surface_damage(surface->surface, 0, 0,
1130 server_allocation->width, server_allocation->height);
1131 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001132
Pekka Paalanen71233882013-04-25 13:57:53 +03001133 DBG_OBJ(surface->surface, "leaf %d busy\n",
1134 (int)(leaf - &surface->leaf[0]));
1135
Pekka Paalanena4eda732012-11-19 17:16:02 +02001136 leaf->busy = 1;
1137 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001138}
1139
1140static int
1141shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1142{
1143 return -1;
1144}
1145
1146static void
1147shm_surface_release(struct toysurface *base)
1148{
1149}
1150
1151static void
1152shm_surface_destroy(struct toysurface *base)
1153{
1154 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001155 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001156
Pekka Paalanenaef02542013-04-25 13:57:47 +03001157 for (i = 0; i < MAX_LEAVES; i++)
1158 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001159
1160 free(surface);
1161}
1162
1163static struct toysurface *
1164shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1165 uint32_t flags, struct rectangle *rectangle)
1166{
1167 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001168 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001169
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001170 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001171 surface->base.prepare = shm_surface_prepare;
1172 surface->base.swap = shm_surface_swap;
1173 surface->base.acquire = shm_surface_acquire;
1174 surface->base.release = shm_surface_release;
1175 surface->base.destroy = shm_surface_destroy;
1176
1177 surface->display = display;
1178 surface->surface = wl_surface;
1179 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001180
1181 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001182}
1183
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001184/*
1185 * The following correspondences between file names and cursors was copied
1186 * from: https://bugs.kde.org/attachment.cgi?id=67313
1187 */
1188
1189static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001190 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001191 "sw-resize",
1192 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001193};
1194
1195static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001196 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001197 "se-resize",
1198 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001199};
1200
1201static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001202 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001203 "s-resize",
1204 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001205};
1206
1207static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209 "closedhand",
1210 "208530c400c041818281048008011002"
1211};
1212
1213static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215 "default",
1216 "top_left_arrow",
1217 "left-arrow"
1218};
1219
1220static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "w-resize",
1223 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "e-resize",
1229 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "nw-resize",
1235 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001240 "ne-resize",
1241 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242};
1243
1244static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001246 "n-resize",
1247 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248};
1249
1250static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001251 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252 "ibeam",
1253 "text"
1254};
1255
1256static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001257 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258 "pointer",
1259 "pointing_hand",
1260 "e29285e634086352946a0e7090d73106"
1261};
1262
1263static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001264 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001265 "wait",
1266 "0426c94ea35c87780ff01dc239897213"
1267};
1268
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001269static const char *move_draggings[] = {
1270 "dnd-move"
1271};
1272
1273static const char *copy_draggings[] = {
1274 "dnd-copy"
1275};
1276
1277static const char *forbidden_draggings[] = {
1278 "dnd-none",
1279 "dnd-no-drop"
1280};
1281
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001282struct cursor_alternatives {
1283 const char **names;
1284 size_t count;
1285};
1286
1287static const struct cursor_alternatives cursors[] = {
1288 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1289 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1290 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1291 {grabbings, ARRAY_LENGTH(grabbings)},
1292 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1293 {left_sides, ARRAY_LENGTH(left_sides)},
1294 {right_sides, ARRAY_LENGTH(right_sides)},
1295 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1296 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1297 {top_sides, ARRAY_LENGTH(top_sides)},
1298 {xterms, ARRAY_LENGTH(xterms)},
1299 {hand1s, ARRAY_LENGTH(hand1s)},
1300 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001301 {move_draggings, ARRAY_LENGTH(move_draggings)},
1302 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1303 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001304};
1305
1306static void
1307create_cursors(struct display *display)
1308{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001309 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001310 struct weston_config *config;
1311 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001312 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001313 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001314 unsigned int i, j;
1315 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001316
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001317 config_file = weston_config_get_name_from_env();
1318 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001319 s = weston_config_get_section(config, "shell", NULL, NULL);
1320 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1321 weston_config_section_get_int(s, "cursor-size", &size, 32);
1322 weston_config_destroy(config);
1323
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001324 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001325 if (!display->cursor_theme) {
1326 fprintf(stderr, "could not load theme '%s'\n", theme);
1327 return;
1328 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001329 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001330 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001331 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001332
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001333 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001334 cursor = NULL;
1335 for (j = 0; !cursor && j < cursors[i].count; ++j)
1336 cursor = wl_cursor_theme_get_cursor(
1337 display->cursor_theme, cursors[i].names[j]);
1338
1339 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001340 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001341 cursors[i].names[0]);
1342
1343 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001344 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001345}
1346
1347static void
1348destroy_cursors(struct display *display)
1349{
1350 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001351 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001352}
1353
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001354struct wl_cursor_image *
1355display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001356{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001357 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001358
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001359 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001360}
1361
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001362static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001363surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001364{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001365 if (!surface->cairo_surface)
1366 return;
1367
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001368 if (surface->opaque_region) {
1369 wl_surface_set_opaque_region(surface->surface,
1370 surface->opaque_region);
1371 wl_region_destroy(surface->opaque_region);
1372 surface->opaque_region = NULL;
1373 }
1374
1375 if (surface->input_region) {
1376 wl_surface_set_input_region(surface->surface,
1377 surface->input_region);
1378 wl_region_destroy(surface->input_region);
1379 surface->input_region = NULL;
1380 }
1381
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001382 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001383 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001384 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001385
Pekka Paalanen89dee002013-02-13 16:17:20 +02001386 cairo_surface_destroy(surface->cairo_surface);
1387 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001388}
1389
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001390int
1391window_has_focus(struct window *window)
1392{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001393 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001394}
1395
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001396static void
1397window_close(struct window *window)
1398{
1399 if (window->close_handler)
1400 window->close_handler(window->user_data);
1401 else
1402 display_exit(window->display);
1403}
1404
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001405struct display *
1406window_get_display(struct window *window)
1407{
1408 return window->display;
1409}
1410
Pekka Paalanen89dee002013-02-13 16:17:20 +02001411static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001412handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1413 int32_t width, int32_t height)
1414{
1415 struct window *window = data;
1416
1417 window_schedule_resize(window, width, height);
1418}
1419
1420static const struct ivi_surface_listener ivi_surface_listener = {
1421 handle_ivi_surface_configure,
1422};
1423
1424static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001425surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001426{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001427 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001428 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001429
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001430 if (!surface->toysurface && display->dpy &&
1431 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001432 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001433 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001434 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001435 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001436 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001437 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001438
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001439 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001440 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001441 surface->surface,
1442 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001443
Pekka Paalanen89dee002013-02-13 16:17:20 +02001444 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001445 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001446 allocation.width, allocation.height, flags,
1447 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001448}
1449
1450static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001451window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001452{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001453 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001454 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001455
Pekka Paalanenec076692012-11-30 13:37:27 +02001456 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001457 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001458
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001459 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1460 flags |= SURFACE_HINT_RGB565;
1461
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001462 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001463}
1464
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001465int
1466window_get_buffer_transform(struct window *window)
1467{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001468 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001469}
1470
1471void
1472window_set_buffer_transform(struct window *window,
1473 enum wl_output_transform transform)
1474{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001475 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001476 wl_surface_set_buffer_transform(window->main_surface->surface,
1477 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001478}
1479
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001480void
1481window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001482 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001483{
1484 window->main_surface->buffer_scale = scale;
1485 wl_surface_set_buffer_scale(window->main_surface->surface,
1486 scale);
1487}
1488
1489uint32_t
1490window_get_buffer_scale(struct window *window)
1491{
1492 return window->main_surface->buffer_scale;
1493}
1494
Alexander Larssond68f5232013-05-22 14:41:33 +02001495uint32_t
1496window_get_output_scale(struct window *window)
1497{
1498 struct window_output *window_output;
1499 struct window_output *window_output_tmp;
1500 int scale = 1;
1501
1502 wl_list_for_each_safe(window_output, window_output_tmp,
1503 &window->window_output_list, link) {
1504 if (window_output->output->scale > scale)
1505 scale = window_output->output->scale;
1506 }
1507
1508 return scale;
1509}
1510
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001511static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001512
Pekka Paalanen4e373742013-02-13 16:17:13 +02001513static void
1514surface_destroy(struct surface *surface)
1515{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001516 if (surface->frame_cb)
1517 wl_callback_destroy(surface->frame_cb);
1518
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001519 if (surface->input_region)
1520 wl_region_destroy(surface->input_region);
1521
1522 if (surface->opaque_region)
1523 wl_region_destroy(surface->opaque_region);
1524
Pekka Paalanen35e82632013-04-25 13:57:48 +03001525 if (surface->subsurface)
1526 wl_subsurface_destroy(surface->subsurface);
1527
Pekka Paalanen4e373742013-02-13 16:17:13 +02001528 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001529
1530 if (surface->toysurface)
1531 surface->toysurface->destroy(surface->toysurface);
1532
Pekka Paalanen35e82632013-04-25 13:57:48 +03001533 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001534 free(surface);
1535}
1536
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001537void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001538window_destroy(struct window *window)
1539{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001540 struct display *display = window->display;
1541 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001542 struct window_output *window_output;
1543 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001544
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001545 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001546
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001547 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001548 if (input->touch_focus == window)
1549 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001550 if (input->pointer_focus == window)
1551 input->pointer_focus = NULL;
1552 if (input->keyboard_focus == window)
1553 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001554 if (input->focus_widget &&
1555 input->focus_widget->window == window)
1556 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001557 }
1558
Rob Bradford7507b572012-05-15 17:55:34 +01001559 wl_list_for_each_safe(window_output, window_output_tmp,
1560 &window->window_output_list, link) {
1561 free (window_output);
1562 }
1563
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001564 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001565 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001566
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001567 if (window->xdg_surface)
1568 xdg_surface_destroy(window->xdg_surface);
1569 if (window->xdg_popup)
1570 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001571
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001572 if (window->ivi_surface)
1573 ivi_surface_destroy(window->ivi_surface);
1574
Pekka Paalanen4e373742013-02-13 16:17:13 +02001575 surface_destroy(window->main_surface);
1576
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001577 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001578
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001579 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001580 free(window);
1581}
1582
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001583static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001584widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001585{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001586 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001587
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001588 wl_list_for_each(child, &widget->child_list, link) {
1589 target = widget_find_widget(child, x, y);
1590 if (target)
1591 return target;
1592 }
1593
1594 if (widget->allocation.x <= x &&
1595 x < widget->allocation.x + widget->allocation.width &&
1596 widget->allocation.y <= y &&
1597 y < widget->allocation.y + widget->allocation.height) {
1598 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001599 }
1600
1601 return NULL;
1602}
1603
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001604static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001605window_find_widget(struct window *window, int32_t x, int32_t y)
1606{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001607 struct surface *surface;
1608 struct widget *widget;
1609
1610 wl_list_for_each(surface, &window->subsurface_list, link) {
1611 widget = widget_find_widget(surface->widget, x, y);
1612 if (widget)
1613 return widget;
1614 }
1615
1616 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001617}
1618
1619static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001620widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001621{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001622 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001623
Peter Huttererf3d62272013-08-08 11:57:05 +10001624 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001625 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001626 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001627 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001628 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001629 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001630 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001631 widget->tooltip = NULL;
1632 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001633 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001634 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001635
1636 return widget;
1637}
1638
1639struct widget *
1640window_add_widget(struct window *window, void *data)
1641{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001642 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001643
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001644 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001645 wl_list_init(&widget->link);
1646 window->main_surface->widget = widget;
1647
1648 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001649}
1650
1651struct widget *
1652widget_add_widget(struct widget *parent, void *data)
1653{
1654 struct widget *widget;
1655
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001656 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001657 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001658
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001659 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001660}
1661
1662void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001663widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001665 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001666 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001667 struct input *input;
1668
Pekka Paalanen35e82632013-04-25 13:57:48 +03001669 /* Destroy the sub-surface along with the root widget */
1670 if (surface->widget == widget && surface->subsurface)
1671 surface_destroy(widget->surface);
1672
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001673 if (widget->tooltip)
1674 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001675
Pekka Paalanene156fb62012-01-19 13:51:38 +02001676 wl_list_for_each(input, &display->input_list, link) {
1677 if (input->focus_widget == widget)
1678 input->focus_widget = NULL;
1679 }
1680
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001681 wl_list_remove(&widget->link);
1682 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001683}
1684
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001685void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001686widget_set_default_cursor(struct widget *widget, int cursor)
1687{
1688 widget->default_cursor = cursor;
1689}
1690
1691void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001692widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001693{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001694 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001695}
1696
1697void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001698widget_set_size(struct widget *widget, int32_t width, int32_t height)
1699{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001700 widget->allocation.width = width;
1701 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001702}
1703
1704void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001705widget_set_allocation(struct widget *widget,
1706 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001707{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001708 widget->allocation.x = x;
1709 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001710 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001711}
1712
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001713void
1714widget_set_transparent(struct widget *widget, int transparent)
1715{
1716 widget->opaque = !transparent;
1717}
1718
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001719void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001720widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001721{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001722 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001723}
1724
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001725static cairo_surface_t *
1726widget_get_cairo_surface(struct widget *widget)
1727{
1728 struct surface *surface = widget->surface;
1729 struct window *window = widget->window;
1730
Neil Roberts97b747c2013-12-19 16:17:12 +00001731 assert(widget->use_cairo);
1732
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001733 if (!surface->cairo_surface) {
1734 if (surface == window->main_surface)
1735 window_create_main_surface(window);
1736 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001737 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001738 }
1739
1740 return surface->cairo_surface;
1741}
1742
Alexander Larsson15901f02013-05-22 14:41:25 +02001743static void
1744widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1745{
1746 struct surface *surface = widget->surface;
1747 double angle;
1748 cairo_matrix_t m;
1749 enum wl_output_transform transform;
1750 int surface_width, surface_height;
1751 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001752 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001753
1754 surface_width = surface->allocation.width;
1755 surface_height = surface->allocation.height;
1756
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001757 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001758 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001759
Alexander Larsson15901f02013-05-22 14:41:25 +02001760 switch (transform) {
1761 case WL_OUTPUT_TRANSFORM_FLIPPED:
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1764 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1765 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1766 break;
1767 default:
1768 cairo_matrix_init_identity(&m);
1769 break;
1770 }
1771
1772 switch (transform) {
1773 case WL_OUTPUT_TRANSFORM_NORMAL:
1774 default:
1775 angle = 0;
1776 translate_x = 0;
1777 translate_y = 0;
1778 break;
1779 case WL_OUTPUT_TRANSFORM_FLIPPED:
1780 angle = 0;
1781 translate_x = surface_width;
1782 translate_y = 0;
1783 break;
1784 case WL_OUTPUT_TRANSFORM_90:
1785 angle = M_PI_2;
1786 translate_x = surface_height;
1787 translate_y = 0;
1788 break;
1789 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1790 angle = M_PI_2;
1791 translate_x = surface_height;
1792 translate_y = surface_width;
1793 break;
1794 case WL_OUTPUT_TRANSFORM_180:
1795 angle = M_PI;
1796 translate_x = surface_width;
1797 translate_y = surface_height;
1798 break;
1799 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1800 angle = M_PI;
1801 translate_x = 0;
1802 translate_y = surface_height;
1803 break;
1804 case WL_OUTPUT_TRANSFORM_270:
1805 angle = M_PI + M_PI_2;
1806 translate_x = 0;
1807 translate_y = surface_width;
1808 break;
1809 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1810 angle = M_PI + M_PI_2;
1811 translate_x = 0;
1812 translate_y = 0;
1813 break;
1814 }
1815
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001816 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001817 cairo_translate(cr, translate_x, translate_y);
1818 cairo_rotate(cr, angle);
1819 cairo_transform(cr, &m);
1820}
1821
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001822cairo_t *
1823widget_cairo_create(struct widget *widget)
1824{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001825 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001826 cairo_surface_t *cairo_surface;
1827 cairo_t *cr;
1828
1829 cairo_surface = widget_get_cairo_surface(widget);
1830 cr = cairo_create(cairo_surface);
1831
Alexander Larsson15901f02013-05-22 14:41:25 +02001832 widget_cairo_update_transform(widget, cr);
1833
Pekka Paalanen35e82632013-04-25 13:57:48 +03001834 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1835
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001836 return cr;
1837}
1838
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001839struct wl_surface *
1840widget_get_wl_surface(struct widget *widget)
1841{
1842 return widget->surface->surface;
1843}
1844
Neil Roberts5e10a042013-09-09 00:40:17 +01001845struct wl_subsurface *
1846widget_get_wl_subsurface(struct widget *widget)
1847{
1848 return widget->surface->subsurface;
1849}
1850
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001851uint32_t
1852widget_get_last_time(struct widget *widget)
1853{
1854 return widget->surface->last_time;
1855}
1856
1857void
1858widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1859{
1860 struct wl_compositor *comp = widget->window->display->compositor;
1861 struct surface *surface = widget->surface;
1862
1863 if (!surface->input_region)
1864 surface->input_region = wl_compositor_create_region(comp);
1865
1866 if (rect) {
1867 wl_region_add(surface->input_region,
1868 rect->x, rect->y, rect->width, rect->height);
1869 }
1870}
1871
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001872void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001873widget_set_resize_handler(struct widget *widget,
1874 widget_resize_handler_t handler)
1875{
1876 widget->resize_handler = handler;
1877}
1878
1879void
1880widget_set_redraw_handler(struct widget *widget,
1881 widget_redraw_handler_t handler)
1882{
1883 widget->redraw_handler = handler;
1884}
1885
1886void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001887widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001888{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001889 widget->enter_handler = handler;
1890}
1891
1892void
1893widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1894{
1895 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001896}
1897
1898void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001899widget_set_motion_handler(struct widget *widget,
1900 widget_motion_handler_t handler)
1901{
1902 widget->motion_handler = handler;
1903}
1904
1905void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001906widget_set_button_handler(struct widget *widget,
1907 widget_button_handler_t handler)
1908{
1909 widget->button_handler = handler;
1910}
1911
1912void
Rusty Lynch041815a2013-08-08 21:20:38 -07001913widget_set_touch_up_handler(struct widget *widget,
1914 widget_touch_up_handler_t handler)
1915{
1916 widget->touch_up_handler = handler;
1917}
1918
1919void
1920widget_set_touch_down_handler(struct widget *widget,
1921 widget_touch_down_handler_t handler)
1922{
1923 widget->touch_down_handler = handler;
1924}
1925
1926void
1927widget_set_touch_motion_handler(struct widget *widget,
1928 widget_touch_motion_handler_t handler)
1929{
1930 widget->touch_motion_handler = handler;
1931}
1932
1933void
1934widget_set_touch_frame_handler(struct widget *widget,
1935 widget_touch_frame_handler_t handler)
1936{
1937 widget->touch_frame_handler = handler;
1938}
1939
1940void
1941widget_set_touch_cancel_handler(struct widget *widget,
1942 widget_touch_cancel_handler_t handler)
1943{
1944 widget->touch_cancel_handler = handler;
1945}
1946
1947void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001948widget_set_axis_handler(struct widget *widget,
1949 widget_axis_handler_t handler)
1950{
1951 widget->axis_handler = handler;
1952}
1953
Peter Hutterer87743e92016-01-18 16:38:22 +10001954void
1955widget_set_pointer_frame_handler(struct widget *widget,
1956 widget_pointer_frame_handler_t handler)
1957{
1958 widget->pointer_frame_handler = handler;
1959}
1960
1961void
1962widget_set_axis_handlers(struct widget *widget,
1963 widget_axis_handler_t axis_handler,
1964 widget_axis_source_handler_t axis_source_handler,
1965 widget_axis_stop_handler_t axis_stop_handler,
1966 widget_axis_discrete_handler_t axis_discrete_handler)
1967{
1968 widget->axis_handler = axis_handler;
1969 widget->axis_source_handler = axis_source_handler;
1970 widget->axis_stop_handler = axis_stop_handler;
1971 widget->axis_discrete_handler = axis_discrete_handler;
1972}
1973
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001974static void
1975window_schedule_redraw_task(struct window *window);
1976
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001977void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001978widget_schedule_redraw(struct widget *widget)
1979{
Pekka Paalanen71233882013-04-25 13:57:53 +03001980 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001981 widget->surface->redraw_needed = 1;
1982 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001983}
1984
Neil Roberts97b747c2013-12-19 16:17:12 +00001985void
1986widget_set_use_cairo(struct widget *widget,
1987 int use_cairo)
1988{
1989 widget->use_cairo = use_cairo;
1990}
1991
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001992cairo_surface_t *
1993window_get_surface(struct window *window)
1994{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001995 cairo_surface_t *cairo_surface;
1996
1997 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1998
1999 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002000}
2001
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002002struct wl_surface *
2003window_get_wl_surface(struct window *window)
2004{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002005 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002006}
2007
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002008static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002009tooltip_redraw_handler(struct widget *widget, void *data)
2010{
2011 cairo_t *cr;
2012 const int32_t r = 3;
2013 struct tooltip *tooltip = data;
2014 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002015
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002016 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002017 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002018 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2019 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2020 cairo_paint(cr);
2021
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002022 width = widget->allocation.width;
2023 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002024 rounded_rect(cr, 0, 0, width, height, r);
2025
2026 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2027 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2028 cairo_fill(cr);
2029
2030 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2031 cairo_move_to(cr, 10, 16);
2032 cairo_show_text(cr, tooltip->entry);
2033 cairo_destroy(cr);
2034}
2035
2036static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002037get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002038{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002039 cairo_t *cr;
2040 cairo_text_extents_t extents;
2041
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002042 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002043 * created yet, and parent does not have a valid surface
2044 * outside repaint, either.
2045 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002046 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002047 cairo_text_extents(cr, tooltip->entry, &extents);
2048 cairo_destroy(cr);
2049
2050 return extents;
2051}
2052
2053static int
2054window_create_tooltip(struct tooltip *tooltip)
2055{
2056 struct widget *parent = tooltip->parent;
2057 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002058 const int offset_y = 27;
2059 const int margin = 3;
2060 cairo_text_extents_t extents;
2061
2062 if (tooltip->widget)
2063 return 0;
2064
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002065 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002066
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002067 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002068 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002069 widget_set_allocation(tooltip->widget,
2070 tooltip->x, tooltip->y + offset_y,
2071 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002072
2073 return 0;
2074}
2075
2076void
2077widget_destroy_tooltip(struct widget *parent)
2078{
2079 struct tooltip *tooltip = parent->tooltip;
2080
2081 parent->tooltip_count = 0;
2082 if (!tooltip)
2083 return;
2084
2085 if (tooltip->widget) {
2086 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002087 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002088 }
2089
2090 close(tooltip->tooltip_fd);
2091 free(tooltip->entry);
2092 free(tooltip);
2093 parent->tooltip = NULL;
2094}
2095
2096static void
2097tooltip_func(struct task *task, uint32_t events)
2098{
2099 struct tooltip *tooltip =
2100 container_of(task, struct tooltip, tooltip_task);
2101 uint64_t exp;
2102
Martin Olsson8df662a2012-07-08 03:03:47 +02002103 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2104 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002105 window_create_tooltip(tooltip);
2106}
2107
2108#define TOOLTIP_TIMEOUT 500
2109static int
2110tooltip_timer_reset(struct tooltip *tooltip)
2111{
2112 struct itimerspec its;
2113
2114 its.it_interval.tv_sec = 0;
2115 its.it_interval.tv_nsec = 0;
2116 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2117 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2118 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2119 fprintf(stderr, "could not set timerfd\n: %m");
2120 return -1;
2121 }
2122
2123 return 0;
2124}
2125
2126int
2127widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2128{
2129 struct tooltip *tooltip = parent->tooltip;
2130
2131 parent->tooltip_count++;
2132 if (tooltip) {
2133 tooltip->x = x;
2134 tooltip->y = y;
2135 tooltip_timer_reset(tooltip);
2136 return 0;
2137 }
2138
2139 /* the handler might be triggered too fast via input device motion, so
2140 * we need this check here to make sure tooltip is fully initialized */
2141 if (parent->tooltip_count > 1)
2142 return 0;
2143
2144 tooltip = malloc(sizeof *tooltip);
2145 if (!tooltip)
2146 return -1;
2147
2148 parent->tooltip = tooltip;
2149 tooltip->parent = parent;
2150 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002151 tooltip->x = x;
2152 tooltip->y = y;
2153 tooltip->entry = strdup(entry);
2154 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2155 if (tooltip->tooltip_fd < 0) {
2156 fprintf(stderr, "could not create timerfd\n: %m");
2157 return -1;
2158 }
2159
2160 tooltip->tooltip_task.run = tooltip_func;
2161 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2162 EPOLLIN, &tooltip->tooltip_task);
2163 tooltip_timer_reset(tooltip);
2164
2165 return 0;
2166}
2167
2168static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002169frame_resize_handler(struct widget *widget,
2170 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002171{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002173 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002174 struct rectangle interior;
2175 struct rectangle input;
2176 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002177
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002178 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002179 interior.x = 0;
2180 interior.y = 0;
2181 interior.width = width;
2182 interior.height = height;
2183 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002184 frame_resize(frame->frame, width, height);
2185 frame_interior(frame->frame, &interior.x, &interior.y,
2186 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002187 }
2188
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002189 widget_set_allocation(child, interior.x, interior.y,
2190 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002192 if (child->resize_handler) {
2193 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 child->user_data);
2195
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002196 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 width = child->allocation.width;
2198 height = child->allocation.height;
2199 } else {
2200 frame_resize_inside(frame->frame,
2201 child->allocation.width,
2202 child->allocation.height);
2203 width = frame_width(frame->frame);
2204 height = frame_height(frame->frame);
2205 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002206 }
2207
Scott Moreauf7e498c2012-05-14 11:39:29 -06002208 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002209
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002210 widget->surface->input_region =
2211 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002212 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002213 frame_input_rect(frame->frame, &input.x, &input.y,
2214 &input.width, &input.height);
2215 wl_region_add(widget->surface->input_region,
2216 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002217 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002219 }
2220
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002221 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002222
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002223 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002224 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002225 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2226 &opaque.width, &opaque.height);
2227
2228 wl_region_add(widget->surface->opaque_region,
2229 opaque.x, opaque.y,
2230 opaque.width, opaque.height);
2231 } else {
2232 wl_region_add(widget->surface->opaque_region,
2233 0, 0, width, height);
2234 }
Martin Minarik1998b152012-05-10 02:04:35 +02002235 }
2236
Martin Minarik1998b152012-05-10 02:04:35 +02002237
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002238 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002239}
2240
2241static void
2242frame_redraw_handler(struct widget *widget, void *data)
2243{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002244 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002245 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002246 struct window *window = widget->window;
2247
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002248 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002249 return;
2250
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002251 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002252
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002253 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002254
2255 cairo_destroy(cr);
2256}
2257
2258static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002259frame_get_pointer_image_for_location(struct window_frame *frame,
2260 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002261{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002262 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002263
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002264 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002265 return CURSOR_LEFT_PTR;
2266
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002267 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002268 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002269 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002270 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002271 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002272 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002273 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002274 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002275 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002276 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002277 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002278 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002279 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002280 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002281 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002282 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002283 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002284 case THEME_LOCATION_EXTERIOR:
2285 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002286 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002287 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002288 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002289}
2290
Pekka Paalanen26237862014-09-10 15:10:30 +03002291static void
2292frame_menu_func(void *data, struct input *input, int index)
2293{
2294 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002295
2296 switch (index) {
2297 case 0: /* close */
2298 window_close(window);
2299 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002300 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002301 /* we don't have a way to get out of fullscreen for now */
2302 if (window->fullscreen_handler)
2303 window->fullscreen_handler(window, window->user_data);
2304 break;
2305 }
2306}
2307
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002308void
2309window_show_frame_menu(struct window *window,
2310 struct input *input, uint32_t time)
2311{
2312 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002313 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002314
Pekka Paalanen26237862014-09-10 15:10:30 +03002315 static const char *entries[] = {
2316 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002317 "Fullscreen"
2318 };
2319
2320 if (window->fullscreen_handler)
2321 count = ARRAY_LENGTH(entries);
2322 else
2323 count = ARRAY_LENGTH(entries) - 1;
2324
2325 input_get_position(input, &x, &y);
2326 window_show_menu(window->display, input, time, window,
2327 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002328}
2329
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002330static int
2331frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002332 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002333{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002334 struct window_frame *frame = data;
2335 enum theme_location location;
2336
2337 location = frame_pointer_enter(frame->frame, input, x, y);
2338 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2339 widget_schedule_redraw(frame->widget);
2340
2341 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002342}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002343
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002344static int
2345frame_motion_handler(struct widget *widget,
2346 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002347 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002348{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002349 struct window_frame *frame = data;
2350 enum theme_location location;
2351
2352 location = frame_pointer_motion(frame->frame, input, x, y);
2353 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2354 widget_schedule_redraw(frame->widget);
2355
2356 return frame_get_pointer_image_for_location(data, location);
2357}
2358
2359static void
2360frame_leave_handler(struct widget *widget,
2361 struct input *input, void *data)
2362{
2363 struct window_frame *frame = data;
2364
2365 frame_pointer_leave(frame->frame, input);
2366 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2367 widget_schedule_redraw(frame->widget);
2368}
2369
2370static void
2371frame_handle_status(struct window_frame *frame, struct input *input,
2372 uint32_t time, enum theme_location location)
2373{
2374 struct window *window = frame->widget->window;
2375 uint32_t status;
2376
2377 status = frame_status(frame->frame);
2378 if (status & FRAME_STATUS_REPAINT)
2379 widget_schedule_redraw(frame->widget);
2380
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002381 if (status & FRAME_STATUS_MINIMIZE) {
2382 window_set_minimized(window);
2383 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2384 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002385
2386 if (status & FRAME_STATUS_MENU) {
2387 window_show_frame_menu(window, input, time);
2388 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2389 }
2390
2391 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002392 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002393 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2394 }
2395
2396 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002397 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002398 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002399 }
2400
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002401 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002402 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002403 xdg_surface_move(window->xdg_surface,
2404 input_get_seat(input),
2405 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002406
2407 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2408 }
2409
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002410 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002411 input_ungrab(input);
2412
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002413 xdg_surface_resize(window->xdg_surface,
2414 input_get_seat(input),
2415 window->display->serial,
2416 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002417
2418 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2419 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002420}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002421
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002422#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002423static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002424frame_button_handler(struct widget *widget,
2425 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002426 uint32_t button, enum wl_pointer_button_state state,
2427 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002428
2429{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002430 struct window_frame *frame = data;
2431 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002432
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002433 frame->double_click = 0;
2434 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2435 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2436 frame->double_click = 1;
2437 frame->did_double = 1;
2438 } else
2439 frame->did_double = 0;
2440
2441 frame->last_time = time;
2442 } else if (frame->did_double == 1) {
2443 frame->double_click = 1;
2444 frame->did_double = 0;
2445 }
2446
2447 if (frame->double_click)
2448 location = frame_double_click(frame->frame, input,
2449 button, state);
2450 else
2451 location = frame_pointer_button(frame->frame, input,
2452 button, state);
2453
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002454 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002455}
2456
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002457static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002458frame_touch_down_handler(struct widget *widget, struct input *input,
2459 uint32_t serial, uint32_t time, int32_t id,
2460 float x, float y, void *data)
2461{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002462 struct window_frame *frame = data;
2463
Xiong Zhang382de462014-06-12 11:06:26 +08002464 frame->double_click = 0;
2465 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2466 frame->last_id == id) {
2467 frame->double_click = 1;
2468 frame->did_double = 1;
2469 frame->double_id = id;
2470 } else
2471 frame->did_double = 0;
2472
2473 frame->last_time = time;
2474 frame->last_id = id;
2475
2476 if (frame->double_click)
2477 frame_double_touch_down(frame->frame, input, id, x, y);
2478 else
2479 frame_touch_down(frame->frame, input, id, x, y);
2480
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002481 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2482}
2483
2484static void
2485frame_touch_up_handler(struct widget *widget,
2486 struct input *input, uint32_t serial, uint32_t time,
2487 int32_t id, void *data)
2488{
2489 struct window_frame *frame = data;
2490
Xiong Zhang382de462014-06-12 11:06:26 +08002491 if (frame->double_id == id && frame->did_double) {
2492 frame->did_double = 0;
2493 frame->double_id = 0;
2494 frame_double_touch_up(frame->frame, input, id);
2495 } else
2496 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002497 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002498}
2499
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002500struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002501window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002502{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002503 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002504 uint32_t buttons;
2505
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002506 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002507 buttons = FRAME_BUTTON_NONE;
2508 } else {
2509 buttons = FRAME_BUTTON_ALL;
2510 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002511
Peter Huttererf3d62272013-08-08 11:57:05 +10002512 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002513 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002514 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002515
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002516 frame->widget = window_add_widget(window, frame);
2517 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002518
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002519 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2520 widget_set_resize_handler(frame->widget, frame_resize_handler);
2521 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002522 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002523 widget_set_motion_handler(frame->widget, frame_motion_handler);
2524 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002525 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002526 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002527
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002528 window->frame = frame;
2529
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002530 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002531}
2532
Kristian Høgsberga1627922012-06-20 17:30:03 -04002533void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002534window_frame_set_child_size(struct widget *widget, int child_width,
2535 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002536{
2537 struct display *display = widget->window->display;
2538 struct theme *t = display->theme;
2539 int decoration_width, decoration_height;
2540 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002541 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002542
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002543 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002544 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002545 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002546 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002547
2548 width = child_width + decoration_width;
2549 height = child_height + decoration_height;
2550 } else {
2551 width = child_width;
2552 height = child_height;
2553 }
2554
2555 window_schedule_resize(widget->window, width, height);
2556}
2557
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002558static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002559window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002560{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002561 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002562
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002563 /* frame->child must be destroyed by the application */
2564 widget_destroy(frame->widget);
2565 free(frame);
2566}
2567
2568static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002569input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002570 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002571{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002572 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002573 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002574
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002575 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002576 return;
2577
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002578 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002579 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002580 widget = old;
2581 if (input->grab)
2582 widget = input->grab;
2583 if (widget->leave_handler)
2584 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002585 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002586 }
2587
2588 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002589 widget = focus;
2590 if (input->grab)
2591 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002592 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002593 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002594 cursor = widget->enter_handler(focus, input, x, y,
2595 widget->user_data);
2596 else
2597 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002598
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002599 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002600 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002601}
2602
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002603void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002604touch_grab(struct input *input, int32_t touch_id)
2605{
2606 input->touch_grab = 1;
2607 input->touch_grab_id = touch_id;
2608}
2609
2610void
2611touch_ungrab(struct input *input)
2612{
2613 struct touch_point *tp, *tmp;
2614
2615 input->touch_grab = 0;
2616
2617 wl_list_for_each_safe(tp, tmp,
2618 &input->touch_point_list, link) {
2619 if (tp->id != input->touch_grab_id)
2620 continue;
2621 wl_list_remove(&tp->link);
2622 free(tp);
2623
2624 return;
2625 }
2626}
2627
2628void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002629input_grab(struct input *input, struct widget *widget, uint32_t button)
2630{
2631 input->grab = widget;
2632 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002633
2634 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002635}
2636
2637void
2638input_ungrab(struct input *input)
2639{
2640 struct widget *widget;
2641
2642 input->grab = NULL;
2643 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002644 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002645 input->sx, input->sy);
2646 input_set_focus_widget(input, widget, input->sx, input->sy);
2647 }
2648}
2649
2650static void
Derek Foreman118a4292015-04-22 17:23:35 -05002651cursor_delay_timer_reset(struct input *input, uint32_t duration)
2652{
2653 struct itimerspec its;
2654
2655 if (!duration)
2656 input->cursor_timer_running = false;
2657 else
2658 input->cursor_timer_running = true;
2659
2660 its.it_interval.tv_sec = 0;
2661 its.it_interval.tv_nsec = 0;
2662 its.it_value.tv_sec = duration / 1000;
2663 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2664 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2665 fprintf(stderr, "could not set cursor timerfd\n: %m");
2666}
2667
2668static void cancel_pointer_image_update(struct input *input)
2669{
2670 if (input->cursor_timer_running)
2671 cursor_delay_timer_reset(input, 0);
2672}
2673
2674static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002675input_remove_pointer_focus(struct input *input)
2676{
2677 struct window *window = input->pointer_focus;
2678
2679 if (!window)
2680 return;
2681
2682 input_set_focus_widget(input, NULL, 0, 0);
2683
2684 input->pointer_focus = NULL;
2685 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002686 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002687}
2688
2689static void
2690pointer_handle_enter(void *data, struct wl_pointer *pointer,
2691 uint32_t serial, struct wl_surface *surface,
2692 wl_fixed_t sx_w, wl_fixed_t sy_w)
2693{
2694 struct input *input = data;
2695 struct window *window;
2696 struct widget *widget;
2697 float sx = wl_fixed_to_double(sx_w);
2698 float sy = wl_fixed_to_double(sy_w);
2699
2700 if (!surface) {
2701 /* enter event for a window we've just destroyed */
2702 return;
2703 }
2704
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002705 window = wl_surface_get_user_data(surface);
2706 if (surface != window->main_surface->surface) {
2707 DBG("Ignoring input event from subsurface %p\n", surface);
2708 return;
2709 }
2710
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002711 input->display->serial = serial;
2712 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002713 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002714
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002715 input->sx = sx;
2716 input->sy = sy;
2717
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002718 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002719 input_set_focus_widget(input, widget, sx, sy);
2720}
2721
2722static void
2723pointer_handle_leave(void *data, struct wl_pointer *pointer,
2724 uint32_t serial, struct wl_surface *surface)
2725{
2726 struct input *input = data;
2727
2728 input->display->serial = serial;
2729 input_remove_pointer_focus(input);
2730}
2731
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002732static void
Daniel Stone37816df2012-05-16 18:45:18 +01002733pointer_handle_motion(void *data, struct wl_pointer *pointer,
2734 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002735{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002736 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002737 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002738 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002739 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002740 float sx = wl_fixed_to_double(sx_w);
2741 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002742
Paul Winwoodb22bf572013-08-29 10:52:54 +01002743 if (!window)
2744 return;
2745
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002746 input->sx = sx;
2747 input->sy = sy;
2748
Rob Bradford5f087742013-07-11 19:41:27 +01002749 /* when making the window smaller - e.g. after a unmaximise we might
2750 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002751 * based on the old surface dimensions. However, if we have an active
2752 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002753 */
Derek Foreman46812b62015-08-26 17:13:27 -05002754 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002755 sy < window->main_surface->allocation.y ||
2756 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002757 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002758 return;
2759
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002760 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002761 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002762 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002763 }
2764
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002765 if (input->grab)
2766 widget = input->grab;
2767 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002768 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002769 if (widget) {
2770 if (widget->motion_handler)
2771 cursor = widget->motion_handler(input->focus_widget,
2772 input, time, sx, sy,
2773 widget->user_data);
2774 else
2775 cursor = widget->default_cursor;
2776 } else
2777 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002778
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002779 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002780}
2781
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002782static void
Daniel Stone37816df2012-05-16 18:45:18 +01002783pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002784 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002785{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002786 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002787 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002788 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002789
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002790 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002791 if (input->focus_widget && input->grab == NULL &&
2792 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002793 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002794
Neil Roberts6b28aad2012-01-23 19:11:18 +00002795 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002796 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002797 (*widget->button_handler)(widget,
2798 input, time,
2799 button, state,
2800 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002801
Daniel Stone4dbadb12012-05-30 16:31:51 +01002802 if (input->grab && input->grab_button == button &&
2803 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002804 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002805}
2806
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002807static void
Daniel Stone37816df2012-05-16 18:45:18 +01002808pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002809 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002810{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002811 struct input *input = data;
2812 struct widget *widget;
2813
2814 widget = input->focus_widget;
2815 if (input->grab)
2816 widget = input->grab;
2817 if (widget && widget->axis_handler)
2818 (*widget->axis_handler)(widget,
2819 input, time,
2820 axis, value,
2821 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002822}
2823
Peter Hutterer87743e92016-01-18 16:38:22 +10002824static void
2825pointer_handle_frame(void *data, struct wl_pointer *pointer)
2826{
2827 struct input *input = data;
2828 struct widget *widget;
2829
2830 widget = input->focus_widget;
2831 if (input->grab)
2832 widget = input->grab;
2833 if (widget && widget->pointer_frame_handler)
2834 (*widget->pointer_frame_handler)(widget,
2835 input,
2836 widget->user_data);
2837}
2838
2839static void
2840pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2841 uint32_t source)
2842{
2843 struct input *input = data;
2844 struct widget *widget;
2845
2846 widget = input->focus_widget;
2847 if (input->grab)
2848 widget = input->grab;
2849 if (widget && widget->axis_source_handler)
2850 (*widget->axis_source_handler)(widget,
2851 input,
2852 source,
2853 widget->user_data);
2854}
2855
2856static void
2857pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2858 uint32_t time, uint32_t axis)
2859{
2860 struct input *input = data;
2861 struct widget *widget;
2862
2863 widget = input->focus_widget;
2864 if (input->grab)
2865 widget = input->grab;
2866 if (widget && widget->axis_stop_handler)
2867 (*widget->axis_stop_handler)(widget,
2868 input, time,
2869 axis,
2870 widget->user_data);
2871}
2872
2873static void
2874pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2875 uint32_t axis, int32_t discrete)
2876{
2877 struct input *input = data;
2878 struct widget *widget;
2879
2880 widget = input->focus_widget;
2881 if (input->grab)
2882 widget = input->grab;
2883 if (widget && widget->axis_discrete_handler)
2884 (*widget->axis_discrete_handler)(widget,
2885 input,
2886 axis,
2887 discrete,
2888 widget->user_data);
2889}
2890
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002891static const struct wl_pointer_listener pointer_listener = {
2892 pointer_handle_enter,
2893 pointer_handle_leave,
2894 pointer_handle_motion,
2895 pointer_handle_button,
2896 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002897 pointer_handle_frame,
2898 pointer_handle_axis_source,
2899 pointer_handle_axis_stop,
2900 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002901};
2902
2903static void
2904input_remove_keyboard_focus(struct input *input)
2905{
2906 struct window *window = input->keyboard_focus;
2907 struct itimerspec its;
2908
2909 its.it_interval.tv_sec = 0;
2910 its.it_interval.tv_nsec = 0;
2911 its.it_value.tv_sec = 0;
2912 its.it_value.tv_nsec = 0;
2913 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2914
2915 if (!window)
2916 return;
2917
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002918 if (window->keyboard_focus_handler)
2919 (*window->keyboard_focus_handler)(window, NULL,
2920 window->user_data);
2921
2922 input->keyboard_focus = NULL;
2923}
2924
2925static void
2926keyboard_repeat_func(struct task *task, uint32_t events)
2927{
2928 struct input *input =
2929 container_of(task, struct input, repeat_task);
2930 struct window *window = input->keyboard_focus;
2931 uint64_t exp;
2932
2933 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2934 /* If we change the timer between the fd becoming
2935 * readable and getting here, there'll be nothing to
2936 * read and we get EAGAIN. */
2937 return;
2938
2939 if (window && window->key_handler) {
2940 (*window->key_handler)(window, input, input->repeat_time,
2941 input->repeat_key, input->repeat_sym,
2942 WL_KEYBOARD_KEY_STATE_PRESSED,
2943 window->user_data);
2944 }
2945}
2946
2947static void
2948keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2949 uint32_t format, int fd, uint32_t size)
2950{
2951 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002952 struct xkb_keymap *keymap;
2953 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002954 char *map_str;
2955
2956 if (!data) {
2957 close(fd);
2958 return;
2959 }
2960
2961 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2962 close(fd);
2963 return;
2964 }
2965
2966 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2967 if (map_str == MAP_FAILED) {
2968 close(fd);
2969 return;
2970 }
2971
Ran Benita2e1968f2014-08-19 23:59:51 +03002972 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2973 map_str,
2974 XKB_KEYMAP_FORMAT_TEXT_V1,
2975 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002976 munmap(map_str, size);
2977 close(fd);
2978
Rui Matos3eccb862013-10-10 19:44:22 +02002979 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002980 fprintf(stderr, "failed to compile keymap\n");
2981 return;
2982 }
2983
Rui Matos3eccb862013-10-10 19:44:22 +02002984 state = xkb_state_new(keymap);
2985 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002986 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002987 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002988 return;
2989 }
2990
Rui Matos3eccb862013-10-10 19:44:22 +02002991 xkb_keymap_unref(input->xkb.keymap);
2992 xkb_state_unref(input->xkb.state);
2993 input->xkb.keymap = keymap;
2994 input->xkb.state = state;
2995
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002996 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002997 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002998 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002999 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003000 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003001 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003002}
3003
3004static void
3005keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3006 uint32_t serial, struct wl_surface *surface,
3007 struct wl_array *keys)
3008{
3009 struct input *input = data;
3010 struct window *window;
3011
3012 input->display->serial = serial;
3013 input->keyboard_focus = wl_surface_get_user_data(surface);
3014
3015 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003016 if (window->keyboard_focus_handler)
3017 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003018 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003019}
3020
3021static void
3022keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3023 uint32_t serial, struct wl_surface *surface)
3024{
3025 struct input *input = data;
3026
3027 input->display->serial = serial;
3028 input_remove_keyboard_focus(input);
3029}
3030
Scott Moreau210d0792012-03-22 10:47:01 -06003031static void
Daniel Stone37816df2012-05-16 18:45:18 +01003032keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003033 uint32_t serial, uint32_t time, uint32_t key,
3034 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003035{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003036 struct input *input = data;
3037 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003038 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003039 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003040 const xkb_keysym_t *syms;
3041 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003042 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003043
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003044 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003045 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003046 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003047 return;
3048
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003049 /* We only use input grabs for pointer events for now, so just
3050 * ignore key presses if a grab is active. We expand the key
3051 * event delivery mechanism to route events to widgets to
3052 * properly handle key grabs. In the meantime, this prevents
3053 * key event devlivery while a grab is active. */
3054 if (input->grab && input->grab_button == 0)
3055 return;
3056
Ran Benita2e1968f2014-08-19 23:59:51 +03003057 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003058
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003059 sym = XKB_KEY_NoSymbol;
3060 if (num_syms == 1)
3061 sym = syms[0];
3062
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003063
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003064 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003065 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003066 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003067 } else if (sym == XKB_KEY_F11 &&
3068 window->fullscreen_handler &&
3069 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3070 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003071 } else if (sym == XKB_KEY_F4 &&
3072 input->modifiers == MOD_ALT_MASK &&
3073 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003074 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003075 } else if (window->key_handler) {
3076 (*window->key_handler)(window, input, time, key,
3077 sym, state, window->user_data);
3078 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003079
3080 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3081 key == input->repeat_key) {
3082 its.it_interval.tv_sec = 0;
3083 its.it_interval.tv_nsec = 0;
3084 its.it_value.tv_sec = 0;
3085 its.it_value.tv_nsec = 0;
3086 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003087 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3088 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003089 input->repeat_sym = sym;
3090 input->repeat_key = key;
3091 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003092 its.it_interval.tv_sec = input->repeat_rate_sec;
3093 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3094 its.it_value.tv_sec = input->repeat_delay_sec;
3095 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003096 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3097 }
3098}
3099
3100static void
Daniel Stone351eb612012-05-31 15:27:47 -04003101keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3102 uint32_t serial, uint32_t mods_depressed,
3103 uint32_t mods_latched, uint32_t mods_locked,
3104 uint32_t group)
3105{
3106 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003107 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003108
Matt Ropere61561f2013-06-24 16:52:43 +01003109 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3110 if (!input->xkb.keymap)
3111 return;
3112
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003113 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3114 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003115 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003116 XKB_STATE_MODS_DEPRESSED |
3117 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003118 input->modifiers = 0;
3119 if (mask & input->xkb.control_mask)
3120 input->modifiers |= MOD_CONTROL_MASK;
3121 if (mask & input->xkb.alt_mask)
3122 input->modifiers |= MOD_ALT_MASK;
3123 if (mask & input->xkb.shift_mask)
3124 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003125}
3126
Jonny Lamb06959082014-08-12 14:58:27 +02003127static void
3128set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3129{
3130 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3131 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3132
3133 /* a rate of zero disables any repeating, regardless of the delay's
3134 * value */
3135 if (rate == 0)
3136 return;
3137
3138 if (rate == 1)
3139 input->repeat_rate_sec = 1;
3140 else
3141 input->repeat_rate_nsec = 1000000000 / rate;
3142
3143 input->repeat_delay_sec = delay / 1000;
3144 delay -= (input->repeat_delay_sec * 1000);
3145 input->repeat_delay_nsec = delay * 1000 * 1000;
3146}
3147
3148static void
3149keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3150 int32_t rate, int32_t delay)
3151{
3152 struct input *input = data;
3153
3154 set_repeat_info(input, rate, delay);
3155}
3156
Daniel Stone37816df2012-05-16 18:45:18 +01003157static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003158 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003159 keyboard_handle_enter,
3160 keyboard_handle_leave,
3161 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003162 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003163 keyboard_handle_repeat_info
3164
Daniel Stone37816df2012-05-16 18:45:18 +01003165};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003166
3167static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003168touch_handle_down(void *data, struct wl_touch *wl_touch,
3169 uint32_t serial, uint32_t time, struct wl_surface *surface,
3170 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3171{
3172 struct input *input = data;
3173 struct widget *widget;
3174 float sx = wl_fixed_to_double(x_w);
3175 float sy = wl_fixed_to_double(y_w);
3176
Rusty Lynch1084da52013-08-15 09:10:08 -07003177 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003178 input->touch_focus = wl_surface_get_user_data(surface);
3179 if (!input->touch_focus) {
3180 DBG("Failed to find to touch focus for surface %p\n", surface);
3181 return;
3182 }
3183
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003184 if (surface != input->touch_focus->main_surface->surface) {
3185 DBG("Ignoring input event from subsurface %p\n", surface);
3186 input->touch_focus = NULL;
3187 return;
3188 }
3189
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003190 if (input->grab)
3191 widget = input->grab;
3192 else
3193 widget = window_find_widget(input->touch_focus,
3194 wl_fixed_to_double(x_w),
3195 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003196 if (widget) {
3197 struct touch_point *tp = xmalloc(sizeof *tp);
3198 if (tp) {
3199 tp->id = id;
3200 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003201 tp->x = sx;
3202 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003203 wl_list_insert(&input->touch_point_list, &tp->link);
3204
3205 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003206 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003207 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003208 sx, sy,
3209 widget->user_data);
3210 }
3211 }
3212}
3213
3214static void
3215touch_handle_up(void *data, struct wl_touch *wl_touch,
3216 uint32_t serial, uint32_t time, int32_t id)
3217{
3218 struct input *input = data;
3219 struct touch_point *tp, *tmp;
3220
Rusty Lynch041815a2013-08-08 21:20:38 -07003221 if (!input->touch_focus) {
3222 DBG("No touch focus found for touch up event!\n");
3223 return;
3224 }
3225
3226 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3227 if (tp->id != id)
3228 continue;
3229
3230 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003231 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003232 time, id,
3233 tp->widget->user_data);
3234
3235 wl_list_remove(&tp->link);
3236 free(tp);
3237
3238 return;
3239 }
3240}
3241
3242static void
3243touch_handle_motion(void *data, struct wl_touch *wl_touch,
3244 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3245{
3246 struct input *input = data;
3247 struct touch_point *tp;
3248 float sx = wl_fixed_to_double(x_w);
3249 float sy = wl_fixed_to_double(y_w);
3250
3251 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3252
3253 if (!input->touch_focus) {
3254 DBG("No touch focus found for touch motion event!\n");
3255 return;
3256 }
3257
3258 wl_list_for_each(tp, &input->touch_point_list, link) {
3259 if (tp->id != id)
3260 continue;
3261
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003262 tp->x = sx;
3263 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003264 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003265 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003266 id, sx, sy,
3267 tp->widget->user_data);
3268 return;
3269 }
3270}
3271
3272static void
3273touch_handle_frame(void *data, struct wl_touch *wl_touch)
3274{
3275 struct input *input = data;
3276 struct touch_point *tp, *tmp;
3277
3278 DBG("touch_handle_frame\n");
3279
3280 if (!input->touch_focus) {
3281 DBG("No touch focus found for touch frame event!\n");
3282 return;
3283 }
3284
3285 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3286 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003287 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003288 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003289 }
3290}
3291
3292static void
3293touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3294{
3295 struct input *input = data;
3296 struct touch_point *tp, *tmp;
3297
3298 DBG("touch_handle_cancel\n");
3299
3300 if (!input->touch_focus) {
3301 DBG("No touch focus found for touch cancel event!\n");
3302 return;
3303 }
3304
3305 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3306 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003307 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3308 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003309
3310 wl_list_remove(&tp->link);
3311 free(tp);
3312 }
3313}
3314
3315static const struct wl_touch_listener touch_listener = {
3316 touch_handle_down,
3317 touch_handle_up,
3318 touch_handle_motion,
3319 touch_handle_frame,
3320 touch_handle_cancel,
3321};
3322
3323static void
Daniel Stone37816df2012-05-16 18:45:18 +01003324seat_handle_capabilities(void *data, struct wl_seat *seat,
3325 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003326{
Daniel Stone37816df2012-05-16 18:45:18 +01003327 struct input *input = data;
3328
3329 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3330 input->pointer = wl_seat_get_pointer(seat);
3331 wl_pointer_set_user_data(input->pointer, input);
3332 wl_pointer_add_listener(input->pointer, &pointer_listener,
3333 input);
3334 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003335 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003336 wl_pointer_release(input->pointer);
3337 else
3338 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003339 input->pointer = NULL;
3340 }
3341
3342 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3343 input->keyboard = wl_seat_get_keyboard(seat);
3344 wl_keyboard_set_user_data(input->keyboard, input);
3345 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3346 input);
3347 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003348 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003349 wl_keyboard_release(input->keyboard);
3350 else
3351 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003352 input->keyboard = NULL;
3353 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003354
3355 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3356 input->touch = wl_seat_get_touch(seat);
3357 wl_touch_set_user_data(input->touch, input);
3358 wl_touch_add_listener(input->touch, &touch_listener, input);
3359 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003360 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003361 wl_touch_release(input->touch);
3362 else
3363 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003364 input->touch = NULL;
3365 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003366}
3367
Rob Bradford08031182013-08-13 20:11:03 +01003368static void
3369seat_handle_name(void *data, struct wl_seat *seat,
3370 const char *name)
3371{
3372
3373}
3374
Daniel Stone37816df2012-05-16 18:45:18 +01003375static const struct wl_seat_listener seat_listener = {
3376 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003377 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003378};
3379
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003380void
3381input_get_position(struct input *input, int32_t *x, int32_t *y)
3382{
3383 *x = input->sx;
3384 *y = input->sy;
3385}
3386
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003387int
3388input_get_touch(struct input *input, int32_t id, float *x, float *y)
3389{
3390 struct touch_point *tp;
3391
3392 wl_list_for_each(tp, &input->touch_point_list, link) {
3393 if (tp->id != id)
3394 continue;
3395
3396 *x = tp->x;
3397 *y = tp->y;
3398 return 0;
3399 }
3400
3401 return -1;
3402}
3403
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003404struct display *
3405input_get_display(struct input *input)
3406{
3407 return input->display;
3408}
3409
Daniel Stone37816df2012-05-16 18:45:18 +01003410struct wl_seat *
3411input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003412{
Daniel Stone37816df2012-05-16 18:45:18 +01003413 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003414}
3415
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003416uint32_t
3417input_get_modifiers(struct input *input)
3418{
3419 return input->modifiers;
3420}
3421
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003422struct widget *
3423input_get_focus_widget(struct input *input)
3424{
3425 return input->focus_widget;
3426}
3427
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003428struct data_offer {
3429 struct wl_data_offer *offer;
3430 struct input *input;
3431 struct wl_array types;
3432 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003433
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003434 struct task io_task;
3435 int fd;
3436 data_func_t func;
3437 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003438 uint32_t dnd_action;
3439 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003440 void *user_data;
3441};
3442
3443static void
3444data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3445{
3446 struct data_offer *offer = data;
3447 char **p;
3448
3449 p = wl_array_add(&offer->types, sizeof *p);
3450 *p = strdup(type);
3451}
3452
Carlos Garnacho9c931792016-01-18 23:52:12 +01003453static void
3454data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3455{
3456 struct data_offer *offer = data;
3457
3458 offer->source_actions = source_actions;
3459}
3460
3461static void
3462data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3463{
3464 struct data_offer *offer = data;
3465
3466 offer->dnd_action = dnd_action;
3467}
3468
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003469static const struct wl_data_offer_listener data_offer_listener = {
3470 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003471 data_offer_source_actions,
3472 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003473};
3474
3475static void
3476data_offer_destroy(struct data_offer *offer)
3477{
3478 char **p;
3479
3480 offer->refcount--;
3481 if (offer->refcount == 0) {
3482 wl_data_offer_destroy(offer->offer);
3483 for (p = offer->types.data; *p; p++)
3484 free(*p);
3485 wl_array_release(&offer->types);
3486 free(offer);
3487 }
3488}
3489
3490static void
3491data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003492 struct wl_data_device *data_device,
3493 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003494{
3495 struct data_offer *offer;
3496
Brian Lovinbc919262013-08-07 15:34:59 -07003497 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003498
3499 wl_array_init(&offer->types);
3500 offer->refcount = 1;
3501 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003502 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003503 wl_data_offer_add_listener(offer->offer,
3504 &data_offer_listener, offer);
3505}
3506
3507static void
3508data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003509 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003510 wl_fixed_t x_w, wl_fixed_t y_w,
3511 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003512{
3513 struct input *input = data;
3514 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003515 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003516 float x = wl_fixed_to_double(x_w);
3517 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003518 char **p;
3519
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003520 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003521 input->drag_enter_serial = serial;
3522 input->drag_focus = window,
3523 input->drag_x = x;
3524 input->drag_y = y;
3525
3526 if (!input->touch_grab)
3527 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003528
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003529 if (offer) {
3530 input->drag_offer = wl_data_offer_get_user_data(offer);
3531
3532 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3533 *p = NULL;
3534
3535 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003536
3537 if (input->display->data_device_manager_version >=
3538 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3539 wl_data_offer_set_actions(offer,
3540 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3541 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3542 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3543 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003544 } else {
3545 input->drag_offer = NULL;
3546 types_data = NULL;
3547 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003548
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003549 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003550 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003551 window->user_data);
3552}
3553
3554static void
3555data_device_leave(void *data, struct wl_data_device *data_device)
3556{
3557 struct input *input = data;
3558
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003559 if (input->drag_offer) {
3560 data_offer_destroy(input->drag_offer);
3561 input->drag_offer = NULL;
3562 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003563}
3564
3565static void
3566data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003567 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003568{
3569 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003570 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003571 float x = wl_fixed_to_double(x_w);
3572 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003573 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003574
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003575 input->drag_x = x;
3576 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003577
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003578 if (input->drag_offer)
3579 types_data = input->drag_offer->types.data;
3580 else
3581 types_data = NULL;
3582
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003583 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003584 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003585 window->user_data);
3586}
3587
3588static void
3589data_device_drop(void *data, struct wl_data_device *data_device)
3590{
3591 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003592 struct window *window = input->drag_focus;
3593 float x, y;
3594
3595 x = input->drag_x;
3596 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003597
3598 if (window->drop_handler)
3599 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003600 x, y, window->user_data);
3601
3602 if (input->touch_grab)
3603 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003604}
3605
3606static void
3607data_device_selection(void *data,
3608 struct wl_data_device *wl_data_device,
3609 struct wl_data_offer *offer)
3610{
3611 struct input *input = data;
3612 char **p;
3613
3614 if (input->selection_offer)
3615 data_offer_destroy(input->selection_offer);
3616
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003617 if (offer) {
3618 input->selection_offer = wl_data_offer_get_user_data(offer);
3619 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3620 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003621 } else {
3622 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003623 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003624}
3625
3626static const struct wl_data_device_listener data_device_listener = {
3627 data_device_data_offer,
3628 data_device_enter,
3629 data_device_leave,
3630 data_device_motion,
3631 data_device_drop,
3632 data_device_selection
3633};
3634
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003635static void
3636input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003637{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003638 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003639 struct wl_cursor *cursor;
3640 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003641
Daniel Stone80972742012-11-07 17:51:39 +11003642 if (!input->pointer)
3643 return;
3644
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003645 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003646 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003647 return;
3648
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003649 if (index >= (int) cursor->image_count) {
3650 fprintf(stderr, "cursor index out of range\n");
3651 return;
3652 }
3653
3654 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003655 buffer = wl_cursor_image_get_buffer(image);
3656 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003657 return;
3658
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003659 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3660 wl_surface_damage(input->pointer_surface, 0, 0,
3661 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003662 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003663 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3664 input->pointer_surface,
3665 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003666}
3667
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003668static const struct wl_callback_listener pointer_surface_listener;
3669
Derek Foreman493d9792015-03-04 16:26:25 -06003670static bool
3671input_set_pointer_special(struct input *input)
3672{
3673 if (input->current_cursor == CURSOR_BLANK) {
3674 wl_pointer_set_cursor(input->pointer,
3675 input->pointer_enter_serial,
3676 NULL, 0, 0);
3677 return true;
3678 }
3679
3680 if (input->current_cursor == CURSOR_UNSET)
3681 return true;
3682
3683 return false;
3684}
3685
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003686static void
Derek Foreman118a4292015-04-22 17:23:35 -05003687schedule_pointer_image_update(struct input *input,
3688 struct wl_cursor *cursor,
3689 uint32_t duration,
3690 bool force_frame)
3691{
3692 /* Some silly cursor sets have enormous pauses in them. In these
3693 * cases it's better to use a timer even if it results in less
3694 * accurate presentation, since it will save us having to set the
3695 * same cursor image over and over again.
3696 *
3697 * This is really not the way we're supposed to time any kind of
3698 * animation, but we're pretending it's OK here because we don't
3699 * want animated cursors with long delays to needlessly hog CPU.
3700 *
3701 * We use force_frame to ensure we don't accumulate large timing
3702 * errors by running off the wrong clock.
3703 */
3704 if (!force_frame && duration > 100) {
3705 struct timespec tp;
3706
3707 clock_gettime(CLOCK_MONOTONIC, &tp);
3708 input->cursor_timer_start = tp.tv_sec * 1000
3709 + tp.tv_nsec / 1000000;
3710 cursor_delay_timer_reset(input, duration);
3711 return;
3712 }
3713
3714 /* for short durations we'll just spin on frame callbacks for
3715 * accurate timing - the way any kind of timing sensitive animation
3716 * should really be done. */
3717 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3718 wl_callback_add_listener(input->cursor_frame_cb,
3719 &pointer_surface_listener, input);
3720
3721}
3722
3723static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003724pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3725 uint32_t time)
3726{
3727 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003728 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003729 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003730 uint32_t duration;
3731 bool force_frame = true;
3732
3733 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003734
3735 if (callback) {
3736 assert(callback == input->cursor_frame_cb);
3737 wl_callback_destroy(callback);
3738 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003739 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003740 }
3741
Daniel Stone80972742012-11-07 17:51:39 +11003742 if (!input->pointer)
3743 return;
3744
Derek Foreman493d9792015-03-04 16:26:25 -06003745 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003746 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003747
Daniel Stonea494f1d2012-06-18 19:31:12 +01003748 cursor = input->display->cursors[input->current_cursor];
3749 if (!cursor)
3750 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003751
3752 /* FIXME We don't have the current time on the first call so we set
3753 * the animation start to the time of the first frame callback. */
3754 if (time == 0)
3755 input->cursor_anim_start = 0;
3756 else if (input->cursor_anim_start == 0)
3757 input->cursor_anim_start = time;
3758
Derek Foreman118a4292015-04-22 17:23:35 -05003759 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003760
Derek Foreman118a4292015-04-22 17:23:35 -05003761 if (time == 0 || input->cursor_anim_start == 0) {
3762 duration = 0;
3763 i = 0;
3764 } else
3765 i = wl_cursor_frame_and_duration(
3766 cursor,
3767 time - input->cursor_anim_start,
3768 &duration);
3769
3770 if (cursor->image_count > 1)
3771 schedule_pointer_image_update(input, cursor, duration,
3772 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003773
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003774 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003775}
3776
Derek Foreman118a4292015-04-22 17:23:35 -05003777static void
3778cursor_timer_func(struct task *task, uint32_t events)
3779{
3780 struct input *input = container_of(task, struct input, cursor_task);
3781 struct timespec tp;
3782 struct wl_cursor *cursor;
3783 uint32_t time;
3784 uint64_t exp;
3785
3786 if (!input->cursor_timer_running)
3787 return;
3788
3789 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3790 return;
3791
3792 cursor = input->display->cursors[input->current_cursor];
3793 if (!cursor)
3794 return;
3795
3796 clock_gettime(CLOCK_MONOTONIC, &tp);
3797 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3798 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3799}
3800
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003801static const struct wl_callback_listener pointer_surface_listener = {
3802 pointer_surface_frame_callback
3803};
3804
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003805void
3806input_set_pointer_image(struct input *input, int pointer)
3807{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003808 int force = 0;
3809
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003810 if (!input->pointer)
3811 return;
3812
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003813 if (input->pointer_enter_serial > input->cursor_serial)
3814 force = 1;
3815
3816 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003817 return;
3818
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003819 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003820 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003821 if (!input->cursor_frame_cb)
3822 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003823 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003824 /* The current frame callback may be stuck if, for instance,
3825 * the set cursor request was processed by the server after
3826 * this client lost the focus. In this case the cursor surface
3827 * might not be mapped and the frame callback wouldn't ever
3828 * complete. Send a set_cursor and attach to try to map the
3829 * cursor surface again so that the callback will finish */
3830 input_set_pointer_image_index(input, 0);
3831 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003832}
3833
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003834struct wl_data_device *
3835input_get_data_device(struct input *input)
3836{
3837 return input->data_device;
3838}
3839
3840void
3841input_set_selection(struct input *input,
3842 struct wl_data_source *source, uint32_t time)
3843{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003844 if (input->data_device)
3845 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003846}
3847
3848void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003849input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003850{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003851 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003852 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003853}
3854
3855static void
3856offer_io_func(struct task *task, uint32_t events)
3857{
3858 struct data_offer *offer =
3859 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003860 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003861 unsigned int len;
3862 char buffer[4096];
3863
3864 len = read(offer->fd, buffer, sizeof buffer);
3865 offer->func(buffer, len,
3866 offer->x, offer->y, offer->user_data);
3867
3868 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003869 if (display->data_device_manager_version >=
3870 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3871 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003872 close(offer->fd);
3873 data_offer_destroy(offer);
3874 }
3875}
3876
3877static void
3878data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3879 data_func_t func, void *user_data)
3880{
3881 int p[2];
3882
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003883 if (pipe2(p, O_CLOEXEC) == -1)
3884 return;
3885
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003886 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3887 close(p[1]);
3888
3889 offer->io_task.run = offer_io_func;
3890 offer->fd = p[0];
3891 offer->func = func;
3892 offer->refcount++;
3893 offer->user_data = user_data;
3894
3895 display_watch_fd(offer->input->display,
3896 offer->fd, EPOLLIN, &offer->io_task);
3897}
3898
3899void
3900input_receive_drag_data(struct input *input, const char *mime_type,
3901 data_func_t func, void *data)
3902{
3903 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003904 input->drag_offer->x = input->drag_x;
3905 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003906}
3907
3908int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003909input_receive_drag_data_to_fd(struct input *input,
3910 const char *mime_type, int fd)
3911{
3912 if (input->drag_offer)
3913 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3914
3915 return 0;
3916}
3917
3918int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003919input_receive_selection_data(struct input *input, const char *mime_type,
3920 data_func_t func, void *data)
3921{
3922 char **p;
3923
3924 if (input->selection_offer == NULL)
3925 return -1;
3926
3927 for (p = input->selection_offer->types.data; *p; p++)
3928 if (strcmp(mime_type, *p) == 0)
3929 break;
3930
3931 if (*p == NULL)
3932 return -1;
3933
3934 data_offer_receive_data(input->selection_offer,
3935 mime_type, func, data);
3936 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003937}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003938
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003939int
3940input_receive_selection_data_to_fd(struct input *input,
3941 const char *mime_type, int fd)
3942{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003943 if (input->selection_offer)
3944 wl_data_offer_receive(input->selection_offer->offer,
3945 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003946
3947 return 0;
3948}
3949
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003950void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003951window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003952{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003953 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003954 return;
3955
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003956 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003957}
3958
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003959static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003960surface_set_synchronized(struct surface *surface)
3961{
3962 if (!surface->subsurface)
3963 return;
3964
3965 if (surface->synchronized)
3966 return;
3967
3968 wl_subsurface_set_sync(surface->subsurface);
3969 surface->synchronized = 1;
3970}
3971
3972static void
3973surface_set_synchronized_default(struct surface *surface)
3974{
3975 if (!surface->subsurface)
3976 return;
3977
3978 if (surface->synchronized == surface->synchronized_default)
3979 return;
3980
3981 if (surface->synchronized_default)
3982 wl_subsurface_set_sync(surface->subsurface);
3983 else
3984 wl_subsurface_set_desync(surface->subsurface);
3985
3986 surface->synchronized = surface->synchronized_default;
3987}
3988
3989static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003990surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003991{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003992 struct widget *widget = surface->widget;
3993 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003994
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003995 if (surface->input_region) {
3996 wl_region_destroy(surface->input_region);
3997 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003998 }
3999
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004000 if (surface->opaque_region)
4001 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004002
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004003 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004004
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004005 if (widget->resize_handler)
4006 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004007 widget->allocation.width,
4008 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004009 widget->user_data);
4010
Pekka Paalanen35e82632013-04-25 13:57:48 +03004011 if (surface->subsurface &&
4012 (surface->allocation.x != widget->allocation.x ||
4013 surface->allocation.y != widget->allocation.y)) {
4014 wl_subsurface_set_position(surface->subsurface,
4015 widget->allocation.x,
4016 widget->allocation.y);
4017 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004018 if (surface->allocation.width != widget->allocation.width ||
4019 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004020 window_schedule_redraw(widget->window);
4021 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004022 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004023
4024 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004025 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004026 widget->allocation.width,
4027 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004028}
4029
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004030static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004031window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004032{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004033 struct surface *surface;
4034
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004035 widget_set_allocation(window->main_surface->widget,
4036 window->pending_allocation.x,
4037 window->pending_allocation.y,
4038 window->pending_allocation.width,
4039 window->pending_allocation.height);
4040
4041 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004042
4043 /* The main surface is in the list, too. Main surface's
4044 * resize_handler is responsible for calling widget_set_allocation()
4045 * on all sub-surface root widgets, so they will be resized
4046 * properly.
4047 */
4048 wl_list_for_each(surface, &window->subsurface_list, link) {
4049 if (surface == window->main_surface)
4050 continue;
4051
4052 surface_set_synchronized(surface);
4053 surface_resize(surface);
4054 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004055
4056 if (!window->fullscreen && !window->maximized)
4057 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004058}
4059
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004060static void
4061idle_resize(struct window *window)
4062{
4063 window->resize_needed = 0;
4064 window->redraw_needed = 1;
4065
4066 DBG("from %dx%d to %dx%d\n",
4067 window->main_surface->server_allocation.width,
4068 window->main_surface->server_allocation.height,
4069 window->pending_allocation.width,
4070 window->pending_allocation.height);
4071
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004072 window_do_resize(window);
4073}
4074
4075static void
4076undo_resize(struct window *window)
4077{
4078 window->pending_allocation.width =
4079 window->main_surface->server_allocation.width;
4080 window->pending_allocation.height =
4081 window->main_surface->server_allocation.height;
4082
4083 DBG("back to %dx%d\n",
4084 window->main_surface->server_allocation.width,
4085 window->main_surface->server_allocation.height);
4086
4087 window_do_resize(window);
4088
4089 if (window->pending_allocation.width == 0 &&
4090 window->pending_allocation.height == 0) {
4091 fprintf(stderr, "Error: Could not draw a surface, "
4092 "most likely due to insufficient disk space in "
4093 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4094 exit(EXIT_FAILURE);
4095 }
4096}
4097
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004098void
4099window_schedule_resize(struct window *window, int width, int height)
4100{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004101 /* We should probably get these numbers from the theme. */
4102 const int min_width = 200, min_height = 200;
4103
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004104 window->pending_allocation.x = 0;
4105 window->pending_allocation.y = 0;
4106 window->pending_allocation.width = width;
4107 window->pending_allocation.height = height;
4108
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004109 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004110 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004111 window->min_allocation.width = min_width;
4112 else
4113 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004114 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004115 window->min_allocation.height = min_height;
4116 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004117 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004118 }
4119
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004120 if (window->pending_allocation.width < window->min_allocation.width)
4121 window->pending_allocation.width = window->min_allocation.width;
4122 if (window->pending_allocation.height < window->min_allocation.height)
4123 window->pending_allocation.height = window->min_allocation.height;
4124
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004125 window->resize_needed = 1;
4126 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004127}
4128
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004129void
4130widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4131{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004132 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004133}
4134
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004135static int
4136window_get_shadow_margin(struct window *window)
4137{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004138 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004139 return frame_get_shadow_margin(window->frame->frame);
4140 else
4141 return 0;
4142}
4143
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004144static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004145handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004146 int32_t width, int32_t height,
4147 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004148{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004149 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004150 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004151
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004152 window->maximized = 0;
4153 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004154 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004155 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004156
4157 wl_array_for_each(p, states) {
4158 uint32_t state = *p;
4159 switch (state) {
4160 case XDG_SURFACE_STATE_MAXIMIZED:
4161 window->maximized = 1;
4162 break;
4163 case XDG_SURFACE_STATE_FULLSCREEN:
4164 window->fullscreen = 1;
4165 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004166 case XDG_SURFACE_STATE_RESIZING:
4167 window->resizing = 1;
4168 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004169 case XDG_SURFACE_STATE_ACTIVATED:
4170 window->focused = 1;
4171 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004172 default:
4173 /* Unknown state */
4174 break;
4175 }
4176 }
4177
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004178 if (window->frame) {
4179 if (window->maximized) {
4180 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4181 } else {
4182 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4183 }
4184
4185 if (window->focused) {
4186 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4187 } else {
4188 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4189 }
4190 }
4191
Jasper St. Pierref184c382014-05-06 08:33:27 -04004192 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004193 /* The width / height params are for window geometry,
4194 * but window_schedule_resize takes allocation. Add
4195 * on the shadow margin to get the difference. */
4196 int margin = window_get_shadow_margin(window);
4197
4198 window_schedule_resize(window,
4199 width + margin * 2,
4200 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004201 } else {
4202 window_schedule_resize(window,
4203 window->saved_allocation.width,
4204 window->saved_allocation.height);
4205 }
4206
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004207 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004208
4209 if (window->state_changed_handler)
4210 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004211}
4212
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004213static void
4214handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4215{
4216 struct window *window = data;
4217 window_close(window);
4218}
4219
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004220static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004221 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004222 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004223};
4224
4225static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004226window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004227{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004228 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004229
4230 if (!window->xdg_surface)
4231 return;
4232
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004233 if (window->parent == window->last_parent)
4234 return;
4235
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004236 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004237 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004238 else
4239 parent_surface = NULL;
4240
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004241 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004242 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004243}
4244
4245static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004246window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004247{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004248 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004249 frame_input_rect(window->frame->frame,
4250 &geometry->x,
4251 &geometry->y,
4252 &geometry->width,
4253 &geometry->height);
4254 else
4255 window_get_allocation(window, geometry);
4256}
4257
4258static void
4259window_sync_geometry(struct window *window)
4260{
4261 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004262
4263 if (!window->xdg_surface)
4264 return;
4265
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004266 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004267 if (geometry.x == window->last_geometry.x &&
4268 geometry.y == window->last_geometry.y &&
4269 geometry.width == window->last_geometry.width &&
4270 geometry.height == window->last_geometry.height)
4271 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004272
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004273 xdg_surface_set_window_geometry(window->xdg_surface,
4274 geometry.x,
4275 geometry.y,
4276 geometry.width,
4277 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004278 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004279}
4280
4281static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004282window_flush(struct window *window)
4283{
4284 struct surface *surface;
4285
4286 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004287 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004288 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004289 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004290 }
4291 }
4292
4293 wl_list_for_each(surface, &window->subsurface_list, link) {
4294 if (surface == window->main_surface)
4295 continue;
4296
4297 surface_flush(surface);
4298 }
4299
4300 surface_flush(window->main_surface);
4301}
4302
4303static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004304menu_destroy(struct menu *menu)
4305{
4306 widget_destroy(menu->widget);
4307 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004308 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004309 free(menu);
4310}
4311
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004312void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004313window_get_allocation(struct window *window,
4314 struct rectangle *allocation)
4315{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004316 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004317}
4318
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004319static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004320widget_redraw(struct widget *widget)
4321{
4322 struct widget *child;
4323
4324 if (widget->redraw_handler)
4325 widget->redraw_handler(widget, widget->user_data);
4326 wl_list_for_each(child, &widget->child_list, link)
4327 widget_redraw(child);
4328}
4329
4330static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004331frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4332{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004333 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004334
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004335 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004336 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004337 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004338 surface->frame_cb = NULL;
4339
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004340 surface->last_time = time;
4341
Pekka Paalanen71233882013-04-25 13:57:53 +03004342 if (surface->redraw_needed || surface->window->redraw_needed) {
4343 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004344 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004345 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004346}
4347
4348static const struct wl_callback_listener listener = {
4349 frame_callback
4350};
4351
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004352static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004353surface_redraw(struct surface *surface)
4354{
Pekka Paalanen71233882013-04-25 13:57:53 +03004355 DBG_OBJ(surface->surface, "begin\n");
4356
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004357 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004358 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004359
4360 /* Whole-window redraw forces a redraw even if the previous has
4361 * not yet hit the screen.
4362 */
4363 if (surface->frame_cb) {
4364 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004365 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004366
Pekka Paalanen71233882013-04-25 13:57:53 +03004367 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004368 wl_callback_destroy(surface->frame_cb);
4369 }
4370
Neil Roberts97b747c2013-12-19 16:17:12 +00004371 if (surface->widget->use_cairo &&
4372 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004373 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004374 return -1;
4375 }
4376
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004377 surface->frame_cb = wl_surface_frame(surface->surface);
4378 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004379 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004380
4381 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004382 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004383 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004384 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004385 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004386}
4387
4388static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004389idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004390{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004391 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004392 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004393 int failed = 0;
4394 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004395
Pekka Paalanen71233882013-04-25 13:57:53 +03004396 DBG(" --------- \n");
4397
Pekka Paalaneneebff542013-04-25 13:57:52 +03004398 wl_list_init(&window->redraw_task.link);
4399 window->redraw_task_scheduled = 0;
4400
4401 if (window->resize_needed) {
4402 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004403 if (window->main_surface->frame_cb) {
4404 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004405 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004406 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004407
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004408 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004409 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004410 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004411
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004412 if (surface_redraw(window->main_surface) < 0) {
4413 /*
4414 * Only main_surface failure will cause us to undo the resize.
4415 * If sub-surfaces fail, they will just be broken with old
4416 * content.
4417 */
4418 failed = 1;
4419 } else {
4420 wl_list_for_each(surface, &window->subsurface_list, link) {
4421 if (surface == window->main_surface)
4422 continue;
4423
4424 surface_redraw(surface);
4425 }
4426 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004427
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004428 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004429 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004430
4431 wl_list_for_each(surface, &window->subsurface_list, link)
4432 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004433
4434 if (resized && failed) {
4435 /* Restore widget tree to correspond to what is on screen. */
4436 undo_resize(window);
4437 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004438}
4439
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004440static void
4441window_schedule_redraw_task(struct window *window)
4442{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004443 if (!window->redraw_task_scheduled) {
4444 window->redraw_task.run = idle_redraw;
4445 display_defer(window->display, &window->redraw_task);
4446 window->redraw_task_scheduled = 1;
4447 }
4448}
4449
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004450void
4451window_schedule_redraw(struct window *window)
4452{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004453 struct surface *surface;
4454
Pekka Paalanen71233882013-04-25 13:57:53 +03004455 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4456
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004457 wl_list_for_each(surface, &window->subsurface_list, link)
4458 surface->redraw_needed = 1;
4459
4460 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004461}
4462
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004463int
4464window_is_fullscreen(struct window *window)
4465{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004466 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004467}
4468
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004469void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004470window_set_fullscreen(struct window *window, int fullscreen)
4471{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004472 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004473 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004474
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004475 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004476 return;
4477
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004478 if (fullscreen)
4479 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4480 else
4481 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004482}
4483
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004484int
4485window_is_maximized(struct window *window)
4486{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004487 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004488}
4489
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004490void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004491window_set_maximized(struct window *window, int maximized)
4492{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004493 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004494 return;
4495
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004496 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004497 return;
4498
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004499 if (maximized)
4500 xdg_surface_set_maximized(window->xdg_surface);
4501 else
4502 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004503}
4504
Jasper St. Pierrede680992014-04-10 17:23:49 -07004505int
4506window_is_resizing(struct window *window)
4507{
4508 return window->resizing;
4509}
4510
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004511void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004512window_set_minimized(struct window *window)
4513{
4514 if (!window->xdg_surface)
4515 return;
4516
4517 xdg_surface_set_minimized(window->xdg_surface);
4518}
4519
4520void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004521window_set_user_data(struct window *window, void *data)
4522{
4523 window->user_data = data;
4524}
4525
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004526void *
4527window_get_user_data(struct window *window)
4528{
4529 return window->user_data;
4530}
4531
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004532void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004533window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004534 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004535{
4536 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004537}
4538
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004539void
4540window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004541 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004542{
4543 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004544}
4545
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004546void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004547window_set_data_handler(struct window *window, window_data_handler_t handler)
4548{
4549 window->data_handler = handler;
4550}
4551
4552void
4553window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4554{
4555 window->drop_handler = handler;
4556}
4557
4558void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004559window_set_close_handler(struct window *window,
4560 window_close_handler_t handler)
4561{
4562 window->close_handler = handler;
4563}
4564
4565void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004566window_set_fullscreen_handler(struct window *window,
4567 window_fullscreen_handler_t handler)
4568{
4569 window->fullscreen_handler = handler;
4570}
4571
4572void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004573window_set_output_handler(struct window *window,
4574 window_output_handler_t handler)
4575{
4576 window->output_handler = handler;
4577}
4578
4579void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004580window_set_state_changed_handler(struct window *window,
4581 window_state_changed_handler_t handler)
4582{
4583 window->state_changed_handler = handler;
4584}
4585
4586void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004587window_set_title(struct window *window, const char *title)
4588{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004589 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004590 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004591 if (window->frame) {
4592 frame_set_title(window->frame->frame, title);
4593 widget_schedule_redraw(window->frame->widget);
4594 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004595 if (window->xdg_surface)
4596 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004597}
4598
4599const char *
4600window_get_title(struct window *window)
4601{
4602 return window->title;
4603}
4604
4605void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004606window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4607{
4608 struct text_cursor_position *text_cursor_position =
4609 window->display->text_cursor_position;
4610
Scott Moreau9295ce02012-06-01 12:46:10 -06004611 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004612 return;
4613
4614 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004615 window->main_surface->surface,
4616 wl_fixed_from_int(x),
4617 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004618}
4619
Casey Dahlin9074db52012-04-19 22:50:09 -04004620static void
4621surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004622 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004623{
Rob Bradford7507b572012-05-15 17:55:34 +01004624 struct window *window = data;
4625 struct output *output;
4626 struct output *output_found = NULL;
4627 struct window_output *window_output;
4628
4629 wl_list_for_each(output, &window->display->output_list, link) {
4630 if (output->output == wl_output) {
4631 output_found = output;
4632 break;
4633 }
4634 }
4635
4636 if (!output_found)
4637 return;
4638
Brian Lovinbc919262013-08-07 15:34:59 -07004639 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004640 window_output->output = output_found;
4641
4642 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004643
4644 if (window->output_handler)
4645 window->output_handler(window, output_found, 1,
4646 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004647}
4648
4649static void
4650surface_leave(void *data,
4651 struct wl_surface *wl_surface, struct wl_output *output)
4652{
Rob Bradford7507b572012-05-15 17:55:34 +01004653 struct window *window = data;
4654 struct window_output *window_output;
4655 struct window_output *window_output_found = NULL;
4656
4657 wl_list_for_each(window_output, &window->window_output_list, link) {
4658 if (window_output->output->output == output) {
4659 window_output_found = window_output;
4660 break;
4661 }
4662 }
4663
4664 if (window_output_found) {
4665 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004666
4667 if (window->output_handler)
4668 window->output_handler(window, window_output->output,
4669 0, window->user_data);
4670
Rob Bradford7507b572012-05-15 17:55:34 +01004671 free(window_output_found);
4672 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004673}
4674
4675static const struct wl_surface_listener surface_listener = {
4676 surface_enter,
4677 surface_leave
4678};
4679
Pekka Paalanen4e373742013-02-13 16:17:13 +02004680static struct surface *
4681surface_create(struct window *window)
4682{
4683 struct display *display = window->display;
4684 struct surface *surface;
4685
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004686 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004687 surface->window = window;
4688 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004689 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004690 wl_surface_add_listener(surface->surface, &surface_listener, window);
4691
Pekka Paalanen35e82632013-04-25 13:57:48 +03004692 wl_list_insert(&window->subsurface_list, &surface->link);
4693
Pekka Paalanen4e373742013-02-13 16:17:13 +02004694 return surface;
4695}
4696
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004697static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004698get_preferred_buffer_type(struct display *display)
4699{
4700#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004701 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004702 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4703#endif
4704
4705 return WINDOW_BUFFER_TYPE_SHM;
4706}
4707
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004708static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004709window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004710{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004711 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004712 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004713
Peter Huttererf3d62272013-08-08 11:57:05 +10004714 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004715 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004716 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004717
4718 surface = surface_create(window);
4719 window->main_surface = surface;
4720
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004721 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004722
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004723 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004724 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004725
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004726 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004727
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004728 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004729 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004730 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004731
Rob Bradford7507b572012-05-15 17:55:34 +01004732 wl_list_init (&window->window_output_list);
4733
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004734 return window;
4735}
4736
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004737struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004738window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004739{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004740 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004741 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004742
4743 window = window_create_internal(display, 0);
4744
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004745 if (window->display->xdg_shell) {
4746 window->xdg_surface =
4747 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4748 window->main_surface->surface);
4749 fail_on_null(window->xdg_surface);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004750
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004751 xdg_surface_set_user_data(window->xdg_surface, window);
4752 xdg_surface_add_listener(window->xdg_surface,
4753 &xdg_surface_listener, window);
4754 } else if (display->ivi_application) {
4755 /* auto generation of ivi_id based on process id + basement of id */
4756 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4757 window->ivi_surface =
4758 ivi_application_surface_create(display->ivi_application,
4759 id_ivisurf, window->main_surface->surface);
4760 fail_on_null(window->ivi_surface);
4761
4762 ivi_surface_add_listener(window->ivi_surface,
4763 &ivi_surface_listener, window);
4764 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004765
4766 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004767}
4768
4769struct window *
4770window_create_custom(struct display *display)
4771{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004772 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004773}
4774
Jasper St. Pierre53686042013-12-09 15:26:25 -05004775void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004776window_set_parent(struct window *window,
4777 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004778{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004779 window->parent = parent_window;
4780 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004781}
4782
4783struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004784window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004785{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004786 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004787}
4788
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004789static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004790menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004791{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004792 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004793 int next;
4794
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004795 frame_interior(menu->frame, &x, &y, &width, &height);
4796 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004797 if (menu->current != next) {
4798 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004799 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004800 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004801}
4802
4803static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004804menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004805 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004806 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004807{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004808 struct menu *menu = data;
4809
4810 if (widget == menu->widget)
4811 menu_set_item(data, y);
4812
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004813 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004814}
4815
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004816static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004817menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004818 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004819{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004820 struct menu *menu = data;
4821
4822 if (widget == menu->widget)
4823 menu_set_item(data, y);
4824
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004825 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004826}
4827
4828static void
4829menu_leave_handler(struct widget *widget, struct input *input, void *data)
4830{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004831 struct menu *menu = data;
4832
4833 if (widget == menu->widget)
4834 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004835}
4836
4837static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004838menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004839 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004840 uint32_t button, enum wl_pointer_button_state state,
4841 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004842
4843{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004844 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004845
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004846 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4847 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004848 /* Either relase after press-drag-release or
4849 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004850 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05004851 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004852 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004853 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004854 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004855 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004856}
4857
4858static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004859menu_touch_up_handler(struct widget *widget,
4860 struct input *input,
4861 uint32_t serial,
4862 uint32_t time,
4863 int32_t id,
4864 void *data)
4865{
4866 struct menu *menu = data;
4867
4868 input_ungrab(input);
4869 menu_destroy(menu);
4870}
4871
4872static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004873menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004874{
4875 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004876 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004877 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004878
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004879 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004880
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004881 frame_repaint(menu->frame, cr);
4882 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004883
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004884 theme_set_background_source(menu->window->display->theme,
4885 cr, THEME_FRAME_ACTIVE);
4886 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004887 cairo_fill(cr);
4888
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004889 cairo_select_font_face(cr, "sans",
4890 CAIRO_FONT_SLANT_NORMAL,
4891 CAIRO_FONT_WEIGHT_NORMAL);
4892 cairo_set_font_size(cr, 12);
4893
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004894 for (i = 0; i < menu->count; i++) {
4895 if (i == menu->current) {
4896 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004897 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004898 cairo_fill(cr);
4899 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004900 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004901 cairo_show_text(cr, menu->entries[i]);
4902 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004903 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4904 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004905 cairo_show_text(cr, menu->entries[i]);
4906 }
4907 }
4908
4909 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004910}
4911
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004912static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004913handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004914{
4915 struct window *window = data;
4916 struct menu *menu = window->main_surface->widget->user_data;
4917
4918 input_ungrab(menu->input);
4919 menu_destroy(menu);
4920}
4921
4922static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004923 handle_popup_popup_done,
4924};
4925
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004926static struct menu *
4927create_menu(struct display *display,
4928 struct input *input, uint32_t time,
4929 menu_func_t func, const char **entries, int count,
4930 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004931{
4932 struct window *window;
4933 struct menu *menu;
4934
4935 menu = malloc(sizeof *menu);
4936 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004937 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004938
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004939 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004940 if (!window) {
4941 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004942 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004943 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004944
4945 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004946 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004947 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004948 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004949 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004950 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004951 menu->entries = entries;
4952 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004953 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004954 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004955 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004956 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004957 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004958
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004959 input_ungrab(input);
4960
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004961 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004962 widget_set_enter_handler(menu->widget, menu_enter_handler);
4963 widget_set_leave_handler(menu->widget, menu_leave_handler);
4964 widget_set_motion_handler(menu->widget, menu_motion_handler);
4965 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004966 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004967
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004968 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004969 frame_resize_inside(menu->frame, 200, count * 20);
4970 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4971 window_schedule_resize(window, frame_width(menu->frame),
4972 frame_height(menu->frame));
4973
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004974 return menu;
4975}
4976
4977struct window *
4978window_create_menu(struct display *display,
4979 struct input *input, uint32_t time,
4980 menu_func_t func, const char **entries, int count,
4981 void *user_data)
4982{
4983 struct menu *menu;
4984 menu = create_menu(display, input, time, func, entries, count, user_data);
4985
4986 if (menu == NULL)
4987 return NULL;
4988
4989 return menu->window;
4990}
4991
4992void
4993window_show_menu(struct display *display,
4994 struct input *input, uint32_t time, struct window *parent,
4995 int32_t x, int32_t y,
4996 menu_func_t func, const char **entries, int count)
4997{
4998 struct menu *menu;
4999 struct window *window;
5000 int32_t ix, iy;
5001
5002 menu = create_menu(display, input, time, func, entries, count, parent);
5003
5004 if (menu == NULL)
5005 return;
5006
5007 window = menu->window;
5008
5009 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5010 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5011
5012 window->x = x;
5013 window->y = y;
5014
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005015 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005016
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005017 if (!display->xdg_shell)
5018 return;
5019
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005020 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
5021 window->main_surface->surface,
5022 parent->main_surface->surface,
5023 input->seat,
5024 display_get_serial(window->display),
5025 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08005026 window->y - iy);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005027 fail_on_null(window->xdg_popup);
5028
5029 xdg_popup_set_user_data(window->xdg_popup, window);
5030 xdg_popup_add_listener(window->xdg_popup,
5031 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005032}
5033
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005034void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005035window_set_buffer_type(struct window *window, enum window_buffer_type type)
5036{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005037 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005038}
5039
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005040enum window_buffer_type
5041window_get_buffer_type(struct window *window)
5042{
5043 return window->main_surface->buffer_type;
5044}
5045
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005046void
5047window_set_preferred_format(struct window *window,
5048 enum preferred_format format)
5049{
5050 window->preferred_format = format;
5051}
5052
Pekka Paalanen35e82632013-04-25 13:57:48 +03005053struct widget *
5054window_add_subsurface(struct window *window, void *data,
5055 enum subsurface_mode default_mode)
5056{
5057 struct widget *widget;
5058 struct surface *surface;
5059 struct wl_surface *parent;
5060 struct wl_subcompositor *subcompo = window->display->subcompositor;
5061
Pekka Paalanen35e82632013-04-25 13:57:48 +03005062 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005063 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005064 widget = widget_create(window, surface, data);
5065 wl_list_init(&widget->link);
5066 surface->widget = widget;
5067
5068 parent = window->main_surface->surface;
5069 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5070 surface->surface,
5071 parent);
5072 surface->synchronized = 1;
5073
5074 switch (default_mode) {
5075 case SUBSURFACE_SYNCHRONIZED:
5076 surface->synchronized_default = 1;
5077 break;
5078 case SUBSURFACE_DESYNCHRONIZED:
5079 surface->synchronized_default = 0;
5080 break;
5081 default:
5082 assert(!"bad enum subsurface_mode");
5083 }
5084
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005085 window->resize_needed = 1;
5086 window_schedule_redraw(window);
5087
Pekka Paalanen35e82632013-04-25 13:57:48 +03005088 return widget;
5089}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005090
5091static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005092display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005093 struct wl_output *wl_output,
5094 int x, int y,
5095 int physical_width,
5096 int physical_height,
5097 int subpixel,
5098 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005099 const char *model,
5100 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005101{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005102 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005103
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005104 output->allocation.x = x;
5105 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005106 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005107
5108 if (output->make)
5109 free(output->make);
5110 output->make = strdup(make);
5111
5112 if (output->model)
5113 free(output->model);
5114 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005115}
5116
5117static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005118display_handle_done(void *data,
5119 struct wl_output *wl_output)
5120{
5121}
5122
5123static void
5124display_handle_scale(void *data,
5125 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005126 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005127{
5128 struct output *output = data;
5129
5130 output->scale = scale;
5131}
5132
5133static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005134display_handle_mode(void *data,
5135 struct wl_output *wl_output,
5136 uint32_t flags,
5137 int width,
5138 int height,
5139 int refresh)
5140{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005141 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005142 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005143
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005144 if (flags & WL_OUTPUT_MODE_CURRENT) {
5145 output->allocation.width = width;
5146 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005147 if (display->output_configure_handler)
5148 (*display->output_configure_handler)(
5149 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005150 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005151}
5152
5153static const struct wl_output_listener output_listener = {
5154 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005155 display_handle_mode,
5156 display_handle_done,
5157 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005158};
5159
5160static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005161display_add_output(struct display *d, uint32_t id)
5162{
5163 struct output *output;
5164
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005165 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005166 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005167 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005168 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005169 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005170 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005171 wl_list_insert(d->output_list.prev, &output->link);
5172
5173 wl_output_add_listener(output->output, &output_listener, output);
5174}
5175
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005176static void
5177output_destroy(struct output *output)
5178{
5179 if (output->destroy_handler)
5180 (*output->destroy_handler)(output, output->user_data);
5181
5182 wl_output_destroy(output->output);
5183 wl_list_remove(&output->link);
5184 free(output);
5185}
5186
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005187static void
5188display_destroy_output(struct display *d, uint32_t id)
5189{
5190 struct output *output;
5191
5192 wl_list_for_each(output, &d->output_list, link) {
5193 if (output->server_output_id == id) {
5194 output_destroy(output);
5195 break;
5196 }
5197 }
5198}
5199
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005200void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005201display_set_global_handler(struct display *display,
5202 display_global_handler_t handler)
5203{
5204 struct global *global;
5205
5206 display->global_handler = handler;
5207 if (!handler)
5208 return;
5209
5210 wl_list_for_each(global, &display->global_list, link)
5211 display->global_handler(display,
5212 global->name, global->interface,
5213 global->version, display->user_data);
5214}
5215
5216void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005217display_set_global_handler_remove(struct display *display,
5218 display_global_handler_t remove_handler)
5219{
5220 display->global_handler_remove = remove_handler;
5221 if (!remove_handler)
5222 return;
5223}
5224
5225void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005226display_set_output_configure_handler(struct display *display,
5227 display_output_handler_t handler)
5228{
5229 struct output *output;
5230
5231 display->output_configure_handler = handler;
5232 if (!handler)
5233 return;
5234
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005235 wl_list_for_each(output, &display->output_list, link) {
5236 if (output->allocation.width == 0 &&
5237 output->allocation.height == 0)
5238 continue;
5239
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005240 (*display->output_configure_handler)(output,
5241 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005242 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005243}
5244
5245void
5246output_set_user_data(struct output *output, void *data)
5247{
5248 output->user_data = data;
5249}
5250
5251void *
5252output_get_user_data(struct output *output)
5253{
5254 return output->user_data;
5255}
5256
5257void
5258output_set_destroy_handler(struct output *output,
5259 display_output_handler_t handler)
5260{
5261 output->destroy_handler = handler;
5262 /* FIXME: implement this, once we have way to remove outputs */
5263}
5264
5265void
Scott Moreau4e072362012-09-29 02:03:11 -06005266output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005267{
Scott Moreau4e072362012-09-29 02:03:11 -06005268 struct rectangle allocation = output->allocation;
5269
5270 switch (output->transform) {
5271 case WL_OUTPUT_TRANSFORM_90:
5272 case WL_OUTPUT_TRANSFORM_270:
5273 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5274 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5275 /* Swap width and height */
5276 allocation.width = output->allocation.height;
5277 allocation.height = output->allocation.width;
5278 break;
5279 }
5280
5281 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005282}
5283
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005284struct wl_output *
5285output_get_wl_output(struct output *output)
5286{
5287 return output->output;
5288}
5289
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005290enum wl_output_transform
5291output_get_transform(struct output *output)
5292{
5293 return output->transform;
5294}
5295
Alexander Larssonafd319a2013-05-22 14:41:27 +02005296uint32_t
5297output_get_scale(struct output *output)
5298{
5299 return output->scale;
5300}
5301
Jason Ekstrand738715d2014-04-02 19:53:50 -05005302const char *
5303output_get_make(struct output *output)
5304{
5305 return output->make;
5306}
5307
5308const char *
5309output_get_model(struct output *output)
5310{
5311 return output->model;
5312}
5313
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005314static void
Daniel Stone97f68542012-05-30 16:32:01 +01005315fini_xkb(struct input *input)
5316{
5317 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005318 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005319}
5320
5321static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005322display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005323{
5324 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005325 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005326
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005327 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005328 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005329 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005330 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005331 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005332 input->pointer_focus = NULL;
5333 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005334 input->seat_version = seat_version;
5335
Rusty Lynch041815a2013-08-08 21:20:38 -07005336 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005337 wl_list_insert(d->input_list.prev, &input->link);
5338
Daniel Stone37816df2012-05-16 18:45:18 +01005339 wl_seat_add_listener(input->seat, &seat_listener, input);
5340 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005341
Jason Ekstranda669bd52014-04-02 19:53:51 -05005342 if (d->data_device_manager) {
5343 input->data_device =
5344 wl_data_device_manager_get_data_device(d->data_device_manager,
5345 input->seat);
5346 wl_data_device_add_listener(input->data_device,
5347 &data_device_listener,
5348 input);
5349 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005350
5351 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005352 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005353
Derek Foreman118a4292015-04-22 17:23:35 -05005354 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5355 TFD_CLOEXEC | TFD_NONBLOCK);
5356 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5357 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005358 set_repeat_info(input, 40, 400);
5359
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005360 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5361 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005362 input->repeat_task.run = keyboard_repeat_func;
5363 display_watch_fd(d, input->repeat_timer_fd,
5364 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005365}
5366
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005367static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005368input_destroy(struct input *input)
5369{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005370 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005371 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005372
5373 if (input->drag_offer)
5374 data_offer_destroy(input->drag_offer);
5375
5376 if (input->selection_offer)
5377 data_offer_destroy(input->selection_offer);
5378
kabeer khan6ce67ec2014-10-20 11:55:29 +05305379 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005380 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305381 wl_data_device_release(input->data_device);
5382 else
5383 wl_data_device_destroy(input->data_device);
5384 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005385 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005386 if (input->touch)
5387 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005388 if (input->pointer)
5389 wl_pointer_release(input->pointer);
5390 if (input->keyboard)
5391 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005392 } else {
5393 if (input->touch)
5394 wl_touch_destroy(input->touch);
5395 if (input->pointer)
5396 wl_pointer_destroy(input->pointer);
5397 if (input->keyboard)
5398 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005399 }
5400
Daniel Stone97f68542012-05-30 16:32:01 +01005401 fini_xkb(input);
5402
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005403 wl_surface_destroy(input->pointer_surface);
5404
Pekka Paalanene1207c72011-12-16 12:02:09 +02005405 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005406 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005407 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005408 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005409 free(input);
5410}
5411
5412static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005413shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5414{
5415 struct display *d = data;
5416
5417 if (format == WL_SHM_FORMAT_RGB565)
5418 d->has_rgb565 = 1;
5419}
5420
5421struct wl_shm_listener shm_listener = {
5422 shm_format
5423};
5424
5425static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005426xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5427{
5428 xdg_shell_pong(shell, serial);
5429}
5430
5431static const struct xdg_shell_listener xdg_shell_listener = {
5432 xdg_shell_ping,
5433};
5434
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005435#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005436#ifdef static_assert
5437static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5438 "Interface version doesn't match implementation version");
5439#endif
5440
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005441static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005442registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5443 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005444{
5445 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005446 struct global *global;
5447
Brian Lovinbc919262013-08-07 15:34:59 -07005448 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005449 global->name = id;
5450 global->interface = strdup(interface);
5451 global->version = version;
5452 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005453
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005454 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005455 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005456 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005457 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005458 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005459 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005460 display_add_input(d, id, version);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005461 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005462 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005463 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005464 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005465 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005466 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005467 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305468 &wl_data_device_manager_interface,
5469 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005470 } else if (strcmp(interface, "xdg_shell") == 0) {
5471 d->xdg_shell = wl_registry_bind(registry, id,
5472 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005473 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005474 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005475 } else if (strcmp(interface, "text_cursor_position") == 0) {
5476 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005477 wl_registry_bind(registry, id,
5478 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005479 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5480 d->subcompositor =
5481 wl_registry_bind(registry, id,
5482 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005483 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005484 else if (strcmp(interface, "ivi_application") == 0) {
5485 d->ivi_application =
5486 wl_registry_bind(registry, id,
5487 &ivi_application_interface, 1);
5488 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005489
5490 if (d->global_handler)
5491 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005492}
5493
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005494static void
5495registry_handle_global_remove(void *data, struct wl_registry *registry,
5496 uint32_t name)
5497{
5498 struct display *d = data;
5499 struct global *global;
5500 struct global *tmp;
5501
5502 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5503 if (global->name != name)
5504 continue;
5505
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005506 if (strcmp(global->interface, "wl_output") == 0)
5507 display_destroy_output(d, name);
5508
5509 /* XXX: Should destroy remaining bound globals */
5510
5511 if (d->global_handler_remove)
5512 d->global_handler_remove(d, name, global->interface,
5513 global->version, d->user_data);
5514
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005515 wl_list_remove(&global->link);
5516 free(global->interface);
5517 free(global);
5518 }
5519}
5520
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005521void *
5522display_bind(struct display *display, uint32_t name,
5523 const struct wl_interface *interface, uint32_t version)
5524{
5525 return wl_registry_bind(display->registry, name, interface, version);
5526}
5527
5528static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005529 registry_handle_global,
5530 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005531};
5532
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005533#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005534static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005535init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005536{
5537 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005538 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005539
Rob Clark6396ed32012-03-11 19:48:41 -05005540#ifdef USE_CAIRO_GLESV2
5541# define GL_BIT EGL_OPENGL_ES2_BIT
5542#else
5543# define GL_BIT EGL_OPENGL_BIT
5544#endif
5545
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005546 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005547 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005548 EGL_RED_SIZE, 1,
5549 EGL_GREEN_SIZE, 1,
5550 EGL_BLUE_SIZE, 1,
5551 EGL_ALPHA_SIZE, 1,
5552 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005553 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005554 EGL_NONE
5555 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005556
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005557#ifdef USE_CAIRO_GLESV2
5558 static const EGLint context_attribs[] = {
5559 EGL_CONTEXT_CLIENT_VERSION, 2,
5560 EGL_NONE
5561 };
5562 EGLint api = EGL_OPENGL_ES_API;
5563#else
5564 EGLint *context_attribs = NULL;
5565 EGLint api = EGL_OPENGL_API;
5566#endif
5567
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005568 d->dpy =
5569 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5570 d->display, NULL);
5571
Yuval Fledel45568f62010-12-06 09:18:12 -05005572 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005573 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005574 return -1;
5575 }
5576
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005577 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005578 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005579 return -1;
5580 }
5581
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005582 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5583 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005584 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005585 return -1;
5586 }
5587
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005588 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005589 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005590 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005591 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005592 return -1;
5593 }
5594
Benjamin Franzke0c991632011-09-27 21:57:31 +02005595 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5596 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005597 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005598 return -1;
5599 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005600
5601 return 0;
5602}
5603
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005604static void
5605fini_egl(struct display *display)
5606{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005607 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005608
5609 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5610 EGL_NO_CONTEXT);
5611
5612 eglTerminate(display->dpy);
5613 eglReleaseThread();
5614}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005615#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005616
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005617static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005618init_dummy_surface(struct display *display)
5619{
5620 int len;
5621 void *data;
5622
5623 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005624 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005625 display->dummy_surface =
5626 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5627 1, 1, len);
5628 display->dummy_surface_data = data;
5629}
5630
5631static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005632handle_display_data(struct task *task, uint32_t events)
5633{
5634 struct display *display =
5635 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005636 struct epoll_event ep;
5637 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005638
5639 display->display_fd_events = events;
5640
5641 if (events & EPOLLERR || events & EPOLLHUP) {
5642 display_exit(display);
5643 return;
5644 }
5645
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005646 if (events & EPOLLIN) {
5647 ret = wl_display_dispatch(display->display);
5648 if (ret == -1) {
5649 display_exit(display);
5650 return;
5651 }
5652 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005653
5654 if (events & EPOLLOUT) {
5655 ret = wl_display_flush(display->display);
5656 if (ret == 0) {
5657 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5658 ep.data.ptr = &display->display_task;
5659 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5660 display->display_fd, &ep);
5661 } else if (ret == -1 && errno != EAGAIN) {
5662 display_exit(display);
5663 return;
5664 }
5665 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005666}
5667
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005668static void
5669log_handler(const char *format, va_list args)
5670{
5671 vfprintf(stderr, format, args);
5672}
5673
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005674struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005675display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005676{
5677 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005678
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005679 wl_log_set_handler_client(log_handler);
5680
Peter Huttererf3d62272013-08-08 11:57:05 +10005681 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005682 if (d == NULL)
5683 return NULL;
5684
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005685 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005686 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005687 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005688 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005689 return NULL;
5690 }
5691
Rob Bradford5ab9c752013-07-26 16:29:43 +01005692 d->xkb_context = xkb_context_new(0);
5693 if (d->xkb_context == NULL) {
5694 fprintf(stderr, "Failed to create XKB context\n");
5695 free(d);
5696 return NULL;
5697 }
5698
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005699 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005700 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005701 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005702 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5703 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005704
5705 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005706 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005707 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005708 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005709
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005710 d->registry = wl_display_get_registry(d->display);
5711 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005712
Marek Chalupaa519d062014-12-05 13:49:40 +01005713 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005714 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5715 return NULL;
5716 }
5717
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005718#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005719 if (init_egl(d) < 0)
5720 fprintf(stderr, "EGL does not seem to work, "
5721 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005722#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005723
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005724 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005725
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005726 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005727
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005728 wl_list_init(&d->window_list);
5729
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005730 init_dummy_surface(d);
5731
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005732 return d;
5733}
5734
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005735static void
5736display_destroy_outputs(struct display *display)
5737{
5738 struct output *tmp;
5739 struct output *output;
5740
5741 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5742 output_destroy(output);
5743}
5744
Pekka Paalanene1207c72011-12-16 12:02:09 +02005745static void
5746display_destroy_inputs(struct display *display)
5747{
5748 struct input *tmp;
5749 struct input *input;
5750
5751 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5752 input_destroy(input);
5753}
5754
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005755void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005756display_destroy(struct display *display)
5757{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005758 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005759 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5760 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005761
5762 if (!wl_list_empty(&display->deferred_list))
5763 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5764
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005765 cairo_surface_destroy(display->dummy_surface);
5766 free(display->dummy_surface_data);
5767
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005768 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005769 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005770
Daniel Stone97f68542012-05-30 16:32:01 +01005771 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005772
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005773 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005774 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005775
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005776#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005777 if (display->argb_device)
5778 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005779#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005780
Pekka Paalanen35e82632013-04-25 13:57:48 +03005781 if (display->subcompositor)
5782 wl_subcompositor_destroy(display->subcompositor);
5783
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005784 if (display->xdg_shell)
5785 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005786
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005787 if (display->ivi_application)
5788 ivi_application_destroy(display->ivi_application);
5789
Pekka Paalanenc2052982011-12-16 11:41:32 +02005790 if (display->shm)
5791 wl_shm_destroy(display->shm);
5792
5793 if (display->data_device_manager)
5794 wl_data_device_manager_destroy(display->data_device_manager);
5795
5796 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005797 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005798
5799 close(display->epoll_fd);
5800
U. Artie Eoff44874d92012-10-02 21:12:35 -07005801 if (!(display->display_fd_events & EPOLLERR) &&
5802 !(display->display_fd_events & EPOLLHUP))
5803 wl_display_flush(display->display);
5804
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005805 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005806 free(display);
5807}
5808
5809void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005810display_set_user_data(struct display *display, void *data)
5811{
5812 display->user_data = data;
5813}
5814
5815void *
5816display_get_user_data(struct display *display)
5817{
5818 return display->user_data;
5819}
5820
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005821struct wl_display *
5822display_get_display(struct display *display)
5823{
5824 return display->display;
5825}
5826
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005827int
5828display_has_subcompositor(struct display *display)
5829{
5830 if (display->subcompositor)
5831 return 1;
5832
5833 wl_display_roundtrip(display->display);
5834
5835 return display->subcompositor != NULL;
5836}
5837
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005838cairo_device_t *
5839display_get_cairo_device(struct display *display)
5840{
5841 return display->argb_device;
5842}
5843
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005844struct output *
5845display_get_output(struct display *display)
5846{
5847 return container_of(display->output_list.next, struct output, link);
5848}
5849
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005850struct wl_compositor *
5851display_get_compositor(struct display *display)
5852{
5853 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005854}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005855
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005856uint32_t
5857display_get_serial(struct display *display)
5858{
5859 return display->serial;
5860}
5861
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005862EGLDisplay
5863display_get_egl_display(struct display *d)
5864{
5865 return d->dpy;
5866}
5867
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005868struct wl_data_source *
5869display_create_data_source(struct display *display)
5870{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005871 if (display->data_device_manager)
5872 return wl_data_device_manager_create_data_source(display->data_device_manager);
5873 else
5874 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005875}
5876
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005877EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005878display_get_argb_egl_config(struct display *d)
5879{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005880 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005881}
5882
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005883int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005884display_acquire_window_surface(struct display *display,
5885 struct window *window,
5886 EGLContext ctx)
5887{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005888 struct surface *surface = window->main_surface;
5889
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005890 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005891 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005892
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005893 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005894 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005895}
5896
5897void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005898display_release_window_surface(struct display *display,
5899 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005900{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005901 struct surface *surface = window->main_surface;
5902
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005903 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005904 return;
5905
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005906 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005907}
5908
5909void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005910display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005911{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005912 wl_list_insert(&display->deferred_list, &task->link);
5913}
5914
5915void
5916display_watch_fd(struct display *display,
5917 int fd, uint32_t events, struct task *task)
5918{
5919 struct epoll_event ep;
5920
5921 ep.events = events;
5922 ep.data.ptr = task;
5923 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5924}
5925
5926void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005927display_unwatch_fd(struct display *display, int fd)
5928{
5929 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5930}
5931
5932void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005933display_run(struct display *display)
5934{
5935 struct task *task;
5936 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005937 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005938
Pekka Paalanen826d7952011-12-15 10:14:07 +02005939 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005940 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005941 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005942 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005943 struct task, link);
5944 wl_list_remove(&task->link);
5945 task->run(task, 0);
5946 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005947
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005948 wl_display_dispatch_pending(display->display);
5949
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005950 if (!display->running)
5951 break;
5952
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005953 ret = wl_display_flush(display->display);
5954 if (ret < 0 && errno == EAGAIN) {
5955 ep[0].events =
5956 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5957 ep[0].data.ptr = &display->display_task;
5958
5959 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5960 display->display_fd, &ep[0]);
5961 } else if (ret < 0) {
5962 break;
5963 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005964
5965 count = epoll_wait(display->epoll_fd,
5966 ep, ARRAY_LENGTH(ep), -1);
5967 for (i = 0; i < count; i++) {
5968 task = ep[i].data.ptr;
5969 task->run(task, ep[i].events);
5970 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005971 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005972}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005973
5974void
5975display_exit(struct display *display)
5976{
5977 display->running = 0;
5978}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005979
Carlos Garnacho9c931792016-01-18 23:52:12 +01005980int
5981display_get_data_device_manager_version(struct display *display)
5982{
5983 return display->data_device_manager_version;
5984}
5985
Jan Arne Petersencd997062012-11-18 19:06:44 +01005986void
5987keysym_modifiers_add(struct wl_array *modifiers_map,
5988 const char *name)
5989{
5990 size_t len = strlen(name) + 1;
5991 char *p;
5992
5993 p = wl_array_add(modifiers_map, len);
5994
5995 if (p == NULL)
5996 return;
5997
5998 strncpy(p, name, len);
5999}
6000
6001static xkb_mod_index_t
6002keysym_modifiers_get_index(struct wl_array *modifiers_map,
6003 const char *name)
6004{
6005 xkb_mod_index_t index = 0;
6006 char *p = modifiers_map->data;
6007
6008 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6009 if (strcmp(p, name) == 0)
6010 return index;
6011
6012 index++;
6013 p += strlen(p) + 1;
6014 }
6015
6016 return XKB_MOD_INVALID;
6017}
6018
6019xkb_mod_mask_t
6020keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6021 const char *name)
6022{
6023 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6024
6025 if (index == XKB_MOD_INVALID)
6026 return XKB_MOD_INVALID;
6027
6028 return 1 << index;
6029}
Kristian Høgsbergce278412013-07-25 15:20:20 -07006030
6031void *
6032fail_on_null(void *p)
6033{
6034 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10006035 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07006036 exit(EXIT_FAILURE);
6037 }
6038
6039 return p;
6040}
6041
6042void *
6043xmalloc(size_t s)
6044{
6045 return fail_on_null(malloc(s));
6046}
6047
Peter Huttererf3d62272013-08-08 11:57:05 +10006048void *
6049xzalloc(size_t s)
6050{
6051 return fail_on_null(zalloc(s));
6052}
6053
Kristian Høgsbergce278412013-07-25 15:20:20 -07006054char *
6055xstrdup(const char *s)
6056{
6057 return fail_on_null(strdup(s));
6058}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08006059
6060void *
6061xrealloc(char *p, size_t s)
6062{
6063 return fail_on_null(realloc(p, s));
6064}