blob: 8393424938a5d7ead3d6896504909682fe950b49 [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) {
3936 if (!window->xdg_popup && !window->xdg_surface) {
3937 window->xdg_surface = xdg_shell_get_xdg_surface(window->display->xdg_shell,
3938 window->main_surface->surface);
3939 fail_on_null(window->xdg_surface);
3940
3941 xdg_surface_set_user_data(window->xdg_surface, window);
3942 xdg_surface_add_listener(window->xdg_surface,
3943 &xdg_surface_listener, window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05003944
3945 window_sync_transient_for(window);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003946 window_sync_margin(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003947 }
3948 }
3949
3950 wl_list_for_each(surface, &window->subsurface_list, link) {
3951 if (surface == window->main_surface)
3952 continue;
3953
3954 surface_flush(surface);
3955 }
3956
3957 surface_flush(window->main_surface);
3958}
3959
3960static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003961menu_destroy(struct menu *menu)
3962{
3963 widget_destroy(menu->widget);
3964 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003965 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003966 free(menu);
3967}
3968
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003969void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003970window_get_allocation(struct window *window,
3971 struct rectangle *allocation)
3972{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003973 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003974}
3975
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003976static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003977widget_redraw(struct widget *widget)
3978{
3979 struct widget *child;
3980
3981 if (widget->redraw_handler)
3982 widget->redraw_handler(widget, widget->user_data);
3983 wl_list_for_each(child, &widget->child_list, link)
3984 widget_redraw(child);
3985}
3986
3987static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003988frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3989{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003990 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003991
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003992 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003993 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003994 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003995 surface->frame_cb = NULL;
3996
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003997 surface->last_time = time;
3998
Pekka Paalanen71233882013-04-25 13:57:53 +03003999 if (surface->redraw_needed || surface->window->redraw_needed) {
4000 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004001 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004002 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004003}
4004
4005static const struct wl_callback_listener listener = {
4006 frame_callback
4007};
4008
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004009static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004010surface_redraw(struct surface *surface)
4011{
Pekka Paalanen71233882013-04-25 13:57:53 +03004012 DBG_OBJ(surface->surface, "begin\n");
4013
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004014 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004015 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004016
4017 /* Whole-window redraw forces a redraw even if the previous has
4018 * not yet hit the screen.
4019 */
4020 if (surface->frame_cb) {
4021 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004022 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004023
Pekka Paalanen71233882013-04-25 13:57:53 +03004024 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004025 wl_callback_destroy(surface->frame_cb);
4026 }
4027
Neil Roberts97b747c2013-12-19 16:17:12 +00004028 if (surface->widget->use_cairo &&
4029 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004030 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4031 return -1;
4032 }
4033
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004034 surface->frame_cb = wl_surface_frame(surface->surface);
4035 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004036 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004037
4038 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004039 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004040 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004041 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004042 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004043}
4044
4045static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004046idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004047{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004048 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004049 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004050 int failed = 0;
4051 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004052
Pekka Paalanen71233882013-04-25 13:57:53 +03004053 DBG(" --------- \n");
4054
Pekka Paalaneneebff542013-04-25 13:57:52 +03004055 wl_list_init(&window->redraw_task.link);
4056 window->redraw_task_scheduled = 0;
4057
4058 if (window->resize_needed) {
4059 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004060 if (window->main_surface->frame_cb) {
4061 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004062 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004063 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004064
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004065 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004066 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004067 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004068
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004069 if (surface_redraw(window->main_surface) < 0) {
4070 /*
4071 * Only main_surface failure will cause us to undo the resize.
4072 * If sub-surfaces fail, they will just be broken with old
4073 * content.
4074 */
4075 failed = 1;
4076 } else {
4077 wl_list_for_each(surface, &window->subsurface_list, link) {
4078 if (surface == window->main_surface)
4079 continue;
4080
4081 surface_redraw(surface);
4082 }
4083 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004084
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004085 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004086 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004087
4088 wl_list_for_each(surface, &window->subsurface_list, link)
4089 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004090
4091 if (resized && failed) {
4092 /* Restore widget tree to correspond to what is on screen. */
4093 undo_resize(window);
4094 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004095}
4096
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004097static void
4098window_schedule_redraw_task(struct window *window)
4099{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004100 if (!window->redraw_task_scheduled) {
4101 window->redraw_task.run = idle_redraw;
4102 display_defer(window->display, &window->redraw_task);
4103 window->redraw_task_scheduled = 1;
4104 }
4105}
4106
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004107void
4108window_schedule_redraw(struct window *window)
4109{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004110 struct surface *surface;
4111
Pekka Paalanen71233882013-04-25 13:57:53 +03004112 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4113
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004114 wl_list_for_each(surface, &window->subsurface_list, link)
4115 surface->redraw_needed = 1;
4116
4117 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004118}
4119
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004120int
4121window_is_fullscreen(struct window *window)
4122{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004123 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004124}
4125
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004126void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004127window_set_fullscreen(struct window *window, int fullscreen)
4128{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004129 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004130 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004131
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004132 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004133 return;
4134
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004135 xdg_surface_request_change_state(window->xdg_surface,
4136 XDG_SURFACE_STATE_FULLSCREEN,
4137 fullscreen ? 1 : 0,
4138 0);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004139}
4140
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004141int
4142window_is_maximized(struct window *window)
4143{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004144 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004145}
4146
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004147void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004148window_set_maximized(struct window *window, int maximized)
4149{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004150 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004151 return;
4152
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004153 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004154 return;
4155
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05004156 xdg_surface_request_change_state(window->xdg_surface,
4157 XDG_SURFACE_STATE_MAXIMIZED,
4158 maximized ? 1 : 0,
4159 0);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004160}
4161
4162void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004163window_set_minimized(struct window *window)
4164{
4165 if (!window->xdg_surface)
4166 return;
4167
4168 xdg_surface_set_minimized(window->xdg_surface);
4169}
4170
4171void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004172window_set_user_data(struct window *window, void *data)
4173{
4174 window->user_data = data;
4175}
4176
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004177void *
4178window_get_user_data(struct window *window)
4179{
4180 return window->user_data;
4181}
4182
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004183void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004184window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004185 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004186{
4187 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004188}
4189
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004190void
4191window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004192 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004193{
4194 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004195}
4196
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004197void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004198window_set_data_handler(struct window *window, window_data_handler_t handler)
4199{
4200 window->data_handler = handler;
4201}
4202
4203void
4204window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4205{
4206 window->drop_handler = handler;
4207}
4208
4209void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004210window_set_close_handler(struct window *window,
4211 window_close_handler_t handler)
4212{
4213 window->close_handler = handler;
4214}
4215
4216void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004217window_set_fullscreen_handler(struct window *window,
4218 window_fullscreen_handler_t handler)
4219{
4220 window->fullscreen_handler = handler;
4221}
4222
4223void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004224window_set_output_handler(struct window *window,
4225 window_output_handler_t handler)
4226{
4227 window->output_handler = handler;
4228}
4229
4230void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004231window_set_title(struct window *window, const char *title)
4232{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004233 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004234 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004235 if (window->frame) {
4236 frame_set_title(window->frame->frame, title);
4237 widget_schedule_redraw(window->frame->widget);
4238 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004239 if (window->xdg_surface)
4240 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004241}
4242
4243const char *
4244window_get_title(struct window *window)
4245{
4246 return window->title;
4247}
4248
4249void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004250window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4251{
4252 struct text_cursor_position *text_cursor_position =
4253 window->display->text_cursor_position;
4254
Scott Moreau9295ce02012-06-01 12:46:10 -06004255 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004256 return;
4257
4258 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004259 window->main_surface->surface,
4260 wl_fixed_from_int(x),
4261 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004262}
4263
4264void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004265window_damage(struct window *window, int32_t x, int32_t y,
4266 int32_t width, int32_t height)
4267{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004268 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004269}
4270
Casey Dahlin9074db52012-04-19 22:50:09 -04004271static void
4272surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004273 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004274{
Rob Bradford7507b572012-05-15 17:55:34 +01004275 struct window *window = data;
4276 struct output *output;
4277 struct output *output_found = NULL;
4278 struct window_output *window_output;
4279
4280 wl_list_for_each(output, &window->display->output_list, link) {
4281 if (output->output == wl_output) {
4282 output_found = output;
4283 break;
4284 }
4285 }
4286
4287 if (!output_found)
4288 return;
4289
Brian Lovinbc919262013-08-07 15:34:59 -07004290 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004291 window_output->output = output_found;
4292
4293 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004294
4295 if (window->output_handler)
4296 window->output_handler(window, output_found, 1,
4297 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004298}
4299
4300static void
4301surface_leave(void *data,
4302 struct wl_surface *wl_surface, struct wl_output *output)
4303{
Rob Bradford7507b572012-05-15 17:55:34 +01004304 struct window *window = data;
4305 struct window_output *window_output;
4306 struct window_output *window_output_found = NULL;
4307
4308 wl_list_for_each(window_output, &window->window_output_list, link) {
4309 if (window_output->output->output == output) {
4310 window_output_found = window_output;
4311 break;
4312 }
4313 }
4314
4315 if (window_output_found) {
4316 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004317
4318 if (window->output_handler)
4319 window->output_handler(window, window_output->output,
4320 0, window->user_data);
4321
Rob Bradford7507b572012-05-15 17:55:34 +01004322 free(window_output_found);
4323 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004324}
4325
4326static const struct wl_surface_listener surface_listener = {
4327 surface_enter,
4328 surface_leave
4329};
4330
Pekka Paalanen4e373742013-02-13 16:17:13 +02004331static struct surface *
4332surface_create(struct window *window)
4333{
4334 struct display *display = window->display;
4335 struct surface *surface;
4336
Brian Lovinbc919262013-08-07 15:34:59 -07004337 surface = xmalloc(sizeof *surface);
4338 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004339 if (!surface)
4340 return NULL;
4341
4342 surface->window = window;
4343 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004344 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004345 wl_surface_add_listener(surface->surface, &surface_listener, window);
4346
Pekka Paalanen35e82632013-04-25 13:57:48 +03004347 wl_list_insert(&window->subsurface_list, &surface->link);
4348
Pekka Paalanen4e373742013-02-13 16:17:13 +02004349 return surface;
4350}
4351
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004352static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004353window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004354{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004355 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004356 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004357
Peter Huttererf3d62272013-08-08 11:57:05 +10004358 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004359 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004360 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004361
4362 surface = surface_create(window);
4363 window->main_surface = surface;
4364
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004365 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004366
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004367 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004368 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004369
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004370 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004371#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004372 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004373#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004374 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004375#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004376 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004377 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004378
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004379 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004380 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004381 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004382
Rob Bradford7507b572012-05-15 17:55:34 +01004383 wl_list_init (&window->window_output_list);
4384
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004385 return window;
4386}
4387
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004388struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004389window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004390{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004391 return window_create_internal(display, 0);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004392}
4393
4394struct window *
4395window_create_custom(struct display *display)
4396{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004397 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004398}
4399
Jasper St. Pierre53686042013-12-09 15:26:25 -05004400void
4401window_set_transient_for(struct window *window,
4402 struct window *parent_window)
4403{
4404 window->transient_for = parent_window;
4405 window_sync_transient_for(window);
4406}
4407
4408struct window *
4409window_get_transient_for(struct window *window)
4410{
4411 return window->transient_for;
4412}
4413
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004414static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004415menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004416{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004417 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004418 int next;
4419
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004420 frame_interior(menu->frame, &x, &y, &width, &height);
4421 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004422 if (menu->current != next) {
4423 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004424 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004425 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004426}
4427
4428static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004429menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004430 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004431 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004432{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004433 struct menu *menu = data;
4434
4435 if (widget == menu->widget)
4436 menu_set_item(data, y);
4437
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004438 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004439}
4440
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004441static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004442menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004443 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004444{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004445 struct menu *menu = data;
4446
4447 if (widget == menu->widget)
4448 menu_set_item(data, y);
4449
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004450 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004451}
4452
4453static void
4454menu_leave_handler(struct widget *widget, struct input *input, void *data)
4455{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004456 struct menu *menu = data;
4457
4458 if (widget == menu->widget)
4459 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004460}
4461
4462static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004463menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004464 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004465 uint32_t button, enum wl_pointer_button_state state,
4466 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004467
4468{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004469 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004470
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004471 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4472 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004473 /* Either relase after press-drag-release or
4474 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004475 menu->func(menu->parent, input,
4476 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004477 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004478 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004479 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004480 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004481 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004482}
4483
4484static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004485menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004486{
4487 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004488 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004489 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004490
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004491 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004492
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004493 frame_repaint(menu->frame, cr);
4494 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004495
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004496 theme_set_background_source(menu->window->display->theme,
4497 cr, THEME_FRAME_ACTIVE);
4498 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004499 cairo_fill(cr);
4500
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004501 cairo_select_font_face(cr, "sans",
4502 CAIRO_FONT_SLANT_NORMAL,
4503 CAIRO_FONT_WEIGHT_NORMAL);
4504 cairo_set_font_size(cr, 12);
4505
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004506 for (i = 0; i < menu->count; i++) {
4507 if (i == menu->current) {
4508 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004509 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004510 cairo_fill(cr);
4511 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004512 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004513 cairo_show_text(cr, menu->entries[i]);
4514 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004515 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4516 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004517 cairo_show_text(cr, menu->entries[i]);
4518 }
4519 }
4520
4521 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004522}
4523
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004524static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004525handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4526{
4527 struct window *window = data;
4528 struct menu *menu = window->main_surface->widget->user_data;
4529
4530 input_ungrab(menu->input);
4531 menu_destroy(menu);
4532}
4533
4534static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004535 handle_popup_popup_done,
4536};
4537
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004538void
4539window_show_menu(struct display *display,
4540 struct input *input, uint32_t time, struct window *parent,
4541 int32_t x, int32_t y,
4542 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004543{
4544 struct window *window;
4545 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004546 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547
4548 menu = malloc(sizeof *menu);
4549 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004550 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004551
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004552 window = window_create_internal(parent->display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004553 if (!window) {
4554 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004555 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004556 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004557
4558 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004559 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004560 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004561 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4562 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004563 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004564 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004565 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004566 menu->entries = entries;
4567 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004568 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004569 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004570 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004571 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004572 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004573 window->x = x;
4574 window->y = y;
4575
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004576 input_ungrab(input);
4577
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004578 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004579 widget_set_enter_handler(menu->widget, menu_enter_handler);
4580 widget_set_leave_handler(menu->widget, menu_leave_handler);
4581 widget_set_motion_handler(menu->widget, menu_motion_handler);
4582 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004583
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004584 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004585 frame_resize_inside(menu->frame, 200, count * 20);
4586 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4587 window_schedule_resize(window, frame_width(menu->frame),
4588 frame_height(menu->frame));
4589
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004590 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004591
4592 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4593 window->main_surface->surface,
4594 parent->main_surface->surface,
4595 input->seat,
4596 display_get_serial(window->display),
4597 window->x - ix,
4598 window->y - iy,
4599 0);
4600 fail_on_null(window->xdg_popup);
4601
4602 xdg_popup_set_user_data(window->xdg_popup, window);
4603 xdg_popup_add_listener(window->xdg_popup,
4604 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004605}
4606
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004607void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004608window_set_buffer_type(struct window *window, enum window_buffer_type type)
4609{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004610 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004611}
4612
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004613void
4614window_set_preferred_format(struct window *window,
4615 enum preferred_format format)
4616{
4617 window->preferred_format = format;
4618}
4619
Pekka Paalanen35e82632013-04-25 13:57:48 +03004620struct widget *
4621window_add_subsurface(struct window *window, void *data,
4622 enum subsurface_mode default_mode)
4623{
4624 struct widget *widget;
4625 struct surface *surface;
4626 struct wl_surface *parent;
4627 struct wl_subcompositor *subcompo = window->display->subcompositor;
4628
Pekka Paalanen35e82632013-04-25 13:57:48 +03004629 surface = surface_create(window);
4630 widget = widget_create(window, surface, data);
4631 wl_list_init(&widget->link);
4632 surface->widget = widget;
4633
4634 parent = window->main_surface->surface;
4635 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4636 surface->surface,
4637 parent);
4638 surface->synchronized = 1;
4639
4640 switch (default_mode) {
4641 case SUBSURFACE_SYNCHRONIZED:
4642 surface->synchronized_default = 1;
4643 break;
4644 case SUBSURFACE_DESYNCHRONIZED:
4645 surface->synchronized_default = 0;
4646 break;
4647 default:
4648 assert(!"bad enum subsurface_mode");
4649 }
4650
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004651 window->resize_needed = 1;
4652 window_schedule_redraw(window);
4653
Pekka Paalanen35e82632013-04-25 13:57:48 +03004654 return widget;
4655}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004656
4657static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004658display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004659 struct wl_output *wl_output,
4660 int x, int y,
4661 int physical_width,
4662 int physical_height,
4663 int subpixel,
4664 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004665 const char *model,
4666 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004667{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004668 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004669
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004670 output->allocation.x = x;
4671 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004672 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004673
4674 if (output->make)
4675 free(output->make);
4676 output->make = strdup(make);
4677
4678 if (output->model)
4679 free(output->model);
4680 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004681}
4682
4683static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004684display_handle_done(void *data,
4685 struct wl_output *wl_output)
4686{
4687}
4688
4689static void
4690display_handle_scale(void *data,
4691 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004692 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004693{
4694 struct output *output = data;
4695
4696 output->scale = scale;
4697}
4698
4699static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004700display_handle_mode(void *data,
4701 struct wl_output *wl_output,
4702 uint32_t flags,
4703 int width,
4704 int height,
4705 int refresh)
4706{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004707 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004708 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004709
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004710 if (flags & WL_OUTPUT_MODE_CURRENT) {
4711 output->allocation.width = width;
4712 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004713 if (display->output_configure_handler)
4714 (*display->output_configure_handler)(
4715 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004716 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004717}
4718
4719static const struct wl_output_listener output_listener = {
4720 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004721 display_handle_mode,
4722 display_handle_done,
4723 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004724};
4725
4726static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004727display_add_output(struct display *d, uint32_t id)
4728{
4729 struct output *output;
4730
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004731 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004732 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004733 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004734 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004735 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004736 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004737 wl_list_insert(d->output_list.prev, &output->link);
4738
4739 wl_output_add_listener(output->output, &output_listener, output);
4740}
4741
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004742static void
4743output_destroy(struct output *output)
4744{
4745 if (output->destroy_handler)
4746 (*output->destroy_handler)(output, output->user_data);
4747
4748 wl_output_destroy(output->output);
4749 wl_list_remove(&output->link);
4750 free(output);
4751}
4752
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004753static void
4754display_destroy_output(struct display *d, uint32_t id)
4755{
4756 struct output *output;
4757
4758 wl_list_for_each(output, &d->output_list, link) {
4759 if (output->server_output_id == id) {
4760 output_destroy(output);
4761 break;
4762 }
4763 }
4764}
4765
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004766void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004767display_set_global_handler(struct display *display,
4768 display_global_handler_t handler)
4769{
4770 struct global *global;
4771
4772 display->global_handler = handler;
4773 if (!handler)
4774 return;
4775
4776 wl_list_for_each(global, &display->global_list, link)
4777 display->global_handler(display,
4778 global->name, global->interface,
4779 global->version, display->user_data);
4780}
4781
4782void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004783display_set_global_handler_remove(struct display *display,
4784 display_global_handler_t remove_handler)
4785{
4786 display->global_handler_remove = remove_handler;
4787 if (!remove_handler)
4788 return;
4789}
4790
4791void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004792display_set_output_configure_handler(struct display *display,
4793 display_output_handler_t handler)
4794{
4795 struct output *output;
4796
4797 display->output_configure_handler = handler;
4798 if (!handler)
4799 return;
4800
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004801 wl_list_for_each(output, &display->output_list, link) {
4802 if (output->allocation.width == 0 &&
4803 output->allocation.height == 0)
4804 continue;
4805
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004806 (*display->output_configure_handler)(output,
4807 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004808 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004809}
4810
4811void
4812output_set_user_data(struct output *output, void *data)
4813{
4814 output->user_data = data;
4815}
4816
4817void *
4818output_get_user_data(struct output *output)
4819{
4820 return output->user_data;
4821}
4822
4823void
4824output_set_destroy_handler(struct output *output,
4825 display_output_handler_t handler)
4826{
4827 output->destroy_handler = handler;
4828 /* FIXME: implement this, once we have way to remove outputs */
4829}
4830
4831void
Scott Moreau4e072362012-09-29 02:03:11 -06004832output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004833{
Scott Moreau4e072362012-09-29 02:03:11 -06004834 struct rectangle allocation = output->allocation;
4835
4836 switch (output->transform) {
4837 case WL_OUTPUT_TRANSFORM_90:
4838 case WL_OUTPUT_TRANSFORM_270:
4839 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4840 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4841 /* Swap width and height */
4842 allocation.width = output->allocation.height;
4843 allocation.height = output->allocation.width;
4844 break;
4845 }
4846
4847 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004848}
4849
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004850struct wl_output *
4851output_get_wl_output(struct output *output)
4852{
4853 return output->output;
4854}
4855
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004856enum wl_output_transform
4857output_get_transform(struct output *output)
4858{
4859 return output->transform;
4860}
4861
Alexander Larssonafd319a2013-05-22 14:41:27 +02004862uint32_t
4863output_get_scale(struct output *output)
4864{
4865 return output->scale;
4866}
4867
Jason Ekstrand738715d2014-04-02 19:53:50 -05004868const char *
4869output_get_make(struct output *output)
4870{
4871 return output->make;
4872}
4873
4874const char *
4875output_get_model(struct output *output)
4876{
4877 return output->model;
4878}
4879
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004880static void
Daniel Stone97f68542012-05-30 16:32:01 +01004881fini_xkb(struct input *input)
4882{
4883 xkb_state_unref(input->xkb.state);
4884 xkb_map_unref(input->xkb.keymap);
4885}
4886
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004887#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004888
Daniel Stone97f68542012-05-30 16:32:01 +01004889static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004890display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004891{
4892 struct input *input;
4893
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004894 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004895 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004896 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004897 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004898 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004899 input->pointer_focus = NULL;
4900 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004901 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004902 wl_list_insert(d->input_list.prev, &input->link);
4903
Daniel Stone37816df2012-05-16 18:45:18 +01004904 wl_seat_add_listener(input->seat, &seat_listener, input);
4905 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004906
Jason Ekstranda669bd52014-04-02 19:53:51 -05004907 if (d->data_device_manager) {
4908 input->data_device =
4909 wl_data_device_manager_get_data_device(d->data_device_manager,
4910 input->seat);
4911 wl_data_device_add_listener(input->data_device,
4912 &data_device_listener,
4913 input);
4914 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004915
4916 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004917
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004918 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4919 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004920 input->repeat_task.run = keyboard_repeat_func;
4921 display_watch_fd(d, input->repeat_timer_fd,
4922 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004923}
4924
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004925static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004926input_destroy(struct input *input)
4927{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004928 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004929 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004930
4931 if (input->drag_offer)
4932 data_offer_destroy(input->drag_offer);
4933
4934 if (input->selection_offer)
4935 data_offer_destroy(input->selection_offer);
4936
Jason Ekstranda669bd52014-04-02 19:53:51 -05004937 if (input->data_device)
4938 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004939
4940 if (input->display->seat_version >= 3) {
4941 if (input->pointer)
4942 wl_pointer_release(input->pointer);
4943 if (input->keyboard)
4944 wl_keyboard_release(input->keyboard);
4945 }
4946
Daniel Stone97f68542012-05-30 16:32:01 +01004947 fini_xkb(input);
4948
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004949 wl_surface_destroy(input->pointer_surface);
4950
Pekka Paalanene1207c72011-12-16 12:02:09 +02004951 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004952 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004953 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004954 free(input);
4955}
4956
4957static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004958init_workspace_manager(struct display *d, uint32_t id)
4959{
4960 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004961 wl_registry_bind(d->registry, id,
4962 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004963 if (d->workspace_manager != NULL)
4964 workspace_manager_add_listener(d->workspace_manager,
4965 &workspace_manager_listener,
4966 d);
4967}
4968
4969static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004970shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4971{
4972 struct display *d = data;
4973
4974 if (format == WL_SHM_FORMAT_RGB565)
4975 d->has_rgb565 = 1;
4976}
4977
4978struct wl_shm_listener shm_listener = {
4979 shm_format
4980};
4981
4982static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004983xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
4984{
4985 xdg_shell_pong(shell, serial);
4986}
4987
4988static const struct xdg_shell_listener xdg_shell_listener = {
4989 xdg_shell_ping,
4990};
4991
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08004992#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08004993#ifdef static_assert
4994static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
4995 "Interface version doesn't match implementation version");
4996#endif
4997
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004998static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004999registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5000 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005001{
5002 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005003 struct global *global;
5004
Brian Lovinbc919262013-08-07 15:34:59 -07005005 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005006 global->name = id;
5007 global->interface = strdup(interface);
5008 global->version = version;
5009 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005010
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005011 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005012 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005013 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005014 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005015 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005016 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005017 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005018 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005019 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005020 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005021 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005022 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5023 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005024 wl_registry_bind(registry, id,
5025 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005026 } else if (strcmp(interface, "xdg_shell") == 0) {
5027 d->xdg_shell = wl_registry_bind(registry, id,
5028 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005029 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005030 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005031 } else if (strcmp(interface, "text_cursor_position") == 0) {
5032 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005033 wl_registry_bind(registry, id,
5034 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005035 } else if (strcmp(interface, "workspace_manager") == 0) {
5036 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005037 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5038 d->subcompositor =
5039 wl_registry_bind(registry, id,
5040 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005041 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005042
5043 if (d->global_handler)
5044 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005045}
5046
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005047static void
5048registry_handle_global_remove(void *data, struct wl_registry *registry,
5049 uint32_t name)
5050{
5051 struct display *d = data;
5052 struct global *global;
5053 struct global *tmp;
5054
5055 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5056 if (global->name != name)
5057 continue;
5058
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005059 if (strcmp(global->interface, "wl_output") == 0)
5060 display_destroy_output(d, name);
5061
5062 /* XXX: Should destroy remaining bound globals */
5063
5064 if (d->global_handler_remove)
5065 d->global_handler_remove(d, name, global->interface,
5066 global->version, d->user_data);
5067
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005068 wl_list_remove(&global->link);
5069 free(global->interface);
5070 free(global);
5071 }
5072}
5073
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005074void *
5075display_bind(struct display *display, uint32_t name,
5076 const struct wl_interface *interface, uint32_t version)
5077{
5078 return wl_registry_bind(display->registry, name, interface, version);
5079}
5080
5081static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005082 registry_handle_global,
5083 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005084};
5085
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005086#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005087static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005088init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005089{
5090 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005091 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005092
Rob Clark6396ed32012-03-11 19:48:41 -05005093#ifdef USE_CAIRO_GLESV2
5094# define GL_BIT EGL_OPENGL_ES2_BIT
5095#else
5096# define GL_BIT EGL_OPENGL_BIT
5097#endif
5098
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005099 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005100 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005101 EGL_RED_SIZE, 1,
5102 EGL_GREEN_SIZE, 1,
5103 EGL_BLUE_SIZE, 1,
5104 EGL_ALPHA_SIZE, 1,
5105 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005106 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005107 EGL_NONE
5108 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005109
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005110#ifdef USE_CAIRO_GLESV2
5111 static const EGLint context_attribs[] = {
5112 EGL_CONTEXT_CLIENT_VERSION, 2,
5113 EGL_NONE
5114 };
5115 EGLint api = EGL_OPENGL_ES_API;
5116#else
5117 EGLint *context_attribs = NULL;
5118 EGLint api = EGL_OPENGL_API;
5119#endif
5120
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005121 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005122 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005123 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005124 return -1;
5125 }
5126
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005127 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005128 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005129 return -1;
5130 }
5131
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005132 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5133 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005134 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005135 return -1;
5136 }
5137
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005138 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005139 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005140 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005141 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005142 return -1;
5143 }
5144
Benjamin Franzke0c991632011-09-27 21:57:31 +02005145 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5146 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005147 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005148 return -1;
5149 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005150
5151 return 0;
5152}
5153
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005154static void
5155fini_egl(struct display *display)
5156{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005157 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005158
5159 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5160 EGL_NO_CONTEXT);
5161
5162 eglTerminate(display->dpy);
5163 eglReleaseThread();
5164}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005165#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005166
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005167static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005168init_dummy_surface(struct display *display)
5169{
5170 int len;
5171 void *data;
5172
5173 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5174 data = malloc(len);
5175 display->dummy_surface =
5176 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5177 1, 1, len);
5178 display->dummy_surface_data = data;
5179}
5180
5181static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005182handle_display_data(struct task *task, uint32_t events)
5183{
5184 struct display *display =
5185 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005186 struct epoll_event ep;
5187 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005188
5189 display->display_fd_events = events;
5190
5191 if (events & EPOLLERR || events & EPOLLHUP) {
5192 display_exit(display);
5193 return;
5194 }
5195
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005196 if (events & EPOLLIN) {
5197 ret = wl_display_dispatch(display->display);
5198 if (ret == -1) {
5199 display_exit(display);
5200 return;
5201 }
5202 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005203
5204 if (events & EPOLLOUT) {
5205 ret = wl_display_flush(display->display);
5206 if (ret == 0) {
5207 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5208 ep.data.ptr = &display->display_task;
5209 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5210 display->display_fd, &ep);
5211 } else if (ret == -1 && errno != EAGAIN) {
5212 display_exit(display);
5213 return;
5214 }
5215 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005216}
5217
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005218static void
5219log_handler(const char *format, va_list args)
5220{
5221 vfprintf(stderr, format, args);
5222}
5223
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005224struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005225display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005226{
5227 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005228
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005229 wl_log_set_handler_client(log_handler);
5230
Peter Huttererf3d62272013-08-08 11:57:05 +10005231 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005232 if (d == NULL)
5233 return NULL;
5234
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005235 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005236 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005237 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005238 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005239 return NULL;
5240 }
5241
Rob Bradford5ab9c752013-07-26 16:29:43 +01005242 d->xkb_context = xkb_context_new(0);
5243 if (d->xkb_context == NULL) {
5244 fprintf(stderr, "Failed to create XKB context\n");
5245 free(d);
5246 return NULL;
5247 }
5248
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005249 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005250 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005251 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005252 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5253 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005254
5255 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005256 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005257 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005258 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005259
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005260 d->workspace = 0;
5261 d->workspace_count = 1;
5262
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005263 d->registry = wl_display_get_registry(d->display);
5264 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005265
5266 if (wl_display_dispatch(d->display) < 0) {
5267 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5268 return NULL;
5269 }
5270
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005271#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005272 if (init_egl(d) < 0)
5273 fprintf(stderr, "EGL does not seem to work, "
5274 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005275#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005276
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005277 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005278
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005279 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005280
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005281 wl_list_init(&d->window_list);
5282
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005283 init_dummy_surface(d);
5284
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005285 return d;
5286}
5287
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005288static void
5289display_destroy_outputs(struct display *display)
5290{
5291 struct output *tmp;
5292 struct output *output;
5293
5294 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5295 output_destroy(output);
5296}
5297
Pekka Paalanene1207c72011-12-16 12:02:09 +02005298static void
5299display_destroy_inputs(struct display *display)
5300{
5301 struct input *tmp;
5302 struct input *input;
5303
5304 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5305 input_destroy(input);
5306}
5307
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005308void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005309display_destroy(struct display *display)
5310{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005311 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005312 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5313 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005314
5315 if (!wl_list_empty(&display->deferred_list))
5316 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5317
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005318 cairo_surface_destroy(display->dummy_surface);
5319 free(display->dummy_surface_data);
5320
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005321 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005322 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005323
Daniel Stone97f68542012-05-30 16:32:01 +01005324 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005325
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005326 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005327 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005328
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005329#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005330 if (display->argb_device)
5331 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005332#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005333
Pekka Paalanen35e82632013-04-25 13:57:48 +03005334 if (display->subcompositor)
5335 wl_subcompositor_destroy(display->subcompositor);
5336
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005337 if (display->xdg_shell)
5338 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005339
5340 if (display->shm)
5341 wl_shm_destroy(display->shm);
5342
5343 if (display->data_device_manager)
5344 wl_data_device_manager_destroy(display->data_device_manager);
5345
5346 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005347 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005348
5349 close(display->epoll_fd);
5350
U. Artie Eoff44874d92012-10-02 21:12:35 -07005351 if (!(display->display_fd_events & EPOLLERR) &&
5352 !(display->display_fd_events & EPOLLHUP))
5353 wl_display_flush(display->display);
5354
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005355 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005356 free(display);
5357}
5358
5359void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005360display_set_user_data(struct display *display, void *data)
5361{
5362 display->user_data = data;
5363}
5364
5365void *
5366display_get_user_data(struct display *display)
5367{
5368 return display->user_data;
5369}
5370
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005371struct wl_display *
5372display_get_display(struct display *display)
5373{
5374 return display->display;
5375}
5376
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005377int
5378display_has_subcompositor(struct display *display)
5379{
5380 if (display->subcompositor)
5381 return 1;
5382
5383 wl_display_roundtrip(display->display);
5384
5385 return display->subcompositor != NULL;
5386}
5387
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005388cairo_device_t *
5389display_get_cairo_device(struct display *display)
5390{
5391 return display->argb_device;
5392}
5393
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005394struct output *
5395display_get_output(struct display *display)
5396{
5397 return container_of(display->output_list.next, struct output, link);
5398}
5399
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005400struct wl_compositor *
5401display_get_compositor(struct display *display)
5402{
5403 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005404}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005405
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005406uint32_t
5407display_get_serial(struct display *display)
5408{
5409 return display->serial;
5410}
5411
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005412EGLDisplay
5413display_get_egl_display(struct display *d)
5414{
5415 return d->dpy;
5416}
5417
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005418struct wl_data_source *
5419display_create_data_source(struct display *display)
5420{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005421 if (display->data_device_manager)
5422 return wl_data_device_manager_create_data_source(display->data_device_manager);
5423 else
5424 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005425}
5426
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005427EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005428display_get_argb_egl_config(struct display *d)
5429{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005430 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005431}
5432
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005433int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005434display_acquire_window_surface(struct display *display,
5435 struct window *window,
5436 EGLContext ctx)
5437{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005438 struct surface *surface = window->main_surface;
5439
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005440 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005441 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005442
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005443 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005444 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005445}
5446
5447void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005448display_release_window_surface(struct display *display,
5449 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005450{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005451 struct surface *surface = window->main_surface;
5452
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005453 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005454 return;
5455
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005456 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005457}
5458
5459void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005460display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005461{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005462 wl_list_insert(&display->deferred_list, &task->link);
5463}
5464
5465void
5466display_watch_fd(struct display *display,
5467 int fd, uint32_t events, struct task *task)
5468{
5469 struct epoll_event ep;
5470
5471 ep.events = events;
5472 ep.data.ptr = task;
5473 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5474}
5475
5476void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005477display_unwatch_fd(struct display *display, int fd)
5478{
5479 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5480}
5481
5482void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005483display_run(struct display *display)
5484{
5485 struct task *task;
5486 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005487 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005488
Pekka Paalanen826d7952011-12-15 10:14:07 +02005489 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005490 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005491 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005492 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005493 struct task, link);
5494 wl_list_remove(&task->link);
5495 task->run(task, 0);
5496 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005497
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005498 wl_display_dispatch_pending(display->display);
5499
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005500 if (!display->running)
5501 break;
5502
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005503 ret = wl_display_flush(display->display);
5504 if (ret < 0 && errno == EAGAIN) {
5505 ep[0].events =
5506 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5507 ep[0].data.ptr = &display->display_task;
5508
5509 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5510 display->display_fd, &ep[0]);
5511 } else if (ret < 0) {
5512 break;
5513 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005514
5515 count = epoll_wait(display->epoll_fd,
5516 ep, ARRAY_LENGTH(ep), -1);
5517 for (i = 0; i < count; i++) {
5518 task = ep[i].data.ptr;
5519 task->run(task, ep[i].events);
5520 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005521 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005522}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005523
5524void
5525display_exit(struct display *display)
5526{
5527 display->running = 0;
5528}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005529
5530void
5531keysym_modifiers_add(struct wl_array *modifiers_map,
5532 const char *name)
5533{
5534 size_t len = strlen(name) + 1;
5535 char *p;
5536
5537 p = wl_array_add(modifiers_map, len);
5538
5539 if (p == NULL)
5540 return;
5541
5542 strncpy(p, name, len);
5543}
5544
5545static xkb_mod_index_t
5546keysym_modifiers_get_index(struct wl_array *modifiers_map,
5547 const char *name)
5548{
5549 xkb_mod_index_t index = 0;
5550 char *p = modifiers_map->data;
5551
5552 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5553 if (strcmp(p, name) == 0)
5554 return index;
5555
5556 index++;
5557 p += strlen(p) + 1;
5558 }
5559
5560 return XKB_MOD_INVALID;
5561}
5562
5563xkb_mod_mask_t
5564keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5565 const char *name)
5566{
5567 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5568
5569 if (index == XKB_MOD_INVALID)
5570 return XKB_MOD_INVALID;
5571
5572 return 1 << index;
5573}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005574
5575void *
5576fail_on_null(void *p)
5577{
5578 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005579 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005580 exit(EXIT_FAILURE);
5581 }
5582
5583 return p;
5584}
5585
5586void *
5587xmalloc(size_t s)
5588{
5589 return fail_on_null(malloc(s));
5590}
5591
Peter Huttererf3d62272013-08-08 11:57:05 +10005592void *
5593xzalloc(size_t s)
5594{
5595 return fail_on_null(zalloc(s));
5596}
5597
Kristian Høgsbergce278412013-07-25 15:20:20 -07005598char *
5599xstrdup(const char *s)
5600{
5601 return fail_on_null(strdup(s));
5602}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005603
5604void *
5605xrealloc(char *p, size_t s)
5606{
5607 return fail_on_null(realloc(p, s));
5608}