blob: 005102b02f0e5d3816cb32e9a6215a149bb7d1dc [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 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050068#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030088 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050093 struct xdg_shell *xdg_shell;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800121 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200133
134 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100135 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500136};
137
Rob Bradford7507b572012-05-15 17:55:34 +0100138struct window_output {
139 struct output *output;
140 struct wl_list link;
141};
142
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200143struct toysurface {
144 /*
145 * Prepare the surface for drawing. Makes sure there is a surface
146 * of the right size available for rendering, and returns it.
147 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200148 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200149 * If flags has SURFACE_HINT_RESIZE set, the user is
150 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151 * Returns the Cairo surface to draw to.
152 */
153 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200154 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200155 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156
157 /*
158 * Post the surface to the server, returning the server allocation
159 * rectangle. The Cairo surface from prepare() must be destroyed
160 * after calling this.
161 */
162 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200163 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164 struct rectangle *server_allocation);
165
166 /*
167 * Make the toysurface current with the given EGL context.
168 * Returns 0 on success, and negative of failure.
169 */
170 int (*acquire)(struct toysurface *base, EGLContext ctx);
171
172 /*
173 * Release the toysurface from the EGL context, returning control
174 * to Cairo.
175 */
176 void (*release)(struct toysurface *base);
177
178 /*
179 * Destroy the toysurface, including the Cairo surface, any
180 * backing storage, and the Wayland protocol objects.
181 */
182 void (*destroy)(struct toysurface *base);
183};
184
Pekka Paalanen4e373742013-02-13 16:17:13 +0200185struct surface {
186 struct window *window;
187
188 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300189 struct wl_subsurface *subsurface;
190 int synchronized;
191 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200192 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200193 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300194 int redraw_needed;
195 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300196 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197
198 struct rectangle allocation;
199 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200200
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200201 struct wl_region *input_region;
202 struct wl_region *opaque_region;
203
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200204 enum window_buffer_type buffer_type;
205 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200206 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200207
208 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300209
210 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200211};
212
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500213struct window {
214 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100215 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500216 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200217 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400218 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500219 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500220 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400221 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300222 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400223 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400224 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500225 int custom;
226 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400227
Pekka Paalanen99436862012-11-19 17:15:59 +0200228 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400229
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500230 int fullscreen;
231 int maximized;
232
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200233 enum preferred_format preferred_format;
234
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500235 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500236 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400237 window_data_handler_t data_handler;
238 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500239 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400240 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200241 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400242
Pekka Paalanen4e373742013-02-13 16:17:13 +0200243 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500244 struct xdg_surface *xdg_surface;
245 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300246
Jasper St. Pierre53686042013-12-09 15:26:25 -0500247 struct window *transient_for;
248
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500249 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500250
Pekka Paalanen35e82632013-04-25 13:57:48 +0300251 /* struct surface::link, contains also main_surface */
252 struct wl_list subsurface_list;
253
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500254 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400255 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500256};
257
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500258struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500259 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200260 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300261 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500262 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400263 struct wl_list link;
264 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500265 widget_resize_handler_t resize_handler;
266 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500267 widget_enter_handler_t enter_handler;
268 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500269 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500270 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700271 widget_touch_down_handler_t touch_down_handler;
272 widget_touch_up_handler_t touch_up_handler;
273 widget_touch_motion_handler_t touch_motion_handler;
274 widget_touch_frame_handler_t touch_frame_handler;
275 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200276 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400277 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500278 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300279 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500280 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000281 /* If this is set to false then no cairo surface will be
282 * created before redrawing the surface. This is useful if the
283 * redraw handler is going to do completely custom rendering
284 * such as using EGL directly */
285 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400286};
287
Rusty Lynch041815a2013-08-08 21:20:38 -0700288struct touch_point {
289 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800290 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700291 struct widget *widget;
292 struct wl_list link;
293};
294
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400295struct input {
296 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100297 struct wl_seat *seat;
298 struct wl_pointer *pointer;
299 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700300 struct wl_touch *touch;
301 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400302 struct window *pointer_focus;
303 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700304 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300305 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300306 uint32_t cursor_anim_start;
307 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300308 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400309 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400310 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400311 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400312 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400313 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400314
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500315 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500316 struct widget *grab;
317 uint32_t grab_button;
318
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400319 struct wl_data_device *data_device;
320 struct data_offer *drag_offer;
321 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800322 uint32_t touch_grab;
323 int32_t touch_grab_id;
324 float drag_x, drag_y;
325 struct window *drag_focus;
326 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100327
328 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100329 struct xkb_keymap *keymap;
330 struct xkb_state *state;
331 xkb_mod_mask_t control_mask;
332 xkb_mod_mask_t alt_mask;
333 xkb_mod_mask_t shift_mask;
334 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400335
336 struct task repeat_task;
337 int repeat_timer_fd;
338 uint32_t repeat_sym;
339 uint32_t repeat_key;
340 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400341};
342
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500343struct output {
344 struct display *display;
345 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800346 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500347 struct rectangle allocation;
348 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600349 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200350 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500351 char *make;
352 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200353
354 display_output_handler_t destroy_handler;
355 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500356};
357
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500358struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500359 struct widget *widget;
360 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500361 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500362};
363
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500364struct menu {
365 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500366 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500367 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500368 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700369 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500370 const char **entries;
371 uint32_t time;
372 int current;
373 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400374 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500375 menu_func_t func;
376};
377
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300378struct tooltip {
379 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300380 struct widget *widget;
381 char *entry;
382 struct task tooltip_task;
383 int tooltip_fd;
384 float x, y;
385};
386
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700387struct shm_pool {
388 struct wl_shm_pool *pool;
389 size_t size;
390 size_t used;
391 void *data;
392};
393
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400394enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300395 CURSOR_DEFAULT = 100,
396 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400397};
398
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200399static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400400
Pekka Paalanen97777442013-05-22 10:20:05 +0300401/* #define DEBUG */
402
403#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300404
405static void
406debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
407__attribute__ ((format (printf, 4, 5)));
408
409static void
410debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
411{
412 va_list ap;
413 struct timeval tv;
414
415 gettimeofday(&tv, NULL);
416 fprintf(stderr, "%8ld.%03ld ",
417 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
418
419 if (proxy)
420 fprintf(stderr, "%s@%d ",
421 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
422
423 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
424 fprintf(stderr, "%s ", func);
425
426 va_start(ap, fmt);
427 vfprintf(stderr, fmt, ap);
428 va_end(ap);
429}
430
431#define DBG(fmt, ...) \
432 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
433
434#define DBG_OBJ(obj, fmt, ...) \
435 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
436
437#else
438
439#define DBG(...) do {} while (0)
440#define DBG_OBJ(...) do {} while (0)
441
442#endif
443
Alexander Larsson1818e312013-05-22 14:41:31 +0200444static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200445surface_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 +0200446{
447 int32_t tmp;
448
449 switch (buffer_transform) {
450 case WL_OUTPUT_TRANSFORM_90:
451 case WL_OUTPUT_TRANSFORM_270:
452 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
453 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
454 tmp = *width;
455 *width = *height;
456 *height = tmp;
457 break;
458 default:
459 break;
460 }
461
462 *width *= buffer_scale;
463 *height *= buffer_scale;
464}
465
466static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200467buffer_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 +0200468{
469 int32_t tmp;
470
471 switch (buffer_transform) {
472 case WL_OUTPUT_TRANSFORM_90:
473 case WL_OUTPUT_TRANSFORM_270:
474 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
476 tmp = *width;
477 *width = *height;
478 *height = tmp;
479 break;
480 default:
481 break;
482 }
483
484 *width /= buffer_scale;
485 *height /= buffer_scale;
486}
487
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500488#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400489
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200490struct egl_window_surface {
491 struct toysurface base;
492 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100493 struct display *display;
494 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200495 struct wl_egl_window *egl_window;
496 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100497};
498
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200499static struct egl_window_surface *
500to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100501{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200502 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100503}
504
505static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200506egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200507 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200508 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100509{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510 struct egl_window_surface *surface = to_egl_window_surface(base);
511
Alexander Larsson1818e312013-05-22 14:41:31 +0200512 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
513
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
515 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
516
517 return cairo_surface_reference(surface->cairo_surface);
518}
519
520static void
521egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200522 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 struct rectangle *server_allocation)
524{
525 struct egl_window_surface *surface = to_egl_window_surface(base);
526
527 cairo_gl_surface_swapbuffers(surface->cairo_surface);
528 wl_egl_window_get_attached_size(surface->egl_window,
529 &server_allocation->width,
530 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200531
532 buffer_to_surface_size (buffer_transform, buffer_scale,
533 &server_allocation->width,
534 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200535}
536
537static int
538egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
539{
540 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200541 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100542
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200543 device = cairo_surface_get_device(surface->cairo_surface);
544 if (!device)
545 return -1;
546
547 if (!ctx) {
548 if (device == surface->display->argb_device)
549 ctx = surface->display->argb_ctx;
550 else
551 assert(0);
552 }
553
554 cairo_device_flush(device);
555 cairo_device_acquire(device);
556 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
557 surface->egl_surface, ctx))
558 fprintf(stderr, "failed to make surface current\n");
559
560 return 0;
561}
562
563static void
564egl_window_surface_release(struct toysurface *base)
565{
566 struct egl_window_surface *surface = to_egl_window_surface(base);
567 cairo_device_t *device;
568
569 device = cairo_surface_get_device(surface->cairo_surface);
570 if (!device)
571 return;
572
573 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
574 surface->display->argb_ctx))
575 fprintf(stderr, "failed to make context current\n");
576
577 cairo_device_release(device);
578}
579
580static void
581egl_window_surface_destroy(struct toysurface *base)
582{
583 struct egl_window_surface *surface = to_egl_window_surface(base);
584 struct display *d = surface->display;
585
586 cairo_surface_destroy(surface->cairo_surface);
587 eglDestroySurface(d->dpy, surface->egl_surface);
588 wl_egl_window_destroy(surface->egl_window);
589 surface->surface = NULL;
590
591 free(surface);
592}
593
594static struct toysurface *
595egl_window_surface_create(struct display *display,
596 struct wl_surface *wl_surface,
597 uint32_t flags,
598 struct rectangle *rectangle)
599{
600 struct egl_window_surface *surface;
601
Pekka Paalanenb3627362012-11-19 17:16:00 +0200602 if (display->dpy == EGL_NO_DISPLAY)
603 return NULL;
604
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200605 surface = calloc(1, sizeof *surface);
606 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100607 return NULL;
608
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200609 surface->base.prepare = egl_window_surface_prepare;
610 surface->base.swap = egl_window_surface_swap;
611 surface->base.acquire = egl_window_surface_acquire;
612 surface->base.release = egl_window_surface_release;
613 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100614
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200615 surface->display = display;
616 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400617
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200618 surface->egl_window = wl_egl_window_create(surface->surface,
619 rectangle->width,
620 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100621
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200622 surface->egl_surface = eglCreateWindowSurface(display->dpy,
623 display->argb_config,
624 surface->egl_window,
625 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100626
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200627 surface->cairo_surface =
628 cairo_gl_surface_create_for_egl(display->argb_device,
629 surface->egl_surface,
630 rectangle->width,
631 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634}
635
Pekka Paalanenb3627362012-11-19 17:16:00 +0200636#else
637
638static struct toysurface *
639egl_window_surface_create(struct display *display,
640 struct wl_surface *wl_surface,
641 uint32_t flags,
642 struct rectangle *rectangle)
643{
644 return NULL;
645}
646
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400647#endif
648
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200649struct shm_surface_data {
650 struct wl_buffer *buffer;
651 struct shm_pool *pool;
652};
653
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400654struct wl_buffer *
655display_get_buffer_for_surface(struct display *display,
656 cairo_surface_t *surface)
657{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200658 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400659
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200660 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400661
662 return data->buffer;
663}
664
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500665static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700666shm_pool_destroy(struct shm_pool *pool);
667
668static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669shm_surface_data_destroy(void *p)
670{
671 struct shm_surface_data *data = p;
672
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200673 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700674 if (data->pool)
675 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300676
677 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400678}
679
Kristian Høgsberg16626282012-04-03 11:21:27 -0400680static struct wl_shm_pool *
681make_shm_pool(struct display *display, int size, void **data)
682{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400683 struct wl_shm_pool *pool;
684 int fd;
685
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300686 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400687 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300688 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
689 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400690 return NULL;
691 }
692
693 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400694 if (*data == MAP_FAILED) {
695 fprintf(stderr, "mmap failed: %m\n");
696 close(fd);
697 return NULL;
698 }
699
700 pool = wl_shm_create_pool(display->shm, fd, size);
701
702 close(fd);
703
704 return pool;
705}
706
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700707static struct shm_pool *
708shm_pool_create(struct display *display, size_t size)
709{
710 struct shm_pool *pool = malloc(sizeof *pool);
711
712 if (!pool)
713 return NULL;
714
715 pool->pool = make_shm_pool(display, size, &pool->data);
716 if (!pool->pool) {
717 free(pool);
718 return NULL;
719 }
720
721 pool->size = size;
722 pool->used = 0;
723
724 return pool;
725}
726
727static void *
728shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
729{
730 if (pool->used + size > pool->size)
731 return NULL;
732
733 *offset = pool->used;
734 pool->used += size;
735
736 return (char *) pool->data + *offset;
737}
738
739/* destroy the pool. this does not unmap the memory though */
740static void
741shm_pool_destroy(struct shm_pool *pool)
742{
743 munmap(pool->data, pool->size);
744 wl_shm_pool_destroy(pool->pool);
745 free(pool);
746}
747
748/* Start allocating from the beginning of the pool again */
749static void
750shm_pool_reset(struct shm_pool *pool)
751{
752 pool->used = 0;
753}
754
755static int
756data_length_for_shm_surface(struct rectangle *rect)
757{
758 int stride;
759
760 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
761 rect->width);
762 return stride * rect->height;
763}
764
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500765static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700766display_create_shm_surface_from_pool(struct display *display,
767 struct rectangle *rectangle,
768 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400769{
770 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400771 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400772 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200773 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700774 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400775 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400776
777 data = malloc(sizeof *data);
778 if (data == NULL)
779 return NULL;
780
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200781 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
782 cairo_format = CAIRO_FORMAT_RGB16_565;
783 else
784 cairo_format = CAIRO_FORMAT_ARGB32;
785
786 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700787 length = stride * rectangle->height;
788 data->pool = NULL;
789 map = shm_pool_allocate(pool, length, &offset);
790
791 if (!map) {
792 free(data);
793 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400794 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400795
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400796 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200797 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400798 rectangle->width,
799 rectangle->height,
800 stride);
801
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200802 cairo_surface_set_user_data(surface, &shm_surface_data_key,
803 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200805 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
806 format = WL_SHM_FORMAT_RGB565;
807 else {
808 if (flags & SURFACE_OPAQUE)
809 format = WL_SHM_FORMAT_XRGB8888;
810 else
811 format = WL_SHM_FORMAT_ARGB8888;
812 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400813
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200814 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
815 rectangle->width,
816 rectangle->height,
817 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400818
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700819 return surface;
820}
821
822static cairo_surface_t *
823display_create_shm_surface(struct display *display,
824 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200825 struct shm_pool *alternate_pool,
826 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700827{
828 struct shm_surface_data *data;
829 struct shm_pool *pool;
830 cairo_surface_t *surface;
831
Pekka Paalanen99436862012-11-19 17:15:59 +0200832 if (alternate_pool) {
833 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700834 surface = display_create_shm_surface_from_pool(display,
835 rectangle,
836 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200837 alternate_pool);
838 if (surface) {
839 data = cairo_surface_get_user_data(surface,
840 &shm_surface_data_key);
841 goto out;
842 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700843 }
844
845 pool = shm_pool_create(display,
846 data_length_for_shm_surface(rectangle));
847 if (!pool)
848 return NULL;
849
850 surface =
851 display_create_shm_surface_from_pool(display, rectangle,
852 flags, pool);
853
854 if (!surface) {
855 shm_pool_destroy(pool);
856 return NULL;
857 }
858
859 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200860 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700861 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400862
Pekka Paalanen99436862012-11-19 17:15:59 +0200863out:
864 if (data_ret)
865 *data_ret = data;
866
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400867 return surface;
868}
869
nobled7b87cb02011-02-01 18:51:47 +0000870static int
871check_size(struct rectangle *rect)
872{
873 if (rect->width && rect->height)
874 return 0;
875
876 fprintf(stderr, "tried to create surface of "
877 "width: %d, height: %d\n", rect->width, rect->height);
878 return -1;
879}
880
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400881cairo_surface_t *
882display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100883 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400884 struct rectangle *rectangle,
885 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400886{
nobled7b87cb02011-02-01 18:51:47 +0000887 if (check_size(rectangle) < 0)
888 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200889
890 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200891 return display_create_shm_surface(display, rectangle, flags,
892 NULL, NULL);
893}
894
Pekka Paalanena4eda732012-11-19 17:16:02 +0200895struct shm_surface_leaf {
896 cairo_surface_t *cairo_surface;
897 /* 'data' is automatically destroyed, when 'cairo_surface' is */
898 struct shm_surface_data *data;
899
900 struct shm_pool *resize_pool;
901 int busy;
902};
903
904static void
905shm_surface_leaf_release(struct shm_surface_leaf *leaf)
906{
907 if (leaf->cairo_surface)
908 cairo_surface_destroy(leaf->cairo_surface);
909 /* leaf->data already destroyed via cairo private */
910
911 if (leaf->resize_pool)
912 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200913
914 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200915}
916
Pekka Paalanenaef02542013-04-25 13:57:47 +0300917#define MAX_LEAVES 3
918
Pekka Paalanen99436862012-11-19 17:15:59 +0200919struct shm_surface {
920 struct toysurface base;
921 struct display *display;
922 struct wl_surface *surface;
923 uint32_t flags;
924 int dx, dy;
925
Pekka Paalanenaef02542013-04-25 13:57:47 +0300926 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200927 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200928};
929
930static struct shm_surface *
931to_shm_surface(struct toysurface *base)
932{
933 return container_of(base, struct shm_surface, base);
934}
935
Pekka Paalanena4eda732012-11-19 17:16:02 +0200936static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300937shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
938{
939#ifdef DEBUG
940 struct shm_surface_leaf *leaf;
941 char bufs[MAX_LEAVES + 1];
942 int i;
943
944 for (i = 0; i < MAX_LEAVES; i++) {
945 leaf = &surface->leaf[i];
946
947 if (leaf->busy)
948 bufs[i] = 'b';
949 else if (leaf->cairo_surface)
950 bufs[i] = 'a';
951 else
952 bufs[i] = ' ';
953 }
954
955 bufs[MAX_LEAVES] = '\0';
956 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
957#endif
958}
959
960static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200961shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
962{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200963 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300964 struct shm_surface_leaf *leaf;
965 int i;
966 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300967
968 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200969
Pekka Paalanenaef02542013-04-25 13:57:47 +0300970 for (i = 0; i < MAX_LEAVES; i++) {
971 leaf = &surface->leaf[i];
972 if (leaf->data && leaf->data->buffer == buffer) {
973 leaf->busy = 0;
974 break;
975 }
976 }
977 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200978
Pekka Paalanenaef02542013-04-25 13:57:47 +0300979 /* Leave one free leaf with storage, release others */
980 free_found = 0;
981 for (i = 0; i < MAX_LEAVES; i++) {
982 leaf = &surface->leaf[i];
983
984 if (!leaf->cairo_surface || leaf->busy)
985 continue;
986
987 if (!free_found)
988 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300989 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300990 shm_surface_leaf_release(leaf);
991 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300992
Pekka Paalanen97777442013-05-22 10:20:05 +0300993 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200994}
995
996static const struct wl_buffer_listener shm_surface_buffer_listener = {
997 shm_surface_buffer_release
998};
999
Pekka Paalanen99436862012-11-19 17:15:59 +02001000static cairo_surface_t *
1001shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001002 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001003 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001004{
Pekka Paalanenec076692012-11-30 13:37:27 +02001005 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001006 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001007 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001008 struct shm_surface_leaf *leaf = NULL;
1009 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001010
1011 surface->dx = dx;
1012 surface->dy = dy;
1013
Pekka Paalanenaef02542013-04-25 13:57:47 +03001014 /* pick a free buffer, preferrably one that already has storage */
1015 for (i = 0; i < MAX_LEAVES; i++) {
1016 if (surface->leaf[i].busy)
1017 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001018
Pekka Paalanenaef02542013-04-25 13:57:47 +03001019 if (!leaf || surface->leaf[i].cairo_surface)
1020 leaf = &surface->leaf[i];
1021 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001022 DBG_OBJ(surface->surface, "pick leaf %d\n",
1023 (int)(leaf - &surface->leaf[0]));
1024
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 if (!leaf) {
1026 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001027 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001028 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001029 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001030 }
1031
Pekka Paalanena4eda732012-11-19 17:16:02 +02001032 if (!resize_hint && leaf->resize_pool) {
1033 cairo_surface_destroy(leaf->cairo_surface);
1034 leaf->cairo_surface = NULL;
1035 shm_pool_destroy(leaf->resize_pool);
1036 leaf->resize_pool = NULL;
1037 }
1038
Alexander Larsson1818e312013-05-22 14:41:31 +02001039 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1040
Pekka Paalanena4eda732012-11-19 17:16:02 +02001041 if (leaf->cairo_surface &&
1042 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1043 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001044 goto out;
1045
Pekka Paalanena4eda732012-11-19 17:16:02 +02001046 if (leaf->cairo_surface)
1047 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001048
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001049#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001050 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001051 /* Create a big pool to allocate from, while continuously
1052 * resizing. Mmapping a new pool in the server
1053 * is relatively expensive, so reusing a pool performs
1054 * better, but may temporarily reserve unneeded memory.
1055 */
1056 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 leaf->resize_pool = shm_pool_create(surface->display,
1058 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001059 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001060#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001061
Alexander Larsson1818e312013-05-22 14:41:31 +02001062 rect.width = width;
1063 rect.height = height;
1064
Pekka Paalanena4eda732012-11-19 17:16:02 +02001065 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001066 display_create_shm_surface(surface->display, &rect,
1067 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001068 leaf->resize_pool,
1069 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001070 if (!leaf->cairo_surface)
1071 return NULL;
1072
Pekka Paalanena4eda732012-11-19 17:16:02 +02001073 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001074 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001075
1076out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001077 surface->current = leaf;
1078
1079 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001080}
1081
1082static void
1083shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001084 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001085 struct rectangle *server_allocation)
1086{
1087 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001089
1090 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001091 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001092 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001094
Alexander Larsson1818e312013-05-22 14:41:31 +02001095 buffer_to_surface_size (buffer_transform, buffer_scale,
1096 &server_allocation->width,
1097 &server_allocation->height);
1098
Pekka Paalanena4eda732012-11-19 17:16:02 +02001099 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001100 surface->dx, surface->dy);
1101 wl_surface_damage(surface->surface, 0, 0,
1102 server_allocation->width, server_allocation->height);
1103 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104
Pekka Paalanen71233882013-04-25 13:57:53 +03001105 DBG_OBJ(surface->surface, "leaf %d busy\n",
1106 (int)(leaf - &surface->leaf[0]));
1107
Pekka Paalanena4eda732012-11-19 17:16:02 +02001108 leaf->busy = 1;
1109 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001110}
1111
1112static int
1113shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1114{
1115 return -1;
1116}
1117
1118static void
1119shm_surface_release(struct toysurface *base)
1120{
1121}
1122
1123static void
1124shm_surface_destroy(struct toysurface *base)
1125{
1126 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001127 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001128
Pekka Paalanenaef02542013-04-25 13:57:47 +03001129 for (i = 0; i < MAX_LEAVES; i++)
1130 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001131
1132 free(surface);
1133}
1134
1135static struct toysurface *
1136shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1137 uint32_t flags, struct rectangle *rectangle)
1138{
1139 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001140 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001141
Brian Lovinbc919262013-08-07 15:34:59 -07001142 surface = xmalloc(sizeof *surface);
1143 memset(surface, 0, sizeof *surface);
1144
Pekka Paalanen99436862012-11-19 17:15:59 +02001145 if (!surface)
1146 return NULL;
1147
1148 surface->base.prepare = shm_surface_prepare;
1149 surface->base.swap = shm_surface_swap;
1150 surface->base.acquire = shm_surface_acquire;
1151 surface->base.release = shm_surface_release;
1152 surface->base.destroy = shm_surface_destroy;
1153
1154 surface->display = display;
1155 surface->surface = wl_surface;
1156 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001157
1158 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001159}
1160
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001161/*
1162 * The following correspondences between file names and cursors was copied
1163 * from: https://bugs.kde.org/attachment.cgi?id=67313
1164 */
1165
1166static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001167 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001168 "sw-resize",
1169 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001170};
1171
1172static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001173 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001174 "se-resize",
1175 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001176};
1177
1178static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001179 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001180 "s-resize",
1181 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001182};
1183
1184static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001185 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001186 "closedhand",
1187 "208530c400c041818281048008011002"
1188};
1189
1190static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001191 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192 "default",
1193 "top_left_arrow",
1194 "left-arrow"
1195};
1196
1197static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001198 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001199 "w-resize",
1200 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001201};
1202
1203static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001204 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001205 "e-resize",
1206 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001207};
1208
1209static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001210 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001211 "nw-resize",
1212 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001213};
1214
1215static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001216 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001217 "ne-resize",
1218 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001219};
1220
1221static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001222 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001223 "n-resize",
1224 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001225};
1226
1227static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001228 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229 "ibeam",
1230 "text"
1231};
1232
1233static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001234 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235 "pointer",
1236 "pointing_hand",
1237 "e29285e634086352946a0e7090d73106"
1238};
1239
1240static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001241 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001242 "wait",
1243 "0426c94ea35c87780ff01dc239897213"
1244};
1245
1246struct cursor_alternatives {
1247 const char **names;
1248 size_t count;
1249};
1250
1251static const struct cursor_alternatives cursors[] = {
1252 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1253 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1254 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1255 {grabbings, ARRAY_LENGTH(grabbings)},
1256 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1257 {left_sides, ARRAY_LENGTH(left_sides)},
1258 {right_sides, ARRAY_LENGTH(right_sides)},
1259 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1260 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1261 {top_sides, ARRAY_LENGTH(top_sides)},
1262 {xterms, ARRAY_LENGTH(xterms)},
1263 {hand1s, ARRAY_LENGTH(hand1s)},
1264 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001265};
1266
1267static void
1268create_cursors(struct display *display)
1269{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001270 struct weston_config *config;
1271 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001272 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001273 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001274 unsigned int i, j;
1275 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001276
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001277 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001278 s = weston_config_get_section(config, "shell", NULL, NULL);
1279 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1280 weston_config_section_get_int(s, "cursor-size", &size, 32);
1281 weston_config_destroy(config);
1282
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001283 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001284 if (!display->cursor_theme) {
1285 fprintf(stderr, "could not load theme '%s'\n", theme);
1286 return;
1287 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001288 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001289 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001290 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001291
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001292 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001293 cursor = NULL;
1294 for (j = 0; !cursor && j < cursors[i].count; ++j)
1295 cursor = wl_cursor_theme_get_cursor(
1296 display->cursor_theme, cursors[i].names[j]);
1297
1298 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001299 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001300 cursors[i].names[0]);
1301
1302 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001303 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001304}
1305
1306static void
1307destroy_cursors(struct display *display)
1308{
1309 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001310 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001311}
1312
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001313struct wl_cursor_image *
1314display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001315{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001316 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001317
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001318 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001319}
1320
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001321static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001322surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001323{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001324 if (!surface->cairo_surface)
1325 return;
1326
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001327 if (surface->opaque_region) {
1328 wl_surface_set_opaque_region(surface->surface,
1329 surface->opaque_region);
1330 wl_region_destroy(surface->opaque_region);
1331 surface->opaque_region = NULL;
1332 }
1333
1334 if (surface->input_region) {
1335 wl_surface_set_input_region(surface->surface,
1336 surface->input_region);
1337 wl_region_destroy(surface->input_region);
1338 surface->input_region = NULL;
1339 }
1340
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001341 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001342 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001343 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001344
Pekka Paalanen89dee002013-02-13 16:17:20 +02001345 cairo_surface_destroy(surface->cairo_surface);
1346 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001347}
1348
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001349int
1350window_has_focus(struct window *window)
1351{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001352 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001353}
1354
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001355static void
1356window_close(struct window *window)
1357{
1358 if (window->close_handler)
1359 window->close_handler(window->user_data);
1360 else
1361 display_exit(window->display);
1362}
1363
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001364struct display *
1365window_get_display(struct window *window)
1366{
1367 return window->display;
1368}
1369
Pekka Paalanen89dee002013-02-13 16:17:20 +02001370static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001371surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001372{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001373 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001374 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001375
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001376 if (!surface->toysurface && display->dpy &&
1377 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001378 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001379 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001380 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001381 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001382 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001383 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001384
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001385 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001386 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001387 surface->surface,
1388 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001389
Pekka Paalanen89dee002013-02-13 16:17:20 +02001390 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001391 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001392 allocation.width, allocation.height, flags,
1393 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001394}
1395
1396static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001397window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001398{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001399 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001400 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001401
Pekka Paalanenec076692012-11-30 13:37:27 +02001402 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001403 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001404
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001405 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1406 flags |= SURFACE_HINT_RGB565;
1407
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001408 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001409}
1410
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001411int
1412window_get_buffer_transform(struct window *window)
1413{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001415}
1416
1417void
1418window_set_buffer_transform(struct window *window,
1419 enum wl_output_transform transform)
1420{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001421 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001422 wl_surface_set_buffer_transform(window->main_surface->surface,
1423 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001424}
1425
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001426void
1427window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001428 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001429{
1430 window->main_surface->buffer_scale = scale;
1431 wl_surface_set_buffer_scale(window->main_surface->surface,
1432 scale);
1433}
1434
1435uint32_t
1436window_get_buffer_scale(struct window *window)
1437{
1438 return window->main_surface->buffer_scale;
1439}
1440
Alexander Larssond68f5232013-05-22 14:41:33 +02001441uint32_t
1442window_get_output_scale(struct window *window)
1443{
1444 struct window_output *window_output;
1445 struct window_output *window_output_tmp;
1446 int scale = 1;
1447
1448 wl_list_for_each_safe(window_output, window_output_tmp,
1449 &window->window_output_list, link) {
1450 if (window_output->output->scale > scale)
1451 scale = window_output->output->scale;
1452 }
1453
1454 return scale;
1455}
1456
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001457static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001458
Pekka Paalanen4e373742013-02-13 16:17:13 +02001459static void
1460surface_destroy(struct surface *surface)
1461{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001462 if (surface->frame_cb)
1463 wl_callback_destroy(surface->frame_cb);
1464
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001465 if (surface->input_region)
1466 wl_region_destroy(surface->input_region);
1467
1468 if (surface->opaque_region)
1469 wl_region_destroy(surface->opaque_region);
1470
Pekka Paalanen35e82632013-04-25 13:57:48 +03001471 if (surface->subsurface)
1472 wl_subsurface_destroy(surface->subsurface);
1473
Pekka Paalanen4e373742013-02-13 16:17:13 +02001474 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001475
1476 if (surface->toysurface)
1477 surface->toysurface->destroy(surface->toysurface);
1478
Pekka Paalanen35e82632013-04-25 13:57:48 +03001479 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001480 free(surface);
1481}
1482
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001483void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001484window_destroy(struct window *window)
1485{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001486 struct display *display = window->display;
1487 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001488 struct window_output *window_output;
1489 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001490
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001491 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001492
Rusty Lynch1084da52013-08-15 09:10:08 -07001493 wl_list_for_each(input, &display->input_list, link) {
1494 if (input->touch_focus == window)
1495 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001496 if (input->pointer_focus == window)
1497 input->pointer_focus = NULL;
1498 if (input->keyboard_focus == window)
1499 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001500 if (input->focus_widget &&
1501 input->focus_widget->window == window)
1502 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001503 }
1504
Rob Bradford7507b572012-05-15 17:55:34 +01001505 wl_list_for_each_safe(window_output, window_output_tmp,
1506 &window->window_output_list, link) {
1507 free (window_output);
1508 }
1509
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001510 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001511 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001512
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001513 if (window->xdg_surface)
1514 xdg_surface_destroy(window->xdg_surface);
1515 if (window->xdg_popup)
1516 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001517
1518 surface_destroy(window->main_surface);
1519
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001520 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001521
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001522 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001523 free(window);
1524}
1525
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001526static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001527widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001528{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001529 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001530
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001531 wl_list_for_each(child, &widget->child_list, link) {
1532 target = widget_find_widget(child, x, y);
1533 if (target)
1534 return target;
1535 }
1536
1537 if (widget->allocation.x <= x &&
1538 x < widget->allocation.x + widget->allocation.width &&
1539 widget->allocation.y <= y &&
1540 y < widget->allocation.y + widget->allocation.height) {
1541 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001542 }
1543
1544 return NULL;
1545}
1546
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001547static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001548window_find_widget(struct window *window, int32_t x, int32_t y)
1549{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001550 struct surface *surface;
1551 struct widget *widget;
1552
1553 wl_list_for_each(surface, &window->subsurface_list, link) {
1554 widget = widget_find_widget(surface->widget, x, y);
1555 if (widget)
1556 return widget;
1557 }
1558
1559 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001560}
1561
1562static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001563widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001564{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001565 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001566
Peter Huttererf3d62272013-08-08 11:57:05 +10001567 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001568 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001569 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001570 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001571 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001572 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001573 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001574 widget->tooltip = NULL;
1575 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001576 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001577 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001578
1579 return widget;
1580}
1581
1582struct widget *
1583window_add_widget(struct window *window, void *data)
1584{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001585 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001586
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001587 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001588 wl_list_init(&widget->link);
1589 window->main_surface->widget = widget;
1590
1591 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001592}
1593
1594struct widget *
1595widget_add_widget(struct widget *parent, void *data)
1596{
1597 struct widget *widget;
1598
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001599 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001600 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001601
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001602 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001603}
1604
1605void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001606widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001607{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001608 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001609 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001610 struct input *input;
1611
Pekka Paalanen35e82632013-04-25 13:57:48 +03001612 /* Destroy the sub-surface along with the root widget */
1613 if (surface->widget == widget && surface->subsurface)
1614 surface_destroy(widget->surface);
1615
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001616 if (widget->tooltip)
1617 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001618
Pekka Paalanene156fb62012-01-19 13:51:38 +02001619 wl_list_for_each(input, &display->input_list, link) {
1620 if (input->focus_widget == widget)
1621 input->focus_widget = NULL;
1622 }
1623
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001624 wl_list_remove(&widget->link);
1625 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001626}
1627
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001628void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001629widget_set_default_cursor(struct widget *widget, int cursor)
1630{
1631 widget->default_cursor = cursor;
1632}
1633
1634void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001635widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001636{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001637 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001638}
1639
1640void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001641widget_set_size(struct widget *widget, int32_t width, int32_t height)
1642{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001643 widget->allocation.width = width;
1644 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001645}
1646
1647void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001648widget_set_allocation(struct widget *widget,
1649 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001650{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001651 widget->allocation.x = x;
1652 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001653 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001654}
1655
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001656void
1657widget_set_transparent(struct widget *widget, int transparent)
1658{
1659 widget->opaque = !transparent;
1660}
1661
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001663widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001665 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001666}
1667
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001668static cairo_surface_t *
1669widget_get_cairo_surface(struct widget *widget)
1670{
1671 struct surface *surface = widget->surface;
1672 struct window *window = widget->window;
1673
Neil Roberts97b747c2013-12-19 16:17:12 +00001674 assert(widget->use_cairo);
1675
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001676 if (!surface->cairo_surface) {
1677 if (surface == window->main_surface)
1678 window_create_main_surface(window);
1679 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001680 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001681 }
1682
1683 return surface->cairo_surface;
1684}
1685
Alexander Larsson15901f02013-05-22 14:41:25 +02001686static void
1687widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1688{
1689 struct surface *surface = widget->surface;
1690 double angle;
1691 cairo_matrix_t m;
1692 enum wl_output_transform transform;
1693 int surface_width, surface_height;
1694 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001695 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001696
1697 surface_width = surface->allocation.width;
1698 surface_height = surface->allocation.height;
1699
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001700 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001701 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001702
Alexander Larsson15901f02013-05-22 14:41:25 +02001703 switch (transform) {
1704 case WL_OUTPUT_TRANSFORM_FLIPPED:
1705 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1706 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1707 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1708 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1709 break;
1710 default:
1711 cairo_matrix_init_identity(&m);
1712 break;
1713 }
1714
1715 switch (transform) {
1716 case WL_OUTPUT_TRANSFORM_NORMAL:
1717 default:
1718 angle = 0;
1719 translate_x = 0;
1720 translate_y = 0;
1721 break;
1722 case WL_OUTPUT_TRANSFORM_FLIPPED:
1723 angle = 0;
1724 translate_x = surface_width;
1725 translate_y = 0;
1726 break;
1727 case WL_OUTPUT_TRANSFORM_90:
1728 angle = M_PI_2;
1729 translate_x = surface_height;
1730 translate_y = 0;
1731 break;
1732 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1733 angle = M_PI_2;
1734 translate_x = surface_height;
1735 translate_y = surface_width;
1736 break;
1737 case WL_OUTPUT_TRANSFORM_180:
1738 angle = M_PI;
1739 translate_x = surface_width;
1740 translate_y = surface_height;
1741 break;
1742 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1743 angle = M_PI;
1744 translate_x = 0;
1745 translate_y = surface_height;
1746 break;
1747 case WL_OUTPUT_TRANSFORM_270:
1748 angle = M_PI + M_PI_2;
1749 translate_x = 0;
1750 translate_y = surface_width;
1751 break;
1752 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1753 angle = M_PI + M_PI_2;
1754 translate_x = 0;
1755 translate_y = 0;
1756 break;
1757 }
1758
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001759 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001760 cairo_translate(cr, translate_x, translate_y);
1761 cairo_rotate(cr, angle);
1762 cairo_transform(cr, &m);
1763}
1764
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001765cairo_t *
1766widget_cairo_create(struct widget *widget)
1767{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001768 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001769 cairo_surface_t *cairo_surface;
1770 cairo_t *cr;
1771
1772 cairo_surface = widget_get_cairo_surface(widget);
1773 cr = cairo_create(cairo_surface);
1774
Alexander Larsson15901f02013-05-22 14:41:25 +02001775 widget_cairo_update_transform(widget, cr);
1776
Pekka Paalanen35e82632013-04-25 13:57:48 +03001777 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1778
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001779 return cr;
1780}
1781
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001782struct wl_surface *
1783widget_get_wl_surface(struct widget *widget)
1784{
1785 return widget->surface->surface;
1786}
1787
Neil Roberts5e10a042013-09-09 00:40:17 +01001788struct wl_subsurface *
1789widget_get_wl_subsurface(struct widget *widget)
1790{
1791 return widget->surface->subsurface;
1792}
1793
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001794uint32_t
1795widget_get_last_time(struct widget *widget)
1796{
1797 return widget->surface->last_time;
1798}
1799
1800void
1801widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1802{
1803 struct wl_compositor *comp = widget->window->display->compositor;
1804 struct surface *surface = widget->surface;
1805
1806 if (!surface->input_region)
1807 surface->input_region = wl_compositor_create_region(comp);
1808
1809 if (rect) {
1810 wl_region_add(surface->input_region,
1811 rect->x, rect->y, rect->width, rect->height);
1812 }
1813}
1814
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001815void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001816widget_set_resize_handler(struct widget *widget,
1817 widget_resize_handler_t handler)
1818{
1819 widget->resize_handler = handler;
1820}
1821
1822void
1823widget_set_redraw_handler(struct widget *widget,
1824 widget_redraw_handler_t handler)
1825{
1826 widget->redraw_handler = handler;
1827}
1828
1829void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001830widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001831{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001832 widget->enter_handler = handler;
1833}
1834
1835void
1836widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1837{
1838 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001839}
1840
1841void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001842widget_set_motion_handler(struct widget *widget,
1843 widget_motion_handler_t handler)
1844{
1845 widget->motion_handler = handler;
1846}
1847
1848void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001849widget_set_button_handler(struct widget *widget,
1850 widget_button_handler_t handler)
1851{
1852 widget->button_handler = handler;
1853}
1854
1855void
Rusty Lynch041815a2013-08-08 21:20:38 -07001856widget_set_touch_up_handler(struct widget *widget,
1857 widget_touch_up_handler_t handler)
1858{
1859 widget->touch_up_handler = handler;
1860}
1861
1862void
1863widget_set_touch_down_handler(struct widget *widget,
1864 widget_touch_down_handler_t handler)
1865{
1866 widget->touch_down_handler = handler;
1867}
1868
1869void
1870widget_set_touch_motion_handler(struct widget *widget,
1871 widget_touch_motion_handler_t handler)
1872{
1873 widget->touch_motion_handler = handler;
1874}
1875
1876void
1877widget_set_touch_frame_handler(struct widget *widget,
1878 widget_touch_frame_handler_t handler)
1879{
1880 widget->touch_frame_handler = handler;
1881}
1882
1883void
1884widget_set_touch_cancel_handler(struct widget *widget,
1885 widget_touch_cancel_handler_t handler)
1886{
1887 widget->touch_cancel_handler = handler;
1888}
1889
1890void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001891widget_set_axis_handler(struct widget *widget,
1892 widget_axis_handler_t handler)
1893{
1894 widget->axis_handler = handler;
1895}
1896
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001897static void
1898window_schedule_redraw_task(struct window *window);
1899
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001900void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001901widget_schedule_redraw(struct widget *widget)
1902{
Pekka Paalanen71233882013-04-25 13:57:53 +03001903 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001904 widget->surface->redraw_needed = 1;
1905 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001906}
1907
Neil Roberts97b747c2013-12-19 16:17:12 +00001908void
1909widget_set_use_cairo(struct widget *widget,
1910 int use_cairo)
1911{
1912 widget->use_cairo = use_cairo;
1913}
1914
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001915cairo_surface_t *
1916window_get_surface(struct window *window)
1917{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001918 cairo_surface_t *cairo_surface;
1919
1920 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1921
1922 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001923}
1924
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001925struct wl_surface *
1926window_get_wl_surface(struct window *window)
1927{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001928 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001929}
1930
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001931static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001932tooltip_redraw_handler(struct widget *widget, void *data)
1933{
1934 cairo_t *cr;
1935 const int32_t r = 3;
1936 struct tooltip *tooltip = data;
1937 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001938
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001939 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001940 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1942 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1943 cairo_paint(cr);
1944
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001945 width = widget->allocation.width;
1946 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001947 rounded_rect(cr, 0, 0, width, height, r);
1948
1949 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1950 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1951 cairo_fill(cr);
1952
1953 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1954 cairo_move_to(cr, 10, 16);
1955 cairo_show_text(cr, tooltip->entry);
1956 cairo_destroy(cr);
1957}
1958
1959static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001960get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001961{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001962 cairo_t *cr;
1963 cairo_text_extents_t extents;
1964
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001965 /* Use the dummy_surface because tooltip's surface was not
1966 * created yet, and parent does not have a valid surface
1967 * outside repaint, either.
1968 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001969 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001970 cairo_text_extents(cr, tooltip->entry, &extents);
1971 cairo_destroy(cr);
1972
1973 return extents;
1974}
1975
1976static int
1977window_create_tooltip(struct tooltip *tooltip)
1978{
1979 struct widget *parent = tooltip->parent;
1980 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001981 const int offset_y = 27;
1982 const int margin = 3;
1983 cairo_text_extents_t extents;
1984
1985 if (tooltip->widget)
1986 return 0;
1987
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001988 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001989
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001990 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001991 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001992 widget_set_allocation(tooltip->widget,
1993 tooltip->x, tooltip->y + offset_y,
1994 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001995
1996 return 0;
1997}
1998
1999void
2000widget_destroy_tooltip(struct widget *parent)
2001{
2002 struct tooltip *tooltip = parent->tooltip;
2003
2004 parent->tooltip_count = 0;
2005 if (!tooltip)
2006 return;
2007
2008 if (tooltip->widget) {
2009 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002010 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002011 }
2012
2013 close(tooltip->tooltip_fd);
2014 free(tooltip->entry);
2015 free(tooltip);
2016 parent->tooltip = NULL;
2017}
2018
2019static void
2020tooltip_func(struct task *task, uint32_t events)
2021{
2022 struct tooltip *tooltip =
2023 container_of(task, struct tooltip, tooltip_task);
2024 uint64_t exp;
2025
Martin Olsson8df662a2012-07-08 03:03:47 +02002026 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2027 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002028 window_create_tooltip(tooltip);
2029}
2030
2031#define TOOLTIP_TIMEOUT 500
2032static int
2033tooltip_timer_reset(struct tooltip *tooltip)
2034{
2035 struct itimerspec its;
2036
2037 its.it_interval.tv_sec = 0;
2038 its.it_interval.tv_nsec = 0;
2039 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2040 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2041 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2042 fprintf(stderr, "could not set timerfd\n: %m");
2043 return -1;
2044 }
2045
2046 return 0;
2047}
2048
2049int
2050widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2051{
2052 struct tooltip *tooltip = parent->tooltip;
2053
2054 parent->tooltip_count++;
2055 if (tooltip) {
2056 tooltip->x = x;
2057 tooltip->y = y;
2058 tooltip_timer_reset(tooltip);
2059 return 0;
2060 }
2061
2062 /* the handler might be triggered too fast via input device motion, so
2063 * we need this check here to make sure tooltip is fully initialized */
2064 if (parent->tooltip_count > 1)
2065 return 0;
2066
2067 tooltip = malloc(sizeof *tooltip);
2068 if (!tooltip)
2069 return -1;
2070
2071 parent->tooltip = tooltip;
2072 tooltip->parent = parent;
2073 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002074 tooltip->x = x;
2075 tooltip->y = y;
2076 tooltip->entry = strdup(entry);
2077 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2078 if (tooltip->tooltip_fd < 0) {
2079 fprintf(stderr, "could not create timerfd\n: %m");
2080 return -1;
2081 }
2082
2083 tooltip->tooltip_task.run = tooltip_func;
2084 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2085 EPOLLIN, &tooltip->tooltip_task);
2086 tooltip_timer_reset(tooltip);
2087
2088 return 0;
2089}
2090
2091static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002092workspace_manager_state(void *data,
2093 struct workspace_manager *workspace_manager,
2094 uint32_t current,
2095 uint32_t count)
2096{
2097 struct display *display = data;
2098
2099 display->workspace = current;
2100 display->workspace_count = count;
2101}
2102
2103static const struct workspace_manager_listener workspace_manager_listener = {
2104 workspace_manager_state
2105};
2106
2107static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002108frame_resize_handler(struct widget *widget,
2109 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002110{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002111 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002112 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002113 struct rectangle interior;
2114 struct rectangle input;
2115 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002116
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002117 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002118 interior.x = 0;
2119 interior.y = 0;
2120 interior.width = width;
2121 interior.height = height;
2122 } else {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002123 if (widget->window->maximized) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002124 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2125 } else {
2126 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2127 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002128
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002129 frame_resize(frame->frame, width, height);
2130 frame_interior(frame->frame, &interior.x, &interior.y,
2131 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002132 }
2133
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002134 widget_set_allocation(child, interior.x, interior.y,
2135 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002136
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002137 if (child->resize_handler) {
2138 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139 child->user_data);
2140
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002141 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002142 width = child->allocation.width;
2143 height = child->allocation.height;
2144 } else {
2145 frame_resize_inside(frame->frame,
2146 child->allocation.width,
2147 child->allocation.height);
2148 width = frame_width(frame->frame);
2149 height = frame_height(frame->frame);
2150 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002151 }
2152
Scott Moreauf7e498c2012-05-14 11:39:29 -06002153 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002154
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002155 widget->surface->input_region =
2156 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002157 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002158 frame_input_rect(frame->frame, &input.x, &input.y,
2159 &input.width, &input.height);
2160 wl_region_add(widget->surface->input_region,
2161 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002162 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002163 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002164 }
2165
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002166 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002167
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002168 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002169 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002170 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2171 &opaque.width, &opaque.height);
2172
2173 wl_region_add(widget->surface->opaque_region,
2174 opaque.x, opaque.y,
2175 opaque.width, opaque.height);
2176 } else {
2177 wl_region_add(widget->surface->opaque_region,
2178 0, 0, width, height);
2179 }
Martin Minarik1998b152012-05-10 02:04:35 +02002180 }
2181
Martin Minarik1998b152012-05-10 02:04:35 +02002182
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002183 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002184}
2185
2186static void
2187frame_redraw_handler(struct widget *widget, void *data)
2188{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002189 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191 struct window *window = widget->window;
2192
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002193 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002194 return;
2195
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002196 if (window->focused) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2198 } else {
2199 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2200 }
2201
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002202 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002203
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002204 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002205
2206 cairo_destroy(cr);
2207}
2208
2209static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002210frame_get_pointer_image_for_location(struct window_frame *frame,
2211 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002212{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002213 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002214
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002215 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002216 return CURSOR_LEFT_PTR;
2217
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002218 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002219 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002220 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002221 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002222 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002223 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002224 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002225 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002226 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002227 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002228 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002229 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002230 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002231 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002232 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002233 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002234 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002235 case THEME_LOCATION_EXTERIOR:
2236 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002237 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002238 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002239 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002240}
2241
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002242static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002243frame_menu_func(struct window *window,
2244 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002245{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002246 struct display *display;
2247
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002248 switch (index) {
2249 case 0: /* close */
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002250 window_close(window);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002251 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002252 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002253 display = window->display;
2254 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002255 workspace_manager_move_surface(
2256 display->workspace_manager,
2257 window->main_surface->surface,
2258 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002259 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002260 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002261 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002262 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002263 workspace_manager_move_surface(
2264 display->workspace_manager,
2265 window->main_surface->surface,
2266 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002267 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002268 case 3: /* fullscreen */
2269 /* we don't have a way to get out of fullscreen for now */
2270 if (window->fullscreen_handler)
2271 window->fullscreen_handler(window, window->user_data);
2272 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002273 }
2274}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002275
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002276void
2277window_show_frame_menu(struct window *window,
2278 struct input *input, uint32_t time)
2279{
2280 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002281 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002282
2283 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002284 "Close",
2285 "Move to workspace above", "Move to workspace below",
2286 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002287 };
2288
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002289 if (window->fullscreen_handler)
2290 count = ARRAY_LENGTH(entries);
2291 else
2292 count = ARRAY_LENGTH(entries) - 1;
2293
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002294 input_get_position(input, &x, &y);
2295 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002296 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002297}
2298
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002299static int
2300frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002301 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002302{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002303 struct window_frame *frame = data;
2304 enum theme_location location;
2305
2306 location = frame_pointer_enter(frame->frame, input, x, y);
2307 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2308 widget_schedule_redraw(frame->widget);
2309
2310 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002311}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002312
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002313static int
2314frame_motion_handler(struct widget *widget,
2315 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002316 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002317{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002318 struct window_frame *frame = data;
2319 enum theme_location location;
2320
2321 location = frame_pointer_motion(frame->frame, input, x, y);
2322 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2323 widget_schedule_redraw(frame->widget);
2324
2325 return frame_get_pointer_image_for_location(data, location);
2326}
2327
2328static void
2329frame_leave_handler(struct widget *widget,
2330 struct input *input, void *data)
2331{
2332 struct window_frame *frame = data;
2333
2334 frame_pointer_leave(frame->frame, input);
2335 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2336 widget_schedule_redraw(frame->widget);
2337}
2338
2339static void
2340frame_handle_status(struct window_frame *frame, struct input *input,
2341 uint32_t time, enum theme_location location)
2342{
2343 struct window *window = frame->widget->window;
2344 uint32_t status;
2345
2346 status = frame_status(frame->frame);
2347 if (status & FRAME_STATUS_REPAINT)
2348 widget_schedule_redraw(frame->widget);
2349
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002350 if (status & FRAME_STATUS_MINIMIZE) {
2351 window_set_minimized(window);
2352 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2353 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002354
2355 if (status & FRAME_STATUS_MENU) {
2356 window_show_frame_menu(window, input, time);
2357 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2358 }
2359
2360 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002361 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002362 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2363 }
2364
2365 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002366 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002367 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002368 }
2369
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002370 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002371 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002372 xdg_surface_move(window->xdg_surface,
2373 input_get_seat(input),
2374 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002375
2376 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2377 }
2378
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002379 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002380 input_ungrab(input);
2381
2382 window->resizing = 1;
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002383 xdg_surface_resize(window->xdg_surface,
2384 input_get_seat(input),
2385 window->display->serial,
2386 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002387
2388 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2389 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002390}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002391
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002392static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002393frame_button_handler(struct widget *widget,
2394 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002395 uint32_t button, enum wl_pointer_button_state state,
2396 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002397
2398{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002399 struct window_frame *frame = data;
2400 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002401
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002402 location = frame_pointer_button(frame->frame, input, button, state);
2403 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002404}
2405
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002406static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002407frame_touch_down_handler(struct widget *widget, struct input *input,
2408 uint32_t serial, uint32_t time, int32_t id,
2409 float x, float y, void *data)
2410{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002411 struct window_frame *frame = data;
2412
2413 frame_touch_down(frame->frame, input, id, x, y);
2414 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2415}
2416
2417static void
2418frame_touch_up_handler(struct widget *widget,
2419 struct input *input, uint32_t serial, uint32_t time,
2420 int32_t id, void *data)
2421{
2422 struct window_frame *frame = data;
2423
2424 frame_touch_up(frame->frame, input, id);
2425 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002426}
2427
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002428struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002429window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002430{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002431 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002432 uint32_t buttons;
2433
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002434 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002435 buttons = FRAME_BUTTON_NONE;
2436 } else {
2437 buttons = FRAME_BUTTON_ALL;
2438 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002439
Peter Huttererf3d62272013-08-08 11:57:05 +10002440 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002441 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002442 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002443
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002444 frame->widget = window_add_widget(window, frame);
2445 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002446
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002447 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2448 widget_set_resize_handler(frame->widget, frame_resize_handler);
2449 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002450 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002451 widget_set_motion_handler(frame->widget, frame_motion_handler);
2452 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002453 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002454 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002455
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002456 window->frame = frame;
2457
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002458 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002459}
2460
Kristian Høgsberga1627922012-06-20 17:30:03 -04002461void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002462window_frame_set_child_size(struct widget *widget, int child_width,
2463 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002464{
2465 struct display *display = widget->window->display;
2466 struct theme *t = display->theme;
2467 int decoration_width, decoration_height;
2468 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002469 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002470
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002471 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002472 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002473 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002474 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002475
2476 width = child_width + decoration_width;
2477 height = child_height + decoration_height;
2478 } else {
2479 width = child_width;
2480 height = child_height;
2481 }
2482
2483 window_schedule_resize(widget->window, width, height);
2484}
2485
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002486static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002487window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002488{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002489 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002490
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002491 /* frame->child must be destroyed by the application */
2492 widget_destroy(frame->widget);
2493 free(frame);
2494}
2495
2496static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002497input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002498 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002499{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002500 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002501 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002502
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002503 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002504 return;
2505
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002506 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002507 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002508 widget = old;
2509 if (input->grab)
2510 widget = input->grab;
2511 if (widget->leave_handler)
2512 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002513 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002514 }
2515
2516 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002517 widget = focus;
2518 if (input->grab)
2519 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002520 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002521 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002522 cursor = widget->enter_handler(focus, input, x, y,
2523 widget->user_data);
2524 else
2525 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002526
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002527 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002528 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002529}
2530
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002531void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002532touch_grab(struct input *input, int32_t touch_id)
2533{
2534 input->touch_grab = 1;
2535 input->touch_grab_id = touch_id;
2536}
2537
2538void
2539touch_ungrab(struct input *input)
2540{
2541 struct touch_point *tp, *tmp;
2542
2543 input->touch_grab = 0;
2544
2545 wl_list_for_each_safe(tp, tmp,
2546 &input->touch_point_list, link) {
2547 if (tp->id != input->touch_grab_id)
2548 continue;
2549 wl_list_remove(&tp->link);
2550 free(tp);
2551
2552 return;
2553 }
2554}
2555
2556void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002557input_grab(struct input *input, struct widget *widget, uint32_t button)
2558{
2559 input->grab = widget;
2560 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002561
2562 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002563}
2564
2565void
2566input_ungrab(struct input *input)
2567{
2568 struct widget *widget;
2569
2570 input->grab = NULL;
2571 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002572 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002573 input->sx, input->sy);
2574 input_set_focus_widget(input, widget, input->sx, input->sy);
2575 }
2576}
2577
2578static void
2579input_remove_pointer_focus(struct input *input)
2580{
2581 struct window *window = input->pointer_focus;
2582
2583 if (!window)
2584 return;
2585
2586 input_set_focus_widget(input, NULL, 0, 0);
2587
2588 input->pointer_focus = NULL;
2589 input->current_cursor = CURSOR_UNSET;
2590}
2591
2592static void
2593pointer_handle_enter(void *data, struct wl_pointer *pointer,
2594 uint32_t serial, struct wl_surface *surface,
2595 wl_fixed_t sx_w, wl_fixed_t sy_w)
2596{
2597 struct input *input = data;
2598 struct window *window;
2599 struct widget *widget;
2600 float sx = wl_fixed_to_double(sx_w);
2601 float sy = wl_fixed_to_double(sy_w);
2602
2603 if (!surface) {
2604 /* enter event for a window we've just destroyed */
2605 return;
2606 }
2607
2608 input->display->serial = serial;
2609 input->pointer_enter_serial = serial;
2610 input->pointer_focus = wl_surface_get_user_data(surface);
2611 window = input->pointer_focus;
2612
Pekka Paalanen99436862012-11-19 17:15:59 +02002613 if (window->resizing) {
2614 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002615 /* Schedule a redraw to free the pool */
2616 window_schedule_redraw(window);
2617 }
2618
2619 input->sx = sx;
2620 input->sy = sy;
2621
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002622 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002623 input_set_focus_widget(input, widget, sx, sy);
2624}
2625
2626static void
2627pointer_handle_leave(void *data, struct wl_pointer *pointer,
2628 uint32_t serial, struct wl_surface *surface)
2629{
2630 struct input *input = data;
2631
2632 input->display->serial = serial;
2633 input_remove_pointer_focus(input);
2634}
2635
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002636static void
Daniel Stone37816df2012-05-16 18:45:18 +01002637pointer_handle_motion(void *data, struct wl_pointer *pointer,
2638 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002639{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002640 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002641 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002642 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002643 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002644 float sx = wl_fixed_to_double(sx_w);
2645 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002646
Paul Winwoodb22bf572013-08-29 10:52:54 +01002647 input->sx = sx;
2648 input->sy = sy;
2649
2650 if (!window)
2651 return;
2652
Rob Bradford5f087742013-07-11 19:41:27 +01002653 /* when making the window smaller - e.g. after a unmaximise we might
2654 * still have a pending motion event that the compositor has picked
2655 * based on the old surface dimensions
2656 */
2657 if (sx > window->main_surface->allocation.width ||
2658 sy > window->main_surface->allocation.height)
2659 return;
2660
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002661 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002662 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002663 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002664 }
2665
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002666 if (input->grab)
2667 widget = input->grab;
2668 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002669 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002670 if (widget) {
2671 if (widget->motion_handler)
2672 cursor = widget->motion_handler(input->focus_widget,
2673 input, time, sx, sy,
2674 widget->user_data);
2675 else
2676 cursor = widget->default_cursor;
2677 } else
2678 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002679
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002680 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002681}
2682
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002683static void
Daniel Stone37816df2012-05-16 18:45:18 +01002684pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002685 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002686{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002687 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002688 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002689 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002690
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002691 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002692 if (input->focus_widget && input->grab == NULL &&
2693 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002694 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002695
Neil Roberts6b28aad2012-01-23 19:11:18 +00002696 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002697 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002698 (*widget->button_handler)(widget,
2699 input, time,
2700 button, state,
2701 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002702
Daniel Stone4dbadb12012-05-30 16:31:51 +01002703 if (input->grab && input->grab_button == button &&
2704 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002705 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002706}
2707
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002708static void
Daniel Stone37816df2012-05-16 18:45:18 +01002709pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002710 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002711{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002712 struct input *input = data;
2713 struct widget *widget;
2714
2715 widget = input->focus_widget;
2716 if (input->grab)
2717 widget = input->grab;
2718 if (widget && widget->axis_handler)
2719 (*widget->axis_handler)(widget,
2720 input, time,
2721 axis, value,
2722 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002723}
2724
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002725static const struct wl_pointer_listener pointer_listener = {
2726 pointer_handle_enter,
2727 pointer_handle_leave,
2728 pointer_handle_motion,
2729 pointer_handle_button,
2730 pointer_handle_axis,
2731};
2732
2733static void
2734input_remove_keyboard_focus(struct input *input)
2735{
2736 struct window *window = input->keyboard_focus;
2737 struct itimerspec its;
2738
2739 its.it_interval.tv_sec = 0;
2740 its.it_interval.tv_nsec = 0;
2741 its.it_value.tv_sec = 0;
2742 its.it_value.tv_nsec = 0;
2743 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2744
2745 if (!window)
2746 return;
2747
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002748 if (window->keyboard_focus_handler)
2749 (*window->keyboard_focus_handler)(window, NULL,
2750 window->user_data);
2751
2752 input->keyboard_focus = NULL;
2753}
2754
2755static void
2756keyboard_repeat_func(struct task *task, uint32_t events)
2757{
2758 struct input *input =
2759 container_of(task, struct input, repeat_task);
2760 struct window *window = input->keyboard_focus;
2761 uint64_t exp;
2762
2763 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2764 /* If we change the timer between the fd becoming
2765 * readable and getting here, there'll be nothing to
2766 * read and we get EAGAIN. */
2767 return;
2768
2769 if (window && window->key_handler) {
2770 (*window->key_handler)(window, input, input->repeat_time,
2771 input->repeat_key, input->repeat_sym,
2772 WL_KEYBOARD_KEY_STATE_PRESSED,
2773 window->user_data);
2774 }
2775}
2776
2777static void
2778keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2779 uint32_t format, int fd, uint32_t size)
2780{
2781 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002782 struct xkb_keymap *keymap;
2783 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002784 char *map_str;
2785
2786 if (!data) {
2787 close(fd);
2788 return;
2789 }
2790
2791 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2792 close(fd);
2793 return;
2794 }
2795
2796 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2797 if (map_str == MAP_FAILED) {
2798 close(fd);
2799 return;
2800 }
2801
Rui Matos3eccb862013-10-10 19:44:22 +02002802 keymap = xkb_map_new_from_string(input->display->xkb_context,
2803 map_str,
2804 XKB_KEYMAP_FORMAT_TEXT_V1,
2805 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002806 munmap(map_str, size);
2807 close(fd);
2808
Rui Matos3eccb862013-10-10 19:44:22 +02002809 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002810 fprintf(stderr, "failed to compile keymap\n");
2811 return;
2812 }
2813
Rui Matos3eccb862013-10-10 19:44:22 +02002814 state = xkb_state_new(keymap);
2815 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002816 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002817 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002818 return;
2819 }
2820
Rui Matos3eccb862013-10-10 19:44:22 +02002821 xkb_keymap_unref(input->xkb.keymap);
2822 xkb_state_unref(input->xkb.state);
2823 input->xkb.keymap = keymap;
2824 input->xkb.state = state;
2825
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002826 input->xkb.control_mask =
2827 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2828 input->xkb.alt_mask =
2829 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2830 input->xkb.shift_mask =
2831 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2832}
2833
2834static void
2835keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2836 uint32_t serial, struct wl_surface *surface,
2837 struct wl_array *keys)
2838{
2839 struct input *input = data;
2840 struct window *window;
2841
2842 input->display->serial = serial;
2843 input->keyboard_focus = wl_surface_get_user_data(surface);
2844
2845 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002846 if (window->keyboard_focus_handler)
2847 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002848 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002849}
2850
2851static void
2852keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2853 uint32_t serial, struct wl_surface *surface)
2854{
2855 struct input *input = data;
2856
2857 input->display->serial = serial;
2858 input_remove_keyboard_focus(input);
2859}
2860
Scott Moreau210d0792012-03-22 10:47:01 -06002861static void
Daniel Stone37816df2012-05-16 18:45:18 +01002862keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002863 uint32_t serial, uint32_t time, uint32_t key,
2864 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002865{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002866 struct input *input = data;
2867 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002868 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002869 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002870 const xkb_keysym_t *syms;
2871 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002872 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002873
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002874 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002875 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002876 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002877 return;
2878
Daniel Stone97f68542012-05-30 16:32:01 +01002879 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002880
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002881 sym = XKB_KEY_NoSymbol;
2882 if (num_syms == 1)
2883 sym = syms[0];
2884
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002885
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002886 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002887 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002888 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002889 } else if (sym == XKB_KEY_F11 &&
2890 window->fullscreen_handler &&
2891 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2892 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002893 } else if (sym == XKB_KEY_F4 &&
2894 input->modifiers == MOD_ALT_MASK &&
2895 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002896 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002897 } else if (window->key_handler) {
2898 (*window->key_handler)(window, input, time, key,
2899 sym, state, window->user_data);
2900 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002901
2902 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2903 key == input->repeat_key) {
2904 its.it_interval.tv_sec = 0;
2905 its.it_interval.tv_nsec = 0;
2906 its.it_value.tv_sec = 0;
2907 its.it_value.tv_nsec = 0;
2908 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002909 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2910 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002911 input->repeat_sym = sym;
2912 input->repeat_key = key;
2913 input->repeat_time = time;
2914 its.it_interval.tv_sec = 0;
2915 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2916 its.it_value.tv_sec = 0;
2917 its.it_value.tv_nsec = 400 * 1000 * 1000;
2918 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2919 }
2920}
2921
2922static void
Daniel Stone351eb612012-05-31 15:27:47 -04002923keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2924 uint32_t serial, uint32_t mods_depressed,
2925 uint32_t mods_latched, uint32_t mods_locked,
2926 uint32_t group)
2927{
2928 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002929 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002930
Matt Ropere61561f2013-06-24 16:52:43 +01002931 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2932 if (!input->xkb.keymap)
2933 return;
2934
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002935 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2936 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002937 mask = xkb_state_serialize_mods(input->xkb.state,
2938 XKB_STATE_DEPRESSED |
2939 XKB_STATE_LATCHED);
2940 input->modifiers = 0;
2941 if (mask & input->xkb.control_mask)
2942 input->modifiers |= MOD_CONTROL_MASK;
2943 if (mask & input->xkb.alt_mask)
2944 input->modifiers |= MOD_ALT_MASK;
2945 if (mask & input->xkb.shift_mask)
2946 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002947}
2948
Daniel Stone37816df2012-05-16 18:45:18 +01002949static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002950 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002951 keyboard_handle_enter,
2952 keyboard_handle_leave,
2953 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002954 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002955};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002956
2957static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002958touch_handle_down(void *data, struct wl_touch *wl_touch,
2959 uint32_t serial, uint32_t time, struct wl_surface *surface,
2960 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2961{
2962 struct input *input = data;
2963 struct widget *widget;
2964 float sx = wl_fixed_to_double(x_w);
2965 float sy = wl_fixed_to_double(y_w);
2966
Rusty Lynch1084da52013-08-15 09:10:08 -07002967 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002968 input->touch_focus = wl_surface_get_user_data(surface);
2969 if (!input->touch_focus) {
2970 DBG("Failed to find to touch focus for surface %p\n", surface);
2971 return;
2972 }
2973
2974 widget = window_find_widget(input->touch_focus,
2975 wl_fixed_to_double(x_w),
2976 wl_fixed_to_double(y_w));
2977 if (widget) {
2978 struct touch_point *tp = xmalloc(sizeof *tp);
2979 if (tp) {
2980 tp->id = id;
2981 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08002982 tp->x = sx;
2983 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07002984 wl_list_insert(&input->touch_point_list, &tp->link);
2985
2986 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07002987 (*widget->touch_down_handler)(widget, input,
2988 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002989 sx, sy,
2990 widget->user_data);
2991 }
2992 }
2993}
2994
2995static void
2996touch_handle_up(void *data, struct wl_touch *wl_touch,
2997 uint32_t serial, uint32_t time, int32_t id)
2998{
2999 struct input *input = data;
3000 struct touch_point *tp, *tmp;
3001
Rusty Lynch041815a2013-08-08 21:20:38 -07003002 if (!input->touch_focus) {
3003 DBG("No touch focus found for touch up event!\n");
3004 return;
3005 }
3006
3007 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3008 if (tp->id != id)
3009 continue;
3010
3011 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003012 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003013 time, id,
3014 tp->widget->user_data);
3015
3016 wl_list_remove(&tp->link);
3017 free(tp);
3018
3019 return;
3020 }
3021}
3022
3023static void
3024touch_handle_motion(void *data, struct wl_touch *wl_touch,
3025 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3026{
3027 struct input *input = data;
3028 struct touch_point *tp;
3029 float sx = wl_fixed_to_double(x_w);
3030 float sy = wl_fixed_to_double(y_w);
3031
3032 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3033
3034 if (!input->touch_focus) {
3035 DBG("No touch focus found for touch motion event!\n");
3036 return;
3037 }
3038
3039 wl_list_for_each(tp, &input->touch_point_list, link) {
3040 if (tp->id != id)
3041 continue;
3042
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003043 tp->x = sx;
3044 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003045 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003046 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003047 id, sx, sy,
3048 tp->widget->user_data);
3049 return;
3050 }
3051}
3052
3053static void
3054touch_handle_frame(void *data, struct wl_touch *wl_touch)
3055{
3056 struct input *input = data;
3057 struct touch_point *tp, *tmp;
3058
3059 DBG("touch_handle_frame\n");
3060
3061 if (!input->touch_focus) {
3062 DBG("No touch focus found for touch frame event!\n");
3063 return;
3064 }
3065
3066 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3067 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003068 (*tp->widget->touch_frame_handler)(tp->widget, input,
3069 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003070 }
3071}
3072
3073static void
3074touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3075{
3076 struct input *input = data;
3077 struct touch_point *tp, *tmp;
3078
3079 DBG("touch_handle_cancel\n");
3080
3081 if (!input->touch_focus) {
3082 DBG("No touch focus found for touch cancel event!\n");
3083 return;
3084 }
3085
3086 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3087 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003088 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3089 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003090
3091 wl_list_remove(&tp->link);
3092 free(tp);
3093 }
3094}
3095
3096static const struct wl_touch_listener touch_listener = {
3097 touch_handle_down,
3098 touch_handle_up,
3099 touch_handle_motion,
3100 touch_handle_frame,
3101 touch_handle_cancel,
3102};
3103
3104static void
Daniel Stone37816df2012-05-16 18:45:18 +01003105seat_handle_capabilities(void *data, struct wl_seat *seat,
3106 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003107{
Daniel Stone37816df2012-05-16 18:45:18 +01003108 struct input *input = data;
3109
3110 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3111 input->pointer = wl_seat_get_pointer(seat);
3112 wl_pointer_set_user_data(input->pointer, input);
3113 wl_pointer_add_listener(input->pointer, &pointer_listener,
3114 input);
3115 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3116 wl_pointer_destroy(input->pointer);
3117 input->pointer = NULL;
3118 }
3119
3120 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3121 input->keyboard = wl_seat_get_keyboard(seat);
3122 wl_keyboard_set_user_data(input->keyboard, input);
3123 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3124 input);
3125 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3126 wl_keyboard_destroy(input->keyboard);
3127 input->keyboard = NULL;
3128 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003129
3130 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3131 input->touch = wl_seat_get_touch(seat);
3132 wl_touch_set_user_data(input->touch, input);
3133 wl_touch_add_listener(input->touch, &touch_listener, input);
3134 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3135 wl_touch_destroy(input->touch);
3136 input->touch = NULL;
3137 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003138}
3139
Rob Bradford08031182013-08-13 20:11:03 +01003140static void
3141seat_handle_name(void *data, struct wl_seat *seat,
3142 const char *name)
3143{
3144
3145}
3146
Daniel Stone37816df2012-05-16 18:45:18 +01003147static const struct wl_seat_listener seat_listener = {
3148 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003149 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003150};
3151
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003152void
3153input_get_position(struct input *input, int32_t *x, int32_t *y)
3154{
3155 *x = input->sx;
3156 *y = input->sy;
3157}
3158
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003159int
3160input_get_touch(struct input *input, int32_t id, float *x, float *y)
3161{
3162 struct touch_point *tp;
3163
3164 wl_list_for_each(tp, &input->touch_point_list, link) {
3165 if (tp->id != id)
3166 continue;
3167
3168 *x = tp->x;
3169 *y = tp->y;
3170 return 0;
3171 }
3172
3173 return -1;
3174}
3175
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003176struct display *
3177input_get_display(struct input *input)
3178{
3179 return input->display;
3180}
3181
Daniel Stone37816df2012-05-16 18:45:18 +01003182struct wl_seat *
3183input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003184{
Daniel Stone37816df2012-05-16 18:45:18 +01003185 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003186}
3187
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003188uint32_t
3189input_get_modifiers(struct input *input)
3190{
3191 return input->modifiers;
3192}
3193
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003194struct widget *
3195input_get_focus_widget(struct input *input)
3196{
3197 return input->focus_widget;
3198}
3199
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003200struct data_offer {
3201 struct wl_data_offer *offer;
3202 struct input *input;
3203 struct wl_array types;
3204 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003205
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003206 struct task io_task;
3207 int fd;
3208 data_func_t func;
3209 int32_t x, y;
3210 void *user_data;
3211};
3212
3213static void
3214data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3215{
3216 struct data_offer *offer = data;
3217 char **p;
3218
3219 p = wl_array_add(&offer->types, sizeof *p);
3220 *p = strdup(type);
3221}
3222
3223static const struct wl_data_offer_listener data_offer_listener = {
3224 data_offer_offer,
3225};
3226
3227static void
3228data_offer_destroy(struct data_offer *offer)
3229{
3230 char **p;
3231
3232 offer->refcount--;
3233 if (offer->refcount == 0) {
3234 wl_data_offer_destroy(offer->offer);
3235 for (p = offer->types.data; *p; p++)
3236 free(*p);
3237 wl_array_release(&offer->types);
3238 free(offer);
3239 }
3240}
3241
3242static void
3243data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003244 struct wl_data_device *data_device,
3245 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003246{
3247 struct data_offer *offer;
3248
Brian Lovinbc919262013-08-07 15:34:59 -07003249 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003250
3251 wl_array_init(&offer->types);
3252 offer->refcount = 1;
3253 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003254 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003255 wl_data_offer_add_listener(offer->offer,
3256 &data_offer_listener, offer);
3257}
3258
3259static void
3260data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003261 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003262 wl_fixed_t x_w, wl_fixed_t y_w,
3263 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003264{
3265 struct input *input = data;
3266 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003267 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003268 float x = wl_fixed_to_double(x_w);
3269 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003270 char **p;
3271
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003272 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003273 input->drag_enter_serial = serial;
3274 input->drag_focus = window,
3275 input->drag_x = x;
3276 input->drag_y = y;
3277
3278 if (!input->touch_grab)
3279 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003280
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003281 if (offer) {
3282 input->drag_offer = wl_data_offer_get_user_data(offer);
3283
3284 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3285 *p = NULL;
3286
3287 types_data = input->drag_offer->types.data;
3288 } else {
3289 input->drag_offer = NULL;
3290 types_data = NULL;
3291 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003292
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003293 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003294 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003295 window->user_data);
3296}
3297
3298static void
3299data_device_leave(void *data, struct wl_data_device *data_device)
3300{
3301 struct input *input = data;
3302
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003303 if (input->drag_offer) {
3304 data_offer_destroy(input->drag_offer);
3305 input->drag_offer = NULL;
3306 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003307}
3308
3309static void
3310data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003311 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003312{
3313 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003314 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003315 float x = wl_fixed_to_double(x_w);
3316 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003317 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003318
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003319 input->drag_x = x;
3320 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003321
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003322 if (input->drag_offer)
3323 types_data = input->drag_offer->types.data;
3324 else
3325 types_data = NULL;
3326
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003327 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003328 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003329 window->user_data);
3330}
3331
3332static void
3333data_device_drop(void *data, struct wl_data_device *data_device)
3334{
3335 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003336 struct window *window = input->drag_focus;
3337 float x, y;
3338
3339 x = input->drag_x;
3340 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003341
3342 if (window->drop_handler)
3343 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003344 x, y, window->user_data);
3345
3346 if (input->touch_grab)
3347 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003348}
3349
3350static void
3351data_device_selection(void *data,
3352 struct wl_data_device *wl_data_device,
3353 struct wl_data_offer *offer)
3354{
3355 struct input *input = data;
3356 char **p;
3357
3358 if (input->selection_offer)
3359 data_offer_destroy(input->selection_offer);
3360
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003361 if (offer) {
3362 input->selection_offer = wl_data_offer_get_user_data(offer);
3363 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3364 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003365 } else {
3366 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003367 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003368}
3369
3370static const struct wl_data_device_listener data_device_listener = {
3371 data_device_data_offer,
3372 data_device_enter,
3373 data_device_leave,
3374 data_device_motion,
3375 data_device_drop,
3376 data_device_selection
3377};
3378
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003379static void
3380input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003381{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003382 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003383 struct wl_cursor *cursor;
3384 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003385
Daniel Stone80972742012-11-07 17:51:39 +11003386 if (!input->pointer)
3387 return;
3388
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003389 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003390 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003391 return;
3392
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003393 if (index >= (int) cursor->image_count) {
3394 fprintf(stderr, "cursor index out of range\n");
3395 return;
3396 }
3397
3398 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003399 buffer = wl_cursor_image_get_buffer(image);
3400 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003401 return;
3402
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003403 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3404 wl_surface_damage(input->pointer_surface, 0, 0,
3405 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003406 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003407 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3408 input->pointer_surface,
3409 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003410}
3411
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003412static const struct wl_callback_listener pointer_surface_listener;
3413
3414static void
3415pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3416 uint32_t time)
3417{
3418 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003419 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003420 int i;
3421
3422 if (callback) {
3423 assert(callback == input->cursor_frame_cb);
3424 wl_callback_destroy(callback);
3425 input->cursor_frame_cb = NULL;
3426 }
3427
Daniel Stone80972742012-11-07 17:51:39 +11003428 if (!input->pointer)
3429 return;
3430
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003431 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003432 wl_pointer_set_cursor(input->pointer,
3433 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003434 NULL, 0, 0);
3435 return;
3436 }
3437
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003438 if (input->current_cursor == CURSOR_UNSET)
3439 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003440 cursor = input->display->cursors[input->current_cursor];
3441 if (!cursor)
3442 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003443
3444 /* FIXME We don't have the current time on the first call so we set
3445 * the animation start to the time of the first frame callback. */
3446 if (time == 0)
3447 input->cursor_anim_start = 0;
3448 else if (input->cursor_anim_start == 0)
3449 input->cursor_anim_start = time;
3450
3451 if (time == 0 || input->cursor_anim_start == 0)
3452 i = 0;
3453 else
3454 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3455
Pekka Paalanenbc106382012-10-10 12:49:31 +03003456 if (cursor->image_count > 1) {
3457 input->cursor_frame_cb =
3458 wl_surface_frame(input->pointer_surface);
3459 wl_callback_add_listener(input->cursor_frame_cb,
3460 &pointer_surface_listener, input);
3461 }
3462
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003463 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003464}
3465
3466static const struct wl_callback_listener pointer_surface_listener = {
3467 pointer_surface_frame_callback
3468};
3469
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003470void
3471input_set_pointer_image(struct input *input, int pointer)
3472{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003473 int force = 0;
3474
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003475 if (!input->pointer)
3476 return;
3477
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003478 if (input->pointer_enter_serial > input->cursor_serial)
3479 force = 1;
3480
3481 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003482 return;
3483
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003484 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003485 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003486 if (!input->cursor_frame_cb)
3487 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003488 else if (force) {
3489 /* The current frame callback may be stuck if, for instance,
3490 * the set cursor request was processed by the server after
3491 * this client lost the focus. In this case the cursor surface
3492 * might not be mapped and the frame callback wouldn't ever
3493 * complete. Send a set_cursor and attach to try to map the
3494 * cursor surface again so that the callback will finish */
3495 input_set_pointer_image_index(input, 0);
3496 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003497}
3498
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003499struct wl_data_device *
3500input_get_data_device(struct input *input)
3501{
3502 return input->data_device;
3503}
3504
3505void
3506input_set_selection(struct input *input,
3507 struct wl_data_source *source, uint32_t time)
3508{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003509 if (input->data_device)
3510 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003511}
3512
3513void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003514input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003515{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003516 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003517 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003518}
3519
3520static void
3521offer_io_func(struct task *task, uint32_t events)
3522{
3523 struct data_offer *offer =
3524 container_of(task, struct data_offer, io_task);
3525 unsigned int len;
3526 char buffer[4096];
3527
3528 len = read(offer->fd, buffer, sizeof buffer);
3529 offer->func(buffer, len,
3530 offer->x, offer->y, offer->user_data);
3531
3532 if (len == 0) {
3533 close(offer->fd);
3534 data_offer_destroy(offer);
3535 }
3536}
3537
3538static void
3539data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3540 data_func_t func, void *user_data)
3541{
3542 int p[2];
3543
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003544 if (pipe2(p, O_CLOEXEC) == -1)
3545 return;
3546
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003547 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3548 close(p[1]);
3549
3550 offer->io_task.run = offer_io_func;
3551 offer->fd = p[0];
3552 offer->func = func;
3553 offer->refcount++;
3554 offer->user_data = user_data;
3555
3556 display_watch_fd(offer->input->display,
3557 offer->fd, EPOLLIN, &offer->io_task);
3558}
3559
3560void
3561input_receive_drag_data(struct input *input, const char *mime_type,
3562 data_func_t func, void *data)
3563{
3564 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003565 input->drag_offer->x = input->drag_x;
3566 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003567}
3568
3569int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003570input_receive_drag_data_to_fd(struct input *input,
3571 const char *mime_type, int fd)
3572{
3573 if (input->drag_offer)
3574 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3575
3576 return 0;
3577}
3578
3579int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003580input_receive_selection_data(struct input *input, const char *mime_type,
3581 data_func_t func, void *data)
3582{
3583 char **p;
3584
3585 if (input->selection_offer == NULL)
3586 return -1;
3587
3588 for (p = input->selection_offer->types.data; *p; p++)
3589 if (strcmp(mime_type, *p) == 0)
3590 break;
3591
3592 if (*p == NULL)
3593 return -1;
3594
3595 data_offer_receive_data(input->selection_offer,
3596 mime_type, func, data);
3597 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003598}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003599
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003600int
3601input_receive_selection_data_to_fd(struct input *input,
3602 const char *mime_type, int fd)
3603{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003604 if (input->selection_offer)
3605 wl_data_offer_receive(input->selection_offer->offer,
3606 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003607
3608 return 0;
3609}
3610
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003611void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003612window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003613{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003614 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003615 return;
3616
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003617 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003618}
3619
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003620static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003621surface_set_synchronized(struct surface *surface)
3622{
3623 if (!surface->subsurface)
3624 return;
3625
3626 if (surface->synchronized)
3627 return;
3628
3629 wl_subsurface_set_sync(surface->subsurface);
3630 surface->synchronized = 1;
3631}
3632
3633static void
3634surface_set_synchronized_default(struct surface *surface)
3635{
3636 if (!surface->subsurface)
3637 return;
3638
3639 if (surface->synchronized == surface->synchronized_default)
3640 return;
3641
3642 if (surface->synchronized_default)
3643 wl_subsurface_set_sync(surface->subsurface);
3644 else
3645 wl_subsurface_set_desync(surface->subsurface);
3646
3647 surface->synchronized = surface->synchronized_default;
3648}
3649
3650static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003651surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003652{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003653 struct widget *widget = surface->widget;
3654 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003655
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003656 if (surface->input_region) {
3657 wl_region_destroy(surface->input_region);
3658 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003659 }
3660
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003661 if (surface->opaque_region)
3662 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003663
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003664 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003665
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003666 if (widget->resize_handler)
3667 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003668 widget->allocation.width,
3669 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003670 widget->user_data);
3671
Pekka Paalanen35e82632013-04-25 13:57:48 +03003672 if (surface->subsurface &&
3673 (surface->allocation.x != widget->allocation.x ||
3674 surface->allocation.y != widget->allocation.y)) {
3675 wl_subsurface_set_position(surface->subsurface,
3676 widget->allocation.x,
3677 widget->allocation.y);
3678 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003679 if (surface->allocation.width != widget->allocation.width ||
3680 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003681 window_schedule_redraw(widget->window);
3682 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003683 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003684
3685 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003686 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003687 widget->allocation.width,
3688 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003689}
3690
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003691static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003692hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3693{
3694 /*
3695 * This hack should be removed, when EGL respects
3696 * eglSwapInterval(0).
3697 *
3698 * If this window has sub-surfaces, especially a free-running
3699 * EGL-widget, we need to post the parent surface once with
3700 * all the old state to guarantee, that the EGL-widget will
3701 * receive its frame callback soon. Otherwise, a forced call
3702 * to eglSwapBuffers may end up blocking, waiting for a frame
3703 * event that will never come, because we will commit the parent
3704 * surface with all new state only after eglSwapBuffers returns.
3705 *
3706 * This assumes, that:
3707 * 1. When the EGL widget's resize hook is called, it pauses.
3708 * 2. When the EGL widget's redraw hook is called, it forces a
3709 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3710 * In a single threaded application condition 1 is a no-op.
3711 *
3712 * XXX: This should actually be after the surface_resize() calls,
3713 * but cannot, because then it would commit the incomplete state
3714 * accumulated from the widget resize hooks.
3715 */
3716 if (window->subsurface_list.next != &window->main_surface->link ||
3717 window->subsurface_list.prev != &window->main_surface->link)
3718 wl_surface_commit(window->main_surface->surface);
3719}
3720
3721static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003722window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003723{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003724 struct surface *surface;
3725
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003726 widget_set_allocation(window->main_surface->widget,
3727 window->pending_allocation.x,
3728 window->pending_allocation.y,
3729 window->pending_allocation.width,
3730 window->pending_allocation.height);
3731
3732 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003733
3734 /* The main surface is in the list, too. Main surface's
3735 * resize_handler is responsible for calling widget_set_allocation()
3736 * on all sub-surface root widgets, so they will be resized
3737 * properly.
3738 */
3739 wl_list_for_each(surface, &window->subsurface_list, link) {
3740 if (surface == window->main_surface)
3741 continue;
3742
3743 surface_set_synchronized(surface);
3744 surface_resize(surface);
3745 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003746
3747 if (!window->fullscreen && !window->maximized)
3748 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003749}
3750
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003751static void
3752idle_resize(struct window *window)
3753{
3754 window->resize_needed = 0;
3755 window->redraw_needed = 1;
3756
3757 DBG("from %dx%d to %dx%d\n",
3758 window->main_surface->server_allocation.width,
3759 window->main_surface->server_allocation.height,
3760 window->pending_allocation.width,
3761 window->pending_allocation.height);
3762
3763 hack_prevent_EGL_sub_surface_deadlock(window);
3764
3765 window_do_resize(window);
3766}
3767
3768static void
3769undo_resize(struct window *window)
3770{
3771 window->pending_allocation.width =
3772 window->main_surface->server_allocation.width;
3773 window->pending_allocation.height =
3774 window->main_surface->server_allocation.height;
3775
3776 DBG("back to %dx%d\n",
3777 window->main_surface->server_allocation.width,
3778 window->main_surface->server_allocation.height);
3779
3780 window_do_resize(window);
3781
3782 if (window->pending_allocation.width == 0 &&
3783 window->pending_allocation.height == 0) {
3784 fprintf(stderr, "Error: Could not draw a surface, "
3785 "most likely due to insufficient disk space in "
3786 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3787 exit(EXIT_FAILURE);
3788 }
3789}
3790
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003791void
3792window_schedule_resize(struct window *window, int width, int height)
3793{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003794 /* We should probably get these numbers from the theme. */
3795 const int min_width = 200, min_height = 200;
3796
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003797 window->pending_allocation.x = 0;
3798 window->pending_allocation.y = 0;
3799 window->pending_allocation.width = width;
3800 window->pending_allocation.height = height;
3801
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003802 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003803 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003804 window->min_allocation.width = min_width;
3805 else
3806 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003807 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003808 window->min_allocation.height = min_height;
3809 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003810 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003811 }
3812
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003813 if (window->pending_allocation.width < window->min_allocation.width)
3814 window->pending_allocation.width = window->min_allocation.width;
3815 if (window->pending_allocation.height < window->min_allocation.height)
3816 window->pending_allocation.height = window->min_allocation.height;
3817
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003818 window->resize_needed = 1;
3819 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003820}
3821
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003822void
3823widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3824{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003825 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003826}
3827
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003828static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003829handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003830 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003831{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003832 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003833
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003834 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003835}
3836
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003837static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003838handle_surface_change_state(void *data, struct xdg_surface *xdg_surface,
3839 uint32_t state,
3840 uint32_t value,
3841 uint32_t serial)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003842{
3843 struct window *window = data;
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003844
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003845 switch (state) {
3846 case XDG_SURFACE_STATE_MAXIMIZED:
3847 window->maximized = value;
3848 break;
3849 case XDG_SURFACE_STATE_FULLSCREEN:
3850 window->fullscreen = value;
3851 break;
3852 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003853
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003854 if (!window->fullscreen && !window->maximized)
3855 window_schedule_resize(window,
3856 window->saved_allocation.width,
3857 window->saved_allocation.height);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003858
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003859 xdg_surface_ack_change_state(xdg_surface, state, value, serial);
3860 window_schedule_redraw(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003861}
3862
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003863static void
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003864handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003865{
3866 struct window *window = data;
3867 window->focused = 1;
3868}
3869
3870static void
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003871handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003872{
3873 struct window *window = data;
3874 window->focused = 0;
3875}
3876
3877static void
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003878handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3879{
3880 struct window *window = data;
3881 window_close(window);
3882}
3883
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003884static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003885 handle_surface_configure,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003886 handle_surface_change_state,
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003887 handle_surface_activated,
3888 handle_surface_deactivated,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003889 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003890};
3891
3892static void
Jasper St. Pierre53686042013-12-09 15:26:25 -05003893window_sync_transient_for(struct window *window)
3894{
3895 struct wl_surface *parent_surface;
3896
3897 if (!window->xdg_surface)
3898 return;
3899
3900 if (window->transient_for)
3901 parent_surface = window->transient_for->main_surface->surface;
3902 else
3903 parent_surface = NULL;
3904
3905 xdg_surface_set_transient_for(window->xdg_surface, parent_surface);
3906}
3907
3908static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003909window_sync_margin(struct window *window)
3910{
3911 int margin;
3912
3913 if (!window->xdg_surface)
3914 return;
3915
3916 if (!window->frame)
3917 return;
3918
3919 margin = frame_get_shadow_margin(window->frame->frame);
3920
3921 /* Shadow size is the same on every side. */
3922 xdg_surface_set_margin(window->xdg_surface,
3923 margin,
3924 margin,
3925 margin,
3926 margin);
3927}
3928
3929static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003930window_flush(struct window *window)
3931{
3932 struct surface *surface;
3933
3934 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003935 if (window->xdg_surface) {
Jasper St. Pierre53686042013-12-09 15:26:25 -05003936 window_sync_transient_for(window);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003937 window_sync_margin(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003938 }
3939 }
3940
3941 wl_list_for_each(surface, &window->subsurface_list, link) {
3942 if (surface == window->main_surface)
3943 continue;
3944
3945 surface_flush(surface);
3946 }
3947
3948 surface_flush(window->main_surface);
3949}
3950
3951static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003952menu_destroy(struct menu *menu)
3953{
3954 widget_destroy(menu->widget);
3955 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003956 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003957 free(menu);
3958}
3959
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003960void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003961window_get_allocation(struct window *window,
3962 struct rectangle *allocation)
3963{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003964 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003965}
3966
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003967static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003968widget_redraw(struct widget *widget)
3969{
3970 struct widget *child;
3971
3972 if (widget->redraw_handler)
3973 widget->redraw_handler(widget, widget->user_data);
3974 wl_list_for_each(child, &widget->child_list, link)
3975 widget_redraw(child);
3976}
3977
3978static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003979frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3980{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003981 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003982
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003983 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003984 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003985 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003986 surface->frame_cb = NULL;
3987
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003988 surface->last_time = time;
3989
Pekka Paalanen71233882013-04-25 13:57:53 +03003990 if (surface->redraw_needed || surface->window->redraw_needed) {
3991 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003992 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003993 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003994}
3995
3996static const struct wl_callback_listener listener = {
3997 frame_callback
3998};
3999
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004000static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004001surface_redraw(struct surface *surface)
4002{
Pekka Paalanen71233882013-04-25 13:57:53 +03004003 DBG_OBJ(surface->surface, "begin\n");
4004
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004005 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004006 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004007
4008 /* Whole-window redraw forces a redraw even if the previous has
4009 * not yet hit the screen.
4010 */
4011 if (surface->frame_cb) {
4012 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004013 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004014
Pekka Paalanen71233882013-04-25 13:57:53 +03004015 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004016 wl_callback_destroy(surface->frame_cb);
4017 }
4018
Neil Roberts97b747c2013-12-19 16:17:12 +00004019 if (surface->widget->use_cairo &&
4020 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004021 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4022 return -1;
4023 }
4024
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004025 surface->frame_cb = wl_surface_frame(surface->surface);
4026 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004027 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004028
4029 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004030 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004031 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004032 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004033 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004034}
4035
4036static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004037idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004038{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004039 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004040 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004041 int failed = 0;
4042 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004043
Pekka Paalanen71233882013-04-25 13:57:53 +03004044 DBG(" --------- \n");
4045
Pekka Paalaneneebff542013-04-25 13:57:52 +03004046 wl_list_init(&window->redraw_task.link);
4047 window->redraw_task_scheduled = 0;
4048
4049 if (window->resize_needed) {
4050 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004051 if (window->main_surface->frame_cb) {
4052 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004053 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004054 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004055
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004056 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004057 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004058 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004059
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004060 if (surface_redraw(window->main_surface) < 0) {
4061 /*
4062 * Only main_surface failure will cause us to undo the resize.
4063 * If sub-surfaces fail, they will just be broken with old
4064 * content.
4065 */
4066 failed = 1;
4067 } else {
4068 wl_list_for_each(surface, &window->subsurface_list, link) {
4069 if (surface == window->main_surface)
4070 continue;
4071
4072 surface_redraw(surface);
4073 }
4074 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004075
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004076 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004077 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004078
4079 wl_list_for_each(surface, &window->subsurface_list, link)
4080 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004081
4082 if (resized && failed) {
4083 /* Restore widget tree to correspond to what is on screen. */
4084 undo_resize(window);
4085 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004086}
4087
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004088static void
4089window_schedule_redraw_task(struct window *window)
4090{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004091 if (!window->redraw_task_scheduled) {
4092 window->redraw_task.run = idle_redraw;
4093 display_defer(window->display, &window->redraw_task);
4094 window->redraw_task_scheduled = 1;
4095 }
4096}
4097
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004098void
4099window_schedule_redraw(struct window *window)
4100{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004101 struct surface *surface;
4102
Pekka Paalanen71233882013-04-25 13:57:53 +03004103 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4104
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004105 wl_list_for_each(surface, &window->subsurface_list, link)
4106 surface->redraw_needed = 1;
4107
4108 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004109}
4110
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004111int
4112window_is_fullscreen(struct window *window)
4113{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004114 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004115}
4116
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004117void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004118window_set_fullscreen(struct window *window, int fullscreen)
4119{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004120 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004121 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004122
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004123 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004124 return;
4125
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004126 xdg_surface_request_change_state(window->xdg_surface,
4127 XDG_SURFACE_STATE_FULLSCREEN,
4128 fullscreen ? 1 : 0,
4129 0);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004130}
4131
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004132int
4133window_is_maximized(struct window *window)
4134{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004135 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004136}
4137
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004138void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004139window_set_maximized(struct window *window, int maximized)
4140{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004141 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004142 return;
4143
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004144 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004145 return;
4146
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004147 xdg_surface_request_change_state(window->xdg_surface,
4148 XDG_SURFACE_STATE_MAXIMIZED,
4149 maximized ? 1 : 0,
4150 0);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004151}
4152
4153void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004154window_set_minimized(struct window *window)
4155{
4156 if (!window->xdg_surface)
4157 return;
4158
4159 xdg_surface_set_minimized(window->xdg_surface);
4160}
4161
4162void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004163window_set_user_data(struct window *window, void *data)
4164{
4165 window->user_data = data;
4166}
4167
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004168void *
4169window_get_user_data(struct window *window)
4170{
4171 return window->user_data;
4172}
4173
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004174void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004175window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004176 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004177{
4178 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004179}
4180
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004181void
4182window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004183 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004184{
4185 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004186}
4187
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004188void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004189window_set_data_handler(struct window *window, window_data_handler_t handler)
4190{
4191 window->data_handler = handler;
4192}
4193
4194void
4195window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4196{
4197 window->drop_handler = handler;
4198}
4199
4200void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004201window_set_close_handler(struct window *window,
4202 window_close_handler_t handler)
4203{
4204 window->close_handler = handler;
4205}
4206
4207void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004208window_set_fullscreen_handler(struct window *window,
4209 window_fullscreen_handler_t handler)
4210{
4211 window->fullscreen_handler = handler;
4212}
4213
4214void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004215window_set_output_handler(struct window *window,
4216 window_output_handler_t handler)
4217{
4218 window->output_handler = handler;
4219}
4220
4221void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004222window_set_title(struct window *window, const char *title)
4223{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004224 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004225 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004226 if (window->frame) {
4227 frame_set_title(window->frame->frame, title);
4228 widget_schedule_redraw(window->frame->widget);
4229 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004230 if (window->xdg_surface)
4231 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004232}
4233
4234const char *
4235window_get_title(struct window *window)
4236{
4237 return window->title;
4238}
4239
4240void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004241window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4242{
4243 struct text_cursor_position *text_cursor_position =
4244 window->display->text_cursor_position;
4245
Scott Moreau9295ce02012-06-01 12:46:10 -06004246 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004247 return;
4248
4249 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004250 window->main_surface->surface,
4251 wl_fixed_from_int(x),
4252 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004253}
4254
4255void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004256window_damage(struct window *window, int32_t x, int32_t y,
4257 int32_t width, int32_t height)
4258{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004259 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004260}
4261
Casey Dahlin9074db52012-04-19 22:50:09 -04004262static void
4263surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004264 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004265{
Rob Bradford7507b572012-05-15 17:55:34 +01004266 struct window *window = data;
4267 struct output *output;
4268 struct output *output_found = NULL;
4269 struct window_output *window_output;
4270
4271 wl_list_for_each(output, &window->display->output_list, link) {
4272 if (output->output == wl_output) {
4273 output_found = output;
4274 break;
4275 }
4276 }
4277
4278 if (!output_found)
4279 return;
4280
Brian Lovinbc919262013-08-07 15:34:59 -07004281 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004282 window_output->output = output_found;
4283
4284 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004285
4286 if (window->output_handler)
4287 window->output_handler(window, output_found, 1,
4288 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004289}
4290
4291static void
4292surface_leave(void *data,
4293 struct wl_surface *wl_surface, struct wl_output *output)
4294{
Rob Bradford7507b572012-05-15 17:55:34 +01004295 struct window *window = data;
4296 struct window_output *window_output;
4297 struct window_output *window_output_found = NULL;
4298
4299 wl_list_for_each(window_output, &window->window_output_list, link) {
4300 if (window_output->output->output == output) {
4301 window_output_found = window_output;
4302 break;
4303 }
4304 }
4305
4306 if (window_output_found) {
4307 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004308
4309 if (window->output_handler)
4310 window->output_handler(window, window_output->output,
4311 0, window->user_data);
4312
Rob Bradford7507b572012-05-15 17:55:34 +01004313 free(window_output_found);
4314 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004315}
4316
4317static const struct wl_surface_listener surface_listener = {
4318 surface_enter,
4319 surface_leave
4320};
4321
Pekka Paalanen4e373742013-02-13 16:17:13 +02004322static struct surface *
4323surface_create(struct window *window)
4324{
4325 struct display *display = window->display;
4326 struct surface *surface;
4327
Brian Lovinbc919262013-08-07 15:34:59 -07004328 surface = xmalloc(sizeof *surface);
4329 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004330 if (!surface)
4331 return NULL;
4332
4333 surface->window = window;
4334 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004335 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004336 wl_surface_add_listener(surface->surface, &surface_listener, window);
4337
Pekka Paalanen35e82632013-04-25 13:57:48 +03004338 wl_list_insert(&window->subsurface_list, &surface->link);
4339
Pekka Paalanen4e373742013-02-13 16:17:13 +02004340 return surface;
4341}
4342
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004343static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004344window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004345{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004346 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004347 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004348
Peter Huttererf3d62272013-08-08 11:57:05 +10004349 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004350 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004351 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004352
4353 surface = surface_create(window);
4354 window->main_surface = surface;
4355
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004356 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004357
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004358 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004359 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004360
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004361 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004362#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004363 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004364#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004365 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004366#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004367 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004368 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004369
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004370 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004371 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004372 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004373
Rob Bradford7507b572012-05-15 17:55:34 +01004374 wl_list_init (&window->window_output_list);
4375
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004376 return window;
4377}
4378
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004379struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004380window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004381{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004382 struct window *window;
4383
4384 window = window_create_internal(display, 0);
4385
4386 window->xdg_surface =
4387 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4388 window->main_surface->surface);
4389 fail_on_null(window->xdg_surface);
4390
4391 xdg_surface_set_user_data(window->xdg_surface, window);
4392 xdg_surface_add_listener(window->xdg_surface,
4393 &xdg_surface_listener, window);
4394
4395 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004396}
4397
4398struct window *
4399window_create_custom(struct display *display)
4400{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004401 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004402}
4403
Jasper St. Pierre53686042013-12-09 15:26:25 -05004404void
4405window_set_transient_for(struct window *window,
4406 struct window *parent_window)
4407{
4408 window->transient_for = parent_window;
4409 window_sync_transient_for(window);
4410}
4411
4412struct window *
4413window_get_transient_for(struct window *window)
4414{
4415 return window->transient_for;
4416}
4417
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004418static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004419menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004420{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004421 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004422 int next;
4423
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004424 frame_interior(menu->frame, &x, &y, &width, &height);
4425 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004426 if (menu->current != next) {
4427 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004428 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004429 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004430}
4431
4432static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004433menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004434 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004435 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004436{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004437 struct menu *menu = data;
4438
4439 if (widget == menu->widget)
4440 menu_set_item(data, y);
4441
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004442 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004443}
4444
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004445static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004446menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004447 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004448{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004449 struct menu *menu = data;
4450
4451 if (widget == menu->widget)
4452 menu_set_item(data, y);
4453
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004454 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004455}
4456
4457static void
4458menu_leave_handler(struct widget *widget, struct input *input, void *data)
4459{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004460 struct menu *menu = data;
4461
4462 if (widget == menu->widget)
4463 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004464}
4465
4466static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004467menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004468 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004469 uint32_t button, enum wl_pointer_button_state state,
4470 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004471
4472{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004473 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004474
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004475 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4476 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004477 /* Either relase after press-drag-release or
4478 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004479 menu->func(menu->parent, input,
4480 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004481 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004482 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004483 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004484 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004485 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004486}
4487
4488static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004489menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004490{
4491 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004492 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004493 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004494
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004495 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004496
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004497 frame_repaint(menu->frame, cr);
4498 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004499
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004500 theme_set_background_source(menu->window->display->theme,
4501 cr, THEME_FRAME_ACTIVE);
4502 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004503 cairo_fill(cr);
4504
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004505 cairo_select_font_face(cr, "sans",
4506 CAIRO_FONT_SLANT_NORMAL,
4507 CAIRO_FONT_WEIGHT_NORMAL);
4508 cairo_set_font_size(cr, 12);
4509
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004510 for (i = 0; i < menu->count; i++) {
4511 if (i == menu->current) {
4512 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004513 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004514 cairo_fill(cr);
4515 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004516 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004517 cairo_show_text(cr, menu->entries[i]);
4518 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004519 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4520 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004521 cairo_show_text(cr, menu->entries[i]);
4522 }
4523 }
4524
4525 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004526}
4527
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004528static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004529handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4530{
4531 struct window *window = data;
4532 struct menu *menu = window->main_surface->widget->user_data;
4533
4534 input_ungrab(menu->input);
4535 menu_destroy(menu);
4536}
4537
4538static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004539 handle_popup_popup_done,
4540};
4541
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004542void
4543window_show_menu(struct display *display,
4544 struct input *input, uint32_t time, struct window *parent,
4545 int32_t x, int32_t y,
4546 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547{
4548 struct window *window;
4549 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004550 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004551
4552 menu = malloc(sizeof *menu);
4553 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004554 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004555
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004556 window = window_create_internal(parent->display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004557 if (!window) {
4558 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004559 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004560 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004561
4562 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004563 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004564 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004565 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4566 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004567 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004568 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004569 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004570 menu->entries = entries;
4571 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004572 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004573 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004574 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004575 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004576 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004577 window->x = x;
4578 window->y = y;
4579
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004580 input_ungrab(input);
4581
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004582 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004583 widget_set_enter_handler(menu->widget, menu_enter_handler);
4584 widget_set_leave_handler(menu->widget, menu_leave_handler);
4585 widget_set_motion_handler(menu->widget, menu_motion_handler);
4586 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004587
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004588 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004589 frame_resize_inside(menu->frame, 200, count * 20);
4590 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4591 window_schedule_resize(window, frame_width(menu->frame),
4592 frame_height(menu->frame));
4593
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004594 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004595
4596 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4597 window->main_surface->surface,
4598 parent->main_surface->surface,
4599 input->seat,
4600 display_get_serial(window->display),
4601 window->x - ix,
4602 window->y - iy,
4603 0);
4604 fail_on_null(window->xdg_popup);
4605
4606 xdg_popup_set_user_data(window->xdg_popup, window);
4607 xdg_popup_add_listener(window->xdg_popup,
4608 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004609}
4610
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004611void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004612window_set_buffer_type(struct window *window, enum window_buffer_type type)
4613{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004614 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004615}
4616
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004617enum window_buffer_type
4618window_get_buffer_type(struct window *window)
4619{
4620 return window->main_surface->buffer_type;
4621}
4622
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004623void
4624window_set_preferred_format(struct window *window,
4625 enum preferred_format format)
4626{
4627 window->preferred_format = format;
4628}
4629
Pekka Paalanen35e82632013-04-25 13:57:48 +03004630struct widget *
4631window_add_subsurface(struct window *window, void *data,
4632 enum subsurface_mode default_mode)
4633{
4634 struct widget *widget;
4635 struct surface *surface;
4636 struct wl_surface *parent;
4637 struct wl_subcompositor *subcompo = window->display->subcompositor;
4638
Pekka Paalanen35e82632013-04-25 13:57:48 +03004639 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004640 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004641 widget = widget_create(window, surface, data);
4642 wl_list_init(&widget->link);
4643 surface->widget = widget;
4644
4645 parent = window->main_surface->surface;
4646 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4647 surface->surface,
4648 parent);
4649 surface->synchronized = 1;
4650
4651 switch (default_mode) {
4652 case SUBSURFACE_SYNCHRONIZED:
4653 surface->synchronized_default = 1;
4654 break;
4655 case SUBSURFACE_DESYNCHRONIZED:
4656 surface->synchronized_default = 0;
4657 break;
4658 default:
4659 assert(!"bad enum subsurface_mode");
4660 }
4661
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004662 window->resize_needed = 1;
4663 window_schedule_redraw(window);
4664
Pekka Paalanen35e82632013-04-25 13:57:48 +03004665 return widget;
4666}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004667
4668static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004669display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004670 struct wl_output *wl_output,
4671 int x, int y,
4672 int physical_width,
4673 int physical_height,
4674 int subpixel,
4675 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004676 const char *model,
4677 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004678{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004679 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004680
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004681 output->allocation.x = x;
4682 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004683 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004684
4685 if (output->make)
4686 free(output->make);
4687 output->make = strdup(make);
4688
4689 if (output->model)
4690 free(output->model);
4691 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004692}
4693
4694static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004695display_handle_done(void *data,
4696 struct wl_output *wl_output)
4697{
4698}
4699
4700static void
4701display_handle_scale(void *data,
4702 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004703 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004704{
4705 struct output *output = data;
4706
4707 output->scale = scale;
4708}
4709
4710static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004711display_handle_mode(void *data,
4712 struct wl_output *wl_output,
4713 uint32_t flags,
4714 int width,
4715 int height,
4716 int refresh)
4717{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004718 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004719 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004720
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004721 if (flags & WL_OUTPUT_MODE_CURRENT) {
4722 output->allocation.width = width;
4723 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004724 if (display->output_configure_handler)
4725 (*display->output_configure_handler)(
4726 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004727 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004728}
4729
4730static const struct wl_output_listener output_listener = {
4731 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004732 display_handle_mode,
4733 display_handle_done,
4734 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004735};
4736
4737static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004738display_add_output(struct display *d, uint32_t id)
4739{
4740 struct output *output;
4741
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004742 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004743 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004744 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004745 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004746 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004747 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004748 wl_list_insert(d->output_list.prev, &output->link);
4749
4750 wl_output_add_listener(output->output, &output_listener, output);
4751}
4752
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004753static void
4754output_destroy(struct output *output)
4755{
4756 if (output->destroy_handler)
4757 (*output->destroy_handler)(output, output->user_data);
4758
4759 wl_output_destroy(output->output);
4760 wl_list_remove(&output->link);
4761 free(output);
4762}
4763
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004764static void
4765display_destroy_output(struct display *d, uint32_t id)
4766{
4767 struct output *output;
4768
4769 wl_list_for_each(output, &d->output_list, link) {
4770 if (output->server_output_id == id) {
4771 output_destroy(output);
4772 break;
4773 }
4774 }
4775}
4776
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004777void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004778display_set_global_handler(struct display *display,
4779 display_global_handler_t handler)
4780{
4781 struct global *global;
4782
4783 display->global_handler = handler;
4784 if (!handler)
4785 return;
4786
4787 wl_list_for_each(global, &display->global_list, link)
4788 display->global_handler(display,
4789 global->name, global->interface,
4790 global->version, display->user_data);
4791}
4792
4793void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004794display_set_global_handler_remove(struct display *display,
4795 display_global_handler_t remove_handler)
4796{
4797 display->global_handler_remove = remove_handler;
4798 if (!remove_handler)
4799 return;
4800}
4801
4802void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004803display_set_output_configure_handler(struct display *display,
4804 display_output_handler_t handler)
4805{
4806 struct output *output;
4807
4808 display->output_configure_handler = handler;
4809 if (!handler)
4810 return;
4811
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004812 wl_list_for_each(output, &display->output_list, link) {
4813 if (output->allocation.width == 0 &&
4814 output->allocation.height == 0)
4815 continue;
4816
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004817 (*display->output_configure_handler)(output,
4818 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004819 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004820}
4821
4822void
4823output_set_user_data(struct output *output, void *data)
4824{
4825 output->user_data = data;
4826}
4827
4828void *
4829output_get_user_data(struct output *output)
4830{
4831 return output->user_data;
4832}
4833
4834void
4835output_set_destroy_handler(struct output *output,
4836 display_output_handler_t handler)
4837{
4838 output->destroy_handler = handler;
4839 /* FIXME: implement this, once we have way to remove outputs */
4840}
4841
4842void
Scott Moreau4e072362012-09-29 02:03:11 -06004843output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004844{
Scott Moreau4e072362012-09-29 02:03:11 -06004845 struct rectangle allocation = output->allocation;
4846
4847 switch (output->transform) {
4848 case WL_OUTPUT_TRANSFORM_90:
4849 case WL_OUTPUT_TRANSFORM_270:
4850 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4851 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4852 /* Swap width and height */
4853 allocation.width = output->allocation.height;
4854 allocation.height = output->allocation.width;
4855 break;
4856 }
4857
4858 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004859}
4860
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004861struct wl_output *
4862output_get_wl_output(struct output *output)
4863{
4864 return output->output;
4865}
4866
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004867enum wl_output_transform
4868output_get_transform(struct output *output)
4869{
4870 return output->transform;
4871}
4872
Alexander Larssonafd319a2013-05-22 14:41:27 +02004873uint32_t
4874output_get_scale(struct output *output)
4875{
4876 return output->scale;
4877}
4878
Jason Ekstrand738715d2014-04-02 19:53:50 -05004879const char *
4880output_get_make(struct output *output)
4881{
4882 return output->make;
4883}
4884
4885const char *
4886output_get_model(struct output *output)
4887{
4888 return output->model;
4889}
4890
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004891static void
Daniel Stone97f68542012-05-30 16:32:01 +01004892fini_xkb(struct input *input)
4893{
4894 xkb_state_unref(input->xkb.state);
4895 xkb_map_unref(input->xkb.keymap);
4896}
4897
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004898#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004899
Daniel Stone97f68542012-05-30 16:32:01 +01004900static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004901display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004902{
4903 struct input *input;
4904
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004905 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004906 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004907 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004908 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004909 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004910 input->pointer_focus = NULL;
4911 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004912 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004913 wl_list_insert(d->input_list.prev, &input->link);
4914
Daniel Stone37816df2012-05-16 18:45:18 +01004915 wl_seat_add_listener(input->seat, &seat_listener, input);
4916 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004917
Jason Ekstranda669bd52014-04-02 19:53:51 -05004918 if (d->data_device_manager) {
4919 input->data_device =
4920 wl_data_device_manager_get_data_device(d->data_device_manager,
4921 input->seat);
4922 wl_data_device_add_listener(input->data_device,
4923 &data_device_listener,
4924 input);
4925 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004926
4927 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004928
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004929 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4930 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004931 input->repeat_task.run = keyboard_repeat_func;
4932 display_watch_fd(d, input->repeat_timer_fd,
4933 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004934}
4935
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004936static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004937input_destroy(struct input *input)
4938{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004939 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004940 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004941
4942 if (input->drag_offer)
4943 data_offer_destroy(input->drag_offer);
4944
4945 if (input->selection_offer)
4946 data_offer_destroy(input->selection_offer);
4947
Jason Ekstranda669bd52014-04-02 19:53:51 -05004948 if (input->data_device)
4949 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004950
4951 if (input->display->seat_version >= 3) {
4952 if (input->pointer)
4953 wl_pointer_release(input->pointer);
4954 if (input->keyboard)
4955 wl_keyboard_release(input->keyboard);
4956 }
4957
Daniel Stone97f68542012-05-30 16:32:01 +01004958 fini_xkb(input);
4959
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004960 wl_surface_destroy(input->pointer_surface);
4961
Pekka Paalanene1207c72011-12-16 12:02:09 +02004962 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004963 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004964 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004965 free(input);
4966}
4967
4968static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004969init_workspace_manager(struct display *d, uint32_t id)
4970{
4971 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004972 wl_registry_bind(d->registry, id,
4973 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004974 if (d->workspace_manager != NULL)
4975 workspace_manager_add_listener(d->workspace_manager,
4976 &workspace_manager_listener,
4977 d);
4978}
4979
4980static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004981shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4982{
4983 struct display *d = data;
4984
4985 if (format == WL_SHM_FORMAT_RGB565)
4986 d->has_rgb565 = 1;
4987}
4988
4989struct wl_shm_listener shm_listener = {
4990 shm_format
4991};
4992
4993static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004994xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
4995{
4996 xdg_shell_pong(shell, serial);
4997}
4998
4999static const struct xdg_shell_listener xdg_shell_listener = {
5000 xdg_shell_ping,
5001};
5002
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08005003#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005004#ifdef static_assert
5005static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5006 "Interface version doesn't match implementation version");
5007#endif
5008
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005009static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005010registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5011 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005012{
5013 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005014 struct global *global;
5015
Brian Lovinbc919262013-08-07 15:34:59 -07005016 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005017 global->name = id;
5018 global->interface = strdup(interface);
5019 global->version = version;
5020 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005021
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005022 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005023 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005024 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005025 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005026 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005027 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005028 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005029 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005030 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005031 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005032 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005033 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5034 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005035 wl_registry_bind(registry, id,
5036 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005037 } else if (strcmp(interface, "xdg_shell") == 0) {
5038 d->xdg_shell = wl_registry_bind(registry, id,
5039 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005040 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005041 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005042 } else if (strcmp(interface, "text_cursor_position") == 0) {
5043 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005044 wl_registry_bind(registry, id,
5045 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005046 } else if (strcmp(interface, "workspace_manager") == 0) {
5047 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005048 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5049 d->subcompositor =
5050 wl_registry_bind(registry, id,
5051 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005052 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005053
5054 if (d->global_handler)
5055 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005056}
5057
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005058static void
5059registry_handle_global_remove(void *data, struct wl_registry *registry,
5060 uint32_t name)
5061{
5062 struct display *d = data;
5063 struct global *global;
5064 struct global *tmp;
5065
5066 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5067 if (global->name != name)
5068 continue;
5069
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005070 if (strcmp(global->interface, "wl_output") == 0)
5071 display_destroy_output(d, name);
5072
5073 /* XXX: Should destroy remaining bound globals */
5074
5075 if (d->global_handler_remove)
5076 d->global_handler_remove(d, name, global->interface,
5077 global->version, d->user_data);
5078
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005079 wl_list_remove(&global->link);
5080 free(global->interface);
5081 free(global);
5082 }
5083}
5084
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005085void *
5086display_bind(struct display *display, uint32_t name,
5087 const struct wl_interface *interface, uint32_t version)
5088{
5089 return wl_registry_bind(display->registry, name, interface, version);
5090}
5091
5092static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005093 registry_handle_global,
5094 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005095};
5096
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005097#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005098static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005099init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005100{
5101 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005102 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005103
Rob Clark6396ed32012-03-11 19:48:41 -05005104#ifdef USE_CAIRO_GLESV2
5105# define GL_BIT EGL_OPENGL_ES2_BIT
5106#else
5107# define GL_BIT EGL_OPENGL_BIT
5108#endif
5109
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005110 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005111 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005112 EGL_RED_SIZE, 1,
5113 EGL_GREEN_SIZE, 1,
5114 EGL_BLUE_SIZE, 1,
5115 EGL_ALPHA_SIZE, 1,
5116 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005117 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005118 EGL_NONE
5119 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005120
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005121#ifdef USE_CAIRO_GLESV2
5122 static const EGLint context_attribs[] = {
5123 EGL_CONTEXT_CLIENT_VERSION, 2,
5124 EGL_NONE
5125 };
5126 EGLint api = EGL_OPENGL_ES_API;
5127#else
5128 EGLint *context_attribs = NULL;
5129 EGLint api = EGL_OPENGL_API;
5130#endif
5131
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005132 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005133 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005134 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005135 return -1;
5136 }
5137
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005138 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005139 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005140 return -1;
5141 }
5142
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005143 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5144 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005145 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005146 return -1;
5147 }
5148
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005149 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005150 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005151 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005152 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005153 return -1;
5154 }
5155
Benjamin Franzke0c991632011-09-27 21:57:31 +02005156 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5157 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005158 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005159 return -1;
5160 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005161
5162 return 0;
5163}
5164
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005165static void
5166fini_egl(struct display *display)
5167{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005168 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005169
5170 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5171 EGL_NO_CONTEXT);
5172
5173 eglTerminate(display->dpy);
5174 eglReleaseThread();
5175}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005176#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005177
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005178static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005179init_dummy_surface(struct display *display)
5180{
5181 int len;
5182 void *data;
5183
5184 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5185 data = malloc(len);
5186 display->dummy_surface =
5187 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5188 1, 1, len);
5189 display->dummy_surface_data = data;
5190}
5191
5192static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005193handle_display_data(struct task *task, uint32_t events)
5194{
5195 struct display *display =
5196 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005197 struct epoll_event ep;
5198 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005199
5200 display->display_fd_events = events;
5201
5202 if (events & EPOLLERR || events & EPOLLHUP) {
5203 display_exit(display);
5204 return;
5205 }
5206
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005207 if (events & EPOLLIN) {
5208 ret = wl_display_dispatch(display->display);
5209 if (ret == -1) {
5210 display_exit(display);
5211 return;
5212 }
5213 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005214
5215 if (events & EPOLLOUT) {
5216 ret = wl_display_flush(display->display);
5217 if (ret == 0) {
5218 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5219 ep.data.ptr = &display->display_task;
5220 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5221 display->display_fd, &ep);
5222 } else if (ret == -1 && errno != EAGAIN) {
5223 display_exit(display);
5224 return;
5225 }
5226 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005227}
5228
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005229static void
5230log_handler(const char *format, va_list args)
5231{
5232 vfprintf(stderr, format, args);
5233}
5234
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005235struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005236display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005237{
5238 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005239
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005240 wl_log_set_handler_client(log_handler);
5241
Peter Huttererf3d62272013-08-08 11:57:05 +10005242 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005243 if (d == NULL)
5244 return NULL;
5245
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005246 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005247 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005248 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005249 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005250 return NULL;
5251 }
5252
Rob Bradford5ab9c752013-07-26 16:29:43 +01005253 d->xkb_context = xkb_context_new(0);
5254 if (d->xkb_context == NULL) {
5255 fprintf(stderr, "Failed to create XKB context\n");
5256 free(d);
5257 return NULL;
5258 }
5259
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005260 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005261 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005262 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005263 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5264 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005265
5266 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005267 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005268 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005269 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005270
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005271 d->workspace = 0;
5272 d->workspace_count = 1;
5273
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005274 d->registry = wl_display_get_registry(d->display);
5275 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005276
5277 if (wl_display_dispatch(d->display) < 0) {
5278 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5279 return NULL;
5280 }
5281
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005282#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005283 if (init_egl(d) < 0)
5284 fprintf(stderr, "EGL does not seem to work, "
5285 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005286#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005287
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005288 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005289
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005290 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005291
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005292 wl_list_init(&d->window_list);
5293
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005294 init_dummy_surface(d);
5295
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005296 return d;
5297}
5298
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005299static void
5300display_destroy_outputs(struct display *display)
5301{
5302 struct output *tmp;
5303 struct output *output;
5304
5305 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5306 output_destroy(output);
5307}
5308
Pekka Paalanene1207c72011-12-16 12:02:09 +02005309static void
5310display_destroy_inputs(struct display *display)
5311{
5312 struct input *tmp;
5313 struct input *input;
5314
5315 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5316 input_destroy(input);
5317}
5318
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005319void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005320display_destroy(struct display *display)
5321{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005322 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005323 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5324 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005325
5326 if (!wl_list_empty(&display->deferred_list))
5327 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5328
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005329 cairo_surface_destroy(display->dummy_surface);
5330 free(display->dummy_surface_data);
5331
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005332 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005333 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005334
Daniel Stone97f68542012-05-30 16:32:01 +01005335 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005336
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005337 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005338 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005339
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005340#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005341 if (display->argb_device)
5342 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005343#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005344
Pekka Paalanen35e82632013-04-25 13:57:48 +03005345 if (display->subcompositor)
5346 wl_subcompositor_destroy(display->subcompositor);
5347
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005348 if (display->xdg_shell)
5349 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005350
5351 if (display->shm)
5352 wl_shm_destroy(display->shm);
5353
5354 if (display->data_device_manager)
5355 wl_data_device_manager_destroy(display->data_device_manager);
5356
5357 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005358 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005359
5360 close(display->epoll_fd);
5361
U. Artie Eoff44874d92012-10-02 21:12:35 -07005362 if (!(display->display_fd_events & EPOLLERR) &&
5363 !(display->display_fd_events & EPOLLHUP))
5364 wl_display_flush(display->display);
5365
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005366 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005367 free(display);
5368}
5369
5370void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005371display_set_user_data(struct display *display, void *data)
5372{
5373 display->user_data = data;
5374}
5375
5376void *
5377display_get_user_data(struct display *display)
5378{
5379 return display->user_data;
5380}
5381
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005382struct wl_display *
5383display_get_display(struct display *display)
5384{
5385 return display->display;
5386}
5387
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005388int
5389display_has_subcompositor(struct display *display)
5390{
5391 if (display->subcompositor)
5392 return 1;
5393
5394 wl_display_roundtrip(display->display);
5395
5396 return display->subcompositor != NULL;
5397}
5398
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005399cairo_device_t *
5400display_get_cairo_device(struct display *display)
5401{
5402 return display->argb_device;
5403}
5404
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005405struct output *
5406display_get_output(struct display *display)
5407{
5408 return container_of(display->output_list.next, struct output, link);
5409}
5410
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005411struct wl_compositor *
5412display_get_compositor(struct display *display)
5413{
5414 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005415}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005416
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005417uint32_t
5418display_get_serial(struct display *display)
5419{
5420 return display->serial;
5421}
5422
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005423EGLDisplay
5424display_get_egl_display(struct display *d)
5425{
5426 return d->dpy;
5427}
5428
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005429struct wl_data_source *
5430display_create_data_source(struct display *display)
5431{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005432 if (display->data_device_manager)
5433 return wl_data_device_manager_create_data_source(display->data_device_manager);
5434 else
5435 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005436}
5437
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005438EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005439display_get_argb_egl_config(struct display *d)
5440{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005441 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005442}
5443
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005444int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005445display_acquire_window_surface(struct display *display,
5446 struct window *window,
5447 EGLContext ctx)
5448{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005449 struct surface *surface = window->main_surface;
5450
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005451 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005452 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005453
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005454 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005455 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005456}
5457
5458void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005459display_release_window_surface(struct display *display,
5460 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005461{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005462 struct surface *surface = window->main_surface;
5463
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005464 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005465 return;
5466
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005467 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005468}
5469
5470void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005471display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005472{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005473 wl_list_insert(&display->deferred_list, &task->link);
5474}
5475
5476void
5477display_watch_fd(struct display *display,
5478 int fd, uint32_t events, struct task *task)
5479{
5480 struct epoll_event ep;
5481
5482 ep.events = events;
5483 ep.data.ptr = task;
5484 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5485}
5486
5487void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005488display_unwatch_fd(struct display *display, int fd)
5489{
5490 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5491}
5492
5493void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005494display_run(struct display *display)
5495{
5496 struct task *task;
5497 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005498 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005499
Pekka Paalanen826d7952011-12-15 10:14:07 +02005500 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005501 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005502 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005503 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005504 struct task, link);
5505 wl_list_remove(&task->link);
5506 task->run(task, 0);
5507 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005508
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005509 wl_display_dispatch_pending(display->display);
5510
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005511 if (!display->running)
5512 break;
5513
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005514 ret = wl_display_flush(display->display);
5515 if (ret < 0 && errno == EAGAIN) {
5516 ep[0].events =
5517 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5518 ep[0].data.ptr = &display->display_task;
5519
5520 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5521 display->display_fd, &ep[0]);
5522 } else if (ret < 0) {
5523 break;
5524 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005525
5526 count = epoll_wait(display->epoll_fd,
5527 ep, ARRAY_LENGTH(ep), -1);
5528 for (i = 0; i < count; i++) {
5529 task = ep[i].data.ptr;
5530 task->run(task, ep[i].events);
5531 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005532 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005533}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005534
5535void
5536display_exit(struct display *display)
5537{
5538 display->running = 0;
5539}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005540
5541void
5542keysym_modifiers_add(struct wl_array *modifiers_map,
5543 const char *name)
5544{
5545 size_t len = strlen(name) + 1;
5546 char *p;
5547
5548 p = wl_array_add(modifiers_map, len);
5549
5550 if (p == NULL)
5551 return;
5552
5553 strncpy(p, name, len);
5554}
5555
5556static xkb_mod_index_t
5557keysym_modifiers_get_index(struct wl_array *modifiers_map,
5558 const char *name)
5559{
5560 xkb_mod_index_t index = 0;
5561 char *p = modifiers_map->data;
5562
5563 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5564 if (strcmp(p, name) == 0)
5565 return index;
5566
5567 index++;
5568 p += strlen(p) + 1;
5569 }
5570
5571 return XKB_MOD_INVALID;
5572}
5573
5574xkb_mod_mask_t
5575keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5576 const char *name)
5577{
5578 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5579
5580 if (index == XKB_MOD_INVALID)
5581 return XKB_MOD_INVALID;
5582
5583 return 1 << index;
5584}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005585
5586void *
5587fail_on_null(void *p)
5588{
5589 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005590 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005591 exit(EXIT_FAILURE);
5592 }
5593
5594 return p;
5595}
5596
5597void *
5598xmalloc(size_t s)
5599{
5600 return fail_on_null(malloc(s));
5601}
5602
Peter Huttererf3d62272013-08-08 11:57:05 +10005603void *
5604xzalloc(size_t s)
5605{
5606 return fail_on_null(zalloc(s));
5607}
5608
Kristian Høgsbergce278412013-07-25 15:20:20 -07005609char *
5610xstrdup(const char *s)
5611{
5612 return fail_on_null(strdup(s));
5613}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005614
5615void *
5616xrealloc(char *p, size_t s)
5617{
5618 return fail_on_null(realloc(p, s));
5619}