blob: 4de73ed27ae84c7b8df2f6453c308bed55b329e3 [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 Harringtone99e4bf2016-03-16 14:15:18 -070071#include "shared/xalloc.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080072#include "shared/zalloc.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080073#include "xdg-shell-unstable-v5-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060074#include "text-cursor-position-client-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070075#include "shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050076
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050077#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050078
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +090079#include <sys/types.h>
80#include "ivi-application-client-protocol.h"
81#define IVI_SURFACE_ID 9000
82
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070083struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030084
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040085struct global {
86 uint32_t name;
87 char *interface;
88 uint32_t version;
89 struct wl_list link;
90};
91
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050092struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050093 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040094 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050095 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030096 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040097 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040098 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060099 struct text_cursor_position *text_cursor_position;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500100 struct xdg_shell *xdg_shell;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900101 struct ivi_application *ivi_application; /* ivi style shell */
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500103 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200104 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +0200105 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400106 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400107
108 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700109 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400110 struct task display_task;
111
112 int epoll_fd;
113 struct wl_list deferred_list;
114
Pekka Paalanen826d7952011-12-15 10:14:07 +0200115 int running;
116
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400117 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400118 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400119 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500120 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400121
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400122 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400123
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300124 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300125 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400126
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200127 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400128 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800129 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200130
131 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100132
133 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200134
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200135 /* A hack to get text extents for tooltips */
136 cairo_surface_t *dummy_surface;
137 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200138
139 int has_rgb565;
kabeer khan6ce67ec2014-10-20 11:55:29 +0530140 int data_device_manager_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500141};
142
Rob Bradford7507b572012-05-15 17:55:34 +0100143struct window_output {
144 struct output *output;
145 struct wl_list link;
146};
147
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200148struct toysurface {
149 /*
Chris Michaelb50ed172015-11-23 15:13:57 -0500150 * Prepare the surface for drawing. Ensure there is a surface
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800151 * of the right size available for rendering, and return it.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200152 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200153 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200154 * If flags has SURFACE_HINT_RESIZE set, the user is
155 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156 * Returns the Cairo surface to draw to.
157 */
158 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200159 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200160 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161
162 /*
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
166 */
167 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200168 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200169 struct rectangle *server_allocation);
170
171 /*
172 * Make the toysurface current with the given EGL context.
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -0800173 * Returns 0 on success, and negative on failure.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200174 */
175 int (*acquire)(struct toysurface *base, EGLContext ctx);
176
177 /*
178 * Release the toysurface from the EGL context, returning control
179 * to Cairo.
180 */
181 void (*release)(struct toysurface *base);
182
183 /*
184 * Destroy the toysurface, including the Cairo surface, any
185 * backing storage, and the Wayland protocol objects.
186 */
187 void (*destroy)(struct toysurface *base);
188};
189
Pekka Paalanen4e373742013-02-13 16:17:13 +0200190struct surface {
191 struct window *window;
192
193 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300194 struct wl_subsurface *subsurface;
195 int synchronized;
196 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200198 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300199 int redraw_needed;
200 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300201 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200202
203 struct rectangle allocation;
204 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200205
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200206 struct wl_region *input_region;
207 struct wl_region *opaque_region;
208
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209 enum window_buffer_type buffer_type;
210 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200211 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200212
213 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300214
215 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200216};
217
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500218struct window {
219 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100220 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500221 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200222 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400223 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500224 struct rectangle pending_allocation;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200225 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400227 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300228 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400229 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400230 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500231 int custom;
232 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400233
Pekka Paalanen99436862012-11-19 17:15:59 +0200234 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400235
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500236 int fullscreen;
237 int maximized;
238
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200239 enum preferred_format preferred_format;
240
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500241 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500242 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400243 window_data_handler_t data_handler;
244 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500245 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400246 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200247 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700248 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400249
Pekka Paalanen4e373742013-02-13 16:17:13 +0200250 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500251 struct xdg_surface *xdg_surface;
252 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300253
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700254 struct window *parent;
Jasper St. Pierre66bc9492015-02-13 14:01:55 +0800255 struct window *last_parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500256
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +0900257 struct ivi_surface *ivi_surface;
258
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500259 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500260
Pekka Paalanen35e82632013-04-25 13:57:48 +0300261 /* struct surface::link, contains also main_surface */
262 struct wl_list subsurface_list;
263
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500264 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400265 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500266};
267
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500268struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500269 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200270 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300271 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500272 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400273 struct wl_list link;
274 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500275 widget_resize_handler_t resize_handler;
276 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500277 widget_enter_handler_t enter_handler;
278 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500279 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500280 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700281 widget_touch_down_handler_t touch_down_handler;
282 widget_touch_up_handler_t touch_up_handler;
283 widget_touch_motion_handler_t touch_motion_handler;
284 widget_touch_frame_handler_t touch_frame_handler;
285 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200286 widget_axis_handler_t axis_handler;
Peter Hutterer87743e92016-01-18 16:38:22 +1000287 widget_pointer_frame_handler_t pointer_frame_handler;
288 widget_axis_source_handler_t axis_source_handler;
289 widget_axis_stop_handler_t axis_stop_handler;
290 widget_axis_discrete_handler_t axis_discrete_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400291 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500292 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300293 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500294 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000295 /* If this is set to false then no cairo surface will be
296 * created before redrawing the surface. This is useful if the
297 * redraw handler is going to do completely custom rendering
298 * such as using EGL directly */
299 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400300};
301
Rusty Lynch041815a2013-08-08 21:20:38 -0700302struct touch_point {
303 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800304 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct widget *widget;
306 struct wl_list link;
307};
308
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400309struct input {
310 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100311 struct wl_seat *seat;
312 struct wl_pointer *pointer;
313 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700314 struct wl_touch *touch;
315 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316 struct window *pointer_focus;
317 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700318 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300319 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300320 uint32_t cursor_anim_start;
321 struct wl_callback *cursor_frame_cb;
Derek Foreman118a4292015-04-22 17:23:35 -0500322 uint32_t cursor_timer_start;
323 uint32_t cursor_anim_current;
324 int cursor_delay_fd;
325 bool cursor_timer_running;
326 struct task cursor_task;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300327 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400328 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400329 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400330 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400331 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400332 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400333
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500334 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500335 struct widget *grab;
336 uint32_t grab_button;
337
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400338 struct wl_data_device *data_device;
339 struct data_offer *drag_offer;
340 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800341 uint32_t touch_grab;
342 int32_t touch_grab_id;
343 float drag_x, drag_y;
344 struct window *drag_focus;
345 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100346
347 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100348 struct xkb_keymap *keymap;
349 struct xkb_state *state;
350 xkb_mod_mask_t control_mask;
351 xkb_mod_mask_t alt_mask;
352 xkb_mod_mask_t shift_mask;
353 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400354
Jonny Lamb06959082014-08-12 14:58:27 +0200355 int32_t repeat_rate_sec;
356 int32_t repeat_rate_nsec;
357 int32_t repeat_delay_sec;
358 int32_t repeat_delay_nsec;
359
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400360 struct task repeat_task;
361 int repeat_timer_fd;
362 uint32_t repeat_sym;
363 uint32_t repeat_key;
364 uint32_t repeat_time;
Derek Foreman3a1580f2015-10-14 09:39:59 -0500365 int seat_version;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400366};
367
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500368struct output {
369 struct display *display;
370 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800371 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500372 struct rectangle allocation;
373 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600374 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200375 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500376 char *make;
377 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200378
379 display_output_handler_t destroy_handler;
380 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500381};
382
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500383struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500384 struct widget *widget;
385 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500386 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800387
388 uint32_t last_time;
389 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800390 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500391};
392
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500393struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400394 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500395 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500396 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500397 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700398 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500399 const char **entries;
400 uint32_t time;
401 int current;
402 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400403 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500404 menu_func_t func;
405};
406
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300407struct tooltip {
408 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300409 struct widget *widget;
410 char *entry;
411 struct task tooltip_task;
412 int tooltip_fd;
413 float x, y;
414};
415
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700416struct shm_pool {
417 struct wl_shm_pool *pool;
418 size_t size;
419 size_t used;
420 void *data;
421};
422
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400423enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300424 CURSOR_DEFAULT = 100,
425 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400426};
427
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200428static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400429
Pekka Paalanen97777442013-05-22 10:20:05 +0300430/* #define DEBUG */
431
432#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300433
434static void
435debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
436__attribute__ ((format (printf, 4, 5)));
437
438static void
439debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
440{
441 va_list ap;
442 struct timeval tv;
443
444 gettimeofday(&tv, NULL);
445 fprintf(stderr, "%8ld.%03ld ",
446 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
447
448 if (proxy)
449 fprintf(stderr, "%s@%d ",
450 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
451
452 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
453 fprintf(stderr, "%s ", func);
454
455 va_start(ap, fmt);
456 vfprintf(stderr, fmt, ap);
457 va_end(ap);
458}
459
460#define DBG(fmt, ...) \
461 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
462
463#define DBG_OBJ(obj, fmt, ...) \
464 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
465
466#else
467
468#define DBG(...) do {} while (0)
469#define DBG_OBJ(...) do {} while (0)
470
471#endif
472
Alexander Larsson1818e312013-05-22 14:41:31 +0200473static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200474surface_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 +0200475{
476 int32_t tmp;
477
478 switch (buffer_transform) {
479 case WL_OUTPUT_TRANSFORM_90:
480 case WL_OUTPUT_TRANSFORM_270:
481 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
482 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
483 tmp = *width;
484 *width = *height;
485 *height = tmp;
486 break;
487 default:
488 break;
489 }
490
491 *width *= buffer_scale;
492 *height *= buffer_scale;
493}
494
495static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200496buffer_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 +0200497{
498 int32_t tmp;
499
500 switch (buffer_transform) {
501 case WL_OUTPUT_TRANSFORM_90:
502 case WL_OUTPUT_TRANSFORM_270:
503 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
504 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
505 tmp = *width;
506 *width = *height;
507 *height = tmp;
508 break;
509 default:
510 break;
511 }
512
513 *width /= buffer_scale;
514 *height /= buffer_scale;
515}
516
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500517#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400518
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519struct egl_window_surface {
520 struct toysurface base;
521 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100522 struct display *display;
523 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 struct wl_egl_window *egl_window;
525 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100526};
527
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200528static struct egl_window_surface *
529to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100530{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200531 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100532}
533
534static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200535egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200536 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200537 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100538{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200539 struct egl_window_surface *surface = to_egl_window_surface(base);
540
Alexander Larsson1818e312013-05-22 14:41:31 +0200541 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
542
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200543 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
544 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
545
546 return cairo_surface_reference(surface->cairo_surface);
547}
548
549static void
550egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200551 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200552 struct rectangle *server_allocation)
553{
554 struct egl_window_surface *surface = to_egl_window_surface(base);
555
556 cairo_gl_surface_swapbuffers(surface->cairo_surface);
557 wl_egl_window_get_attached_size(surface->egl_window,
558 &server_allocation->width,
559 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200560
561 buffer_to_surface_size (buffer_transform, buffer_scale,
562 &server_allocation->width,
563 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200564}
565
566static int
567egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
568{
569 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200570 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100571
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200572 device = cairo_surface_get_device(surface->cairo_surface);
573 if (!device)
574 return -1;
575
576 if (!ctx) {
577 if (device == surface->display->argb_device)
578 ctx = surface->display->argb_ctx;
579 else
580 assert(0);
581 }
582
583 cairo_device_flush(device);
584 cairo_device_acquire(device);
585 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
586 surface->egl_surface, ctx))
587 fprintf(stderr, "failed to make surface current\n");
588
589 return 0;
590}
591
592static void
593egl_window_surface_release(struct toysurface *base)
594{
595 struct egl_window_surface *surface = to_egl_window_surface(base);
596 cairo_device_t *device;
597
598 device = cairo_surface_get_device(surface->cairo_surface);
599 if (!device)
600 return;
601
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200602 if (!eglMakeCurrent(surface->display->dpy,
603 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200604 fprintf(stderr, "failed to make context current\n");
605
606 cairo_device_release(device);
607}
608
609static void
610egl_window_surface_destroy(struct toysurface *base)
611{
612 struct egl_window_surface *surface = to_egl_window_surface(base);
613 struct display *d = surface->display;
614
615 cairo_surface_destroy(surface->cairo_surface);
616 eglDestroySurface(d->dpy, surface->egl_surface);
617 wl_egl_window_destroy(surface->egl_window);
618 surface->surface = NULL;
619
620 free(surface);
621}
622
623static struct toysurface *
624egl_window_surface_create(struct display *display,
625 struct wl_surface *wl_surface,
626 uint32_t flags,
627 struct rectangle *rectangle)
628{
629 struct egl_window_surface *surface;
630
Pekka Paalanenb3627362012-11-19 17:16:00 +0200631 if (display->dpy == EGL_NO_DISPLAY)
632 return NULL;
633
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800634 surface = zalloc(sizeof *surface);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200635 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100636 return NULL;
637
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200638 surface->base.prepare = egl_window_surface_prepare;
639 surface->base.swap = egl_window_surface_swap;
640 surface->base.acquire = egl_window_surface_acquire;
641 surface->base.release = egl_window_surface_release;
642 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200644 surface->display = display;
645 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400646
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200647 surface->egl_window = wl_egl_window_create(surface->surface,
648 rectangle->width,
649 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650
Jonny Lamb4bdcb572015-03-20 15:26:53 +0100651 surface->egl_surface =
Jonny Lambabff8832015-03-24 13:12:09 +0100652 weston_platform_create_egl_surface(display->dpy,
653 display->argb_config,
654 surface->egl_window, NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100655
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200656 surface->cairo_surface =
657 cairo_gl_surface_create_for_egl(display->argb_device,
658 surface->egl_surface,
659 rectangle->width,
660 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100661
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200662 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100663}
664
Pekka Paalanenb3627362012-11-19 17:16:00 +0200665#else
666
667static struct toysurface *
668egl_window_surface_create(struct display *display,
669 struct wl_surface *wl_surface,
670 uint32_t flags,
671 struct rectangle *rectangle)
672{
673 return NULL;
674}
675
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676#endif
677
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200678struct shm_surface_data {
679 struct wl_buffer *buffer;
680 struct shm_pool *pool;
681};
682
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400683struct wl_buffer *
684display_get_buffer_for_surface(struct display *display,
685 cairo_surface_t *surface)
686{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200687 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400688
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200689 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690
691 return data->buffer;
692}
693
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500694static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700695shm_pool_destroy(struct shm_pool *pool);
696
697static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400698shm_surface_data_destroy(void *p)
699{
700 struct shm_surface_data *data = p;
701
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200702 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700703 if (data->pool)
704 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300705
706 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400707}
708
Kristian Høgsberg16626282012-04-03 11:21:27 -0400709static struct wl_shm_pool *
710make_shm_pool(struct display *display, int size, void **data)
711{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400712 struct wl_shm_pool *pool;
713 int fd;
714
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300715 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400716 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300717 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
718 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400719 return NULL;
720 }
721
722 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400723 if (*data == MAP_FAILED) {
724 fprintf(stderr, "mmap failed: %m\n");
725 close(fd);
726 return NULL;
727 }
728
729 pool = wl_shm_create_pool(display->shm, fd, size);
730
731 close(fd);
732
733 return pool;
734}
735
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700736static struct shm_pool *
737shm_pool_create(struct display *display, size_t size)
738{
739 struct shm_pool *pool = malloc(sizeof *pool);
740
741 if (!pool)
742 return NULL;
743
744 pool->pool = make_shm_pool(display, size, &pool->data);
745 if (!pool->pool) {
746 free(pool);
747 return NULL;
748 }
749
750 pool->size = size;
751 pool->used = 0;
752
753 return pool;
754}
755
756static void *
757shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
758{
759 if (pool->used + size > pool->size)
760 return NULL;
761
762 *offset = pool->used;
763 pool->used += size;
764
765 return (char *) pool->data + *offset;
766}
767
768/* destroy the pool. this does not unmap the memory though */
769static void
770shm_pool_destroy(struct shm_pool *pool)
771{
772 munmap(pool->data, pool->size);
773 wl_shm_pool_destroy(pool->pool);
774 free(pool);
775}
776
777/* Start allocating from the beginning of the pool again */
778static void
779shm_pool_reset(struct shm_pool *pool)
780{
781 pool->used = 0;
782}
783
784static int
785data_length_for_shm_surface(struct rectangle *rect)
786{
787 int stride;
788
789 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
790 rect->width);
791 return stride * rect->height;
792}
793
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500794static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700795display_create_shm_surface_from_pool(struct display *display,
796 struct rectangle *rectangle,
797 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400798{
799 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400800 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400801 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200802 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700803 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400804 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400805
806 data = malloc(sizeof *data);
807 if (data == NULL)
808 return NULL;
809
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200810 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
811 cairo_format = CAIRO_FORMAT_RGB16_565;
812 else
813 cairo_format = CAIRO_FORMAT_ARGB32;
814
815 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700816 length = stride * rectangle->height;
817 data->pool = NULL;
818 map = shm_pool_allocate(pool, length, &offset);
819
820 if (!map) {
821 free(data);
822 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400823 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400825 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200826 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400827 rectangle->width,
828 rectangle->height,
829 stride);
830
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200831 cairo_surface_set_user_data(surface, &shm_surface_data_key,
832 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200834 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
835 format = WL_SHM_FORMAT_RGB565;
836 else {
837 if (flags & SURFACE_OPAQUE)
838 format = WL_SHM_FORMAT_XRGB8888;
839 else
840 format = WL_SHM_FORMAT_ARGB8888;
841 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400842
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200843 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
844 rectangle->width,
845 rectangle->height,
846 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400847
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700848 return surface;
849}
850
851static cairo_surface_t *
852display_create_shm_surface(struct display *display,
853 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200854 struct shm_pool *alternate_pool,
855 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700856{
857 struct shm_surface_data *data;
858 struct shm_pool *pool;
859 cairo_surface_t *surface;
860
Pekka Paalanen99436862012-11-19 17:15:59 +0200861 if (alternate_pool) {
862 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700863 surface = display_create_shm_surface_from_pool(display,
864 rectangle,
865 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200866 alternate_pool);
867 if (surface) {
868 data = cairo_surface_get_user_data(surface,
869 &shm_surface_data_key);
870 goto out;
871 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700872 }
873
874 pool = shm_pool_create(display,
875 data_length_for_shm_surface(rectangle));
876 if (!pool)
877 return NULL;
878
879 surface =
880 display_create_shm_surface_from_pool(display, rectangle,
881 flags, pool);
882
883 if (!surface) {
884 shm_pool_destroy(pool);
885 return NULL;
886 }
887
888 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200889 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700890 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400891
Pekka Paalanen99436862012-11-19 17:15:59 +0200892out:
893 if (data_ret)
894 *data_ret = data;
895
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400896 return surface;
897}
898
nobled7b87cb02011-02-01 18:51:47 +0000899static int
900check_size(struct rectangle *rect)
901{
902 if (rect->width && rect->height)
903 return 0;
904
905 fprintf(stderr, "tried to create surface of "
906 "width: %d, height: %d\n", rect->width, rect->height);
907 return -1;
908}
909
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400910cairo_surface_t *
911display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100912 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400913 struct rectangle *rectangle,
914 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400915{
nobled7b87cb02011-02-01 18:51:47 +0000916 if (check_size(rectangle) < 0)
917 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200918
919 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200920 return display_create_shm_surface(display, rectangle, flags,
921 NULL, NULL);
922}
923
Pekka Paalanena4eda732012-11-19 17:16:02 +0200924struct shm_surface_leaf {
925 cairo_surface_t *cairo_surface;
926 /* 'data' is automatically destroyed, when 'cairo_surface' is */
927 struct shm_surface_data *data;
928
929 struct shm_pool *resize_pool;
930 int busy;
931};
932
933static void
934shm_surface_leaf_release(struct shm_surface_leaf *leaf)
935{
936 if (leaf->cairo_surface)
937 cairo_surface_destroy(leaf->cairo_surface);
938 /* leaf->data already destroyed via cairo private */
939
940 if (leaf->resize_pool)
941 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200942
943 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200944}
945
Pekka Paalanenaef02542013-04-25 13:57:47 +0300946#define MAX_LEAVES 3
947
Pekka Paalanen99436862012-11-19 17:15:59 +0200948struct shm_surface {
949 struct toysurface base;
950 struct display *display;
951 struct wl_surface *surface;
952 uint32_t flags;
953 int dx, dy;
954
Pekka Paalanenaef02542013-04-25 13:57:47 +0300955 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200956 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200957};
958
959static struct shm_surface *
960to_shm_surface(struct toysurface *base)
961{
962 return container_of(base, struct shm_surface, base);
963}
964
Pekka Paalanena4eda732012-11-19 17:16:02 +0200965static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300966shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
967{
968#ifdef DEBUG
969 struct shm_surface_leaf *leaf;
970 char bufs[MAX_LEAVES + 1];
971 int i;
972
973 for (i = 0; i < MAX_LEAVES; i++) {
974 leaf = &surface->leaf[i];
975
976 if (leaf->busy)
977 bufs[i] = 'b';
978 else if (leaf->cairo_surface)
979 bufs[i] = 'a';
980 else
981 bufs[i] = ' ';
982 }
983
984 bufs[MAX_LEAVES] = '\0';
985 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
986#endif
987}
988
989static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200990shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
991{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200992 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300993 struct shm_surface_leaf *leaf;
994 int i;
995 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300996
997 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200998
Pekka Paalanenaef02542013-04-25 13:57:47 +0300999 for (i = 0; i < MAX_LEAVES; i++) {
1000 leaf = &surface->leaf[i];
1001 if (leaf->data && leaf->data->buffer == buffer) {
1002 leaf->busy = 0;
1003 break;
1004 }
1005 }
1006 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001007
Pekka Paalanenaef02542013-04-25 13:57:47 +03001008 /* Leave one free leaf with storage, release others */
1009 free_found = 0;
1010 for (i = 0; i < MAX_LEAVES; i++) {
1011 leaf = &surface->leaf[i];
1012
1013 if (!leaf->cairo_surface || leaf->busy)
1014 continue;
1015
1016 if (!free_found)
1017 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001018 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001019 shm_surface_leaf_release(leaf);
1020 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001021
Pekka Paalanen97777442013-05-22 10:20:05 +03001022 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001023}
1024
1025static const struct wl_buffer_listener shm_surface_buffer_listener = {
1026 shm_surface_buffer_release
1027};
1028
Pekka Paalanen99436862012-11-19 17:15:59 +02001029static cairo_surface_t *
1030shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001031 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001032 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001033{
Pekka Paalanenec076692012-11-30 13:37:27 +02001034 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001035 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001036 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001037 struct shm_surface_leaf *leaf = NULL;
1038 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001039
1040 surface->dx = dx;
1041 surface->dy = dy;
1042
Bryce Harringtona86c3ee2015-03-18 18:42:00 -07001043 /* pick a free buffer, preferably one that already has storage */
Pekka Paalanenaef02542013-04-25 13:57:47 +03001044 for (i = 0; i < MAX_LEAVES; i++) {
1045 if (surface->leaf[i].busy)
1046 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001047
Pekka Paalanenaef02542013-04-25 13:57:47 +03001048 if (!leaf || surface->leaf[i].cairo_surface)
1049 leaf = &surface->leaf[i];
1050 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001051 DBG_OBJ(surface->surface, "pick leaf %d\n",
1052 (int)(leaf - &surface->leaf[0]));
1053
Pekka Paalanenaef02542013-04-25 13:57:47 +03001054 if (!leaf) {
1055 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001056 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001057 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001058 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001059 }
1060
Pekka Paalanena4eda732012-11-19 17:16:02 +02001061 if (!resize_hint && leaf->resize_pool) {
1062 cairo_surface_destroy(leaf->cairo_surface);
1063 leaf->cairo_surface = NULL;
1064 shm_pool_destroy(leaf->resize_pool);
1065 leaf->resize_pool = NULL;
1066 }
1067
Alexander Larsson1818e312013-05-22 14:41:31 +02001068 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1069
Pekka Paalanena4eda732012-11-19 17:16:02 +02001070 if (leaf->cairo_surface &&
1071 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1072 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001073 goto out;
1074
Pekka Paalanena4eda732012-11-19 17:16:02 +02001075 if (leaf->cairo_surface)
1076 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001077
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001078#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001080 /* Create a big pool to allocate from, while continuously
1081 * resizing. Mmapping a new pool in the server
1082 * is relatively expensive, so reusing a pool performs
1083 * better, but may temporarily reserve unneeded memory.
1084 */
1085 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001086 leaf->resize_pool = shm_pool_create(surface->display,
1087 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001088 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001089#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001090
Alexander Larsson1818e312013-05-22 14:41:31 +02001091 rect.width = width;
1092 rect.height = height;
1093
Pekka Paalanena4eda732012-11-19 17:16:02 +02001094 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001095 display_create_shm_surface(surface->display, &rect,
1096 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001097 leaf->resize_pool,
1098 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001099 if (!leaf->cairo_surface)
1100 return NULL;
1101
Pekka Paalanena4eda732012-11-19 17:16:02 +02001102 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001103 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001104
1105out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001106 surface->current = leaf;
1107
1108 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001109}
1110
1111static void
1112shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001113 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001114 struct rectangle *server_allocation)
1115{
1116 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001117 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001118
1119 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001121 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001122 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001123
Alexander Larsson1818e312013-05-22 14:41:31 +02001124 buffer_to_surface_size (buffer_transform, buffer_scale,
1125 &server_allocation->width,
1126 &server_allocation->height);
1127
Pekka Paalanena4eda732012-11-19 17:16:02 +02001128 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001129 surface->dx, surface->dy);
1130 wl_surface_damage(surface->surface, 0, 0,
1131 server_allocation->width, server_allocation->height);
1132 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001133
Pekka Paalanen71233882013-04-25 13:57:53 +03001134 DBG_OBJ(surface->surface, "leaf %d busy\n",
1135 (int)(leaf - &surface->leaf[0]));
1136
Pekka Paalanena4eda732012-11-19 17:16:02 +02001137 leaf->busy = 1;
1138 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001139}
1140
1141static int
1142shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1143{
1144 return -1;
1145}
1146
1147static void
1148shm_surface_release(struct toysurface *base)
1149{
1150}
1151
1152static void
1153shm_surface_destroy(struct toysurface *base)
1154{
1155 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001156 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001157
Pekka Paalanenaef02542013-04-25 13:57:47 +03001158 for (i = 0; i < MAX_LEAVES; i++)
1159 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001160
1161 free(surface);
1162}
1163
1164static struct toysurface *
1165shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1166 uint32_t flags, struct rectangle *rectangle)
1167{
1168 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001169 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001170
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07001171 surface = xzalloc(sizeof *surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001172 surface->base.prepare = shm_surface_prepare;
1173 surface->base.swap = shm_surface_swap;
1174 surface->base.acquire = shm_surface_acquire;
1175 surface->base.release = shm_surface_release;
1176 surface->base.destroy = shm_surface_destroy;
1177
1178 surface->display = display;
1179 surface->surface = wl_surface;
1180 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001181
1182 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001183}
1184
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001185/*
1186 * The following correspondences between file names and cursors was copied
1187 * from: https://bugs.kde.org/attachment.cgi?id=67313
1188 */
1189
1190static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001191 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001192 "sw-resize",
1193 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001194};
1195
1196static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001197 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001198 "se-resize",
1199 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001200};
1201
1202static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001203 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001204 "s-resize",
1205 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001206};
1207
1208static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001209 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001210 "closedhand",
1211 "208530c400c041818281048008011002"
1212};
1213
1214static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001215 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001216 "default",
1217 "top_left_arrow",
1218 "left-arrow"
1219};
1220
1221static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001222 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001223 "w-resize",
1224 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001225};
1226
1227static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001228 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001229 "e-resize",
1230 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001231};
1232
1233static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001234 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001235 "nw-resize",
1236 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001237};
1238
1239static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001240 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001241 "ne-resize",
1242 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001243};
1244
1245static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001246 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001247 "n-resize",
1248 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001249};
1250
1251static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001252 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001253 "ibeam",
1254 "text"
1255};
1256
1257static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001258 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001259 "pointer",
1260 "pointing_hand",
1261 "e29285e634086352946a0e7090d73106"
1262};
1263
1264static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001265 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001266 "wait",
1267 "0426c94ea35c87780ff01dc239897213"
1268};
1269
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001270static const char *move_draggings[] = {
1271 "dnd-move"
1272};
1273
1274static const char *copy_draggings[] = {
1275 "dnd-copy"
1276};
1277
1278static const char *forbidden_draggings[] = {
1279 "dnd-none",
1280 "dnd-no-drop"
1281};
1282
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001283struct cursor_alternatives {
1284 const char **names;
1285 size_t count;
1286};
1287
1288static const struct cursor_alternatives cursors[] = {
1289 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1290 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1291 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1292 {grabbings, ARRAY_LENGTH(grabbings)},
1293 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1294 {left_sides, ARRAY_LENGTH(left_sides)},
1295 {right_sides, ARRAY_LENGTH(right_sides)},
1296 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1297 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1298 {top_sides, ARRAY_LENGTH(top_sides)},
1299 {xterms, ARRAY_LENGTH(xterms)},
1300 {hand1s, ARRAY_LENGTH(hand1s)},
1301 {watches, ARRAY_LENGTH(watches)},
Carlos Garnacho5ccf0472016-01-15 21:14:25 +01001302 {move_draggings, ARRAY_LENGTH(move_draggings)},
1303 {copy_draggings, ARRAY_LENGTH(copy_draggings)},
1304 {forbidden_draggings, ARRAY_LENGTH(forbidden_draggings)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001305};
1306
1307static void
1308create_cursors(struct display *display)
1309{
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001310 const char *config_file;
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001311 struct weston_config *config;
1312 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001313 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001314 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001315 unsigned int i, j;
1316 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001317
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001318 config_file = weston_config_get_name_from_env();
1319 config = weston_config_parse(config_file);
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001320 s = weston_config_get_section(config, "shell", NULL, NULL);
1321 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1322 weston_config_section_get_int(s, "cursor-size", &size, 32);
1323 weston_config_destroy(config);
1324
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001325 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001326 if (!display->cursor_theme) {
1327 fprintf(stderr, "could not load theme '%s'\n", theme);
1328 return;
1329 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001330 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001331 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001332 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001333
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001334 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001335 cursor = NULL;
1336 for (j = 0; !cursor && j < cursors[i].count; ++j)
1337 cursor = wl_cursor_theme_get_cursor(
1338 display->cursor_theme, cursors[i].names[j]);
1339
1340 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001341 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001342 cursors[i].names[0]);
1343
1344 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001345 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001346}
1347
1348static void
1349destroy_cursors(struct display *display)
1350{
1351 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001352 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001353}
1354
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001355struct wl_cursor_image *
1356display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001357{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001358 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001359
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001360 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001361}
1362
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001363static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001364surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001365{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001366 if (!surface->cairo_surface)
1367 return;
1368
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001369 if (surface->opaque_region) {
1370 wl_surface_set_opaque_region(surface->surface,
1371 surface->opaque_region);
1372 wl_region_destroy(surface->opaque_region);
1373 surface->opaque_region = NULL;
1374 }
1375
1376 if (surface->input_region) {
1377 wl_surface_set_input_region(surface->surface,
1378 surface->input_region);
1379 wl_region_destroy(surface->input_region);
1380 surface->input_region = NULL;
1381 }
1382
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001383 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001384 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001385 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001386
Pekka Paalanen89dee002013-02-13 16:17:20 +02001387 cairo_surface_destroy(surface->cairo_surface);
1388 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001389}
1390
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001391int
1392window_has_focus(struct window *window)
1393{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001394 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001395}
1396
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001397static void
1398window_close(struct window *window)
1399{
1400 if (window->close_handler)
1401 window->close_handler(window->user_data);
1402 else
1403 display_exit(window->display);
1404}
1405
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001406struct display *
1407window_get_display(struct window *window)
1408{
1409 return window->display;
1410}
1411
Pekka Paalanen89dee002013-02-13 16:17:20 +02001412static void
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001413handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1414 int32_t width, int32_t height)
1415{
1416 struct window *window = data;
1417
1418 window_schedule_resize(window, width, height);
1419}
1420
1421static const struct ivi_surface_listener ivi_surface_listener = {
1422 handle_ivi_surface_configure,
1423};
1424
1425static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001426surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001427{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001428 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001429 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001430
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001431 if (!surface->toysurface && display->dpy &&
1432 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001433 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001434 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001435 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001436 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001437 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001438 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001439
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001440 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001441 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001442 surface->surface,
1443 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001444
Pekka Paalanen89dee002013-02-13 16:17:20 +02001445 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001446 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001447 allocation.width, allocation.height, flags,
1448 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001449}
1450
1451static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001452window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001453{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001454 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001455 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001456
Pekka Paalanenec076692012-11-30 13:37:27 +02001457 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001458 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001459
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001460 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1461 flags |= SURFACE_HINT_RGB565;
1462
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001463 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001464}
1465
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001466int
1467window_get_buffer_transform(struct window *window)
1468{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001469 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001470}
1471
1472void
1473window_set_buffer_transform(struct window *window,
1474 enum wl_output_transform transform)
1475{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001476 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001477 wl_surface_set_buffer_transform(window->main_surface->surface,
1478 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001479}
1480
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001481void
1482window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001483 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001484{
1485 window->main_surface->buffer_scale = scale;
1486 wl_surface_set_buffer_scale(window->main_surface->surface,
1487 scale);
1488}
1489
1490uint32_t
1491window_get_buffer_scale(struct window *window)
1492{
1493 return window->main_surface->buffer_scale;
1494}
1495
Alexander Larssond68f5232013-05-22 14:41:33 +02001496uint32_t
1497window_get_output_scale(struct window *window)
1498{
1499 struct window_output *window_output;
1500 struct window_output *window_output_tmp;
1501 int scale = 1;
1502
1503 wl_list_for_each_safe(window_output, window_output_tmp,
1504 &window->window_output_list, link) {
1505 if (window_output->output->scale > scale)
1506 scale = window_output->output->scale;
1507 }
1508
1509 return scale;
1510}
1511
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001512static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001513
Pekka Paalanen4e373742013-02-13 16:17:13 +02001514static void
1515surface_destroy(struct surface *surface)
1516{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001517 if (surface->frame_cb)
1518 wl_callback_destroy(surface->frame_cb);
1519
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001520 if (surface->input_region)
1521 wl_region_destroy(surface->input_region);
1522
1523 if (surface->opaque_region)
1524 wl_region_destroy(surface->opaque_region);
1525
Pekka Paalanen35e82632013-04-25 13:57:48 +03001526 if (surface->subsurface)
1527 wl_subsurface_destroy(surface->subsurface);
1528
Pekka Paalanen4e373742013-02-13 16:17:13 +02001529 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001530
1531 if (surface->toysurface)
1532 surface->toysurface->destroy(surface->toysurface);
1533
Pekka Paalanen35e82632013-04-25 13:57:48 +03001534 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001535 free(surface);
1536}
1537
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001538void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001539window_destroy(struct window *window)
1540{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001541 struct display *display = window->display;
1542 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001543 struct window_output *window_output;
1544 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001545
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001546 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001547
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001548 wl_list_for_each(input, &display->input_list, link) {
Rusty Lynch1084da52013-08-15 09:10:08 -07001549 if (input->touch_focus == window)
1550 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001551 if (input->pointer_focus == window)
1552 input->pointer_focus = NULL;
1553 if (input->keyboard_focus == window)
1554 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001555 if (input->focus_widget &&
1556 input->focus_widget->window == window)
1557 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001558 }
1559
Rob Bradford7507b572012-05-15 17:55:34 +01001560 wl_list_for_each_safe(window_output, window_output_tmp,
1561 &window->window_output_list, link) {
1562 free (window_output);
1563 }
1564
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001565 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001566 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001567
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001568 if (window->xdg_surface)
1569 xdg_surface_destroy(window->xdg_surface);
1570 if (window->xdg_popup)
1571 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001572
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09001573 if (window->ivi_surface)
1574 ivi_surface_destroy(window->ivi_surface);
1575
Pekka Paalanen4e373742013-02-13 16:17:13 +02001576 surface_destroy(window->main_surface);
1577
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001578 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001579
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001580 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001581 free(window);
1582}
1583
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001584static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001585widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001586{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001588
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001589 wl_list_for_each(child, &widget->child_list, link) {
1590 target = widget_find_widget(child, x, y);
1591 if (target)
1592 return target;
1593 }
1594
1595 if (widget->allocation.x <= x &&
1596 x < widget->allocation.x + widget->allocation.width &&
1597 widget->allocation.y <= y &&
1598 y < widget->allocation.y + widget->allocation.height) {
1599 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001600 }
1601
1602 return NULL;
1603}
1604
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001605static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001606window_find_widget(struct window *window, int32_t x, int32_t y)
1607{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001608 struct surface *surface;
1609 struct widget *widget;
1610
1611 wl_list_for_each(surface, &window->subsurface_list, link) {
1612 widget = widget_find_widget(surface->widget, x, y);
1613 if (widget)
1614 return widget;
1615 }
1616
1617 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001618}
1619
1620static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001621widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001622{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001623 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001624
Peter Huttererf3d62272013-08-08 11:57:05 +10001625 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001626 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001627 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001628 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001629 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001630 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001631 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001632 widget->tooltip = NULL;
1633 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001634 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001635 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001636
1637 return widget;
1638}
1639
1640struct widget *
1641window_add_widget(struct window *window, void *data)
1642{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001643 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001644
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001645 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001646 wl_list_init(&widget->link);
1647 window->main_surface->widget = widget;
1648
1649 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001650}
1651
1652struct widget *
1653widget_add_widget(struct widget *parent, void *data)
1654{
1655 struct widget *widget;
1656
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001657 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001658 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001659
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001660 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001661}
1662
1663void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001664widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001665{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001666 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001667 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001668 struct input *input;
1669
Pekka Paalanen35e82632013-04-25 13:57:48 +03001670 /* Destroy the sub-surface along with the root widget */
1671 if (surface->widget == widget && surface->subsurface)
1672 surface_destroy(widget->surface);
1673
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001674 if (widget->tooltip)
1675 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001676
Pekka Paalanene156fb62012-01-19 13:51:38 +02001677 wl_list_for_each(input, &display->input_list, link) {
1678 if (input->focus_widget == widget)
1679 input->focus_widget = NULL;
1680 }
1681
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001682 wl_list_remove(&widget->link);
1683 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001684}
1685
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001686void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001687widget_set_default_cursor(struct widget *widget, int cursor)
1688{
1689 widget->default_cursor = cursor;
1690}
1691
1692void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001693widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001695 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001696}
1697
1698void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001699widget_set_size(struct widget *widget, int32_t width, int32_t height)
1700{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001701 widget->allocation.width = width;
1702 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001703}
1704
1705void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001706widget_set_allocation(struct widget *widget,
1707 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001708{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001709 widget->allocation.x = x;
1710 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001711 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001712}
1713
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001714void
1715widget_set_transparent(struct widget *widget, int transparent)
1716{
1717 widget->opaque = !transparent;
1718}
1719
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001720void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001721widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001722{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001723 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001724}
1725
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001726static cairo_surface_t *
1727widget_get_cairo_surface(struct widget *widget)
1728{
1729 struct surface *surface = widget->surface;
1730 struct window *window = widget->window;
1731
Neil Roberts97b747c2013-12-19 16:17:12 +00001732 assert(widget->use_cairo);
1733
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001734 if (!surface->cairo_surface) {
1735 if (surface == window->main_surface)
1736 window_create_main_surface(window);
1737 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001738 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001739 }
1740
1741 return surface->cairo_surface;
1742}
1743
Alexander Larsson15901f02013-05-22 14:41:25 +02001744static void
1745widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1746{
1747 struct surface *surface = widget->surface;
1748 double angle;
1749 cairo_matrix_t m;
1750 enum wl_output_transform transform;
1751 int surface_width, surface_height;
1752 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001753 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001754
1755 surface_width = surface->allocation.width;
1756 surface_height = surface->allocation.height;
1757
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001758 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001759 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001760
Alexander Larsson15901f02013-05-22 14:41:25 +02001761 switch (transform) {
1762 case WL_OUTPUT_TRANSFORM_FLIPPED:
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1764 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1765 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1766 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1767 break;
1768 default:
1769 cairo_matrix_init_identity(&m);
1770 break;
1771 }
1772
1773 switch (transform) {
1774 case WL_OUTPUT_TRANSFORM_NORMAL:
1775 default:
1776 angle = 0;
1777 translate_x = 0;
1778 translate_y = 0;
1779 break;
1780 case WL_OUTPUT_TRANSFORM_FLIPPED:
1781 angle = 0;
1782 translate_x = surface_width;
1783 translate_y = 0;
1784 break;
1785 case WL_OUTPUT_TRANSFORM_90:
1786 angle = M_PI_2;
1787 translate_x = surface_height;
1788 translate_y = 0;
1789 break;
1790 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1791 angle = M_PI_2;
1792 translate_x = surface_height;
1793 translate_y = surface_width;
1794 break;
1795 case WL_OUTPUT_TRANSFORM_180:
1796 angle = M_PI;
1797 translate_x = surface_width;
1798 translate_y = surface_height;
1799 break;
1800 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1801 angle = M_PI;
1802 translate_x = 0;
1803 translate_y = surface_height;
1804 break;
1805 case WL_OUTPUT_TRANSFORM_270:
1806 angle = M_PI + M_PI_2;
1807 translate_x = 0;
1808 translate_y = surface_width;
1809 break;
1810 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1811 angle = M_PI + M_PI_2;
1812 translate_x = 0;
1813 translate_y = 0;
1814 break;
1815 }
1816
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001817 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001818 cairo_translate(cr, translate_x, translate_y);
1819 cairo_rotate(cr, angle);
1820 cairo_transform(cr, &m);
1821}
1822
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001823cairo_t *
1824widget_cairo_create(struct widget *widget)
1825{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001826 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001827 cairo_surface_t *cairo_surface;
1828 cairo_t *cr;
1829
1830 cairo_surface = widget_get_cairo_surface(widget);
1831 cr = cairo_create(cairo_surface);
1832
Alexander Larsson15901f02013-05-22 14:41:25 +02001833 widget_cairo_update_transform(widget, cr);
1834
Pekka Paalanen35e82632013-04-25 13:57:48 +03001835 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1836
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001837 return cr;
1838}
1839
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001840struct wl_surface *
1841widget_get_wl_surface(struct widget *widget)
1842{
1843 return widget->surface->surface;
1844}
1845
Neil Roberts5e10a042013-09-09 00:40:17 +01001846struct wl_subsurface *
1847widget_get_wl_subsurface(struct widget *widget)
1848{
1849 return widget->surface->subsurface;
1850}
1851
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001852uint32_t
1853widget_get_last_time(struct widget *widget)
1854{
1855 return widget->surface->last_time;
1856}
1857
1858void
1859widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1860{
1861 struct wl_compositor *comp = widget->window->display->compositor;
1862 struct surface *surface = widget->surface;
1863
1864 if (!surface->input_region)
1865 surface->input_region = wl_compositor_create_region(comp);
1866
1867 if (rect) {
1868 wl_region_add(surface->input_region,
1869 rect->x, rect->y, rect->width, rect->height);
1870 }
1871}
1872
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001873void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001874widget_set_resize_handler(struct widget *widget,
1875 widget_resize_handler_t handler)
1876{
1877 widget->resize_handler = handler;
1878}
1879
1880void
1881widget_set_redraw_handler(struct widget *widget,
1882 widget_redraw_handler_t handler)
1883{
1884 widget->redraw_handler = handler;
1885}
1886
1887void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001888widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001889{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001890 widget->enter_handler = handler;
1891}
1892
1893void
1894widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1895{
1896 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001897}
1898
1899void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001900widget_set_motion_handler(struct widget *widget,
1901 widget_motion_handler_t handler)
1902{
1903 widget->motion_handler = handler;
1904}
1905
1906void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001907widget_set_button_handler(struct widget *widget,
1908 widget_button_handler_t handler)
1909{
1910 widget->button_handler = handler;
1911}
1912
1913void
Rusty Lynch041815a2013-08-08 21:20:38 -07001914widget_set_touch_up_handler(struct widget *widget,
1915 widget_touch_up_handler_t handler)
1916{
1917 widget->touch_up_handler = handler;
1918}
1919
1920void
1921widget_set_touch_down_handler(struct widget *widget,
1922 widget_touch_down_handler_t handler)
1923{
1924 widget->touch_down_handler = handler;
1925}
1926
1927void
1928widget_set_touch_motion_handler(struct widget *widget,
1929 widget_touch_motion_handler_t handler)
1930{
1931 widget->touch_motion_handler = handler;
1932}
1933
1934void
1935widget_set_touch_frame_handler(struct widget *widget,
1936 widget_touch_frame_handler_t handler)
1937{
1938 widget->touch_frame_handler = handler;
1939}
1940
1941void
1942widget_set_touch_cancel_handler(struct widget *widget,
1943 widget_touch_cancel_handler_t handler)
1944{
1945 widget->touch_cancel_handler = handler;
1946}
1947
1948void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001949widget_set_axis_handler(struct widget *widget,
1950 widget_axis_handler_t handler)
1951{
1952 widget->axis_handler = handler;
1953}
1954
Peter Hutterer87743e92016-01-18 16:38:22 +10001955void
1956widget_set_pointer_frame_handler(struct widget *widget,
1957 widget_pointer_frame_handler_t handler)
1958{
1959 widget->pointer_frame_handler = handler;
1960}
1961
1962void
1963widget_set_axis_handlers(struct widget *widget,
1964 widget_axis_handler_t axis_handler,
1965 widget_axis_source_handler_t axis_source_handler,
1966 widget_axis_stop_handler_t axis_stop_handler,
1967 widget_axis_discrete_handler_t axis_discrete_handler)
1968{
1969 widget->axis_handler = axis_handler;
1970 widget->axis_source_handler = axis_source_handler;
1971 widget->axis_stop_handler = axis_stop_handler;
1972 widget->axis_discrete_handler = axis_discrete_handler;
1973}
1974
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001975static void
1976window_schedule_redraw_task(struct window *window);
1977
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001978void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001979widget_schedule_redraw(struct widget *widget)
1980{
Pekka Paalanen71233882013-04-25 13:57:53 +03001981 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001982 widget->surface->redraw_needed = 1;
1983 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001984}
1985
Neil Roberts97b747c2013-12-19 16:17:12 +00001986void
1987widget_set_use_cairo(struct widget *widget,
1988 int use_cairo)
1989{
1990 widget->use_cairo = use_cairo;
1991}
1992
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001993cairo_surface_t *
1994window_get_surface(struct window *window)
1995{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001996 cairo_surface_t *cairo_surface;
1997
1998 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1999
2000 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04002001}
2002
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002003struct wl_surface *
2004window_get_wl_surface(struct window *window)
2005{
Pekka Paalanen4e373742013-02-13 16:17:13 +02002006 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002007}
2008
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002009static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002010tooltip_redraw_handler(struct widget *widget, void *data)
2011{
2012 cairo_t *cr;
2013 const int32_t r = 3;
2014 struct tooltip *tooltip = data;
2015 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002016
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002017 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002018 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2020 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2021 cairo_paint(cr);
2022
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02002023 width = widget->allocation.width;
2024 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002025 rounded_rect(cr, 0, 0, width, height, r);
2026
2027 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2028 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2029 cairo_fill(cr);
2030
2031 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2032 cairo_move_to(cr, 10, 16);
2033 cairo_show_text(cr, tooltip->entry);
2034 cairo_destroy(cr);
2035}
2036
2037static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002038get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002039{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002040 cairo_t *cr;
2041 cairo_text_extents_t extents;
2042
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08002043 /* Use the dummy_surface because the tooltip's surface was not
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02002044 * created yet, and parent does not have a valid surface
2045 * outside repaint, either.
2046 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002047 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002048 cairo_text_extents(cr, tooltip->entry, &extents);
2049 cairo_destroy(cr);
2050
2051 return extents;
2052}
2053
2054static int
2055window_create_tooltip(struct tooltip *tooltip)
2056{
2057 struct widget *parent = tooltip->parent;
2058 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002059 const int offset_y = 27;
2060 const int margin = 3;
2061 cairo_text_extents_t extents;
2062
2063 if (tooltip->widget)
2064 return 0;
2065
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002066 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002067
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002068 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002069 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002070 widget_set_allocation(tooltip->widget,
2071 tooltip->x, tooltip->y + offset_y,
2072 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002073
2074 return 0;
2075}
2076
2077void
2078widget_destroy_tooltip(struct widget *parent)
2079{
2080 struct tooltip *tooltip = parent->tooltip;
2081
2082 parent->tooltip_count = 0;
2083 if (!tooltip)
2084 return;
2085
2086 if (tooltip->widget) {
2087 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002088 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002089 }
2090
2091 close(tooltip->tooltip_fd);
2092 free(tooltip->entry);
2093 free(tooltip);
2094 parent->tooltip = NULL;
2095}
2096
2097static void
2098tooltip_func(struct task *task, uint32_t events)
2099{
2100 struct tooltip *tooltip =
2101 container_of(task, struct tooltip, tooltip_task);
2102 uint64_t exp;
2103
Martin Olsson8df662a2012-07-08 03:03:47 +02002104 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2105 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002106 window_create_tooltip(tooltip);
2107}
2108
2109#define TOOLTIP_TIMEOUT 500
2110static int
2111tooltip_timer_reset(struct tooltip *tooltip)
2112{
2113 struct itimerspec its;
2114
2115 its.it_interval.tv_sec = 0;
2116 its.it_interval.tv_nsec = 0;
2117 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2118 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2119 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2120 fprintf(stderr, "could not set timerfd\n: %m");
2121 return -1;
2122 }
2123
2124 return 0;
2125}
2126
2127int
2128widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2129{
2130 struct tooltip *tooltip = parent->tooltip;
2131
2132 parent->tooltip_count++;
2133 if (tooltip) {
2134 tooltip->x = x;
2135 tooltip->y = y;
2136 tooltip_timer_reset(tooltip);
2137 return 0;
2138 }
2139
2140 /* the handler might be triggered too fast via input device motion, so
2141 * we need this check here to make sure tooltip is fully initialized */
2142 if (parent->tooltip_count > 1)
2143 return 0;
2144
2145 tooltip = malloc(sizeof *tooltip);
2146 if (!tooltip)
2147 return -1;
2148
2149 parent->tooltip = tooltip;
2150 tooltip->parent = parent;
2151 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002152 tooltip->x = x;
2153 tooltip->y = y;
2154 tooltip->entry = strdup(entry);
2155 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2156 if (tooltip->tooltip_fd < 0) {
2157 fprintf(stderr, "could not create timerfd\n: %m");
2158 return -1;
2159 }
2160
2161 tooltip->tooltip_task.run = tooltip_func;
2162 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2163 EPOLLIN, &tooltip->tooltip_task);
2164 tooltip_timer_reset(tooltip);
2165
2166 return 0;
2167}
2168
2169static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170frame_resize_handler(struct widget *widget,
2171 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002172{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002173 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002174 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002175 struct rectangle interior;
2176 struct rectangle input;
2177 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002178
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002179 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002180 interior.x = 0;
2181 interior.y = 0;
2182 interior.width = width;
2183 interior.height = height;
2184 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002185 frame_resize(frame->frame, width, height);
2186 frame_interior(frame->frame, &interior.x, &interior.y,
2187 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002188 }
2189
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 widget_set_allocation(child, interior.x, interior.y,
2191 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 if (child->resize_handler) {
2194 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002195 child->user_data);
2196
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002197 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 width = child->allocation.width;
2199 height = child->allocation.height;
2200 } else {
2201 frame_resize_inside(frame->frame,
2202 child->allocation.width,
2203 child->allocation.height);
2204 width = frame_width(frame->frame);
2205 height = frame_height(frame->frame);
2206 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002207 }
2208
Scott Moreauf7e498c2012-05-14 11:39:29 -06002209 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002210
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002211 widget->surface->input_region =
2212 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002213 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002214 frame_input_rect(frame->frame, &input.x, &input.y,
2215 &input.width, &input.height);
2216 wl_region_add(widget->surface->input_region,
2217 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002218 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002219 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002220 }
2221
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002222 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002223
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002224 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002225 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002226 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2227 &opaque.width, &opaque.height);
2228
2229 wl_region_add(widget->surface->opaque_region,
2230 opaque.x, opaque.y,
2231 opaque.width, opaque.height);
2232 } else {
2233 wl_region_add(widget->surface->opaque_region,
2234 0, 0, width, height);
2235 }
Martin Minarik1998b152012-05-10 02:04:35 +02002236 }
2237
Martin Minarik1998b152012-05-10 02:04:35 +02002238
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002239 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002240}
2241
2242static void
2243frame_redraw_handler(struct widget *widget, void *data)
2244{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002245 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002246 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002247 struct window *window = widget->window;
2248
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002249 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002250 return;
2251
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002252 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002253
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002254 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002255
2256 cairo_destroy(cr);
2257}
2258
2259static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002260frame_get_pointer_image_for_location(struct window_frame *frame,
2261 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002263 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002264
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002265 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002266 return CURSOR_LEFT_PTR;
2267
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002268 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002269 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002270 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002271 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002272 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002273 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002274 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002275 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002276 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002277 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002278 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002279 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002280 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002281 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002282 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002283 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002284 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002285 case THEME_LOCATION_EXTERIOR:
2286 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002287 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002288 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002289 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002290}
2291
Pekka Paalanen26237862014-09-10 15:10:30 +03002292static void
2293frame_menu_func(void *data, struct input *input, int index)
2294{
2295 struct window *window = data;
Pekka Paalanen26237862014-09-10 15:10:30 +03002296
2297 switch (index) {
2298 case 0: /* close */
2299 window_close(window);
2300 break;
Jonas Ådahl5b0b7702015-11-17 16:00:35 +08002301 case 1: /* fullscreen */
Pekka Paalanen26237862014-09-10 15:10:30 +03002302 /* we don't have a way to get out of fullscreen for now */
2303 if (window->fullscreen_handler)
2304 window->fullscreen_handler(window, window->user_data);
2305 break;
2306 }
2307}
2308
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002309void
2310window_show_frame_menu(struct window *window,
2311 struct input *input, uint32_t time)
2312{
2313 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002314 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002315
Pekka Paalanen26237862014-09-10 15:10:30 +03002316 static const char *entries[] = {
2317 "Close",
Pekka Paalanen26237862014-09-10 15:10:30 +03002318 "Fullscreen"
2319 };
2320
2321 if (window->fullscreen_handler)
2322 count = ARRAY_LENGTH(entries);
2323 else
2324 count = ARRAY_LENGTH(entries) - 1;
2325
2326 input_get_position(input, &x, &y);
2327 window_show_menu(window->display, input, time, window,
2328 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002329}
2330
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002331static int
2332frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002333 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002334{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002335 struct window_frame *frame = data;
2336 enum theme_location location;
2337
2338 location = frame_pointer_enter(frame->frame, input, x, y);
2339 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2340 widget_schedule_redraw(frame->widget);
2341
2342 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002343}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002344
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002345static int
2346frame_motion_handler(struct widget *widget,
2347 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002348 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002349{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002350 struct window_frame *frame = data;
2351 enum theme_location location;
2352
2353 location = frame_pointer_motion(frame->frame, input, x, y);
2354 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2355 widget_schedule_redraw(frame->widget);
2356
2357 return frame_get_pointer_image_for_location(data, location);
2358}
2359
2360static void
2361frame_leave_handler(struct widget *widget,
2362 struct input *input, void *data)
2363{
2364 struct window_frame *frame = data;
2365
2366 frame_pointer_leave(frame->frame, input);
2367 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2368 widget_schedule_redraw(frame->widget);
2369}
2370
2371static void
2372frame_handle_status(struct window_frame *frame, struct input *input,
2373 uint32_t time, enum theme_location location)
2374{
2375 struct window *window = frame->widget->window;
2376 uint32_t status;
2377
2378 status = frame_status(frame->frame);
2379 if (status & FRAME_STATUS_REPAINT)
2380 widget_schedule_redraw(frame->widget);
2381
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002382 if (status & FRAME_STATUS_MINIMIZE) {
2383 window_set_minimized(window);
2384 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2385 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002386
2387 if (status & FRAME_STATUS_MENU) {
2388 window_show_frame_menu(window, input, time);
2389 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2390 }
2391
2392 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002393 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002394 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2395 }
2396
2397 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002398 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002399 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002400 }
2401
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002402 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002403 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002404 xdg_surface_move(window->xdg_surface,
2405 input_get_seat(input),
2406 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002407
2408 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2409 }
2410
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002411 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002412 input_ungrab(input);
2413
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002414 xdg_surface_resize(window->xdg_surface,
2415 input_get_seat(input),
2416 window->display->serial,
2417 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002418
2419 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2420 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002422
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002423#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002424static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425frame_button_handler(struct widget *widget,
2426 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002427 uint32_t button, enum wl_pointer_button_state state,
2428 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002429
2430{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002431 struct window_frame *frame = data;
2432 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002433
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002434 frame->double_click = 0;
2435 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2436 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2437 frame->double_click = 1;
2438 frame->did_double = 1;
2439 } else
2440 frame->did_double = 0;
2441
2442 frame->last_time = time;
2443 } else if (frame->did_double == 1) {
2444 frame->double_click = 1;
2445 frame->did_double = 0;
2446 }
2447
2448 if (frame->double_click)
2449 location = frame_double_click(frame->frame, input,
2450 button, state);
2451 else
2452 location = frame_pointer_button(frame->frame, input,
2453 button, state);
2454
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002455 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002456}
2457
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002458static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002459frame_touch_down_handler(struct widget *widget, struct input *input,
2460 uint32_t serial, uint32_t time, int32_t id,
2461 float x, float y, void *data)
2462{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002463 struct window_frame *frame = data;
2464
Xiong Zhang382de462014-06-12 11:06:26 +08002465 frame->double_click = 0;
2466 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2467 frame->last_id == id) {
2468 frame->double_click = 1;
2469 frame->did_double = 1;
2470 frame->double_id = id;
2471 } else
2472 frame->did_double = 0;
2473
2474 frame->last_time = time;
2475 frame->last_id = id;
2476
2477 if (frame->double_click)
2478 frame_double_touch_down(frame->frame, input, id, x, y);
2479 else
2480 frame_touch_down(frame->frame, input, id, x, y);
2481
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002482 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2483}
2484
2485static void
2486frame_touch_up_handler(struct widget *widget,
2487 struct input *input, uint32_t serial, uint32_t time,
2488 int32_t id, void *data)
2489{
2490 struct window_frame *frame = data;
2491
Xiong Zhang382de462014-06-12 11:06:26 +08002492 if (frame->double_id == id && frame->did_double) {
2493 frame->did_double = 0;
2494 frame->double_id = 0;
2495 frame_double_touch_up(frame->frame, input, id);
2496 } else
2497 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002498 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002499}
2500
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002501struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002502window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002503{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002504 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002505 uint32_t buttons;
2506
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002507 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002508 buttons = FRAME_BUTTON_NONE;
2509 } else {
2510 buttons = FRAME_BUTTON_ALL;
2511 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002512
Peter Huttererf3d62272013-08-08 11:57:05 +10002513 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002514 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002515 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002516
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002517 frame->widget = window_add_widget(window, frame);
2518 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002519
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002520 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2521 widget_set_resize_handler(frame->widget, frame_resize_handler);
2522 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002523 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002524 widget_set_motion_handler(frame->widget, frame_motion_handler);
2525 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002526 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002527 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002528
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002529 window->frame = frame;
2530
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002531 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002532}
2533
Kristian Høgsberga1627922012-06-20 17:30:03 -04002534void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002535window_frame_set_child_size(struct widget *widget, int child_width,
2536 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002537{
2538 struct display *display = widget->window->display;
2539 struct theme *t = display->theme;
2540 int decoration_width, decoration_height;
2541 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002542 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002543
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002544 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002545 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002546 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002547 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002548
2549 width = child_width + decoration_width;
2550 height = child_height + decoration_height;
2551 } else {
2552 width = child_width;
2553 height = child_height;
2554 }
2555
2556 window_schedule_resize(widget->window, width, height);
2557}
2558
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002559static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002560window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002561{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002562 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002563
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002564 /* frame->child must be destroyed by the application */
2565 widget_destroy(frame->widget);
2566 free(frame);
2567}
2568
2569static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002570input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002571 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002572{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002573 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002574 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002575
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002576 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002577 return;
2578
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002579 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002580 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002581 widget = old;
2582 if (input->grab)
2583 widget = input->grab;
2584 if (widget->leave_handler)
2585 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002586 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002587 }
2588
2589 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002590 widget = focus;
2591 if (input->grab)
2592 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002593 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002594 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002595 cursor = widget->enter_handler(focus, input, x, y,
2596 widget->user_data);
2597 else
2598 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002599
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002600 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002601 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002602}
2603
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002604void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002605touch_grab(struct input *input, int32_t touch_id)
2606{
2607 input->touch_grab = 1;
2608 input->touch_grab_id = touch_id;
2609}
2610
2611void
2612touch_ungrab(struct input *input)
2613{
2614 struct touch_point *tp, *tmp;
2615
2616 input->touch_grab = 0;
2617
2618 wl_list_for_each_safe(tp, tmp,
2619 &input->touch_point_list, link) {
2620 if (tp->id != input->touch_grab_id)
2621 continue;
2622 wl_list_remove(&tp->link);
2623 free(tp);
2624
2625 return;
2626 }
2627}
2628
2629void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002630input_grab(struct input *input, struct widget *widget, uint32_t button)
2631{
2632 input->grab = widget;
2633 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002634
2635 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002636}
2637
2638void
2639input_ungrab(struct input *input)
2640{
2641 struct widget *widget;
2642
2643 input->grab = NULL;
2644 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002645 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002646 input->sx, input->sy);
2647 input_set_focus_widget(input, widget, input->sx, input->sy);
2648 }
2649}
2650
2651static void
Derek Foreman118a4292015-04-22 17:23:35 -05002652cursor_delay_timer_reset(struct input *input, uint32_t duration)
2653{
2654 struct itimerspec its;
2655
2656 if (!duration)
2657 input->cursor_timer_running = false;
2658 else
2659 input->cursor_timer_running = true;
2660
2661 its.it_interval.tv_sec = 0;
2662 its.it_interval.tv_nsec = 0;
2663 its.it_value.tv_sec = duration / 1000;
2664 its.it_value.tv_nsec = (duration % 1000) * 1000 * 1000;
2665 if (timerfd_settime(input->cursor_delay_fd, 0, &its, NULL) < 0)
2666 fprintf(stderr, "could not set cursor timerfd\n: %m");
2667}
2668
2669static void cancel_pointer_image_update(struct input *input)
2670{
2671 if (input->cursor_timer_running)
2672 cursor_delay_timer_reset(input, 0);
2673}
2674
2675static void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002676input_remove_pointer_focus(struct input *input)
2677{
2678 struct window *window = input->pointer_focus;
2679
2680 if (!window)
2681 return;
2682
2683 input_set_focus_widget(input, NULL, 0, 0);
2684
2685 input->pointer_focus = NULL;
2686 input->current_cursor = CURSOR_UNSET;
Derek Foreman118a4292015-04-22 17:23:35 -05002687 cancel_pointer_image_update(input);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002688}
2689
2690static void
2691pointer_handle_enter(void *data, struct wl_pointer *pointer,
2692 uint32_t serial, struct wl_surface *surface,
2693 wl_fixed_t sx_w, wl_fixed_t sy_w)
2694{
2695 struct input *input = data;
2696 struct window *window;
2697 struct widget *widget;
2698 float sx = wl_fixed_to_double(sx_w);
2699 float sy = wl_fixed_to_double(sy_w);
2700
2701 if (!surface) {
2702 /* enter event for a window we've just destroyed */
2703 return;
2704 }
2705
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002706 window = wl_surface_get_user_data(surface);
2707 if (surface != window->main_surface->surface) {
2708 DBG("Ignoring input event from subsurface %p\n", surface);
2709 return;
2710 }
2711
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002712 input->display->serial = serial;
2713 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002714 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002715
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002716 input->sx = sx;
2717 input->sy = sy;
2718
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002719 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002720 input_set_focus_widget(input, widget, sx, sy);
2721}
2722
2723static void
2724pointer_handle_leave(void *data, struct wl_pointer *pointer,
2725 uint32_t serial, struct wl_surface *surface)
2726{
2727 struct input *input = data;
2728
2729 input->display->serial = serial;
2730 input_remove_pointer_focus(input);
2731}
2732
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002733static void
Daniel Stone37816df2012-05-16 18:45:18 +01002734pointer_handle_motion(void *data, struct wl_pointer *pointer,
2735 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002736{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002737 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002738 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002739 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002740 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002741 float sx = wl_fixed_to_double(sx_w);
2742 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002743
Paul Winwoodb22bf572013-08-29 10:52:54 +01002744 if (!window)
2745 return;
2746
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002747 input->sx = sx;
2748 input->sy = sy;
2749
Rob Bradford5f087742013-07-11 19:41:27 +01002750 /* when making the window smaller - e.g. after a unmaximise we might
2751 * still have a pending motion event that the compositor has picked
Derek Foreman46812b62015-08-26 17:13:27 -05002752 * based on the old surface dimensions. However, if we have an active
2753 * grab, we expect to see input from outside the window anyway.
Rob Bradford5f087742013-07-11 19:41:27 +01002754 */
Derek Foreman46812b62015-08-26 17:13:27 -05002755 if (!input->grab && (sx < window->main_surface->allocation.x ||
Derek Foreman5d135482015-08-26 17:13:26 -05002756 sy < window->main_surface->allocation.y ||
2757 sx > window->main_surface->allocation.width ||
Derek Foreman46812b62015-08-26 17:13:27 -05002758 sy > window->main_surface->allocation.height))
Rob Bradford5f087742013-07-11 19:41:27 +01002759 return;
2760
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002761 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002762 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002763 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002764 }
2765
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002766 if (input->grab)
2767 widget = input->grab;
2768 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002769 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002770 if (widget) {
2771 if (widget->motion_handler)
2772 cursor = widget->motion_handler(input->focus_widget,
2773 input, time, sx, sy,
2774 widget->user_data);
2775 else
2776 cursor = widget->default_cursor;
2777 } else
2778 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002779
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002780 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002781}
2782
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002783static void
Daniel Stone37816df2012-05-16 18:45:18 +01002784pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002785 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002786{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002787 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002788 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002789 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002790
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002791 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002792 if (input->focus_widget && input->grab == NULL &&
2793 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002794 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002795
Neil Roberts6b28aad2012-01-23 19:11:18 +00002796 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002797 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002798 (*widget->button_handler)(widget,
2799 input, time,
2800 button, state,
2801 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002802
Daniel Stone4dbadb12012-05-30 16:31:51 +01002803 if (input->grab && input->grab_button == button &&
2804 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002805 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002806}
2807
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002808static void
Daniel Stone37816df2012-05-16 18:45:18 +01002809pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002810 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002811{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002812 struct input *input = data;
2813 struct widget *widget;
2814
2815 widget = input->focus_widget;
2816 if (input->grab)
2817 widget = input->grab;
2818 if (widget && widget->axis_handler)
2819 (*widget->axis_handler)(widget,
2820 input, time,
2821 axis, value,
2822 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002823}
2824
Peter Hutterer87743e92016-01-18 16:38:22 +10002825static void
2826pointer_handle_frame(void *data, struct wl_pointer *pointer)
2827{
2828 struct input *input = data;
2829 struct widget *widget;
2830
2831 widget = input->focus_widget;
2832 if (input->grab)
2833 widget = input->grab;
2834 if (widget && widget->pointer_frame_handler)
2835 (*widget->pointer_frame_handler)(widget,
2836 input,
2837 widget->user_data);
2838}
2839
2840static void
2841pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
2842 uint32_t source)
2843{
2844 struct input *input = data;
2845 struct widget *widget;
2846
2847 widget = input->focus_widget;
2848 if (input->grab)
2849 widget = input->grab;
2850 if (widget && widget->axis_source_handler)
2851 (*widget->axis_source_handler)(widget,
2852 input,
2853 source,
2854 widget->user_data);
2855}
2856
2857static void
2858pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
2859 uint32_t time, uint32_t axis)
2860{
2861 struct input *input = data;
2862 struct widget *widget;
2863
2864 widget = input->focus_widget;
2865 if (input->grab)
2866 widget = input->grab;
2867 if (widget && widget->axis_stop_handler)
2868 (*widget->axis_stop_handler)(widget,
2869 input, time,
2870 axis,
2871 widget->user_data);
2872}
2873
2874static void
2875pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
2876 uint32_t axis, int32_t discrete)
2877{
2878 struct input *input = data;
2879 struct widget *widget;
2880
2881 widget = input->focus_widget;
2882 if (input->grab)
2883 widget = input->grab;
2884 if (widget && widget->axis_discrete_handler)
2885 (*widget->axis_discrete_handler)(widget,
2886 input,
2887 axis,
2888 discrete,
2889 widget->user_data);
2890}
2891
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002892static const struct wl_pointer_listener pointer_listener = {
2893 pointer_handle_enter,
2894 pointer_handle_leave,
2895 pointer_handle_motion,
2896 pointer_handle_button,
2897 pointer_handle_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10002898 pointer_handle_frame,
2899 pointer_handle_axis_source,
2900 pointer_handle_axis_stop,
2901 pointer_handle_axis_discrete,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002902};
2903
2904static void
2905input_remove_keyboard_focus(struct input *input)
2906{
2907 struct window *window = input->keyboard_focus;
2908 struct itimerspec its;
2909
2910 its.it_interval.tv_sec = 0;
2911 its.it_interval.tv_nsec = 0;
2912 its.it_value.tv_sec = 0;
2913 its.it_value.tv_nsec = 0;
2914 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2915
2916 if (!window)
2917 return;
2918
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002919 if (window->keyboard_focus_handler)
2920 (*window->keyboard_focus_handler)(window, NULL,
2921 window->user_data);
2922
2923 input->keyboard_focus = NULL;
2924}
2925
2926static void
2927keyboard_repeat_func(struct task *task, uint32_t events)
2928{
2929 struct input *input =
2930 container_of(task, struct input, repeat_task);
2931 struct window *window = input->keyboard_focus;
2932 uint64_t exp;
2933
2934 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2935 /* If we change the timer between the fd becoming
2936 * readable and getting here, there'll be nothing to
2937 * read and we get EAGAIN. */
2938 return;
2939
2940 if (window && window->key_handler) {
2941 (*window->key_handler)(window, input, input->repeat_time,
2942 input->repeat_key, input->repeat_sym,
2943 WL_KEYBOARD_KEY_STATE_PRESSED,
2944 window->user_data);
2945 }
2946}
2947
2948static void
2949keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2950 uint32_t format, int fd, uint32_t size)
2951{
2952 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002953 struct xkb_keymap *keymap;
2954 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002955 char *map_str;
2956
2957 if (!data) {
2958 close(fd);
2959 return;
2960 }
2961
2962 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2963 close(fd);
2964 return;
2965 }
2966
2967 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2968 if (map_str == MAP_FAILED) {
2969 close(fd);
2970 return;
2971 }
2972
Ran Benita2e1968f2014-08-19 23:59:51 +03002973 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2974 map_str,
2975 XKB_KEYMAP_FORMAT_TEXT_V1,
2976 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002977 munmap(map_str, size);
2978 close(fd);
2979
Rui Matos3eccb862013-10-10 19:44:22 +02002980 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002981 fprintf(stderr, "failed to compile keymap\n");
2982 return;
2983 }
2984
Rui Matos3eccb862013-10-10 19:44:22 +02002985 state = xkb_state_new(keymap);
2986 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002987 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002988 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002989 return;
2990 }
2991
Rui Matos3eccb862013-10-10 19:44:22 +02002992 xkb_keymap_unref(input->xkb.keymap);
2993 xkb_state_unref(input->xkb.state);
2994 input->xkb.keymap = keymap;
2995 input->xkb.state = state;
2996
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002997 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002998 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002999 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003000 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003001 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03003002 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003003}
3004
3005static void
3006keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3007 uint32_t serial, struct wl_surface *surface,
3008 struct wl_array *keys)
3009{
3010 struct input *input = data;
3011 struct window *window;
3012
3013 input->display->serial = serial;
3014 input->keyboard_focus = wl_surface_get_user_data(surface);
3015
3016 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003017 if (window->keyboard_focus_handler)
3018 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003019 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04003020}
3021
3022static void
3023keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3024 uint32_t serial, struct wl_surface *surface)
3025{
3026 struct input *input = data;
3027
3028 input->display->serial = serial;
3029 input_remove_keyboard_focus(input);
3030}
3031
Scott Moreau210d0792012-03-22 10:47:01 -06003032static void
Daniel Stone37816df2012-05-16 18:45:18 +01003033keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003034 uint32_t serial, uint32_t time, uint32_t key,
3035 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003036{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003037 struct input *input = data;
3038 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003039 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003040 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003041 const xkb_keysym_t *syms;
3042 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003043 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003044
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003045 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003046 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003047 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003048 return;
3049
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07003050 /* We only use input grabs for pointer events for now, so just
3051 * ignore key presses if a grab is active. We expand the key
3052 * event delivery mechanism to route events to widgets to
3053 * properly handle key grabs. In the meantime, this prevents
3054 * key event devlivery while a grab is active. */
3055 if (input->grab && input->grab_button == 0)
3056 return;
3057
Ran Benita2e1968f2014-08-19 23:59:51 +03003058 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003059
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003060 sym = XKB_KEY_NoSymbol;
3061 if (num_syms == 1)
3062 sym = syms[0];
3063
Kristian Høgsberg211b5172014-01-11 13:10:21 -08003064
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003065 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003066 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05003067 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003068 } else if (sym == XKB_KEY_F11 &&
3069 window->fullscreen_handler &&
3070 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3071 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003072 } else if (sym == XKB_KEY_F4 &&
3073 input->modifiers == MOD_ALT_MASK &&
3074 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003075 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003076 } else if (window->key_handler) {
3077 (*window->key_handler)(window, input, time, key,
3078 sym, state, window->user_data);
3079 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003080
3081 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3082 key == input->repeat_key) {
3083 its.it_interval.tv_sec = 0;
3084 its.it_interval.tv_nsec = 0;
3085 its.it_value.tv_sec = 0;
3086 its.it_value.tv_nsec = 0;
3087 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08003088 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3089 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003090 input->repeat_sym = sym;
3091 input->repeat_key = key;
3092 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02003093 its.it_interval.tv_sec = input->repeat_rate_sec;
3094 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3095 its.it_value.tv_sec = input->repeat_delay_sec;
3096 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003097 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3098 }
3099}
3100
3101static void
Daniel Stone351eb612012-05-31 15:27:47 -04003102keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3103 uint32_t serial, uint32_t mods_depressed,
3104 uint32_t mods_latched, uint32_t mods_locked,
3105 uint32_t group)
3106{
3107 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003108 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003109
Matt Ropere61561f2013-06-24 16:52:43 +01003110 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3111 if (!input->xkb.keymap)
3112 return;
3113
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003114 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3115 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003116 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03003117 XKB_STATE_MODS_DEPRESSED |
3118 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003119 input->modifiers = 0;
3120 if (mask & input->xkb.control_mask)
3121 input->modifiers |= MOD_CONTROL_MASK;
3122 if (mask & input->xkb.alt_mask)
3123 input->modifiers |= MOD_ALT_MASK;
3124 if (mask & input->xkb.shift_mask)
3125 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003126}
3127
Jonny Lamb06959082014-08-12 14:58:27 +02003128static void
3129set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3130{
3131 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3132 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3133
3134 /* a rate of zero disables any repeating, regardless of the delay's
3135 * value */
3136 if (rate == 0)
3137 return;
3138
3139 if (rate == 1)
3140 input->repeat_rate_sec = 1;
3141 else
3142 input->repeat_rate_nsec = 1000000000 / rate;
3143
3144 input->repeat_delay_sec = delay / 1000;
3145 delay -= (input->repeat_delay_sec * 1000);
3146 input->repeat_delay_nsec = delay * 1000 * 1000;
3147}
3148
3149static void
3150keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3151 int32_t rate, int32_t delay)
3152{
3153 struct input *input = data;
3154
3155 set_repeat_info(input, rate, delay);
3156}
3157
Daniel Stone37816df2012-05-16 18:45:18 +01003158static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003159 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003160 keyboard_handle_enter,
3161 keyboard_handle_leave,
3162 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003163 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003164 keyboard_handle_repeat_info
3165
Daniel Stone37816df2012-05-16 18:45:18 +01003166};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003167
3168static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003169touch_handle_down(void *data, struct wl_touch *wl_touch,
3170 uint32_t serial, uint32_t time, struct wl_surface *surface,
3171 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3172{
3173 struct input *input = data;
3174 struct widget *widget;
3175 float sx = wl_fixed_to_double(x_w);
3176 float sy = wl_fixed_to_double(y_w);
3177
Rusty Lynch1084da52013-08-15 09:10:08 -07003178 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003179 input->touch_focus = wl_surface_get_user_data(surface);
3180 if (!input->touch_focus) {
3181 DBG("Failed to find to touch focus for surface %p\n", surface);
3182 return;
3183 }
3184
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003185 if (surface != input->touch_focus->main_surface->surface) {
3186 DBG("Ignoring input event from subsurface %p\n", surface);
3187 input->touch_focus = NULL;
3188 return;
3189 }
3190
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003191 if (input->grab)
3192 widget = input->grab;
3193 else
3194 widget = window_find_widget(input->touch_focus,
3195 wl_fixed_to_double(x_w),
3196 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003197 if (widget) {
3198 struct touch_point *tp = xmalloc(sizeof *tp);
3199 if (tp) {
3200 tp->id = id;
3201 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003202 tp->x = sx;
3203 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003204 wl_list_insert(&input->touch_point_list, &tp->link);
3205
3206 if (widget->touch_down_handler)
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09003207 (*widget->touch_down_handler)(widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003208 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003209 sx, sy,
3210 widget->user_data);
3211 }
3212 }
3213}
3214
3215static void
3216touch_handle_up(void *data, struct wl_touch *wl_touch,
3217 uint32_t serial, uint32_t time, int32_t id)
3218{
3219 struct input *input = data;
3220 struct touch_point *tp, *tmp;
3221
Rusty Lynch041815a2013-08-08 21:20:38 -07003222 if (!input->touch_focus) {
3223 DBG("No touch focus found for touch up event!\n");
3224 return;
3225 }
3226
3227 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3228 if (tp->id != id)
3229 continue;
3230
3231 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003232 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003233 time, id,
3234 tp->widget->user_data);
3235
3236 wl_list_remove(&tp->link);
3237 free(tp);
3238
3239 return;
3240 }
3241}
3242
3243static void
3244touch_handle_motion(void *data, struct wl_touch *wl_touch,
3245 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3246{
3247 struct input *input = data;
3248 struct touch_point *tp;
3249 float sx = wl_fixed_to_double(x_w);
3250 float sy = wl_fixed_to_double(y_w);
3251
3252 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3253
3254 if (!input->touch_focus) {
3255 DBG("No touch focus found for touch motion event!\n");
3256 return;
3257 }
3258
3259 wl_list_for_each(tp, &input->touch_point_list, link) {
3260 if (tp->id != id)
3261 continue;
3262
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003263 tp->x = sx;
3264 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003265 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003266 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003267 id, sx, sy,
3268 tp->widget->user_data);
3269 return;
3270 }
3271}
3272
3273static void
3274touch_handle_frame(void *data, struct wl_touch *wl_touch)
3275{
3276 struct input *input = data;
3277 struct touch_point *tp, *tmp;
3278
3279 DBG("touch_handle_frame\n");
3280
3281 if (!input->touch_focus) {
3282 DBG("No touch focus found for touch frame event!\n");
3283 return;
3284 }
3285
3286 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3287 if (tp->widget->touch_frame_handler)
Michael Vetter2a18a522015-05-15 17:17:47 +02003288 (*tp->widget->touch_frame_handler)(tp->widget, input,
Rusty Lynch1084da52013-08-15 09:10:08 -07003289 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003290 }
3291}
3292
3293static void
3294touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3295{
3296 struct input *input = data;
3297 struct touch_point *tp, *tmp;
3298
3299 DBG("touch_handle_cancel\n");
3300
3301 if (!input->touch_focus) {
3302 DBG("No touch focus found for touch cancel event!\n");
3303 return;
3304 }
3305
3306 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3307 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003308 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3309 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003310
3311 wl_list_remove(&tp->link);
3312 free(tp);
3313 }
3314}
3315
3316static const struct wl_touch_listener touch_listener = {
3317 touch_handle_down,
3318 touch_handle_up,
3319 touch_handle_motion,
3320 touch_handle_frame,
3321 touch_handle_cancel,
3322};
3323
3324static void
Daniel Stone37816df2012-05-16 18:45:18 +01003325seat_handle_capabilities(void *data, struct wl_seat *seat,
3326 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003327{
Daniel Stone37816df2012-05-16 18:45:18 +01003328 struct input *input = data;
3329
3330 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3331 input->pointer = wl_seat_get_pointer(seat);
3332 wl_pointer_set_user_data(input->pointer, input);
3333 wl_pointer_add_listener(input->pointer, &pointer_listener,
3334 input);
3335 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003336 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003337 wl_pointer_release(input->pointer);
3338 else
3339 wl_pointer_destroy(input->pointer);
Daniel Stone37816df2012-05-16 18:45:18 +01003340 input->pointer = NULL;
3341 }
3342
3343 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3344 input->keyboard = wl_seat_get_keyboard(seat);
3345 wl_keyboard_set_user_data(input->keyboard, input);
3346 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3347 input);
3348 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003349 if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003350 wl_keyboard_release(input->keyboard);
3351 else
3352 wl_keyboard_destroy(input->keyboard);
Daniel Stone37816df2012-05-16 18:45:18 +01003353 input->keyboard = NULL;
3354 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003355
3356 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3357 input->touch = wl_seat_get_touch(seat);
3358 wl_touch_set_user_data(input->touch, input);
3359 wl_touch_add_listener(input->touch, &touch_listener, input);
3360 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05003361 if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
FORT Davidf7bb9352015-10-09 18:17:43 +02003362 wl_touch_release(input->touch);
3363 else
3364 wl_touch_destroy(input->touch);
Rusty Lynch041815a2013-08-08 21:20:38 -07003365 input->touch = NULL;
3366 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003367}
3368
Rob Bradford08031182013-08-13 20:11:03 +01003369static void
3370seat_handle_name(void *data, struct wl_seat *seat,
3371 const char *name)
3372{
3373
3374}
3375
Daniel Stone37816df2012-05-16 18:45:18 +01003376static const struct wl_seat_listener seat_listener = {
3377 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003378 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003379};
3380
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003381void
3382input_get_position(struct input *input, int32_t *x, int32_t *y)
3383{
3384 *x = input->sx;
3385 *y = input->sy;
3386}
3387
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003388int
3389input_get_touch(struct input *input, int32_t id, float *x, float *y)
3390{
3391 struct touch_point *tp;
3392
3393 wl_list_for_each(tp, &input->touch_point_list, link) {
3394 if (tp->id != id)
3395 continue;
3396
3397 *x = tp->x;
3398 *y = tp->y;
3399 return 0;
3400 }
3401
3402 return -1;
3403}
3404
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003405struct display *
3406input_get_display(struct input *input)
3407{
3408 return input->display;
3409}
3410
Daniel Stone37816df2012-05-16 18:45:18 +01003411struct wl_seat *
3412input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003413{
Daniel Stone37816df2012-05-16 18:45:18 +01003414 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003415}
3416
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003417uint32_t
3418input_get_modifiers(struct input *input)
3419{
3420 return input->modifiers;
3421}
3422
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003423struct widget *
3424input_get_focus_widget(struct input *input)
3425{
3426 return input->focus_widget;
3427}
3428
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003429struct data_offer {
3430 struct wl_data_offer *offer;
3431 struct input *input;
3432 struct wl_array types;
3433 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003434
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003435 struct task io_task;
3436 int fd;
3437 data_func_t func;
3438 int32_t x, y;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003439 uint32_t dnd_action;
3440 uint32_t source_actions;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003441 void *user_data;
3442};
3443
3444static void
3445data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3446{
3447 struct data_offer *offer = data;
3448 char **p;
3449
3450 p = wl_array_add(&offer->types, sizeof *p);
3451 *p = strdup(type);
3452}
3453
Carlos Garnacho9c931792016-01-18 23:52:12 +01003454static void
3455data_offer_source_actions(void *data, struct wl_data_offer *wl_data_offer, uint32_t source_actions)
3456{
3457 struct data_offer *offer = data;
3458
3459 offer->source_actions = source_actions;
3460}
3461
3462static void
3463data_offer_action(void *data, struct wl_data_offer *wl_data_offer, uint32_t dnd_action)
3464{
3465 struct data_offer *offer = data;
3466
3467 offer->dnd_action = dnd_action;
3468}
3469
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003470static const struct wl_data_offer_listener data_offer_listener = {
3471 data_offer_offer,
Carlos Garnacho9c931792016-01-18 23:52:12 +01003472 data_offer_source_actions,
3473 data_offer_action
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003474};
3475
3476static void
3477data_offer_destroy(struct data_offer *offer)
3478{
3479 char **p;
3480
3481 offer->refcount--;
3482 if (offer->refcount == 0) {
3483 wl_data_offer_destroy(offer->offer);
3484 for (p = offer->types.data; *p; p++)
3485 free(*p);
3486 wl_array_release(&offer->types);
3487 free(offer);
3488 }
3489}
3490
3491static void
3492data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003493 struct wl_data_device *data_device,
3494 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003495{
3496 struct data_offer *offer;
3497
Brian Lovinbc919262013-08-07 15:34:59 -07003498 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003499
3500 wl_array_init(&offer->types);
3501 offer->refcount = 1;
3502 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003503 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003504 wl_data_offer_add_listener(offer->offer,
3505 &data_offer_listener, offer);
3506}
3507
3508static void
3509data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003510 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003511 wl_fixed_t x_w, wl_fixed_t y_w,
3512 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003513{
3514 struct input *input = data;
3515 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003516 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003517 float x = wl_fixed_to_double(x_w);
3518 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003519 char **p;
3520
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003521 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003522 input->drag_enter_serial = serial;
3523 input->drag_focus = window,
3524 input->drag_x = x;
3525 input->drag_y = y;
3526
3527 if (!input->touch_grab)
3528 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003529
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003530 if (offer) {
3531 input->drag_offer = wl_data_offer_get_user_data(offer);
3532
3533 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3534 *p = NULL;
3535
3536 types_data = input->drag_offer->types.data;
Carlos Garnacho9c931792016-01-18 23:52:12 +01003537
3538 if (input->display->data_device_manager_version >=
3539 WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
3540 wl_data_offer_set_actions(offer,
3541 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
3542 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
3543 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
3544 }
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003545 } else {
3546 input->drag_offer = NULL;
3547 types_data = NULL;
3548 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003549
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003550 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003551 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003552 window->user_data);
3553}
3554
3555static void
3556data_device_leave(void *data, struct wl_data_device *data_device)
3557{
3558 struct input *input = data;
3559
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003560 if (input->drag_offer) {
3561 data_offer_destroy(input->drag_offer);
3562 input->drag_offer = NULL;
3563 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003564}
3565
3566static void
3567data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003568 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003569{
3570 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003571 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003572 float x = wl_fixed_to_double(x_w);
3573 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003574 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003575
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003576 input->drag_x = x;
3577 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003578
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003579 if (input->drag_offer)
3580 types_data = input->drag_offer->types.data;
3581 else
3582 types_data = NULL;
3583
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003584 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003585 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003586 window->user_data);
3587}
3588
3589static void
3590data_device_drop(void *data, struct wl_data_device *data_device)
3591{
3592 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003593 struct window *window = input->drag_focus;
3594 float x, y;
3595
3596 x = input->drag_x;
3597 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003598
3599 if (window->drop_handler)
3600 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003601 x, y, window->user_data);
3602
3603 if (input->touch_grab)
3604 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003605}
3606
3607static void
3608data_device_selection(void *data,
3609 struct wl_data_device *wl_data_device,
3610 struct wl_data_offer *offer)
3611{
3612 struct input *input = data;
3613 char **p;
3614
3615 if (input->selection_offer)
3616 data_offer_destroy(input->selection_offer);
3617
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003618 if (offer) {
3619 input->selection_offer = wl_data_offer_get_user_data(offer);
3620 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3621 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003622 } else {
3623 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003624 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003625}
3626
3627static const struct wl_data_device_listener data_device_listener = {
3628 data_device_data_offer,
3629 data_device_enter,
3630 data_device_leave,
3631 data_device_motion,
3632 data_device_drop,
3633 data_device_selection
3634};
3635
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003636static void
3637input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003638{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003639 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003640 struct wl_cursor *cursor;
3641 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003642
Daniel Stone80972742012-11-07 17:51:39 +11003643 if (!input->pointer)
3644 return;
3645
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003646 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003647 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003648 return;
3649
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003650 if (index >= (int) cursor->image_count) {
3651 fprintf(stderr, "cursor index out of range\n");
3652 return;
3653 }
3654
3655 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003656 buffer = wl_cursor_image_get_buffer(image);
3657 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003658 return;
3659
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003660 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3661 wl_surface_damage(input->pointer_surface, 0, 0,
3662 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003663 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003664 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3665 input->pointer_surface,
3666 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003667}
3668
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003669static const struct wl_callback_listener pointer_surface_listener;
3670
Derek Foreman493d9792015-03-04 16:26:25 -06003671static bool
3672input_set_pointer_special(struct input *input)
3673{
3674 if (input->current_cursor == CURSOR_BLANK) {
3675 wl_pointer_set_cursor(input->pointer,
3676 input->pointer_enter_serial,
3677 NULL, 0, 0);
3678 return true;
3679 }
3680
3681 if (input->current_cursor == CURSOR_UNSET)
3682 return true;
3683
3684 return false;
3685}
3686
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003687static void
Derek Foreman118a4292015-04-22 17:23:35 -05003688schedule_pointer_image_update(struct input *input,
3689 struct wl_cursor *cursor,
3690 uint32_t duration,
3691 bool force_frame)
3692{
3693 /* Some silly cursor sets have enormous pauses in them. In these
3694 * cases it's better to use a timer even if it results in less
3695 * accurate presentation, since it will save us having to set the
3696 * same cursor image over and over again.
3697 *
3698 * This is really not the way we're supposed to time any kind of
3699 * animation, but we're pretending it's OK here because we don't
3700 * want animated cursors with long delays to needlessly hog CPU.
3701 *
3702 * We use force_frame to ensure we don't accumulate large timing
3703 * errors by running off the wrong clock.
3704 */
3705 if (!force_frame && duration > 100) {
3706 struct timespec tp;
3707
3708 clock_gettime(CLOCK_MONOTONIC, &tp);
3709 input->cursor_timer_start = tp.tv_sec * 1000
3710 + tp.tv_nsec / 1000000;
3711 cursor_delay_timer_reset(input, duration);
3712 return;
3713 }
3714
3715 /* for short durations we'll just spin on frame callbacks for
3716 * accurate timing - the way any kind of timing sensitive animation
3717 * should really be done. */
3718 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
3719 wl_callback_add_listener(input->cursor_frame_cb,
3720 &pointer_surface_listener, input);
3721
3722}
3723
3724static void
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003725pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3726 uint32_t time)
3727{
3728 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003729 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003730 int i;
Derek Foreman118a4292015-04-22 17:23:35 -05003731 uint32_t duration;
3732 bool force_frame = true;
3733
3734 cancel_pointer_image_update(input);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003735
3736 if (callback) {
3737 assert(callback == input->cursor_frame_cb);
3738 wl_callback_destroy(callback);
3739 input->cursor_frame_cb = NULL;
Derek Foreman118a4292015-04-22 17:23:35 -05003740 force_frame = false;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003741 }
3742
Daniel Stone80972742012-11-07 17:51:39 +11003743 if (!input->pointer)
3744 return;
3745
Derek Foreman493d9792015-03-04 16:26:25 -06003746 if (input_set_pointer_special(input))
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003747 return;
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003748
Daniel Stonea494f1d2012-06-18 19:31:12 +01003749 cursor = input->display->cursors[input->current_cursor];
3750 if (!cursor)
3751 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003752
3753 /* FIXME We don't have the current time on the first call so we set
3754 * the animation start to the time of the first frame callback. */
3755 if (time == 0)
3756 input->cursor_anim_start = 0;
3757 else if (input->cursor_anim_start == 0)
3758 input->cursor_anim_start = time;
3759
Derek Foreman118a4292015-04-22 17:23:35 -05003760 input->cursor_anim_current = time;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003761
Derek Foreman118a4292015-04-22 17:23:35 -05003762 if (time == 0 || input->cursor_anim_start == 0) {
3763 duration = 0;
3764 i = 0;
3765 } else
3766 i = wl_cursor_frame_and_duration(
3767 cursor,
3768 time - input->cursor_anim_start,
3769 &duration);
3770
3771 if (cursor->image_count > 1)
3772 schedule_pointer_image_update(input, cursor, duration,
3773 force_frame);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003774
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003775 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003776}
3777
Derek Foreman118a4292015-04-22 17:23:35 -05003778static void
3779cursor_timer_func(struct task *task, uint32_t events)
3780{
3781 struct input *input = container_of(task, struct input, cursor_task);
3782 struct timespec tp;
3783 struct wl_cursor *cursor;
3784 uint32_t time;
3785 uint64_t exp;
3786
3787 if (!input->cursor_timer_running)
3788 return;
3789
3790 if (read(input->cursor_delay_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
3791 return;
3792
3793 cursor = input->display->cursors[input->current_cursor];
3794 if (!cursor)
3795 return;
3796
3797 clock_gettime(CLOCK_MONOTONIC, &tp);
3798 time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000 - input->cursor_timer_start;
3799 pointer_surface_frame_callback(input, NULL, input->cursor_anim_current + time);
3800}
3801
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003802static const struct wl_callback_listener pointer_surface_listener = {
3803 pointer_surface_frame_callback
3804};
3805
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003806void
3807input_set_pointer_image(struct input *input, int pointer)
3808{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003809 int force = 0;
3810
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003811 if (!input->pointer)
3812 return;
3813
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003814 if (input->pointer_enter_serial > input->cursor_serial)
3815 force = 1;
3816
3817 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003818 return;
3819
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003820 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003821 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003822 if (!input->cursor_frame_cb)
3823 pointer_surface_frame_callback(input, NULL, 0);
Derek Foreman493d9792015-03-04 16:26:25 -06003824 else if (force && !input_set_pointer_special(input)) {
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003825 /* The current frame callback may be stuck if, for instance,
3826 * the set cursor request was processed by the server after
3827 * this client lost the focus. In this case the cursor surface
3828 * might not be mapped and the frame callback wouldn't ever
3829 * complete. Send a set_cursor and attach to try to map the
3830 * cursor surface again so that the callback will finish */
3831 input_set_pointer_image_index(input, 0);
3832 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003833}
3834
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003835struct wl_data_device *
3836input_get_data_device(struct input *input)
3837{
3838 return input->data_device;
3839}
3840
3841void
3842input_set_selection(struct input *input,
3843 struct wl_data_source *source, uint32_t time)
3844{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003845 if (input->data_device)
3846 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003847}
3848
3849void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003850input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003851{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003852 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003853 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003854}
3855
3856static void
3857offer_io_func(struct task *task, uint32_t events)
3858{
3859 struct data_offer *offer =
3860 container_of(task, struct data_offer, io_task);
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003861 struct display *display = offer->input->display;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003862 unsigned int len;
3863 char buffer[4096];
3864
3865 len = read(offer->fd, buffer, sizeof buffer);
3866 offer->func(buffer, len,
3867 offer->x, offer->y, offer->user_data);
3868
3869 if (len == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01003870 if (display->data_device_manager_version >=
3871 WL_DATA_OFFER_FINISH_SINCE_VERSION)
3872 wl_data_offer_finish(offer->offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003873 close(offer->fd);
3874 data_offer_destroy(offer);
3875 }
3876}
3877
3878static void
3879data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3880 data_func_t func, void *user_data)
3881{
3882 int p[2];
3883
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003884 if (pipe2(p, O_CLOEXEC) == -1)
3885 return;
3886
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003887 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3888 close(p[1]);
3889
3890 offer->io_task.run = offer_io_func;
3891 offer->fd = p[0];
3892 offer->func = func;
3893 offer->refcount++;
3894 offer->user_data = user_data;
3895
3896 display_watch_fd(offer->input->display,
3897 offer->fd, EPOLLIN, &offer->io_task);
3898}
3899
3900void
3901input_receive_drag_data(struct input *input, const char *mime_type,
3902 data_func_t func, void *data)
3903{
3904 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003905 input->drag_offer->x = input->drag_x;
3906 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003907}
3908
3909int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003910input_receive_drag_data_to_fd(struct input *input,
3911 const char *mime_type, int fd)
3912{
3913 if (input->drag_offer)
3914 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3915
3916 return 0;
3917}
3918
3919int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003920input_receive_selection_data(struct input *input, const char *mime_type,
3921 data_func_t func, void *data)
3922{
3923 char **p;
3924
3925 if (input->selection_offer == NULL)
3926 return -1;
3927
3928 for (p = input->selection_offer->types.data; *p; p++)
3929 if (strcmp(mime_type, *p) == 0)
3930 break;
3931
3932 if (*p == NULL)
3933 return -1;
3934
3935 data_offer_receive_data(input->selection_offer,
3936 mime_type, func, data);
3937 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003938}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003939
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003940int
3941input_receive_selection_data_to_fd(struct input *input,
3942 const char *mime_type, int fd)
3943{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003944 if (input->selection_offer)
3945 wl_data_offer_receive(input->selection_offer->offer,
3946 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003947
3948 return 0;
3949}
3950
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003951void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003952window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003953{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003954 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003955 return;
3956
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003957 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003958}
3959
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003960static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003961surface_set_synchronized(struct surface *surface)
3962{
3963 if (!surface->subsurface)
3964 return;
3965
3966 if (surface->synchronized)
3967 return;
3968
3969 wl_subsurface_set_sync(surface->subsurface);
3970 surface->synchronized = 1;
3971}
3972
3973static void
3974surface_set_synchronized_default(struct surface *surface)
3975{
3976 if (!surface->subsurface)
3977 return;
3978
3979 if (surface->synchronized == surface->synchronized_default)
3980 return;
3981
3982 if (surface->synchronized_default)
3983 wl_subsurface_set_sync(surface->subsurface);
3984 else
3985 wl_subsurface_set_desync(surface->subsurface);
3986
3987 surface->synchronized = surface->synchronized_default;
3988}
3989
3990static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003991surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003992{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003993 struct widget *widget = surface->widget;
3994 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003995
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003996 if (surface->input_region) {
3997 wl_region_destroy(surface->input_region);
3998 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003999 }
4000
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004001 if (surface->opaque_region)
4002 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004003
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004004 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05004005
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004006 if (widget->resize_handler)
4007 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004008 widget->allocation.width,
4009 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004010 widget->user_data);
4011
Pekka Paalanen35e82632013-04-25 13:57:48 +03004012 if (surface->subsurface &&
4013 (surface->allocation.x != widget->allocation.x ||
4014 surface->allocation.y != widget->allocation.y)) {
4015 wl_subsurface_set_position(surface->subsurface,
4016 widget->allocation.x,
4017 widget->allocation.y);
4018 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004019 if (surface->allocation.width != widget->allocation.width ||
4020 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004021 window_schedule_redraw(widget->window);
4022 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004023 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004024
4025 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004026 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02004027 widget->allocation.width,
4028 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004029}
4030
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004031static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004032window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004033{
Pekka Paalanen35e82632013-04-25 13:57:48 +03004034 struct surface *surface;
4035
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004036 widget_set_allocation(window->main_surface->widget,
4037 window->pending_allocation.x,
4038 window->pending_allocation.y,
4039 window->pending_allocation.width,
4040 window->pending_allocation.height);
4041
4042 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004043
4044 /* The main surface is in the list, too. Main surface's
4045 * resize_handler is responsible for calling widget_set_allocation()
4046 * on all sub-surface root widgets, so they will be resized
4047 * properly.
4048 */
4049 wl_list_for_each(surface, &window->subsurface_list, link) {
4050 if (surface == window->main_surface)
4051 continue;
4052
4053 surface_set_synchronized(surface);
4054 surface_resize(surface);
4055 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004056
4057 if (!window->fullscreen && !window->maximized)
4058 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02004059}
4060
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004061static void
4062idle_resize(struct window *window)
4063{
4064 window->resize_needed = 0;
4065 window->redraw_needed = 1;
4066
4067 DBG("from %dx%d to %dx%d\n",
4068 window->main_surface->server_allocation.width,
4069 window->main_surface->server_allocation.height,
4070 window->pending_allocation.width,
4071 window->pending_allocation.height);
4072
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004073 window_do_resize(window);
4074}
4075
4076static void
4077undo_resize(struct window *window)
4078{
4079 window->pending_allocation.width =
4080 window->main_surface->server_allocation.width;
4081 window->pending_allocation.height =
4082 window->main_surface->server_allocation.height;
4083
4084 DBG("back to %dx%d\n",
4085 window->main_surface->server_allocation.width,
4086 window->main_surface->server_allocation.height);
4087
4088 window_do_resize(window);
4089
4090 if (window->pending_allocation.width == 0 &&
4091 window->pending_allocation.height == 0) {
4092 fprintf(stderr, "Error: Could not draw a surface, "
4093 "most likely due to insufficient disk space in "
4094 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
4095 exit(EXIT_FAILURE);
4096 }
4097}
4098
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004099void
4100window_schedule_resize(struct window *window, int width, int height)
4101{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004102 /* We should probably get these numbers from the theme. */
4103 const int min_width = 200, min_height = 200;
4104
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05004105 window->pending_allocation.x = 0;
4106 window->pending_allocation.y = 0;
4107 window->pending_allocation.width = width;
4108 window->pending_allocation.height = height;
4109
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004110 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004111 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004112 window->min_allocation.width = min_width;
4113 else
4114 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004115 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004116 window->min_allocation.height = min_height;
4117 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07004118 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07004119 }
4120
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04004121 if (window->pending_allocation.width < window->min_allocation.width)
4122 window->pending_allocation.width = window->min_allocation.width;
4123 if (window->pending_allocation.height < window->min_allocation.height)
4124 window->pending_allocation.height = window->min_allocation.height;
4125
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004126 window->resize_needed = 1;
4127 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004128}
4129
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004130void
4131widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4132{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05004133 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05004134}
4135
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004136static int
4137window_get_shadow_margin(struct window *window)
4138{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004139 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004140 return frame_get_shadow_margin(window->frame->frame);
4141 else
4142 return 0;
4143}
4144
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004145static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004146handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004147 int32_t width, int32_t height,
4148 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004149{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004150 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004151 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04004152
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004153 window->maximized = 0;
4154 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004155 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004156 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004157
4158 wl_array_for_each(p, states) {
4159 uint32_t state = *p;
4160 switch (state) {
4161 case XDG_SURFACE_STATE_MAXIMIZED:
4162 window->maximized = 1;
4163 break;
4164 case XDG_SURFACE_STATE_FULLSCREEN:
4165 window->fullscreen = 1;
4166 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04004167 case XDG_SURFACE_STATE_RESIZING:
4168 window->resizing = 1;
4169 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04004170 case XDG_SURFACE_STATE_ACTIVATED:
4171 window->focused = 1;
4172 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004173 default:
4174 /* Unknown state */
4175 break;
4176 }
4177 }
4178
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04004179 if (window->frame) {
4180 if (window->maximized) {
4181 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4182 } else {
4183 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4184 }
4185
4186 if (window->focused) {
4187 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4188 } else {
4189 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4190 }
4191 }
4192
Jasper St. Pierref184c382014-05-06 08:33:27 -04004193 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04004194 /* The width / height params are for window geometry,
4195 * but window_schedule_resize takes allocation. Add
4196 * on the shadow margin to get the difference. */
4197 int margin = window_get_shadow_margin(window);
4198
4199 window_schedule_resize(window,
4200 width + margin * 2,
4201 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04004202 } else {
4203 window_schedule_resize(window,
4204 window->saved_allocation.width,
4205 window->saved_allocation.height);
4206 }
4207
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07004208 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07004209
4210 if (window->state_changed_handler)
4211 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004212}
4213
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004214static void
4215handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
4216{
4217 struct window *window = data;
4218 window_close(window);
4219}
4220
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004221static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004222 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004223 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004224};
4225
4226static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004227window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004228{
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004229 struct xdg_surface *parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004230
4231 if (!window->xdg_surface)
4232 return;
4233
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004234 if (window->parent == window->last_parent)
4235 return;
4236
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004237 if (window->parent)
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004238 parent_surface = window->parent->xdg_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004239 else
4240 parent_surface = NULL;
4241
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004242 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08004243 window->last_parent = window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004244}
4245
4246static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004247window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004248{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004249 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004250 frame_input_rect(window->frame->frame,
4251 &geometry->x,
4252 &geometry->y,
4253 &geometry->width,
4254 &geometry->height);
4255 else
4256 window_get_allocation(window, geometry);
4257}
4258
4259static void
4260window_sync_geometry(struct window *window)
4261{
4262 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004263
4264 if (!window->xdg_surface)
4265 return;
4266
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004267 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004268 if (geometry.x == window->last_geometry.x &&
4269 geometry.y == window->last_geometry.y &&
4270 geometry.width == window->last_geometry.width &&
4271 geometry.height == window->last_geometry.height)
4272 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004273
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004274 xdg_surface_set_window_geometry(window->xdg_surface,
4275 geometry.x,
4276 geometry.y,
4277 geometry.width,
4278 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004279 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004280}
4281
4282static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004283window_flush(struct window *window)
4284{
4285 struct surface *surface;
4286
4287 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004288 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004289 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004290 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004291 }
4292 }
4293
4294 wl_list_for_each(surface, &window->subsurface_list, link) {
4295 if (surface == window->main_surface)
4296 continue;
4297
4298 surface_flush(surface);
4299 }
4300
4301 surface_flush(window->main_surface);
4302}
4303
4304static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004305menu_destroy(struct menu *menu)
4306{
4307 widget_destroy(menu->widget);
4308 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004309 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004310 free(menu);
4311}
4312
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004313void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004314window_get_allocation(struct window *window,
4315 struct rectangle *allocation)
4316{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004317 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004318}
4319
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004320static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004321widget_redraw(struct widget *widget)
4322{
4323 struct widget *child;
4324
4325 if (widget->redraw_handler)
4326 widget->redraw_handler(widget, widget->user_data);
4327 wl_list_for_each(child, &widget->child_list, link)
4328 widget_redraw(child);
4329}
4330
4331static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004332frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4333{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004334 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004335
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004336 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004337 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004338 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004339 surface->frame_cb = NULL;
4340
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004341 surface->last_time = time;
4342
Pekka Paalanen71233882013-04-25 13:57:53 +03004343 if (surface->redraw_needed || surface->window->redraw_needed) {
4344 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004345 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004346 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004347}
4348
4349static const struct wl_callback_listener listener = {
4350 frame_callback
4351};
4352
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004353static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004354surface_redraw(struct surface *surface)
4355{
Pekka Paalanen71233882013-04-25 13:57:53 +03004356 DBG_OBJ(surface->surface, "begin\n");
4357
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004358 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004359 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004360
4361 /* Whole-window redraw forces a redraw even if the previous has
4362 * not yet hit the screen.
4363 */
4364 if (surface->frame_cb) {
4365 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004366 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004367
Pekka Paalanen71233882013-04-25 13:57:53 +03004368 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004369 wl_callback_destroy(surface->frame_cb);
4370 }
4371
Neil Roberts97b747c2013-12-19 16:17:12 +00004372 if (surface->widget->use_cairo &&
4373 !widget_get_cairo_surface(surface->widget)) {
Bryce Harringtonf69bd1a2015-11-20 11:57:43 -08004374 DBG_OBJ(surface->surface, "cancelled due to buffer failure\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004375 return -1;
4376 }
4377
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004378 surface->frame_cb = wl_surface_frame(surface->surface);
4379 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004380 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004381
4382 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004383 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004384 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004385 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004386 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004387}
4388
4389static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004390idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004391{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004392 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004393 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004394 int failed = 0;
4395 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004396
Pekka Paalanen71233882013-04-25 13:57:53 +03004397 DBG(" --------- \n");
4398
Pekka Paalaneneebff542013-04-25 13:57:52 +03004399 wl_list_init(&window->redraw_task.link);
4400 window->redraw_task_scheduled = 0;
4401
4402 if (window->resize_needed) {
4403 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004404 if (window->main_surface->frame_cb) {
4405 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004406 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004407 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004408
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004409 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004410 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004411 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004412
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004413 if (surface_redraw(window->main_surface) < 0) {
4414 /*
4415 * Only main_surface failure will cause us to undo the resize.
4416 * If sub-surfaces fail, they will just be broken with old
4417 * content.
4418 */
4419 failed = 1;
4420 } else {
4421 wl_list_for_each(surface, &window->subsurface_list, link) {
4422 if (surface == window->main_surface)
4423 continue;
4424
4425 surface_redraw(surface);
4426 }
4427 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004428
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004429 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004430 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004431
4432 wl_list_for_each(surface, &window->subsurface_list, link)
4433 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004434
4435 if (resized && failed) {
4436 /* Restore widget tree to correspond to what is on screen. */
4437 undo_resize(window);
4438 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004439}
4440
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004441static void
4442window_schedule_redraw_task(struct window *window)
4443{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004444 if (!window->redraw_task_scheduled) {
4445 window->redraw_task.run = idle_redraw;
4446 display_defer(window->display, &window->redraw_task);
4447 window->redraw_task_scheduled = 1;
4448 }
4449}
4450
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004451void
4452window_schedule_redraw(struct window *window)
4453{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004454 struct surface *surface;
4455
Pekka Paalanen71233882013-04-25 13:57:53 +03004456 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4457
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004458 wl_list_for_each(surface, &window->subsurface_list, link)
4459 surface->redraw_needed = 1;
4460
4461 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004462}
4463
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004464int
4465window_is_fullscreen(struct window *window)
4466{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004467 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004468}
4469
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004470void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004471window_set_fullscreen(struct window *window, int fullscreen)
4472{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004473 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004474 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004475
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004476 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004477 return;
4478
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004479 if (fullscreen)
4480 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4481 else
4482 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004483}
4484
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004485int
4486window_is_maximized(struct window *window)
4487{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004488 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004489}
4490
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004491void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004492window_set_maximized(struct window *window, int maximized)
4493{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004494 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004495 return;
4496
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004497 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004498 return;
4499
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004500 if (maximized)
4501 xdg_surface_set_maximized(window->xdg_surface);
4502 else
4503 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004504}
4505
Jasper St. Pierrede680992014-04-10 17:23:49 -07004506int
4507window_is_resizing(struct window *window)
4508{
4509 return window->resizing;
4510}
4511
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004512void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004513window_set_minimized(struct window *window)
4514{
4515 if (!window->xdg_surface)
4516 return;
4517
4518 xdg_surface_set_minimized(window->xdg_surface);
4519}
4520
4521void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004522window_set_user_data(struct window *window, void *data)
4523{
4524 window->user_data = data;
4525}
4526
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004527void *
4528window_get_user_data(struct window *window)
4529{
4530 return window->user_data;
4531}
4532
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004533void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004534window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004535 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004536{
4537 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004538}
4539
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004540void
4541window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004542 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004543{
4544 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004545}
4546
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004547void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004548window_set_data_handler(struct window *window, window_data_handler_t handler)
4549{
4550 window->data_handler = handler;
4551}
4552
4553void
4554window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4555{
4556 window->drop_handler = handler;
4557}
4558
4559void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004560window_set_close_handler(struct window *window,
4561 window_close_handler_t handler)
4562{
4563 window->close_handler = handler;
4564}
4565
4566void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004567window_set_fullscreen_handler(struct window *window,
4568 window_fullscreen_handler_t handler)
4569{
4570 window->fullscreen_handler = handler;
4571}
4572
4573void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004574window_set_output_handler(struct window *window,
4575 window_output_handler_t handler)
4576{
4577 window->output_handler = handler;
4578}
4579
4580void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004581window_set_state_changed_handler(struct window *window,
4582 window_state_changed_handler_t handler)
4583{
4584 window->state_changed_handler = handler;
4585}
4586
4587void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004588window_set_title(struct window *window, const char *title)
4589{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004590 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004591 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004592 if (window->frame) {
4593 frame_set_title(window->frame->frame, title);
4594 widget_schedule_redraw(window->frame->widget);
4595 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004596 if (window->xdg_surface)
4597 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004598}
4599
4600const char *
4601window_get_title(struct window *window)
4602{
4603 return window->title;
4604}
4605
4606void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004607window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4608{
4609 struct text_cursor_position *text_cursor_position =
4610 window->display->text_cursor_position;
4611
Scott Moreau9295ce02012-06-01 12:46:10 -06004612 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004613 return;
4614
4615 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004616 window->main_surface->surface,
4617 wl_fixed_from_int(x),
4618 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004619}
4620
Casey Dahlin9074db52012-04-19 22:50:09 -04004621static void
4622surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004623 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004624{
Rob Bradford7507b572012-05-15 17:55:34 +01004625 struct window *window = data;
4626 struct output *output;
4627 struct output *output_found = NULL;
4628 struct window_output *window_output;
4629
4630 wl_list_for_each(output, &window->display->output_list, link) {
4631 if (output->output == wl_output) {
4632 output_found = output;
4633 break;
4634 }
4635 }
4636
4637 if (!output_found)
4638 return;
4639
Brian Lovinbc919262013-08-07 15:34:59 -07004640 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004641 window_output->output = output_found;
4642
4643 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004644
4645 if (window->output_handler)
4646 window->output_handler(window, output_found, 1,
4647 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004648}
4649
4650static void
4651surface_leave(void *data,
4652 struct wl_surface *wl_surface, struct wl_output *output)
4653{
Rob Bradford7507b572012-05-15 17:55:34 +01004654 struct window *window = data;
4655 struct window_output *window_output;
4656 struct window_output *window_output_found = NULL;
4657
4658 wl_list_for_each(window_output, &window->window_output_list, link) {
4659 if (window_output->output->output == output) {
4660 window_output_found = window_output;
4661 break;
4662 }
4663 }
4664
4665 if (window_output_found) {
4666 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004667
4668 if (window->output_handler)
4669 window->output_handler(window, window_output->output,
4670 0, window->user_data);
4671
Rob Bradford7507b572012-05-15 17:55:34 +01004672 free(window_output_found);
4673 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004674}
4675
4676static const struct wl_surface_listener surface_listener = {
4677 surface_enter,
4678 surface_leave
4679};
4680
Pekka Paalanen4e373742013-02-13 16:17:13 +02004681static struct surface *
4682surface_create(struct window *window)
4683{
4684 struct display *display = window->display;
4685 struct surface *surface;
4686
Bryce Harringtonda9d8fa2015-06-19 16:12:22 -07004687 surface = xzalloc(sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004688 surface->window = window;
4689 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004690 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004691 wl_surface_add_listener(surface->surface, &surface_listener, window);
4692
Pekka Paalanen35e82632013-04-25 13:57:48 +03004693 wl_list_insert(&window->subsurface_list, &surface->link);
4694
Pekka Paalanen4e373742013-02-13 16:17:13 +02004695 return surface;
4696}
4697
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004698static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004699get_preferred_buffer_type(struct display *display)
4700{
4701#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004702 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004703 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4704#endif
4705
4706 return WINDOW_BUFFER_TYPE_SHM;
4707}
4708
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004709static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004710window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004711{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004712 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004713 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004714
Peter Huttererf3d62272013-08-08 11:57:05 +10004715 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004716 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004717 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004718
4719 surface = surface_create(window);
4720 window->main_surface = surface;
4721
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004722 assert(custom || display->xdg_shell || display->ivi_application);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004723
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004724 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004725 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004726
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004727 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004728
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004729 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004730 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004731 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004732
Rob Bradford7507b572012-05-15 17:55:34 +01004733 wl_list_init (&window->window_output_list);
4734
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004735 return window;
4736}
4737
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004738struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004739window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004740{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004741 struct window *window;
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004742 uint32_t id_ivisurf;
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004743
4744 window = window_create_internal(display, 0);
4745
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004746 if (window->display->xdg_shell) {
4747 window->xdg_surface =
4748 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4749 window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07004750 fail_on_null(window->xdg_surface, 0, __FILE__, __LINE__);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004751
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004752 xdg_surface_set_user_data(window->xdg_surface, window);
4753 xdg_surface_add_listener(window->xdg_surface,
4754 &xdg_surface_listener, window);
4755 } else if (display->ivi_application) {
4756 /* auto generation of ivi_id based on process id + basement of id */
4757 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4758 window->ivi_surface =
4759 ivi_application_surface_create(display->ivi_application,
4760 id_ivisurf, window->main_surface->surface);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07004761 fail_on_null(window->ivi_surface, 0, __FILE__, __LINE__);
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09004762
4763 ivi_surface_add_listener(window->ivi_surface,
4764 &ivi_surface_listener, window);
4765 }
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004766
4767 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004768}
4769
4770struct window *
4771window_create_custom(struct display *display)
4772{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004773 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004774}
4775
Jasper St. Pierre53686042013-12-09 15:26:25 -05004776void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004777window_set_parent(struct window *window,
4778 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004779{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004780 window->parent = parent_window;
4781 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004782}
4783
4784struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004785window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004786{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004787 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004788}
4789
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004790static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004791menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004792{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004793 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004794 int next;
4795
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004796 frame_interior(menu->frame, &x, &y, &width, &height);
4797 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004798 if (menu->current != next) {
4799 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004800 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004801 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004802}
4803
4804static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004805menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004806 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004807 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004808{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004809 struct menu *menu = data;
4810
4811 if (widget == menu->widget)
4812 menu_set_item(data, y);
4813
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004814 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004815}
4816
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004817static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004818menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004819 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004820{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004821 struct menu *menu = data;
4822
4823 if (widget == menu->widget)
4824 menu_set_item(data, y);
4825
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004826 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004827}
4828
4829static void
4830menu_leave_handler(struct widget *widget, struct input *input, void *data)
4831{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004832 struct menu *menu = data;
4833
4834 if (widget == menu->widget)
4835 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004836}
4837
4838static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004839menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004840 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004841 uint32_t button, enum wl_pointer_button_state state,
4842 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004843
4844{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004845 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004846
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004847 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4848 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004849 /* Either relase after press-drag-release or
4850 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004851 menu->func(menu->user_data, input, menu->current);
Derek Foreman673bbe22015-09-11 14:28:15 -05004852 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004853 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004854 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004855 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004856 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004857}
4858
4859static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004860menu_touch_up_handler(struct widget *widget,
4861 struct input *input,
4862 uint32_t serial,
4863 uint32_t time,
4864 int32_t id,
4865 void *data)
4866{
4867 struct menu *menu = data;
4868
4869 input_ungrab(input);
4870 menu_destroy(menu);
4871}
4872
4873static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004874menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004875{
4876 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004877 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004878 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004879
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004880 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004881
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004882 frame_repaint(menu->frame, cr);
4883 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004884
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004885 theme_set_background_source(menu->window->display->theme,
4886 cr, THEME_FRAME_ACTIVE);
4887 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004888 cairo_fill(cr);
4889
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004890 cairo_select_font_face(cr, "sans",
4891 CAIRO_FONT_SLANT_NORMAL,
4892 CAIRO_FONT_WEIGHT_NORMAL);
4893 cairo_set_font_size(cr, 12);
4894
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004895 for (i = 0; i < menu->count; i++) {
4896 if (i == menu->current) {
4897 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004898 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004899 cairo_fill(cr);
4900 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004901 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004902 cairo_show_text(cr, menu->entries[i]);
4903 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004904 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4905 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004906 cairo_show_text(cr, menu->entries[i]);
4907 }
4908 }
4909
4910 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004911}
4912
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004913static void
Jasper St. Pierreecf2a0f2015-02-13 14:01:56 +08004914handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004915{
4916 struct window *window = data;
4917 struct menu *menu = window->main_surface->widget->user_data;
4918
4919 input_ungrab(menu->input);
4920 menu_destroy(menu);
4921}
4922
4923static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004924 handle_popup_popup_done,
4925};
4926
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004927static struct menu *
4928create_menu(struct display *display,
4929 struct input *input, uint32_t time,
4930 menu_func_t func, const char **entries, int count,
4931 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004932{
4933 struct window *window;
4934 struct menu *menu;
4935
4936 menu = malloc(sizeof *menu);
4937 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004938 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004939
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004940 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004941 if (!window) {
4942 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004943 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004944 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004945
4946 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004947 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004948 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004949 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004950 FRAME_BUTTON_NONE, NULL);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07004951 fail_on_null(menu->frame, 0, __FILE__, __LINE__);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004952 menu->entries = entries;
4953 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004954 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004955 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004956 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004957 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004958 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004959
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004960 input_ungrab(input);
4961
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004962 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004963 widget_set_enter_handler(menu->widget, menu_enter_handler);
4964 widget_set_leave_handler(menu->widget, menu_leave_handler);
4965 widget_set_motion_handler(menu->widget, menu_motion_handler);
4966 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004967 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004968
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004969 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004970 frame_resize_inside(menu->frame, 200, count * 20);
4971 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4972 window_schedule_resize(window, frame_width(menu->frame),
4973 frame_height(menu->frame));
4974
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004975 return menu;
4976}
4977
4978struct window *
4979window_create_menu(struct display *display,
4980 struct input *input, uint32_t time,
4981 menu_func_t func, const char **entries, int count,
4982 void *user_data)
4983{
4984 struct menu *menu;
4985 menu = create_menu(display, input, time, func, entries, count, user_data);
4986
4987 if (menu == NULL)
4988 return NULL;
4989
4990 return menu->window;
4991}
4992
4993void
4994window_show_menu(struct display *display,
4995 struct input *input, uint32_t time, struct window *parent,
4996 int32_t x, int32_t y,
4997 menu_func_t func, const char **entries, int count)
4998{
4999 struct menu *menu;
5000 struct window *window;
5001 int32_t ix, iy;
5002
5003 menu = create_menu(display, input, time, func, entries, count, parent);
5004
5005 if (menu == NULL)
5006 return;
5007
5008 window = menu->window;
5009
5010 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
5011 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
5012
5013 window->x = x;
5014 window->y = y;
5015
Kristian Høgsbergc680e902013-10-23 21:49:30 -07005016 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005017
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005018 if (!display->xdg_shell)
5019 return;
5020
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005021 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
5022 window->main_surface->surface,
5023 parent->main_surface->surface,
5024 input->seat,
5025 display_get_serial(window->display),
5026 window->x - ix,
Jasper St. Pierre14f330c2015-02-13 14:01:57 +08005027 window->y - iy);
Bryce Harringtone99e4bf2016-03-16 14:15:18 -07005028 fail_on_null(window->xdg_popup, 0, __FILE__, __LINE__);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005029
5030 xdg_popup_set_user_data(window->xdg_popup, window);
5031 xdg_popup_add_listener(window->xdg_popup,
5032 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05005033}
5034
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05005035void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005036window_set_buffer_type(struct window *window, enum window_buffer_type type)
5037{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005038 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04005039}
5040
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005041enum window_buffer_type
5042window_get_buffer_type(struct window *window)
5043{
5044 return window->main_surface->buffer_type;
5045}
5046
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005047void
5048window_set_preferred_format(struct window *window,
5049 enum preferred_format format)
5050{
5051 window->preferred_format = format;
5052}
5053
Pekka Paalanen35e82632013-04-25 13:57:48 +03005054struct widget *
5055window_add_subsurface(struct window *window, void *data,
5056 enum subsurface_mode default_mode)
5057{
5058 struct widget *widget;
5059 struct surface *surface;
5060 struct wl_surface *parent;
5061 struct wl_subcompositor *subcompo = window->display->subcompositor;
5062
Pekka Paalanen35e82632013-04-25 13:57:48 +03005063 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02005064 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005065 widget = widget_create(window, surface, data);
5066 wl_list_init(&widget->link);
5067 surface->widget = widget;
5068
5069 parent = window->main_surface->surface;
5070 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
5071 surface->surface,
5072 parent);
5073 surface->synchronized = 1;
5074
5075 switch (default_mode) {
5076 case SUBSURFACE_SYNCHRONIZED:
5077 surface->synchronized_default = 1;
5078 break;
5079 case SUBSURFACE_DESYNCHRONIZED:
5080 surface->synchronized_default = 0;
5081 break;
5082 default:
5083 assert(!"bad enum subsurface_mode");
5084 }
5085
Jasper St. Pierree22952b2013-11-11 20:07:33 -05005086 window->resize_needed = 1;
5087 window_schedule_redraw(window);
5088
Pekka Paalanen35e82632013-04-25 13:57:48 +03005089 return widget;
5090}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04005091
5092static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005093display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005094 struct wl_output *wl_output,
5095 int x, int y,
5096 int physical_width,
5097 int physical_height,
5098 int subpixel,
5099 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04005100 const char *model,
5101 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005102{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005103 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005104
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005105 output->allocation.x = x;
5106 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06005107 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05005108
5109 if (output->make)
5110 free(output->make);
5111 output->make = strdup(make);
5112
5113 if (output->model)
5114 free(output->model);
5115 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005116}
5117
5118static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02005119display_handle_done(void *data,
5120 struct wl_output *wl_output)
5121{
5122}
5123
5124static void
5125display_handle_scale(void *data,
5126 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02005127 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02005128{
5129 struct output *output = data;
5130
5131 output->scale = scale;
5132}
5133
5134static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005135display_handle_mode(void *data,
5136 struct wl_output *wl_output,
5137 uint32_t flags,
5138 int width,
5139 int height,
5140 int refresh)
5141{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005142 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005143 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04005144
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005145 if (flags & WL_OUTPUT_MODE_CURRENT) {
5146 output->allocation.width = width;
5147 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005148 if (display->output_configure_handler)
5149 (*display->output_configure_handler)(
5150 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005151 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005152}
5153
5154static const struct wl_output_listener output_listener = {
5155 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02005156 display_handle_mode,
5157 display_handle_done,
5158 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005159};
5160
5161static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005162display_add_output(struct display *d, uint32_t id)
5163{
5164 struct output *output;
5165
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07005166 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005167 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02005168 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005169 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02005170 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005171 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005172 wl_list_insert(d->output_list.prev, &output->link);
5173
5174 wl_output_add_listener(output->output, &output_listener, output);
5175}
5176
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005177static void
5178output_destroy(struct output *output)
5179{
5180 if (output->destroy_handler)
5181 (*output->destroy_handler)(output, output->user_data);
5182
5183 wl_output_destroy(output->output);
5184 wl_list_remove(&output->link);
5185 free(output);
5186}
5187
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005188static void
5189display_destroy_output(struct display *d, uint32_t id)
5190{
5191 struct output *output;
5192
5193 wl_list_for_each(output, &d->output_list, link) {
5194 if (output->server_output_id == id) {
5195 output_destroy(output);
5196 break;
5197 }
5198 }
5199}
5200
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005201void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005202display_set_global_handler(struct display *display,
5203 display_global_handler_t handler)
5204{
5205 struct global *global;
5206
5207 display->global_handler = handler;
5208 if (!handler)
5209 return;
5210
5211 wl_list_for_each(global, &display->global_list, link)
5212 display->global_handler(display,
5213 global->name, global->interface,
5214 global->version, display->user_data);
5215}
5216
5217void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005218display_set_global_handler_remove(struct display *display,
5219 display_global_handler_t remove_handler)
5220{
5221 display->global_handler_remove = remove_handler;
5222 if (!remove_handler)
5223 return;
5224}
5225
5226void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005227display_set_output_configure_handler(struct display *display,
5228 display_output_handler_t handler)
5229{
5230 struct output *output;
5231
5232 display->output_configure_handler = handler;
5233 if (!handler)
5234 return;
5235
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005236 wl_list_for_each(output, &display->output_list, link) {
5237 if (output->allocation.width == 0 &&
5238 output->allocation.height == 0)
5239 continue;
5240
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005241 (*display->output_configure_handler)(output,
5242 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005243 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005244}
5245
5246void
5247output_set_user_data(struct output *output, void *data)
5248{
5249 output->user_data = data;
5250}
5251
5252void *
5253output_get_user_data(struct output *output)
5254{
5255 return output->user_data;
5256}
5257
5258void
5259output_set_destroy_handler(struct output *output,
5260 display_output_handler_t handler)
5261{
5262 output->destroy_handler = handler;
5263 /* FIXME: implement this, once we have way to remove outputs */
5264}
5265
5266void
Scott Moreau4e072362012-09-29 02:03:11 -06005267output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005268{
Scott Moreau4e072362012-09-29 02:03:11 -06005269 struct rectangle allocation = output->allocation;
5270
5271 switch (output->transform) {
5272 case WL_OUTPUT_TRANSFORM_90:
5273 case WL_OUTPUT_TRANSFORM_270:
5274 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5275 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5276 /* Swap width and height */
5277 allocation.width = output->allocation.height;
5278 allocation.height = output->allocation.width;
5279 break;
5280 }
5281
5282 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005283}
5284
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005285struct wl_output *
5286output_get_wl_output(struct output *output)
5287{
5288 return output->output;
5289}
5290
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005291enum wl_output_transform
5292output_get_transform(struct output *output)
5293{
5294 return output->transform;
5295}
5296
Alexander Larssonafd319a2013-05-22 14:41:27 +02005297uint32_t
5298output_get_scale(struct output *output)
5299{
5300 return output->scale;
5301}
5302
Jason Ekstrand738715d2014-04-02 19:53:50 -05005303const char *
5304output_get_make(struct output *output)
5305{
5306 return output->make;
5307}
5308
5309const char *
5310output_get_model(struct output *output)
5311{
5312 return output->model;
5313}
5314
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005315static void
Daniel Stone97f68542012-05-30 16:32:01 +01005316fini_xkb(struct input *input)
5317{
5318 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005319 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005320}
5321
5322static void
Derek Foreman3a1580f2015-10-14 09:39:59 -05005323display_add_input(struct display *d, uint32_t id, int display_seat_version)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005324{
5325 struct input *input;
Peter Hutterer87743e92016-01-18 16:38:22 +10005326 int seat_version = MIN(display_seat_version, 5);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005327
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005328 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005329 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005330 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Derek Foreman3a1580f2015-10-14 09:39:59 -05005331 seat_version);
Rusty Lynch1084da52013-08-15 09:10:08 -07005332 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005333 input->pointer_focus = NULL;
5334 input->keyboard_focus = NULL;
Derek Foreman3a1580f2015-10-14 09:39:59 -05005335 input->seat_version = seat_version;
5336
Rusty Lynch041815a2013-08-08 21:20:38 -07005337 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005338 wl_list_insert(d->input_list.prev, &input->link);
5339
Daniel Stone37816df2012-05-16 18:45:18 +01005340 wl_seat_add_listener(input->seat, &seat_listener, input);
5341 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005342
Jason Ekstranda669bd52014-04-02 19:53:51 -05005343 if (d->data_device_manager) {
5344 input->data_device =
5345 wl_data_device_manager_get_data_device(d->data_device_manager,
5346 input->seat);
5347 wl_data_device_add_listener(input->data_device,
5348 &data_device_listener,
5349 input);
5350 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005351
5352 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Derek Foreman118a4292015-04-22 17:23:35 -05005353 input->cursor_task.run = cursor_timer_func;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005354
Derek Foreman118a4292015-04-22 17:23:35 -05005355 input->cursor_delay_fd = timerfd_create(CLOCK_MONOTONIC,
5356 TFD_CLOEXEC | TFD_NONBLOCK);
5357 display_watch_fd(d, input->cursor_delay_fd, EPOLLIN,
5358 &input->cursor_task);
Jonny Lamb06959082014-08-12 14:58:27 +02005359 set_repeat_info(input, 40, 400);
5360
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005361 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5362 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005363 input->repeat_task.run = keyboard_repeat_func;
5364 display_watch_fd(d, input->repeat_timer_fd,
5365 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005366}
5367
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005368static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005369input_destroy(struct input *input)
5370{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005371 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005372 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005373
5374 if (input->drag_offer)
5375 data_offer_destroy(input->drag_offer);
5376
5377 if (input->selection_offer)
5378 data_offer_destroy(input->selection_offer);
5379
kabeer khan6ce67ec2014-10-20 11:55:29 +05305380 if (input->data_device) {
Dawid Gajownik74a635b2015-08-06 17:12:19 -03005381 if (input->display->data_device_manager_version >= 2)
kabeer khan6ce67ec2014-10-20 11:55:29 +05305382 wl_data_device_release(input->data_device);
5383 else
5384 wl_data_device_destroy(input->data_device);
5385 }
Derek Foreman3a1580f2015-10-14 09:39:59 -05005386 if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) {
FORT Davidf7bb9352015-10-09 18:17:43 +02005387 if (input->touch)
5388 wl_touch_release(input->touch);
Rob Bradford08031182013-08-13 20:11:03 +01005389 if (input->pointer)
5390 wl_pointer_release(input->pointer);
5391 if (input->keyboard)
5392 wl_keyboard_release(input->keyboard);
FORT Davidf7bb9352015-10-09 18:17:43 +02005393 } else {
5394 if (input->touch)
5395 wl_touch_destroy(input->touch);
5396 if (input->pointer)
5397 wl_pointer_destroy(input->pointer);
5398 if (input->keyboard)
5399 wl_keyboard_destroy(input->keyboard);
Rob Bradford08031182013-08-13 20:11:03 +01005400 }
5401
Daniel Stone97f68542012-05-30 16:32:01 +01005402 fini_xkb(input);
5403
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005404 wl_surface_destroy(input->pointer_surface);
5405
Pekka Paalanene1207c72011-12-16 12:02:09 +02005406 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005407 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005408 close(input->repeat_timer_fd);
Derek Foreman118a4292015-04-22 17:23:35 -05005409 close(input->cursor_delay_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005410 free(input);
5411}
5412
5413static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005414shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5415{
5416 struct display *d = data;
5417
5418 if (format == WL_SHM_FORMAT_RGB565)
5419 d->has_rgb565 = 1;
5420}
5421
5422struct wl_shm_listener shm_listener = {
5423 shm_format
5424};
5425
5426static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005427xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5428{
5429 xdg_shell_pong(shell, serial);
5430}
5431
5432static const struct xdg_shell_listener xdg_shell_listener = {
5433 xdg_shell_ping,
5434};
5435
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +08005436#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005437#ifdef static_assert
5438static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5439 "Interface version doesn't match implementation version");
5440#endif
5441
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005442static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005443registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5444 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005445{
5446 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005447 struct global *global;
5448
Brian Lovinbc919262013-08-07 15:34:59 -07005449 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005450 global->name = id;
5451 global->interface = strdup(interface);
5452 global->version = version;
5453 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005454
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005455 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005456 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005457 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005458 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005459 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005460 } else if (strcmp(interface, "wl_seat") == 0) {
Derek Foreman3a1580f2015-10-14 09:39:59 -05005461 display_add_input(d, id, version);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005462 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005463 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005464 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005465 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
Carlos Garnacho78d4bf92016-01-15 21:14:23 +01005466 d->data_device_manager_version = MIN(version, 3);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005467 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005468 wl_registry_bind(registry, id,
kabeer khan6ce67ec2014-10-20 11:55:29 +05305469 &wl_data_device_manager_interface,
5470 d->data_device_manager_version);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005471 } else if (strcmp(interface, "xdg_shell") == 0) {
5472 d->xdg_shell = wl_registry_bind(registry, id,
5473 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005474 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005475 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005476 } else if (strcmp(interface, "text_cursor_position") == 0) {
5477 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005478 wl_registry_bind(registry, id,
5479 &text_cursor_position_interface, 1);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005480 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5481 d->subcompositor =
5482 wl_registry_bind(registry, id,
5483 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005484 }
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005485 else if (strcmp(interface, "ivi_application") == 0) {
5486 d->ivi_application =
5487 wl_registry_bind(registry, id,
5488 &ivi_application_interface, 1);
5489 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005490
5491 if (d->global_handler)
5492 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005493}
5494
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005495static void
5496registry_handle_global_remove(void *data, struct wl_registry *registry,
5497 uint32_t name)
5498{
5499 struct display *d = data;
5500 struct global *global;
5501 struct global *tmp;
5502
5503 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5504 if (global->name != name)
5505 continue;
5506
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005507 if (strcmp(global->interface, "wl_output") == 0)
5508 display_destroy_output(d, name);
5509
5510 /* XXX: Should destroy remaining bound globals */
5511
5512 if (d->global_handler_remove)
5513 d->global_handler_remove(d, name, global->interface,
5514 global->version, d->user_data);
5515
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005516 wl_list_remove(&global->link);
5517 free(global->interface);
5518 free(global);
5519 }
5520}
5521
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005522void *
5523display_bind(struct display *display, uint32_t name,
5524 const struct wl_interface *interface, uint32_t version)
5525{
5526 return wl_registry_bind(display->registry, name, interface, version);
5527}
5528
5529static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005530 registry_handle_global,
5531 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005532};
5533
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005534#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005535static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005536init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005537{
5538 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005539 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005540
Rob Clark6396ed32012-03-11 19:48:41 -05005541#ifdef USE_CAIRO_GLESV2
5542# define GL_BIT EGL_OPENGL_ES2_BIT
5543#else
5544# define GL_BIT EGL_OPENGL_BIT
5545#endif
5546
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005547 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005548 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005549 EGL_RED_SIZE, 1,
5550 EGL_GREEN_SIZE, 1,
5551 EGL_BLUE_SIZE, 1,
5552 EGL_ALPHA_SIZE, 1,
5553 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005554 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005555 EGL_NONE
5556 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005557
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005558#ifdef USE_CAIRO_GLESV2
5559 static const EGLint context_attribs[] = {
5560 EGL_CONTEXT_CLIENT_VERSION, 2,
5561 EGL_NONE
5562 };
5563 EGLint api = EGL_OPENGL_ES_API;
5564#else
5565 EGLint *context_attribs = NULL;
5566 EGLint api = EGL_OPENGL_API;
5567#endif
5568
Jonny Lamb51a7ae52015-03-20 15:26:51 +01005569 d->dpy =
5570 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
5571 d->display, NULL);
5572
Yuval Fledel45568f62010-12-06 09:18:12 -05005573 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005574 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005575 return -1;
5576 }
5577
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005578 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005579 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005580 return -1;
5581 }
5582
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005583 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5584 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005585 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005586 return -1;
5587 }
5588
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005589 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005590 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005591 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005592 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005593 return -1;
5594 }
5595
Benjamin Franzke0c991632011-09-27 21:57:31 +02005596 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5597 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005598 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005599 return -1;
5600 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005601
5602 return 0;
5603}
5604
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005605static void
5606fini_egl(struct display *display)
5607{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005608 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005609
5610 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5611 EGL_NO_CONTEXT);
5612
5613 eglTerminate(display->dpy);
5614 eglReleaseThread();
5615}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005616#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005617
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005618static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005619init_dummy_surface(struct display *display)
5620{
5621 int len;
5622 void *data;
5623
5624 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
Derek Foreman22044922014-11-20 15:42:35 -06005625 data = xmalloc(len);
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005626 display->dummy_surface =
5627 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5628 1, 1, len);
5629 display->dummy_surface_data = data;
5630}
5631
5632static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005633handle_display_data(struct task *task, uint32_t events)
5634{
5635 struct display *display =
5636 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005637 struct epoll_event ep;
5638 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005639
5640 display->display_fd_events = events;
5641
5642 if (events & EPOLLERR || events & EPOLLHUP) {
5643 display_exit(display);
5644 return;
5645 }
5646
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005647 if (events & EPOLLIN) {
5648 ret = wl_display_dispatch(display->display);
5649 if (ret == -1) {
5650 display_exit(display);
5651 return;
5652 }
5653 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005654
5655 if (events & EPOLLOUT) {
5656 ret = wl_display_flush(display->display);
5657 if (ret == 0) {
5658 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5659 ep.data.ptr = &display->display_task;
5660 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5661 display->display_fd, &ep);
5662 } else if (ret == -1 && errno != EAGAIN) {
5663 display_exit(display);
5664 return;
5665 }
5666 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005667}
5668
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005669static void
5670log_handler(const char *format, va_list args)
5671{
5672 vfprintf(stderr, format, args);
5673}
5674
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005675struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005676display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005677{
5678 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005679
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005680 wl_log_set_handler_client(log_handler);
5681
Peter Huttererf3d62272013-08-08 11:57:05 +10005682 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005683 if (d == NULL)
5684 return NULL;
5685
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005686 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005687 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005688 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005689 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005690 return NULL;
5691 }
5692
Rob Bradford5ab9c752013-07-26 16:29:43 +01005693 d->xkb_context = xkb_context_new(0);
5694 if (d->xkb_context == NULL) {
5695 fprintf(stderr, "Failed to create XKB context\n");
5696 free(d);
5697 return NULL;
5698 }
5699
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005700 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005701 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005702 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005703 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5704 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005705
5706 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005707 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005708 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005709 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005710
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005711 d->registry = wl_display_get_registry(d->display);
5712 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005713
Marek Chalupaa519d062014-12-05 13:49:40 +01005714 if (wl_display_roundtrip(d->display) < 0) {
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005715 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5716 return NULL;
5717 }
5718
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005719#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005720 if (init_egl(d) < 0)
5721 fprintf(stderr, "EGL does not seem to work, "
5722 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005723#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005724
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005725 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005726
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005727 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005728
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005729 wl_list_init(&d->window_list);
5730
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005731 init_dummy_surface(d);
5732
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005733 return d;
5734}
5735
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005736static void
5737display_destroy_outputs(struct display *display)
5738{
5739 struct output *tmp;
5740 struct output *output;
5741
5742 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5743 output_destroy(output);
5744}
5745
Pekka Paalanene1207c72011-12-16 12:02:09 +02005746static void
5747display_destroy_inputs(struct display *display)
5748{
5749 struct input *tmp;
5750 struct input *input;
5751
5752 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5753 input_destroy(input);
5754}
5755
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005756void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005757display_destroy(struct display *display)
5758{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005759 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005760 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5761 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005762
5763 if (!wl_list_empty(&display->deferred_list))
5764 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5765
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005766 cairo_surface_destroy(display->dummy_surface);
5767 free(display->dummy_surface_data);
5768
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005769 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005770 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005771
Daniel Stone97f68542012-05-30 16:32:01 +01005772 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005773
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005774 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005775 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005776
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005777#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005778 if (display->argb_device)
5779 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005780#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005781
Pekka Paalanen35e82632013-04-25 13:57:48 +03005782 if (display->subcompositor)
5783 wl_subcompositor_destroy(display->subcompositor);
5784
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005785 if (display->xdg_shell)
5786 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005787
Nobuhiko Tanibatab1d121d2014-11-27 13:24:52 +09005788 if (display->ivi_application)
5789 ivi_application_destroy(display->ivi_application);
5790
Pekka Paalanenc2052982011-12-16 11:41:32 +02005791 if (display->shm)
5792 wl_shm_destroy(display->shm);
5793
5794 if (display->data_device_manager)
5795 wl_data_device_manager_destroy(display->data_device_manager);
5796
5797 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005798 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005799
5800 close(display->epoll_fd);
5801
U. Artie Eoff44874d92012-10-02 21:12:35 -07005802 if (!(display->display_fd_events & EPOLLERR) &&
5803 !(display->display_fd_events & EPOLLHUP))
5804 wl_display_flush(display->display);
5805
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005806 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005807 free(display);
5808}
5809
5810void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005811display_set_user_data(struct display *display, void *data)
5812{
5813 display->user_data = data;
5814}
5815
5816void *
5817display_get_user_data(struct display *display)
5818{
5819 return display->user_data;
5820}
5821
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005822struct wl_display *
5823display_get_display(struct display *display)
5824{
5825 return display->display;
5826}
5827
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005828int
5829display_has_subcompositor(struct display *display)
5830{
5831 if (display->subcompositor)
5832 return 1;
5833
5834 wl_display_roundtrip(display->display);
5835
5836 return display->subcompositor != NULL;
5837}
5838
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005839cairo_device_t *
5840display_get_cairo_device(struct display *display)
5841{
5842 return display->argb_device;
5843}
5844
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005845struct output *
5846display_get_output(struct display *display)
5847{
Armin Krezović7dda25b2016-06-23 11:59:31 +02005848 if (wl_list_empty(&display->output_list))
5849 return NULL;
5850
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005851 return container_of(display->output_list.next, struct output, link);
5852}
5853
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005854struct wl_compositor *
5855display_get_compositor(struct display *display)
5856{
5857 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005858}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005859
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005860uint32_t
5861display_get_serial(struct display *display)
5862{
5863 return display->serial;
5864}
5865
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005866EGLDisplay
5867display_get_egl_display(struct display *d)
5868{
5869 return d->dpy;
5870}
5871
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005872struct wl_data_source *
5873display_create_data_source(struct display *display)
5874{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005875 if (display->data_device_manager)
5876 return wl_data_device_manager_create_data_source(display->data_device_manager);
5877 else
5878 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005879}
5880
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005881EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005882display_get_argb_egl_config(struct display *d)
5883{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005884 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005885}
5886
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005887int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005888display_acquire_window_surface(struct display *display,
5889 struct window *window,
5890 EGLContext ctx)
5891{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005892 struct surface *surface = window->main_surface;
5893
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005894 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005895 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005896
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005897 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005898 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005899}
5900
5901void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005902display_release_window_surface(struct display *display,
5903 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005904{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005905 struct surface *surface = window->main_surface;
5906
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005907 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005908 return;
5909
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005910 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005911}
5912
5913void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005914display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005915{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005916 wl_list_insert(&display->deferred_list, &task->link);
5917}
5918
5919void
5920display_watch_fd(struct display *display,
5921 int fd, uint32_t events, struct task *task)
5922{
5923 struct epoll_event ep;
5924
5925 ep.events = events;
5926 ep.data.ptr = task;
5927 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5928}
5929
5930void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005931display_unwatch_fd(struct display *display, int fd)
5932{
5933 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5934}
5935
5936void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005937display_run(struct display *display)
5938{
5939 struct task *task;
5940 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005941 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005942
Pekka Paalanen826d7952011-12-15 10:14:07 +02005943 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005944 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005945 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005946 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005947 struct task, link);
5948 wl_list_remove(&task->link);
5949 task->run(task, 0);
5950 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005951
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005952 wl_display_dispatch_pending(display->display);
5953
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005954 if (!display->running)
5955 break;
5956
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005957 ret = wl_display_flush(display->display);
5958 if (ret < 0 && errno == EAGAIN) {
5959 ep[0].events =
5960 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5961 ep[0].data.ptr = &display->display_task;
5962
5963 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5964 display->display_fd, &ep[0]);
5965 } else if (ret < 0) {
5966 break;
5967 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005968
5969 count = epoll_wait(display->epoll_fd,
5970 ep, ARRAY_LENGTH(ep), -1);
5971 for (i = 0; i < count; i++) {
5972 task = ep[i].data.ptr;
5973 task->run(task, ep[i].events);
5974 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005975 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005976}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005977
5978void
5979display_exit(struct display *display)
5980{
5981 display->running = 0;
5982}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005983
Carlos Garnacho9c931792016-01-18 23:52:12 +01005984int
5985display_get_data_device_manager_version(struct display *display)
5986{
5987 return display->data_device_manager_version;
5988}
5989
Jan Arne Petersencd997062012-11-18 19:06:44 +01005990void
5991keysym_modifiers_add(struct wl_array *modifiers_map,
5992 const char *name)
5993{
5994 size_t len = strlen(name) + 1;
5995 char *p;
5996
5997 p = wl_array_add(modifiers_map, len);
5998
5999 if (p == NULL)
6000 return;
6001
6002 strncpy(p, name, len);
6003}
6004
6005static xkb_mod_index_t
6006keysym_modifiers_get_index(struct wl_array *modifiers_map,
6007 const char *name)
6008{
6009 xkb_mod_index_t index = 0;
6010 char *p = modifiers_map->data;
6011
6012 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
6013 if (strcmp(p, name) == 0)
6014 return index;
6015
6016 index++;
6017 p += strlen(p) + 1;
6018 }
6019
6020 return XKB_MOD_INVALID;
6021}
6022
6023xkb_mod_mask_t
6024keysym_modifiers_get_mask(struct wl_array *modifiers_map,
6025 const char *name)
6026{
6027 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
6028
6029 if (index == XKB_MOD_INVALID)
6030 return XKB_MOD_INVALID;
6031
6032 return 1 << index;
6033}