blob: 1d58ad28275e5cc05651ef7033de0b7ba3277dd7 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050068#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030088 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050093 struct xdg_shell *xdg_shell;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800121 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200133
134 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100135 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500136};
137
Rob Bradford7507b572012-05-15 17:55:34 +0100138struct window_output {
139 struct output *output;
140 struct wl_list link;
141};
142
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200143struct toysurface {
144 /*
145 * Prepare the surface for drawing. Makes sure there is a surface
146 * of the right size available for rendering, and returns it.
147 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200148 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200149 * If flags has SURFACE_HINT_RESIZE set, the user is
150 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151 * Returns the Cairo surface to draw to.
152 */
153 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200154 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200155 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156
157 /*
158 * Post the surface to the server, returning the server allocation
159 * rectangle. The Cairo surface from prepare() must be destroyed
160 * after calling this.
161 */
162 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200163 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164 struct rectangle *server_allocation);
165
166 /*
167 * Make the toysurface current with the given EGL context.
168 * Returns 0 on success, and negative of failure.
169 */
170 int (*acquire)(struct toysurface *base, EGLContext ctx);
171
172 /*
173 * Release the toysurface from the EGL context, returning control
174 * to Cairo.
175 */
176 void (*release)(struct toysurface *base);
177
178 /*
179 * Destroy the toysurface, including the Cairo surface, any
180 * backing storage, and the Wayland protocol objects.
181 */
182 void (*destroy)(struct toysurface *base);
183};
184
Pekka Paalanen4e373742013-02-13 16:17:13 +0200185struct surface {
186 struct window *window;
187
188 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300189 struct wl_subsurface *subsurface;
190 int synchronized;
191 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200192 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200193 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300194 int redraw_needed;
195 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300196 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197
198 struct rectangle allocation;
199 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200200
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200201 struct wl_region *input_region;
202 struct wl_region *opaque_region;
203
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200204 enum window_buffer_type buffer_type;
205 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200206 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200207
208 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300209
210 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200211};
212
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500213struct window {
214 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100215 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500216 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200217 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400218 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500219 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500220 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400221 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300222 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400223 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400224 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500225 int custom;
226 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400227
Pekka Paalanen99436862012-11-19 17:15:59 +0200228 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400229
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500230 int fullscreen;
231 int maximized;
232
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700233 int next_attach_serial;
234
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200235 enum preferred_format preferred_format;
236
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500237 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500238 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400239 window_data_handler_t data_handler;
240 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500241 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400242 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200243 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700244 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400245
Pekka Paalanen4e373742013-02-13 16:17:13 +0200246 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500247 struct xdg_surface *xdg_surface;
248 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300249
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700250 struct window *parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500251
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500252 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500253
Pekka Paalanen35e82632013-04-25 13:57:48 +0300254 /* struct surface::link, contains also main_surface */
255 struct wl_list subsurface_list;
256
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500257 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400258 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500259};
260
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500261struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500262 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200263 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300264 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500265 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400266 struct wl_list link;
267 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500268 widget_resize_handler_t resize_handler;
269 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500270 widget_enter_handler_t enter_handler;
271 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500272 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500273 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700274 widget_touch_down_handler_t touch_down_handler;
275 widget_touch_up_handler_t touch_up_handler;
276 widget_touch_motion_handler_t touch_motion_handler;
277 widget_touch_frame_handler_t touch_frame_handler;
278 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200279 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400280 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500281 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300282 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500283 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000284 /* If this is set to false then no cairo surface will be
285 * created before redrawing the surface. This is useful if the
286 * redraw handler is going to do completely custom rendering
287 * such as using EGL directly */
288 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400289};
290
Rusty Lynch041815a2013-08-08 21:20:38 -0700291struct touch_point {
292 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800293 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700294 struct widget *widget;
295 struct wl_list link;
296};
297
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400298struct input {
299 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100300 struct wl_seat *seat;
301 struct wl_pointer *pointer;
302 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700303 struct wl_touch *touch;
304 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400305 struct window *pointer_focus;
306 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700307 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300308 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300309 uint32_t cursor_anim_start;
310 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300311 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400312 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400313 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400314 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400315 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400317
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500318 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500319 struct widget *grab;
320 uint32_t grab_button;
321
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400322 struct wl_data_device *data_device;
323 struct data_offer *drag_offer;
324 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800325 uint32_t touch_grab;
326 int32_t touch_grab_id;
327 float drag_x, drag_y;
328 struct window *drag_focus;
329 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100330
331 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100332 struct xkb_keymap *keymap;
333 struct xkb_state *state;
334 xkb_mod_mask_t control_mask;
335 xkb_mod_mask_t alt_mask;
336 xkb_mod_mask_t shift_mask;
337 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400338
339 struct task repeat_task;
340 int repeat_timer_fd;
341 uint32_t repeat_sym;
342 uint32_t repeat_key;
343 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400344};
345
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500346struct output {
347 struct display *display;
348 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800349 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500350 struct rectangle allocation;
351 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600352 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200353 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500354 char *make;
355 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200356
357 display_output_handler_t destroy_handler;
358 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500359};
360
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500361struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500362 struct widget *widget;
363 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500364 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500365};
366
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500367struct menu {
368 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500369 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500370 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500371 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700372 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500373 const char **entries;
374 uint32_t time;
375 int current;
376 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400377 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500378 menu_func_t func;
379};
380
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300381struct tooltip {
382 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300383 struct widget *widget;
384 char *entry;
385 struct task tooltip_task;
386 int tooltip_fd;
387 float x, y;
388};
389
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700390struct shm_pool {
391 struct wl_shm_pool *pool;
392 size_t size;
393 size_t used;
394 void *data;
395};
396
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400397enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300398 CURSOR_DEFAULT = 100,
399 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400400};
401
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200402static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400403
Pekka Paalanen97777442013-05-22 10:20:05 +0300404/* #define DEBUG */
405
406#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300407
408static void
409debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
410__attribute__ ((format (printf, 4, 5)));
411
412static void
413debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
414{
415 va_list ap;
416 struct timeval tv;
417
418 gettimeofday(&tv, NULL);
419 fprintf(stderr, "%8ld.%03ld ",
420 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
421
422 if (proxy)
423 fprintf(stderr, "%s@%d ",
424 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
425
426 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
427 fprintf(stderr, "%s ", func);
428
429 va_start(ap, fmt);
430 vfprintf(stderr, fmt, ap);
431 va_end(ap);
432}
433
434#define DBG(fmt, ...) \
435 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
436
437#define DBG_OBJ(obj, fmt, ...) \
438 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
439
440#else
441
442#define DBG(...) do {} while (0)
443#define DBG_OBJ(...) do {} while (0)
444
445#endif
446
Alexander Larsson1818e312013-05-22 14:41:31 +0200447static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200448surface_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 +0200449{
450 int32_t tmp;
451
452 switch (buffer_transform) {
453 case WL_OUTPUT_TRANSFORM_90:
454 case WL_OUTPUT_TRANSFORM_270:
455 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
456 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
457 tmp = *width;
458 *width = *height;
459 *height = tmp;
460 break;
461 default:
462 break;
463 }
464
465 *width *= buffer_scale;
466 *height *= buffer_scale;
467}
468
469static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200470buffer_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 +0200471{
472 int32_t tmp;
473
474 switch (buffer_transform) {
475 case WL_OUTPUT_TRANSFORM_90:
476 case WL_OUTPUT_TRANSFORM_270:
477 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
478 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
479 tmp = *width;
480 *width = *height;
481 *height = tmp;
482 break;
483 default:
484 break;
485 }
486
487 *width /= buffer_scale;
488 *height /= buffer_scale;
489}
490
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500491#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400492
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200493struct egl_window_surface {
494 struct toysurface base;
495 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100496 struct display *display;
497 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200498 struct wl_egl_window *egl_window;
499 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100500};
501
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200502static struct egl_window_surface *
503to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100504{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200505 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100506}
507
508static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200509egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200510 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200511 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100512{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513 struct egl_window_surface *surface = to_egl_window_surface(base);
514
Alexander Larsson1818e312013-05-22 14:41:31 +0200515 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
516
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
518 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
519
520 return cairo_surface_reference(surface->cairo_surface);
521}
522
523static void
524egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200525 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526 struct rectangle *server_allocation)
527{
528 struct egl_window_surface *surface = to_egl_window_surface(base);
529
530 cairo_gl_surface_swapbuffers(surface->cairo_surface);
531 wl_egl_window_get_attached_size(surface->egl_window,
532 &server_allocation->width,
533 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200534
535 buffer_to_surface_size (buffer_transform, buffer_scale,
536 &server_allocation->width,
537 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538}
539
540static int
541egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
542{
543 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200544 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100545
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200546 device = cairo_surface_get_device(surface->cairo_surface);
547 if (!device)
548 return -1;
549
550 if (!ctx) {
551 if (device == surface->display->argb_device)
552 ctx = surface->display->argb_ctx;
553 else
554 assert(0);
555 }
556
557 cairo_device_flush(device);
558 cairo_device_acquire(device);
559 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
560 surface->egl_surface, ctx))
561 fprintf(stderr, "failed to make surface current\n");
562
563 return 0;
564}
565
566static void
567egl_window_surface_release(struct toysurface *base)
568{
569 struct egl_window_surface *surface = to_egl_window_surface(base);
570 cairo_device_t *device;
571
572 device = cairo_surface_get_device(surface->cairo_surface);
573 if (!device)
574 return;
575
576 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
577 surface->display->argb_ctx))
578 fprintf(stderr, "failed to make context current\n");
579
580 cairo_device_release(device);
581}
582
583static void
584egl_window_surface_destroy(struct toysurface *base)
585{
586 struct egl_window_surface *surface = to_egl_window_surface(base);
587 struct display *d = surface->display;
588
589 cairo_surface_destroy(surface->cairo_surface);
590 eglDestroySurface(d->dpy, surface->egl_surface);
591 wl_egl_window_destroy(surface->egl_window);
592 surface->surface = NULL;
593
594 free(surface);
595}
596
597static struct toysurface *
598egl_window_surface_create(struct display *display,
599 struct wl_surface *wl_surface,
600 uint32_t flags,
601 struct rectangle *rectangle)
602{
603 struct egl_window_surface *surface;
604
Pekka Paalanenb3627362012-11-19 17:16:00 +0200605 if (display->dpy == EGL_NO_DISPLAY)
606 return NULL;
607
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200608 surface = calloc(1, sizeof *surface);
609 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100610 return NULL;
611
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200612 surface->base.prepare = egl_window_surface_prepare;
613 surface->base.swap = egl_window_surface_swap;
614 surface->base.acquire = egl_window_surface_acquire;
615 surface->base.release = egl_window_surface_release;
616 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100617
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200618 surface->display = display;
619 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400620
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200621 surface->egl_window = wl_egl_window_create(surface->surface,
622 rectangle->width,
623 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100624
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200625 surface->egl_surface = eglCreateWindowSurface(display->dpy,
626 display->argb_config,
627 surface->egl_window,
628 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100629
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200630 surface->cairo_surface =
631 cairo_gl_surface_create_for_egl(display->argb_device,
632 surface->egl_surface,
633 rectangle->width,
634 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200636 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637}
638
Pekka Paalanenb3627362012-11-19 17:16:00 +0200639#else
640
641static struct toysurface *
642egl_window_surface_create(struct display *display,
643 struct wl_surface *wl_surface,
644 uint32_t flags,
645 struct rectangle *rectangle)
646{
647 return NULL;
648}
649
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400650#endif
651
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200652struct shm_surface_data {
653 struct wl_buffer *buffer;
654 struct shm_pool *pool;
655};
656
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400657struct wl_buffer *
658display_get_buffer_for_surface(struct display *display,
659 cairo_surface_t *surface)
660{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200661 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400662
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200663 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400664
665 return data->buffer;
666}
667
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500668static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700669shm_pool_destroy(struct shm_pool *pool);
670
671static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400672shm_surface_data_destroy(void *p)
673{
674 struct shm_surface_data *data = p;
675
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200676 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700677 if (data->pool)
678 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300679
680 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681}
682
Kristian Høgsberg16626282012-04-03 11:21:27 -0400683static struct wl_shm_pool *
684make_shm_pool(struct display *display, int size, void **data)
685{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400686 struct wl_shm_pool *pool;
687 int fd;
688
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300689 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400690 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300691 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
692 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400693 return NULL;
694 }
695
696 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400697 if (*data == MAP_FAILED) {
698 fprintf(stderr, "mmap failed: %m\n");
699 close(fd);
700 return NULL;
701 }
702
703 pool = wl_shm_create_pool(display->shm, fd, size);
704
705 close(fd);
706
707 return pool;
708}
709
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700710static struct shm_pool *
711shm_pool_create(struct display *display, size_t size)
712{
713 struct shm_pool *pool = malloc(sizeof *pool);
714
715 if (!pool)
716 return NULL;
717
718 pool->pool = make_shm_pool(display, size, &pool->data);
719 if (!pool->pool) {
720 free(pool);
721 return NULL;
722 }
723
724 pool->size = size;
725 pool->used = 0;
726
727 return pool;
728}
729
730static void *
731shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
732{
733 if (pool->used + size > pool->size)
734 return NULL;
735
736 *offset = pool->used;
737 pool->used += size;
738
739 return (char *) pool->data + *offset;
740}
741
742/* destroy the pool. this does not unmap the memory though */
743static void
744shm_pool_destroy(struct shm_pool *pool)
745{
746 munmap(pool->data, pool->size);
747 wl_shm_pool_destroy(pool->pool);
748 free(pool);
749}
750
751/* Start allocating from the beginning of the pool again */
752static void
753shm_pool_reset(struct shm_pool *pool)
754{
755 pool->used = 0;
756}
757
758static int
759data_length_for_shm_surface(struct rectangle *rect)
760{
761 int stride;
762
763 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
764 rect->width);
765 return stride * rect->height;
766}
767
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500768static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700769display_create_shm_surface_from_pool(struct display *display,
770 struct rectangle *rectangle,
771 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400772{
773 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400774 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400775 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200776 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700777 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400778 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400779
780 data = malloc(sizeof *data);
781 if (data == NULL)
782 return NULL;
783
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200784 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
785 cairo_format = CAIRO_FORMAT_RGB16_565;
786 else
787 cairo_format = CAIRO_FORMAT_ARGB32;
788
789 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700790 length = stride * rectangle->height;
791 data->pool = NULL;
792 map = shm_pool_allocate(pool, length, &offset);
793
794 if (!map) {
795 free(data);
796 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400797 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400798
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400799 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200800 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400801 rectangle->width,
802 rectangle->height,
803 stride);
804
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200805 cairo_surface_set_user_data(surface, &shm_surface_data_key,
806 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400807
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200808 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
809 format = WL_SHM_FORMAT_RGB565;
810 else {
811 if (flags & SURFACE_OPAQUE)
812 format = WL_SHM_FORMAT_XRGB8888;
813 else
814 format = WL_SHM_FORMAT_ARGB8888;
815 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400816
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200817 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
818 rectangle->width,
819 rectangle->height,
820 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400821
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700822 return surface;
823}
824
825static cairo_surface_t *
826display_create_shm_surface(struct display *display,
827 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200828 struct shm_pool *alternate_pool,
829 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700830{
831 struct shm_surface_data *data;
832 struct shm_pool *pool;
833 cairo_surface_t *surface;
834
Pekka Paalanen99436862012-11-19 17:15:59 +0200835 if (alternate_pool) {
836 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700837 surface = display_create_shm_surface_from_pool(display,
838 rectangle,
839 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200840 alternate_pool);
841 if (surface) {
842 data = cairo_surface_get_user_data(surface,
843 &shm_surface_data_key);
844 goto out;
845 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700846 }
847
848 pool = shm_pool_create(display,
849 data_length_for_shm_surface(rectangle));
850 if (!pool)
851 return NULL;
852
853 surface =
854 display_create_shm_surface_from_pool(display, rectangle,
855 flags, pool);
856
857 if (!surface) {
858 shm_pool_destroy(pool);
859 return NULL;
860 }
861
862 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200863 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700864 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400865
Pekka Paalanen99436862012-11-19 17:15:59 +0200866out:
867 if (data_ret)
868 *data_ret = data;
869
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400870 return surface;
871}
872
nobled7b87cb02011-02-01 18:51:47 +0000873static int
874check_size(struct rectangle *rect)
875{
876 if (rect->width && rect->height)
877 return 0;
878
879 fprintf(stderr, "tried to create surface of "
880 "width: %d, height: %d\n", rect->width, rect->height);
881 return -1;
882}
883
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400884cairo_surface_t *
885display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100886 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400887 struct rectangle *rectangle,
888 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400889{
nobled7b87cb02011-02-01 18:51:47 +0000890 if (check_size(rectangle) < 0)
891 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200892
893 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200894 return display_create_shm_surface(display, rectangle, flags,
895 NULL, NULL);
896}
897
Pekka Paalanena4eda732012-11-19 17:16:02 +0200898struct shm_surface_leaf {
899 cairo_surface_t *cairo_surface;
900 /* 'data' is automatically destroyed, when 'cairo_surface' is */
901 struct shm_surface_data *data;
902
903 struct shm_pool *resize_pool;
904 int busy;
905};
906
907static void
908shm_surface_leaf_release(struct shm_surface_leaf *leaf)
909{
910 if (leaf->cairo_surface)
911 cairo_surface_destroy(leaf->cairo_surface);
912 /* leaf->data already destroyed via cairo private */
913
914 if (leaf->resize_pool)
915 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200916
917 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200918}
919
Pekka Paalanenaef02542013-04-25 13:57:47 +0300920#define MAX_LEAVES 3
921
Pekka Paalanen99436862012-11-19 17:15:59 +0200922struct shm_surface {
923 struct toysurface base;
924 struct display *display;
925 struct wl_surface *surface;
926 uint32_t flags;
927 int dx, dy;
928
Pekka Paalanenaef02542013-04-25 13:57:47 +0300929 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200930 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200931};
932
933static struct shm_surface *
934to_shm_surface(struct toysurface *base)
935{
936 return container_of(base, struct shm_surface, base);
937}
938
Pekka Paalanena4eda732012-11-19 17:16:02 +0200939static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300940shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
941{
942#ifdef DEBUG
943 struct shm_surface_leaf *leaf;
944 char bufs[MAX_LEAVES + 1];
945 int i;
946
947 for (i = 0; i < MAX_LEAVES; i++) {
948 leaf = &surface->leaf[i];
949
950 if (leaf->busy)
951 bufs[i] = 'b';
952 else if (leaf->cairo_surface)
953 bufs[i] = 'a';
954 else
955 bufs[i] = ' ';
956 }
957
958 bufs[MAX_LEAVES] = '\0';
959 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
960#endif
961}
962
963static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200964shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
965{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200966 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300967 struct shm_surface_leaf *leaf;
968 int i;
969 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300970
971 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200972
Pekka Paalanenaef02542013-04-25 13:57:47 +0300973 for (i = 0; i < MAX_LEAVES; i++) {
974 leaf = &surface->leaf[i];
975 if (leaf->data && leaf->data->buffer == buffer) {
976 leaf->busy = 0;
977 break;
978 }
979 }
980 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200981
Pekka Paalanenaef02542013-04-25 13:57:47 +0300982 /* Leave one free leaf with storage, release others */
983 free_found = 0;
984 for (i = 0; i < MAX_LEAVES; i++) {
985 leaf = &surface->leaf[i];
986
987 if (!leaf->cairo_surface || leaf->busy)
988 continue;
989
990 if (!free_found)
991 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300992 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300993 shm_surface_leaf_release(leaf);
994 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300995
Pekka Paalanen97777442013-05-22 10:20:05 +0300996 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200997}
998
999static const struct wl_buffer_listener shm_surface_buffer_listener = {
1000 shm_surface_buffer_release
1001};
1002
Pekka Paalanen99436862012-11-19 17:15:59 +02001003static cairo_surface_t *
1004shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001005 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001006 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001007{
Pekka Paalanenec076692012-11-30 13:37:27 +02001008 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001009 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001010 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001011 struct shm_surface_leaf *leaf = NULL;
1012 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001013
1014 surface->dx = dx;
1015 surface->dy = dy;
1016
Pekka Paalanenaef02542013-04-25 13:57:47 +03001017 /* pick a free buffer, preferrably one that already has storage */
1018 for (i = 0; i < MAX_LEAVES; i++) {
1019 if (surface->leaf[i].busy)
1020 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001021
Pekka Paalanenaef02542013-04-25 13:57:47 +03001022 if (!leaf || surface->leaf[i].cairo_surface)
1023 leaf = &surface->leaf[i];
1024 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001025 DBG_OBJ(surface->surface, "pick leaf %d\n",
1026 (int)(leaf - &surface->leaf[0]));
1027
Pekka Paalanenaef02542013-04-25 13:57:47 +03001028 if (!leaf) {
1029 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001030 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001031 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001032 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001033 }
1034
Pekka Paalanena4eda732012-11-19 17:16:02 +02001035 if (!resize_hint && leaf->resize_pool) {
1036 cairo_surface_destroy(leaf->cairo_surface);
1037 leaf->cairo_surface = NULL;
1038 shm_pool_destroy(leaf->resize_pool);
1039 leaf->resize_pool = NULL;
1040 }
1041
Alexander Larsson1818e312013-05-22 14:41:31 +02001042 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1043
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 if (leaf->cairo_surface &&
1045 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1046 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001047 goto out;
1048
Pekka Paalanena4eda732012-11-19 17:16:02 +02001049 if (leaf->cairo_surface)
1050 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001051
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001052#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001053 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001054 /* Create a big pool to allocate from, while continuously
1055 * resizing. Mmapping a new pool in the server
1056 * is relatively expensive, so reusing a pool performs
1057 * better, but may temporarily reserve unneeded memory.
1058 */
1059 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001060 leaf->resize_pool = shm_pool_create(surface->display,
1061 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001062 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001063#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001064
Alexander Larsson1818e312013-05-22 14:41:31 +02001065 rect.width = width;
1066 rect.height = height;
1067
Pekka Paalanena4eda732012-11-19 17:16:02 +02001068 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001069 display_create_shm_surface(surface->display, &rect,
1070 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001071 leaf->resize_pool,
1072 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001073 if (!leaf->cairo_surface)
1074 return NULL;
1075
Pekka Paalanena4eda732012-11-19 17:16:02 +02001076 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001077 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001078
1079out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001080 surface->current = leaf;
1081
1082 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001083}
1084
1085static void
1086shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001087 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001088 struct rectangle *server_allocation)
1089{
1090 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001091 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001092
1093 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001094 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001095 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001096 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001097
Alexander Larsson1818e312013-05-22 14:41:31 +02001098 buffer_to_surface_size (buffer_transform, buffer_scale,
1099 &server_allocation->width,
1100 &server_allocation->height);
1101
Pekka Paalanena4eda732012-11-19 17:16:02 +02001102 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001103 surface->dx, surface->dy);
1104 wl_surface_damage(surface->surface, 0, 0,
1105 server_allocation->width, server_allocation->height);
1106 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001107
Pekka Paalanen71233882013-04-25 13:57:53 +03001108 DBG_OBJ(surface->surface, "leaf %d busy\n",
1109 (int)(leaf - &surface->leaf[0]));
1110
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 leaf->busy = 1;
1112 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001113}
1114
1115static int
1116shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1117{
1118 return -1;
1119}
1120
1121static void
1122shm_surface_release(struct toysurface *base)
1123{
1124}
1125
1126static void
1127shm_surface_destroy(struct toysurface *base)
1128{
1129 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001130 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001131
Pekka Paalanenaef02542013-04-25 13:57:47 +03001132 for (i = 0; i < MAX_LEAVES; i++)
1133 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001134
1135 free(surface);
1136}
1137
1138static struct toysurface *
1139shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1140 uint32_t flags, struct rectangle *rectangle)
1141{
1142 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001143 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001144
Brian Lovinbc919262013-08-07 15:34:59 -07001145 surface = xmalloc(sizeof *surface);
1146 memset(surface, 0, sizeof *surface);
1147
Pekka Paalanen99436862012-11-19 17:15:59 +02001148 if (!surface)
1149 return NULL;
1150
1151 surface->base.prepare = shm_surface_prepare;
1152 surface->base.swap = shm_surface_swap;
1153 surface->base.acquire = shm_surface_acquire;
1154 surface->base.release = shm_surface_release;
1155 surface->base.destroy = shm_surface_destroy;
1156
1157 surface->display = display;
1158 surface->surface = wl_surface;
1159 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001160
1161 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001162}
1163
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001164/*
1165 * The following correspondences between file names and cursors was copied
1166 * from: https://bugs.kde.org/attachment.cgi?id=67313
1167 */
1168
1169static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001170 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001171 "sw-resize",
1172 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001173};
1174
1175static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001176 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001177 "se-resize",
1178 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001179};
1180
1181static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001182 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001183 "s-resize",
1184 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001185};
1186
1187static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001188 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001189 "closedhand",
1190 "208530c400c041818281048008011002"
1191};
1192
1193static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001194 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001195 "default",
1196 "top_left_arrow",
1197 "left-arrow"
1198};
1199
1200static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001201 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001202 "w-resize",
1203 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001204};
1205
1206static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001207 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001208 "e-resize",
1209 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001210};
1211
1212static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001213 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001214 "nw-resize",
1215 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001216};
1217
1218static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001219 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001220 "ne-resize",
1221 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001222};
1223
1224static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001225 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001226 "n-resize",
1227 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001228};
1229
1230static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001232 "ibeam",
1233 "text"
1234};
1235
1236static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001237 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001238 "pointer",
1239 "pointing_hand",
1240 "e29285e634086352946a0e7090d73106"
1241};
1242
1243static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001244 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001245 "wait",
1246 "0426c94ea35c87780ff01dc239897213"
1247};
1248
1249struct cursor_alternatives {
1250 const char **names;
1251 size_t count;
1252};
1253
1254static const struct cursor_alternatives cursors[] = {
1255 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1256 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1257 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1258 {grabbings, ARRAY_LENGTH(grabbings)},
1259 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1260 {left_sides, ARRAY_LENGTH(left_sides)},
1261 {right_sides, ARRAY_LENGTH(right_sides)},
1262 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1263 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1264 {top_sides, ARRAY_LENGTH(top_sides)},
1265 {xterms, ARRAY_LENGTH(xterms)},
1266 {hand1s, ARRAY_LENGTH(hand1s)},
1267 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001268};
1269
1270static void
1271create_cursors(struct display *display)
1272{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001273 struct weston_config *config;
1274 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001275 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001276 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001277 unsigned int i, j;
1278 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001279
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001280 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001281 s = weston_config_get_section(config, "shell", NULL, NULL);
1282 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1283 weston_config_section_get_int(s, "cursor-size", &size, 32);
1284 weston_config_destroy(config);
1285
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001286 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001287 if (!display->cursor_theme) {
1288 fprintf(stderr, "could not load theme '%s'\n", theme);
1289 return;
1290 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001291 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001292 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001293 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001294
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001295 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001296 cursor = NULL;
1297 for (j = 0; !cursor && j < cursors[i].count; ++j)
1298 cursor = wl_cursor_theme_get_cursor(
1299 display->cursor_theme, cursors[i].names[j]);
1300
1301 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001302 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001303 cursors[i].names[0]);
1304
1305 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001306 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001307}
1308
1309static void
1310destroy_cursors(struct display *display)
1311{
1312 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001313 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001314}
1315
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001316struct wl_cursor_image *
1317display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001318{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001319 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001320
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001321 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001322}
1323
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001324static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001325surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001326{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001327 if (!surface->cairo_surface)
1328 return;
1329
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001330 if (surface->opaque_region) {
1331 wl_surface_set_opaque_region(surface->surface,
1332 surface->opaque_region);
1333 wl_region_destroy(surface->opaque_region);
1334 surface->opaque_region = NULL;
1335 }
1336
1337 if (surface->input_region) {
1338 wl_surface_set_input_region(surface->surface,
1339 surface->input_region);
1340 wl_region_destroy(surface->input_region);
1341 surface->input_region = NULL;
1342 }
1343
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07001344 if (surface->window->next_attach_serial > 0) {
1345 xdg_surface_ack_configure(surface->window->xdg_surface,
1346 surface->window->next_attach_serial);
1347 surface->window->next_attach_serial = 0;
1348 }
1349
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001351 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001353
Pekka Paalanen89dee002013-02-13 16:17:20 +02001354 cairo_surface_destroy(surface->cairo_surface);
1355 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001356}
1357
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001358int
1359window_has_focus(struct window *window)
1360{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001361 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001362}
1363
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001364static void
1365window_close(struct window *window)
1366{
1367 if (window->close_handler)
1368 window->close_handler(window->user_data);
1369 else
1370 display_exit(window->display);
1371}
1372
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001373struct display *
1374window_get_display(struct window *window)
1375{
1376 return window->display;
1377}
1378
Pekka Paalanen89dee002013-02-13 16:17:20 +02001379static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001380surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001381{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001382 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001383 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001384
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001385 if (!surface->toysurface && display->dpy &&
1386 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001387 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001388 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001389 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001390 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001391 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001392 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001393
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001394 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001395 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001396 surface->surface,
1397 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001398
Pekka Paalanen89dee002013-02-13 16:17:20 +02001399 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001400 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001401 allocation.width, allocation.height, flags,
1402 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001403}
1404
1405static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001406window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001408 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410
Pekka Paalanenec076692012-11-30 13:37:27 +02001411 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001412 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001413
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001414 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1415 flags |= SURFACE_HINT_RGB565;
1416
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001417 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001418}
1419
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001420int
1421window_get_buffer_transform(struct window *window)
1422{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001423 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001424}
1425
1426void
1427window_set_buffer_transform(struct window *window,
1428 enum wl_output_transform transform)
1429{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001430 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001431 wl_surface_set_buffer_transform(window->main_surface->surface,
1432 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001433}
1434
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001435void
1436window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001437 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001438{
1439 window->main_surface->buffer_scale = scale;
1440 wl_surface_set_buffer_scale(window->main_surface->surface,
1441 scale);
1442}
1443
1444uint32_t
1445window_get_buffer_scale(struct window *window)
1446{
1447 return window->main_surface->buffer_scale;
1448}
1449
Alexander Larssond68f5232013-05-22 14:41:33 +02001450uint32_t
1451window_get_output_scale(struct window *window)
1452{
1453 struct window_output *window_output;
1454 struct window_output *window_output_tmp;
1455 int scale = 1;
1456
1457 wl_list_for_each_safe(window_output, window_output_tmp,
1458 &window->window_output_list, link) {
1459 if (window_output->output->scale > scale)
1460 scale = window_output->output->scale;
1461 }
1462
1463 return scale;
1464}
1465
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001466static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001467
Pekka Paalanen4e373742013-02-13 16:17:13 +02001468static void
1469surface_destroy(struct surface *surface)
1470{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001471 if (surface->frame_cb)
1472 wl_callback_destroy(surface->frame_cb);
1473
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001474 if (surface->input_region)
1475 wl_region_destroy(surface->input_region);
1476
1477 if (surface->opaque_region)
1478 wl_region_destroy(surface->opaque_region);
1479
Pekka Paalanen35e82632013-04-25 13:57:48 +03001480 if (surface->subsurface)
1481 wl_subsurface_destroy(surface->subsurface);
1482
Pekka Paalanen4e373742013-02-13 16:17:13 +02001483 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001484
1485 if (surface->toysurface)
1486 surface->toysurface->destroy(surface->toysurface);
1487
Pekka Paalanen35e82632013-04-25 13:57:48 +03001488 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001489 free(surface);
1490}
1491
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001492void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001493window_destroy(struct window *window)
1494{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001495 struct display *display = window->display;
1496 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001497 struct window_output *window_output;
1498 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001499
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001500 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001501
Rusty Lynch1084da52013-08-15 09:10:08 -07001502 wl_list_for_each(input, &display->input_list, link) {
1503 if (input->touch_focus == window)
1504 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001505 if (input->pointer_focus == window)
1506 input->pointer_focus = NULL;
1507 if (input->keyboard_focus == window)
1508 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001509 if (input->focus_widget &&
1510 input->focus_widget->window == window)
1511 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001512 }
1513
Rob Bradford7507b572012-05-15 17:55:34 +01001514 wl_list_for_each_safe(window_output, window_output_tmp,
1515 &window->window_output_list, link) {
1516 free (window_output);
1517 }
1518
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001519 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001520 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001521
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001522 if (window->xdg_surface)
1523 xdg_surface_destroy(window->xdg_surface);
1524 if (window->xdg_popup)
1525 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001526
1527 surface_destroy(window->main_surface);
1528
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001529 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001530
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001531 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001532 free(window);
1533}
1534
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001535static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001536widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001537{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001538 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001539
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001540 wl_list_for_each(child, &widget->child_list, link) {
1541 target = widget_find_widget(child, x, y);
1542 if (target)
1543 return target;
1544 }
1545
1546 if (widget->allocation.x <= x &&
1547 x < widget->allocation.x + widget->allocation.width &&
1548 widget->allocation.y <= y &&
1549 y < widget->allocation.y + widget->allocation.height) {
1550 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001551 }
1552
1553 return NULL;
1554}
1555
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001556static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001557window_find_widget(struct window *window, int32_t x, int32_t y)
1558{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001559 struct surface *surface;
1560 struct widget *widget;
1561
1562 wl_list_for_each(surface, &window->subsurface_list, link) {
1563 widget = widget_find_widget(surface->widget, x, y);
1564 if (widget)
1565 return widget;
1566 }
1567
1568 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001569}
1570
1571static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001572widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001573{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001574 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001575
Peter Huttererf3d62272013-08-08 11:57:05 +10001576 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001577 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001578 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001579 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001580 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001581 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001582 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001583 widget->tooltip = NULL;
1584 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001585 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001586 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587
1588 return widget;
1589}
1590
1591struct widget *
1592window_add_widget(struct window *window, void *data)
1593{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001594 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001595
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001596 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001597 wl_list_init(&widget->link);
1598 window->main_surface->widget = widget;
1599
1600 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001601}
1602
1603struct widget *
1604widget_add_widget(struct widget *parent, void *data)
1605{
1606 struct widget *widget;
1607
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001608 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001609 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001610
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001611 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001612}
1613
1614void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001615widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001616{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001617 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001618 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001619 struct input *input;
1620
Pekka Paalanen35e82632013-04-25 13:57:48 +03001621 /* Destroy the sub-surface along with the root widget */
1622 if (surface->widget == widget && surface->subsurface)
1623 surface_destroy(widget->surface);
1624
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001625 if (widget->tooltip)
1626 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001627
Pekka Paalanene156fb62012-01-19 13:51:38 +02001628 wl_list_for_each(input, &display->input_list, link) {
1629 if (input->focus_widget == widget)
1630 input->focus_widget = NULL;
1631 }
1632
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001633 wl_list_remove(&widget->link);
1634 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001635}
1636
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001637void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001638widget_set_default_cursor(struct widget *widget, int cursor)
1639{
1640 widget->default_cursor = cursor;
1641}
1642
1643void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001644widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001645{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001646 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647}
1648
1649void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001650widget_set_size(struct widget *widget, int32_t width, int32_t height)
1651{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001652 widget->allocation.width = width;
1653 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001654}
1655
1656void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001657widget_set_allocation(struct widget *widget,
1658 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001659{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001660 widget->allocation.x = x;
1661 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001662 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663}
1664
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001665void
1666widget_set_transparent(struct widget *widget, int transparent)
1667{
1668 widget->opaque = !transparent;
1669}
1670
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001671void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001672widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001673{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001674 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001675}
1676
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001677static cairo_surface_t *
1678widget_get_cairo_surface(struct widget *widget)
1679{
1680 struct surface *surface = widget->surface;
1681 struct window *window = widget->window;
1682
Neil Roberts97b747c2013-12-19 16:17:12 +00001683 assert(widget->use_cairo);
1684
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001685 if (!surface->cairo_surface) {
1686 if (surface == window->main_surface)
1687 window_create_main_surface(window);
1688 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001689 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001690 }
1691
1692 return surface->cairo_surface;
1693}
1694
Alexander Larsson15901f02013-05-22 14:41:25 +02001695static void
1696widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1697{
1698 struct surface *surface = widget->surface;
1699 double angle;
1700 cairo_matrix_t m;
1701 enum wl_output_transform transform;
1702 int surface_width, surface_height;
1703 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001704 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001705
1706 surface_width = surface->allocation.width;
1707 surface_height = surface->allocation.height;
1708
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001709 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001710 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001711
Alexander Larsson15901f02013-05-22 14:41:25 +02001712 switch (transform) {
1713 case WL_OUTPUT_TRANSFORM_FLIPPED:
1714 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1715 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1716 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1717 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1718 break;
1719 default:
1720 cairo_matrix_init_identity(&m);
1721 break;
1722 }
1723
1724 switch (transform) {
1725 case WL_OUTPUT_TRANSFORM_NORMAL:
1726 default:
1727 angle = 0;
1728 translate_x = 0;
1729 translate_y = 0;
1730 break;
1731 case WL_OUTPUT_TRANSFORM_FLIPPED:
1732 angle = 0;
1733 translate_x = surface_width;
1734 translate_y = 0;
1735 break;
1736 case WL_OUTPUT_TRANSFORM_90:
1737 angle = M_PI_2;
1738 translate_x = surface_height;
1739 translate_y = 0;
1740 break;
1741 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1742 angle = M_PI_2;
1743 translate_x = surface_height;
1744 translate_y = surface_width;
1745 break;
1746 case WL_OUTPUT_TRANSFORM_180:
1747 angle = M_PI;
1748 translate_x = surface_width;
1749 translate_y = surface_height;
1750 break;
1751 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1752 angle = M_PI;
1753 translate_x = 0;
1754 translate_y = surface_height;
1755 break;
1756 case WL_OUTPUT_TRANSFORM_270:
1757 angle = M_PI + M_PI_2;
1758 translate_x = 0;
1759 translate_y = surface_width;
1760 break;
1761 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1762 angle = M_PI + M_PI_2;
1763 translate_x = 0;
1764 translate_y = 0;
1765 break;
1766 }
1767
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001768 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001769 cairo_translate(cr, translate_x, translate_y);
1770 cairo_rotate(cr, angle);
1771 cairo_transform(cr, &m);
1772}
1773
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001774cairo_t *
1775widget_cairo_create(struct widget *widget)
1776{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001777 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001778 cairo_surface_t *cairo_surface;
1779 cairo_t *cr;
1780
1781 cairo_surface = widget_get_cairo_surface(widget);
1782 cr = cairo_create(cairo_surface);
1783
Alexander Larsson15901f02013-05-22 14:41:25 +02001784 widget_cairo_update_transform(widget, cr);
1785
Pekka Paalanen35e82632013-04-25 13:57:48 +03001786 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1787
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001788 return cr;
1789}
1790
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001791struct wl_surface *
1792widget_get_wl_surface(struct widget *widget)
1793{
1794 return widget->surface->surface;
1795}
1796
Neil Roberts5e10a042013-09-09 00:40:17 +01001797struct wl_subsurface *
1798widget_get_wl_subsurface(struct widget *widget)
1799{
1800 return widget->surface->subsurface;
1801}
1802
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001803uint32_t
1804widget_get_last_time(struct widget *widget)
1805{
1806 return widget->surface->last_time;
1807}
1808
1809void
1810widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1811{
1812 struct wl_compositor *comp = widget->window->display->compositor;
1813 struct surface *surface = widget->surface;
1814
1815 if (!surface->input_region)
1816 surface->input_region = wl_compositor_create_region(comp);
1817
1818 if (rect) {
1819 wl_region_add(surface->input_region,
1820 rect->x, rect->y, rect->width, rect->height);
1821 }
1822}
1823
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001824void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001825widget_set_resize_handler(struct widget *widget,
1826 widget_resize_handler_t handler)
1827{
1828 widget->resize_handler = handler;
1829}
1830
1831void
1832widget_set_redraw_handler(struct widget *widget,
1833 widget_redraw_handler_t handler)
1834{
1835 widget->redraw_handler = handler;
1836}
1837
1838void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001839widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001840{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001841 widget->enter_handler = handler;
1842}
1843
1844void
1845widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1846{
1847 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001848}
1849
1850void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001851widget_set_motion_handler(struct widget *widget,
1852 widget_motion_handler_t handler)
1853{
1854 widget->motion_handler = handler;
1855}
1856
1857void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001858widget_set_button_handler(struct widget *widget,
1859 widget_button_handler_t handler)
1860{
1861 widget->button_handler = handler;
1862}
1863
1864void
Rusty Lynch041815a2013-08-08 21:20:38 -07001865widget_set_touch_up_handler(struct widget *widget,
1866 widget_touch_up_handler_t handler)
1867{
1868 widget->touch_up_handler = handler;
1869}
1870
1871void
1872widget_set_touch_down_handler(struct widget *widget,
1873 widget_touch_down_handler_t handler)
1874{
1875 widget->touch_down_handler = handler;
1876}
1877
1878void
1879widget_set_touch_motion_handler(struct widget *widget,
1880 widget_touch_motion_handler_t handler)
1881{
1882 widget->touch_motion_handler = handler;
1883}
1884
1885void
1886widget_set_touch_frame_handler(struct widget *widget,
1887 widget_touch_frame_handler_t handler)
1888{
1889 widget->touch_frame_handler = handler;
1890}
1891
1892void
1893widget_set_touch_cancel_handler(struct widget *widget,
1894 widget_touch_cancel_handler_t handler)
1895{
1896 widget->touch_cancel_handler = handler;
1897}
1898
1899void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001900widget_set_axis_handler(struct widget *widget,
1901 widget_axis_handler_t handler)
1902{
1903 widget->axis_handler = handler;
1904}
1905
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001906static void
1907window_schedule_redraw_task(struct window *window);
1908
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001909void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001910widget_schedule_redraw(struct widget *widget)
1911{
Pekka Paalanen71233882013-04-25 13:57:53 +03001912 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001913 widget->surface->redraw_needed = 1;
1914 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001915}
1916
Neil Roberts97b747c2013-12-19 16:17:12 +00001917void
1918widget_set_use_cairo(struct widget *widget,
1919 int use_cairo)
1920{
1921 widget->use_cairo = use_cairo;
1922}
1923
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001924cairo_surface_t *
1925window_get_surface(struct window *window)
1926{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001927 cairo_surface_t *cairo_surface;
1928
1929 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1930
1931 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001932}
1933
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001934struct wl_surface *
1935window_get_wl_surface(struct window *window)
1936{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001937 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001938}
1939
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001940static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001941tooltip_redraw_handler(struct widget *widget, void *data)
1942{
1943 cairo_t *cr;
1944 const int32_t r = 3;
1945 struct tooltip *tooltip = data;
1946 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001947
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001948 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001949 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001950 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1951 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1952 cairo_paint(cr);
1953
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001954 width = widget->allocation.width;
1955 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001956 rounded_rect(cr, 0, 0, width, height, r);
1957
1958 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1959 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1960 cairo_fill(cr);
1961
1962 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1963 cairo_move_to(cr, 10, 16);
1964 cairo_show_text(cr, tooltip->entry);
1965 cairo_destroy(cr);
1966}
1967
1968static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001969get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001970{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001971 cairo_t *cr;
1972 cairo_text_extents_t extents;
1973
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001974 /* Use the dummy_surface because tooltip's surface was not
1975 * created yet, and parent does not have a valid surface
1976 * outside repaint, either.
1977 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001978 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001979 cairo_text_extents(cr, tooltip->entry, &extents);
1980 cairo_destroy(cr);
1981
1982 return extents;
1983}
1984
1985static int
1986window_create_tooltip(struct tooltip *tooltip)
1987{
1988 struct widget *parent = tooltip->parent;
1989 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001990 const int offset_y = 27;
1991 const int margin = 3;
1992 cairo_text_extents_t extents;
1993
1994 if (tooltip->widget)
1995 return 0;
1996
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001997 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001998
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001999 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002000 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002001 widget_set_allocation(tooltip->widget,
2002 tooltip->x, tooltip->y + offset_y,
2003 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002004
2005 return 0;
2006}
2007
2008void
2009widget_destroy_tooltip(struct widget *parent)
2010{
2011 struct tooltip *tooltip = parent->tooltip;
2012
2013 parent->tooltip_count = 0;
2014 if (!tooltip)
2015 return;
2016
2017 if (tooltip->widget) {
2018 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002020 }
2021
2022 close(tooltip->tooltip_fd);
2023 free(tooltip->entry);
2024 free(tooltip);
2025 parent->tooltip = NULL;
2026}
2027
2028static void
2029tooltip_func(struct task *task, uint32_t events)
2030{
2031 struct tooltip *tooltip =
2032 container_of(task, struct tooltip, tooltip_task);
2033 uint64_t exp;
2034
Martin Olsson8df662a2012-07-08 03:03:47 +02002035 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2036 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002037 window_create_tooltip(tooltip);
2038}
2039
2040#define TOOLTIP_TIMEOUT 500
2041static int
2042tooltip_timer_reset(struct tooltip *tooltip)
2043{
2044 struct itimerspec its;
2045
2046 its.it_interval.tv_sec = 0;
2047 its.it_interval.tv_nsec = 0;
2048 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2049 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2050 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2051 fprintf(stderr, "could not set timerfd\n: %m");
2052 return -1;
2053 }
2054
2055 return 0;
2056}
2057
2058int
2059widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2060{
2061 struct tooltip *tooltip = parent->tooltip;
2062
2063 parent->tooltip_count++;
2064 if (tooltip) {
2065 tooltip->x = x;
2066 tooltip->y = y;
2067 tooltip_timer_reset(tooltip);
2068 return 0;
2069 }
2070
2071 /* the handler might be triggered too fast via input device motion, so
2072 * we need this check here to make sure tooltip is fully initialized */
2073 if (parent->tooltip_count > 1)
2074 return 0;
2075
2076 tooltip = malloc(sizeof *tooltip);
2077 if (!tooltip)
2078 return -1;
2079
2080 parent->tooltip = tooltip;
2081 tooltip->parent = parent;
2082 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002083 tooltip->x = x;
2084 tooltip->y = y;
2085 tooltip->entry = strdup(entry);
2086 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2087 if (tooltip->tooltip_fd < 0) {
2088 fprintf(stderr, "could not create timerfd\n: %m");
2089 return -1;
2090 }
2091
2092 tooltip->tooltip_task.run = tooltip_func;
2093 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2094 EPOLLIN, &tooltip->tooltip_task);
2095 tooltip_timer_reset(tooltip);
2096
2097 return 0;
2098}
2099
2100static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002101workspace_manager_state(void *data,
2102 struct workspace_manager *workspace_manager,
2103 uint32_t current,
2104 uint32_t count)
2105{
2106 struct display *display = data;
2107
2108 display->workspace = current;
2109 display->workspace_count = count;
2110}
2111
2112static const struct workspace_manager_listener workspace_manager_listener = {
2113 workspace_manager_state
2114};
2115
2116static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002117frame_resize_handler(struct widget *widget,
2118 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002119{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002120 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002121 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002122 struct rectangle interior;
2123 struct rectangle input;
2124 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002125
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002126 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002127 interior.x = 0;
2128 interior.y = 0;
2129 interior.width = width;
2130 interior.height = height;
2131 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002132 frame_resize(frame->frame, width, height);
2133 frame_interior(frame->frame, &interior.x, &interior.y,
2134 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002135 }
2136
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002137 widget_set_allocation(child, interior.x, interior.y,
2138 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002140 if (child->resize_handler) {
2141 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002142 child->user_data);
2143
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002144 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002145 width = child->allocation.width;
2146 height = child->allocation.height;
2147 } else {
2148 frame_resize_inside(frame->frame,
2149 child->allocation.width,
2150 child->allocation.height);
2151 width = frame_width(frame->frame);
2152 height = frame_height(frame->frame);
2153 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002154 }
2155
Scott Moreauf7e498c2012-05-14 11:39:29 -06002156 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002157
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002158 widget->surface->input_region =
2159 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002160 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002161 frame_input_rect(frame->frame, &input.x, &input.y,
2162 &input.width, &input.height);
2163 wl_region_add(widget->surface->input_region,
2164 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002165 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002166 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002167 }
2168
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002170
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002171 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002172 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002173 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2174 &opaque.width, &opaque.height);
2175
2176 wl_region_add(widget->surface->opaque_region,
2177 opaque.x, opaque.y,
2178 opaque.width, opaque.height);
2179 } else {
2180 wl_region_add(widget->surface->opaque_region,
2181 0, 0, width, height);
2182 }
Martin Minarik1998b152012-05-10 02:04:35 +02002183 }
2184
Martin Minarik1998b152012-05-10 02:04:35 +02002185
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002186 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002187}
2188
2189static void
2190frame_redraw_handler(struct widget *widget, void *data)
2191{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 struct window *window = widget->window;
2195
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002196 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002197 return;
2198
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002199 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002202
2203 cairo_destroy(cr);
2204}
2205
2206static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002207frame_get_pointer_image_for_location(struct window_frame *frame,
2208 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002209{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002210 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002211
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002212 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002213 return CURSOR_LEFT_PTR;
2214
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002215 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002216 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002217 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002218 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002219 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002220 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002221 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002222 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002223 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002224 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002225 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002226 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002227 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002228 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002229 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002230 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002231 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002232 case THEME_LOCATION_EXTERIOR:
2233 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002234 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002235 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002236 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002237}
2238
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002239static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002240frame_menu_func(struct window *window,
2241 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002242{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002243 struct display *display;
2244
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002245 switch (index) {
2246 case 0: /* close */
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002247 window_close(window);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002248 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002249 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002250 display = window->display;
2251 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002252 workspace_manager_move_surface(
2253 display->workspace_manager,
2254 window->main_surface->surface,
2255 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002256 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002257 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002258 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002259 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002260 workspace_manager_move_surface(
2261 display->workspace_manager,
2262 window->main_surface->surface,
2263 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002264 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002265 case 3: /* fullscreen */
2266 /* we don't have a way to get out of fullscreen for now */
2267 if (window->fullscreen_handler)
2268 window->fullscreen_handler(window, window->user_data);
2269 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002270 }
2271}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002272
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002273void
2274window_show_frame_menu(struct window *window,
2275 struct input *input, uint32_t time)
2276{
2277 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002278 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002279
2280 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002281 "Close",
2282 "Move to workspace above", "Move to workspace below",
2283 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002284 };
2285
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002286 if (window->fullscreen_handler)
2287 count = ARRAY_LENGTH(entries);
2288 else
2289 count = ARRAY_LENGTH(entries) - 1;
2290
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002291 input_get_position(input, &x, &y);
2292 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002293 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002294}
2295
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002296static int
2297frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002298 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002299{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002300 struct window_frame *frame = data;
2301 enum theme_location location;
2302
2303 location = frame_pointer_enter(frame->frame, input, x, y);
2304 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2305 widget_schedule_redraw(frame->widget);
2306
2307 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002308}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002309
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002310static int
2311frame_motion_handler(struct widget *widget,
2312 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002313 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002314{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002315 struct window_frame *frame = data;
2316 enum theme_location location;
2317
2318 location = frame_pointer_motion(frame->frame, input, x, y);
2319 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2320 widget_schedule_redraw(frame->widget);
2321
2322 return frame_get_pointer_image_for_location(data, location);
2323}
2324
2325static void
2326frame_leave_handler(struct widget *widget,
2327 struct input *input, void *data)
2328{
2329 struct window_frame *frame = data;
2330
2331 frame_pointer_leave(frame->frame, input);
2332 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2333 widget_schedule_redraw(frame->widget);
2334}
2335
2336static void
2337frame_handle_status(struct window_frame *frame, struct input *input,
2338 uint32_t time, enum theme_location location)
2339{
2340 struct window *window = frame->widget->window;
2341 uint32_t status;
2342
2343 status = frame_status(frame->frame);
2344 if (status & FRAME_STATUS_REPAINT)
2345 widget_schedule_redraw(frame->widget);
2346
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002347 if (status & FRAME_STATUS_MINIMIZE) {
2348 window_set_minimized(window);
2349 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2350 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002351
2352 if (status & FRAME_STATUS_MENU) {
2353 window_show_frame_menu(window, input, time);
2354 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2355 }
2356
2357 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002358 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002359 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2360 }
2361
2362 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002363 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002364 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002365 }
2366
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002367 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002368 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002369 xdg_surface_move(window->xdg_surface,
2370 input_get_seat(input),
2371 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002372
2373 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2374 }
2375
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002376 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002377 input_ungrab(input);
2378
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002379 xdg_surface_resize(window->xdg_surface,
2380 input_get_seat(input),
2381 window->display->serial,
2382 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002383
2384 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2385 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002386}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002387
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002388static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002389frame_button_handler(struct widget *widget,
2390 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002391 uint32_t button, enum wl_pointer_button_state state,
2392 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002393
2394{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002395 struct window_frame *frame = data;
2396 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002397
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002398 location = frame_pointer_button(frame->frame, input, button, state);
2399 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002400}
2401
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002402static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002403frame_touch_down_handler(struct widget *widget, struct input *input,
2404 uint32_t serial, uint32_t time, int32_t id,
2405 float x, float y, void *data)
2406{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002407 struct window_frame *frame = data;
2408
2409 frame_touch_down(frame->frame, input, id, x, y);
2410 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2411}
2412
2413static void
2414frame_touch_up_handler(struct widget *widget,
2415 struct input *input, uint32_t serial, uint32_t time,
2416 int32_t id, void *data)
2417{
2418 struct window_frame *frame = data;
2419
2420 frame_touch_up(frame->frame, input, id);
2421 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002422}
2423
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002424struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002425window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002426{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002427 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002428 uint32_t buttons;
2429
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002430 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002431 buttons = FRAME_BUTTON_NONE;
2432 } else {
2433 buttons = FRAME_BUTTON_ALL;
2434 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002435
Peter Huttererf3d62272013-08-08 11:57:05 +10002436 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002437 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002438 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002439
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002440 frame->widget = window_add_widget(window, frame);
2441 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002442
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002443 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2444 widget_set_resize_handler(frame->widget, frame_resize_handler);
2445 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002446 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002447 widget_set_motion_handler(frame->widget, frame_motion_handler);
2448 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002449 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002450 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002451
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002452 window->frame = frame;
2453
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002454 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002455}
2456
Kristian Høgsberga1627922012-06-20 17:30:03 -04002457void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002458window_frame_set_child_size(struct widget *widget, int child_width,
2459 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002460{
2461 struct display *display = widget->window->display;
2462 struct theme *t = display->theme;
2463 int decoration_width, decoration_height;
2464 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002465 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002466
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002467 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002468 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002469 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002470 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002471
2472 width = child_width + decoration_width;
2473 height = child_height + decoration_height;
2474 } else {
2475 width = child_width;
2476 height = child_height;
2477 }
2478
2479 window_schedule_resize(widget->window, width, height);
2480}
2481
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002482static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002483window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002484{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002485 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002486
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002487 /* frame->child must be destroyed by the application */
2488 widget_destroy(frame->widget);
2489 free(frame);
2490}
2491
2492static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002493input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002494 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002495{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002496 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002497 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002498
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002499 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002500 return;
2501
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002502 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002503 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002504 widget = old;
2505 if (input->grab)
2506 widget = input->grab;
2507 if (widget->leave_handler)
2508 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002509 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002510 }
2511
2512 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002513 widget = focus;
2514 if (input->grab)
2515 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002516 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002517 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002518 cursor = widget->enter_handler(focus, input, x, y,
2519 widget->user_data);
2520 else
2521 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002522
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002523 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002524 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002525}
2526
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002527void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002528touch_grab(struct input *input, int32_t touch_id)
2529{
2530 input->touch_grab = 1;
2531 input->touch_grab_id = touch_id;
2532}
2533
2534void
2535touch_ungrab(struct input *input)
2536{
2537 struct touch_point *tp, *tmp;
2538
2539 input->touch_grab = 0;
2540
2541 wl_list_for_each_safe(tp, tmp,
2542 &input->touch_point_list, link) {
2543 if (tp->id != input->touch_grab_id)
2544 continue;
2545 wl_list_remove(&tp->link);
2546 free(tp);
2547
2548 return;
2549 }
2550}
2551
2552void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002553input_grab(struct input *input, struct widget *widget, uint32_t button)
2554{
2555 input->grab = widget;
2556 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002557
2558 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002559}
2560
2561void
2562input_ungrab(struct input *input)
2563{
2564 struct widget *widget;
2565
2566 input->grab = NULL;
2567 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002568 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002569 input->sx, input->sy);
2570 input_set_focus_widget(input, widget, input->sx, input->sy);
2571 }
2572}
2573
2574static void
2575input_remove_pointer_focus(struct input *input)
2576{
2577 struct window *window = input->pointer_focus;
2578
2579 if (!window)
2580 return;
2581
2582 input_set_focus_widget(input, NULL, 0, 0);
2583
2584 input->pointer_focus = NULL;
2585 input->current_cursor = CURSOR_UNSET;
2586}
2587
2588static void
2589pointer_handle_enter(void *data, struct wl_pointer *pointer,
2590 uint32_t serial, struct wl_surface *surface,
2591 wl_fixed_t sx_w, wl_fixed_t sy_w)
2592{
2593 struct input *input = data;
2594 struct window *window;
2595 struct widget *widget;
2596 float sx = wl_fixed_to_double(sx_w);
2597 float sy = wl_fixed_to_double(sy_w);
2598
2599 if (!surface) {
2600 /* enter event for a window we've just destroyed */
2601 return;
2602 }
2603
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002604 window = wl_surface_get_user_data(surface);
2605 if (surface != window->main_surface->surface) {
2606 DBG("Ignoring input event from subsurface %p\n", surface);
2607 return;
2608 }
2609
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002610 input->display->serial = serial;
2611 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002612 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002613
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002614 input->sx = sx;
2615 input->sy = sy;
2616
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002617 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002618 input_set_focus_widget(input, widget, sx, sy);
2619}
2620
2621static void
2622pointer_handle_leave(void *data, struct wl_pointer *pointer,
2623 uint32_t serial, struct wl_surface *surface)
2624{
2625 struct input *input = data;
2626
2627 input->display->serial = serial;
2628 input_remove_pointer_focus(input);
2629}
2630
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002631static void
Daniel Stone37816df2012-05-16 18:45:18 +01002632pointer_handle_motion(void *data, struct wl_pointer *pointer,
2633 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002634{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002635 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002636 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002637 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002638 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002639 float sx = wl_fixed_to_double(sx_w);
2640 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002641
Paul Winwoodb22bf572013-08-29 10:52:54 +01002642 if (!window)
2643 return;
2644
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002645 input->sx = sx;
2646 input->sy = sy;
2647
Rob Bradford5f087742013-07-11 19:41:27 +01002648 /* when making the window smaller - e.g. after a unmaximise we might
2649 * still have a pending motion event that the compositor has picked
2650 * based on the old surface dimensions
2651 */
2652 if (sx > window->main_surface->allocation.width ||
2653 sy > window->main_surface->allocation.height)
2654 return;
2655
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002656 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002657 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002658 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002659 }
2660
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002661 if (input->grab)
2662 widget = input->grab;
2663 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002664 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002665 if (widget) {
2666 if (widget->motion_handler)
2667 cursor = widget->motion_handler(input->focus_widget,
2668 input, time, sx, sy,
2669 widget->user_data);
2670 else
2671 cursor = widget->default_cursor;
2672 } else
2673 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002674
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002675 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002676}
2677
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002678static void
Daniel Stone37816df2012-05-16 18:45:18 +01002679pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002680 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002681{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002682 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002683 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002684 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002685
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002686 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002687 if (input->focus_widget && input->grab == NULL &&
2688 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002689 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002690
Neil Roberts6b28aad2012-01-23 19:11:18 +00002691 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002692 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002693 (*widget->button_handler)(widget,
2694 input, time,
2695 button, state,
2696 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002697
Daniel Stone4dbadb12012-05-30 16:31:51 +01002698 if (input->grab && input->grab_button == button &&
2699 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002700 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002701}
2702
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002703static void
Daniel Stone37816df2012-05-16 18:45:18 +01002704pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002705 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002706{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002707 struct input *input = data;
2708 struct widget *widget;
2709
2710 widget = input->focus_widget;
2711 if (input->grab)
2712 widget = input->grab;
2713 if (widget && widget->axis_handler)
2714 (*widget->axis_handler)(widget,
2715 input, time,
2716 axis, value,
2717 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002718}
2719
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002720static const struct wl_pointer_listener pointer_listener = {
2721 pointer_handle_enter,
2722 pointer_handle_leave,
2723 pointer_handle_motion,
2724 pointer_handle_button,
2725 pointer_handle_axis,
2726};
2727
2728static void
2729input_remove_keyboard_focus(struct input *input)
2730{
2731 struct window *window = input->keyboard_focus;
2732 struct itimerspec its;
2733
2734 its.it_interval.tv_sec = 0;
2735 its.it_interval.tv_nsec = 0;
2736 its.it_value.tv_sec = 0;
2737 its.it_value.tv_nsec = 0;
2738 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2739
2740 if (!window)
2741 return;
2742
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002743 if (window->keyboard_focus_handler)
2744 (*window->keyboard_focus_handler)(window, NULL,
2745 window->user_data);
2746
2747 input->keyboard_focus = NULL;
2748}
2749
2750static void
2751keyboard_repeat_func(struct task *task, uint32_t events)
2752{
2753 struct input *input =
2754 container_of(task, struct input, repeat_task);
2755 struct window *window = input->keyboard_focus;
2756 uint64_t exp;
2757
2758 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2759 /* If we change the timer between the fd becoming
2760 * readable and getting here, there'll be nothing to
2761 * read and we get EAGAIN. */
2762 return;
2763
2764 if (window && window->key_handler) {
2765 (*window->key_handler)(window, input, input->repeat_time,
2766 input->repeat_key, input->repeat_sym,
2767 WL_KEYBOARD_KEY_STATE_PRESSED,
2768 window->user_data);
2769 }
2770}
2771
2772static void
2773keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2774 uint32_t format, int fd, uint32_t size)
2775{
2776 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002777 struct xkb_keymap *keymap;
2778 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002779 char *map_str;
2780
2781 if (!data) {
2782 close(fd);
2783 return;
2784 }
2785
2786 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2787 close(fd);
2788 return;
2789 }
2790
2791 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2792 if (map_str == MAP_FAILED) {
2793 close(fd);
2794 return;
2795 }
2796
Rui Matos3eccb862013-10-10 19:44:22 +02002797 keymap = xkb_map_new_from_string(input->display->xkb_context,
2798 map_str,
2799 XKB_KEYMAP_FORMAT_TEXT_V1,
2800 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002801 munmap(map_str, size);
2802 close(fd);
2803
Rui Matos3eccb862013-10-10 19:44:22 +02002804 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002805 fprintf(stderr, "failed to compile keymap\n");
2806 return;
2807 }
2808
Rui Matos3eccb862013-10-10 19:44:22 +02002809 state = xkb_state_new(keymap);
2810 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002811 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002812 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002813 return;
2814 }
2815
Rui Matos3eccb862013-10-10 19:44:22 +02002816 xkb_keymap_unref(input->xkb.keymap);
2817 xkb_state_unref(input->xkb.state);
2818 input->xkb.keymap = keymap;
2819 input->xkb.state = state;
2820
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002821 input->xkb.control_mask =
2822 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2823 input->xkb.alt_mask =
2824 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2825 input->xkb.shift_mask =
2826 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2827}
2828
2829static void
2830keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2831 uint32_t serial, struct wl_surface *surface,
2832 struct wl_array *keys)
2833{
2834 struct input *input = data;
2835 struct window *window;
2836
2837 input->display->serial = serial;
2838 input->keyboard_focus = wl_surface_get_user_data(surface);
2839
2840 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002841 if (window->keyboard_focus_handler)
2842 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002843 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002844}
2845
2846static void
2847keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2848 uint32_t serial, struct wl_surface *surface)
2849{
2850 struct input *input = data;
2851
2852 input->display->serial = serial;
2853 input_remove_keyboard_focus(input);
2854}
2855
Scott Moreau210d0792012-03-22 10:47:01 -06002856static void
Daniel Stone37816df2012-05-16 18:45:18 +01002857keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002858 uint32_t serial, uint32_t time, uint32_t key,
2859 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002860{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002861 struct input *input = data;
2862 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002863 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002864 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002865 const xkb_keysym_t *syms;
2866 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002867 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002868
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002869 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002870 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002871 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002872 return;
2873
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002874 /* We only use input grabs for pointer events for now, so just
2875 * ignore key presses if a grab is active. We expand the key
2876 * event delivery mechanism to route events to widgets to
2877 * properly handle key grabs. In the meantime, this prevents
2878 * key event devlivery while a grab is active. */
2879 if (input->grab && input->grab_button == 0)
2880 return;
2881
Daniel Stone97f68542012-05-30 16:32:01 +01002882 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002883
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002884 sym = XKB_KEY_NoSymbol;
2885 if (num_syms == 1)
2886 sym = syms[0];
2887
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002888
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002889 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002890 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002891 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002892 } else if (sym == XKB_KEY_F11 &&
2893 window->fullscreen_handler &&
2894 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2895 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002896 } else if (sym == XKB_KEY_F4 &&
2897 input->modifiers == MOD_ALT_MASK &&
2898 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002899 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002900 } else if (window->key_handler) {
2901 (*window->key_handler)(window, input, time, key,
2902 sym, state, window->user_data);
2903 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002904
2905 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2906 key == input->repeat_key) {
2907 its.it_interval.tv_sec = 0;
2908 its.it_interval.tv_nsec = 0;
2909 its.it_value.tv_sec = 0;
2910 its.it_value.tv_nsec = 0;
2911 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002912 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2913 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002914 input->repeat_sym = sym;
2915 input->repeat_key = key;
2916 input->repeat_time = time;
2917 its.it_interval.tv_sec = 0;
2918 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2919 its.it_value.tv_sec = 0;
2920 its.it_value.tv_nsec = 400 * 1000 * 1000;
2921 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2922 }
2923}
2924
2925static void
Daniel Stone351eb612012-05-31 15:27:47 -04002926keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2927 uint32_t serial, uint32_t mods_depressed,
2928 uint32_t mods_latched, uint32_t mods_locked,
2929 uint32_t group)
2930{
2931 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002932 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002933
Matt Ropere61561f2013-06-24 16:52:43 +01002934 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2935 if (!input->xkb.keymap)
2936 return;
2937
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002938 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2939 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002940 mask = xkb_state_serialize_mods(input->xkb.state,
2941 XKB_STATE_DEPRESSED |
2942 XKB_STATE_LATCHED);
2943 input->modifiers = 0;
2944 if (mask & input->xkb.control_mask)
2945 input->modifiers |= MOD_CONTROL_MASK;
2946 if (mask & input->xkb.alt_mask)
2947 input->modifiers |= MOD_ALT_MASK;
2948 if (mask & input->xkb.shift_mask)
2949 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002950}
2951
Daniel Stone37816df2012-05-16 18:45:18 +01002952static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002953 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002954 keyboard_handle_enter,
2955 keyboard_handle_leave,
2956 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002957 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002958};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002959
2960static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002961touch_handle_down(void *data, struct wl_touch *wl_touch,
2962 uint32_t serial, uint32_t time, struct wl_surface *surface,
2963 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2964{
2965 struct input *input = data;
2966 struct widget *widget;
2967 float sx = wl_fixed_to_double(x_w);
2968 float sy = wl_fixed_to_double(y_w);
2969
Rusty Lynch1084da52013-08-15 09:10:08 -07002970 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002971 input->touch_focus = wl_surface_get_user_data(surface);
2972 if (!input->touch_focus) {
2973 DBG("Failed to find to touch focus for surface %p\n", surface);
2974 return;
2975 }
2976
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002977 if (surface != input->touch_focus->main_surface->surface) {
2978 DBG("Ignoring input event from subsurface %p\n", surface);
2979 input->touch_focus = NULL;
2980 return;
2981 }
2982
Kristian Høgsberg1f671172014-04-29 14:30:44 -07002983 if (input->grab)
2984 widget = input->grab;
2985 else
2986 widget = window_find_widget(input->touch_focus,
2987 wl_fixed_to_double(x_w),
2988 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07002989 if (widget) {
2990 struct touch_point *tp = xmalloc(sizeof *tp);
2991 if (tp) {
2992 tp->id = id;
2993 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08002994 tp->x = sx;
2995 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07002996 wl_list_insert(&input->touch_point_list, &tp->link);
2997
2998 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07002999 (*widget->touch_down_handler)(widget, input,
3000 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003001 sx, sy,
3002 widget->user_data);
3003 }
3004 }
3005}
3006
3007static void
3008touch_handle_up(void *data, struct wl_touch *wl_touch,
3009 uint32_t serial, uint32_t time, int32_t id)
3010{
3011 struct input *input = data;
3012 struct touch_point *tp, *tmp;
3013
Rusty Lynch041815a2013-08-08 21:20:38 -07003014 if (!input->touch_focus) {
3015 DBG("No touch focus found for touch up event!\n");
3016 return;
3017 }
3018
3019 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3020 if (tp->id != id)
3021 continue;
3022
3023 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003024 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003025 time, id,
3026 tp->widget->user_data);
3027
3028 wl_list_remove(&tp->link);
3029 free(tp);
3030
3031 return;
3032 }
3033}
3034
3035static void
3036touch_handle_motion(void *data, struct wl_touch *wl_touch,
3037 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3038{
3039 struct input *input = data;
3040 struct touch_point *tp;
3041 float sx = wl_fixed_to_double(x_w);
3042 float sy = wl_fixed_to_double(y_w);
3043
3044 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3045
3046 if (!input->touch_focus) {
3047 DBG("No touch focus found for touch motion event!\n");
3048 return;
3049 }
3050
3051 wl_list_for_each(tp, &input->touch_point_list, link) {
3052 if (tp->id != id)
3053 continue;
3054
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003055 tp->x = sx;
3056 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003057 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003058 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003059 id, sx, sy,
3060 tp->widget->user_data);
3061 return;
3062 }
3063}
3064
3065static void
3066touch_handle_frame(void *data, struct wl_touch *wl_touch)
3067{
3068 struct input *input = data;
3069 struct touch_point *tp, *tmp;
3070
3071 DBG("touch_handle_frame\n");
3072
3073 if (!input->touch_focus) {
3074 DBG("No touch focus found for touch frame event!\n");
3075 return;
3076 }
3077
3078 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3079 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003080 (*tp->widget->touch_frame_handler)(tp->widget, input,
3081 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003082 }
3083}
3084
3085static void
3086touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3087{
3088 struct input *input = data;
3089 struct touch_point *tp, *tmp;
3090
3091 DBG("touch_handle_cancel\n");
3092
3093 if (!input->touch_focus) {
3094 DBG("No touch focus found for touch cancel event!\n");
3095 return;
3096 }
3097
3098 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3099 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003100 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3101 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003102
3103 wl_list_remove(&tp->link);
3104 free(tp);
3105 }
3106}
3107
3108static const struct wl_touch_listener touch_listener = {
3109 touch_handle_down,
3110 touch_handle_up,
3111 touch_handle_motion,
3112 touch_handle_frame,
3113 touch_handle_cancel,
3114};
3115
3116static void
Daniel Stone37816df2012-05-16 18:45:18 +01003117seat_handle_capabilities(void *data, struct wl_seat *seat,
3118 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003119{
Daniel Stone37816df2012-05-16 18:45:18 +01003120 struct input *input = data;
3121
3122 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3123 input->pointer = wl_seat_get_pointer(seat);
3124 wl_pointer_set_user_data(input->pointer, input);
3125 wl_pointer_add_listener(input->pointer, &pointer_listener,
3126 input);
3127 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3128 wl_pointer_destroy(input->pointer);
3129 input->pointer = NULL;
3130 }
3131
3132 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3133 input->keyboard = wl_seat_get_keyboard(seat);
3134 wl_keyboard_set_user_data(input->keyboard, input);
3135 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3136 input);
3137 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3138 wl_keyboard_destroy(input->keyboard);
3139 input->keyboard = NULL;
3140 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003141
3142 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3143 input->touch = wl_seat_get_touch(seat);
3144 wl_touch_set_user_data(input->touch, input);
3145 wl_touch_add_listener(input->touch, &touch_listener, input);
3146 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3147 wl_touch_destroy(input->touch);
3148 input->touch = NULL;
3149 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003150}
3151
Rob Bradford08031182013-08-13 20:11:03 +01003152static void
3153seat_handle_name(void *data, struct wl_seat *seat,
3154 const char *name)
3155{
3156
3157}
3158
Daniel Stone37816df2012-05-16 18:45:18 +01003159static const struct wl_seat_listener seat_listener = {
3160 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003161 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003162};
3163
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003164void
3165input_get_position(struct input *input, int32_t *x, int32_t *y)
3166{
3167 *x = input->sx;
3168 *y = input->sy;
3169}
3170
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003171int
3172input_get_touch(struct input *input, int32_t id, float *x, float *y)
3173{
3174 struct touch_point *tp;
3175
3176 wl_list_for_each(tp, &input->touch_point_list, link) {
3177 if (tp->id != id)
3178 continue;
3179
3180 *x = tp->x;
3181 *y = tp->y;
3182 return 0;
3183 }
3184
3185 return -1;
3186}
3187
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003188struct display *
3189input_get_display(struct input *input)
3190{
3191 return input->display;
3192}
3193
Daniel Stone37816df2012-05-16 18:45:18 +01003194struct wl_seat *
3195input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003196{
Daniel Stone37816df2012-05-16 18:45:18 +01003197 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003198}
3199
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003200uint32_t
3201input_get_modifiers(struct input *input)
3202{
3203 return input->modifiers;
3204}
3205
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003206struct widget *
3207input_get_focus_widget(struct input *input)
3208{
3209 return input->focus_widget;
3210}
3211
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003212struct data_offer {
3213 struct wl_data_offer *offer;
3214 struct input *input;
3215 struct wl_array types;
3216 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003217
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003218 struct task io_task;
3219 int fd;
3220 data_func_t func;
3221 int32_t x, y;
3222 void *user_data;
3223};
3224
3225static void
3226data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3227{
3228 struct data_offer *offer = data;
3229 char **p;
3230
3231 p = wl_array_add(&offer->types, sizeof *p);
3232 *p = strdup(type);
3233}
3234
3235static const struct wl_data_offer_listener data_offer_listener = {
3236 data_offer_offer,
3237};
3238
3239static void
3240data_offer_destroy(struct data_offer *offer)
3241{
3242 char **p;
3243
3244 offer->refcount--;
3245 if (offer->refcount == 0) {
3246 wl_data_offer_destroy(offer->offer);
3247 for (p = offer->types.data; *p; p++)
3248 free(*p);
3249 wl_array_release(&offer->types);
3250 free(offer);
3251 }
3252}
3253
3254static void
3255data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003256 struct wl_data_device *data_device,
3257 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003258{
3259 struct data_offer *offer;
3260
Brian Lovinbc919262013-08-07 15:34:59 -07003261 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003262
3263 wl_array_init(&offer->types);
3264 offer->refcount = 1;
3265 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003266 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003267 wl_data_offer_add_listener(offer->offer,
3268 &data_offer_listener, offer);
3269}
3270
3271static void
3272data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003273 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003274 wl_fixed_t x_w, wl_fixed_t y_w,
3275 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003276{
3277 struct input *input = data;
3278 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003279 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003280 float x = wl_fixed_to_double(x_w);
3281 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003282 char **p;
3283
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003284 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003285 input->drag_enter_serial = serial;
3286 input->drag_focus = window,
3287 input->drag_x = x;
3288 input->drag_y = y;
3289
3290 if (!input->touch_grab)
3291 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003292
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003293 if (offer) {
3294 input->drag_offer = wl_data_offer_get_user_data(offer);
3295
3296 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3297 *p = NULL;
3298
3299 types_data = input->drag_offer->types.data;
3300 } else {
3301 input->drag_offer = NULL;
3302 types_data = NULL;
3303 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003304
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003305 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003306 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003307 window->user_data);
3308}
3309
3310static void
3311data_device_leave(void *data, struct wl_data_device *data_device)
3312{
3313 struct input *input = data;
3314
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003315 if (input->drag_offer) {
3316 data_offer_destroy(input->drag_offer);
3317 input->drag_offer = NULL;
3318 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003319}
3320
3321static void
3322data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003323 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003324{
3325 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003326 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003327 float x = wl_fixed_to_double(x_w);
3328 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003329 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003330
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003331 input->drag_x = x;
3332 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003333
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003334 if (input->drag_offer)
3335 types_data = input->drag_offer->types.data;
3336 else
3337 types_data = NULL;
3338
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003339 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003340 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003341 window->user_data);
3342}
3343
3344static void
3345data_device_drop(void *data, struct wl_data_device *data_device)
3346{
3347 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003348 struct window *window = input->drag_focus;
3349 float x, y;
3350
3351 x = input->drag_x;
3352 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003353
3354 if (window->drop_handler)
3355 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003356 x, y, window->user_data);
3357
3358 if (input->touch_grab)
3359 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003360}
3361
3362static void
3363data_device_selection(void *data,
3364 struct wl_data_device *wl_data_device,
3365 struct wl_data_offer *offer)
3366{
3367 struct input *input = data;
3368 char **p;
3369
3370 if (input->selection_offer)
3371 data_offer_destroy(input->selection_offer);
3372
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003373 if (offer) {
3374 input->selection_offer = wl_data_offer_get_user_data(offer);
3375 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3376 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003377 } else {
3378 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003379 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003380}
3381
3382static const struct wl_data_device_listener data_device_listener = {
3383 data_device_data_offer,
3384 data_device_enter,
3385 data_device_leave,
3386 data_device_motion,
3387 data_device_drop,
3388 data_device_selection
3389};
3390
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003391static void
3392input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003393{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003394 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003395 struct wl_cursor *cursor;
3396 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003397
Daniel Stone80972742012-11-07 17:51:39 +11003398 if (!input->pointer)
3399 return;
3400
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003401 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003402 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003403 return;
3404
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003405 if (index >= (int) cursor->image_count) {
3406 fprintf(stderr, "cursor index out of range\n");
3407 return;
3408 }
3409
3410 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003411 buffer = wl_cursor_image_get_buffer(image);
3412 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003413 return;
3414
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003415 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3416 wl_surface_damage(input->pointer_surface, 0, 0,
3417 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003418 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003419 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3420 input->pointer_surface,
3421 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003422}
3423
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003424static const struct wl_callback_listener pointer_surface_listener;
3425
3426static void
3427pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3428 uint32_t time)
3429{
3430 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003431 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003432 int i;
3433
3434 if (callback) {
3435 assert(callback == input->cursor_frame_cb);
3436 wl_callback_destroy(callback);
3437 input->cursor_frame_cb = NULL;
3438 }
3439
Daniel Stone80972742012-11-07 17:51:39 +11003440 if (!input->pointer)
3441 return;
3442
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003443 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003444 wl_pointer_set_cursor(input->pointer,
3445 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003446 NULL, 0, 0);
3447 return;
3448 }
3449
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003450 if (input->current_cursor == CURSOR_UNSET)
3451 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003452 cursor = input->display->cursors[input->current_cursor];
3453 if (!cursor)
3454 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003455
3456 /* FIXME We don't have the current time on the first call so we set
3457 * the animation start to the time of the first frame callback. */
3458 if (time == 0)
3459 input->cursor_anim_start = 0;
3460 else if (input->cursor_anim_start == 0)
3461 input->cursor_anim_start = time;
3462
3463 if (time == 0 || input->cursor_anim_start == 0)
3464 i = 0;
3465 else
3466 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3467
Pekka Paalanenbc106382012-10-10 12:49:31 +03003468 if (cursor->image_count > 1) {
3469 input->cursor_frame_cb =
3470 wl_surface_frame(input->pointer_surface);
3471 wl_callback_add_listener(input->cursor_frame_cb,
3472 &pointer_surface_listener, input);
3473 }
3474
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003475 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003476}
3477
3478static const struct wl_callback_listener pointer_surface_listener = {
3479 pointer_surface_frame_callback
3480};
3481
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003482void
3483input_set_pointer_image(struct input *input, int pointer)
3484{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003485 int force = 0;
3486
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003487 if (!input->pointer)
3488 return;
3489
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003490 if (input->pointer_enter_serial > input->cursor_serial)
3491 force = 1;
3492
3493 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003494 return;
3495
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003496 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003497 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003498 if (!input->cursor_frame_cb)
3499 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003500 else if (force) {
3501 /* The current frame callback may be stuck if, for instance,
3502 * the set cursor request was processed by the server after
3503 * this client lost the focus. In this case the cursor surface
3504 * might not be mapped and the frame callback wouldn't ever
3505 * complete. Send a set_cursor and attach to try to map the
3506 * cursor surface again so that the callback will finish */
3507 input_set_pointer_image_index(input, 0);
3508 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003509}
3510
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003511struct wl_data_device *
3512input_get_data_device(struct input *input)
3513{
3514 return input->data_device;
3515}
3516
3517void
3518input_set_selection(struct input *input,
3519 struct wl_data_source *source, uint32_t time)
3520{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003521 if (input->data_device)
3522 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003523}
3524
3525void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003526input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003527{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003528 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003529 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003530}
3531
3532static void
3533offer_io_func(struct task *task, uint32_t events)
3534{
3535 struct data_offer *offer =
3536 container_of(task, struct data_offer, io_task);
3537 unsigned int len;
3538 char buffer[4096];
3539
3540 len = read(offer->fd, buffer, sizeof buffer);
3541 offer->func(buffer, len,
3542 offer->x, offer->y, offer->user_data);
3543
3544 if (len == 0) {
3545 close(offer->fd);
3546 data_offer_destroy(offer);
3547 }
3548}
3549
3550static void
3551data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3552 data_func_t func, void *user_data)
3553{
3554 int p[2];
3555
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003556 if (pipe2(p, O_CLOEXEC) == -1)
3557 return;
3558
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003559 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3560 close(p[1]);
3561
3562 offer->io_task.run = offer_io_func;
3563 offer->fd = p[0];
3564 offer->func = func;
3565 offer->refcount++;
3566 offer->user_data = user_data;
3567
3568 display_watch_fd(offer->input->display,
3569 offer->fd, EPOLLIN, &offer->io_task);
3570}
3571
3572void
3573input_receive_drag_data(struct input *input, const char *mime_type,
3574 data_func_t func, void *data)
3575{
3576 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003577 input->drag_offer->x = input->drag_x;
3578 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003579}
3580
3581int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003582input_receive_drag_data_to_fd(struct input *input,
3583 const char *mime_type, int fd)
3584{
3585 if (input->drag_offer)
3586 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3587
3588 return 0;
3589}
3590
3591int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003592input_receive_selection_data(struct input *input, const char *mime_type,
3593 data_func_t func, void *data)
3594{
3595 char **p;
3596
3597 if (input->selection_offer == NULL)
3598 return -1;
3599
3600 for (p = input->selection_offer->types.data; *p; p++)
3601 if (strcmp(mime_type, *p) == 0)
3602 break;
3603
3604 if (*p == NULL)
3605 return -1;
3606
3607 data_offer_receive_data(input->selection_offer,
3608 mime_type, func, data);
3609 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003610}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003611
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003612int
3613input_receive_selection_data_to_fd(struct input *input,
3614 const char *mime_type, int fd)
3615{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003616 if (input->selection_offer)
3617 wl_data_offer_receive(input->selection_offer->offer,
3618 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003619
3620 return 0;
3621}
3622
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003623void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003624window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003625{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003626 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003627 return;
3628
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003629 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003630}
3631
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003632static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003633surface_set_synchronized(struct surface *surface)
3634{
3635 if (!surface->subsurface)
3636 return;
3637
3638 if (surface->synchronized)
3639 return;
3640
3641 wl_subsurface_set_sync(surface->subsurface);
3642 surface->synchronized = 1;
3643}
3644
3645static void
3646surface_set_synchronized_default(struct surface *surface)
3647{
3648 if (!surface->subsurface)
3649 return;
3650
3651 if (surface->synchronized == surface->synchronized_default)
3652 return;
3653
3654 if (surface->synchronized_default)
3655 wl_subsurface_set_sync(surface->subsurface);
3656 else
3657 wl_subsurface_set_desync(surface->subsurface);
3658
3659 surface->synchronized = surface->synchronized_default;
3660}
3661
3662static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003663surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003664{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003665 struct widget *widget = surface->widget;
3666 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003667
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003668 if (surface->input_region) {
3669 wl_region_destroy(surface->input_region);
3670 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003671 }
3672
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003673 if (surface->opaque_region)
3674 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003675
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003676 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003677
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003678 if (widget->resize_handler)
3679 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003680 widget->allocation.width,
3681 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003682 widget->user_data);
3683
Pekka Paalanen35e82632013-04-25 13:57:48 +03003684 if (surface->subsurface &&
3685 (surface->allocation.x != widget->allocation.x ||
3686 surface->allocation.y != widget->allocation.y)) {
3687 wl_subsurface_set_position(surface->subsurface,
3688 widget->allocation.x,
3689 widget->allocation.y);
3690 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003691 if (surface->allocation.width != widget->allocation.width ||
3692 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003693 window_schedule_redraw(widget->window);
3694 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003695 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003696
3697 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003698 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003699 widget->allocation.width,
3700 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003701}
3702
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003703static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003704hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3705{
3706 /*
3707 * This hack should be removed, when EGL respects
3708 * eglSwapInterval(0).
3709 *
3710 * If this window has sub-surfaces, especially a free-running
3711 * EGL-widget, we need to post the parent surface once with
3712 * all the old state to guarantee, that the EGL-widget will
3713 * receive its frame callback soon. Otherwise, a forced call
3714 * to eglSwapBuffers may end up blocking, waiting for a frame
3715 * event that will never come, because we will commit the parent
3716 * surface with all new state only after eglSwapBuffers returns.
3717 *
3718 * This assumes, that:
3719 * 1. When the EGL widget's resize hook is called, it pauses.
3720 * 2. When the EGL widget's redraw hook is called, it forces a
3721 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3722 * In a single threaded application condition 1 is a no-op.
3723 *
3724 * XXX: This should actually be after the surface_resize() calls,
3725 * but cannot, because then it would commit the incomplete state
3726 * accumulated from the widget resize hooks.
3727 */
3728 if (window->subsurface_list.next != &window->main_surface->link ||
3729 window->subsurface_list.prev != &window->main_surface->link)
3730 wl_surface_commit(window->main_surface->surface);
3731}
3732
3733static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003734window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003735{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003736 struct surface *surface;
3737
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003738 widget_set_allocation(window->main_surface->widget,
3739 window->pending_allocation.x,
3740 window->pending_allocation.y,
3741 window->pending_allocation.width,
3742 window->pending_allocation.height);
3743
3744 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003745
3746 /* The main surface is in the list, too. Main surface's
3747 * resize_handler is responsible for calling widget_set_allocation()
3748 * on all sub-surface root widgets, so they will be resized
3749 * properly.
3750 */
3751 wl_list_for_each(surface, &window->subsurface_list, link) {
3752 if (surface == window->main_surface)
3753 continue;
3754
3755 surface_set_synchronized(surface);
3756 surface_resize(surface);
3757 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003758
3759 if (!window->fullscreen && !window->maximized)
3760 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003761}
3762
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003763static void
3764idle_resize(struct window *window)
3765{
3766 window->resize_needed = 0;
3767 window->redraw_needed = 1;
3768
3769 DBG("from %dx%d to %dx%d\n",
3770 window->main_surface->server_allocation.width,
3771 window->main_surface->server_allocation.height,
3772 window->pending_allocation.width,
3773 window->pending_allocation.height);
3774
3775 hack_prevent_EGL_sub_surface_deadlock(window);
3776
3777 window_do_resize(window);
3778}
3779
3780static void
3781undo_resize(struct window *window)
3782{
3783 window->pending_allocation.width =
3784 window->main_surface->server_allocation.width;
3785 window->pending_allocation.height =
3786 window->main_surface->server_allocation.height;
3787
3788 DBG("back to %dx%d\n",
3789 window->main_surface->server_allocation.width,
3790 window->main_surface->server_allocation.height);
3791
3792 window_do_resize(window);
3793
3794 if (window->pending_allocation.width == 0 &&
3795 window->pending_allocation.height == 0) {
3796 fprintf(stderr, "Error: Could not draw a surface, "
3797 "most likely due to insufficient disk space in "
3798 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3799 exit(EXIT_FAILURE);
3800 }
3801}
3802
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003803void
3804window_schedule_resize(struct window *window, int width, int height)
3805{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003806 /* We should probably get these numbers from the theme. */
3807 const int min_width = 200, min_height = 200;
3808
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003809 window->pending_allocation.x = 0;
3810 window->pending_allocation.y = 0;
3811 window->pending_allocation.width = width;
3812 window->pending_allocation.height = height;
3813
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003814 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003815 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003816 window->min_allocation.width = min_width;
3817 else
3818 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003819 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003820 window->min_allocation.height = min_height;
3821 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003822 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003823 }
3824
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003825 if (window->pending_allocation.width < window->min_allocation.width)
3826 window->pending_allocation.width = window->min_allocation.width;
3827 if (window->pending_allocation.height < window->min_allocation.height)
3828 window->pending_allocation.height = window->min_allocation.height;
3829
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003830 window->resize_needed = 1;
3831 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003832}
3833
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003834void
3835widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3836{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003837 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003838}
3839
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003840static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003841handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003842 int32_t width, int32_t height,
3843 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003844{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003845 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003846 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003847
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003848 if (width > 0 && height > 0) {
3849 window_schedule_resize(window, width, height);
3850 } else {
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003851 window_schedule_resize(window,
3852 window->saved_allocation.width,
3853 window->saved_allocation.height);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003854 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003855
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003856 window->maximized = 0;
3857 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003858 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003859 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003860
3861 wl_array_for_each(p, states) {
3862 uint32_t state = *p;
3863 switch (state) {
3864 case XDG_SURFACE_STATE_MAXIMIZED:
3865 window->maximized = 1;
3866 break;
3867 case XDG_SURFACE_STATE_FULLSCREEN:
3868 window->fullscreen = 1;
3869 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003870 case XDG_SURFACE_STATE_RESIZING:
3871 window->resizing = 1;
3872 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003873 case XDG_SURFACE_STATE_ACTIVATED:
3874 window->focused = 1;
3875 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003876 default:
3877 /* Unknown state */
3878 break;
3879 }
3880 }
3881
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003882 if (window->frame) {
3883 if (window->maximized) {
3884 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3885 } else {
3886 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3887 }
3888
3889 if (window->focused) {
3890 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3891 } else {
3892 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3893 }
3894 }
3895
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003896 window->next_attach_serial = serial;
Jasper St. Pierrede680992014-04-10 17:23:49 -07003897
3898 if (window->state_changed_handler)
3899 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003900}
3901
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003902static void
3903handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3904{
3905 struct window *window = data;
3906 window_close(window);
3907}
3908
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003909static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003910 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003911 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003912};
3913
3914static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003915window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05003916{
3917 struct wl_surface *parent_surface;
3918
3919 if (!window->xdg_surface)
3920 return;
3921
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003922 if (window->parent)
3923 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05003924 else
3925 parent_surface = NULL;
3926
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003927 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre53686042013-12-09 15:26:25 -05003928}
3929
3930static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003931window_sync_margin(struct window *window)
3932{
3933 int margin;
3934
3935 if (!window->xdg_surface)
3936 return;
3937
3938 if (!window->frame)
3939 return;
3940
3941 margin = frame_get_shadow_margin(window->frame->frame);
3942
3943 /* Shadow size is the same on every side. */
3944 xdg_surface_set_margin(window->xdg_surface,
3945 margin,
3946 margin,
3947 margin,
3948 margin);
3949}
3950
3951static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003952window_flush(struct window *window)
3953{
3954 struct surface *surface;
3955
3956 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003957 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003958 window_sync_parent(window);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003959 window_sync_margin(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003960 }
3961 }
3962
3963 wl_list_for_each(surface, &window->subsurface_list, link) {
3964 if (surface == window->main_surface)
3965 continue;
3966
3967 surface_flush(surface);
3968 }
3969
3970 surface_flush(window->main_surface);
3971}
3972
3973static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003974menu_destroy(struct menu *menu)
3975{
3976 widget_destroy(menu->widget);
3977 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003978 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003979 free(menu);
3980}
3981
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003982void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003983window_get_allocation(struct window *window,
3984 struct rectangle *allocation)
3985{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003986 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003987}
3988
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003989static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003990widget_redraw(struct widget *widget)
3991{
3992 struct widget *child;
3993
3994 if (widget->redraw_handler)
3995 widget->redraw_handler(widget, widget->user_data);
3996 wl_list_for_each(child, &widget->child_list, link)
3997 widget_redraw(child);
3998}
3999
4000static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004001frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4002{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004003 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004004
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004005 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004006 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004007 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004008 surface->frame_cb = NULL;
4009
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004010 surface->last_time = time;
4011
Pekka Paalanen71233882013-04-25 13:57:53 +03004012 if (surface->redraw_needed || surface->window->redraw_needed) {
4013 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004014 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004015 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004016}
4017
4018static const struct wl_callback_listener listener = {
4019 frame_callback
4020};
4021
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004022static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004023surface_redraw(struct surface *surface)
4024{
Pekka Paalanen71233882013-04-25 13:57:53 +03004025 DBG_OBJ(surface->surface, "begin\n");
4026
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004027 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004028 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004029
4030 /* Whole-window redraw forces a redraw even if the previous has
4031 * not yet hit the screen.
4032 */
4033 if (surface->frame_cb) {
4034 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004035 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004036
Pekka Paalanen71233882013-04-25 13:57:53 +03004037 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004038 wl_callback_destroy(surface->frame_cb);
4039 }
4040
Neil Roberts97b747c2013-12-19 16:17:12 +00004041 if (surface->widget->use_cairo &&
4042 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004043 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4044 return -1;
4045 }
4046
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004047 surface->frame_cb = wl_surface_frame(surface->surface);
4048 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004049 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004050
4051 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004052 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004053 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004054 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004055 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004056}
4057
4058static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004059idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004060{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004061 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004062 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004063 int failed = 0;
4064 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004065
Pekka Paalanen71233882013-04-25 13:57:53 +03004066 DBG(" --------- \n");
4067
Pekka Paalaneneebff542013-04-25 13:57:52 +03004068 wl_list_init(&window->redraw_task.link);
4069 window->redraw_task_scheduled = 0;
4070
4071 if (window->resize_needed) {
4072 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004073 if (window->main_surface->frame_cb) {
4074 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004075 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004076 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004077
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004078 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004079 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004080 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004081
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004082 if (surface_redraw(window->main_surface) < 0) {
4083 /*
4084 * Only main_surface failure will cause us to undo the resize.
4085 * If sub-surfaces fail, they will just be broken with old
4086 * content.
4087 */
4088 failed = 1;
4089 } else {
4090 wl_list_for_each(surface, &window->subsurface_list, link) {
4091 if (surface == window->main_surface)
4092 continue;
4093
4094 surface_redraw(surface);
4095 }
4096 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004097
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004098 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004099 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004100
4101 wl_list_for_each(surface, &window->subsurface_list, link)
4102 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004103
4104 if (resized && failed) {
4105 /* Restore widget tree to correspond to what is on screen. */
4106 undo_resize(window);
4107 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004108}
4109
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004110static void
4111window_schedule_redraw_task(struct window *window)
4112{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004113 if (!window->redraw_task_scheduled) {
4114 window->redraw_task.run = idle_redraw;
4115 display_defer(window->display, &window->redraw_task);
4116 window->redraw_task_scheduled = 1;
4117 }
4118}
4119
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004120void
4121window_schedule_redraw(struct window *window)
4122{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004123 struct surface *surface;
4124
Pekka Paalanen71233882013-04-25 13:57:53 +03004125 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4126
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004127 wl_list_for_each(surface, &window->subsurface_list, link)
4128 surface->redraw_needed = 1;
4129
4130 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004131}
4132
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004133int
4134window_is_fullscreen(struct window *window)
4135{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004136 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004137}
4138
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004139void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004140window_set_fullscreen(struct window *window, int fullscreen)
4141{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004142 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004143 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004144
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004145 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004146 return;
4147
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004148 if (fullscreen)
4149 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4150 else
4151 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004152}
4153
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004154int
4155window_is_maximized(struct window *window)
4156{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004157 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004158}
4159
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004160void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004161window_set_maximized(struct window *window, int maximized)
4162{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004163 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004164 return;
4165
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004166 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004167 return;
4168
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004169 if (maximized)
4170 xdg_surface_set_maximized(window->xdg_surface);
4171 else
4172 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004173}
4174
Jasper St. Pierrede680992014-04-10 17:23:49 -07004175int
4176window_is_resizing(struct window *window)
4177{
4178 return window->resizing;
4179}
4180
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004181void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004182window_set_minimized(struct window *window)
4183{
4184 if (!window->xdg_surface)
4185 return;
4186
4187 xdg_surface_set_minimized(window->xdg_surface);
4188}
4189
4190void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004191window_set_user_data(struct window *window, void *data)
4192{
4193 window->user_data = data;
4194}
4195
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004196void *
4197window_get_user_data(struct window *window)
4198{
4199 return window->user_data;
4200}
4201
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004202void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004203window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004204 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004205{
4206 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004207}
4208
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004209void
4210window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004211 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004212{
4213 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004214}
4215
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004216void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004217window_set_data_handler(struct window *window, window_data_handler_t handler)
4218{
4219 window->data_handler = handler;
4220}
4221
4222void
4223window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4224{
4225 window->drop_handler = handler;
4226}
4227
4228void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004229window_set_close_handler(struct window *window,
4230 window_close_handler_t handler)
4231{
4232 window->close_handler = handler;
4233}
4234
4235void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004236window_set_fullscreen_handler(struct window *window,
4237 window_fullscreen_handler_t handler)
4238{
4239 window->fullscreen_handler = handler;
4240}
4241
4242void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004243window_set_output_handler(struct window *window,
4244 window_output_handler_t handler)
4245{
4246 window->output_handler = handler;
4247}
4248
4249void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004250window_set_state_changed_handler(struct window *window,
4251 window_state_changed_handler_t handler)
4252{
4253 window->state_changed_handler = handler;
4254}
4255
4256void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004257window_set_title(struct window *window, const char *title)
4258{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004259 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004260 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004261 if (window->frame) {
4262 frame_set_title(window->frame->frame, title);
4263 widget_schedule_redraw(window->frame->widget);
4264 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004265 if (window->xdg_surface)
4266 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004267}
4268
4269const char *
4270window_get_title(struct window *window)
4271{
4272 return window->title;
4273}
4274
4275void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004276window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4277{
4278 struct text_cursor_position *text_cursor_position =
4279 window->display->text_cursor_position;
4280
Scott Moreau9295ce02012-06-01 12:46:10 -06004281 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004282 return;
4283
4284 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004285 window->main_surface->surface,
4286 wl_fixed_from_int(x),
4287 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004288}
4289
4290void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004291window_damage(struct window *window, int32_t x, int32_t y,
4292 int32_t width, int32_t height)
4293{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004294 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004295}
4296
Casey Dahlin9074db52012-04-19 22:50:09 -04004297static void
4298surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004299 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004300{
Rob Bradford7507b572012-05-15 17:55:34 +01004301 struct window *window = data;
4302 struct output *output;
4303 struct output *output_found = NULL;
4304 struct window_output *window_output;
4305
4306 wl_list_for_each(output, &window->display->output_list, link) {
4307 if (output->output == wl_output) {
4308 output_found = output;
4309 break;
4310 }
4311 }
4312
4313 if (!output_found)
4314 return;
4315
Brian Lovinbc919262013-08-07 15:34:59 -07004316 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004317 window_output->output = output_found;
4318
4319 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004320
4321 if (window->output_handler)
4322 window->output_handler(window, output_found, 1,
4323 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004324}
4325
4326static void
4327surface_leave(void *data,
4328 struct wl_surface *wl_surface, struct wl_output *output)
4329{
Rob Bradford7507b572012-05-15 17:55:34 +01004330 struct window *window = data;
4331 struct window_output *window_output;
4332 struct window_output *window_output_found = NULL;
4333
4334 wl_list_for_each(window_output, &window->window_output_list, link) {
4335 if (window_output->output->output == output) {
4336 window_output_found = window_output;
4337 break;
4338 }
4339 }
4340
4341 if (window_output_found) {
4342 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004343
4344 if (window->output_handler)
4345 window->output_handler(window, window_output->output,
4346 0, window->user_data);
4347
Rob Bradford7507b572012-05-15 17:55:34 +01004348 free(window_output_found);
4349 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004350}
4351
4352static const struct wl_surface_listener surface_listener = {
4353 surface_enter,
4354 surface_leave
4355};
4356
Pekka Paalanen4e373742013-02-13 16:17:13 +02004357static struct surface *
4358surface_create(struct window *window)
4359{
4360 struct display *display = window->display;
4361 struct surface *surface;
4362
Brian Lovinbc919262013-08-07 15:34:59 -07004363 surface = xmalloc(sizeof *surface);
4364 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004365 if (!surface)
4366 return NULL;
4367
4368 surface->window = window;
4369 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004370 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004371 wl_surface_add_listener(surface->surface, &surface_listener, window);
4372
Pekka Paalanen35e82632013-04-25 13:57:48 +03004373 wl_list_insert(&window->subsurface_list, &surface->link);
4374
Pekka Paalanen4e373742013-02-13 16:17:13 +02004375 return surface;
4376}
4377
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004378static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004379get_preferred_buffer_type(struct display *display)
4380{
4381#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004382 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004383 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4384#endif
4385
4386 return WINDOW_BUFFER_TYPE_SHM;
4387}
4388
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004389static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004390window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004391{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004392 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004393 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004394
Peter Huttererf3d62272013-08-08 11:57:05 +10004395 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004396 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004397 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004398
4399 surface = surface_create(window);
4400 window->main_surface = surface;
4401
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004402 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004403
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004404 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004405 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004406
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004407 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004408
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004409 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004410 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004411 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004412
Rob Bradford7507b572012-05-15 17:55:34 +01004413 wl_list_init (&window->window_output_list);
4414
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004415 return window;
4416}
4417
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004418struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004419window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004420{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004421 struct window *window;
4422
4423 window = window_create_internal(display, 0);
4424
4425 window->xdg_surface =
4426 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4427 window->main_surface->surface);
4428 fail_on_null(window->xdg_surface);
4429
4430 xdg_surface_set_user_data(window->xdg_surface, window);
4431 xdg_surface_add_listener(window->xdg_surface,
4432 &xdg_surface_listener, window);
4433
4434 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004435}
4436
4437struct window *
4438window_create_custom(struct display *display)
4439{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004440 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004441}
4442
Jasper St. Pierre53686042013-12-09 15:26:25 -05004443void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004444window_set_parent(struct window *window,
4445 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004446{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004447 window->parent = parent_window;
4448 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004449}
4450
4451struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004452window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004453{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004454 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004455}
4456
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004457static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004458menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004459{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004460 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004461 int next;
4462
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004463 frame_interior(menu->frame, &x, &y, &width, &height);
4464 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004465 if (menu->current != next) {
4466 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004467 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004468 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004469}
4470
4471static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004472menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004473 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004474 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004475{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004476 struct menu *menu = data;
4477
4478 if (widget == menu->widget)
4479 menu_set_item(data, y);
4480
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004481 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004482}
4483
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004484static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004485menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004486 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004487{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004488 struct menu *menu = data;
4489
4490 if (widget == menu->widget)
4491 menu_set_item(data, y);
4492
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004493 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004494}
4495
4496static void
4497menu_leave_handler(struct widget *widget, struct input *input, void *data)
4498{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004499 struct menu *menu = data;
4500
4501 if (widget == menu->widget)
4502 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004503}
4504
4505static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004506menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004507 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004508 uint32_t button, enum wl_pointer_button_state state,
4509 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004510
4511{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004512 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004513
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004514 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4515 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004516 /* Either relase after press-drag-release or
4517 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004518 menu->func(menu->parent, input,
4519 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004520 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004521 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004522 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004523 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004524 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004525}
4526
4527static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004528menu_touch_up_handler(struct widget *widget,
4529 struct input *input,
4530 uint32_t serial,
4531 uint32_t time,
4532 int32_t id,
4533 void *data)
4534{
4535 struct menu *menu = data;
4536
4537 input_ungrab(input);
4538 menu_destroy(menu);
4539}
4540
4541static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004542menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004543{
4544 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004545 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004546 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004548 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004549
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004550 frame_repaint(menu->frame, cr);
4551 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004552
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004553 theme_set_background_source(menu->window->display->theme,
4554 cr, THEME_FRAME_ACTIVE);
4555 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004556 cairo_fill(cr);
4557
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004558 cairo_select_font_face(cr, "sans",
4559 CAIRO_FONT_SLANT_NORMAL,
4560 CAIRO_FONT_WEIGHT_NORMAL);
4561 cairo_set_font_size(cr, 12);
4562
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004563 for (i = 0; i < menu->count; i++) {
4564 if (i == menu->current) {
4565 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004566 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004567 cairo_fill(cr);
4568 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004569 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004570 cairo_show_text(cr, menu->entries[i]);
4571 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004572 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4573 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 }
4576 }
4577
4578 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004579}
4580
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004581static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004582handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4583{
4584 struct window *window = data;
4585 struct menu *menu = window->main_surface->widget->user_data;
4586
4587 input_ungrab(menu->input);
4588 menu_destroy(menu);
4589}
4590
4591static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004592 handle_popup_popup_done,
4593};
4594
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004595void
4596window_show_menu(struct display *display,
4597 struct input *input, uint32_t time, struct window *parent,
4598 int32_t x, int32_t y,
4599 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004600{
4601 struct window *window;
4602 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004603 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004604
4605 menu = malloc(sizeof *menu);
4606 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004607 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004608
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004609 window = window_create_internal(parent->display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004610 if (!window) {
4611 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004612 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004613 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004614
4615 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004616 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004617 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004618 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4619 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004620 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004621 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004622 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004623 menu->entries = entries;
4624 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004625 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004626 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004627 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004628 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004629 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004630 window->x = x;
4631 window->y = y;
4632
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004633 input_ungrab(input);
4634
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004635 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004636 widget_set_enter_handler(menu->widget, menu_enter_handler);
4637 widget_set_leave_handler(menu->widget, menu_leave_handler);
4638 widget_set_motion_handler(menu->widget, menu_motion_handler);
4639 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004640 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004641
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004642 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004643 frame_resize_inside(menu->frame, 200, count * 20);
4644 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4645 window_schedule_resize(window, frame_width(menu->frame),
4646 frame_height(menu->frame));
4647
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004648 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004649
4650 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4651 window->main_surface->surface,
4652 parent->main_surface->surface,
4653 input->seat,
4654 display_get_serial(window->display),
4655 window->x - ix,
4656 window->y - iy,
4657 0);
4658 fail_on_null(window->xdg_popup);
4659
4660 xdg_popup_set_user_data(window->xdg_popup, window);
4661 xdg_popup_add_listener(window->xdg_popup,
4662 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004663}
4664
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004665void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004666window_set_buffer_type(struct window *window, enum window_buffer_type type)
4667{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004668 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004669}
4670
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004671enum window_buffer_type
4672window_get_buffer_type(struct window *window)
4673{
4674 return window->main_surface->buffer_type;
4675}
4676
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004677void
4678window_set_preferred_format(struct window *window,
4679 enum preferred_format format)
4680{
4681 window->preferred_format = format;
4682}
4683
Pekka Paalanen35e82632013-04-25 13:57:48 +03004684struct widget *
4685window_add_subsurface(struct window *window, void *data,
4686 enum subsurface_mode default_mode)
4687{
4688 struct widget *widget;
4689 struct surface *surface;
4690 struct wl_surface *parent;
4691 struct wl_subcompositor *subcompo = window->display->subcompositor;
4692
Pekka Paalanen35e82632013-04-25 13:57:48 +03004693 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004694 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004695 widget = widget_create(window, surface, data);
4696 wl_list_init(&widget->link);
4697 surface->widget = widget;
4698
4699 parent = window->main_surface->surface;
4700 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4701 surface->surface,
4702 parent);
4703 surface->synchronized = 1;
4704
4705 switch (default_mode) {
4706 case SUBSURFACE_SYNCHRONIZED:
4707 surface->synchronized_default = 1;
4708 break;
4709 case SUBSURFACE_DESYNCHRONIZED:
4710 surface->synchronized_default = 0;
4711 break;
4712 default:
4713 assert(!"bad enum subsurface_mode");
4714 }
4715
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004716 window->resize_needed = 1;
4717 window_schedule_redraw(window);
4718
Pekka Paalanen35e82632013-04-25 13:57:48 +03004719 return widget;
4720}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004721
4722static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004723display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004724 struct wl_output *wl_output,
4725 int x, int y,
4726 int physical_width,
4727 int physical_height,
4728 int subpixel,
4729 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004730 const char *model,
4731 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004732{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004733 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004734
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004735 output->allocation.x = x;
4736 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004737 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004738
4739 if (output->make)
4740 free(output->make);
4741 output->make = strdup(make);
4742
4743 if (output->model)
4744 free(output->model);
4745 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004746}
4747
4748static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004749display_handle_done(void *data,
4750 struct wl_output *wl_output)
4751{
4752}
4753
4754static void
4755display_handle_scale(void *data,
4756 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004757 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004758{
4759 struct output *output = data;
4760
4761 output->scale = scale;
4762}
4763
4764static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004765display_handle_mode(void *data,
4766 struct wl_output *wl_output,
4767 uint32_t flags,
4768 int width,
4769 int height,
4770 int refresh)
4771{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004772 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004773 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004774
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004775 if (flags & WL_OUTPUT_MODE_CURRENT) {
4776 output->allocation.width = width;
4777 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004778 if (display->output_configure_handler)
4779 (*display->output_configure_handler)(
4780 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004781 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004782}
4783
4784static const struct wl_output_listener output_listener = {
4785 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004786 display_handle_mode,
4787 display_handle_done,
4788 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004789};
4790
4791static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004792display_add_output(struct display *d, uint32_t id)
4793{
4794 struct output *output;
4795
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004796 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004797 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004798 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004799 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004800 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004801 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004802 wl_list_insert(d->output_list.prev, &output->link);
4803
4804 wl_output_add_listener(output->output, &output_listener, output);
4805}
4806
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004807static void
4808output_destroy(struct output *output)
4809{
4810 if (output->destroy_handler)
4811 (*output->destroy_handler)(output, output->user_data);
4812
4813 wl_output_destroy(output->output);
4814 wl_list_remove(&output->link);
4815 free(output);
4816}
4817
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004818static void
4819display_destroy_output(struct display *d, uint32_t id)
4820{
4821 struct output *output;
4822
4823 wl_list_for_each(output, &d->output_list, link) {
4824 if (output->server_output_id == id) {
4825 output_destroy(output);
4826 break;
4827 }
4828 }
4829}
4830
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004831void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004832display_set_global_handler(struct display *display,
4833 display_global_handler_t handler)
4834{
4835 struct global *global;
4836
4837 display->global_handler = handler;
4838 if (!handler)
4839 return;
4840
4841 wl_list_for_each(global, &display->global_list, link)
4842 display->global_handler(display,
4843 global->name, global->interface,
4844 global->version, display->user_data);
4845}
4846
4847void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004848display_set_global_handler_remove(struct display *display,
4849 display_global_handler_t remove_handler)
4850{
4851 display->global_handler_remove = remove_handler;
4852 if (!remove_handler)
4853 return;
4854}
4855
4856void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004857display_set_output_configure_handler(struct display *display,
4858 display_output_handler_t handler)
4859{
4860 struct output *output;
4861
4862 display->output_configure_handler = handler;
4863 if (!handler)
4864 return;
4865
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004866 wl_list_for_each(output, &display->output_list, link) {
4867 if (output->allocation.width == 0 &&
4868 output->allocation.height == 0)
4869 continue;
4870
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004871 (*display->output_configure_handler)(output,
4872 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004873 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004874}
4875
4876void
4877output_set_user_data(struct output *output, void *data)
4878{
4879 output->user_data = data;
4880}
4881
4882void *
4883output_get_user_data(struct output *output)
4884{
4885 return output->user_data;
4886}
4887
4888void
4889output_set_destroy_handler(struct output *output,
4890 display_output_handler_t handler)
4891{
4892 output->destroy_handler = handler;
4893 /* FIXME: implement this, once we have way to remove outputs */
4894}
4895
4896void
Scott Moreau4e072362012-09-29 02:03:11 -06004897output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004898{
Scott Moreau4e072362012-09-29 02:03:11 -06004899 struct rectangle allocation = output->allocation;
4900
4901 switch (output->transform) {
4902 case WL_OUTPUT_TRANSFORM_90:
4903 case WL_OUTPUT_TRANSFORM_270:
4904 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4905 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4906 /* Swap width and height */
4907 allocation.width = output->allocation.height;
4908 allocation.height = output->allocation.width;
4909 break;
4910 }
4911
4912 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004913}
4914
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004915struct wl_output *
4916output_get_wl_output(struct output *output)
4917{
4918 return output->output;
4919}
4920
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004921enum wl_output_transform
4922output_get_transform(struct output *output)
4923{
4924 return output->transform;
4925}
4926
Alexander Larssonafd319a2013-05-22 14:41:27 +02004927uint32_t
4928output_get_scale(struct output *output)
4929{
4930 return output->scale;
4931}
4932
Jason Ekstrand738715d2014-04-02 19:53:50 -05004933const char *
4934output_get_make(struct output *output)
4935{
4936 return output->make;
4937}
4938
4939const char *
4940output_get_model(struct output *output)
4941{
4942 return output->model;
4943}
4944
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004945static void
Daniel Stone97f68542012-05-30 16:32:01 +01004946fini_xkb(struct input *input)
4947{
4948 xkb_state_unref(input->xkb.state);
4949 xkb_map_unref(input->xkb.keymap);
4950}
4951
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004952#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004953
Daniel Stone97f68542012-05-30 16:32:01 +01004954static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004955display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004956{
4957 struct input *input;
4958
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004959 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004960 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004961 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004962 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004963 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004964 input->pointer_focus = NULL;
4965 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004966 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004967 wl_list_insert(d->input_list.prev, &input->link);
4968
Daniel Stone37816df2012-05-16 18:45:18 +01004969 wl_seat_add_listener(input->seat, &seat_listener, input);
4970 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004971
Jason Ekstranda669bd52014-04-02 19:53:51 -05004972 if (d->data_device_manager) {
4973 input->data_device =
4974 wl_data_device_manager_get_data_device(d->data_device_manager,
4975 input->seat);
4976 wl_data_device_add_listener(input->data_device,
4977 &data_device_listener,
4978 input);
4979 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004980
4981 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004982
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004983 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4984 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004985 input->repeat_task.run = keyboard_repeat_func;
4986 display_watch_fd(d, input->repeat_timer_fd,
4987 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004988}
4989
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004990static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004991input_destroy(struct input *input)
4992{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004993 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004994 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004995
4996 if (input->drag_offer)
4997 data_offer_destroy(input->drag_offer);
4998
4999 if (input->selection_offer)
5000 data_offer_destroy(input->selection_offer);
5001
Jason Ekstranda669bd52014-04-02 19:53:51 -05005002 if (input->data_device)
5003 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005004
5005 if (input->display->seat_version >= 3) {
5006 if (input->pointer)
5007 wl_pointer_release(input->pointer);
5008 if (input->keyboard)
5009 wl_keyboard_release(input->keyboard);
5010 }
5011
Daniel Stone97f68542012-05-30 16:32:01 +01005012 fini_xkb(input);
5013
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005014 wl_surface_destroy(input->pointer_surface);
5015
Pekka Paalanene1207c72011-12-16 12:02:09 +02005016 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005017 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005018 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005019 free(input);
5020}
5021
5022static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005023init_workspace_manager(struct display *d, uint32_t id)
5024{
5025 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005026 wl_registry_bind(d->registry, id,
5027 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005028 if (d->workspace_manager != NULL)
5029 workspace_manager_add_listener(d->workspace_manager,
5030 &workspace_manager_listener,
5031 d);
5032}
5033
5034static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005035shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5036{
5037 struct display *d = data;
5038
5039 if (format == WL_SHM_FORMAT_RGB565)
5040 d->has_rgb565 = 1;
5041}
5042
5043struct wl_shm_listener shm_listener = {
5044 shm_format
5045};
5046
5047static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005048xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5049{
5050 xdg_shell_pong(shell, serial);
5051}
5052
5053static const struct xdg_shell_listener xdg_shell_listener = {
5054 xdg_shell_ping,
5055};
5056
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08005057#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005058#ifdef static_assert
5059static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5060 "Interface version doesn't match implementation version");
5061#endif
5062
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005063static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005064registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5065 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005066{
5067 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005068 struct global *global;
5069
Brian Lovinbc919262013-08-07 15:34:59 -07005070 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005071 global->name = id;
5072 global->interface = strdup(interface);
5073 global->version = version;
5074 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005075
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005076 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005077 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005078 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005079 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005080 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005081 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005082 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005083 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005084 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005085 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005086 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005087 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5088 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005089 wl_registry_bind(registry, id,
5090 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005091 } else if (strcmp(interface, "xdg_shell") == 0) {
5092 d->xdg_shell = wl_registry_bind(registry, id,
5093 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005094 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005095 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005096 } else if (strcmp(interface, "text_cursor_position") == 0) {
5097 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005098 wl_registry_bind(registry, id,
5099 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005100 } else if (strcmp(interface, "workspace_manager") == 0) {
5101 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005102 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5103 d->subcompositor =
5104 wl_registry_bind(registry, id,
5105 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005106 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005107
5108 if (d->global_handler)
5109 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005110}
5111
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005112static void
5113registry_handle_global_remove(void *data, struct wl_registry *registry,
5114 uint32_t name)
5115{
5116 struct display *d = data;
5117 struct global *global;
5118 struct global *tmp;
5119
5120 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5121 if (global->name != name)
5122 continue;
5123
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005124 if (strcmp(global->interface, "wl_output") == 0)
5125 display_destroy_output(d, name);
5126
5127 /* XXX: Should destroy remaining bound globals */
5128
5129 if (d->global_handler_remove)
5130 d->global_handler_remove(d, name, global->interface,
5131 global->version, d->user_data);
5132
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005133 wl_list_remove(&global->link);
5134 free(global->interface);
5135 free(global);
5136 }
5137}
5138
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005139void *
5140display_bind(struct display *display, uint32_t name,
5141 const struct wl_interface *interface, uint32_t version)
5142{
5143 return wl_registry_bind(display->registry, name, interface, version);
5144}
5145
5146static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005147 registry_handle_global,
5148 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005149};
5150
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005151#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005152static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005153init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005154{
5155 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005156 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005157
Rob Clark6396ed32012-03-11 19:48:41 -05005158#ifdef USE_CAIRO_GLESV2
5159# define GL_BIT EGL_OPENGL_ES2_BIT
5160#else
5161# define GL_BIT EGL_OPENGL_BIT
5162#endif
5163
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005164 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005165 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005166 EGL_RED_SIZE, 1,
5167 EGL_GREEN_SIZE, 1,
5168 EGL_BLUE_SIZE, 1,
5169 EGL_ALPHA_SIZE, 1,
5170 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005171 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005172 EGL_NONE
5173 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005174
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005175#ifdef USE_CAIRO_GLESV2
5176 static const EGLint context_attribs[] = {
5177 EGL_CONTEXT_CLIENT_VERSION, 2,
5178 EGL_NONE
5179 };
5180 EGLint api = EGL_OPENGL_ES_API;
5181#else
5182 EGLint *context_attribs = NULL;
5183 EGLint api = EGL_OPENGL_API;
5184#endif
5185
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005186 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005187 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005188 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005189 return -1;
5190 }
5191
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005192 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005193 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005194 return -1;
5195 }
5196
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005197 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5198 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005199 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005200 return -1;
5201 }
5202
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005203 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005204 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005205 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005206 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005207 return -1;
5208 }
5209
Benjamin Franzke0c991632011-09-27 21:57:31 +02005210 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5211 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005212 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005213 return -1;
5214 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005215
5216 return 0;
5217}
5218
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005219static void
5220fini_egl(struct display *display)
5221{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005222 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005223
5224 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5225 EGL_NO_CONTEXT);
5226
5227 eglTerminate(display->dpy);
5228 eglReleaseThread();
5229}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005230#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005231
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005232static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005233init_dummy_surface(struct display *display)
5234{
5235 int len;
5236 void *data;
5237
5238 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5239 data = malloc(len);
5240 display->dummy_surface =
5241 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5242 1, 1, len);
5243 display->dummy_surface_data = data;
5244}
5245
5246static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005247handle_display_data(struct task *task, uint32_t events)
5248{
5249 struct display *display =
5250 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005251 struct epoll_event ep;
5252 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005253
5254 display->display_fd_events = events;
5255
5256 if (events & EPOLLERR || events & EPOLLHUP) {
5257 display_exit(display);
5258 return;
5259 }
5260
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005261 if (events & EPOLLIN) {
5262 ret = wl_display_dispatch(display->display);
5263 if (ret == -1) {
5264 display_exit(display);
5265 return;
5266 }
5267 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005268
5269 if (events & EPOLLOUT) {
5270 ret = wl_display_flush(display->display);
5271 if (ret == 0) {
5272 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5273 ep.data.ptr = &display->display_task;
5274 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5275 display->display_fd, &ep);
5276 } else if (ret == -1 && errno != EAGAIN) {
5277 display_exit(display);
5278 return;
5279 }
5280 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005281}
5282
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005283static void
5284log_handler(const char *format, va_list args)
5285{
5286 vfprintf(stderr, format, args);
5287}
5288
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005289struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005290display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005291{
5292 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005293
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005294 wl_log_set_handler_client(log_handler);
5295
Peter Huttererf3d62272013-08-08 11:57:05 +10005296 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005297 if (d == NULL)
5298 return NULL;
5299
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005300 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005301 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005302 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005303 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005304 return NULL;
5305 }
5306
Rob Bradford5ab9c752013-07-26 16:29:43 +01005307 d->xkb_context = xkb_context_new(0);
5308 if (d->xkb_context == NULL) {
5309 fprintf(stderr, "Failed to create XKB context\n");
5310 free(d);
5311 return NULL;
5312 }
5313
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005314 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005315 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005316 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005317 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5318 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005319
5320 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005321 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005322 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005323 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005324
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005325 d->workspace = 0;
5326 d->workspace_count = 1;
5327
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005328 d->registry = wl_display_get_registry(d->display);
5329 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005330
5331 if (wl_display_dispatch(d->display) < 0) {
5332 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5333 return NULL;
5334 }
5335
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005336#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005337 if (init_egl(d) < 0)
5338 fprintf(stderr, "EGL does not seem to work, "
5339 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005340#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005341
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005342 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005343
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005344 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005345
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005346 wl_list_init(&d->window_list);
5347
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005348 init_dummy_surface(d);
5349
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005350 return d;
5351}
5352
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005353static void
5354display_destroy_outputs(struct display *display)
5355{
5356 struct output *tmp;
5357 struct output *output;
5358
5359 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5360 output_destroy(output);
5361}
5362
Pekka Paalanene1207c72011-12-16 12:02:09 +02005363static void
5364display_destroy_inputs(struct display *display)
5365{
5366 struct input *tmp;
5367 struct input *input;
5368
5369 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5370 input_destroy(input);
5371}
5372
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005373void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005374display_destroy(struct display *display)
5375{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005376 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005377 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5378 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005379
5380 if (!wl_list_empty(&display->deferred_list))
5381 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5382
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005383 cairo_surface_destroy(display->dummy_surface);
5384 free(display->dummy_surface_data);
5385
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005386 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005387 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005388
Daniel Stone97f68542012-05-30 16:32:01 +01005389 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005390
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005391 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005392 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005393
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005394#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005395 if (display->argb_device)
5396 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005397#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005398
Pekka Paalanen35e82632013-04-25 13:57:48 +03005399 if (display->subcompositor)
5400 wl_subcompositor_destroy(display->subcompositor);
5401
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005402 if (display->xdg_shell)
5403 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005404
5405 if (display->shm)
5406 wl_shm_destroy(display->shm);
5407
5408 if (display->data_device_manager)
5409 wl_data_device_manager_destroy(display->data_device_manager);
5410
5411 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005412 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005413
5414 close(display->epoll_fd);
5415
U. Artie Eoff44874d92012-10-02 21:12:35 -07005416 if (!(display->display_fd_events & EPOLLERR) &&
5417 !(display->display_fd_events & EPOLLHUP))
5418 wl_display_flush(display->display);
5419
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005420 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005421 free(display);
5422}
5423
5424void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005425display_set_user_data(struct display *display, void *data)
5426{
5427 display->user_data = data;
5428}
5429
5430void *
5431display_get_user_data(struct display *display)
5432{
5433 return display->user_data;
5434}
5435
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005436struct wl_display *
5437display_get_display(struct display *display)
5438{
5439 return display->display;
5440}
5441
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005442int
5443display_has_subcompositor(struct display *display)
5444{
5445 if (display->subcompositor)
5446 return 1;
5447
5448 wl_display_roundtrip(display->display);
5449
5450 return display->subcompositor != NULL;
5451}
5452
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005453cairo_device_t *
5454display_get_cairo_device(struct display *display)
5455{
5456 return display->argb_device;
5457}
5458
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005459struct output *
5460display_get_output(struct display *display)
5461{
5462 return container_of(display->output_list.next, struct output, link);
5463}
5464
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005465struct wl_compositor *
5466display_get_compositor(struct display *display)
5467{
5468 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005469}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005470
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005471uint32_t
5472display_get_serial(struct display *display)
5473{
5474 return display->serial;
5475}
5476
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005477EGLDisplay
5478display_get_egl_display(struct display *d)
5479{
5480 return d->dpy;
5481}
5482
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005483struct wl_data_source *
5484display_create_data_source(struct display *display)
5485{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005486 if (display->data_device_manager)
5487 return wl_data_device_manager_create_data_source(display->data_device_manager);
5488 else
5489 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005490}
5491
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005492EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005493display_get_argb_egl_config(struct display *d)
5494{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005495 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005496}
5497
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005498int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005499display_acquire_window_surface(struct display *display,
5500 struct window *window,
5501 EGLContext ctx)
5502{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005503 struct surface *surface = window->main_surface;
5504
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005505 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005506 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005507
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005508 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005509 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005510}
5511
5512void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005513display_release_window_surface(struct display *display,
5514 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005515{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005516 struct surface *surface = window->main_surface;
5517
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005518 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005519 return;
5520
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005521 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005522}
5523
5524void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005525display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005526{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005527 wl_list_insert(&display->deferred_list, &task->link);
5528}
5529
5530void
5531display_watch_fd(struct display *display,
5532 int fd, uint32_t events, struct task *task)
5533{
5534 struct epoll_event ep;
5535
5536 ep.events = events;
5537 ep.data.ptr = task;
5538 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5539}
5540
5541void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005542display_unwatch_fd(struct display *display, int fd)
5543{
5544 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5545}
5546
5547void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005548display_run(struct display *display)
5549{
5550 struct task *task;
5551 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005552 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005553
Pekka Paalanen826d7952011-12-15 10:14:07 +02005554 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005555 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005556 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005557 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005558 struct task, link);
5559 wl_list_remove(&task->link);
5560 task->run(task, 0);
5561 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005562
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005563 wl_display_dispatch_pending(display->display);
5564
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005565 if (!display->running)
5566 break;
5567
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005568 ret = wl_display_flush(display->display);
5569 if (ret < 0 && errno == EAGAIN) {
5570 ep[0].events =
5571 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5572 ep[0].data.ptr = &display->display_task;
5573
5574 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5575 display->display_fd, &ep[0]);
5576 } else if (ret < 0) {
5577 break;
5578 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005579
5580 count = epoll_wait(display->epoll_fd,
5581 ep, ARRAY_LENGTH(ep), -1);
5582 for (i = 0; i < count; i++) {
5583 task = ep[i].data.ptr;
5584 task->run(task, ep[i].events);
5585 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005586 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005587}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005588
5589void
5590display_exit(struct display *display)
5591{
5592 display->running = 0;
5593}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005594
5595void
5596keysym_modifiers_add(struct wl_array *modifiers_map,
5597 const char *name)
5598{
5599 size_t len = strlen(name) + 1;
5600 char *p;
5601
5602 p = wl_array_add(modifiers_map, len);
5603
5604 if (p == NULL)
5605 return;
5606
5607 strncpy(p, name, len);
5608}
5609
5610static xkb_mod_index_t
5611keysym_modifiers_get_index(struct wl_array *modifiers_map,
5612 const char *name)
5613{
5614 xkb_mod_index_t index = 0;
5615 char *p = modifiers_map->data;
5616
5617 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5618 if (strcmp(p, name) == 0)
5619 return index;
5620
5621 index++;
5622 p += strlen(p) + 1;
5623 }
5624
5625 return XKB_MOD_INVALID;
5626}
5627
5628xkb_mod_mask_t
5629keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5630 const char *name)
5631{
5632 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5633
5634 if (index == XKB_MOD_INVALID)
5635 return XKB_MOD_INVALID;
5636
5637 return 1 << index;
5638}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005639
5640void *
5641fail_on_null(void *p)
5642{
5643 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005644 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005645 exit(EXIT_FAILURE);
5646 }
5647
5648 return p;
5649}
5650
5651void *
5652xmalloc(size_t s)
5653{
5654 return fail_on_null(malloc(s));
5655}
5656
Peter Huttererf3d62272013-08-08 11:57:05 +10005657void *
5658xzalloc(size_t s)
5659{
5660 return fail_on_null(zalloc(s));
5661}
5662
Kristian Høgsbergce278412013-07-25 15:20:20 -07005663char *
5664xstrdup(const char *s)
5665{
5666 return fail_on_null(strdup(s));
5667}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005668
5669void *
5670xrealloc(char *p, size_t s)
5671{
5672 return fail_on_null(realloc(p, s));
5673}