blob: e770a040e8a336e7cfa93bae845fde69977280ca [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;
2561}
2562
2563void
2564input_ungrab(struct input *input)
2565{
2566 struct widget *widget;
2567
2568 input->grab = NULL;
2569 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002570 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002571 input->sx, input->sy);
2572 input_set_focus_widget(input, widget, input->sx, input->sy);
2573 }
2574}
2575
2576static void
2577input_remove_pointer_focus(struct input *input)
2578{
2579 struct window *window = input->pointer_focus;
2580
2581 if (!window)
2582 return;
2583
2584 input_set_focus_widget(input, NULL, 0, 0);
2585
2586 input->pointer_focus = NULL;
2587 input->current_cursor = CURSOR_UNSET;
2588}
2589
2590static void
2591pointer_handle_enter(void *data, struct wl_pointer *pointer,
2592 uint32_t serial, struct wl_surface *surface,
2593 wl_fixed_t sx_w, wl_fixed_t sy_w)
2594{
2595 struct input *input = data;
2596 struct window *window;
2597 struct widget *widget;
2598 float sx = wl_fixed_to_double(sx_w);
2599 float sy = wl_fixed_to_double(sy_w);
2600
2601 if (!surface) {
2602 /* enter event for a window we've just destroyed */
2603 return;
2604 }
2605
2606 input->display->serial = serial;
2607 input->pointer_enter_serial = serial;
2608 input->pointer_focus = wl_surface_get_user_data(surface);
2609 window = input->pointer_focus;
2610
Pekka Paalanen99436862012-11-19 17:15:59 +02002611 if (window->resizing) {
2612 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002613 /* Schedule a redraw to free the pool */
2614 window_schedule_redraw(window);
2615 }
2616
2617 input->sx = sx;
2618 input->sy = sy;
2619
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002620 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002621 input_set_focus_widget(input, widget, sx, sy);
2622}
2623
2624static void
2625pointer_handle_leave(void *data, struct wl_pointer *pointer,
2626 uint32_t serial, struct wl_surface *surface)
2627{
2628 struct input *input = data;
2629
2630 input->display->serial = serial;
2631 input_remove_pointer_focus(input);
2632}
2633
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002634static void
Daniel Stone37816df2012-05-16 18:45:18 +01002635pointer_handle_motion(void *data, struct wl_pointer *pointer,
2636 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002637{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002638 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002639 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002640 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002641 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002642 float sx = wl_fixed_to_double(sx_w);
2643 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002644
Paul Winwoodb22bf572013-08-29 10:52:54 +01002645 input->sx = sx;
2646 input->sy = sy;
2647
2648 if (!window)
2649 return;
2650
Rob Bradford5f087742013-07-11 19:41:27 +01002651 /* when making the window smaller - e.g. after a unmaximise we might
2652 * still have a pending motion event that the compositor has picked
2653 * based on the old surface dimensions
2654 */
2655 if (sx > window->main_surface->allocation.width ||
2656 sy > window->main_surface->allocation.height)
2657 return;
2658
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002659 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002660 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002661 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002662 }
2663
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002664 if (input->grab)
2665 widget = input->grab;
2666 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002667 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002668 if (widget) {
2669 if (widget->motion_handler)
2670 cursor = widget->motion_handler(input->focus_widget,
2671 input, time, sx, sy,
2672 widget->user_data);
2673 else
2674 cursor = widget->default_cursor;
2675 } else
2676 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002677
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002678 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002679}
2680
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002681static void
Daniel Stone37816df2012-05-16 18:45:18 +01002682pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002683 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002684{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002685 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002686 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002687 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002688
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002689 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002690 if (input->focus_widget && input->grab == NULL &&
2691 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002692 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002693
Neil Roberts6b28aad2012-01-23 19:11:18 +00002694 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002695 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002696 (*widget->button_handler)(widget,
2697 input, time,
2698 button, state,
2699 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002700
Daniel Stone4dbadb12012-05-30 16:31:51 +01002701 if (input->grab && input->grab_button == button &&
2702 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002703 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002704}
2705
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002706static void
Daniel Stone37816df2012-05-16 18:45:18 +01002707pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002708 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002709{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002710 struct input *input = data;
2711 struct widget *widget;
2712
2713 widget = input->focus_widget;
2714 if (input->grab)
2715 widget = input->grab;
2716 if (widget && widget->axis_handler)
2717 (*widget->axis_handler)(widget,
2718 input, time,
2719 axis, value,
2720 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002721}
2722
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002723static const struct wl_pointer_listener pointer_listener = {
2724 pointer_handle_enter,
2725 pointer_handle_leave,
2726 pointer_handle_motion,
2727 pointer_handle_button,
2728 pointer_handle_axis,
2729};
2730
2731static void
2732input_remove_keyboard_focus(struct input *input)
2733{
2734 struct window *window = input->keyboard_focus;
2735 struct itimerspec its;
2736
2737 its.it_interval.tv_sec = 0;
2738 its.it_interval.tv_nsec = 0;
2739 its.it_value.tv_sec = 0;
2740 its.it_value.tv_nsec = 0;
2741 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2742
2743 if (!window)
2744 return;
2745
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002746 if (window->keyboard_focus_handler)
2747 (*window->keyboard_focus_handler)(window, NULL,
2748 window->user_data);
2749
2750 input->keyboard_focus = NULL;
2751}
2752
2753static void
2754keyboard_repeat_func(struct task *task, uint32_t events)
2755{
2756 struct input *input =
2757 container_of(task, struct input, repeat_task);
2758 struct window *window = input->keyboard_focus;
2759 uint64_t exp;
2760
2761 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2762 /* If we change the timer between the fd becoming
2763 * readable and getting here, there'll be nothing to
2764 * read and we get EAGAIN. */
2765 return;
2766
2767 if (window && window->key_handler) {
2768 (*window->key_handler)(window, input, input->repeat_time,
2769 input->repeat_key, input->repeat_sym,
2770 WL_KEYBOARD_KEY_STATE_PRESSED,
2771 window->user_data);
2772 }
2773}
2774
2775static void
2776keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2777 uint32_t format, int fd, uint32_t size)
2778{
2779 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002780 struct xkb_keymap *keymap;
2781 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002782 char *map_str;
2783
2784 if (!data) {
2785 close(fd);
2786 return;
2787 }
2788
2789 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2790 close(fd);
2791 return;
2792 }
2793
2794 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2795 if (map_str == MAP_FAILED) {
2796 close(fd);
2797 return;
2798 }
2799
Rui Matos3eccb862013-10-10 19:44:22 +02002800 keymap = xkb_map_new_from_string(input->display->xkb_context,
2801 map_str,
2802 XKB_KEYMAP_FORMAT_TEXT_V1,
2803 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002804 munmap(map_str, size);
2805 close(fd);
2806
Rui Matos3eccb862013-10-10 19:44:22 +02002807 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002808 fprintf(stderr, "failed to compile keymap\n");
2809 return;
2810 }
2811
Rui Matos3eccb862013-10-10 19:44:22 +02002812 state = xkb_state_new(keymap);
2813 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002814 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002815 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002816 return;
2817 }
2818
Rui Matos3eccb862013-10-10 19:44:22 +02002819 xkb_keymap_unref(input->xkb.keymap);
2820 xkb_state_unref(input->xkb.state);
2821 input->xkb.keymap = keymap;
2822 input->xkb.state = state;
2823
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002824 input->xkb.control_mask =
2825 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2826 input->xkb.alt_mask =
2827 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2828 input->xkb.shift_mask =
2829 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2830}
2831
2832static void
2833keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2834 uint32_t serial, struct wl_surface *surface,
2835 struct wl_array *keys)
2836{
2837 struct input *input = data;
2838 struct window *window;
2839
2840 input->display->serial = serial;
2841 input->keyboard_focus = wl_surface_get_user_data(surface);
2842
2843 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002844 if (window->keyboard_focus_handler)
2845 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002846 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002847}
2848
2849static void
2850keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2851 uint32_t serial, struct wl_surface *surface)
2852{
2853 struct input *input = data;
2854
2855 input->display->serial = serial;
2856 input_remove_keyboard_focus(input);
2857}
2858
Scott Moreau210d0792012-03-22 10:47:01 -06002859static void
Daniel Stone37816df2012-05-16 18:45:18 +01002860keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002861 uint32_t serial, uint32_t time, uint32_t key,
2862 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002863{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002864 struct input *input = data;
2865 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002866 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002867 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002868 const xkb_keysym_t *syms;
2869 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002870 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002871
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002872 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002873 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002874 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002875 return;
2876
Daniel Stone97f68542012-05-30 16:32:01 +01002877 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002878
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002879 sym = XKB_KEY_NoSymbol;
2880 if (num_syms == 1)
2881 sym = syms[0];
2882
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002883
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002884 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002885 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002886 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002887 } else if (sym == XKB_KEY_F11 &&
2888 window->fullscreen_handler &&
2889 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2890 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002891 } else if (sym == XKB_KEY_F4 &&
2892 input->modifiers == MOD_ALT_MASK &&
2893 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002894 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002895 } else if (window->key_handler) {
2896 (*window->key_handler)(window, input, time, key,
2897 sym, state, window->user_data);
2898 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002899
2900 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2901 key == input->repeat_key) {
2902 its.it_interval.tv_sec = 0;
2903 its.it_interval.tv_nsec = 0;
2904 its.it_value.tv_sec = 0;
2905 its.it_value.tv_nsec = 0;
2906 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002907 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2908 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002909 input->repeat_sym = sym;
2910 input->repeat_key = key;
2911 input->repeat_time = time;
2912 its.it_interval.tv_sec = 0;
2913 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2914 its.it_value.tv_sec = 0;
2915 its.it_value.tv_nsec = 400 * 1000 * 1000;
2916 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2917 }
2918}
2919
2920static void
Daniel Stone351eb612012-05-31 15:27:47 -04002921keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2922 uint32_t serial, uint32_t mods_depressed,
2923 uint32_t mods_latched, uint32_t mods_locked,
2924 uint32_t group)
2925{
2926 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002927 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002928
Matt Ropere61561f2013-06-24 16:52:43 +01002929 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2930 if (!input->xkb.keymap)
2931 return;
2932
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002933 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2934 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002935 mask = xkb_state_serialize_mods(input->xkb.state,
2936 XKB_STATE_DEPRESSED |
2937 XKB_STATE_LATCHED);
2938 input->modifiers = 0;
2939 if (mask & input->xkb.control_mask)
2940 input->modifiers |= MOD_CONTROL_MASK;
2941 if (mask & input->xkb.alt_mask)
2942 input->modifiers |= MOD_ALT_MASK;
2943 if (mask & input->xkb.shift_mask)
2944 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002945}
2946
Daniel Stone37816df2012-05-16 18:45:18 +01002947static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002948 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002949 keyboard_handle_enter,
2950 keyboard_handle_leave,
2951 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002952 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002953};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002954
2955static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002956touch_handle_down(void *data, struct wl_touch *wl_touch,
2957 uint32_t serial, uint32_t time, struct wl_surface *surface,
2958 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2959{
2960 struct input *input = data;
2961 struct widget *widget;
2962 float sx = wl_fixed_to_double(x_w);
2963 float sy = wl_fixed_to_double(y_w);
2964
Rusty Lynch1084da52013-08-15 09:10:08 -07002965 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002966 input->touch_focus = wl_surface_get_user_data(surface);
2967 if (!input->touch_focus) {
2968 DBG("Failed to find to touch focus for surface %p\n", surface);
2969 return;
2970 }
2971
2972 widget = window_find_widget(input->touch_focus,
2973 wl_fixed_to_double(x_w),
2974 wl_fixed_to_double(y_w));
2975 if (widget) {
2976 struct touch_point *tp = xmalloc(sizeof *tp);
2977 if (tp) {
2978 tp->id = id;
2979 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08002980 tp->x = sx;
2981 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07002982 wl_list_insert(&input->touch_point_list, &tp->link);
2983
2984 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07002985 (*widget->touch_down_handler)(widget, input,
2986 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002987 sx, sy,
2988 widget->user_data);
2989 }
2990 }
2991}
2992
2993static void
2994touch_handle_up(void *data, struct wl_touch *wl_touch,
2995 uint32_t serial, uint32_t time, int32_t id)
2996{
2997 struct input *input = data;
2998 struct touch_point *tp, *tmp;
2999
Rusty Lynch041815a2013-08-08 21:20:38 -07003000 if (!input->touch_focus) {
3001 DBG("No touch focus found for touch up event!\n");
3002 return;
3003 }
3004
3005 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3006 if (tp->id != id)
3007 continue;
3008
3009 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003010 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003011 time, id,
3012 tp->widget->user_data);
3013
3014 wl_list_remove(&tp->link);
3015 free(tp);
3016
3017 return;
3018 }
3019}
3020
3021static void
3022touch_handle_motion(void *data, struct wl_touch *wl_touch,
3023 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3024{
3025 struct input *input = data;
3026 struct touch_point *tp;
3027 float sx = wl_fixed_to_double(x_w);
3028 float sy = wl_fixed_to_double(y_w);
3029
3030 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3031
3032 if (!input->touch_focus) {
3033 DBG("No touch focus found for touch motion event!\n");
3034 return;
3035 }
3036
3037 wl_list_for_each(tp, &input->touch_point_list, link) {
3038 if (tp->id != id)
3039 continue;
3040
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003041 tp->x = sx;
3042 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003043 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003044 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003045 id, sx, sy,
3046 tp->widget->user_data);
3047 return;
3048 }
3049}
3050
3051static void
3052touch_handle_frame(void *data, struct wl_touch *wl_touch)
3053{
3054 struct input *input = data;
3055 struct touch_point *tp, *tmp;
3056
3057 DBG("touch_handle_frame\n");
3058
3059 if (!input->touch_focus) {
3060 DBG("No touch focus found for touch frame event!\n");
3061 return;
3062 }
3063
3064 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3065 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003066 (*tp->widget->touch_frame_handler)(tp->widget, input,
3067 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003068
3069 wl_list_remove(&tp->link);
3070 free(tp);
3071 }
3072}
3073
3074static void
3075touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3076{
3077 struct input *input = data;
3078 struct touch_point *tp, *tmp;
3079
3080 DBG("touch_handle_cancel\n");
3081
3082 if (!input->touch_focus) {
3083 DBG("No touch focus found for touch cancel event!\n");
3084 return;
3085 }
3086
3087 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3088 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003089 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3090 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003091
3092 wl_list_remove(&tp->link);
3093 free(tp);
3094 }
3095}
3096
3097static const struct wl_touch_listener touch_listener = {
3098 touch_handle_down,
3099 touch_handle_up,
3100 touch_handle_motion,
3101 touch_handle_frame,
3102 touch_handle_cancel,
3103};
3104
3105static void
Daniel Stone37816df2012-05-16 18:45:18 +01003106seat_handle_capabilities(void *data, struct wl_seat *seat,
3107 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003108{
Daniel Stone37816df2012-05-16 18:45:18 +01003109 struct input *input = data;
3110
3111 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3112 input->pointer = wl_seat_get_pointer(seat);
3113 wl_pointer_set_user_data(input->pointer, input);
3114 wl_pointer_add_listener(input->pointer, &pointer_listener,
3115 input);
3116 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3117 wl_pointer_destroy(input->pointer);
3118 input->pointer = NULL;
3119 }
3120
3121 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3122 input->keyboard = wl_seat_get_keyboard(seat);
3123 wl_keyboard_set_user_data(input->keyboard, input);
3124 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3125 input);
3126 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3127 wl_keyboard_destroy(input->keyboard);
3128 input->keyboard = NULL;
3129 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003130
3131 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3132 input->touch = wl_seat_get_touch(seat);
3133 wl_touch_set_user_data(input->touch, input);
3134 wl_touch_add_listener(input->touch, &touch_listener, input);
3135 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3136 wl_touch_destroy(input->touch);
3137 input->touch = NULL;
3138 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003139}
3140
Rob Bradford08031182013-08-13 20:11:03 +01003141static void
3142seat_handle_name(void *data, struct wl_seat *seat,
3143 const char *name)
3144{
3145
3146}
3147
Daniel Stone37816df2012-05-16 18:45:18 +01003148static const struct wl_seat_listener seat_listener = {
3149 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003150 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003151};
3152
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003153void
3154input_get_position(struct input *input, int32_t *x, int32_t *y)
3155{
3156 *x = input->sx;
3157 *y = input->sy;
3158}
3159
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003160int
3161input_get_touch(struct input *input, int32_t id, float *x, float *y)
3162{
3163 struct touch_point *tp;
3164
3165 wl_list_for_each(tp, &input->touch_point_list, link) {
3166 if (tp->id != id)
3167 continue;
3168
3169 *x = tp->x;
3170 *y = tp->y;
3171 return 0;
3172 }
3173
3174 return -1;
3175}
3176
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003177struct display *
3178input_get_display(struct input *input)
3179{
3180 return input->display;
3181}
3182
Daniel Stone37816df2012-05-16 18:45:18 +01003183struct wl_seat *
3184input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003185{
Daniel Stone37816df2012-05-16 18:45:18 +01003186 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003187}
3188
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003189uint32_t
3190input_get_modifiers(struct input *input)
3191{
3192 return input->modifiers;
3193}
3194
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003195struct widget *
3196input_get_focus_widget(struct input *input)
3197{
3198 return input->focus_widget;
3199}
3200
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003201struct data_offer {
3202 struct wl_data_offer *offer;
3203 struct input *input;
3204 struct wl_array types;
3205 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003206
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003207 struct task io_task;
3208 int fd;
3209 data_func_t func;
3210 int32_t x, y;
3211 void *user_data;
3212};
3213
3214static void
3215data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3216{
3217 struct data_offer *offer = data;
3218 char **p;
3219
3220 p = wl_array_add(&offer->types, sizeof *p);
3221 *p = strdup(type);
3222}
3223
3224static const struct wl_data_offer_listener data_offer_listener = {
3225 data_offer_offer,
3226};
3227
3228static void
3229data_offer_destroy(struct data_offer *offer)
3230{
3231 char **p;
3232
3233 offer->refcount--;
3234 if (offer->refcount == 0) {
3235 wl_data_offer_destroy(offer->offer);
3236 for (p = offer->types.data; *p; p++)
3237 free(*p);
3238 wl_array_release(&offer->types);
3239 free(offer);
3240 }
3241}
3242
3243static void
3244data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003245 struct wl_data_device *data_device,
3246 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003247{
3248 struct data_offer *offer;
3249
Brian Lovinbc919262013-08-07 15:34:59 -07003250 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003251
3252 wl_array_init(&offer->types);
3253 offer->refcount = 1;
3254 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003255 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003256 wl_data_offer_add_listener(offer->offer,
3257 &data_offer_listener, offer);
3258}
3259
3260static void
3261data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003262 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003263 wl_fixed_t x_w, wl_fixed_t y_w,
3264 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003265{
3266 struct input *input = data;
3267 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003268 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003269 float x = wl_fixed_to_double(x_w);
3270 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003271 char **p;
3272
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003273 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003274 input->drag_enter_serial = serial;
3275 input->drag_focus = window,
3276 input->drag_x = x;
3277 input->drag_y = y;
3278
3279 if (!input->touch_grab)
3280 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003281
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003282 if (offer) {
3283 input->drag_offer = wl_data_offer_get_user_data(offer);
3284
3285 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3286 *p = NULL;
3287
3288 types_data = input->drag_offer->types.data;
3289 } else {
3290 input->drag_offer = NULL;
3291 types_data = NULL;
3292 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003293
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003295 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003296 window->user_data);
3297}
3298
3299static void
3300data_device_leave(void *data, struct wl_data_device *data_device)
3301{
3302 struct input *input = data;
3303
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003304 if (input->drag_offer) {
3305 data_offer_destroy(input->drag_offer);
3306 input->drag_offer = NULL;
3307 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003308}
3309
3310static void
3311data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003312 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003313{
3314 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003315 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003316 float x = wl_fixed_to_double(x_w);
3317 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003318 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003319
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003320 input->drag_x = x;
3321 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003322
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003323 if (input->drag_offer)
3324 types_data = input->drag_offer->types.data;
3325 else
3326 types_data = NULL;
3327
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003328 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003329 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003330 window->user_data);
3331}
3332
3333static void
3334data_device_drop(void *data, struct wl_data_device *data_device)
3335{
3336 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003337 struct window *window = input->drag_focus;
3338 float x, y;
3339
3340 x = input->drag_x;
3341 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003342
3343 if (window->drop_handler)
3344 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003345 x, y, window->user_data);
3346
3347 if (input->touch_grab)
3348 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003349}
3350
3351static void
3352data_device_selection(void *data,
3353 struct wl_data_device *wl_data_device,
3354 struct wl_data_offer *offer)
3355{
3356 struct input *input = data;
3357 char **p;
3358
3359 if (input->selection_offer)
3360 data_offer_destroy(input->selection_offer);
3361
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003362 if (offer) {
3363 input->selection_offer = wl_data_offer_get_user_data(offer);
3364 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3365 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003366 } else {
3367 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003368 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003369}
3370
3371static const struct wl_data_device_listener data_device_listener = {
3372 data_device_data_offer,
3373 data_device_enter,
3374 data_device_leave,
3375 data_device_motion,
3376 data_device_drop,
3377 data_device_selection
3378};
3379
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003380static void
3381input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003382{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003383 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003384 struct wl_cursor *cursor;
3385 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003386
Daniel Stone80972742012-11-07 17:51:39 +11003387 if (!input->pointer)
3388 return;
3389
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003390 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003391 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003392 return;
3393
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003394 if (index >= (int) cursor->image_count) {
3395 fprintf(stderr, "cursor index out of range\n");
3396 return;
3397 }
3398
3399 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003400 buffer = wl_cursor_image_get_buffer(image);
3401 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003402 return;
3403
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003404 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3405 wl_surface_damage(input->pointer_surface, 0, 0,
3406 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003407 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003408 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3409 input->pointer_surface,
3410 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003411}
3412
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003413static const struct wl_callback_listener pointer_surface_listener;
3414
3415static void
3416pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3417 uint32_t time)
3418{
3419 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003420 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003421 int i;
3422
3423 if (callback) {
3424 assert(callback == input->cursor_frame_cb);
3425 wl_callback_destroy(callback);
3426 input->cursor_frame_cb = NULL;
3427 }
3428
Daniel Stone80972742012-11-07 17:51:39 +11003429 if (!input->pointer)
3430 return;
3431
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003432 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003433 wl_pointer_set_cursor(input->pointer,
3434 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003435 NULL, 0, 0);
3436 return;
3437 }
3438
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003439 if (input->current_cursor == CURSOR_UNSET)
3440 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003441 cursor = input->display->cursors[input->current_cursor];
3442 if (!cursor)
3443 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003444
3445 /* FIXME We don't have the current time on the first call so we set
3446 * the animation start to the time of the first frame callback. */
3447 if (time == 0)
3448 input->cursor_anim_start = 0;
3449 else if (input->cursor_anim_start == 0)
3450 input->cursor_anim_start = time;
3451
3452 if (time == 0 || input->cursor_anim_start == 0)
3453 i = 0;
3454 else
3455 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3456
Pekka Paalanenbc106382012-10-10 12:49:31 +03003457 if (cursor->image_count > 1) {
3458 input->cursor_frame_cb =
3459 wl_surface_frame(input->pointer_surface);
3460 wl_callback_add_listener(input->cursor_frame_cb,
3461 &pointer_surface_listener, input);
3462 }
3463
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003464 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003465}
3466
3467static const struct wl_callback_listener pointer_surface_listener = {
3468 pointer_surface_frame_callback
3469};
3470
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003471void
3472input_set_pointer_image(struct input *input, int pointer)
3473{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003474 int force = 0;
3475
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003476 if (!input->pointer)
3477 return;
3478
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003479 if (input->pointer_enter_serial > input->cursor_serial)
3480 force = 1;
3481
3482 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003483 return;
3484
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003485 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003486 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003487 if (!input->cursor_frame_cb)
3488 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003489 else if (force) {
3490 /* The current frame callback may be stuck if, for instance,
3491 * the set cursor request was processed by the server after
3492 * this client lost the focus. In this case the cursor surface
3493 * might not be mapped and the frame callback wouldn't ever
3494 * complete. Send a set_cursor and attach to try to map the
3495 * cursor surface again so that the callback will finish */
3496 input_set_pointer_image_index(input, 0);
3497 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003498}
3499
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003500struct wl_data_device *
3501input_get_data_device(struct input *input)
3502{
3503 return input->data_device;
3504}
3505
3506void
3507input_set_selection(struct input *input,
3508 struct wl_data_source *source, uint32_t time)
3509{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003510 if (input->data_device)
3511 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003512}
3513
3514void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003515input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003516{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003517 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003518 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003519}
3520
3521static void
3522offer_io_func(struct task *task, uint32_t events)
3523{
3524 struct data_offer *offer =
3525 container_of(task, struct data_offer, io_task);
3526 unsigned int len;
3527 char buffer[4096];
3528
3529 len = read(offer->fd, buffer, sizeof buffer);
3530 offer->func(buffer, len,
3531 offer->x, offer->y, offer->user_data);
3532
3533 if (len == 0) {
3534 close(offer->fd);
3535 data_offer_destroy(offer);
3536 }
3537}
3538
3539static void
3540data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3541 data_func_t func, void *user_data)
3542{
3543 int p[2];
3544
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003545 if (pipe2(p, O_CLOEXEC) == -1)
3546 return;
3547
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003548 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3549 close(p[1]);
3550
3551 offer->io_task.run = offer_io_func;
3552 offer->fd = p[0];
3553 offer->func = func;
3554 offer->refcount++;
3555 offer->user_data = user_data;
3556
3557 display_watch_fd(offer->input->display,
3558 offer->fd, EPOLLIN, &offer->io_task);
3559}
3560
3561void
3562input_receive_drag_data(struct input *input, const char *mime_type,
3563 data_func_t func, void *data)
3564{
3565 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003566 input->drag_offer->x = input->drag_x;
3567 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003568}
3569
3570int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003571input_receive_drag_data_to_fd(struct input *input,
3572 const char *mime_type, int fd)
3573{
3574 if (input->drag_offer)
3575 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3576
3577 return 0;
3578}
3579
3580int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003581input_receive_selection_data(struct input *input, const char *mime_type,
3582 data_func_t func, void *data)
3583{
3584 char **p;
3585
3586 if (input->selection_offer == NULL)
3587 return -1;
3588
3589 for (p = input->selection_offer->types.data; *p; p++)
3590 if (strcmp(mime_type, *p) == 0)
3591 break;
3592
3593 if (*p == NULL)
3594 return -1;
3595
3596 data_offer_receive_data(input->selection_offer,
3597 mime_type, func, data);
3598 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003599}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003600
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003601int
3602input_receive_selection_data_to_fd(struct input *input,
3603 const char *mime_type, int fd)
3604{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003605 if (input->selection_offer)
3606 wl_data_offer_receive(input->selection_offer->offer,
3607 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003608
3609 return 0;
3610}
3611
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003612void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003613window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003614{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003615 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003616 return;
3617
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003618 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003619}
3620
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003621static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003622surface_set_synchronized(struct surface *surface)
3623{
3624 if (!surface->subsurface)
3625 return;
3626
3627 if (surface->synchronized)
3628 return;
3629
3630 wl_subsurface_set_sync(surface->subsurface);
3631 surface->synchronized = 1;
3632}
3633
3634static void
3635surface_set_synchronized_default(struct surface *surface)
3636{
3637 if (!surface->subsurface)
3638 return;
3639
3640 if (surface->synchronized == surface->synchronized_default)
3641 return;
3642
3643 if (surface->synchronized_default)
3644 wl_subsurface_set_sync(surface->subsurface);
3645 else
3646 wl_subsurface_set_desync(surface->subsurface);
3647
3648 surface->synchronized = surface->synchronized_default;
3649}
3650
3651static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003652surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003653{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003654 struct widget *widget = surface->widget;
3655 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003656
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003657 if (surface->input_region) {
3658 wl_region_destroy(surface->input_region);
3659 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003660 }
3661
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003662 if (surface->opaque_region)
3663 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003664
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003665 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003666
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003667 if (widget->resize_handler)
3668 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003669 widget->allocation.width,
3670 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003671 widget->user_data);
3672
Pekka Paalanen35e82632013-04-25 13:57:48 +03003673 if (surface->subsurface &&
3674 (surface->allocation.x != widget->allocation.x ||
3675 surface->allocation.y != widget->allocation.y)) {
3676 wl_subsurface_set_position(surface->subsurface,
3677 widget->allocation.x,
3678 widget->allocation.y);
3679 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003680 if (surface->allocation.width != widget->allocation.width ||
3681 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003682 window_schedule_redraw(widget->window);
3683 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003684 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003685
3686 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003687 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003688 widget->allocation.width,
3689 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003690}
3691
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003692static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003693hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3694{
3695 /*
3696 * This hack should be removed, when EGL respects
3697 * eglSwapInterval(0).
3698 *
3699 * If this window has sub-surfaces, especially a free-running
3700 * EGL-widget, we need to post the parent surface once with
3701 * all the old state to guarantee, that the EGL-widget will
3702 * receive its frame callback soon. Otherwise, a forced call
3703 * to eglSwapBuffers may end up blocking, waiting for a frame
3704 * event that will never come, because we will commit the parent
3705 * surface with all new state only after eglSwapBuffers returns.
3706 *
3707 * This assumes, that:
3708 * 1. When the EGL widget's resize hook is called, it pauses.
3709 * 2. When the EGL widget's redraw hook is called, it forces a
3710 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3711 * In a single threaded application condition 1 is a no-op.
3712 *
3713 * XXX: This should actually be after the surface_resize() calls,
3714 * but cannot, because then it would commit the incomplete state
3715 * accumulated from the widget resize hooks.
3716 */
3717 if (window->subsurface_list.next != &window->main_surface->link ||
3718 window->subsurface_list.prev != &window->main_surface->link)
3719 wl_surface_commit(window->main_surface->surface);
3720}
3721
3722static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003723window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003724{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003725 struct surface *surface;
3726
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003727 widget_set_allocation(window->main_surface->widget,
3728 window->pending_allocation.x,
3729 window->pending_allocation.y,
3730 window->pending_allocation.width,
3731 window->pending_allocation.height);
3732
3733 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003734
3735 /* The main surface is in the list, too. Main surface's
3736 * resize_handler is responsible for calling widget_set_allocation()
3737 * on all sub-surface root widgets, so they will be resized
3738 * properly.
3739 */
3740 wl_list_for_each(surface, &window->subsurface_list, link) {
3741 if (surface == window->main_surface)
3742 continue;
3743
3744 surface_set_synchronized(surface);
3745 surface_resize(surface);
3746 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003747
3748 if (!window->fullscreen && !window->maximized)
3749 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003750}
3751
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003752static void
3753idle_resize(struct window *window)
3754{
3755 window->resize_needed = 0;
3756 window->redraw_needed = 1;
3757
3758 DBG("from %dx%d to %dx%d\n",
3759 window->main_surface->server_allocation.width,
3760 window->main_surface->server_allocation.height,
3761 window->pending_allocation.width,
3762 window->pending_allocation.height);
3763
3764 hack_prevent_EGL_sub_surface_deadlock(window);
3765
3766 window_do_resize(window);
3767}
3768
3769static void
3770undo_resize(struct window *window)
3771{
3772 window->pending_allocation.width =
3773 window->main_surface->server_allocation.width;
3774 window->pending_allocation.height =
3775 window->main_surface->server_allocation.height;
3776
3777 DBG("back to %dx%d\n",
3778 window->main_surface->server_allocation.width,
3779 window->main_surface->server_allocation.height);
3780
3781 window_do_resize(window);
3782
3783 if (window->pending_allocation.width == 0 &&
3784 window->pending_allocation.height == 0) {
3785 fprintf(stderr, "Error: Could not draw a surface, "
3786 "most likely due to insufficient disk space in "
3787 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3788 exit(EXIT_FAILURE);
3789 }
3790}
3791
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003792void
3793window_schedule_resize(struct window *window, int width, int height)
3794{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003795 /* We should probably get these numbers from the theme. */
3796 const int min_width = 200, min_height = 200;
3797
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003798 window->pending_allocation.x = 0;
3799 window->pending_allocation.y = 0;
3800 window->pending_allocation.width = width;
3801 window->pending_allocation.height = height;
3802
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003803 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003804 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003805 window->min_allocation.width = min_width;
3806 else
3807 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003808 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003809 window->min_allocation.height = min_height;
3810 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003811 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003812 }
3813
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003814 if (window->pending_allocation.width < window->min_allocation.width)
3815 window->pending_allocation.width = window->min_allocation.width;
3816 if (window->pending_allocation.height < window->min_allocation.height)
3817 window->pending_allocation.height = window->min_allocation.height;
3818
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003819 window->resize_needed = 1;
3820 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003821}
3822
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003823void
3824widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3825{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003826 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003827}
3828
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003829static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003830handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003831 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003832{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003833 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003834
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003835 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003836}
3837
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003838static void
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003839handle_surface_change_state(void *data, struct xdg_surface *xdg_surface,
3840 uint32_t state,
3841 uint32_t value,
3842 uint32_t serial)
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003843{
3844 struct window *window = data;
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003845
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003846 switch (state) {
3847 case XDG_SURFACE_STATE_MAXIMIZED:
3848 window->maximized = value;
3849 break;
3850 case XDG_SURFACE_STATE_FULLSCREEN:
3851 window->fullscreen = value;
3852 break;
3853 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003854
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003855 if (!window->fullscreen && !window->maximized)
3856 window_schedule_resize(window,
3857 window->saved_allocation.width,
3858 window->saved_allocation.height);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003859
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003860 xdg_surface_ack_change_state(xdg_surface, state, value, serial);
3861 window_schedule_redraw(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003862}
3863
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003864static void
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003865handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003866{
3867 struct window *window = data;
3868 window->focused = 1;
3869}
3870
3871static void
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003872handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003873{
3874 struct window *window = data;
3875 window->focused = 0;
3876}
3877
3878static void
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003879handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3880{
3881 struct window *window = data;
3882 window_close(window);
3883}
3884
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003885static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003886 handle_surface_configure,
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003887 handle_surface_change_state,
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003888 handle_surface_activated,
3889 handle_surface_deactivated,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003890 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003891};
3892
3893static void
Jasper St. Pierre53686042013-12-09 15:26:25 -05003894window_sync_transient_for(struct window *window)
3895{
3896 struct wl_surface *parent_surface;
3897
3898 if (!window->xdg_surface)
3899 return;
3900
3901 if (window->transient_for)
3902 parent_surface = window->transient_for->main_surface->surface;
3903 else
3904 parent_surface = NULL;
3905
3906 xdg_surface_set_transient_for(window->xdg_surface, parent_surface);
3907}
3908
3909static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003910window_sync_margin(struct window *window)
3911{
3912 int margin;
3913
3914 if (!window->xdg_surface)
3915 return;
3916
3917 if (!window->frame)
3918 return;
3919
3920 margin = frame_get_shadow_margin(window->frame->frame);
3921
3922 /* Shadow size is the same on every side. */
3923 xdg_surface_set_margin(window->xdg_surface,
3924 margin,
3925 margin,
3926 margin,
3927 margin);
3928}
3929
3930static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003931window_flush(struct window *window)
3932{
3933 struct surface *surface;
3934
3935 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003936 if (window->xdg_surface) {
Jasper St. Pierre53686042013-12-09 15:26:25 -05003937 window_sync_transient_for(window);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003938 window_sync_margin(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003939 }
3940 }
3941
3942 wl_list_for_each(surface, &window->subsurface_list, link) {
3943 if (surface == window->main_surface)
3944 continue;
3945
3946 surface_flush(surface);
3947 }
3948
3949 surface_flush(window->main_surface);
3950}
3951
3952static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003953menu_destroy(struct menu *menu)
3954{
3955 widget_destroy(menu->widget);
3956 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003957 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003958 free(menu);
3959}
3960
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003961void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003962window_get_allocation(struct window *window,
3963 struct rectangle *allocation)
3964{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003965 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003966}
3967
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003968static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003969widget_redraw(struct widget *widget)
3970{
3971 struct widget *child;
3972
3973 if (widget->redraw_handler)
3974 widget->redraw_handler(widget, widget->user_data);
3975 wl_list_for_each(child, &widget->child_list, link)
3976 widget_redraw(child);
3977}
3978
3979static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003980frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3981{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003982 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003983
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003984 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003985 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003986 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003987 surface->frame_cb = NULL;
3988
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003989 surface->last_time = time;
3990
Pekka Paalanen71233882013-04-25 13:57:53 +03003991 if (surface->redraw_needed || surface->window->redraw_needed) {
3992 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003993 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003994 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003995}
3996
3997static const struct wl_callback_listener listener = {
3998 frame_callback
3999};
4000
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004001static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004002surface_redraw(struct surface *surface)
4003{
Pekka Paalanen71233882013-04-25 13:57:53 +03004004 DBG_OBJ(surface->surface, "begin\n");
4005
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004006 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004007 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004008
4009 /* Whole-window redraw forces a redraw even if the previous has
4010 * not yet hit the screen.
4011 */
4012 if (surface->frame_cb) {
4013 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004014 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004015
Pekka Paalanen71233882013-04-25 13:57:53 +03004016 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004017 wl_callback_destroy(surface->frame_cb);
4018 }
4019
Neil Roberts97b747c2013-12-19 16:17:12 +00004020 if (surface->widget->use_cairo &&
4021 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004022 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4023 return -1;
4024 }
4025
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004026 surface->frame_cb = wl_surface_frame(surface->surface);
4027 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004028 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004029
4030 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004031 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004032 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004033 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004034 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004035}
4036
4037static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004038idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004039{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004040 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004041 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004042 int failed = 0;
4043 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004044
Pekka Paalanen71233882013-04-25 13:57:53 +03004045 DBG(" --------- \n");
4046
Pekka Paalaneneebff542013-04-25 13:57:52 +03004047 wl_list_init(&window->redraw_task.link);
4048 window->redraw_task_scheduled = 0;
4049
4050 if (window->resize_needed) {
4051 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004052 if (window->main_surface->frame_cb) {
4053 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004054 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004055 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004056
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004057 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004058 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004059 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004060
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004061 if (surface_redraw(window->main_surface) < 0) {
4062 /*
4063 * Only main_surface failure will cause us to undo the resize.
4064 * If sub-surfaces fail, they will just be broken with old
4065 * content.
4066 */
4067 failed = 1;
4068 } else {
4069 wl_list_for_each(surface, &window->subsurface_list, link) {
4070 if (surface == window->main_surface)
4071 continue;
4072
4073 surface_redraw(surface);
4074 }
4075 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004076
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004077 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004078 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004079
4080 wl_list_for_each(surface, &window->subsurface_list, link)
4081 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004082
4083 if (resized && failed) {
4084 /* Restore widget tree to correspond to what is on screen. */
4085 undo_resize(window);
4086 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004087}
4088
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004089static void
4090window_schedule_redraw_task(struct window *window)
4091{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004092 if (!window->redraw_task_scheduled) {
4093 window->redraw_task.run = idle_redraw;
4094 display_defer(window->display, &window->redraw_task);
4095 window->redraw_task_scheduled = 1;
4096 }
4097}
4098
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004099void
4100window_schedule_redraw(struct window *window)
4101{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004102 struct surface *surface;
4103
Pekka Paalanen71233882013-04-25 13:57:53 +03004104 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4105
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004106 wl_list_for_each(surface, &window->subsurface_list, link)
4107 surface->redraw_needed = 1;
4108
4109 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004110}
4111
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004112int
4113window_is_fullscreen(struct window *window)
4114{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004115 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004116}
4117
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004118void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004119window_set_fullscreen(struct window *window, int fullscreen)
4120{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004121 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004122 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004123
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004124 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004125 return;
4126
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004127 xdg_surface_request_change_state(window->xdg_surface,
4128 XDG_SURFACE_STATE_FULLSCREEN,
4129 fullscreen ? 1 : 0,
4130 0);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004131}
4132
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004133int
4134window_is_maximized(struct window *window)
4135{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004136 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004137}
4138
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004139void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004140window_set_maximized(struct window *window, int maximized)
4141{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004142 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004143 return;
4144
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004145 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004146 return;
4147
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004148 xdg_surface_request_change_state(window->xdg_surface,
4149 XDG_SURFACE_STATE_MAXIMIZED,
4150 maximized ? 1 : 0,
4151 0);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004152}
4153
4154void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004155window_set_minimized(struct window *window)
4156{
4157 if (!window->xdg_surface)
4158 return;
4159
4160 xdg_surface_set_minimized(window->xdg_surface);
4161}
4162
4163void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004164window_set_user_data(struct window *window, void *data)
4165{
4166 window->user_data = data;
4167}
4168
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004169void *
4170window_get_user_data(struct window *window)
4171{
4172 return window->user_data;
4173}
4174
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004175void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004176window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004177 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004178{
4179 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004180}
4181
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004182void
4183window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004184 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004185{
4186 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004187}
4188
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004189void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004190window_set_data_handler(struct window *window, window_data_handler_t handler)
4191{
4192 window->data_handler = handler;
4193}
4194
4195void
4196window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4197{
4198 window->drop_handler = handler;
4199}
4200
4201void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004202window_set_close_handler(struct window *window,
4203 window_close_handler_t handler)
4204{
4205 window->close_handler = handler;
4206}
4207
4208void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004209window_set_fullscreen_handler(struct window *window,
4210 window_fullscreen_handler_t handler)
4211{
4212 window->fullscreen_handler = handler;
4213}
4214
4215void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004216window_set_output_handler(struct window *window,
4217 window_output_handler_t handler)
4218{
4219 window->output_handler = handler;
4220}
4221
4222void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004223window_set_title(struct window *window, const char *title)
4224{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004225 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004226 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004227 if (window->frame) {
4228 frame_set_title(window->frame->frame, title);
4229 widget_schedule_redraw(window->frame->widget);
4230 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004231 if (window->xdg_surface)
4232 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004233}
4234
4235const char *
4236window_get_title(struct window *window)
4237{
4238 return window->title;
4239}
4240
4241void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004242window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4243{
4244 struct text_cursor_position *text_cursor_position =
4245 window->display->text_cursor_position;
4246
Scott Moreau9295ce02012-06-01 12:46:10 -06004247 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004248 return;
4249
4250 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004251 window->main_surface->surface,
4252 wl_fixed_from_int(x),
4253 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004254}
4255
4256void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004257window_damage(struct window *window, int32_t x, int32_t y,
4258 int32_t width, int32_t height)
4259{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004260 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004261}
4262
Casey Dahlin9074db52012-04-19 22:50:09 -04004263static void
4264surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004265 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004266{
Rob Bradford7507b572012-05-15 17:55:34 +01004267 struct window *window = data;
4268 struct output *output;
4269 struct output *output_found = NULL;
4270 struct window_output *window_output;
4271
4272 wl_list_for_each(output, &window->display->output_list, link) {
4273 if (output->output == wl_output) {
4274 output_found = output;
4275 break;
4276 }
4277 }
4278
4279 if (!output_found)
4280 return;
4281
Brian Lovinbc919262013-08-07 15:34:59 -07004282 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004283 window_output->output = output_found;
4284
4285 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004286
4287 if (window->output_handler)
4288 window->output_handler(window, output_found, 1,
4289 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004290}
4291
4292static void
4293surface_leave(void *data,
4294 struct wl_surface *wl_surface, struct wl_output *output)
4295{
Rob Bradford7507b572012-05-15 17:55:34 +01004296 struct window *window = data;
4297 struct window_output *window_output;
4298 struct window_output *window_output_found = NULL;
4299
4300 wl_list_for_each(window_output, &window->window_output_list, link) {
4301 if (window_output->output->output == output) {
4302 window_output_found = window_output;
4303 break;
4304 }
4305 }
4306
4307 if (window_output_found) {
4308 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004309
4310 if (window->output_handler)
4311 window->output_handler(window, window_output->output,
4312 0, window->user_data);
4313
Rob Bradford7507b572012-05-15 17:55:34 +01004314 free(window_output_found);
4315 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004316}
4317
4318static const struct wl_surface_listener surface_listener = {
4319 surface_enter,
4320 surface_leave
4321};
4322
Pekka Paalanen4e373742013-02-13 16:17:13 +02004323static struct surface *
4324surface_create(struct window *window)
4325{
4326 struct display *display = window->display;
4327 struct surface *surface;
4328
Brian Lovinbc919262013-08-07 15:34:59 -07004329 surface = xmalloc(sizeof *surface);
4330 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004331 if (!surface)
4332 return NULL;
4333
4334 surface->window = window;
4335 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004336 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004337 wl_surface_add_listener(surface->surface, &surface_listener, window);
4338
Pekka Paalanen35e82632013-04-25 13:57:48 +03004339 wl_list_insert(&window->subsurface_list, &surface->link);
4340
Pekka Paalanen4e373742013-02-13 16:17:13 +02004341 return surface;
4342}
4343
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004344static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004345window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004346{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004347 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004348 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004349
Peter Huttererf3d62272013-08-08 11:57:05 +10004350 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004351 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004352 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004353
4354 surface = surface_create(window);
4355 window->main_surface = surface;
4356
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004357 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004358
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004359 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004360 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004361
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004362 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004363#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004364 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004365#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004366 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004367#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004368 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004369 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004370
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004371 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004372 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004373 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004374
Rob Bradford7507b572012-05-15 17:55:34 +01004375 wl_list_init (&window->window_output_list);
4376
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004377 return window;
4378}
4379
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004380struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004381window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004382{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004383 struct window *window;
4384
4385 window = window_create_internal(display, 0);
4386
4387 window->xdg_surface =
4388 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4389 window->main_surface->surface);
4390 fail_on_null(window->xdg_surface);
4391
4392 xdg_surface_set_user_data(window->xdg_surface, window);
4393 xdg_surface_add_listener(window->xdg_surface,
4394 &xdg_surface_listener, window);
4395
4396 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004397}
4398
4399struct window *
4400window_create_custom(struct display *display)
4401{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004402 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004403}
4404
Jasper St. Pierre53686042013-12-09 15:26:25 -05004405void
4406window_set_transient_for(struct window *window,
4407 struct window *parent_window)
4408{
4409 window->transient_for = parent_window;
4410 window_sync_transient_for(window);
4411}
4412
4413struct window *
4414window_get_transient_for(struct window *window)
4415{
4416 return window->transient_for;
4417}
4418
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004419static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004420menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004421{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004422 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004423 int next;
4424
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004425 frame_interior(menu->frame, &x, &y, &width, &height);
4426 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004427 if (menu->current != next) {
4428 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004429 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004430 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004431}
4432
4433static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004434menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004435 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004436 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004437{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004438 struct menu *menu = data;
4439
4440 if (widget == menu->widget)
4441 menu_set_item(data, y);
4442
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004443 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004444}
4445
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004446static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004447menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004448 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004449{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004450 struct menu *menu = data;
4451
4452 if (widget == menu->widget)
4453 menu_set_item(data, y);
4454
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004455 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004456}
4457
4458static void
4459menu_leave_handler(struct widget *widget, struct input *input, void *data)
4460{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004461 struct menu *menu = data;
4462
4463 if (widget == menu->widget)
4464 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004465}
4466
4467static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004468menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004469 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004470 uint32_t button, enum wl_pointer_button_state state,
4471 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004472
4473{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004474 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004475
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004476 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4477 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004478 /* Either relase after press-drag-release or
4479 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004480 menu->func(menu->parent, input,
4481 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004482 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004483 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004484 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004485 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004486 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004487}
4488
4489static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004490menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004491{
4492 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004493 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004494 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004495
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004496 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004497
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004498 frame_repaint(menu->frame, cr);
4499 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004500
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004501 theme_set_background_source(menu->window->display->theme,
4502 cr, THEME_FRAME_ACTIVE);
4503 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004504 cairo_fill(cr);
4505
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004506 cairo_select_font_face(cr, "sans",
4507 CAIRO_FONT_SLANT_NORMAL,
4508 CAIRO_FONT_WEIGHT_NORMAL);
4509 cairo_set_font_size(cr, 12);
4510
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004511 for (i = 0; i < menu->count; i++) {
4512 if (i == menu->current) {
4513 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004514 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004515 cairo_fill(cr);
4516 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004517 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004518 cairo_show_text(cr, menu->entries[i]);
4519 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004520 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4521 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004522 cairo_show_text(cr, menu->entries[i]);
4523 }
4524 }
4525
4526 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004527}
4528
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004529static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004530handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4531{
4532 struct window *window = data;
4533 struct menu *menu = window->main_surface->widget->user_data;
4534
4535 input_ungrab(menu->input);
4536 menu_destroy(menu);
4537}
4538
4539static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004540 handle_popup_popup_done,
4541};
4542
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004543void
4544window_show_menu(struct display *display,
4545 struct input *input, uint32_t time, struct window *parent,
4546 int32_t x, int32_t y,
4547 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004548{
4549 struct window *window;
4550 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004551 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004552
4553 menu = malloc(sizeof *menu);
4554 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004555 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004556
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004557 window = window_create_internal(parent->display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004558 if (!window) {
4559 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004560 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004561 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004562
4563 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004564 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004565 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004566 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4567 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004568 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004569 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004570 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004571 menu->entries = entries;
4572 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004573 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004574 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004575 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004576 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004577 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004578 window->x = x;
4579 window->y = y;
4580
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004581 input_ungrab(input);
4582
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004583 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004584 widget_set_enter_handler(menu->widget, menu_enter_handler);
4585 widget_set_leave_handler(menu->widget, menu_leave_handler);
4586 widget_set_motion_handler(menu->widget, menu_motion_handler);
4587 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004588
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004589 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004590 frame_resize_inside(menu->frame, 200, count * 20);
4591 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4592 window_schedule_resize(window, frame_width(menu->frame),
4593 frame_height(menu->frame));
4594
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004595 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004596
4597 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4598 window->main_surface->surface,
4599 parent->main_surface->surface,
4600 input->seat,
4601 display_get_serial(window->display),
4602 window->x - ix,
4603 window->y - iy,
4604 0);
4605 fail_on_null(window->xdg_popup);
4606
4607 xdg_popup_set_user_data(window->xdg_popup, window);
4608 xdg_popup_add_listener(window->xdg_popup,
4609 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004610}
4611
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004612void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004613window_set_buffer_type(struct window *window, enum window_buffer_type type)
4614{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004615 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004616}
4617
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004618enum window_buffer_type
4619window_get_buffer_type(struct window *window)
4620{
4621 return window->main_surface->buffer_type;
4622}
4623
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004624void
4625window_set_preferred_format(struct window *window,
4626 enum preferred_format format)
4627{
4628 window->preferred_format = format;
4629}
4630
Pekka Paalanen35e82632013-04-25 13:57:48 +03004631struct widget *
4632window_add_subsurface(struct window *window, void *data,
4633 enum subsurface_mode default_mode)
4634{
4635 struct widget *widget;
4636 struct surface *surface;
4637 struct wl_surface *parent;
4638 struct wl_subcompositor *subcompo = window->display->subcompositor;
4639
Pekka Paalanen35e82632013-04-25 13:57:48 +03004640 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004641 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004642 widget = widget_create(window, surface, data);
4643 wl_list_init(&widget->link);
4644 surface->widget = widget;
4645
4646 parent = window->main_surface->surface;
4647 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4648 surface->surface,
4649 parent);
4650 surface->synchronized = 1;
4651
4652 switch (default_mode) {
4653 case SUBSURFACE_SYNCHRONIZED:
4654 surface->synchronized_default = 1;
4655 break;
4656 case SUBSURFACE_DESYNCHRONIZED:
4657 surface->synchronized_default = 0;
4658 break;
4659 default:
4660 assert(!"bad enum subsurface_mode");
4661 }
4662
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004663 window->resize_needed = 1;
4664 window_schedule_redraw(window);
4665
Pekka Paalanen35e82632013-04-25 13:57:48 +03004666 return widget;
4667}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004668
4669static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004670display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004671 struct wl_output *wl_output,
4672 int x, int y,
4673 int physical_width,
4674 int physical_height,
4675 int subpixel,
4676 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004677 const char *model,
4678 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004679{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004680 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004681
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004682 output->allocation.x = x;
4683 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004684 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004685
4686 if (output->make)
4687 free(output->make);
4688 output->make = strdup(make);
4689
4690 if (output->model)
4691 free(output->model);
4692 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004693}
4694
4695static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004696display_handle_done(void *data,
4697 struct wl_output *wl_output)
4698{
4699}
4700
4701static void
4702display_handle_scale(void *data,
4703 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004704 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004705{
4706 struct output *output = data;
4707
4708 output->scale = scale;
4709}
4710
4711static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004712display_handle_mode(void *data,
4713 struct wl_output *wl_output,
4714 uint32_t flags,
4715 int width,
4716 int height,
4717 int refresh)
4718{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004719 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004720 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004721
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004722 if (flags & WL_OUTPUT_MODE_CURRENT) {
4723 output->allocation.width = width;
4724 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004725 if (display->output_configure_handler)
4726 (*display->output_configure_handler)(
4727 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004728 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004729}
4730
4731static const struct wl_output_listener output_listener = {
4732 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004733 display_handle_mode,
4734 display_handle_done,
4735 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004736};
4737
4738static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004739display_add_output(struct display *d, uint32_t id)
4740{
4741 struct output *output;
4742
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004743 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004744 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004745 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004746 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004747 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004748 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004749 wl_list_insert(d->output_list.prev, &output->link);
4750
4751 wl_output_add_listener(output->output, &output_listener, output);
4752}
4753
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004754static void
4755output_destroy(struct output *output)
4756{
4757 if (output->destroy_handler)
4758 (*output->destroy_handler)(output, output->user_data);
4759
4760 wl_output_destroy(output->output);
4761 wl_list_remove(&output->link);
4762 free(output);
4763}
4764
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004765static void
4766display_destroy_output(struct display *d, uint32_t id)
4767{
4768 struct output *output;
4769
4770 wl_list_for_each(output, &d->output_list, link) {
4771 if (output->server_output_id == id) {
4772 output_destroy(output);
4773 break;
4774 }
4775 }
4776}
4777
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004778void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004779display_set_global_handler(struct display *display,
4780 display_global_handler_t handler)
4781{
4782 struct global *global;
4783
4784 display->global_handler = handler;
4785 if (!handler)
4786 return;
4787
4788 wl_list_for_each(global, &display->global_list, link)
4789 display->global_handler(display,
4790 global->name, global->interface,
4791 global->version, display->user_data);
4792}
4793
4794void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004795display_set_global_handler_remove(struct display *display,
4796 display_global_handler_t remove_handler)
4797{
4798 display->global_handler_remove = remove_handler;
4799 if (!remove_handler)
4800 return;
4801}
4802
4803void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004804display_set_output_configure_handler(struct display *display,
4805 display_output_handler_t handler)
4806{
4807 struct output *output;
4808
4809 display->output_configure_handler = handler;
4810 if (!handler)
4811 return;
4812
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004813 wl_list_for_each(output, &display->output_list, link) {
4814 if (output->allocation.width == 0 &&
4815 output->allocation.height == 0)
4816 continue;
4817
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004818 (*display->output_configure_handler)(output,
4819 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004820 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004821}
4822
4823void
4824output_set_user_data(struct output *output, void *data)
4825{
4826 output->user_data = data;
4827}
4828
4829void *
4830output_get_user_data(struct output *output)
4831{
4832 return output->user_data;
4833}
4834
4835void
4836output_set_destroy_handler(struct output *output,
4837 display_output_handler_t handler)
4838{
4839 output->destroy_handler = handler;
4840 /* FIXME: implement this, once we have way to remove outputs */
4841}
4842
4843void
Scott Moreau4e072362012-09-29 02:03:11 -06004844output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004845{
Scott Moreau4e072362012-09-29 02:03:11 -06004846 struct rectangle allocation = output->allocation;
4847
4848 switch (output->transform) {
4849 case WL_OUTPUT_TRANSFORM_90:
4850 case WL_OUTPUT_TRANSFORM_270:
4851 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4852 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4853 /* Swap width and height */
4854 allocation.width = output->allocation.height;
4855 allocation.height = output->allocation.width;
4856 break;
4857 }
4858
4859 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004860}
4861
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004862struct wl_output *
4863output_get_wl_output(struct output *output)
4864{
4865 return output->output;
4866}
4867
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004868enum wl_output_transform
4869output_get_transform(struct output *output)
4870{
4871 return output->transform;
4872}
4873
Alexander Larssonafd319a2013-05-22 14:41:27 +02004874uint32_t
4875output_get_scale(struct output *output)
4876{
4877 return output->scale;
4878}
4879
Jason Ekstrand738715d2014-04-02 19:53:50 -05004880const char *
4881output_get_make(struct output *output)
4882{
4883 return output->make;
4884}
4885
4886const char *
4887output_get_model(struct output *output)
4888{
4889 return output->model;
4890}
4891
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004892static void
Daniel Stone97f68542012-05-30 16:32:01 +01004893fini_xkb(struct input *input)
4894{
4895 xkb_state_unref(input->xkb.state);
4896 xkb_map_unref(input->xkb.keymap);
4897}
4898
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004899#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004900
Daniel Stone97f68542012-05-30 16:32:01 +01004901static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004902display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004903{
4904 struct input *input;
4905
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004906 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004907 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004908 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004909 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004910 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004911 input->pointer_focus = NULL;
4912 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004913 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004914 wl_list_insert(d->input_list.prev, &input->link);
4915
Daniel Stone37816df2012-05-16 18:45:18 +01004916 wl_seat_add_listener(input->seat, &seat_listener, input);
4917 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004918
Jason Ekstranda669bd52014-04-02 19:53:51 -05004919 if (d->data_device_manager) {
4920 input->data_device =
4921 wl_data_device_manager_get_data_device(d->data_device_manager,
4922 input->seat);
4923 wl_data_device_add_listener(input->data_device,
4924 &data_device_listener,
4925 input);
4926 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004927
4928 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004929
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004930 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4931 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004932 input->repeat_task.run = keyboard_repeat_func;
4933 display_watch_fd(d, input->repeat_timer_fd,
4934 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004935}
4936
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004937static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004938input_destroy(struct input *input)
4939{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004940 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004941 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004942
4943 if (input->drag_offer)
4944 data_offer_destroy(input->drag_offer);
4945
4946 if (input->selection_offer)
4947 data_offer_destroy(input->selection_offer);
4948
Jason Ekstranda669bd52014-04-02 19:53:51 -05004949 if (input->data_device)
4950 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004951
4952 if (input->display->seat_version >= 3) {
4953 if (input->pointer)
4954 wl_pointer_release(input->pointer);
4955 if (input->keyboard)
4956 wl_keyboard_release(input->keyboard);
4957 }
4958
Daniel Stone97f68542012-05-30 16:32:01 +01004959 fini_xkb(input);
4960
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004961 wl_surface_destroy(input->pointer_surface);
4962
Pekka Paalanene1207c72011-12-16 12:02:09 +02004963 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004964 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004965 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004966 free(input);
4967}
4968
4969static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004970init_workspace_manager(struct display *d, uint32_t id)
4971{
4972 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004973 wl_registry_bind(d->registry, id,
4974 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004975 if (d->workspace_manager != NULL)
4976 workspace_manager_add_listener(d->workspace_manager,
4977 &workspace_manager_listener,
4978 d);
4979}
4980
4981static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004982shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4983{
4984 struct display *d = data;
4985
4986 if (format == WL_SHM_FORMAT_RGB565)
4987 d->has_rgb565 = 1;
4988}
4989
4990struct wl_shm_listener shm_listener = {
4991 shm_format
4992};
4993
4994static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004995xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
4996{
4997 xdg_shell_pong(shell, serial);
4998}
4999
5000static const struct xdg_shell_listener xdg_shell_listener = {
5001 xdg_shell_ping,
5002};
5003
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08005004#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005005#ifdef static_assert
5006static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5007 "Interface version doesn't match implementation version");
5008#endif
5009
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005010static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005011registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5012 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005013{
5014 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005015 struct global *global;
5016
Brian Lovinbc919262013-08-07 15:34:59 -07005017 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005018 global->name = id;
5019 global->interface = strdup(interface);
5020 global->version = version;
5021 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005022
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005023 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005024 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005025 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005026 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005027 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005028 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005029 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005030 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005031 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005032 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005033 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005034 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5035 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005036 wl_registry_bind(registry, id,
5037 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005038 } else if (strcmp(interface, "xdg_shell") == 0) {
5039 d->xdg_shell = wl_registry_bind(registry, id,
5040 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005041 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005042 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005043 } else if (strcmp(interface, "text_cursor_position") == 0) {
5044 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005045 wl_registry_bind(registry, id,
5046 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005047 } else if (strcmp(interface, "workspace_manager") == 0) {
5048 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005049 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5050 d->subcompositor =
5051 wl_registry_bind(registry, id,
5052 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005053 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005054
5055 if (d->global_handler)
5056 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005057}
5058
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005059static void
5060registry_handle_global_remove(void *data, struct wl_registry *registry,
5061 uint32_t name)
5062{
5063 struct display *d = data;
5064 struct global *global;
5065 struct global *tmp;
5066
5067 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5068 if (global->name != name)
5069 continue;
5070
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005071 if (strcmp(global->interface, "wl_output") == 0)
5072 display_destroy_output(d, name);
5073
5074 /* XXX: Should destroy remaining bound globals */
5075
5076 if (d->global_handler_remove)
5077 d->global_handler_remove(d, name, global->interface,
5078 global->version, d->user_data);
5079
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005080 wl_list_remove(&global->link);
5081 free(global->interface);
5082 free(global);
5083 }
5084}
5085
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005086void *
5087display_bind(struct display *display, uint32_t name,
5088 const struct wl_interface *interface, uint32_t version)
5089{
5090 return wl_registry_bind(display->registry, name, interface, version);
5091}
5092
5093static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005094 registry_handle_global,
5095 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005096};
5097
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005098#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005099static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005100init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005101{
5102 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005103 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005104
Rob Clark6396ed32012-03-11 19:48:41 -05005105#ifdef USE_CAIRO_GLESV2
5106# define GL_BIT EGL_OPENGL_ES2_BIT
5107#else
5108# define GL_BIT EGL_OPENGL_BIT
5109#endif
5110
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005111 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005112 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005113 EGL_RED_SIZE, 1,
5114 EGL_GREEN_SIZE, 1,
5115 EGL_BLUE_SIZE, 1,
5116 EGL_ALPHA_SIZE, 1,
5117 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005118 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005119 EGL_NONE
5120 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005121
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005122#ifdef USE_CAIRO_GLESV2
5123 static const EGLint context_attribs[] = {
5124 EGL_CONTEXT_CLIENT_VERSION, 2,
5125 EGL_NONE
5126 };
5127 EGLint api = EGL_OPENGL_ES_API;
5128#else
5129 EGLint *context_attribs = NULL;
5130 EGLint api = EGL_OPENGL_API;
5131#endif
5132
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005133 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005134 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005135 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005136 return -1;
5137 }
5138
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005139 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005140 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005141 return -1;
5142 }
5143
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005144 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5145 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005146 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005147 return -1;
5148 }
5149
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005150 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005151 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005152 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005153 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005154 return -1;
5155 }
5156
Benjamin Franzke0c991632011-09-27 21:57:31 +02005157 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5158 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005159 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005160 return -1;
5161 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005162
5163 return 0;
5164}
5165
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005166static void
5167fini_egl(struct display *display)
5168{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005169 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005170
5171 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5172 EGL_NO_CONTEXT);
5173
5174 eglTerminate(display->dpy);
5175 eglReleaseThread();
5176}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005177#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005178
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005179static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005180init_dummy_surface(struct display *display)
5181{
5182 int len;
5183 void *data;
5184
5185 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5186 data = malloc(len);
5187 display->dummy_surface =
5188 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5189 1, 1, len);
5190 display->dummy_surface_data = data;
5191}
5192
5193static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005194handle_display_data(struct task *task, uint32_t events)
5195{
5196 struct display *display =
5197 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005198 struct epoll_event ep;
5199 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005200
5201 display->display_fd_events = events;
5202
5203 if (events & EPOLLERR || events & EPOLLHUP) {
5204 display_exit(display);
5205 return;
5206 }
5207
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005208 if (events & EPOLLIN) {
5209 ret = wl_display_dispatch(display->display);
5210 if (ret == -1) {
5211 display_exit(display);
5212 return;
5213 }
5214 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005215
5216 if (events & EPOLLOUT) {
5217 ret = wl_display_flush(display->display);
5218 if (ret == 0) {
5219 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5220 ep.data.ptr = &display->display_task;
5221 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5222 display->display_fd, &ep);
5223 } else if (ret == -1 && errno != EAGAIN) {
5224 display_exit(display);
5225 return;
5226 }
5227 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005228}
5229
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005230static void
5231log_handler(const char *format, va_list args)
5232{
5233 vfprintf(stderr, format, args);
5234}
5235
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005236struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005237display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005238{
5239 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005240
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005241 wl_log_set_handler_client(log_handler);
5242
Peter Huttererf3d62272013-08-08 11:57:05 +10005243 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005244 if (d == NULL)
5245 return NULL;
5246
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005247 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005248 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005249 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005250 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005251 return NULL;
5252 }
5253
Rob Bradford5ab9c752013-07-26 16:29:43 +01005254 d->xkb_context = xkb_context_new(0);
5255 if (d->xkb_context == NULL) {
5256 fprintf(stderr, "Failed to create XKB context\n");
5257 free(d);
5258 return NULL;
5259 }
5260
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005261 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005262 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005263 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005264 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5265 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005266
5267 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005268 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005269 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005270 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005271
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005272 d->workspace = 0;
5273 d->workspace_count = 1;
5274
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005275 d->registry = wl_display_get_registry(d->display);
5276 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005277
5278 if (wl_display_dispatch(d->display) < 0) {
5279 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5280 return NULL;
5281 }
5282
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005283#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005284 if (init_egl(d) < 0)
5285 fprintf(stderr, "EGL does not seem to work, "
5286 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005287#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005288
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005289 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005290
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005291 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005292
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005293 wl_list_init(&d->window_list);
5294
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005295 init_dummy_surface(d);
5296
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005297 return d;
5298}
5299
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005300static void
5301display_destroy_outputs(struct display *display)
5302{
5303 struct output *tmp;
5304 struct output *output;
5305
5306 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5307 output_destroy(output);
5308}
5309
Pekka Paalanene1207c72011-12-16 12:02:09 +02005310static void
5311display_destroy_inputs(struct display *display)
5312{
5313 struct input *tmp;
5314 struct input *input;
5315
5316 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5317 input_destroy(input);
5318}
5319
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005320void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005321display_destroy(struct display *display)
5322{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005323 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005324 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5325 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005326
5327 if (!wl_list_empty(&display->deferred_list))
5328 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5329
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005330 cairo_surface_destroy(display->dummy_surface);
5331 free(display->dummy_surface_data);
5332
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005333 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005334 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005335
Daniel Stone97f68542012-05-30 16:32:01 +01005336 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005337
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005338 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005339 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005340
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005341#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005342 if (display->argb_device)
5343 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005344#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005345
Pekka Paalanen35e82632013-04-25 13:57:48 +03005346 if (display->subcompositor)
5347 wl_subcompositor_destroy(display->subcompositor);
5348
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005349 if (display->xdg_shell)
5350 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005351
5352 if (display->shm)
5353 wl_shm_destroy(display->shm);
5354
5355 if (display->data_device_manager)
5356 wl_data_device_manager_destroy(display->data_device_manager);
5357
5358 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005359 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005360
5361 close(display->epoll_fd);
5362
U. Artie Eoff44874d92012-10-02 21:12:35 -07005363 if (!(display->display_fd_events & EPOLLERR) &&
5364 !(display->display_fd_events & EPOLLHUP))
5365 wl_display_flush(display->display);
5366
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005367 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005368 free(display);
5369}
5370
5371void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005372display_set_user_data(struct display *display, void *data)
5373{
5374 display->user_data = data;
5375}
5376
5377void *
5378display_get_user_data(struct display *display)
5379{
5380 return display->user_data;
5381}
5382
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005383struct wl_display *
5384display_get_display(struct display *display)
5385{
5386 return display->display;
5387}
5388
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005389int
5390display_has_subcompositor(struct display *display)
5391{
5392 if (display->subcompositor)
5393 return 1;
5394
5395 wl_display_roundtrip(display->display);
5396
5397 return display->subcompositor != NULL;
5398}
5399
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005400cairo_device_t *
5401display_get_cairo_device(struct display *display)
5402{
5403 return display->argb_device;
5404}
5405
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005406struct output *
5407display_get_output(struct display *display)
5408{
5409 return container_of(display->output_list.next, struct output, link);
5410}
5411
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005412struct wl_compositor *
5413display_get_compositor(struct display *display)
5414{
5415 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005416}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005417
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005418uint32_t
5419display_get_serial(struct display *display)
5420{
5421 return display->serial;
5422}
5423
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005424EGLDisplay
5425display_get_egl_display(struct display *d)
5426{
5427 return d->dpy;
5428}
5429
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005430struct wl_data_source *
5431display_create_data_source(struct display *display)
5432{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005433 if (display->data_device_manager)
5434 return wl_data_device_manager_create_data_source(display->data_device_manager);
5435 else
5436 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005437}
5438
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005439EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005440display_get_argb_egl_config(struct display *d)
5441{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005442 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005443}
5444
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005445int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005446display_acquire_window_surface(struct display *display,
5447 struct window *window,
5448 EGLContext ctx)
5449{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005450 struct surface *surface = window->main_surface;
5451
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005452 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005453 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005454
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005455 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005456 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005457}
5458
5459void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005460display_release_window_surface(struct display *display,
5461 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005462{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005463 struct surface *surface = window->main_surface;
5464
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005465 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005466 return;
5467
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005468 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005469}
5470
5471void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005472display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005473{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005474 wl_list_insert(&display->deferred_list, &task->link);
5475}
5476
5477void
5478display_watch_fd(struct display *display,
5479 int fd, uint32_t events, struct task *task)
5480{
5481 struct epoll_event ep;
5482
5483 ep.events = events;
5484 ep.data.ptr = task;
5485 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5486}
5487
5488void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005489display_unwatch_fd(struct display *display, int fd)
5490{
5491 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5492}
5493
5494void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005495display_run(struct display *display)
5496{
5497 struct task *task;
5498 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005499 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005500
Pekka Paalanen826d7952011-12-15 10:14:07 +02005501 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005502 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005503 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005504 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005505 struct task, link);
5506 wl_list_remove(&task->link);
5507 task->run(task, 0);
5508 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005509
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005510 wl_display_dispatch_pending(display->display);
5511
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005512 if (!display->running)
5513 break;
5514
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005515 ret = wl_display_flush(display->display);
5516 if (ret < 0 && errno == EAGAIN) {
5517 ep[0].events =
5518 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5519 ep[0].data.ptr = &display->display_task;
5520
5521 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5522 display->display_fd, &ep[0]);
5523 } else if (ret < 0) {
5524 break;
5525 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005526
5527 count = epoll_wait(display->epoll_fd,
5528 ep, ARRAY_LENGTH(ep), -1);
5529 for (i = 0; i < count; i++) {
5530 task = ep[i].data.ptr;
5531 task->run(task, ep[i].events);
5532 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005533 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005534}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005535
5536void
5537display_exit(struct display *display)
5538{
5539 display->running = 0;
5540}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005541
5542void
5543keysym_modifiers_add(struct wl_array *modifiers_map,
5544 const char *name)
5545{
5546 size_t len = strlen(name) + 1;
5547 char *p;
5548
5549 p = wl_array_add(modifiers_map, len);
5550
5551 if (p == NULL)
5552 return;
5553
5554 strncpy(p, name, len);
5555}
5556
5557static xkb_mod_index_t
5558keysym_modifiers_get_index(struct wl_array *modifiers_map,
5559 const char *name)
5560{
5561 xkb_mod_index_t index = 0;
5562 char *p = modifiers_map->data;
5563
5564 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5565 if (strcmp(p, name) == 0)
5566 return index;
5567
5568 index++;
5569 p += strlen(p) + 1;
5570 }
5571
5572 return XKB_MOD_INVALID;
5573}
5574
5575xkb_mod_mask_t
5576keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5577 const char *name)
5578{
5579 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5580
5581 if (index == XKB_MOD_INVALID)
5582 return XKB_MOD_INVALID;
5583
5584 return 1 << index;
5585}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005586
5587void *
5588fail_on_null(void *p)
5589{
5590 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005591 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005592 exit(EXIT_FAILURE);
5593 }
5594
5595 return p;
5596}
5597
5598void *
5599xmalloc(size_t s)
5600{
5601 return fail_on_null(malloc(s));
5602}
5603
Peter Huttererf3d62272013-08-08 11:57:05 +10005604void *
5605xzalloc(size_t s)
5606{
5607 return fail_on_null(zalloc(s));
5608}
5609
Kristian Høgsbergce278412013-07-25 15:20:20 -07005610char *
5611xstrdup(const char *s)
5612{
5613 return fail_on_null(strdup(s));
5614}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005615
5616void *
5617xrealloc(char *p, size_t s)
5618{
5619 return fail_on_null(realloc(p, s));
5620}