blob: 91c1ea06de6c532ea39931ccd9a72b104b1c889f [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øgsberg83fc0612010-08-04 22:44:55 -0400221 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400222 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300223 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400224 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400225 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500226 int custom;
227 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400228
Pekka Paalanen99436862012-11-19 17:15:59 +0200229 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400230
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500231 int fullscreen;
232 int maximized;
233
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200234 enum preferred_format preferred_format;
235
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500236 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500237 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400238 window_data_handler_t data_handler;
239 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500240 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400241 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200242 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400243
Pekka Paalanen4e373742013-02-13 16:17:13 +0200244 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500245 struct xdg_surface *xdg_surface;
246 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300247
Jasper St. Pierre53686042013-12-09 15:26:25 -0500248 struct window *transient_for;
249
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500250 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500251
Pekka Paalanen35e82632013-04-25 13:57:48 +0300252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
254
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500255 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400256 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500257};
258
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500259struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500260 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200261 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300262 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500263 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400264 struct wl_list link;
265 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500270 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500271 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700272 widget_touch_down_handler_t touch_down_handler;
273 widget_touch_up_handler_t touch_up_handler;
274 widget_touch_motion_handler_t touch_motion_handler;
275 widget_touch_frame_handler_t touch_frame_handler;
276 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200277 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400278 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500279 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300280 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500281 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000282 /* If this is set to false then no cairo surface will be
283 * created before redrawing the surface. This is useful if the
284 * redraw handler is going to do completely custom rendering
285 * such as using EGL directly */
286 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400287};
288
Rusty Lynch041815a2013-08-08 21:20:38 -0700289struct touch_point {
290 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800291 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700292 struct widget *widget;
293 struct wl_list link;
294};
295
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296struct input {
297 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303 struct window *pointer_focus;
304 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300306 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300309 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400310 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400311 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400312 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400313 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400315
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500316 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500317 struct widget *grab;
318 uint32_t grab_button;
319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800323 uint32_t touch_grab;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100328
329 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
335 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400336
337 struct task repeat_task;
338 int repeat_timer_fd;
339 uint32_t repeat_sym;
340 uint32_t repeat_key;
341 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400342};
343
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500344struct output {
345 struct display *display;
346 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800347 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500348 struct rectangle allocation;
349 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600350 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200351 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200352
353 display_output_handler_t destroy_handler;
354 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500355};
356
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500357struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500358 struct widget *widget;
359 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500360 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500361};
362
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500363struct menu {
364 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500365 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500366 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500367 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700368 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500369 const char **entries;
370 uint32_t time;
371 int current;
372 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400373 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500374 menu_func_t func;
375};
376
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300377struct tooltip {
378 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300379 struct widget *widget;
380 char *entry;
381 struct task tooltip_task;
382 int tooltip_fd;
383 float x, y;
384};
385
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700386struct shm_pool {
387 struct wl_shm_pool *pool;
388 size_t size;
389 size_t used;
390 void *data;
391};
392
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400393enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300394 CURSOR_DEFAULT = 100,
395 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400396};
397
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500398enum window_location {
399 WINDOW_INTERIOR = 0,
400 WINDOW_RESIZING_TOP = 1,
401 WINDOW_RESIZING_BOTTOM = 2,
402 WINDOW_RESIZING_LEFT = 4,
403 WINDOW_RESIZING_TOP_LEFT = 5,
404 WINDOW_RESIZING_BOTTOM_LEFT = 6,
405 WINDOW_RESIZING_RIGHT = 8,
406 WINDOW_RESIZING_TOP_RIGHT = 9,
407 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
408 WINDOW_RESIZING_MASK = 15,
409 WINDOW_EXTERIOR = 16,
410 WINDOW_TITLEBAR = 17,
411 WINDOW_CLIENT_AREA = 18,
412};
413
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200414static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400415
Pekka Paalanen97777442013-05-22 10:20:05 +0300416/* #define DEBUG */
417
418#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300419
420static void
421debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
422__attribute__ ((format (printf, 4, 5)));
423
424static void
425debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
426{
427 va_list ap;
428 struct timeval tv;
429
430 gettimeofday(&tv, NULL);
431 fprintf(stderr, "%8ld.%03ld ",
432 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
433
434 if (proxy)
435 fprintf(stderr, "%s@%d ",
436 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
437
438 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
439 fprintf(stderr, "%s ", func);
440
441 va_start(ap, fmt);
442 vfprintf(stderr, fmt, ap);
443 va_end(ap);
444}
445
446#define DBG(fmt, ...) \
447 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
448
449#define DBG_OBJ(obj, fmt, ...) \
450 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
451
452#else
453
454#define DBG(...) do {} while (0)
455#define DBG_OBJ(...) do {} while (0)
456
457#endif
458
Alexander Larsson1818e312013-05-22 14:41:31 +0200459static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200460surface_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 +0200461{
462 int32_t tmp;
463
464 switch (buffer_transform) {
465 case WL_OUTPUT_TRANSFORM_90:
466 case WL_OUTPUT_TRANSFORM_270:
467 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
468 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
469 tmp = *width;
470 *width = *height;
471 *height = tmp;
472 break;
473 default:
474 break;
475 }
476
477 *width *= buffer_scale;
478 *height *= buffer_scale;
479}
480
481static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200482buffer_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 +0200483{
484 int32_t tmp;
485
486 switch (buffer_transform) {
487 case WL_OUTPUT_TRANSFORM_90:
488 case WL_OUTPUT_TRANSFORM_270:
489 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
490 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
491 tmp = *width;
492 *width = *height;
493 *height = tmp;
494 break;
495 default:
496 break;
497 }
498
499 *width /= buffer_scale;
500 *height /= buffer_scale;
501}
502
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500503#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400504
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200505struct egl_window_surface {
506 struct toysurface base;
507 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100508 struct display *display;
509 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510 struct wl_egl_window *egl_window;
511 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100512};
513
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514static struct egl_window_surface *
515to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100516{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100518}
519
520static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200522 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200523 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100524{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200525 struct egl_window_surface *surface = to_egl_window_surface(base);
526
Alexander Larsson1818e312013-05-22 14:41:31 +0200527 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
528
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
530 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
531
532 return cairo_surface_reference(surface->cairo_surface);
533}
534
535static void
536egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200537 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538 struct rectangle *server_allocation)
539{
540 struct egl_window_surface *surface = to_egl_window_surface(base);
541
542 cairo_gl_surface_swapbuffers(surface->cairo_surface);
543 wl_egl_window_get_attached_size(surface->egl_window,
544 &server_allocation->width,
545 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200546
547 buffer_to_surface_size (buffer_transform, buffer_scale,
548 &server_allocation->width,
549 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200550}
551
552static int
553egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
554{
555 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200556 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100557
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200558 device = cairo_surface_get_device(surface->cairo_surface);
559 if (!device)
560 return -1;
561
562 if (!ctx) {
563 if (device == surface->display->argb_device)
564 ctx = surface->display->argb_ctx;
565 else
566 assert(0);
567 }
568
569 cairo_device_flush(device);
570 cairo_device_acquire(device);
571 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
572 surface->egl_surface, ctx))
573 fprintf(stderr, "failed to make surface current\n");
574
575 return 0;
576}
577
578static void
579egl_window_surface_release(struct toysurface *base)
580{
581 struct egl_window_surface *surface = to_egl_window_surface(base);
582 cairo_device_t *device;
583
584 device = cairo_surface_get_device(surface->cairo_surface);
585 if (!device)
586 return;
587
588 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
589 surface->display->argb_ctx))
590 fprintf(stderr, "failed to make context current\n");
591
592 cairo_device_release(device);
593}
594
595static void
596egl_window_surface_destroy(struct toysurface *base)
597{
598 struct egl_window_surface *surface = to_egl_window_surface(base);
599 struct display *d = surface->display;
600
601 cairo_surface_destroy(surface->cairo_surface);
602 eglDestroySurface(d->dpy, surface->egl_surface);
603 wl_egl_window_destroy(surface->egl_window);
604 surface->surface = NULL;
605
606 free(surface);
607}
608
609static struct toysurface *
610egl_window_surface_create(struct display *display,
611 struct wl_surface *wl_surface,
612 uint32_t flags,
613 struct rectangle *rectangle)
614{
615 struct egl_window_surface *surface;
616
Pekka Paalanenb3627362012-11-19 17:16:00 +0200617 if (display->dpy == EGL_NO_DISPLAY)
618 return NULL;
619
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200620 surface = calloc(1, sizeof *surface);
621 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100622 return NULL;
623
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200624 surface->base.prepare = egl_window_surface_prepare;
625 surface->base.swap = egl_window_surface_swap;
626 surface->base.acquire = egl_window_surface_acquire;
627 surface->base.release = egl_window_surface_release;
628 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100629
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200630 surface->display = display;
631 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface->egl_window = wl_egl_window_create(surface->surface,
634 rectangle->width,
635 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100636
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200637 surface->egl_surface = eglCreateWindowSurface(display->dpy,
638 display->argb_config,
639 surface->egl_window,
640 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200642 surface->cairo_surface =
643 cairo_gl_surface_create_for_egl(display->argb_device,
644 surface->egl_surface,
645 rectangle->width,
646 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100647
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200648 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100649}
650
Pekka Paalanenb3627362012-11-19 17:16:00 +0200651#else
652
653static struct toysurface *
654egl_window_surface_create(struct display *display,
655 struct wl_surface *wl_surface,
656 uint32_t flags,
657 struct rectangle *rectangle)
658{
659 return NULL;
660}
661
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400662#endif
663
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200664struct shm_surface_data {
665 struct wl_buffer *buffer;
666 struct shm_pool *pool;
667};
668
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400669struct wl_buffer *
670display_get_buffer_for_surface(struct display *display,
671 cairo_surface_t *surface)
672{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200673 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400674
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200675 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400676
677 return data->buffer;
678}
679
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500680static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700681shm_pool_destroy(struct shm_pool *pool);
682
683static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400684shm_surface_data_destroy(void *p)
685{
686 struct shm_surface_data *data = p;
687
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200688 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700689 if (data->pool)
690 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300691
692 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400693}
694
Kristian Høgsberg16626282012-04-03 11:21:27 -0400695static struct wl_shm_pool *
696make_shm_pool(struct display *display, int size, void **data)
697{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400698 struct wl_shm_pool *pool;
699 int fd;
700
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300701 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400702 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300703 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
704 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400705 return NULL;
706 }
707
708 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400709 if (*data == MAP_FAILED) {
710 fprintf(stderr, "mmap failed: %m\n");
711 close(fd);
712 return NULL;
713 }
714
715 pool = wl_shm_create_pool(display->shm, fd, size);
716
717 close(fd);
718
719 return pool;
720}
721
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700722static struct shm_pool *
723shm_pool_create(struct display *display, size_t size)
724{
725 struct shm_pool *pool = malloc(sizeof *pool);
726
727 if (!pool)
728 return NULL;
729
730 pool->pool = make_shm_pool(display, size, &pool->data);
731 if (!pool->pool) {
732 free(pool);
733 return NULL;
734 }
735
736 pool->size = size;
737 pool->used = 0;
738
739 return pool;
740}
741
742static void *
743shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
744{
745 if (pool->used + size > pool->size)
746 return NULL;
747
748 *offset = pool->used;
749 pool->used += size;
750
751 return (char *) pool->data + *offset;
752}
753
754/* destroy the pool. this does not unmap the memory though */
755static void
756shm_pool_destroy(struct shm_pool *pool)
757{
758 munmap(pool->data, pool->size);
759 wl_shm_pool_destroy(pool->pool);
760 free(pool);
761}
762
763/* Start allocating from the beginning of the pool again */
764static void
765shm_pool_reset(struct shm_pool *pool)
766{
767 pool->used = 0;
768}
769
770static int
771data_length_for_shm_surface(struct rectangle *rect)
772{
773 int stride;
774
775 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
776 rect->width);
777 return stride * rect->height;
778}
779
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500780static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700781display_create_shm_surface_from_pool(struct display *display,
782 struct rectangle *rectangle,
783 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400784{
785 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400786 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400787 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200788 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700789 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400790 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791
792 data = malloc(sizeof *data);
793 if (data == NULL)
794 return NULL;
795
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200796 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
797 cairo_format = CAIRO_FORMAT_RGB16_565;
798 else
799 cairo_format = CAIRO_FORMAT_ARGB32;
800
801 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700802 length = stride * rectangle->height;
803 data->pool = NULL;
804 map = shm_pool_allocate(pool, length, &offset);
805
806 if (!map) {
807 free(data);
808 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400809 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400811 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200812 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400813 rectangle->width,
814 rectangle->height,
815 stride);
816
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200817 cairo_surface_set_user_data(surface, &shm_surface_data_key,
818 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400819
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200820 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
821 format = WL_SHM_FORMAT_RGB565;
822 else {
823 if (flags & SURFACE_OPAQUE)
824 format = WL_SHM_FORMAT_XRGB8888;
825 else
826 format = WL_SHM_FORMAT_ARGB8888;
827 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400828
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200829 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
830 rectangle->width,
831 rectangle->height,
832 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700834 return surface;
835}
836
837static cairo_surface_t *
838display_create_shm_surface(struct display *display,
839 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200840 struct shm_pool *alternate_pool,
841 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842{
843 struct shm_surface_data *data;
844 struct shm_pool *pool;
845 cairo_surface_t *surface;
846
Pekka Paalanen99436862012-11-19 17:15:59 +0200847 if (alternate_pool) {
848 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700849 surface = display_create_shm_surface_from_pool(display,
850 rectangle,
851 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200852 alternate_pool);
853 if (surface) {
854 data = cairo_surface_get_user_data(surface,
855 &shm_surface_data_key);
856 goto out;
857 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700858 }
859
860 pool = shm_pool_create(display,
861 data_length_for_shm_surface(rectangle));
862 if (!pool)
863 return NULL;
864
865 surface =
866 display_create_shm_surface_from_pool(display, rectangle,
867 flags, pool);
868
869 if (!surface) {
870 shm_pool_destroy(pool);
871 return NULL;
872 }
873
874 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200875 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700876 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400877
Pekka Paalanen99436862012-11-19 17:15:59 +0200878out:
879 if (data_ret)
880 *data_ret = data;
881
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400882 return surface;
883}
884
nobled7b87cb02011-02-01 18:51:47 +0000885static int
886check_size(struct rectangle *rect)
887{
888 if (rect->width && rect->height)
889 return 0;
890
891 fprintf(stderr, "tried to create surface of "
892 "width: %d, height: %d\n", rect->width, rect->height);
893 return -1;
894}
895
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400896cairo_surface_t *
897display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400899 struct rectangle *rectangle,
900 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400901{
nobled7b87cb02011-02-01 18:51:47 +0000902 if (check_size(rectangle) < 0)
903 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200904
905 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200906 return display_create_shm_surface(display, rectangle, flags,
907 NULL, NULL);
908}
909
Pekka Paalanena4eda732012-11-19 17:16:02 +0200910struct shm_surface_leaf {
911 cairo_surface_t *cairo_surface;
912 /* 'data' is automatically destroyed, when 'cairo_surface' is */
913 struct shm_surface_data *data;
914
915 struct shm_pool *resize_pool;
916 int busy;
917};
918
919static void
920shm_surface_leaf_release(struct shm_surface_leaf *leaf)
921{
922 if (leaf->cairo_surface)
923 cairo_surface_destroy(leaf->cairo_surface);
924 /* leaf->data already destroyed via cairo private */
925
926 if (leaf->resize_pool)
927 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200928
929 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200930}
931
Pekka Paalanenaef02542013-04-25 13:57:47 +0300932#define MAX_LEAVES 3
933
Pekka Paalanen99436862012-11-19 17:15:59 +0200934struct shm_surface {
935 struct toysurface base;
936 struct display *display;
937 struct wl_surface *surface;
938 uint32_t flags;
939 int dx, dy;
940
Pekka Paalanenaef02542013-04-25 13:57:47 +0300941 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200943};
944
945static struct shm_surface *
946to_shm_surface(struct toysurface *base)
947{
948 return container_of(base, struct shm_surface, base);
949}
950
Pekka Paalanena4eda732012-11-19 17:16:02 +0200951static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300952shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
953{
954#ifdef DEBUG
955 struct shm_surface_leaf *leaf;
956 char bufs[MAX_LEAVES + 1];
957 int i;
958
959 for (i = 0; i < MAX_LEAVES; i++) {
960 leaf = &surface->leaf[i];
961
962 if (leaf->busy)
963 bufs[i] = 'b';
964 else if (leaf->cairo_surface)
965 bufs[i] = 'a';
966 else
967 bufs[i] = ' ';
968 }
969
970 bufs[MAX_LEAVES] = '\0';
971 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
972#endif
973}
974
975static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200976shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
977{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200978 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300979 struct shm_surface_leaf *leaf;
980 int i;
981 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300982
983 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200984
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 for (i = 0; i < MAX_LEAVES; i++) {
986 leaf = &surface->leaf[i];
987 if (leaf->data && leaf->data->buffer == buffer) {
988 leaf->busy = 0;
989 break;
990 }
991 }
992 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200993
Pekka Paalanenaef02542013-04-25 13:57:47 +0300994 /* Leave one free leaf with storage, release others */
995 free_found = 0;
996 for (i = 0; i < MAX_LEAVES; i++) {
997 leaf = &surface->leaf[i];
998
999 if (!leaf->cairo_surface || leaf->busy)
1000 continue;
1001
1002 if (!free_found)
1003 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001004 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001005 shm_surface_leaf_release(leaf);
1006 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001007
Pekka Paalanen97777442013-05-22 10:20:05 +03001008 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001009}
1010
1011static const struct wl_buffer_listener shm_surface_buffer_listener = {
1012 shm_surface_buffer_release
1013};
1014
Pekka Paalanen99436862012-11-19 17:15:59 +02001015static cairo_surface_t *
1016shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001017 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001018 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001019{
Pekka Paalanenec076692012-11-30 13:37:27 +02001020 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001021 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001022 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001023 struct shm_surface_leaf *leaf = NULL;
1024 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001025
1026 surface->dx = dx;
1027 surface->dy = dy;
1028
Pekka Paalanenaef02542013-04-25 13:57:47 +03001029 /* pick a free buffer, preferrably one that already has storage */
1030 for (i = 0; i < MAX_LEAVES; i++) {
1031 if (surface->leaf[i].busy)
1032 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001033
Pekka Paalanenaef02542013-04-25 13:57:47 +03001034 if (!leaf || surface->leaf[i].cairo_surface)
1035 leaf = &surface->leaf[i];
1036 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001037 DBG_OBJ(surface->surface, "pick leaf %d\n",
1038 (int)(leaf - &surface->leaf[0]));
1039
Pekka Paalanenaef02542013-04-25 13:57:47 +03001040 if (!leaf) {
1041 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001042 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001043 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001045 }
1046
Pekka Paalanena4eda732012-11-19 17:16:02 +02001047 if (!resize_hint && leaf->resize_pool) {
1048 cairo_surface_destroy(leaf->cairo_surface);
1049 leaf->cairo_surface = NULL;
1050 shm_pool_destroy(leaf->resize_pool);
1051 leaf->resize_pool = NULL;
1052 }
1053
Alexander Larsson1818e312013-05-22 14:41:31 +02001054 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1055
Pekka Paalanena4eda732012-11-19 17:16:02 +02001056 if (leaf->cairo_surface &&
1057 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1058 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001059 goto out;
1060
Pekka Paalanena4eda732012-11-19 17:16:02 +02001061 if (leaf->cairo_surface)
1062 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001063
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001064#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001065 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001066 /* Create a big pool to allocate from, while continuously
1067 * resizing. Mmapping a new pool in the server
1068 * is relatively expensive, so reusing a pool performs
1069 * better, but may temporarily reserve unneeded memory.
1070 */
1071 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001072 leaf->resize_pool = shm_pool_create(surface->display,
1073 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001074 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001075#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
Alexander Larsson1818e312013-05-22 14:41:31 +02001077 rect.width = width;
1078 rect.height = height;
1079
Pekka Paalanena4eda732012-11-19 17:16:02 +02001080 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001081 display_create_shm_surface(surface->display, &rect,
1082 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001083 leaf->resize_pool,
1084 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001085 if (!leaf->cairo_surface)
1086 return NULL;
1087
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001089 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001090
1091out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001092 surface->current = leaf;
1093
1094 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001095}
1096
1097static void
1098shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001099 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001100 struct rectangle *server_allocation)
1101{
1102 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001103 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001104
1105 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001106 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001107 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001108 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001109
Alexander Larsson1818e312013-05-22 14:41:31 +02001110 buffer_to_surface_size (buffer_transform, buffer_scale,
1111 &server_allocation->width,
1112 &server_allocation->height);
1113
Pekka Paalanena4eda732012-11-19 17:16:02 +02001114 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001115 surface->dx, surface->dy);
1116 wl_surface_damage(surface->surface, 0, 0,
1117 server_allocation->width, server_allocation->height);
1118 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001119
Pekka Paalanen71233882013-04-25 13:57:53 +03001120 DBG_OBJ(surface->surface, "leaf %d busy\n",
1121 (int)(leaf - &surface->leaf[0]));
1122
Pekka Paalanena4eda732012-11-19 17:16:02 +02001123 leaf->busy = 1;
1124 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001125}
1126
1127static int
1128shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1129{
1130 return -1;
1131}
1132
1133static void
1134shm_surface_release(struct toysurface *base)
1135{
1136}
1137
1138static void
1139shm_surface_destroy(struct toysurface *base)
1140{
1141 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001142 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001143
Pekka Paalanenaef02542013-04-25 13:57:47 +03001144 for (i = 0; i < MAX_LEAVES; i++)
1145 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001146
1147 free(surface);
1148}
1149
1150static struct toysurface *
1151shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1152 uint32_t flags, struct rectangle *rectangle)
1153{
1154 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001155 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001156
Brian Lovinbc919262013-08-07 15:34:59 -07001157 surface = xmalloc(sizeof *surface);
1158 memset(surface, 0, sizeof *surface);
1159
Pekka Paalanen99436862012-11-19 17:15:59 +02001160 if (!surface)
1161 return NULL;
1162
1163 surface->base.prepare = shm_surface_prepare;
1164 surface->base.swap = shm_surface_swap;
1165 surface->base.acquire = shm_surface_acquire;
1166 surface->base.release = shm_surface_release;
1167 surface->base.destroy = shm_surface_destroy;
1168
1169 surface->display = display;
1170 surface->surface = wl_surface;
1171 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001172
1173 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001174}
1175
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001176/*
1177 * The following correspondences between file names and cursors was copied
1178 * from: https://bugs.kde.org/attachment.cgi?id=67313
1179 */
1180
1181static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001182 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001183 "sw-resize",
1184 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001185};
1186
1187static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001188 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001189 "se-resize",
1190 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001191};
1192
1193static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001194 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001195 "s-resize",
1196 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001197};
1198
1199static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001200 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001201 "closedhand",
1202 "208530c400c041818281048008011002"
1203};
1204
1205static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001206 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001207 "default",
1208 "top_left_arrow",
1209 "left-arrow"
1210};
1211
1212static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001213 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001214 "w-resize",
1215 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001216};
1217
1218static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001219 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001220 "e-resize",
1221 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001222};
1223
1224static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001225 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001226 "nw-resize",
1227 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001228};
1229
1230static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001232 "ne-resize",
1233 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001234};
1235
1236static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001237 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001238 "n-resize",
1239 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001240};
1241
1242static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001243 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001244 "ibeam",
1245 "text"
1246};
1247
1248static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001249 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001250 "pointer",
1251 "pointing_hand",
1252 "e29285e634086352946a0e7090d73106"
1253};
1254
1255static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001256 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001257 "wait",
1258 "0426c94ea35c87780ff01dc239897213"
1259};
1260
1261struct cursor_alternatives {
1262 const char **names;
1263 size_t count;
1264};
1265
1266static const struct cursor_alternatives cursors[] = {
1267 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1268 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1269 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1270 {grabbings, ARRAY_LENGTH(grabbings)},
1271 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1272 {left_sides, ARRAY_LENGTH(left_sides)},
1273 {right_sides, ARRAY_LENGTH(right_sides)},
1274 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1275 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1276 {top_sides, ARRAY_LENGTH(top_sides)},
1277 {xterms, ARRAY_LENGTH(xterms)},
1278 {hand1s, ARRAY_LENGTH(hand1s)},
1279 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001280};
1281
1282static void
1283create_cursors(struct display *display)
1284{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001285 struct weston_config *config;
1286 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001287 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001288 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001289 unsigned int i, j;
1290 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001291
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001292 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001293 s = weston_config_get_section(config, "shell", NULL, NULL);
1294 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1295 weston_config_section_get_int(s, "cursor-size", &size, 32);
1296 weston_config_destroy(config);
1297
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001298 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001299 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001300 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001301 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001302
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001303 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001304 cursor = NULL;
1305 for (j = 0; !cursor && j < cursors[i].count; ++j)
1306 cursor = wl_cursor_theme_get_cursor(
1307 display->cursor_theme, cursors[i].names[j]);
1308
1309 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001310 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001311 cursors[i].names[0]);
1312
1313 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001314 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001315}
1316
1317static void
1318destroy_cursors(struct display *display)
1319{
1320 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001321 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001322}
1323
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001324struct wl_cursor_image *
1325display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001326{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001327 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001328
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001329 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001330}
1331
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001332static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001333surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001334{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001335 if (!surface->cairo_surface)
1336 return;
1337
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001338 if (surface->opaque_region) {
1339 wl_surface_set_opaque_region(surface->surface,
1340 surface->opaque_region);
1341 wl_region_destroy(surface->opaque_region);
1342 surface->opaque_region = NULL;
1343 }
1344
1345 if (surface->input_region) {
1346 wl_surface_set_input_region(surface->surface,
1347 surface->input_region);
1348 wl_region_destroy(surface->input_region);
1349 surface->input_region = NULL;
1350 }
1351
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001353 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001354 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001355
Pekka Paalanen89dee002013-02-13 16:17:20 +02001356 cairo_surface_destroy(surface->cairo_surface);
1357 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001358}
1359
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001360int
1361window_has_focus(struct window *window)
1362{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001363 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001364}
1365
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001366static void
1367window_close(struct window *window)
1368{
1369 if (window->close_handler)
1370 window->close_handler(window->user_data);
1371 else
1372 display_exit(window->display);
1373}
1374
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001375struct display *
1376window_get_display(struct window *window)
1377{
1378 return window->display;
1379}
1380
Pekka Paalanen89dee002013-02-13 16:17:20 +02001381static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001382surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001383{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001384 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001385 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001386
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001387 if (!surface->toysurface && display->dpy &&
1388 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001389 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001390 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001391 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001392 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001393 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001394 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001395
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001396 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001397 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001398 surface->surface,
1399 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001400
Pekka Paalanen89dee002013-02-13 16:17:20 +02001401 surface->cairo_surface = surface->toysurface->prepare(
1402 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001403 allocation.width, allocation.height, flags,
1404 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001405}
1406
1407static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001408window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001410 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001412 int dx = 0;
1413 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414
Pekka Paalanenec076692012-11-30 13:37:27 +02001415 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001416 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001417
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001418 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1419 flags |= SURFACE_HINT_RGB565;
1420
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001421 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1422 dx = surface->server_allocation.width -
1423 surface->allocation.width;
1424
1425 if (window->resize_edges & WINDOW_RESIZING_TOP)
1426 dy = surface->server_allocation.height -
1427 surface->allocation.height;
1428
1429 window->resize_edges = 0;
1430
Pekka Paalanen89dee002013-02-13 16:17:20 +02001431 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001432}
1433
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001434int
1435window_get_buffer_transform(struct window *window)
1436{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001437 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001438}
1439
1440void
1441window_set_buffer_transform(struct window *window,
1442 enum wl_output_transform transform)
1443{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001444 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001445 wl_surface_set_buffer_transform(window->main_surface->surface,
1446 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001447}
1448
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001449void
1450window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001451 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001452{
1453 window->main_surface->buffer_scale = scale;
1454 wl_surface_set_buffer_scale(window->main_surface->surface,
1455 scale);
1456}
1457
1458uint32_t
1459window_get_buffer_scale(struct window *window)
1460{
1461 return window->main_surface->buffer_scale;
1462}
1463
Alexander Larssond68f5232013-05-22 14:41:33 +02001464uint32_t
1465window_get_output_scale(struct window *window)
1466{
1467 struct window_output *window_output;
1468 struct window_output *window_output_tmp;
1469 int scale = 1;
1470
1471 wl_list_for_each_safe(window_output, window_output_tmp,
1472 &window->window_output_list, link) {
1473 if (window_output->output->scale > scale)
1474 scale = window_output->output->scale;
1475 }
1476
1477 return scale;
1478}
1479
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001480static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001481
Pekka Paalanen4e373742013-02-13 16:17:13 +02001482static void
1483surface_destroy(struct surface *surface)
1484{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001485 if (surface->frame_cb)
1486 wl_callback_destroy(surface->frame_cb);
1487
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001488 if (surface->input_region)
1489 wl_region_destroy(surface->input_region);
1490
1491 if (surface->opaque_region)
1492 wl_region_destroy(surface->opaque_region);
1493
Pekka Paalanen35e82632013-04-25 13:57:48 +03001494 if (surface->subsurface)
1495 wl_subsurface_destroy(surface->subsurface);
1496
Pekka Paalanen4e373742013-02-13 16:17:13 +02001497 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001498
1499 if (surface->toysurface)
1500 surface->toysurface->destroy(surface->toysurface);
1501
Pekka Paalanen35e82632013-04-25 13:57:48 +03001502 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001503 free(surface);
1504}
1505
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001506void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001507window_destroy(struct window *window)
1508{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001509 struct display *display = window->display;
1510 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001511 struct window_output *window_output;
1512 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001513
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001514 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001515
Rusty Lynch1084da52013-08-15 09:10:08 -07001516 wl_list_for_each(input, &display->input_list, link) {
1517 if (input->touch_focus == window)
1518 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001519 if (input->pointer_focus == window)
1520 input->pointer_focus = NULL;
1521 if (input->keyboard_focus == window)
1522 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001523 if (input->focus_widget &&
1524 input->focus_widget->window == window)
1525 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001526 }
1527
Rob Bradford7507b572012-05-15 17:55:34 +01001528 wl_list_for_each_safe(window_output, window_output_tmp,
1529 &window->window_output_list, link) {
1530 free (window_output);
1531 }
1532
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001533 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001534 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001535
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001536 if (window->xdg_surface)
1537 xdg_surface_destroy(window->xdg_surface);
1538 if (window->xdg_popup)
1539 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001540
1541 surface_destroy(window->main_surface);
1542
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001543 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001544
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001545 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001546 free(window);
1547}
1548
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001549static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001550widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001551{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001552 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001553
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001554 wl_list_for_each(child, &widget->child_list, link) {
1555 target = widget_find_widget(child, x, y);
1556 if (target)
1557 return target;
1558 }
1559
1560 if (widget->allocation.x <= x &&
1561 x < widget->allocation.x + widget->allocation.width &&
1562 widget->allocation.y <= y &&
1563 y < widget->allocation.y + widget->allocation.height) {
1564 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001565 }
1566
1567 return NULL;
1568}
1569
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001570static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001571window_find_widget(struct window *window, int32_t x, int32_t y)
1572{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001573 struct surface *surface;
1574 struct widget *widget;
1575
1576 wl_list_for_each(surface, &window->subsurface_list, link) {
1577 widget = widget_find_widget(surface->widget, x, y);
1578 if (widget)
1579 return widget;
1580 }
1581
1582 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001583}
1584
1585static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001586widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001587{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001588 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001589
Peter Huttererf3d62272013-08-08 11:57:05 +10001590 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001591 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001592 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001593 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001594 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001595 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001596 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001597 widget->tooltip = NULL;
1598 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001599 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001600 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001601
1602 return widget;
1603}
1604
1605struct widget *
1606window_add_widget(struct window *window, void *data)
1607{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001608 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001609
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001610 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001611 wl_list_init(&widget->link);
1612 window->main_surface->widget = widget;
1613
1614 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001615}
1616
1617struct widget *
1618widget_add_widget(struct widget *parent, void *data)
1619{
1620 struct widget *widget;
1621
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001622 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001623 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001624
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001625 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001626}
1627
1628void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001629widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001630{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001631 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001632 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001633 struct input *input;
1634
Pekka Paalanen35e82632013-04-25 13:57:48 +03001635 /* Destroy the sub-surface along with the root widget */
1636 if (surface->widget == widget && surface->subsurface)
1637 surface_destroy(widget->surface);
1638
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001639 if (widget->tooltip)
1640 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001641
Pekka Paalanene156fb62012-01-19 13:51:38 +02001642 wl_list_for_each(input, &display->input_list, link) {
1643 if (input->focus_widget == widget)
1644 input->focus_widget = NULL;
1645 }
1646
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001647 wl_list_remove(&widget->link);
1648 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001649}
1650
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001651void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001652widget_set_default_cursor(struct widget *widget, int cursor)
1653{
1654 widget->default_cursor = cursor;
1655}
1656
1657void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001658widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001659{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001660 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001661}
1662
1663void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001664widget_set_size(struct widget *widget, int32_t width, int32_t height)
1665{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001666 widget->allocation.width = width;
1667 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001668}
1669
1670void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001671widget_set_allocation(struct widget *widget,
1672 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001673{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001674 widget->allocation.x = x;
1675 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001676 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677}
1678
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001679void
1680widget_set_transparent(struct widget *widget, int transparent)
1681{
1682 widget->opaque = !transparent;
1683}
1684
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001685void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001686widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001687{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001688 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001689}
1690
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001691static cairo_surface_t *
1692widget_get_cairo_surface(struct widget *widget)
1693{
1694 struct surface *surface = widget->surface;
1695 struct window *window = widget->window;
1696
Neil Roberts97b747c2013-12-19 16:17:12 +00001697 assert(widget->use_cairo);
1698
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001699 if (!surface->cairo_surface) {
1700 if (surface == window->main_surface)
1701 window_create_main_surface(window);
1702 else
1703 surface_create_surface(surface, 0, 0, 0);
1704 }
1705
1706 return surface->cairo_surface;
1707}
1708
Alexander Larsson15901f02013-05-22 14:41:25 +02001709static void
1710widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1711{
1712 struct surface *surface = widget->surface;
1713 double angle;
1714 cairo_matrix_t m;
1715 enum wl_output_transform transform;
1716 int surface_width, surface_height;
1717 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001718 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001719
1720 surface_width = surface->allocation.width;
1721 surface_height = surface->allocation.height;
1722
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001723 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001724 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001725
Alexander Larsson15901f02013-05-22 14:41:25 +02001726 switch (transform) {
1727 case WL_OUTPUT_TRANSFORM_FLIPPED:
1728 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1729 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1730 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1731 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1732 break;
1733 default:
1734 cairo_matrix_init_identity(&m);
1735 break;
1736 }
1737
1738 switch (transform) {
1739 case WL_OUTPUT_TRANSFORM_NORMAL:
1740 default:
1741 angle = 0;
1742 translate_x = 0;
1743 translate_y = 0;
1744 break;
1745 case WL_OUTPUT_TRANSFORM_FLIPPED:
1746 angle = 0;
1747 translate_x = surface_width;
1748 translate_y = 0;
1749 break;
1750 case WL_OUTPUT_TRANSFORM_90:
1751 angle = M_PI_2;
1752 translate_x = surface_height;
1753 translate_y = 0;
1754 break;
1755 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1756 angle = M_PI_2;
1757 translate_x = surface_height;
1758 translate_y = surface_width;
1759 break;
1760 case WL_OUTPUT_TRANSFORM_180:
1761 angle = M_PI;
1762 translate_x = surface_width;
1763 translate_y = surface_height;
1764 break;
1765 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1766 angle = M_PI;
1767 translate_x = 0;
1768 translate_y = surface_height;
1769 break;
1770 case WL_OUTPUT_TRANSFORM_270:
1771 angle = M_PI + M_PI_2;
1772 translate_x = 0;
1773 translate_y = surface_width;
1774 break;
1775 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1776 angle = M_PI + M_PI_2;
1777 translate_x = 0;
1778 translate_y = 0;
1779 break;
1780 }
1781
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001782 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001783 cairo_translate(cr, translate_x, translate_y);
1784 cairo_rotate(cr, angle);
1785 cairo_transform(cr, &m);
1786}
1787
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001788cairo_t *
1789widget_cairo_create(struct widget *widget)
1790{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001791 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001792 cairo_surface_t *cairo_surface;
1793 cairo_t *cr;
1794
1795 cairo_surface = widget_get_cairo_surface(widget);
1796 cr = cairo_create(cairo_surface);
1797
Alexander Larsson15901f02013-05-22 14:41:25 +02001798 widget_cairo_update_transform(widget, cr);
1799
Pekka Paalanen35e82632013-04-25 13:57:48 +03001800 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1801
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001802 return cr;
1803}
1804
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001805struct wl_surface *
1806widget_get_wl_surface(struct widget *widget)
1807{
1808 return widget->surface->surface;
1809}
1810
Neil Roberts5e10a042013-09-09 00:40:17 +01001811struct wl_subsurface *
1812widget_get_wl_subsurface(struct widget *widget)
1813{
1814 return widget->surface->subsurface;
1815}
1816
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001817uint32_t
1818widget_get_last_time(struct widget *widget)
1819{
1820 return widget->surface->last_time;
1821}
1822
1823void
1824widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1825{
1826 struct wl_compositor *comp = widget->window->display->compositor;
1827 struct surface *surface = widget->surface;
1828
1829 if (!surface->input_region)
1830 surface->input_region = wl_compositor_create_region(comp);
1831
1832 if (rect) {
1833 wl_region_add(surface->input_region,
1834 rect->x, rect->y, rect->width, rect->height);
1835 }
1836}
1837
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001838void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001839widget_set_resize_handler(struct widget *widget,
1840 widget_resize_handler_t handler)
1841{
1842 widget->resize_handler = handler;
1843}
1844
1845void
1846widget_set_redraw_handler(struct widget *widget,
1847 widget_redraw_handler_t handler)
1848{
1849 widget->redraw_handler = handler;
1850}
1851
1852void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001853widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001854{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001855 widget->enter_handler = handler;
1856}
1857
1858void
1859widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1860{
1861 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001862}
1863
1864void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001865widget_set_motion_handler(struct widget *widget,
1866 widget_motion_handler_t handler)
1867{
1868 widget->motion_handler = handler;
1869}
1870
1871void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001872widget_set_button_handler(struct widget *widget,
1873 widget_button_handler_t handler)
1874{
1875 widget->button_handler = handler;
1876}
1877
1878void
Rusty Lynch041815a2013-08-08 21:20:38 -07001879widget_set_touch_up_handler(struct widget *widget,
1880 widget_touch_up_handler_t handler)
1881{
1882 widget->touch_up_handler = handler;
1883}
1884
1885void
1886widget_set_touch_down_handler(struct widget *widget,
1887 widget_touch_down_handler_t handler)
1888{
1889 widget->touch_down_handler = handler;
1890}
1891
1892void
1893widget_set_touch_motion_handler(struct widget *widget,
1894 widget_touch_motion_handler_t handler)
1895{
1896 widget->touch_motion_handler = handler;
1897}
1898
1899void
1900widget_set_touch_frame_handler(struct widget *widget,
1901 widget_touch_frame_handler_t handler)
1902{
1903 widget->touch_frame_handler = handler;
1904}
1905
1906void
1907widget_set_touch_cancel_handler(struct widget *widget,
1908 widget_touch_cancel_handler_t handler)
1909{
1910 widget->touch_cancel_handler = handler;
1911}
1912
1913void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001914widget_set_axis_handler(struct widget *widget,
1915 widget_axis_handler_t handler)
1916{
1917 widget->axis_handler = handler;
1918}
1919
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001920static void
1921window_schedule_redraw_task(struct window *window);
1922
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001923void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001924widget_schedule_redraw(struct widget *widget)
1925{
Pekka Paalanen71233882013-04-25 13:57:53 +03001926 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001927 widget->surface->redraw_needed = 1;
1928 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001929}
1930
Neil Roberts97b747c2013-12-19 16:17:12 +00001931void
1932widget_set_use_cairo(struct widget *widget,
1933 int use_cairo)
1934{
1935 widget->use_cairo = use_cairo;
1936}
1937
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001938cairo_surface_t *
1939window_get_surface(struct window *window)
1940{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001941 cairo_surface_t *cairo_surface;
1942
1943 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1944
1945 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001946}
1947
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001948struct wl_surface *
1949window_get_wl_surface(struct window *window)
1950{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001951 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001952}
1953
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001954static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001955tooltip_redraw_handler(struct widget *widget, void *data)
1956{
1957 cairo_t *cr;
1958 const int32_t r = 3;
1959 struct tooltip *tooltip = data;
1960 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001961
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001962 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001963 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001964 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1965 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1966 cairo_paint(cr);
1967
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001968 width = widget->allocation.width;
1969 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001970 rounded_rect(cr, 0, 0, width, height, r);
1971
1972 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1973 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1974 cairo_fill(cr);
1975
1976 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1977 cairo_move_to(cr, 10, 16);
1978 cairo_show_text(cr, tooltip->entry);
1979 cairo_destroy(cr);
1980}
1981
1982static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001983get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001984{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001985 cairo_t *cr;
1986 cairo_text_extents_t extents;
1987
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001988 /* Use the dummy_surface because tooltip's surface was not
1989 * created yet, and parent does not have a valid surface
1990 * outside repaint, either.
1991 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001992 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001993 cairo_text_extents(cr, tooltip->entry, &extents);
1994 cairo_destroy(cr);
1995
1996 return extents;
1997}
1998
1999static int
2000window_create_tooltip(struct tooltip *tooltip)
2001{
2002 struct widget *parent = tooltip->parent;
2003 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002004 const int offset_y = 27;
2005 const int margin = 3;
2006 cairo_text_extents_t extents;
2007
2008 if (tooltip->widget)
2009 return 0;
2010
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002011 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002012
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002013 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002014 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002015 widget_set_allocation(tooltip->widget,
2016 tooltip->x, tooltip->y + offset_y,
2017 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002018
2019 return 0;
2020}
2021
2022void
2023widget_destroy_tooltip(struct widget *parent)
2024{
2025 struct tooltip *tooltip = parent->tooltip;
2026
2027 parent->tooltip_count = 0;
2028 if (!tooltip)
2029 return;
2030
2031 if (tooltip->widget) {
2032 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002033 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002034 }
2035
2036 close(tooltip->tooltip_fd);
2037 free(tooltip->entry);
2038 free(tooltip);
2039 parent->tooltip = NULL;
2040}
2041
2042static void
2043tooltip_func(struct task *task, uint32_t events)
2044{
2045 struct tooltip *tooltip =
2046 container_of(task, struct tooltip, tooltip_task);
2047 uint64_t exp;
2048
Martin Olsson8df662a2012-07-08 03:03:47 +02002049 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2050 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002051 window_create_tooltip(tooltip);
2052}
2053
2054#define TOOLTIP_TIMEOUT 500
2055static int
2056tooltip_timer_reset(struct tooltip *tooltip)
2057{
2058 struct itimerspec its;
2059
2060 its.it_interval.tv_sec = 0;
2061 its.it_interval.tv_nsec = 0;
2062 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2063 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2064 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2065 fprintf(stderr, "could not set timerfd\n: %m");
2066 return -1;
2067 }
2068
2069 return 0;
2070}
2071
2072int
2073widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2074{
2075 struct tooltip *tooltip = parent->tooltip;
2076
2077 parent->tooltip_count++;
2078 if (tooltip) {
2079 tooltip->x = x;
2080 tooltip->y = y;
2081 tooltip_timer_reset(tooltip);
2082 return 0;
2083 }
2084
2085 /* the handler might be triggered too fast via input device motion, so
2086 * we need this check here to make sure tooltip is fully initialized */
2087 if (parent->tooltip_count > 1)
2088 return 0;
2089
2090 tooltip = malloc(sizeof *tooltip);
2091 if (!tooltip)
2092 return -1;
2093
2094 parent->tooltip = tooltip;
2095 tooltip->parent = parent;
2096 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002097 tooltip->x = x;
2098 tooltip->y = y;
2099 tooltip->entry = strdup(entry);
2100 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2101 if (tooltip->tooltip_fd < 0) {
2102 fprintf(stderr, "could not create timerfd\n: %m");
2103 return -1;
2104 }
2105
2106 tooltip->tooltip_task.run = tooltip_func;
2107 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2108 EPOLLIN, &tooltip->tooltip_task);
2109 tooltip_timer_reset(tooltip);
2110
2111 return 0;
2112}
2113
2114static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002115workspace_manager_state(void *data,
2116 struct workspace_manager *workspace_manager,
2117 uint32_t current,
2118 uint32_t count)
2119{
2120 struct display *display = data;
2121
2122 display->workspace = current;
2123 display->workspace_count = count;
2124}
2125
2126static const struct workspace_manager_listener workspace_manager_listener = {
2127 workspace_manager_state
2128};
2129
2130static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002131frame_resize_handler(struct widget *widget,
2132 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002133{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002134 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002135 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002136 struct rectangle interior;
2137 struct rectangle input;
2138 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002140 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002141 interior.x = 0;
2142 interior.y = 0;
2143 interior.width = width;
2144 interior.height = height;
2145 } else {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002146 if (widget->window->maximized) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002147 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2148 } else {
2149 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2150 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002151
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002152 frame_resize(frame->frame, width, height);
2153 frame_interior(frame->frame, &interior.x, &interior.y,
2154 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002155 }
2156
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002157 widget_set_allocation(child, interior.x, interior.y,
2158 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002160 if (child->resize_handler) {
2161 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002162 child->user_data);
2163
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002164 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002165 width = child->allocation.width;
2166 height = child->allocation.height;
2167 } else {
2168 frame_resize_inside(frame->frame,
2169 child->allocation.width,
2170 child->allocation.height);
2171 width = frame_width(frame->frame);
2172 height = frame_height(frame->frame);
2173 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002174 }
2175
Scott Moreauf7e498c2012-05-14 11:39:29 -06002176 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002177
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002178 widget->surface->input_region =
2179 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002180 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002181 frame_input_rect(frame->frame, &input.x, &input.y,
2182 &input.width, &input.height);
2183 wl_region_add(widget->surface->input_region,
2184 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002185 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002186 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002187 }
2188
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002189 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002190
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002191 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002192 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002193 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2194 &opaque.width, &opaque.height);
2195
2196 wl_region_add(widget->surface->opaque_region,
2197 opaque.x, opaque.y,
2198 opaque.width, opaque.height);
2199 } else {
2200 wl_region_add(widget->surface->opaque_region,
2201 0, 0, width, height);
2202 }
Martin Minarik1998b152012-05-10 02:04:35 +02002203 }
2204
Martin Minarik1998b152012-05-10 02:04:35 +02002205
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002206 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002207}
2208
2209static void
2210frame_redraw_handler(struct widget *widget, void *data)
2211{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002213 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002214 struct window *window = widget->window;
2215
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002216 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002217 return;
2218
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002219 if (window->focused) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002220 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2221 } else {
2222 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2223 }
2224
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002225 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002226
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002227 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002228
2229 cairo_destroy(cr);
2230}
2231
2232static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002233frame_get_pointer_image_for_location(struct window_frame *frame,
2234 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002235{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002236 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002237
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002238 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002239 return CURSOR_LEFT_PTR;
2240
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002241 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002242 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002243 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002244 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002245 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002246 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002247 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002248 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002249 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002250 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002251 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002252 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002253 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002254 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002255 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002256 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002257 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002258 case THEME_LOCATION_EXTERIOR:
2259 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002260 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002261 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002262 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002263}
2264
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002265static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002266frame_menu_func(struct window *window,
2267 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002268{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002269 struct display *display;
2270
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002271 switch (index) {
2272 case 0: /* close */
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002273 window_close(window);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002274 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002275 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002276 display = window->display;
2277 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002278 workspace_manager_move_surface(
2279 display->workspace_manager,
2280 window->main_surface->surface,
2281 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002282 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002283 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002284 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002285 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002286 workspace_manager_move_surface(
2287 display->workspace_manager,
2288 window->main_surface->surface,
2289 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002290 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002291 case 3: /* fullscreen */
2292 /* we don't have a way to get out of fullscreen for now */
2293 if (window->fullscreen_handler)
2294 window->fullscreen_handler(window, window->user_data);
2295 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002296 }
2297}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002298
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002299void
2300window_show_frame_menu(struct window *window,
2301 struct input *input, uint32_t time)
2302{
2303 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002304 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002305
2306 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002307 "Close",
2308 "Move to workspace above", "Move to workspace below",
2309 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002310 };
2311
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002312 if (window->fullscreen_handler)
2313 count = ARRAY_LENGTH(entries);
2314 else
2315 count = ARRAY_LENGTH(entries) - 1;
2316
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002317 input_get_position(input, &x, &y);
2318 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002319 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002320}
2321
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002322static int
2323frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002324 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002325{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002326 struct window_frame *frame = data;
2327 enum theme_location location;
2328
2329 location = frame_pointer_enter(frame->frame, input, x, y);
2330 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2331 widget_schedule_redraw(frame->widget);
2332
2333 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002334}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002335
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002336static int
2337frame_motion_handler(struct widget *widget,
2338 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002339 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002340{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002341 struct window_frame *frame = data;
2342 enum theme_location location;
2343
2344 location = frame_pointer_motion(frame->frame, input, x, y);
2345 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2346 widget_schedule_redraw(frame->widget);
2347
2348 return frame_get_pointer_image_for_location(data, location);
2349}
2350
2351static void
2352frame_leave_handler(struct widget *widget,
2353 struct input *input, void *data)
2354{
2355 struct window_frame *frame = data;
2356
2357 frame_pointer_leave(frame->frame, input);
2358 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2359 widget_schedule_redraw(frame->widget);
2360}
2361
2362static void
2363frame_handle_status(struct window_frame *frame, struct input *input,
2364 uint32_t time, enum theme_location location)
2365{
2366 struct window *window = frame->widget->window;
2367 uint32_t status;
2368
2369 status = frame_status(frame->frame);
2370 if (status & FRAME_STATUS_REPAINT)
2371 widget_schedule_redraw(frame->widget);
2372
2373 if (status & FRAME_STATUS_MINIMIZE)
2374 fprintf(stderr,"Minimize stub\n");
2375
2376 if (status & FRAME_STATUS_MENU) {
2377 window_show_frame_menu(window, input, time);
2378 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2379 }
2380
2381 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002382 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002383 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2384 }
2385
2386 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002387 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002388 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002389 }
2390
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002391 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002392 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002393 xdg_surface_move(window->xdg_surface,
2394 input_get_seat(input),
2395 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002396
2397 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2398 }
2399
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002400 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002401 input_ungrab(input);
2402
2403 window->resizing = 1;
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002404 xdg_surface_resize(window->xdg_surface,
2405 input_get_seat(input),
2406 window->display->serial,
2407 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002408
2409 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2410 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002411}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002412
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002413static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002414frame_button_handler(struct widget *widget,
2415 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002416 uint32_t button, enum wl_pointer_button_state state,
2417 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418
2419{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002420 struct window_frame *frame = data;
2421 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002422
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002423 location = frame_pointer_button(frame->frame, input, button, state);
2424 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425}
2426
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002427static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002428frame_touch_down_handler(struct widget *widget, struct input *input,
2429 uint32_t serial, uint32_t time, int32_t id,
2430 float x, float y, void *data)
2431{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002432 struct window_frame *frame = data;
2433
2434 frame_touch_down(frame->frame, input, id, x, y);
2435 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2436}
2437
2438static void
2439frame_touch_up_handler(struct widget *widget,
2440 struct input *input, uint32_t serial, uint32_t time,
2441 int32_t id, void *data)
2442{
2443 struct window_frame *frame = data;
2444
2445 frame_touch_up(frame->frame, input, id);
2446 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002447}
2448
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002449struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002450window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002451{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002452 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002453 uint32_t buttons;
2454
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002455 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002456 buttons = FRAME_BUTTON_NONE;
2457 } else {
2458 buttons = FRAME_BUTTON_ALL;
2459 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002460
Peter Huttererf3d62272013-08-08 11:57:05 +10002461 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002462 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002463 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002464
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002465 frame->widget = window_add_widget(window, frame);
2466 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002467
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002468 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2469 widget_set_resize_handler(frame->widget, frame_resize_handler);
2470 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002471 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002472 widget_set_motion_handler(frame->widget, frame_motion_handler);
2473 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002474 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002475 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002476
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002477 window->frame = frame;
2478
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002479 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002480}
2481
Kristian Høgsberga1627922012-06-20 17:30:03 -04002482void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002483window_frame_set_child_size(struct widget *widget, int child_width,
2484 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002485{
2486 struct display *display = widget->window->display;
2487 struct theme *t = display->theme;
2488 int decoration_width, decoration_height;
2489 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002490 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002491
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002492 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002493 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002494 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002495 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002496
2497 width = child_width + decoration_width;
2498 height = child_height + decoration_height;
2499 } else {
2500 width = child_width;
2501 height = child_height;
2502 }
2503
2504 window_schedule_resize(widget->window, width, height);
2505}
2506
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002507static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002508window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002509{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002510 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002511
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002512 /* frame->child must be destroyed by the application */
2513 widget_destroy(frame->widget);
2514 free(frame);
2515}
2516
2517static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002518input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002519 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002520{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002521 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002522 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002523
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002524 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002525 return;
2526
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002527 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002528 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002529 widget = old;
2530 if (input->grab)
2531 widget = input->grab;
2532 if (widget->leave_handler)
2533 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002534 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002535 }
2536
2537 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002538 widget = focus;
2539 if (input->grab)
2540 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002541 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002542 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002543 cursor = widget->enter_handler(focus, input, x, y,
2544 widget->user_data);
2545 else
2546 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002547
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002548 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002549 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002550}
2551
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002552void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002553touch_grab(struct input *input, int32_t touch_id)
2554{
2555 input->touch_grab = 1;
2556 input->touch_grab_id = touch_id;
2557}
2558
2559void
2560touch_ungrab(struct input *input)
2561{
2562 struct touch_point *tp, *tmp;
2563
2564 input->touch_grab = 0;
2565
2566 wl_list_for_each_safe(tp, tmp,
2567 &input->touch_point_list, link) {
2568 if (tp->id != input->touch_grab_id)
2569 continue;
2570 wl_list_remove(&tp->link);
2571 free(tp);
2572
2573 return;
2574 }
2575}
2576
2577void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002578input_grab(struct input *input, struct widget *widget, uint32_t button)
2579{
2580 input->grab = widget;
2581 input->grab_button = button;
2582}
2583
2584void
2585input_ungrab(struct input *input)
2586{
2587 struct widget *widget;
2588
2589 input->grab = NULL;
2590 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002591 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002592 input->sx, input->sy);
2593 input_set_focus_widget(input, widget, input->sx, input->sy);
2594 }
2595}
2596
2597static void
2598input_remove_pointer_focus(struct input *input)
2599{
2600 struct window *window = input->pointer_focus;
2601
2602 if (!window)
2603 return;
2604
2605 input_set_focus_widget(input, NULL, 0, 0);
2606
2607 input->pointer_focus = NULL;
2608 input->current_cursor = CURSOR_UNSET;
2609}
2610
2611static void
2612pointer_handle_enter(void *data, struct wl_pointer *pointer,
2613 uint32_t serial, struct wl_surface *surface,
2614 wl_fixed_t sx_w, wl_fixed_t sy_w)
2615{
2616 struct input *input = data;
2617 struct window *window;
2618 struct widget *widget;
2619 float sx = wl_fixed_to_double(sx_w);
2620 float sy = wl_fixed_to_double(sy_w);
2621
2622 if (!surface) {
2623 /* enter event for a window we've just destroyed */
2624 return;
2625 }
2626
2627 input->display->serial = serial;
2628 input->pointer_enter_serial = serial;
2629 input->pointer_focus = wl_surface_get_user_data(surface);
2630 window = input->pointer_focus;
2631
Pekka Paalanen99436862012-11-19 17:15:59 +02002632 if (window->resizing) {
2633 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002634 /* Schedule a redraw to free the pool */
2635 window_schedule_redraw(window);
2636 }
2637
2638 input->sx = sx;
2639 input->sy = sy;
2640
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002641 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002642 input_set_focus_widget(input, widget, sx, sy);
2643}
2644
2645static void
2646pointer_handle_leave(void *data, struct wl_pointer *pointer,
2647 uint32_t serial, struct wl_surface *surface)
2648{
2649 struct input *input = data;
2650
2651 input->display->serial = serial;
2652 input_remove_pointer_focus(input);
2653}
2654
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002655static void
Daniel Stone37816df2012-05-16 18:45:18 +01002656pointer_handle_motion(void *data, struct wl_pointer *pointer,
2657 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002658{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002659 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002660 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002661 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002662 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002663 float sx = wl_fixed_to_double(sx_w);
2664 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002665
Paul Winwoodb22bf572013-08-29 10:52:54 +01002666 input->sx = sx;
2667 input->sy = sy;
2668
2669 if (!window)
2670 return;
2671
Rob Bradford5f087742013-07-11 19:41:27 +01002672 /* when making the window smaller - e.g. after a unmaximise we might
2673 * still have a pending motion event that the compositor has picked
2674 * based on the old surface dimensions
2675 */
2676 if (sx > window->main_surface->allocation.width ||
2677 sy > window->main_surface->allocation.height)
2678 return;
2679
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002680 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002681 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002682 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002683 }
2684
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002685 if (input->grab)
2686 widget = input->grab;
2687 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002688 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002689 if (widget) {
2690 if (widget->motion_handler)
2691 cursor = widget->motion_handler(input->focus_widget,
2692 input, time, sx, sy,
2693 widget->user_data);
2694 else
2695 cursor = widget->default_cursor;
2696 } else
2697 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002698
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002699 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002700}
2701
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002702static void
Daniel Stone37816df2012-05-16 18:45:18 +01002703pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002704 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002705{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002706 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002707 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002708 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002709
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002710 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002711 if (input->focus_widget && input->grab == NULL &&
2712 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002713 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002714
Neil Roberts6b28aad2012-01-23 19:11:18 +00002715 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002716 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002717 (*widget->button_handler)(widget,
2718 input, time,
2719 button, state,
2720 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002721
Daniel Stone4dbadb12012-05-30 16:31:51 +01002722 if (input->grab && input->grab_button == button &&
2723 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002724 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002725}
2726
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002727static void
Daniel Stone37816df2012-05-16 18:45:18 +01002728pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002729 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002730{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002731 struct input *input = data;
2732 struct widget *widget;
2733
2734 widget = input->focus_widget;
2735 if (input->grab)
2736 widget = input->grab;
2737 if (widget && widget->axis_handler)
2738 (*widget->axis_handler)(widget,
2739 input, time,
2740 axis, value,
2741 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002742}
2743
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002744static const struct wl_pointer_listener pointer_listener = {
2745 pointer_handle_enter,
2746 pointer_handle_leave,
2747 pointer_handle_motion,
2748 pointer_handle_button,
2749 pointer_handle_axis,
2750};
2751
2752static void
2753input_remove_keyboard_focus(struct input *input)
2754{
2755 struct window *window = input->keyboard_focus;
2756 struct itimerspec its;
2757
2758 its.it_interval.tv_sec = 0;
2759 its.it_interval.tv_nsec = 0;
2760 its.it_value.tv_sec = 0;
2761 its.it_value.tv_nsec = 0;
2762 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2763
2764 if (!window)
2765 return;
2766
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002767 if (window->keyboard_focus_handler)
2768 (*window->keyboard_focus_handler)(window, NULL,
2769 window->user_data);
2770
2771 input->keyboard_focus = NULL;
2772}
2773
2774static void
2775keyboard_repeat_func(struct task *task, uint32_t events)
2776{
2777 struct input *input =
2778 container_of(task, struct input, repeat_task);
2779 struct window *window = input->keyboard_focus;
2780 uint64_t exp;
2781
2782 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2783 /* If we change the timer between the fd becoming
2784 * readable and getting here, there'll be nothing to
2785 * read and we get EAGAIN. */
2786 return;
2787
2788 if (window && window->key_handler) {
2789 (*window->key_handler)(window, input, input->repeat_time,
2790 input->repeat_key, input->repeat_sym,
2791 WL_KEYBOARD_KEY_STATE_PRESSED,
2792 window->user_data);
2793 }
2794}
2795
2796static void
2797keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2798 uint32_t format, int fd, uint32_t size)
2799{
2800 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002801 struct xkb_keymap *keymap;
2802 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002803 char *map_str;
2804
2805 if (!data) {
2806 close(fd);
2807 return;
2808 }
2809
2810 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2811 close(fd);
2812 return;
2813 }
2814
2815 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2816 if (map_str == MAP_FAILED) {
2817 close(fd);
2818 return;
2819 }
2820
Rui Matos3eccb862013-10-10 19:44:22 +02002821 keymap = xkb_map_new_from_string(input->display->xkb_context,
2822 map_str,
2823 XKB_KEYMAP_FORMAT_TEXT_V1,
2824 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002825 munmap(map_str, size);
2826 close(fd);
2827
Rui Matos3eccb862013-10-10 19:44:22 +02002828 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002829 fprintf(stderr, "failed to compile keymap\n");
2830 return;
2831 }
2832
Rui Matos3eccb862013-10-10 19:44:22 +02002833 state = xkb_state_new(keymap);
2834 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002835 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002836 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002837 return;
2838 }
2839
Rui Matos3eccb862013-10-10 19:44:22 +02002840 xkb_keymap_unref(input->xkb.keymap);
2841 xkb_state_unref(input->xkb.state);
2842 input->xkb.keymap = keymap;
2843 input->xkb.state = state;
2844
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002845 input->xkb.control_mask =
2846 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2847 input->xkb.alt_mask =
2848 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2849 input->xkb.shift_mask =
2850 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2851}
2852
2853static void
2854keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2855 uint32_t serial, struct wl_surface *surface,
2856 struct wl_array *keys)
2857{
2858 struct input *input = data;
2859 struct window *window;
2860
2861 input->display->serial = serial;
2862 input->keyboard_focus = wl_surface_get_user_data(surface);
2863
2864 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002865 if (window->keyboard_focus_handler)
2866 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002867 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002868}
2869
2870static void
2871keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2872 uint32_t serial, struct wl_surface *surface)
2873{
2874 struct input *input = data;
2875
2876 input->display->serial = serial;
2877 input_remove_keyboard_focus(input);
2878}
2879
Scott Moreau210d0792012-03-22 10:47:01 -06002880static void
Daniel Stone37816df2012-05-16 18:45:18 +01002881keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002882 uint32_t serial, uint32_t time, uint32_t key,
2883 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002884{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002885 struct input *input = data;
2886 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002887 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002888 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002889 const xkb_keysym_t *syms;
2890 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002891 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002892
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002893 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002894 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002895 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002896 return;
2897
Daniel Stone97f68542012-05-30 16:32:01 +01002898 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002899
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002900 sym = XKB_KEY_NoSymbol;
2901 if (num_syms == 1)
2902 sym = syms[0];
2903
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002904
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002905 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002906 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002907 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002908 } else if (sym == XKB_KEY_F11 &&
2909 window->fullscreen_handler &&
2910 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2911 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002912 } else if (sym == XKB_KEY_F4 &&
2913 input->modifiers == MOD_ALT_MASK &&
2914 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002915 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002916 } else if (window->key_handler) {
2917 (*window->key_handler)(window, input, time, key,
2918 sym, state, window->user_data);
2919 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002920
2921 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2922 key == input->repeat_key) {
2923 its.it_interval.tv_sec = 0;
2924 its.it_interval.tv_nsec = 0;
2925 its.it_value.tv_sec = 0;
2926 its.it_value.tv_nsec = 0;
2927 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002928 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2929 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002930 input->repeat_sym = sym;
2931 input->repeat_key = key;
2932 input->repeat_time = time;
2933 its.it_interval.tv_sec = 0;
2934 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2935 its.it_value.tv_sec = 0;
2936 its.it_value.tv_nsec = 400 * 1000 * 1000;
2937 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2938 }
2939}
2940
2941static void
Daniel Stone351eb612012-05-31 15:27:47 -04002942keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2943 uint32_t serial, uint32_t mods_depressed,
2944 uint32_t mods_latched, uint32_t mods_locked,
2945 uint32_t group)
2946{
2947 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002948 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002949
Matt Ropere61561f2013-06-24 16:52:43 +01002950 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2951 if (!input->xkb.keymap)
2952 return;
2953
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002954 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2955 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002956 mask = xkb_state_serialize_mods(input->xkb.state,
2957 XKB_STATE_DEPRESSED |
2958 XKB_STATE_LATCHED);
2959 input->modifiers = 0;
2960 if (mask & input->xkb.control_mask)
2961 input->modifiers |= MOD_CONTROL_MASK;
2962 if (mask & input->xkb.alt_mask)
2963 input->modifiers |= MOD_ALT_MASK;
2964 if (mask & input->xkb.shift_mask)
2965 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002966}
2967
Daniel Stone37816df2012-05-16 18:45:18 +01002968static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002969 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002970 keyboard_handle_enter,
2971 keyboard_handle_leave,
2972 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002973 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002974};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002975
2976static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002977touch_handle_down(void *data, struct wl_touch *wl_touch,
2978 uint32_t serial, uint32_t time, struct wl_surface *surface,
2979 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2980{
2981 struct input *input = data;
2982 struct widget *widget;
2983 float sx = wl_fixed_to_double(x_w);
2984 float sy = wl_fixed_to_double(y_w);
2985
Rusty Lynch1084da52013-08-15 09:10:08 -07002986 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002987 input->touch_focus = wl_surface_get_user_data(surface);
2988 if (!input->touch_focus) {
2989 DBG("Failed to find to touch focus for surface %p\n", surface);
2990 return;
2991 }
2992
2993 widget = window_find_widget(input->touch_focus,
2994 wl_fixed_to_double(x_w),
2995 wl_fixed_to_double(y_w));
2996 if (widget) {
2997 struct touch_point *tp = xmalloc(sizeof *tp);
2998 if (tp) {
2999 tp->id = id;
3000 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003001 tp->x = sx;
3002 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003003 wl_list_insert(&input->touch_point_list, &tp->link);
3004
3005 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003006 (*widget->touch_down_handler)(widget, input,
3007 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003008 sx, sy,
3009 widget->user_data);
3010 }
3011 }
3012}
3013
3014static void
3015touch_handle_up(void *data, struct wl_touch *wl_touch,
3016 uint32_t serial, uint32_t time, int32_t id)
3017{
3018 struct input *input = data;
3019 struct touch_point *tp, *tmp;
3020
Rusty Lynch041815a2013-08-08 21:20:38 -07003021 if (!input->touch_focus) {
3022 DBG("No touch focus found for touch up event!\n");
3023 return;
3024 }
3025
3026 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3027 if (tp->id != id)
3028 continue;
3029
3030 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003031 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003032 time, id,
3033 tp->widget->user_data);
3034
3035 wl_list_remove(&tp->link);
3036 free(tp);
3037
3038 return;
3039 }
3040}
3041
3042static void
3043touch_handle_motion(void *data, struct wl_touch *wl_touch,
3044 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3045{
3046 struct input *input = data;
3047 struct touch_point *tp;
3048 float sx = wl_fixed_to_double(x_w);
3049 float sy = wl_fixed_to_double(y_w);
3050
3051 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3052
3053 if (!input->touch_focus) {
3054 DBG("No touch focus found for touch motion event!\n");
3055 return;
3056 }
3057
3058 wl_list_for_each(tp, &input->touch_point_list, link) {
3059 if (tp->id != id)
3060 continue;
3061
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003062 tp->x = sx;
3063 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003064 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003065 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003066 id, sx, sy,
3067 tp->widget->user_data);
3068 return;
3069 }
3070}
3071
3072static void
3073touch_handle_frame(void *data, struct wl_touch *wl_touch)
3074{
3075 struct input *input = data;
3076 struct touch_point *tp, *tmp;
3077
3078 DBG("touch_handle_frame\n");
3079
3080 if (!input->touch_focus) {
3081 DBG("No touch focus found for touch frame event!\n");
3082 return;
3083 }
3084
3085 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3086 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003087 (*tp->widget->touch_frame_handler)(tp->widget, input,
3088 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003089
3090 wl_list_remove(&tp->link);
3091 free(tp);
3092 }
3093}
3094
3095static void
3096touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3097{
3098 struct input *input = data;
3099 struct touch_point *tp, *tmp;
3100
3101 DBG("touch_handle_cancel\n");
3102
3103 if (!input->touch_focus) {
3104 DBG("No touch focus found for touch cancel event!\n");
3105 return;
3106 }
3107
3108 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3109 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003110 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3111 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003112
3113 wl_list_remove(&tp->link);
3114 free(tp);
3115 }
3116}
3117
3118static const struct wl_touch_listener touch_listener = {
3119 touch_handle_down,
3120 touch_handle_up,
3121 touch_handle_motion,
3122 touch_handle_frame,
3123 touch_handle_cancel,
3124};
3125
3126static void
Daniel Stone37816df2012-05-16 18:45:18 +01003127seat_handle_capabilities(void *data, struct wl_seat *seat,
3128 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003129{
Daniel Stone37816df2012-05-16 18:45:18 +01003130 struct input *input = data;
3131
3132 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3133 input->pointer = wl_seat_get_pointer(seat);
3134 wl_pointer_set_user_data(input->pointer, input);
3135 wl_pointer_add_listener(input->pointer, &pointer_listener,
3136 input);
3137 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3138 wl_pointer_destroy(input->pointer);
3139 input->pointer = NULL;
3140 }
3141
3142 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3143 input->keyboard = wl_seat_get_keyboard(seat);
3144 wl_keyboard_set_user_data(input->keyboard, input);
3145 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3146 input);
3147 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3148 wl_keyboard_destroy(input->keyboard);
3149 input->keyboard = NULL;
3150 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003151
3152 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3153 input->touch = wl_seat_get_touch(seat);
3154 wl_touch_set_user_data(input->touch, input);
3155 wl_touch_add_listener(input->touch, &touch_listener, input);
3156 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3157 wl_touch_destroy(input->touch);
3158 input->touch = NULL;
3159 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003160}
3161
Rob Bradford08031182013-08-13 20:11:03 +01003162static void
3163seat_handle_name(void *data, struct wl_seat *seat,
3164 const char *name)
3165{
3166
3167}
3168
Daniel Stone37816df2012-05-16 18:45:18 +01003169static const struct wl_seat_listener seat_listener = {
3170 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003171 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003172};
3173
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003174void
3175input_get_position(struct input *input, int32_t *x, int32_t *y)
3176{
3177 *x = input->sx;
3178 *y = input->sy;
3179}
3180
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003181int
3182input_get_touch(struct input *input, int32_t id, float *x, float *y)
3183{
3184 struct touch_point *tp;
3185
3186 wl_list_for_each(tp, &input->touch_point_list, link) {
3187 if (tp->id != id)
3188 continue;
3189
3190 *x = tp->x;
3191 *y = tp->y;
3192 return 0;
3193 }
3194
3195 return -1;
3196}
3197
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003198struct display *
3199input_get_display(struct input *input)
3200{
3201 return input->display;
3202}
3203
Daniel Stone37816df2012-05-16 18:45:18 +01003204struct wl_seat *
3205input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003206{
Daniel Stone37816df2012-05-16 18:45:18 +01003207 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003208}
3209
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003210uint32_t
3211input_get_modifiers(struct input *input)
3212{
3213 return input->modifiers;
3214}
3215
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003216struct widget *
3217input_get_focus_widget(struct input *input)
3218{
3219 return input->focus_widget;
3220}
3221
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003222struct data_offer {
3223 struct wl_data_offer *offer;
3224 struct input *input;
3225 struct wl_array types;
3226 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003227
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003228 struct task io_task;
3229 int fd;
3230 data_func_t func;
3231 int32_t x, y;
3232 void *user_data;
3233};
3234
3235static void
3236data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3237{
3238 struct data_offer *offer = data;
3239 char **p;
3240
3241 p = wl_array_add(&offer->types, sizeof *p);
3242 *p = strdup(type);
3243}
3244
3245static const struct wl_data_offer_listener data_offer_listener = {
3246 data_offer_offer,
3247};
3248
3249static void
3250data_offer_destroy(struct data_offer *offer)
3251{
3252 char **p;
3253
3254 offer->refcount--;
3255 if (offer->refcount == 0) {
3256 wl_data_offer_destroy(offer->offer);
3257 for (p = offer->types.data; *p; p++)
3258 free(*p);
3259 wl_array_release(&offer->types);
3260 free(offer);
3261 }
3262}
3263
3264static void
3265data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003266 struct wl_data_device *data_device,
3267 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003268{
3269 struct data_offer *offer;
3270
Brian Lovinbc919262013-08-07 15:34:59 -07003271 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003272
3273 wl_array_init(&offer->types);
3274 offer->refcount = 1;
3275 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003276 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003277 wl_data_offer_add_listener(offer->offer,
3278 &data_offer_listener, offer);
3279}
3280
3281static void
3282data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003283 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003284 wl_fixed_t x_w, wl_fixed_t y_w,
3285 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003286{
3287 struct input *input = data;
3288 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003289 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003290 float x = wl_fixed_to_double(x_w);
3291 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003292 char **p;
3293
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003295 input->drag_enter_serial = serial;
3296 input->drag_focus = window,
3297 input->drag_x = x;
3298 input->drag_y = y;
3299
3300 if (!input->touch_grab)
3301 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003302
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003303 if (offer) {
3304 input->drag_offer = wl_data_offer_get_user_data(offer);
3305
3306 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3307 *p = NULL;
3308
3309 types_data = input->drag_offer->types.data;
3310 } else {
3311 input->drag_offer = NULL;
3312 types_data = NULL;
3313 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003314
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003315 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003316 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003317 window->user_data);
3318}
3319
3320static void
3321data_device_leave(void *data, struct wl_data_device *data_device)
3322{
3323 struct input *input = data;
3324
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003325 if (input->drag_offer) {
3326 data_offer_destroy(input->drag_offer);
3327 input->drag_offer = NULL;
3328 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003329}
3330
3331static void
3332data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003333 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003334{
3335 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003336 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003337 float x = wl_fixed_to_double(x_w);
3338 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003339 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003340
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003341 input->drag_x = x;
3342 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003343
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003344 if (input->drag_offer)
3345 types_data = input->drag_offer->types.data;
3346 else
3347 types_data = NULL;
3348
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003349 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003350 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003351 window->user_data);
3352}
3353
3354static void
3355data_device_drop(void *data, struct wl_data_device *data_device)
3356{
3357 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003358 struct window *window = input->drag_focus;
3359 float x, y;
3360
3361 x = input->drag_x;
3362 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003363
3364 if (window->drop_handler)
3365 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003366 x, y, window->user_data);
3367
3368 if (input->touch_grab)
3369 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003370}
3371
3372static void
3373data_device_selection(void *data,
3374 struct wl_data_device *wl_data_device,
3375 struct wl_data_offer *offer)
3376{
3377 struct input *input = data;
3378 char **p;
3379
3380 if (input->selection_offer)
3381 data_offer_destroy(input->selection_offer);
3382
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003383 if (offer) {
3384 input->selection_offer = wl_data_offer_get_user_data(offer);
3385 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3386 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003387 } else {
3388 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003389 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003390}
3391
3392static const struct wl_data_device_listener data_device_listener = {
3393 data_device_data_offer,
3394 data_device_enter,
3395 data_device_leave,
3396 data_device_motion,
3397 data_device_drop,
3398 data_device_selection
3399};
3400
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003401static void
3402input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003403{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003404 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003405 struct wl_cursor *cursor;
3406 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003407
Daniel Stone80972742012-11-07 17:51:39 +11003408 if (!input->pointer)
3409 return;
3410
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003411 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003412 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003413 return;
3414
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003415 if (index >= (int) cursor->image_count) {
3416 fprintf(stderr, "cursor index out of range\n");
3417 return;
3418 }
3419
3420 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003421 buffer = wl_cursor_image_get_buffer(image);
3422 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003423 return;
3424
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003425 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3426 wl_surface_damage(input->pointer_surface, 0, 0,
3427 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003428 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003429 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3430 input->pointer_surface,
3431 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003432}
3433
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003434static const struct wl_callback_listener pointer_surface_listener;
3435
3436static void
3437pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3438 uint32_t time)
3439{
3440 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003441 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003442 int i;
3443
3444 if (callback) {
3445 assert(callback == input->cursor_frame_cb);
3446 wl_callback_destroy(callback);
3447 input->cursor_frame_cb = NULL;
3448 }
3449
Daniel Stone80972742012-11-07 17:51:39 +11003450 if (!input->pointer)
3451 return;
3452
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003453 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003454 wl_pointer_set_cursor(input->pointer,
3455 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003456 NULL, 0, 0);
3457 return;
3458 }
3459
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003460 if (input->current_cursor == CURSOR_UNSET)
3461 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003462 cursor = input->display->cursors[input->current_cursor];
3463 if (!cursor)
3464 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003465
3466 /* FIXME We don't have the current time on the first call so we set
3467 * the animation start to the time of the first frame callback. */
3468 if (time == 0)
3469 input->cursor_anim_start = 0;
3470 else if (input->cursor_anim_start == 0)
3471 input->cursor_anim_start = time;
3472
3473 if (time == 0 || input->cursor_anim_start == 0)
3474 i = 0;
3475 else
3476 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3477
Pekka Paalanenbc106382012-10-10 12:49:31 +03003478 if (cursor->image_count > 1) {
3479 input->cursor_frame_cb =
3480 wl_surface_frame(input->pointer_surface);
3481 wl_callback_add_listener(input->cursor_frame_cb,
3482 &pointer_surface_listener, input);
3483 }
3484
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003485 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003486}
3487
3488static const struct wl_callback_listener pointer_surface_listener = {
3489 pointer_surface_frame_callback
3490};
3491
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003492void
3493input_set_pointer_image(struct input *input, int pointer)
3494{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003495 int force = 0;
3496
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003497 if (!input->pointer)
3498 return;
3499
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003500 if (input->pointer_enter_serial > input->cursor_serial)
3501 force = 1;
3502
3503 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003504 return;
3505
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003506 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003507 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003508 if (!input->cursor_frame_cb)
3509 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003510 else if (force) {
3511 /* The current frame callback may be stuck if, for instance,
3512 * the set cursor request was processed by the server after
3513 * this client lost the focus. In this case the cursor surface
3514 * might not be mapped and the frame callback wouldn't ever
3515 * complete. Send a set_cursor and attach to try to map the
3516 * cursor surface again so that the callback will finish */
3517 input_set_pointer_image_index(input, 0);
3518 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003519}
3520
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003521struct wl_data_device *
3522input_get_data_device(struct input *input)
3523{
3524 return input->data_device;
3525}
3526
3527void
3528input_set_selection(struct input *input,
3529 struct wl_data_source *source, uint32_t time)
3530{
3531 wl_data_device_set_selection(input->data_device, source, time);
3532}
3533
3534void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003535input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003536{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003537 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003538 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003539}
3540
3541static void
3542offer_io_func(struct task *task, uint32_t events)
3543{
3544 struct data_offer *offer =
3545 container_of(task, struct data_offer, io_task);
3546 unsigned int len;
3547 char buffer[4096];
3548
3549 len = read(offer->fd, buffer, sizeof buffer);
3550 offer->func(buffer, len,
3551 offer->x, offer->y, offer->user_data);
3552
3553 if (len == 0) {
3554 close(offer->fd);
3555 data_offer_destroy(offer);
3556 }
3557}
3558
3559static void
3560data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3561 data_func_t func, void *user_data)
3562{
3563 int p[2];
3564
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003565 if (pipe2(p, O_CLOEXEC) == -1)
3566 return;
3567
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003568 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3569 close(p[1]);
3570
3571 offer->io_task.run = offer_io_func;
3572 offer->fd = p[0];
3573 offer->func = func;
3574 offer->refcount++;
3575 offer->user_data = user_data;
3576
3577 display_watch_fd(offer->input->display,
3578 offer->fd, EPOLLIN, &offer->io_task);
3579}
3580
3581void
3582input_receive_drag_data(struct input *input, const char *mime_type,
3583 data_func_t func, void *data)
3584{
3585 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003586 input->drag_offer->x = input->drag_x;
3587 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003588}
3589
3590int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003591input_receive_drag_data_to_fd(struct input *input,
3592 const char *mime_type, int fd)
3593{
3594 if (input->drag_offer)
3595 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3596
3597 return 0;
3598}
3599
3600int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003601input_receive_selection_data(struct input *input, const char *mime_type,
3602 data_func_t func, void *data)
3603{
3604 char **p;
3605
3606 if (input->selection_offer == NULL)
3607 return -1;
3608
3609 for (p = input->selection_offer->types.data; *p; p++)
3610 if (strcmp(mime_type, *p) == 0)
3611 break;
3612
3613 if (*p == NULL)
3614 return -1;
3615
3616 data_offer_receive_data(input->selection_offer,
3617 mime_type, func, data);
3618 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003619}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003620
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003621int
3622input_receive_selection_data_to_fd(struct input *input,
3623 const char *mime_type, int fd)
3624{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003625 if (input->selection_offer)
3626 wl_data_offer_receive(input->selection_offer->offer,
3627 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003628
3629 return 0;
3630}
3631
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003632void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003633window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003634{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003635 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003636 return;
3637
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003638 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003639}
3640
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003641static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003642surface_set_synchronized(struct surface *surface)
3643{
3644 if (!surface->subsurface)
3645 return;
3646
3647 if (surface->synchronized)
3648 return;
3649
3650 wl_subsurface_set_sync(surface->subsurface);
3651 surface->synchronized = 1;
3652}
3653
3654static void
3655surface_set_synchronized_default(struct surface *surface)
3656{
3657 if (!surface->subsurface)
3658 return;
3659
3660 if (surface->synchronized == surface->synchronized_default)
3661 return;
3662
3663 if (surface->synchronized_default)
3664 wl_subsurface_set_sync(surface->subsurface);
3665 else
3666 wl_subsurface_set_desync(surface->subsurface);
3667
3668 surface->synchronized = surface->synchronized_default;
3669}
3670
3671static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003672surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003673{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003674 struct widget *widget = surface->widget;
3675 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003676
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003677 if (surface->input_region) {
3678 wl_region_destroy(surface->input_region);
3679 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003680 }
3681
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003682 if (surface->opaque_region)
3683 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003684
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003685 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003686
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003687 if (widget->resize_handler)
3688 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003689 widget->allocation.width,
3690 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003691 widget->user_data);
3692
Pekka Paalanen35e82632013-04-25 13:57:48 +03003693 if (surface->subsurface &&
3694 (surface->allocation.x != widget->allocation.x ||
3695 surface->allocation.y != widget->allocation.y)) {
3696 wl_subsurface_set_position(surface->subsurface,
3697 widget->allocation.x,
3698 widget->allocation.y);
3699 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003700 if (surface->allocation.width != widget->allocation.width ||
3701 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003702 window_schedule_redraw(widget->window);
3703 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003704 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003705
3706 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003707 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003708 widget->allocation.width,
3709 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003710}
3711
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003712static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003713hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3714{
3715 /*
3716 * This hack should be removed, when EGL respects
3717 * eglSwapInterval(0).
3718 *
3719 * If this window has sub-surfaces, especially a free-running
3720 * EGL-widget, we need to post the parent surface once with
3721 * all the old state to guarantee, that the EGL-widget will
3722 * receive its frame callback soon. Otherwise, a forced call
3723 * to eglSwapBuffers may end up blocking, waiting for a frame
3724 * event that will never come, because we will commit the parent
3725 * surface with all new state only after eglSwapBuffers returns.
3726 *
3727 * This assumes, that:
3728 * 1. When the EGL widget's resize hook is called, it pauses.
3729 * 2. When the EGL widget's redraw hook is called, it forces a
3730 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3731 * In a single threaded application condition 1 is a no-op.
3732 *
3733 * XXX: This should actually be after the surface_resize() calls,
3734 * but cannot, because then it would commit the incomplete state
3735 * accumulated from the widget resize hooks.
3736 */
3737 if (window->subsurface_list.next != &window->main_surface->link ||
3738 window->subsurface_list.prev != &window->main_surface->link)
3739 wl_surface_commit(window->main_surface->surface);
3740}
3741
3742static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003743window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003744{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003745 struct surface *surface;
3746
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003747 widget_set_allocation(window->main_surface->widget,
3748 window->pending_allocation.x,
3749 window->pending_allocation.y,
3750 window->pending_allocation.width,
3751 window->pending_allocation.height);
3752
3753 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003754
3755 /* The main surface is in the list, too. Main surface's
3756 * resize_handler is responsible for calling widget_set_allocation()
3757 * on all sub-surface root widgets, so they will be resized
3758 * properly.
3759 */
3760 wl_list_for_each(surface, &window->subsurface_list, link) {
3761 if (surface == window->main_surface)
3762 continue;
3763
3764 surface_set_synchronized(surface);
3765 surface_resize(surface);
3766 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003767}
3768
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003769static void
3770idle_resize(struct window *window)
3771{
3772 window->resize_needed = 0;
3773 window->redraw_needed = 1;
3774
3775 DBG("from %dx%d to %dx%d\n",
3776 window->main_surface->server_allocation.width,
3777 window->main_surface->server_allocation.height,
3778 window->pending_allocation.width,
3779 window->pending_allocation.height);
3780
3781 hack_prevent_EGL_sub_surface_deadlock(window);
3782
3783 window_do_resize(window);
3784}
3785
3786static void
3787undo_resize(struct window *window)
3788{
3789 window->pending_allocation.width =
3790 window->main_surface->server_allocation.width;
3791 window->pending_allocation.height =
3792 window->main_surface->server_allocation.height;
3793
3794 DBG("back to %dx%d\n",
3795 window->main_surface->server_allocation.width,
3796 window->main_surface->server_allocation.height);
3797
3798 window_do_resize(window);
3799
3800 if (window->pending_allocation.width == 0 &&
3801 window->pending_allocation.height == 0) {
3802 fprintf(stderr, "Error: Could not draw a surface, "
3803 "most likely due to insufficient disk space in "
3804 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3805 exit(EXIT_FAILURE);
3806 }
3807}
3808
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003809void
3810window_schedule_resize(struct window *window, int width, int height)
3811{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003812 /* We should probably get these numbers from the theme. */
3813 const int min_width = 200, min_height = 200;
3814
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003815 window->pending_allocation.x = 0;
3816 window->pending_allocation.y = 0;
3817 window->pending_allocation.width = width;
3818 window->pending_allocation.height = height;
3819
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003820 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003821 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003822 window->min_allocation.width = min_width;
3823 else
3824 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003825 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003826 window->min_allocation.height = min_height;
3827 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003828 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003829 }
3830
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003831 if (window->pending_allocation.width < window->min_allocation.width)
3832 window->pending_allocation.width = window->min_allocation.width;
3833 if (window->pending_allocation.height < window->min_allocation.height)
3834 window->pending_allocation.height = window->min_allocation.height;
3835
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003836 window->resize_needed = 1;
3837 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003838}
3839
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003840void
3841widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3842{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003843 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003844}
3845
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003846static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003847handle_surface_ping(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
Scott Moreauff1db4a2012-04-17 19:06:18 -06003848{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003849 xdg_surface_pong(xdg_surface, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003850}
3851
3852static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003853handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003854 int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003855{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003856 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003857
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003858 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003859}
3860
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003861static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003862handle_surface_focused_set(void *data, struct xdg_surface *xdg_surface)
3863{
3864 struct window *window = data;
3865 window->focused = 1;
3866 window_schedule_redraw(window);
3867}
3868
3869static void
3870handle_surface_focused_unset(void *data, struct xdg_surface *xdg_surface)
3871{
3872 struct window *window = data;
3873 window->focused = 0;
3874 window_schedule_redraw(window);
3875}
3876
3877static void
3878handle_surface_request_set_maximized(void *data, struct xdg_surface *xdg_surface)
3879{
3880 struct window *window = data;
3881 window_set_maximized(window, 1);
3882}
3883
3884static void
3885handle_surface_request_unset_maximized(void *data, struct xdg_surface *xdg_surface)
3886{
3887 struct window *window = data;
3888 window_set_maximized(window, 0);
3889}
3890
3891static void
3892handle_surface_request_set_fullscreen(void *data, struct xdg_surface *xdg_surface)
3893{
3894 struct window *window = data;
3895 window_set_fullscreen(window, 1);
3896}
3897
3898static void
3899handle_surface_request_unset_fullscreen(void *data, struct xdg_surface *xdg_surface)
3900{
3901 struct window *window = data;
3902 window_set_fullscreen(window, 0);
3903}
3904
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003905static void
3906handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3907{
3908 struct window *window = data;
3909 window_close(window);
3910}
3911
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003912static const struct xdg_surface_listener xdg_surface_listener = {
3913 handle_surface_ping,
3914 handle_surface_configure,
3915 handle_surface_request_set_maximized,
3916 handle_surface_request_unset_maximized,
3917 handle_surface_request_set_fullscreen,
3918 handle_surface_request_unset_fullscreen,
3919 handle_surface_focused_set,
3920 handle_surface_focused_unset,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003921 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003922};
3923
3924static void
Jasper St. Pierre53686042013-12-09 15:26:25 -05003925window_sync_transient_for(struct window *window)
3926{
3927 struct wl_surface *parent_surface;
3928
3929 if (!window->xdg_surface)
3930 return;
3931
3932 if (window->transient_for)
3933 parent_surface = window->transient_for->main_surface->surface;
3934 else
3935 parent_surface = NULL;
3936
3937 xdg_surface_set_transient_for(window->xdg_surface, parent_surface);
3938}
3939
3940static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003941window_sync_margin(struct window *window)
3942{
3943 int margin;
3944
3945 if (!window->xdg_surface)
3946 return;
3947
3948 if (!window->frame)
3949 return;
3950
3951 margin = frame_get_shadow_margin(window->frame->frame);
3952
3953 /* Shadow size is the same on every side. */
3954 xdg_surface_set_margin(window->xdg_surface,
3955 margin,
3956 margin,
3957 margin,
3958 margin);
3959}
3960
3961static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003962window_flush(struct window *window)
3963{
3964 struct surface *surface;
3965
3966 if (!window->custom) {
3967 if (!window->xdg_popup && !window->xdg_surface) {
3968 window->xdg_surface = xdg_shell_get_xdg_surface(window->display->xdg_shell,
3969 window->main_surface->surface);
3970 fail_on_null(window->xdg_surface);
3971
3972 xdg_surface_set_user_data(window->xdg_surface, window);
3973 xdg_surface_add_listener(window->xdg_surface,
3974 &xdg_surface_listener, window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05003975
3976 window_sync_transient_for(window);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003977 window_sync_margin(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003978 }
3979 }
3980
3981 wl_list_for_each(surface, &window->subsurface_list, link) {
3982 if (surface == window->main_surface)
3983 continue;
3984
3985 surface_flush(surface);
3986 }
3987
3988 surface_flush(window->main_surface);
3989}
3990
3991static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003992menu_destroy(struct menu *menu)
3993{
3994 widget_destroy(menu->widget);
3995 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003996 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003997 free(menu);
3998}
3999
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004000void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004001window_get_allocation(struct window *window,
4002 struct rectangle *allocation)
4003{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004004 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004005}
4006
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004007static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004008widget_redraw(struct widget *widget)
4009{
4010 struct widget *child;
4011
4012 if (widget->redraw_handler)
4013 widget->redraw_handler(widget, widget->user_data);
4014 wl_list_for_each(child, &widget->child_list, link)
4015 widget_redraw(child);
4016}
4017
4018static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004019frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4020{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004021 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004022
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004023 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004024 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004025 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004026 surface->frame_cb = NULL;
4027
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004028 surface->last_time = time;
4029
Pekka Paalanen71233882013-04-25 13:57:53 +03004030 if (surface->redraw_needed || surface->window->redraw_needed) {
4031 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004032 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004033 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004034}
4035
4036static const struct wl_callback_listener listener = {
4037 frame_callback
4038};
4039
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004040static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004041surface_redraw(struct surface *surface)
4042{
Pekka Paalanen71233882013-04-25 13:57:53 +03004043 DBG_OBJ(surface->surface, "begin\n");
4044
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004045 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004046 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004047
4048 /* Whole-window redraw forces a redraw even if the previous has
4049 * not yet hit the screen.
4050 */
4051 if (surface->frame_cb) {
4052 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004053 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004054
Pekka Paalanen71233882013-04-25 13:57:53 +03004055 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004056 wl_callback_destroy(surface->frame_cb);
4057 }
4058
Neil Roberts97b747c2013-12-19 16:17:12 +00004059 if (surface->widget->use_cairo &&
4060 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004061 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4062 return -1;
4063 }
4064
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004065 surface->frame_cb = wl_surface_frame(surface->surface);
4066 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004067 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004068
4069 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004070 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004071 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004072 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004073 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004074}
4075
4076static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004077idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004078{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004079 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004080 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004081 int failed = 0;
4082 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004083
Pekka Paalanen71233882013-04-25 13:57:53 +03004084 DBG(" --------- \n");
4085
Pekka Paalaneneebff542013-04-25 13:57:52 +03004086 wl_list_init(&window->redraw_task.link);
4087 window->redraw_task_scheduled = 0;
4088
4089 if (window->resize_needed) {
4090 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004091 if (window->main_surface->frame_cb) {
4092 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004093 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004094 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004095
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004096 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004097 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004098 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004099
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004100 if (surface_redraw(window->main_surface) < 0) {
4101 /*
4102 * Only main_surface failure will cause us to undo the resize.
4103 * If sub-surfaces fail, they will just be broken with old
4104 * content.
4105 */
4106 failed = 1;
4107 } else {
4108 wl_list_for_each(surface, &window->subsurface_list, link) {
4109 if (surface == window->main_surface)
4110 continue;
4111
4112 surface_redraw(surface);
4113 }
4114 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004115
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004116 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004117 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004118
4119 wl_list_for_each(surface, &window->subsurface_list, link)
4120 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004121
4122 if (resized && failed) {
4123 /* Restore widget tree to correspond to what is on screen. */
4124 undo_resize(window);
4125 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004126}
4127
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004128static void
4129window_schedule_redraw_task(struct window *window)
4130{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004131 if (!window->redraw_task_scheduled) {
4132 window->redraw_task.run = idle_redraw;
4133 display_defer(window->display, &window->redraw_task);
4134 window->redraw_task_scheduled = 1;
4135 }
4136}
4137
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004138void
4139window_schedule_redraw(struct window *window)
4140{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004141 struct surface *surface;
4142
Pekka Paalanen71233882013-04-25 13:57:53 +03004143 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4144
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004145 wl_list_for_each(surface, &window->subsurface_list, link)
4146 surface->redraw_needed = 1;
4147
4148 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004149}
4150
Kristian Høgsberg9a7ad672014-02-05 16:53:43 -08004151static void
4152configure_sync_callback(void *data,
4153 struct wl_callback *callback, uint32_t time)
4154{
4155 struct window *window = data;
4156
4157 DBG("scheduling redraw from maximize sync callback\n");
4158
4159 wl_callback_destroy(callback);
4160
4161 window->redraw_task_scheduled = 0;
4162 window_schedule_redraw_task(window);
4163}
4164
4165static struct wl_callback_listener configure_sync_callback_listener = {
4166 configure_sync_callback,
4167};
4168
4169static void
4170window_delay_redraw(struct window *window)
4171{
4172 struct wl_callback *callback;
4173
4174 DBG("delay scheduled redraw for maximize configure\n");
4175 if (window->redraw_task_scheduled)
4176 wl_list_remove(&window->redraw_task.link);
4177
4178 window->redraw_task_scheduled = 1;
4179 callback = wl_display_sync(window->display->display);
4180 wl_callback_add_listener(callback,
4181 &configure_sync_callback_listener, window);
4182}
4183
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004184int
4185window_is_fullscreen(struct window *window)
4186{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004187 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004188}
4189
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004190void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004191window_set_fullscreen(struct window *window, int fullscreen)
4192{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004193 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004194 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004195
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004196 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004197 return;
4198
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004199 window->fullscreen = fullscreen;
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004200 if (window->fullscreen)
4201 xdg_surface_set_fullscreen(window->xdg_surface);
4202 else
4203 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberg9a7ad672014-02-05 16:53:43 -08004204
4205 window_delay_redraw(window);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004206}
4207
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004208int
4209window_is_maximized(struct window *window)
4210{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004211 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004212}
4213
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004214void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004215window_set_maximized(struct window *window, int maximized)
4216{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004217 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004218 return;
4219
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004220 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004221 return;
4222
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004223 window->maximized = maximized;
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004224 if (window->maximized)
4225 xdg_surface_set_maximized(window->xdg_surface);
4226 else
4227 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsberg9a7ad672014-02-05 16:53:43 -08004228
4229 window_delay_redraw(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004230}
4231
4232void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004233window_set_user_data(struct window *window, void *data)
4234{
4235 window->user_data = data;
4236}
4237
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004238void *
4239window_get_user_data(struct window *window)
4240{
4241 return window->user_data;
4242}
4243
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004244void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004245window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004246 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004247{
4248 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004249}
4250
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004251void
4252window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004253 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004254{
4255 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004256}
4257
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004258void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004259window_set_data_handler(struct window *window, window_data_handler_t handler)
4260{
4261 window->data_handler = handler;
4262}
4263
4264void
4265window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4266{
4267 window->drop_handler = handler;
4268}
4269
4270void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004271window_set_close_handler(struct window *window,
4272 window_close_handler_t handler)
4273{
4274 window->close_handler = handler;
4275}
4276
4277void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004278window_set_fullscreen_handler(struct window *window,
4279 window_fullscreen_handler_t handler)
4280{
4281 window->fullscreen_handler = handler;
4282}
4283
4284void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004285window_set_output_handler(struct window *window,
4286 window_output_handler_t handler)
4287{
4288 window->output_handler = handler;
4289}
4290
4291void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004292window_set_title(struct window *window, const char *title)
4293{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004294 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004295 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004296 if (window->frame) {
4297 frame_set_title(window->frame->frame, title);
4298 widget_schedule_redraw(window->frame->widget);
4299 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004300 if (window->xdg_surface)
4301 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004302}
4303
4304const char *
4305window_get_title(struct window *window)
4306{
4307 return window->title;
4308}
4309
4310void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004311window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4312{
4313 struct text_cursor_position *text_cursor_position =
4314 window->display->text_cursor_position;
4315
Scott Moreau9295ce02012-06-01 12:46:10 -06004316 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004317 return;
4318
4319 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004320 window->main_surface->surface,
4321 wl_fixed_from_int(x),
4322 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004323}
4324
4325void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004326window_damage(struct window *window, int32_t x, int32_t y,
4327 int32_t width, int32_t height)
4328{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004329 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004330}
4331
Casey Dahlin9074db52012-04-19 22:50:09 -04004332static void
4333surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004334 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004335{
Rob Bradford7507b572012-05-15 17:55:34 +01004336 struct window *window = data;
4337 struct output *output;
4338 struct output *output_found = NULL;
4339 struct window_output *window_output;
4340
4341 wl_list_for_each(output, &window->display->output_list, link) {
4342 if (output->output == wl_output) {
4343 output_found = output;
4344 break;
4345 }
4346 }
4347
4348 if (!output_found)
4349 return;
4350
Brian Lovinbc919262013-08-07 15:34:59 -07004351 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004352 window_output->output = output_found;
4353
4354 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004355
4356 if (window->output_handler)
4357 window->output_handler(window, output_found, 1,
4358 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004359}
4360
4361static void
4362surface_leave(void *data,
4363 struct wl_surface *wl_surface, struct wl_output *output)
4364{
Rob Bradford7507b572012-05-15 17:55:34 +01004365 struct window *window = data;
4366 struct window_output *window_output;
4367 struct window_output *window_output_found = NULL;
4368
4369 wl_list_for_each(window_output, &window->window_output_list, link) {
4370 if (window_output->output->output == output) {
4371 window_output_found = window_output;
4372 break;
4373 }
4374 }
4375
4376 if (window_output_found) {
4377 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004378
4379 if (window->output_handler)
4380 window->output_handler(window, window_output->output,
4381 0, window->user_data);
4382
Rob Bradford7507b572012-05-15 17:55:34 +01004383 free(window_output_found);
4384 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004385}
4386
4387static const struct wl_surface_listener surface_listener = {
4388 surface_enter,
4389 surface_leave
4390};
4391
Pekka Paalanen4e373742013-02-13 16:17:13 +02004392static struct surface *
4393surface_create(struct window *window)
4394{
4395 struct display *display = window->display;
4396 struct surface *surface;
4397
Brian Lovinbc919262013-08-07 15:34:59 -07004398 surface = xmalloc(sizeof *surface);
4399 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004400 if (!surface)
4401 return NULL;
4402
4403 surface->window = window;
4404 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004405 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004406 wl_surface_add_listener(surface->surface, &surface_listener, window);
4407
Pekka Paalanen35e82632013-04-25 13:57:48 +03004408 wl_list_insert(&window->subsurface_list, &surface->link);
4409
Pekka Paalanen4e373742013-02-13 16:17:13 +02004410 return surface;
4411}
4412
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004413static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004414window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004415{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004416 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004417 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004418
Peter Huttererf3d62272013-08-08 11:57:05 +10004419 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004420 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004421 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004422
4423 surface = surface_create(window);
4424 window->main_surface = surface;
4425
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004426 fail_on_null(display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004427
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004428 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004429 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004430
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004431 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004432#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004433 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004434#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004435 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004436#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004437 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004438 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004439
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004440 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004441 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004442 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004443
Rob Bradford7507b572012-05-15 17:55:34 +01004444 wl_list_init (&window->window_output_list);
4445
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004446 return window;
4447}
4448
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004449struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004450window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004451{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004452 return window_create_internal(display, 0);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004453}
4454
4455struct window *
4456window_create_custom(struct display *display)
4457{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004458 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004459}
4460
Jasper St. Pierre53686042013-12-09 15:26:25 -05004461void
4462window_set_transient_for(struct window *window,
4463 struct window *parent_window)
4464{
4465 window->transient_for = parent_window;
4466 window_sync_transient_for(window);
4467}
4468
4469struct window *
4470window_get_transient_for(struct window *window)
4471{
4472 return window->transient_for;
4473}
4474
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004475static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004476menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004477{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004478 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004479 int next;
4480
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004481 frame_interior(menu->frame, &x, &y, &width, &height);
4482 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004483 if (menu->current != next) {
4484 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004485 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004486 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004487}
4488
4489static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004490menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004491 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004492 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004493{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004494 struct menu *menu = data;
4495
4496 if (widget == menu->widget)
4497 menu_set_item(data, y);
4498
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004499 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004500}
4501
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004502static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004503menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004504 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004505{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004506 struct menu *menu = data;
4507
4508 if (widget == menu->widget)
4509 menu_set_item(data, y);
4510
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004511 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004512}
4513
4514static void
4515menu_leave_handler(struct widget *widget, struct input *input, void *data)
4516{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004517 struct menu *menu = data;
4518
4519 if (widget == menu->widget)
4520 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004521}
4522
4523static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004524menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004525 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004526 uint32_t button, enum wl_pointer_button_state state,
4527 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004528
4529{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004530 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004531
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004532 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4533 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004534 /* Either relase after press-drag-release or
4535 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004536 menu->func(menu->parent, input,
4537 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004538 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004539 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004540 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004541 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004542 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004543}
4544
4545static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004546menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547{
4548 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004549 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004550 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004551
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004552 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004553
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004554 frame_repaint(menu->frame, cr);
4555 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004556
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004557 theme_set_background_source(menu->window->display->theme,
4558 cr, THEME_FRAME_ACTIVE);
4559 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004560 cairo_fill(cr);
4561
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004562 cairo_select_font_face(cr, "sans",
4563 CAIRO_FONT_SLANT_NORMAL,
4564 CAIRO_FONT_WEIGHT_NORMAL);
4565 cairo_set_font_size(cr, 12);
4566
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004567 for (i = 0; i < menu->count; i++) {
4568 if (i == menu->current) {
4569 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004570 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004571 cairo_fill(cr);
4572 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004573 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004574 cairo_show_text(cr, menu->entries[i]);
4575 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004576 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4577 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004578 cairo_show_text(cr, menu->entries[i]);
4579 }
4580 }
4581
4582 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004583}
4584
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004585static void
4586handle_popup_ping(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4587{
4588 xdg_popup_pong(xdg_popup, serial);
4589}
4590
4591static void
4592handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4593{
4594 struct window *window = data;
4595 struct menu *menu = window->main_surface->widget->user_data;
4596
4597 input_ungrab(menu->input);
4598 menu_destroy(menu);
4599}
4600
4601static const struct xdg_popup_listener xdg_popup_listener = {
4602 handle_popup_ping,
4603 handle_popup_popup_done,
4604};
4605
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004606void
4607window_show_menu(struct display *display,
4608 struct input *input, uint32_t time, struct window *parent,
4609 int32_t x, int32_t y,
4610 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004611{
4612 struct window *window;
4613 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004614 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004615
4616 menu = malloc(sizeof *menu);
4617 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004618 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004619
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004620 window = window_create_internal(parent->display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004621 if (!window) {
4622 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004623 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004624 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004625
4626 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004627 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004628 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004629 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4630 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004631 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004632 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004633 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004634 menu->entries = entries;
4635 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004636 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004637 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004638 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004639 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004640 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004641 window->x = x;
4642 window->y = y;
4643
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004644 input_ungrab(input);
4645
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004646 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004647 widget_set_enter_handler(menu->widget, menu_enter_handler);
4648 widget_set_leave_handler(menu->widget, menu_leave_handler);
4649 widget_set_motion_handler(menu->widget, menu_motion_handler);
4650 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004651
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004652 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004653 frame_resize_inside(menu->frame, 200, count * 20);
4654 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4655 window_schedule_resize(window, frame_width(menu->frame),
4656 frame_height(menu->frame));
4657
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004658 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004659
4660 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4661 window->main_surface->surface,
4662 parent->main_surface->surface,
4663 input->seat,
4664 display_get_serial(window->display),
4665 window->x - ix,
4666 window->y - iy,
4667 0);
4668 fail_on_null(window->xdg_popup);
4669
4670 xdg_popup_set_user_data(window->xdg_popup, window);
4671 xdg_popup_add_listener(window->xdg_popup,
4672 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004673}
4674
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004675void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004676window_set_buffer_type(struct window *window, enum window_buffer_type type)
4677{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004678 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004679}
4680
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004681void
4682window_set_preferred_format(struct window *window,
4683 enum preferred_format format)
4684{
4685 window->preferred_format = format;
4686}
4687
Pekka Paalanen35e82632013-04-25 13:57:48 +03004688struct widget *
4689window_add_subsurface(struct window *window, void *data,
4690 enum subsurface_mode default_mode)
4691{
4692 struct widget *widget;
4693 struct surface *surface;
4694 struct wl_surface *parent;
4695 struct wl_subcompositor *subcompo = window->display->subcompositor;
4696
Pekka Paalanen35e82632013-04-25 13:57:48 +03004697 surface = surface_create(window);
4698 widget = widget_create(window, surface, data);
4699 wl_list_init(&widget->link);
4700 surface->widget = widget;
4701
4702 parent = window->main_surface->surface;
4703 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4704 surface->surface,
4705 parent);
4706 surface->synchronized = 1;
4707
4708 switch (default_mode) {
4709 case SUBSURFACE_SYNCHRONIZED:
4710 surface->synchronized_default = 1;
4711 break;
4712 case SUBSURFACE_DESYNCHRONIZED:
4713 surface->synchronized_default = 0;
4714 break;
4715 default:
4716 assert(!"bad enum subsurface_mode");
4717 }
4718
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004719 window->resize_needed = 1;
4720 window_schedule_redraw(window);
4721
Pekka Paalanen35e82632013-04-25 13:57:48 +03004722 return widget;
4723}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004724
4725static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004726display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004727 struct wl_output *wl_output,
4728 int x, int y,
4729 int physical_width,
4730 int physical_height,
4731 int subpixel,
4732 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004733 const char *model,
4734 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004735{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004736 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004737
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004738 output->allocation.x = x;
4739 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004740 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004741}
4742
4743static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004744display_handle_done(void *data,
4745 struct wl_output *wl_output)
4746{
4747}
4748
4749static void
4750display_handle_scale(void *data,
4751 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004752 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004753{
4754 struct output *output = data;
4755
4756 output->scale = scale;
4757}
4758
4759static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004760display_handle_mode(void *data,
4761 struct wl_output *wl_output,
4762 uint32_t flags,
4763 int width,
4764 int height,
4765 int refresh)
4766{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004767 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004768 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004769
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004770 if (flags & WL_OUTPUT_MODE_CURRENT) {
4771 output->allocation.width = width;
4772 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004773 if (display->output_configure_handler)
4774 (*display->output_configure_handler)(
4775 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004776 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004777}
4778
4779static const struct wl_output_listener output_listener = {
4780 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004781 display_handle_mode,
4782 display_handle_done,
4783 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004784};
4785
4786static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004787display_add_output(struct display *d, uint32_t id)
4788{
4789 struct output *output;
4790
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004791 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004792 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004793 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004794 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004795 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004796 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004797 wl_list_insert(d->output_list.prev, &output->link);
4798
4799 wl_output_add_listener(output->output, &output_listener, output);
4800}
4801
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004802static void
4803output_destroy(struct output *output)
4804{
4805 if (output->destroy_handler)
4806 (*output->destroy_handler)(output, output->user_data);
4807
4808 wl_output_destroy(output->output);
4809 wl_list_remove(&output->link);
4810 free(output);
4811}
4812
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004813static void
4814display_destroy_output(struct display *d, uint32_t id)
4815{
4816 struct output *output;
4817
4818 wl_list_for_each(output, &d->output_list, link) {
4819 if (output->server_output_id == id) {
4820 output_destroy(output);
4821 break;
4822 }
4823 }
4824}
4825
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004826void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004827display_set_global_handler(struct display *display,
4828 display_global_handler_t handler)
4829{
4830 struct global *global;
4831
4832 display->global_handler = handler;
4833 if (!handler)
4834 return;
4835
4836 wl_list_for_each(global, &display->global_list, link)
4837 display->global_handler(display,
4838 global->name, global->interface,
4839 global->version, display->user_data);
4840}
4841
4842void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004843display_set_global_handler_remove(struct display *display,
4844 display_global_handler_t remove_handler)
4845{
4846 display->global_handler_remove = remove_handler;
4847 if (!remove_handler)
4848 return;
4849}
4850
4851void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004852display_set_output_configure_handler(struct display *display,
4853 display_output_handler_t handler)
4854{
4855 struct output *output;
4856
4857 display->output_configure_handler = handler;
4858 if (!handler)
4859 return;
4860
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004861 wl_list_for_each(output, &display->output_list, link) {
4862 if (output->allocation.width == 0 &&
4863 output->allocation.height == 0)
4864 continue;
4865
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004866 (*display->output_configure_handler)(output,
4867 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004868 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004869}
4870
4871void
4872output_set_user_data(struct output *output, void *data)
4873{
4874 output->user_data = data;
4875}
4876
4877void *
4878output_get_user_data(struct output *output)
4879{
4880 return output->user_data;
4881}
4882
4883void
4884output_set_destroy_handler(struct output *output,
4885 display_output_handler_t handler)
4886{
4887 output->destroy_handler = handler;
4888 /* FIXME: implement this, once we have way to remove outputs */
4889}
4890
4891void
Scott Moreau4e072362012-09-29 02:03:11 -06004892output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004893{
Scott Moreau4e072362012-09-29 02:03:11 -06004894 struct rectangle allocation = output->allocation;
4895
4896 switch (output->transform) {
4897 case WL_OUTPUT_TRANSFORM_90:
4898 case WL_OUTPUT_TRANSFORM_270:
4899 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4900 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4901 /* Swap width and height */
4902 allocation.width = output->allocation.height;
4903 allocation.height = output->allocation.width;
4904 break;
4905 }
4906
4907 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004908}
4909
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004910struct wl_output *
4911output_get_wl_output(struct output *output)
4912{
4913 return output->output;
4914}
4915
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004916enum wl_output_transform
4917output_get_transform(struct output *output)
4918{
4919 return output->transform;
4920}
4921
Alexander Larssonafd319a2013-05-22 14:41:27 +02004922uint32_t
4923output_get_scale(struct output *output)
4924{
4925 return output->scale;
4926}
4927
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004928static void
Daniel Stone97f68542012-05-30 16:32:01 +01004929fini_xkb(struct input *input)
4930{
4931 xkb_state_unref(input->xkb.state);
4932 xkb_map_unref(input->xkb.keymap);
4933}
4934
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004935#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004936
Daniel Stone97f68542012-05-30 16:32:01 +01004937static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004938display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004939{
4940 struct input *input;
4941
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004942 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004943 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004944 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004945 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004946 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004947 input->pointer_focus = NULL;
4948 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004949 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004950 wl_list_insert(d->input_list.prev, &input->link);
4951
Daniel Stone37816df2012-05-16 18:45:18 +01004952 wl_seat_add_listener(input->seat, &seat_listener, input);
4953 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004954
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004955 input->data_device =
4956 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004957 input->seat);
4958 wl_data_device_add_listener(input->data_device, &data_device_listener,
4959 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004960
4961 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004962
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004963 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4964 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004965 input->repeat_task.run = keyboard_repeat_func;
4966 display_watch_fd(d, input->repeat_timer_fd,
4967 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004968}
4969
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004970static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004971input_destroy(struct input *input)
4972{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004973 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004974 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004975
4976 if (input->drag_offer)
4977 data_offer_destroy(input->drag_offer);
4978
4979 if (input->selection_offer)
4980 data_offer_destroy(input->selection_offer);
4981
4982 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004983
4984 if (input->display->seat_version >= 3) {
4985 if (input->pointer)
4986 wl_pointer_release(input->pointer);
4987 if (input->keyboard)
4988 wl_keyboard_release(input->keyboard);
4989 }
4990
Daniel Stone97f68542012-05-30 16:32:01 +01004991 fini_xkb(input);
4992
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004993 wl_surface_destroy(input->pointer_surface);
4994
Pekka Paalanene1207c72011-12-16 12:02:09 +02004995 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004996 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004997 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004998 free(input);
4999}
5000
5001static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005002init_workspace_manager(struct display *d, uint32_t id)
5003{
5004 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005005 wl_registry_bind(d->registry, id,
5006 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005007 if (d->workspace_manager != NULL)
5008 workspace_manager_add_listener(d->workspace_manager,
5009 &workspace_manager_listener,
5010 d);
5011}
5012
5013static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005014shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5015{
5016 struct display *d = data;
5017
5018 if (format == WL_SHM_FORMAT_RGB565)
5019 d->has_rgb565 = 1;
5020}
5021
5022struct wl_shm_listener shm_listener = {
5023 shm_format
5024};
5025
5026static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005027registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5028 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005029{
5030 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005031 struct global *global;
5032
Brian Lovinbc919262013-08-07 15:34:59 -07005033 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005034 global->name = id;
5035 global->interface = strdup(interface);
5036 global->version = version;
5037 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005038
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005039 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005040 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005041 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005042 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005043 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005044 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005045 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005046 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005047 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005048 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005049 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005050 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5051 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005052 wl_registry_bind(registry, id,
5053 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005054 } else if (strcmp(interface, "xdg_shell") == 0) {
5055 d->xdg_shell = wl_registry_bind(registry, id,
5056 &xdg_shell_interface, 1);
5057 xdg_shell_use_unstable_version(d->xdg_shell, XDG_SHELL_VERSION_CURRENT);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005058 } else if (strcmp(interface, "text_cursor_position") == 0) {
5059 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005060 wl_registry_bind(registry, id,
5061 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005062 } else if (strcmp(interface, "workspace_manager") == 0) {
5063 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005064 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5065 d->subcompositor =
5066 wl_registry_bind(registry, id,
5067 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005068 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005069
5070 if (d->global_handler)
5071 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005072}
5073
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005074static void
5075registry_handle_global_remove(void *data, struct wl_registry *registry,
5076 uint32_t name)
5077{
5078 struct display *d = data;
5079 struct global *global;
5080 struct global *tmp;
5081
5082 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5083 if (global->name != name)
5084 continue;
5085
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005086 if (strcmp(global->interface, "wl_output") == 0)
5087 display_destroy_output(d, name);
5088
5089 /* XXX: Should destroy remaining bound globals */
5090
5091 if (d->global_handler_remove)
5092 d->global_handler_remove(d, name, global->interface,
5093 global->version, d->user_data);
5094
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005095 wl_list_remove(&global->link);
5096 free(global->interface);
5097 free(global);
5098 }
5099}
5100
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005101void *
5102display_bind(struct display *display, uint32_t name,
5103 const struct wl_interface *interface, uint32_t version)
5104{
5105 return wl_registry_bind(display->registry, name, interface, version);
5106}
5107
5108static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005109 registry_handle_global,
5110 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005111};
5112
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005113#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005114static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005115init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005116{
5117 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005118 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005119
Rob Clark6396ed32012-03-11 19:48:41 -05005120#ifdef USE_CAIRO_GLESV2
5121# define GL_BIT EGL_OPENGL_ES2_BIT
5122#else
5123# define GL_BIT EGL_OPENGL_BIT
5124#endif
5125
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005126 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005127 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005128 EGL_RED_SIZE, 1,
5129 EGL_GREEN_SIZE, 1,
5130 EGL_BLUE_SIZE, 1,
5131 EGL_ALPHA_SIZE, 1,
5132 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005133 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005134 EGL_NONE
5135 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005136
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005137#ifdef USE_CAIRO_GLESV2
5138 static const EGLint context_attribs[] = {
5139 EGL_CONTEXT_CLIENT_VERSION, 2,
5140 EGL_NONE
5141 };
5142 EGLint api = EGL_OPENGL_ES_API;
5143#else
5144 EGLint *context_attribs = NULL;
5145 EGLint api = EGL_OPENGL_API;
5146#endif
5147
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005148 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005149 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005150 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005151 return -1;
5152 }
5153
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005154 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005155 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005156 return -1;
5157 }
5158
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005159 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5160 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005161 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005162 return -1;
5163 }
5164
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005165 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005166 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005167 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005168 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005169 return -1;
5170 }
5171
Benjamin Franzke0c991632011-09-27 21:57:31 +02005172 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5173 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005174 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005175 return -1;
5176 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005177
5178 return 0;
5179}
5180
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005181static void
5182fini_egl(struct display *display)
5183{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005184 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005185
5186 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5187 EGL_NO_CONTEXT);
5188
5189 eglTerminate(display->dpy);
5190 eglReleaseThread();
5191}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005192#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005193
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005194static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005195init_dummy_surface(struct display *display)
5196{
5197 int len;
5198 void *data;
5199
5200 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5201 data = malloc(len);
5202 display->dummy_surface =
5203 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5204 1, 1, len);
5205 display->dummy_surface_data = data;
5206}
5207
5208static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005209handle_display_data(struct task *task, uint32_t events)
5210{
5211 struct display *display =
5212 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005213 struct epoll_event ep;
5214 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005215
5216 display->display_fd_events = events;
5217
5218 if (events & EPOLLERR || events & EPOLLHUP) {
5219 display_exit(display);
5220 return;
5221 }
5222
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005223 if (events & EPOLLIN) {
5224 ret = wl_display_dispatch(display->display);
5225 if (ret == -1) {
5226 display_exit(display);
5227 return;
5228 }
5229 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005230
5231 if (events & EPOLLOUT) {
5232 ret = wl_display_flush(display->display);
5233 if (ret == 0) {
5234 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5235 ep.data.ptr = &display->display_task;
5236 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5237 display->display_fd, &ep);
5238 } else if (ret == -1 && errno != EAGAIN) {
5239 display_exit(display);
5240 return;
5241 }
5242 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005243}
5244
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005245static void
5246log_handler(const char *format, va_list args)
5247{
5248 vfprintf(stderr, format, args);
5249}
5250
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005251struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005252display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005253{
5254 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005255
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005256 wl_log_set_handler_client(log_handler);
5257
Peter Huttererf3d62272013-08-08 11:57:05 +10005258 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005259 if (d == NULL)
5260 return NULL;
5261
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005262 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005263 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005264 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005265 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005266 return NULL;
5267 }
5268
Rob Bradford5ab9c752013-07-26 16:29:43 +01005269 d->xkb_context = xkb_context_new(0);
5270 if (d->xkb_context == NULL) {
5271 fprintf(stderr, "Failed to create XKB context\n");
5272 free(d);
5273 return NULL;
5274 }
5275
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005276 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005277 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005278 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005279 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5280 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005281
5282 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005283 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005284 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005285 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005286
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005287 d->workspace = 0;
5288 d->workspace_count = 1;
5289
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005290 d->registry = wl_display_get_registry(d->display);
5291 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005292
5293 if (wl_display_dispatch(d->display) < 0) {
5294 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5295 return NULL;
5296 }
5297
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005298#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005299 if (init_egl(d) < 0)
5300 fprintf(stderr, "EGL does not seem to work, "
5301 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005302#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005303
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005304 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005305
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005306 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005307
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005308 wl_list_init(&d->window_list);
5309
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005310 init_dummy_surface(d);
5311
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005312 return d;
5313}
5314
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005315static void
5316display_destroy_outputs(struct display *display)
5317{
5318 struct output *tmp;
5319 struct output *output;
5320
5321 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5322 output_destroy(output);
5323}
5324
Pekka Paalanene1207c72011-12-16 12:02:09 +02005325static void
5326display_destroy_inputs(struct display *display)
5327{
5328 struct input *tmp;
5329 struct input *input;
5330
5331 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5332 input_destroy(input);
5333}
5334
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005335void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005336display_destroy(struct display *display)
5337{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005338 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005339 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5340 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005341
5342 if (!wl_list_empty(&display->deferred_list))
5343 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5344
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005345 cairo_surface_destroy(display->dummy_surface);
5346 free(display->dummy_surface_data);
5347
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005348 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005349 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005350
Daniel Stone97f68542012-05-30 16:32:01 +01005351 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005352
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005353 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005354 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005355
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005356#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005357 if (display->argb_device)
5358 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005359#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005360
Pekka Paalanen35e82632013-04-25 13:57:48 +03005361 if (display->subcompositor)
5362 wl_subcompositor_destroy(display->subcompositor);
5363
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005364 if (display->xdg_shell)
5365 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005366
5367 if (display->shm)
5368 wl_shm_destroy(display->shm);
5369
5370 if (display->data_device_manager)
5371 wl_data_device_manager_destroy(display->data_device_manager);
5372
5373 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005374 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005375
5376 close(display->epoll_fd);
5377
U. Artie Eoff44874d92012-10-02 21:12:35 -07005378 if (!(display->display_fd_events & EPOLLERR) &&
5379 !(display->display_fd_events & EPOLLHUP))
5380 wl_display_flush(display->display);
5381
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005382 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005383 free(display);
5384}
5385
5386void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005387display_set_user_data(struct display *display, void *data)
5388{
5389 display->user_data = data;
5390}
5391
5392void *
5393display_get_user_data(struct display *display)
5394{
5395 return display->user_data;
5396}
5397
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005398struct wl_display *
5399display_get_display(struct display *display)
5400{
5401 return display->display;
5402}
5403
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005404int
5405display_has_subcompositor(struct display *display)
5406{
5407 if (display->subcompositor)
5408 return 1;
5409
5410 wl_display_roundtrip(display->display);
5411
5412 return display->subcompositor != NULL;
5413}
5414
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005415cairo_device_t *
5416display_get_cairo_device(struct display *display)
5417{
5418 return display->argb_device;
5419}
5420
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005421struct output *
5422display_get_output(struct display *display)
5423{
5424 return container_of(display->output_list.next, struct output, link);
5425}
5426
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005427struct wl_compositor *
5428display_get_compositor(struct display *display)
5429{
5430 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005431}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005432
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005433uint32_t
5434display_get_serial(struct display *display)
5435{
5436 return display->serial;
5437}
5438
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005439EGLDisplay
5440display_get_egl_display(struct display *d)
5441{
5442 return d->dpy;
5443}
5444
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005445struct wl_data_source *
5446display_create_data_source(struct display *display)
5447{
5448 return wl_data_device_manager_create_data_source(display->data_device_manager);
5449}
5450
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005451EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005452display_get_argb_egl_config(struct display *d)
5453{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005454 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005455}
5456
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005457int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005458display_acquire_window_surface(struct display *display,
5459 struct window *window,
5460 EGLContext ctx)
5461{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005462 struct surface *surface = window->main_surface;
5463
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005464 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005465 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005466
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005467 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005468 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005469}
5470
5471void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005472display_release_window_surface(struct display *display,
5473 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005474{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005475 struct surface *surface = window->main_surface;
5476
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005477 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005478 return;
5479
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005480 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005481}
5482
5483void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005484display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005485{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005486 wl_list_insert(&display->deferred_list, &task->link);
5487}
5488
5489void
5490display_watch_fd(struct display *display,
5491 int fd, uint32_t events, struct task *task)
5492{
5493 struct epoll_event ep;
5494
5495 ep.events = events;
5496 ep.data.ptr = task;
5497 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5498}
5499
5500void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005501display_unwatch_fd(struct display *display, int fd)
5502{
5503 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5504}
5505
5506void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005507display_run(struct display *display)
5508{
5509 struct task *task;
5510 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005511 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005512
Pekka Paalanen826d7952011-12-15 10:14:07 +02005513 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005514 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005515 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005516 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005517 struct task, link);
5518 wl_list_remove(&task->link);
5519 task->run(task, 0);
5520 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005521
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005522 wl_display_dispatch_pending(display->display);
5523
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005524 if (!display->running)
5525 break;
5526
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005527 ret = wl_display_flush(display->display);
5528 if (ret < 0 && errno == EAGAIN) {
5529 ep[0].events =
5530 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5531 ep[0].data.ptr = &display->display_task;
5532
5533 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5534 display->display_fd, &ep[0]);
5535 } else if (ret < 0) {
5536 break;
5537 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005538
5539 count = epoll_wait(display->epoll_fd,
5540 ep, ARRAY_LENGTH(ep), -1);
5541 for (i = 0; i < count; i++) {
5542 task = ep[i].data.ptr;
5543 task->run(task, ep[i].events);
5544 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005545 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005546}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005547
5548void
5549display_exit(struct display *display)
5550{
5551 display->running = 0;
5552}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005553
5554void
5555keysym_modifiers_add(struct wl_array *modifiers_map,
5556 const char *name)
5557{
5558 size_t len = strlen(name) + 1;
5559 char *p;
5560
5561 p = wl_array_add(modifiers_map, len);
5562
5563 if (p == NULL)
5564 return;
5565
5566 strncpy(p, name, len);
5567}
5568
5569static xkb_mod_index_t
5570keysym_modifiers_get_index(struct wl_array *modifiers_map,
5571 const char *name)
5572{
5573 xkb_mod_index_t index = 0;
5574 char *p = modifiers_map->data;
5575
5576 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5577 if (strcmp(p, name) == 0)
5578 return index;
5579
5580 index++;
5581 p += strlen(p) + 1;
5582 }
5583
5584 return XKB_MOD_INVALID;
5585}
5586
5587xkb_mod_mask_t
5588keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5589 const char *name)
5590{
5591 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5592
5593 if (index == XKB_MOD_INVALID)
5594 return XKB_MOD_INVALID;
5595
5596 return 1 << index;
5597}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005598
5599void *
5600fail_on_null(void *p)
5601{
5602 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005603 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005604 exit(EXIT_FAILURE);
5605 }
5606
5607 return p;
5608}
5609
5610void *
5611xmalloc(size_t s)
5612{
5613 return fail_on_null(malloc(s));
5614}
5615
Peter Huttererf3d62272013-08-08 11:57:05 +10005616void *
5617xzalloc(size_t s)
5618{
5619 return fail_on_null(zalloc(s));
5620}
5621
Kristian Høgsbergce278412013-07-25 15:20:20 -07005622char *
5623xstrdup(const char *s)
5624{
5625 return fail_on_null(strdup(s));
5626}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005627
5628void *
5629xrealloc(char *p, size_t s)
5630{
5631 return fail_on_null(realloc(p, s));
5632}