blob: 7d8834514ef6e3662486d04ebed0b1d4f68cd871 [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 {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002132 if (widget->window->maximized) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002133 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2134 } else {
2135 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2136 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002137
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002138 frame_resize(frame->frame, width, height);
2139 frame_interior(frame->frame, &interior.x, &interior.y,
2140 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002141 }
2142
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002143 widget_set_allocation(child, interior.x, interior.y,
2144 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002145
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002146 if (child->resize_handler) {
2147 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002148 child->user_data);
2149
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002150 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002151 width = child->allocation.width;
2152 height = child->allocation.height;
2153 } else {
2154 frame_resize_inside(frame->frame,
2155 child->allocation.width,
2156 child->allocation.height);
2157 width = frame_width(frame->frame);
2158 height = frame_height(frame->frame);
2159 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002160 }
2161
Scott Moreauf7e498c2012-05-14 11:39:29 -06002162 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002163
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002164 widget->surface->input_region =
2165 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002166 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002167 frame_input_rect(frame->frame, &input.x, &input.y,
2168 &input.width, &input.height);
2169 wl_region_add(widget->surface->input_region,
2170 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002171 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002173 }
2174
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002175 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002176
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002177 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002178 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002179 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2180 &opaque.width, &opaque.height);
2181
2182 wl_region_add(widget->surface->opaque_region,
2183 opaque.x, opaque.y,
2184 opaque.width, opaque.height);
2185 } else {
2186 wl_region_add(widget->surface->opaque_region,
2187 0, 0, width, height);
2188 }
Martin Minarik1998b152012-05-10 02:04:35 +02002189 }
2190
Martin Minarik1998b152012-05-10 02:04:35 +02002191
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002192 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002193}
2194
2195static void
2196frame_redraw_handler(struct widget *widget, void *data)
2197{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002198 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002199 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200 struct window *window = widget->window;
2201
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002202 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002203 return;
2204
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002205 if (window->focused) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002206 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2207 } else {
2208 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2209 }
2210
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002211 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002212
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002213 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002214
2215 cairo_destroy(cr);
2216}
2217
2218static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002219frame_get_pointer_image_for_location(struct window_frame *frame,
2220 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002221{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002222 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002223
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002224 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002225 return CURSOR_LEFT_PTR;
2226
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002227 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002228 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002229 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002230 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002231 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002232 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002233 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002234 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002235 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002236 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002237 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002238 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002239 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002240 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002241 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002242 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002243 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002244 case THEME_LOCATION_EXTERIOR:
2245 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002246 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002247 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002248 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002249}
2250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002251static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002252frame_menu_func(struct window *window,
2253 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002254{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002255 struct display *display;
2256
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002257 switch (index) {
2258 case 0: /* close */
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002259 window_close(window);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002260 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002261 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002262 display = window->display;
2263 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002264 workspace_manager_move_surface(
2265 display->workspace_manager,
2266 window->main_surface->surface,
2267 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002268 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002269 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002270 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002271 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002272 workspace_manager_move_surface(
2273 display->workspace_manager,
2274 window->main_surface->surface,
2275 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002276 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002277 case 3: /* fullscreen */
2278 /* we don't have a way to get out of fullscreen for now */
2279 if (window->fullscreen_handler)
2280 window->fullscreen_handler(window, window->user_data);
2281 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002282 }
2283}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002284
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002285void
2286window_show_frame_menu(struct window *window,
2287 struct input *input, uint32_t time)
2288{
2289 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002290 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002291
2292 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002293 "Close",
2294 "Move to workspace above", "Move to workspace below",
2295 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002296 };
2297
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002298 if (window->fullscreen_handler)
2299 count = ARRAY_LENGTH(entries);
2300 else
2301 count = ARRAY_LENGTH(entries) - 1;
2302
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002303 input_get_position(input, &x, &y);
2304 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002305 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002306}
2307
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002308static int
2309frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002310 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002311{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002312 struct window_frame *frame = data;
2313 enum theme_location location;
2314
2315 location = frame_pointer_enter(frame->frame, input, x, y);
2316 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2317 widget_schedule_redraw(frame->widget);
2318
2319 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002320}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002321
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002322static int
2323frame_motion_handler(struct widget *widget,
2324 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002325 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002326{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002327 struct window_frame *frame = data;
2328 enum theme_location location;
2329
2330 location = frame_pointer_motion(frame->frame, input, x, y);
2331 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2332 widget_schedule_redraw(frame->widget);
2333
2334 return frame_get_pointer_image_for_location(data, location);
2335}
2336
2337static void
2338frame_leave_handler(struct widget *widget,
2339 struct input *input, void *data)
2340{
2341 struct window_frame *frame = data;
2342
2343 frame_pointer_leave(frame->frame, input);
2344 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2345 widget_schedule_redraw(frame->widget);
2346}
2347
2348static void
2349frame_handle_status(struct window_frame *frame, struct input *input,
2350 uint32_t time, enum theme_location location)
2351{
2352 struct window *window = frame->widget->window;
2353 uint32_t status;
2354
2355 status = frame_status(frame->frame);
2356 if (status & FRAME_STATUS_REPAINT)
2357 widget_schedule_redraw(frame->widget);
2358
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002359 if (status & FRAME_STATUS_MINIMIZE) {
2360 window_set_minimized(window);
2361 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2362 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002363
2364 if (status & FRAME_STATUS_MENU) {
2365 window_show_frame_menu(window, input, time);
2366 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2367 }
2368
2369 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002370 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002371 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2372 }
2373
2374 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002375 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002376 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002377 }
2378
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002379 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002380 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002381 xdg_surface_move(window->xdg_surface,
2382 input_get_seat(input),
2383 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002384
2385 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2386 }
2387
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002388 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002389 input_ungrab(input);
2390
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002391 xdg_surface_resize(window->xdg_surface,
2392 input_get_seat(input),
2393 window->display->serial,
2394 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002395
2396 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2397 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002398}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002399
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002400static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002401frame_button_handler(struct widget *widget,
2402 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002403 uint32_t button, enum wl_pointer_button_state state,
2404 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002405
2406{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002407 struct window_frame *frame = data;
2408 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002409
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002410 location = frame_pointer_button(frame->frame, input, button, state);
2411 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002412}
2413
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002414static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002415frame_touch_down_handler(struct widget *widget, struct input *input,
2416 uint32_t serial, uint32_t time, int32_t id,
2417 float x, float y, void *data)
2418{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002419 struct window_frame *frame = data;
2420
2421 frame_touch_down(frame->frame, input, id, x, y);
2422 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2423}
2424
2425static void
2426frame_touch_up_handler(struct widget *widget,
2427 struct input *input, uint32_t serial, uint32_t time,
2428 int32_t id, void *data)
2429{
2430 struct window_frame *frame = data;
2431
2432 frame_touch_up(frame->frame, input, id);
2433 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002434}
2435
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002436struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002437window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002438{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002439 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002440 uint32_t buttons;
2441
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002442 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002443 buttons = FRAME_BUTTON_NONE;
2444 } else {
2445 buttons = FRAME_BUTTON_ALL;
2446 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002447
Peter Huttererf3d62272013-08-08 11:57:05 +10002448 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002449 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002450 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002451
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002452 frame->widget = window_add_widget(window, frame);
2453 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002454
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002455 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2456 widget_set_resize_handler(frame->widget, frame_resize_handler);
2457 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002458 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002459 widget_set_motion_handler(frame->widget, frame_motion_handler);
2460 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002461 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002462 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002463
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002464 window->frame = frame;
2465
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002466 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002467}
2468
Kristian Høgsberga1627922012-06-20 17:30:03 -04002469void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002470window_frame_set_child_size(struct widget *widget, int child_width,
2471 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002472{
2473 struct display *display = widget->window->display;
2474 struct theme *t = display->theme;
2475 int decoration_width, decoration_height;
2476 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002477 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002478
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002479 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002480 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002481 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002482 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002483
2484 width = child_width + decoration_width;
2485 height = child_height + decoration_height;
2486 } else {
2487 width = child_width;
2488 height = child_height;
2489 }
2490
2491 window_schedule_resize(widget->window, width, height);
2492}
2493
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002494static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002495window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002496{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002497 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002498
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002499 /* frame->child must be destroyed by the application */
2500 widget_destroy(frame->widget);
2501 free(frame);
2502}
2503
2504static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002505input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002506 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002507{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002508 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002509 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002510
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002511 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002512 return;
2513
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002514 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002515 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002516 widget = old;
2517 if (input->grab)
2518 widget = input->grab;
2519 if (widget->leave_handler)
2520 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002521 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002522 }
2523
2524 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002525 widget = focus;
2526 if (input->grab)
2527 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002528 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002529 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002530 cursor = widget->enter_handler(focus, input, x, y,
2531 widget->user_data);
2532 else
2533 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002534
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002535 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002536 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002537}
2538
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002539void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002540touch_grab(struct input *input, int32_t touch_id)
2541{
2542 input->touch_grab = 1;
2543 input->touch_grab_id = touch_id;
2544}
2545
2546void
2547touch_ungrab(struct input *input)
2548{
2549 struct touch_point *tp, *tmp;
2550
2551 input->touch_grab = 0;
2552
2553 wl_list_for_each_safe(tp, tmp,
2554 &input->touch_point_list, link) {
2555 if (tp->id != input->touch_grab_id)
2556 continue;
2557 wl_list_remove(&tp->link);
2558 free(tp);
2559
2560 return;
2561 }
2562}
2563
2564void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002565input_grab(struct input *input, struct widget *widget, uint32_t button)
2566{
2567 input->grab = widget;
2568 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002569
2570 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002571}
2572
2573void
2574input_ungrab(struct input *input)
2575{
2576 struct widget *widget;
2577
2578 input->grab = NULL;
2579 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002580 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002581 input->sx, input->sy);
2582 input_set_focus_widget(input, widget, input->sx, input->sy);
2583 }
2584}
2585
2586static void
2587input_remove_pointer_focus(struct input *input)
2588{
2589 struct window *window = input->pointer_focus;
2590
2591 if (!window)
2592 return;
2593
2594 input_set_focus_widget(input, NULL, 0, 0);
2595
2596 input->pointer_focus = NULL;
2597 input->current_cursor = CURSOR_UNSET;
2598}
2599
2600static void
2601pointer_handle_enter(void *data, struct wl_pointer *pointer,
2602 uint32_t serial, struct wl_surface *surface,
2603 wl_fixed_t sx_w, wl_fixed_t sy_w)
2604{
2605 struct input *input = data;
2606 struct window *window;
2607 struct widget *widget;
2608 float sx = wl_fixed_to_double(sx_w);
2609 float sy = wl_fixed_to_double(sy_w);
2610
2611 if (!surface) {
2612 /* enter event for a window we've just destroyed */
2613 return;
2614 }
2615
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002616 window = wl_surface_get_user_data(surface);
2617 if (surface != window->main_surface->surface) {
2618 DBG("Ignoring input event from subsurface %p\n", surface);
2619 return;
2620 }
2621
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002622 input->display->serial = serial;
2623 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002624 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002625
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002626 input->sx = sx;
2627 input->sy = sy;
2628
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002629 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002630 input_set_focus_widget(input, widget, sx, sy);
2631}
2632
2633static void
2634pointer_handle_leave(void *data, struct wl_pointer *pointer,
2635 uint32_t serial, struct wl_surface *surface)
2636{
2637 struct input *input = data;
2638
2639 input->display->serial = serial;
2640 input_remove_pointer_focus(input);
2641}
2642
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002643static void
Daniel Stone37816df2012-05-16 18:45:18 +01002644pointer_handle_motion(void *data, struct wl_pointer *pointer,
2645 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002646{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002647 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002648 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002649 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002650 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002651 float sx = wl_fixed_to_double(sx_w);
2652 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002653
Paul Winwoodb22bf572013-08-29 10:52:54 +01002654 if (!window)
2655 return;
2656
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002657 input->sx = sx;
2658 input->sy = sy;
2659
Rob Bradford5f087742013-07-11 19:41:27 +01002660 /* when making the window smaller - e.g. after a unmaximise we might
2661 * still have a pending motion event that the compositor has picked
2662 * based on the old surface dimensions
2663 */
2664 if (sx > window->main_surface->allocation.width ||
2665 sy > window->main_surface->allocation.height)
2666 return;
2667
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002668 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002669 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002670 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002671 }
2672
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002673 if (input->grab)
2674 widget = input->grab;
2675 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002676 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002677 if (widget) {
2678 if (widget->motion_handler)
2679 cursor = widget->motion_handler(input->focus_widget,
2680 input, time, sx, sy,
2681 widget->user_data);
2682 else
2683 cursor = widget->default_cursor;
2684 } else
2685 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002686
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002687 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002688}
2689
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002690static void
Daniel Stone37816df2012-05-16 18:45:18 +01002691pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002692 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002693{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002694 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002695 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002696 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002697
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002698 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002699 if (input->focus_widget && input->grab == NULL &&
2700 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002701 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002702
Neil Roberts6b28aad2012-01-23 19:11:18 +00002703 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002704 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002705 (*widget->button_handler)(widget,
2706 input, time,
2707 button, state,
2708 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002709
Daniel Stone4dbadb12012-05-30 16:31:51 +01002710 if (input->grab && input->grab_button == button &&
2711 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002712 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002713}
2714
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002715static void
Daniel Stone37816df2012-05-16 18:45:18 +01002716pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002717 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002718{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002719 struct input *input = data;
2720 struct widget *widget;
2721
2722 widget = input->focus_widget;
2723 if (input->grab)
2724 widget = input->grab;
2725 if (widget && widget->axis_handler)
2726 (*widget->axis_handler)(widget,
2727 input, time,
2728 axis, value,
2729 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002730}
2731
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002732static const struct wl_pointer_listener pointer_listener = {
2733 pointer_handle_enter,
2734 pointer_handle_leave,
2735 pointer_handle_motion,
2736 pointer_handle_button,
2737 pointer_handle_axis,
2738};
2739
2740static void
2741input_remove_keyboard_focus(struct input *input)
2742{
2743 struct window *window = input->keyboard_focus;
2744 struct itimerspec its;
2745
2746 its.it_interval.tv_sec = 0;
2747 its.it_interval.tv_nsec = 0;
2748 its.it_value.tv_sec = 0;
2749 its.it_value.tv_nsec = 0;
2750 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2751
2752 if (!window)
2753 return;
2754
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002755 if (window->keyboard_focus_handler)
2756 (*window->keyboard_focus_handler)(window, NULL,
2757 window->user_data);
2758
2759 input->keyboard_focus = NULL;
2760}
2761
2762static void
2763keyboard_repeat_func(struct task *task, uint32_t events)
2764{
2765 struct input *input =
2766 container_of(task, struct input, repeat_task);
2767 struct window *window = input->keyboard_focus;
2768 uint64_t exp;
2769
2770 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2771 /* If we change the timer between the fd becoming
2772 * readable and getting here, there'll be nothing to
2773 * read and we get EAGAIN. */
2774 return;
2775
2776 if (window && window->key_handler) {
2777 (*window->key_handler)(window, input, input->repeat_time,
2778 input->repeat_key, input->repeat_sym,
2779 WL_KEYBOARD_KEY_STATE_PRESSED,
2780 window->user_data);
2781 }
2782}
2783
2784static void
2785keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2786 uint32_t format, int fd, uint32_t size)
2787{
2788 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002789 struct xkb_keymap *keymap;
2790 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002791 char *map_str;
2792
2793 if (!data) {
2794 close(fd);
2795 return;
2796 }
2797
2798 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2799 close(fd);
2800 return;
2801 }
2802
2803 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2804 if (map_str == MAP_FAILED) {
2805 close(fd);
2806 return;
2807 }
2808
Rui Matos3eccb862013-10-10 19:44:22 +02002809 keymap = xkb_map_new_from_string(input->display->xkb_context,
2810 map_str,
2811 XKB_KEYMAP_FORMAT_TEXT_V1,
2812 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002813 munmap(map_str, size);
2814 close(fd);
2815
Rui Matos3eccb862013-10-10 19:44:22 +02002816 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002817 fprintf(stderr, "failed to compile keymap\n");
2818 return;
2819 }
2820
Rui Matos3eccb862013-10-10 19:44:22 +02002821 state = xkb_state_new(keymap);
2822 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002823 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002824 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002825 return;
2826 }
2827
Rui Matos3eccb862013-10-10 19:44:22 +02002828 xkb_keymap_unref(input->xkb.keymap);
2829 xkb_state_unref(input->xkb.state);
2830 input->xkb.keymap = keymap;
2831 input->xkb.state = state;
2832
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002833 input->xkb.control_mask =
2834 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2835 input->xkb.alt_mask =
2836 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2837 input->xkb.shift_mask =
2838 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2839}
2840
2841static void
2842keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2843 uint32_t serial, struct wl_surface *surface,
2844 struct wl_array *keys)
2845{
2846 struct input *input = data;
2847 struct window *window;
2848
2849 input->display->serial = serial;
2850 input->keyboard_focus = wl_surface_get_user_data(surface);
2851
2852 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002853 if (window->keyboard_focus_handler)
2854 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002855 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002856}
2857
2858static void
2859keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2860 uint32_t serial, struct wl_surface *surface)
2861{
2862 struct input *input = data;
2863
2864 input->display->serial = serial;
2865 input_remove_keyboard_focus(input);
2866}
2867
Scott Moreau210d0792012-03-22 10:47:01 -06002868static void
Daniel Stone37816df2012-05-16 18:45:18 +01002869keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002870 uint32_t serial, uint32_t time, uint32_t key,
2871 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002872{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002873 struct input *input = data;
2874 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002875 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002876 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002877 const xkb_keysym_t *syms;
2878 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002879 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002880
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002881 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002882 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002883 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002884 return;
2885
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002886 /* We only use input grabs for pointer events for now, so just
2887 * ignore key presses if a grab is active. We expand the key
2888 * event delivery mechanism to route events to widgets to
2889 * properly handle key grabs. In the meantime, this prevents
2890 * key event devlivery while a grab is active. */
2891 if (input->grab && input->grab_button == 0)
2892 return;
2893
Daniel Stone97f68542012-05-30 16:32:01 +01002894 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002895
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002896 sym = XKB_KEY_NoSymbol;
2897 if (num_syms == 1)
2898 sym = syms[0];
2899
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002900
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002901 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002902 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002903 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002904 } else if (sym == XKB_KEY_F11 &&
2905 window->fullscreen_handler &&
2906 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2907 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002908 } else if (sym == XKB_KEY_F4 &&
2909 input->modifiers == MOD_ALT_MASK &&
2910 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002911 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002912 } else if (window->key_handler) {
2913 (*window->key_handler)(window, input, time, key,
2914 sym, state, window->user_data);
2915 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002916
2917 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2918 key == input->repeat_key) {
2919 its.it_interval.tv_sec = 0;
2920 its.it_interval.tv_nsec = 0;
2921 its.it_value.tv_sec = 0;
2922 its.it_value.tv_nsec = 0;
2923 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002924 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2925 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002926 input->repeat_sym = sym;
2927 input->repeat_key = key;
2928 input->repeat_time = time;
2929 its.it_interval.tv_sec = 0;
2930 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2931 its.it_value.tv_sec = 0;
2932 its.it_value.tv_nsec = 400 * 1000 * 1000;
2933 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2934 }
2935}
2936
2937static void
Daniel Stone351eb612012-05-31 15:27:47 -04002938keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2939 uint32_t serial, uint32_t mods_depressed,
2940 uint32_t mods_latched, uint32_t mods_locked,
2941 uint32_t group)
2942{
2943 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002944 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002945
Matt Ropere61561f2013-06-24 16:52:43 +01002946 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2947 if (!input->xkb.keymap)
2948 return;
2949
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002950 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2951 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002952 mask = xkb_state_serialize_mods(input->xkb.state,
2953 XKB_STATE_DEPRESSED |
2954 XKB_STATE_LATCHED);
2955 input->modifiers = 0;
2956 if (mask & input->xkb.control_mask)
2957 input->modifiers |= MOD_CONTROL_MASK;
2958 if (mask & input->xkb.alt_mask)
2959 input->modifiers |= MOD_ALT_MASK;
2960 if (mask & input->xkb.shift_mask)
2961 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002962}
2963
Daniel Stone37816df2012-05-16 18:45:18 +01002964static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002965 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002966 keyboard_handle_enter,
2967 keyboard_handle_leave,
2968 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002969 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002970};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002971
2972static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002973touch_handle_down(void *data, struct wl_touch *wl_touch,
2974 uint32_t serial, uint32_t time, struct wl_surface *surface,
2975 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2976{
2977 struct input *input = data;
2978 struct widget *widget;
2979 float sx = wl_fixed_to_double(x_w);
2980 float sy = wl_fixed_to_double(y_w);
2981
Rusty Lynch1084da52013-08-15 09:10:08 -07002982 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002983 input->touch_focus = wl_surface_get_user_data(surface);
2984 if (!input->touch_focus) {
2985 DBG("Failed to find to touch focus for surface %p\n", surface);
2986 return;
2987 }
2988
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002989 if (surface != input->touch_focus->main_surface->surface) {
2990 DBG("Ignoring input event from subsurface %p\n", surface);
2991 input->touch_focus = NULL;
2992 return;
2993 }
2994
Kristian Høgsberg1f671172014-04-29 14:30:44 -07002995 if (input->grab)
2996 widget = input->grab;
2997 else
2998 widget = window_find_widget(input->touch_focus,
2999 wl_fixed_to_double(x_w),
3000 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003001 if (widget) {
3002 struct touch_point *tp = xmalloc(sizeof *tp);
3003 if (tp) {
3004 tp->id = id;
3005 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003006 tp->x = sx;
3007 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003008 wl_list_insert(&input->touch_point_list, &tp->link);
3009
3010 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003011 (*widget->touch_down_handler)(widget, input,
3012 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003013 sx, sy,
3014 widget->user_data);
3015 }
3016 }
3017}
3018
3019static void
3020touch_handle_up(void *data, struct wl_touch *wl_touch,
3021 uint32_t serial, uint32_t time, int32_t id)
3022{
3023 struct input *input = data;
3024 struct touch_point *tp, *tmp;
3025
Rusty Lynch041815a2013-08-08 21:20:38 -07003026 if (!input->touch_focus) {
3027 DBG("No touch focus found for touch up event!\n");
3028 return;
3029 }
3030
3031 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3032 if (tp->id != id)
3033 continue;
3034
3035 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003036 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003037 time, id,
3038 tp->widget->user_data);
3039
3040 wl_list_remove(&tp->link);
3041 free(tp);
3042
3043 return;
3044 }
3045}
3046
3047static void
3048touch_handle_motion(void *data, struct wl_touch *wl_touch,
3049 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3050{
3051 struct input *input = data;
3052 struct touch_point *tp;
3053 float sx = wl_fixed_to_double(x_w);
3054 float sy = wl_fixed_to_double(y_w);
3055
3056 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3057
3058 if (!input->touch_focus) {
3059 DBG("No touch focus found for touch motion event!\n");
3060 return;
3061 }
3062
3063 wl_list_for_each(tp, &input->touch_point_list, link) {
3064 if (tp->id != id)
3065 continue;
3066
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003067 tp->x = sx;
3068 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003069 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003070 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003071 id, sx, sy,
3072 tp->widget->user_data);
3073 return;
3074 }
3075}
3076
3077static void
3078touch_handle_frame(void *data, struct wl_touch *wl_touch)
3079{
3080 struct input *input = data;
3081 struct touch_point *tp, *tmp;
3082
3083 DBG("touch_handle_frame\n");
3084
3085 if (!input->touch_focus) {
3086 DBG("No touch focus found for touch frame event!\n");
3087 return;
3088 }
3089
3090 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3091 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003092 (*tp->widget->touch_frame_handler)(tp->widget, input,
3093 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003094 }
3095}
3096
3097static void
3098touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3099{
3100 struct input *input = data;
3101 struct touch_point *tp, *tmp;
3102
3103 DBG("touch_handle_cancel\n");
3104
3105 if (!input->touch_focus) {
3106 DBG("No touch focus found for touch cancel event!\n");
3107 return;
3108 }
3109
3110 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3111 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003112 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3113 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003114
3115 wl_list_remove(&tp->link);
3116 free(tp);
3117 }
3118}
3119
3120static const struct wl_touch_listener touch_listener = {
3121 touch_handle_down,
3122 touch_handle_up,
3123 touch_handle_motion,
3124 touch_handle_frame,
3125 touch_handle_cancel,
3126};
3127
3128static void
Daniel Stone37816df2012-05-16 18:45:18 +01003129seat_handle_capabilities(void *data, struct wl_seat *seat,
3130 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003131{
Daniel Stone37816df2012-05-16 18:45:18 +01003132 struct input *input = data;
3133
3134 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3135 input->pointer = wl_seat_get_pointer(seat);
3136 wl_pointer_set_user_data(input->pointer, input);
3137 wl_pointer_add_listener(input->pointer, &pointer_listener,
3138 input);
3139 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3140 wl_pointer_destroy(input->pointer);
3141 input->pointer = NULL;
3142 }
3143
3144 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3145 input->keyboard = wl_seat_get_keyboard(seat);
3146 wl_keyboard_set_user_data(input->keyboard, input);
3147 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3148 input);
3149 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3150 wl_keyboard_destroy(input->keyboard);
3151 input->keyboard = NULL;
3152 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003153
3154 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3155 input->touch = wl_seat_get_touch(seat);
3156 wl_touch_set_user_data(input->touch, input);
3157 wl_touch_add_listener(input->touch, &touch_listener, input);
3158 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3159 wl_touch_destroy(input->touch);
3160 input->touch = NULL;
3161 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003162}
3163
Rob Bradford08031182013-08-13 20:11:03 +01003164static void
3165seat_handle_name(void *data, struct wl_seat *seat,
3166 const char *name)
3167{
3168
3169}
3170
Daniel Stone37816df2012-05-16 18:45:18 +01003171static const struct wl_seat_listener seat_listener = {
3172 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003173 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003174};
3175
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003176void
3177input_get_position(struct input *input, int32_t *x, int32_t *y)
3178{
3179 *x = input->sx;
3180 *y = input->sy;
3181}
3182
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003183int
3184input_get_touch(struct input *input, int32_t id, float *x, float *y)
3185{
3186 struct touch_point *tp;
3187
3188 wl_list_for_each(tp, &input->touch_point_list, link) {
3189 if (tp->id != id)
3190 continue;
3191
3192 *x = tp->x;
3193 *y = tp->y;
3194 return 0;
3195 }
3196
3197 return -1;
3198}
3199
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003200struct display *
3201input_get_display(struct input *input)
3202{
3203 return input->display;
3204}
3205
Daniel Stone37816df2012-05-16 18:45:18 +01003206struct wl_seat *
3207input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003208{
Daniel Stone37816df2012-05-16 18:45:18 +01003209 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003210}
3211
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003212uint32_t
3213input_get_modifiers(struct input *input)
3214{
3215 return input->modifiers;
3216}
3217
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003218struct widget *
3219input_get_focus_widget(struct input *input)
3220{
3221 return input->focus_widget;
3222}
3223
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003224struct data_offer {
3225 struct wl_data_offer *offer;
3226 struct input *input;
3227 struct wl_array types;
3228 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003229
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003230 struct task io_task;
3231 int fd;
3232 data_func_t func;
3233 int32_t x, y;
3234 void *user_data;
3235};
3236
3237static void
3238data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3239{
3240 struct data_offer *offer = data;
3241 char **p;
3242
3243 p = wl_array_add(&offer->types, sizeof *p);
3244 *p = strdup(type);
3245}
3246
3247static const struct wl_data_offer_listener data_offer_listener = {
3248 data_offer_offer,
3249};
3250
3251static void
3252data_offer_destroy(struct data_offer *offer)
3253{
3254 char **p;
3255
3256 offer->refcount--;
3257 if (offer->refcount == 0) {
3258 wl_data_offer_destroy(offer->offer);
3259 for (p = offer->types.data; *p; p++)
3260 free(*p);
3261 wl_array_release(&offer->types);
3262 free(offer);
3263 }
3264}
3265
3266static void
3267data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003268 struct wl_data_device *data_device,
3269 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003270{
3271 struct data_offer *offer;
3272
Brian Lovinbc919262013-08-07 15:34:59 -07003273 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003274
3275 wl_array_init(&offer->types);
3276 offer->refcount = 1;
3277 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003278 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003279 wl_data_offer_add_listener(offer->offer,
3280 &data_offer_listener, offer);
3281}
3282
3283static void
3284data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003285 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003286 wl_fixed_t x_w, wl_fixed_t y_w,
3287 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003288{
3289 struct input *input = data;
3290 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003291 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003292 float x = wl_fixed_to_double(x_w);
3293 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294 char **p;
3295
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003296 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003297 input->drag_enter_serial = serial;
3298 input->drag_focus = window,
3299 input->drag_x = x;
3300 input->drag_y = y;
3301
3302 if (!input->touch_grab)
3303 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003304
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003305 if (offer) {
3306 input->drag_offer = wl_data_offer_get_user_data(offer);
3307
3308 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3309 *p = NULL;
3310
3311 types_data = input->drag_offer->types.data;
3312 } else {
3313 input->drag_offer = NULL;
3314 types_data = NULL;
3315 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003316
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003317 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003318 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003319 window->user_data);
3320}
3321
3322static void
3323data_device_leave(void *data, struct wl_data_device *data_device)
3324{
3325 struct input *input = data;
3326
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003327 if (input->drag_offer) {
3328 data_offer_destroy(input->drag_offer);
3329 input->drag_offer = NULL;
3330 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003331}
3332
3333static void
3334data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003335 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003336{
3337 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003338 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003339 float x = wl_fixed_to_double(x_w);
3340 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003341 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003342
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003343 input->drag_x = x;
3344 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003345
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003346 if (input->drag_offer)
3347 types_data = input->drag_offer->types.data;
3348 else
3349 types_data = NULL;
3350
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003351 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003352 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003353 window->user_data);
3354}
3355
3356static void
3357data_device_drop(void *data, struct wl_data_device *data_device)
3358{
3359 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003360 struct window *window = input->drag_focus;
3361 float x, y;
3362
3363 x = input->drag_x;
3364 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003365
3366 if (window->drop_handler)
3367 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003368 x, y, window->user_data);
3369
3370 if (input->touch_grab)
3371 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003372}
3373
3374static void
3375data_device_selection(void *data,
3376 struct wl_data_device *wl_data_device,
3377 struct wl_data_offer *offer)
3378{
3379 struct input *input = data;
3380 char **p;
3381
3382 if (input->selection_offer)
3383 data_offer_destroy(input->selection_offer);
3384
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003385 if (offer) {
3386 input->selection_offer = wl_data_offer_get_user_data(offer);
3387 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3388 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003389 } else {
3390 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003391 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003392}
3393
3394static const struct wl_data_device_listener data_device_listener = {
3395 data_device_data_offer,
3396 data_device_enter,
3397 data_device_leave,
3398 data_device_motion,
3399 data_device_drop,
3400 data_device_selection
3401};
3402
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003403static void
3404input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003405{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003406 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003407 struct wl_cursor *cursor;
3408 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003409
Daniel Stone80972742012-11-07 17:51:39 +11003410 if (!input->pointer)
3411 return;
3412
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003413 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003414 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003415 return;
3416
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003417 if (index >= (int) cursor->image_count) {
3418 fprintf(stderr, "cursor index out of range\n");
3419 return;
3420 }
3421
3422 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003423 buffer = wl_cursor_image_get_buffer(image);
3424 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003425 return;
3426
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003427 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3428 wl_surface_damage(input->pointer_surface, 0, 0,
3429 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003430 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003431 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3432 input->pointer_surface,
3433 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003434}
3435
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003436static const struct wl_callback_listener pointer_surface_listener;
3437
3438static void
3439pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3440 uint32_t time)
3441{
3442 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003443 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003444 int i;
3445
3446 if (callback) {
3447 assert(callback == input->cursor_frame_cb);
3448 wl_callback_destroy(callback);
3449 input->cursor_frame_cb = NULL;
3450 }
3451
Daniel Stone80972742012-11-07 17:51:39 +11003452 if (!input->pointer)
3453 return;
3454
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003455 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003456 wl_pointer_set_cursor(input->pointer,
3457 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003458 NULL, 0, 0);
3459 return;
3460 }
3461
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003462 if (input->current_cursor == CURSOR_UNSET)
3463 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003464 cursor = input->display->cursors[input->current_cursor];
3465 if (!cursor)
3466 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003467
3468 /* FIXME We don't have the current time on the first call so we set
3469 * the animation start to the time of the first frame callback. */
3470 if (time == 0)
3471 input->cursor_anim_start = 0;
3472 else if (input->cursor_anim_start == 0)
3473 input->cursor_anim_start = time;
3474
3475 if (time == 0 || input->cursor_anim_start == 0)
3476 i = 0;
3477 else
3478 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3479
Pekka Paalanenbc106382012-10-10 12:49:31 +03003480 if (cursor->image_count > 1) {
3481 input->cursor_frame_cb =
3482 wl_surface_frame(input->pointer_surface);
3483 wl_callback_add_listener(input->cursor_frame_cb,
3484 &pointer_surface_listener, input);
3485 }
3486
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003487 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003488}
3489
3490static const struct wl_callback_listener pointer_surface_listener = {
3491 pointer_surface_frame_callback
3492};
3493
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003494void
3495input_set_pointer_image(struct input *input, int pointer)
3496{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003497 int force = 0;
3498
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003499 if (!input->pointer)
3500 return;
3501
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003502 if (input->pointer_enter_serial > input->cursor_serial)
3503 force = 1;
3504
3505 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003506 return;
3507
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003508 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003509 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003510 if (!input->cursor_frame_cb)
3511 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003512 else if (force) {
3513 /* The current frame callback may be stuck if, for instance,
3514 * the set cursor request was processed by the server after
3515 * this client lost the focus. In this case the cursor surface
3516 * might not be mapped and the frame callback wouldn't ever
3517 * complete. Send a set_cursor and attach to try to map the
3518 * cursor surface again so that the callback will finish */
3519 input_set_pointer_image_index(input, 0);
3520 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003521}
3522
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003523struct wl_data_device *
3524input_get_data_device(struct input *input)
3525{
3526 return input->data_device;
3527}
3528
3529void
3530input_set_selection(struct input *input,
3531 struct wl_data_source *source, uint32_t time)
3532{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003533 if (input->data_device)
3534 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003535}
3536
3537void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003538input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003539{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003540 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003541 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003542}
3543
3544static void
3545offer_io_func(struct task *task, uint32_t events)
3546{
3547 struct data_offer *offer =
3548 container_of(task, struct data_offer, io_task);
3549 unsigned int len;
3550 char buffer[4096];
3551
3552 len = read(offer->fd, buffer, sizeof buffer);
3553 offer->func(buffer, len,
3554 offer->x, offer->y, offer->user_data);
3555
3556 if (len == 0) {
3557 close(offer->fd);
3558 data_offer_destroy(offer);
3559 }
3560}
3561
3562static void
3563data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3564 data_func_t func, void *user_data)
3565{
3566 int p[2];
3567
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003568 if (pipe2(p, O_CLOEXEC) == -1)
3569 return;
3570
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003571 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3572 close(p[1]);
3573
3574 offer->io_task.run = offer_io_func;
3575 offer->fd = p[0];
3576 offer->func = func;
3577 offer->refcount++;
3578 offer->user_data = user_data;
3579
3580 display_watch_fd(offer->input->display,
3581 offer->fd, EPOLLIN, &offer->io_task);
3582}
3583
3584void
3585input_receive_drag_data(struct input *input, const char *mime_type,
3586 data_func_t func, void *data)
3587{
3588 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003589 input->drag_offer->x = input->drag_x;
3590 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003591}
3592
3593int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003594input_receive_drag_data_to_fd(struct input *input,
3595 const char *mime_type, int fd)
3596{
3597 if (input->drag_offer)
3598 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3599
3600 return 0;
3601}
3602
3603int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003604input_receive_selection_data(struct input *input, const char *mime_type,
3605 data_func_t func, void *data)
3606{
3607 char **p;
3608
3609 if (input->selection_offer == NULL)
3610 return -1;
3611
3612 for (p = input->selection_offer->types.data; *p; p++)
3613 if (strcmp(mime_type, *p) == 0)
3614 break;
3615
3616 if (*p == NULL)
3617 return -1;
3618
3619 data_offer_receive_data(input->selection_offer,
3620 mime_type, func, data);
3621 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003622}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003623
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003624int
3625input_receive_selection_data_to_fd(struct input *input,
3626 const char *mime_type, int fd)
3627{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003628 if (input->selection_offer)
3629 wl_data_offer_receive(input->selection_offer->offer,
3630 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003631
3632 return 0;
3633}
3634
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003635void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003636window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003637{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003638 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003639 return;
3640
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003641 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003642}
3643
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003644static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003645surface_set_synchronized(struct surface *surface)
3646{
3647 if (!surface->subsurface)
3648 return;
3649
3650 if (surface->synchronized)
3651 return;
3652
3653 wl_subsurface_set_sync(surface->subsurface);
3654 surface->synchronized = 1;
3655}
3656
3657static void
3658surface_set_synchronized_default(struct surface *surface)
3659{
3660 if (!surface->subsurface)
3661 return;
3662
3663 if (surface->synchronized == surface->synchronized_default)
3664 return;
3665
3666 if (surface->synchronized_default)
3667 wl_subsurface_set_sync(surface->subsurface);
3668 else
3669 wl_subsurface_set_desync(surface->subsurface);
3670
3671 surface->synchronized = surface->synchronized_default;
3672}
3673
3674static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003675surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003676{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003677 struct widget *widget = surface->widget;
3678 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003679
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003680 if (surface->input_region) {
3681 wl_region_destroy(surface->input_region);
3682 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003683 }
3684
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003685 if (surface->opaque_region)
3686 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003687
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003688 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003689
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003690 if (widget->resize_handler)
3691 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003692 widget->allocation.width,
3693 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003694 widget->user_data);
3695
Pekka Paalanen35e82632013-04-25 13:57:48 +03003696 if (surface->subsurface &&
3697 (surface->allocation.x != widget->allocation.x ||
3698 surface->allocation.y != widget->allocation.y)) {
3699 wl_subsurface_set_position(surface->subsurface,
3700 widget->allocation.x,
3701 widget->allocation.y);
3702 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003703 if (surface->allocation.width != widget->allocation.width ||
3704 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003705 window_schedule_redraw(widget->window);
3706 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003707 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003708
3709 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003710 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003711 widget->allocation.width,
3712 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003713}
3714
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003715static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003716hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3717{
3718 /*
3719 * This hack should be removed, when EGL respects
3720 * eglSwapInterval(0).
3721 *
3722 * If this window has sub-surfaces, especially a free-running
3723 * EGL-widget, we need to post the parent surface once with
3724 * all the old state to guarantee, that the EGL-widget will
3725 * receive its frame callback soon. Otherwise, a forced call
3726 * to eglSwapBuffers may end up blocking, waiting for a frame
3727 * event that will never come, because we will commit the parent
3728 * surface with all new state only after eglSwapBuffers returns.
3729 *
3730 * This assumes, that:
3731 * 1. When the EGL widget's resize hook is called, it pauses.
3732 * 2. When the EGL widget's redraw hook is called, it forces a
3733 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3734 * In a single threaded application condition 1 is a no-op.
3735 *
3736 * XXX: This should actually be after the surface_resize() calls,
3737 * but cannot, because then it would commit the incomplete state
3738 * accumulated from the widget resize hooks.
3739 */
3740 if (window->subsurface_list.next != &window->main_surface->link ||
3741 window->subsurface_list.prev != &window->main_surface->link)
3742 wl_surface_commit(window->main_surface->surface);
3743}
3744
3745static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003746window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003747{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003748 struct surface *surface;
3749
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003750 widget_set_allocation(window->main_surface->widget,
3751 window->pending_allocation.x,
3752 window->pending_allocation.y,
3753 window->pending_allocation.width,
3754 window->pending_allocation.height);
3755
3756 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003757
3758 /* The main surface is in the list, too. Main surface's
3759 * resize_handler is responsible for calling widget_set_allocation()
3760 * on all sub-surface root widgets, so they will be resized
3761 * properly.
3762 */
3763 wl_list_for_each(surface, &window->subsurface_list, link) {
3764 if (surface == window->main_surface)
3765 continue;
3766
3767 surface_set_synchronized(surface);
3768 surface_resize(surface);
3769 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003770
3771 if (!window->fullscreen && !window->maximized)
3772 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003773}
3774
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003775static void
3776idle_resize(struct window *window)
3777{
3778 window->resize_needed = 0;
3779 window->redraw_needed = 1;
3780
3781 DBG("from %dx%d to %dx%d\n",
3782 window->main_surface->server_allocation.width,
3783 window->main_surface->server_allocation.height,
3784 window->pending_allocation.width,
3785 window->pending_allocation.height);
3786
3787 hack_prevent_EGL_sub_surface_deadlock(window);
3788
3789 window_do_resize(window);
3790}
3791
3792static void
3793undo_resize(struct window *window)
3794{
3795 window->pending_allocation.width =
3796 window->main_surface->server_allocation.width;
3797 window->pending_allocation.height =
3798 window->main_surface->server_allocation.height;
3799
3800 DBG("back to %dx%d\n",
3801 window->main_surface->server_allocation.width,
3802 window->main_surface->server_allocation.height);
3803
3804 window_do_resize(window);
3805
3806 if (window->pending_allocation.width == 0 &&
3807 window->pending_allocation.height == 0) {
3808 fprintf(stderr, "Error: Could not draw a surface, "
3809 "most likely due to insufficient disk space in "
3810 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3811 exit(EXIT_FAILURE);
3812 }
3813}
3814
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003815void
3816window_schedule_resize(struct window *window, int width, int height)
3817{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003818 /* We should probably get these numbers from the theme. */
3819 const int min_width = 200, min_height = 200;
3820
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003821 window->pending_allocation.x = 0;
3822 window->pending_allocation.y = 0;
3823 window->pending_allocation.width = width;
3824 window->pending_allocation.height = height;
3825
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003826 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003827 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003828 window->min_allocation.width = min_width;
3829 else
3830 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003831 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003832 window->min_allocation.height = min_height;
3833 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003834 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003835 }
3836
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003837 if (window->pending_allocation.width < window->min_allocation.width)
3838 window->pending_allocation.width = window->min_allocation.width;
3839 if (window->pending_allocation.height < window->min_allocation.height)
3840 window->pending_allocation.height = window->min_allocation.height;
3841
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003842 window->resize_needed = 1;
3843 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003844}
3845
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003846void
3847widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3848{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003849 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003850}
3851
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003852static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003853handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003854 int32_t width, int32_t height,
3855 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003856{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003857 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003858 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003859
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003860 if (width > 0 && height > 0) {
3861 window_schedule_resize(window, width, height);
3862 } else {
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003863 window_schedule_resize(window,
3864 window->saved_allocation.width,
3865 window->saved_allocation.height);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003866 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003867
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003868 window->maximized = 0;
3869 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003870 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003871 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003872
3873 wl_array_for_each(p, states) {
3874 uint32_t state = *p;
3875 switch (state) {
3876 case XDG_SURFACE_STATE_MAXIMIZED:
3877 window->maximized = 1;
3878 break;
3879 case XDG_SURFACE_STATE_FULLSCREEN:
3880 window->fullscreen = 1;
3881 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003882 case XDG_SURFACE_STATE_RESIZING:
3883 window->resizing = 1;
3884 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003885 case XDG_SURFACE_STATE_ACTIVATED:
3886 window->focused = 1;
3887 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003888 default:
3889 /* Unknown state */
3890 break;
3891 }
3892 }
3893
3894 window->next_attach_serial = serial;
Jasper St. Pierrede680992014-04-10 17:23:49 -07003895
3896 if (window->state_changed_handler)
3897 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003898}
3899
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003900static void
3901handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3902{
3903 struct window *window = data;
3904 window_close(window);
3905}
3906
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003907static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003908 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003909 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003910};
3911
3912static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003913window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05003914{
3915 struct wl_surface *parent_surface;
3916
3917 if (!window->xdg_surface)
3918 return;
3919
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003920 if (window->parent)
3921 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05003922 else
3923 parent_surface = NULL;
3924
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003925 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre53686042013-12-09 15:26:25 -05003926}
3927
3928static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003929window_sync_margin(struct window *window)
3930{
3931 int margin;
3932
3933 if (!window->xdg_surface)
3934 return;
3935
3936 if (!window->frame)
3937 return;
3938
3939 margin = frame_get_shadow_margin(window->frame->frame);
3940
3941 /* Shadow size is the same on every side. */
3942 xdg_surface_set_margin(window->xdg_surface,
3943 margin,
3944 margin,
3945 margin,
3946 margin);
3947}
3948
3949static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003950window_flush(struct window *window)
3951{
3952 struct surface *surface;
3953
3954 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003955 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003956 window_sync_parent(window);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003957 window_sync_margin(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003958 }
3959 }
3960
3961 wl_list_for_each(surface, &window->subsurface_list, link) {
3962 if (surface == window->main_surface)
3963 continue;
3964
3965 surface_flush(surface);
3966 }
3967
3968 surface_flush(window->main_surface);
3969}
3970
3971static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003972menu_destroy(struct menu *menu)
3973{
3974 widget_destroy(menu->widget);
3975 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003976 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003977 free(menu);
3978}
3979
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003980void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003981window_get_allocation(struct window *window,
3982 struct rectangle *allocation)
3983{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003984 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003985}
3986
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003987static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003988widget_redraw(struct widget *widget)
3989{
3990 struct widget *child;
3991
3992 if (widget->redraw_handler)
3993 widget->redraw_handler(widget, widget->user_data);
3994 wl_list_for_each(child, &widget->child_list, link)
3995 widget_redraw(child);
3996}
3997
3998static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003999frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4000{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004001 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004002
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004003 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004004 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004005 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004006 surface->frame_cb = NULL;
4007
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004008 surface->last_time = time;
4009
Pekka Paalanen71233882013-04-25 13:57:53 +03004010 if (surface->redraw_needed || surface->window->redraw_needed) {
4011 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004012 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004013 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004014}
4015
4016static const struct wl_callback_listener listener = {
4017 frame_callback
4018};
4019
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004020static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004021surface_redraw(struct surface *surface)
4022{
Pekka Paalanen71233882013-04-25 13:57:53 +03004023 DBG_OBJ(surface->surface, "begin\n");
4024
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004025 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004026 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004027
4028 /* Whole-window redraw forces a redraw even if the previous has
4029 * not yet hit the screen.
4030 */
4031 if (surface->frame_cb) {
4032 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004033 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004034
Pekka Paalanen71233882013-04-25 13:57:53 +03004035 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004036 wl_callback_destroy(surface->frame_cb);
4037 }
4038
Neil Roberts97b747c2013-12-19 16:17:12 +00004039 if (surface->widget->use_cairo &&
4040 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004041 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4042 return -1;
4043 }
4044
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004045 surface->frame_cb = wl_surface_frame(surface->surface);
4046 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004047 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004048
4049 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004050 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004051 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004052 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004053 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004054}
4055
4056static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004057idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004058{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004059 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004060 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004061 int failed = 0;
4062 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004063
Pekka Paalanen71233882013-04-25 13:57:53 +03004064 DBG(" --------- \n");
4065
Pekka Paalaneneebff542013-04-25 13:57:52 +03004066 wl_list_init(&window->redraw_task.link);
4067 window->redraw_task_scheduled = 0;
4068
4069 if (window->resize_needed) {
4070 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004071 if (window->main_surface->frame_cb) {
4072 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004073 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004074 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004075
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004076 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004077 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004078 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004079
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004080 if (surface_redraw(window->main_surface) < 0) {
4081 /*
4082 * Only main_surface failure will cause us to undo the resize.
4083 * If sub-surfaces fail, they will just be broken with old
4084 * content.
4085 */
4086 failed = 1;
4087 } else {
4088 wl_list_for_each(surface, &window->subsurface_list, link) {
4089 if (surface == window->main_surface)
4090 continue;
4091
4092 surface_redraw(surface);
4093 }
4094 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004095
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004096 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004097 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004098
4099 wl_list_for_each(surface, &window->subsurface_list, link)
4100 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004101
4102 if (resized && failed) {
4103 /* Restore widget tree to correspond to what is on screen. */
4104 undo_resize(window);
4105 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004106}
4107
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004108static void
4109window_schedule_redraw_task(struct window *window)
4110{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004111 if (!window->redraw_task_scheduled) {
4112 window->redraw_task.run = idle_redraw;
4113 display_defer(window->display, &window->redraw_task);
4114 window->redraw_task_scheduled = 1;
4115 }
4116}
4117
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004118void
4119window_schedule_redraw(struct window *window)
4120{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004121 struct surface *surface;
4122
Pekka Paalanen71233882013-04-25 13:57:53 +03004123 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4124
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004125 wl_list_for_each(surface, &window->subsurface_list, link)
4126 surface->redraw_needed = 1;
4127
4128 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004129}
4130
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004131int
4132window_is_fullscreen(struct window *window)
4133{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004134 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004135}
4136
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004137void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004138window_set_fullscreen(struct window *window, int fullscreen)
4139{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004140 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004141 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004142
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004143 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004144 return;
4145
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004146 if (fullscreen)
4147 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4148 else
4149 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004150}
4151
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004152int
4153window_is_maximized(struct window *window)
4154{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004155 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004156}
4157
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004158void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004159window_set_maximized(struct window *window, int maximized)
4160{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004161 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004162 return;
4163
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004164 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004165 return;
4166
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004167 if (maximized)
4168 xdg_surface_set_maximized(window->xdg_surface);
4169 else
4170 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004171}
4172
Jasper St. Pierrede680992014-04-10 17:23:49 -07004173int
4174window_is_resizing(struct window *window)
4175{
4176 return window->resizing;
4177}
4178
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004179void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004180window_set_minimized(struct window *window)
4181{
4182 if (!window->xdg_surface)
4183 return;
4184
4185 xdg_surface_set_minimized(window->xdg_surface);
4186}
4187
4188void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004189window_set_user_data(struct window *window, void *data)
4190{
4191 window->user_data = data;
4192}
4193
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004194void *
4195window_get_user_data(struct window *window)
4196{
4197 return window->user_data;
4198}
4199
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004200void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004201window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004202 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004203{
4204 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004205}
4206
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004207void
4208window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004209 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004210{
4211 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004212}
4213
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004214void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004215window_set_data_handler(struct window *window, window_data_handler_t handler)
4216{
4217 window->data_handler = handler;
4218}
4219
4220void
4221window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4222{
4223 window->drop_handler = handler;
4224}
4225
4226void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004227window_set_close_handler(struct window *window,
4228 window_close_handler_t handler)
4229{
4230 window->close_handler = handler;
4231}
4232
4233void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004234window_set_fullscreen_handler(struct window *window,
4235 window_fullscreen_handler_t handler)
4236{
4237 window->fullscreen_handler = handler;
4238}
4239
4240void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004241window_set_output_handler(struct window *window,
4242 window_output_handler_t handler)
4243{
4244 window->output_handler = handler;
4245}
4246
4247void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004248window_set_state_changed_handler(struct window *window,
4249 window_state_changed_handler_t handler)
4250{
4251 window->state_changed_handler = handler;
4252}
4253
4254void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004255window_set_title(struct window *window, const char *title)
4256{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004257 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004258 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004259 if (window->frame) {
4260 frame_set_title(window->frame->frame, title);
4261 widget_schedule_redraw(window->frame->widget);
4262 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004263 if (window->xdg_surface)
4264 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004265}
4266
4267const char *
4268window_get_title(struct window *window)
4269{
4270 return window->title;
4271}
4272
4273void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004274window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4275{
4276 struct text_cursor_position *text_cursor_position =
4277 window->display->text_cursor_position;
4278
Scott Moreau9295ce02012-06-01 12:46:10 -06004279 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004280 return;
4281
4282 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004283 window->main_surface->surface,
4284 wl_fixed_from_int(x),
4285 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004286}
4287
4288void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004289window_damage(struct window *window, int32_t x, int32_t y,
4290 int32_t width, int32_t height)
4291{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004292 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004293}
4294
Casey Dahlin9074db52012-04-19 22:50:09 -04004295static void
4296surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004297 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004298{
Rob Bradford7507b572012-05-15 17:55:34 +01004299 struct window *window = data;
4300 struct output *output;
4301 struct output *output_found = NULL;
4302 struct window_output *window_output;
4303
4304 wl_list_for_each(output, &window->display->output_list, link) {
4305 if (output->output == wl_output) {
4306 output_found = output;
4307 break;
4308 }
4309 }
4310
4311 if (!output_found)
4312 return;
4313
Brian Lovinbc919262013-08-07 15:34:59 -07004314 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004315 window_output->output = output_found;
4316
4317 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004318
4319 if (window->output_handler)
4320 window->output_handler(window, output_found, 1,
4321 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004322}
4323
4324static void
4325surface_leave(void *data,
4326 struct wl_surface *wl_surface, struct wl_output *output)
4327{
Rob Bradford7507b572012-05-15 17:55:34 +01004328 struct window *window = data;
4329 struct window_output *window_output;
4330 struct window_output *window_output_found = NULL;
4331
4332 wl_list_for_each(window_output, &window->window_output_list, link) {
4333 if (window_output->output->output == output) {
4334 window_output_found = window_output;
4335 break;
4336 }
4337 }
4338
4339 if (window_output_found) {
4340 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004341
4342 if (window->output_handler)
4343 window->output_handler(window, window_output->output,
4344 0, window->user_data);
4345
Rob Bradford7507b572012-05-15 17:55:34 +01004346 free(window_output_found);
4347 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004348}
4349
4350static const struct wl_surface_listener surface_listener = {
4351 surface_enter,
4352 surface_leave
4353};
4354
Pekka Paalanen4e373742013-02-13 16:17:13 +02004355static struct surface *
4356surface_create(struct window *window)
4357{
4358 struct display *display = window->display;
4359 struct surface *surface;
4360
Brian Lovinbc919262013-08-07 15:34:59 -07004361 surface = xmalloc(sizeof *surface);
4362 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004363 if (!surface)
4364 return NULL;
4365
4366 surface->window = window;
4367 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004368 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004369 wl_surface_add_listener(surface->surface, &surface_listener, window);
4370
Pekka Paalanen35e82632013-04-25 13:57:48 +03004371 wl_list_insert(&window->subsurface_list, &surface->link);
4372
Pekka Paalanen4e373742013-02-13 16:17:13 +02004373 return surface;
4374}
4375
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004376static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004377get_preferred_buffer_type(struct display *display)
4378{
4379#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004380 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004381 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4382#endif
4383
4384 return WINDOW_BUFFER_TYPE_SHM;
4385}
4386
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004387static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004388window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004389{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004390 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004391 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004392
Peter Huttererf3d62272013-08-08 11:57:05 +10004393 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004394 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004395 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004396
4397 surface = surface_create(window);
4398 window->main_surface = surface;
4399
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004400 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004401
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004402 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004403 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004404
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004405 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004406
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004407 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004408 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004409 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004410
Rob Bradford7507b572012-05-15 17:55:34 +01004411 wl_list_init (&window->window_output_list);
4412
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004413 return window;
4414}
4415
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004416struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004417window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004418{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004419 struct window *window;
4420
4421 window = window_create_internal(display, 0);
4422
4423 window->xdg_surface =
4424 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4425 window->main_surface->surface);
4426 fail_on_null(window->xdg_surface);
4427
4428 xdg_surface_set_user_data(window->xdg_surface, window);
4429 xdg_surface_add_listener(window->xdg_surface,
4430 &xdg_surface_listener, window);
4431
4432 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004433}
4434
4435struct window *
4436window_create_custom(struct display *display)
4437{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004438 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004439}
4440
Jasper St. Pierre53686042013-12-09 15:26:25 -05004441void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004442window_set_parent(struct window *window,
4443 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004444{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004445 window->parent = parent_window;
4446 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004447}
4448
4449struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004450window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004451{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004452 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004453}
4454
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004455static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004456menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004457{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004458 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004459 int next;
4460
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004461 frame_interior(menu->frame, &x, &y, &width, &height);
4462 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004463 if (menu->current != next) {
4464 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004465 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004466 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004467}
4468
4469static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004470menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004471 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004472 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004473{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004474 struct menu *menu = data;
4475
4476 if (widget == menu->widget)
4477 menu_set_item(data, y);
4478
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004479 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004480}
4481
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004482static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004483menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004484 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004485{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004486 struct menu *menu = data;
4487
4488 if (widget == menu->widget)
4489 menu_set_item(data, y);
4490
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004491 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004492}
4493
4494static void
4495menu_leave_handler(struct widget *widget, struct input *input, void *data)
4496{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004497 struct menu *menu = data;
4498
4499 if (widget == menu->widget)
4500 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004501}
4502
4503static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004504menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004505 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004506 uint32_t button, enum wl_pointer_button_state state,
4507 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004508
4509{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004510 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004511
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004512 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4513 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004514 /* Either relase after press-drag-release or
4515 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004516 menu->func(menu->parent, input,
4517 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004518 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004519 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004520 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004521 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004522 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004523}
4524
4525static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004526menu_touch_up_handler(struct widget *widget,
4527 struct input *input,
4528 uint32_t serial,
4529 uint32_t time,
4530 int32_t id,
4531 void *data)
4532{
4533 struct menu *menu = data;
4534
4535 input_ungrab(input);
4536 menu_destroy(menu);
4537}
4538
4539static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004540menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004541{
4542 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004543 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004544 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004545
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004546 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004548 frame_repaint(menu->frame, cr);
4549 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004550
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004551 theme_set_background_source(menu->window->display->theme,
4552 cr, THEME_FRAME_ACTIVE);
4553 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004554 cairo_fill(cr);
4555
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004556 cairo_select_font_face(cr, "sans",
4557 CAIRO_FONT_SLANT_NORMAL,
4558 CAIRO_FONT_WEIGHT_NORMAL);
4559 cairo_set_font_size(cr, 12);
4560
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004561 for (i = 0; i < menu->count; i++) {
4562 if (i == menu->current) {
4563 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004564 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004565 cairo_fill(cr);
4566 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004567 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004568 cairo_show_text(cr, menu->entries[i]);
4569 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004570 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4571 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004572 cairo_show_text(cr, menu->entries[i]);
4573 }
4574 }
4575
4576 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004577}
4578
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004579static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004580handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4581{
4582 struct window *window = data;
4583 struct menu *menu = window->main_surface->widget->user_data;
4584
4585 input_ungrab(menu->input);
4586 menu_destroy(menu);
4587}
4588
4589static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004590 handle_popup_popup_done,
4591};
4592
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004593void
4594window_show_menu(struct display *display,
4595 struct input *input, uint32_t time, struct window *parent,
4596 int32_t x, int32_t y,
4597 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004598{
4599 struct window *window;
4600 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004601 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004602
4603 menu = malloc(sizeof *menu);
4604 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004605 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004606
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004607 window = window_create_internal(parent->display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004608 if (!window) {
4609 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004610 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004611 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004612
4613 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004614 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004615 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004616 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4617 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004618 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004619 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004620 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004621 menu->entries = entries;
4622 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004623 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004624 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004625 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004626 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004627 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004628 window->x = x;
4629 window->y = y;
4630
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004631 input_ungrab(input);
4632
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004633 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004634 widget_set_enter_handler(menu->widget, menu_enter_handler);
4635 widget_set_leave_handler(menu->widget, menu_leave_handler);
4636 widget_set_motion_handler(menu->widget, menu_motion_handler);
4637 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004638 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004639
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004640 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004641 frame_resize_inside(menu->frame, 200, count * 20);
4642 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4643 window_schedule_resize(window, frame_width(menu->frame),
4644 frame_height(menu->frame));
4645
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004646 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004647
4648 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4649 window->main_surface->surface,
4650 parent->main_surface->surface,
4651 input->seat,
4652 display_get_serial(window->display),
4653 window->x - ix,
4654 window->y - iy,
4655 0);
4656 fail_on_null(window->xdg_popup);
4657
4658 xdg_popup_set_user_data(window->xdg_popup, window);
4659 xdg_popup_add_listener(window->xdg_popup,
4660 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004661}
4662
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004663void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004664window_set_buffer_type(struct window *window, enum window_buffer_type type)
4665{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004666 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004667}
4668
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004669enum window_buffer_type
4670window_get_buffer_type(struct window *window)
4671{
4672 return window->main_surface->buffer_type;
4673}
4674
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004675void
4676window_set_preferred_format(struct window *window,
4677 enum preferred_format format)
4678{
4679 window->preferred_format = format;
4680}
4681
Pekka Paalanen35e82632013-04-25 13:57:48 +03004682struct widget *
4683window_add_subsurface(struct window *window, void *data,
4684 enum subsurface_mode default_mode)
4685{
4686 struct widget *widget;
4687 struct surface *surface;
4688 struct wl_surface *parent;
4689 struct wl_subcompositor *subcompo = window->display->subcompositor;
4690
Pekka Paalanen35e82632013-04-25 13:57:48 +03004691 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004692 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004693 widget = widget_create(window, surface, data);
4694 wl_list_init(&widget->link);
4695 surface->widget = widget;
4696
4697 parent = window->main_surface->surface;
4698 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4699 surface->surface,
4700 parent);
4701 surface->synchronized = 1;
4702
4703 switch (default_mode) {
4704 case SUBSURFACE_SYNCHRONIZED:
4705 surface->synchronized_default = 1;
4706 break;
4707 case SUBSURFACE_DESYNCHRONIZED:
4708 surface->synchronized_default = 0;
4709 break;
4710 default:
4711 assert(!"bad enum subsurface_mode");
4712 }
4713
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004714 window->resize_needed = 1;
4715 window_schedule_redraw(window);
4716
Pekka Paalanen35e82632013-04-25 13:57:48 +03004717 return widget;
4718}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004719
4720static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004721display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004722 struct wl_output *wl_output,
4723 int x, int y,
4724 int physical_width,
4725 int physical_height,
4726 int subpixel,
4727 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004728 const char *model,
4729 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004730{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004731 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004732
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004733 output->allocation.x = x;
4734 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004735 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004736
4737 if (output->make)
4738 free(output->make);
4739 output->make = strdup(make);
4740
4741 if (output->model)
4742 free(output->model);
4743 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004744}
4745
4746static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004747display_handle_done(void *data,
4748 struct wl_output *wl_output)
4749{
4750}
4751
4752static void
4753display_handle_scale(void *data,
4754 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004755 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004756{
4757 struct output *output = data;
4758
4759 output->scale = scale;
4760}
4761
4762static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004763display_handle_mode(void *data,
4764 struct wl_output *wl_output,
4765 uint32_t flags,
4766 int width,
4767 int height,
4768 int refresh)
4769{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004770 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004771 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004772
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004773 if (flags & WL_OUTPUT_MODE_CURRENT) {
4774 output->allocation.width = width;
4775 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004776 if (display->output_configure_handler)
4777 (*display->output_configure_handler)(
4778 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004779 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004780}
4781
4782static const struct wl_output_listener output_listener = {
4783 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004784 display_handle_mode,
4785 display_handle_done,
4786 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004787};
4788
4789static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004790display_add_output(struct display *d, uint32_t id)
4791{
4792 struct output *output;
4793
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004794 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004795 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004796 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004797 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004798 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004799 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004800 wl_list_insert(d->output_list.prev, &output->link);
4801
4802 wl_output_add_listener(output->output, &output_listener, output);
4803}
4804
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004805static void
4806output_destroy(struct output *output)
4807{
4808 if (output->destroy_handler)
4809 (*output->destroy_handler)(output, output->user_data);
4810
4811 wl_output_destroy(output->output);
4812 wl_list_remove(&output->link);
4813 free(output);
4814}
4815
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004816static void
4817display_destroy_output(struct display *d, uint32_t id)
4818{
4819 struct output *output;
4820
4821 wl_list_for_each(output, &d->output_list, link) {
4822 if (output->server_output_id == id) {
4823 output_destroy(output);
4824 break;
4825 }
4826 }
4827}
4828
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004829void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004830display_set_global_handler(struct display *display,
4831 display_global_handler_t handler)
4832{
4833 struct global *global;
4834
4835 display->global_handler = handler;
4836 if (!handler)
4837 return;
4838
4839 wl_list_for_each(global, &display->global_list, link)
4840 display->global_handler(display,
4841 global->name, global->interface,
4842 global->version, display->user_data);
4843}
4844
4845void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004846display_set_global_handler_remove(struct display *display,
4847 display_global_handler_t remove_handler)
4848{
4849 display->global_handler_remove = remove_handler;
4850 if (!remove_handler)
4851 return;
4852}
4853
4854void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004855display_set_output_configure_handler(struct display *display,
4856 display_output_handler_t handler)
4857{
4858 struct output *output;
4859
4860 display->output_configure_handler = handler;
4861 if (!handler)
4862 return;
4863
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004864 wl_list_for_each(output, &display->output_list, link) {
4865 if (output->allocation.width == 0 &&
4866 output->allocation.height == 0)
4867 continue;
4868
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004869 (*display->output_configure_handler)(output,
4870 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004871 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004872}
4873
4874void
4875output_set_user_data(struct output *output, void *data)
4876{
4877 output->user_data = data;
4878}
4879
4880void *
4881output_get_user_data(struct output *output)
4882{
4883 return output->user_data;
4884}
4885
4886void
4887output_set_destroy_handler(struct output *output,
4888 display_output_handler_t handler)
4889{
4890 output->destroy_handler = handler;
4891 /* FIXME: implement this, once we have way to remove outputs */
4892}
4893
4894void
Scott Moreau4e072362012-09-29 02:03:11 -06004895output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004896{
Scott Moreau4e072362012-09-29 02:03:11 -06004897 struct rectangle allocation = output->allocation;
4898
4899 switch (output->transform) {
4900 case WL_OUTPUT_TRANSFORM_90:
4901 case WL_OUTPUT_TRANSFORM_270:
4902 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4903 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4904 /* Swap width and height */
4905 allocation.width = output->allocation.height;
4906 allocation.height = output->allocation.width;
4907 break;
4908 }
4909
4910 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004911}
4912
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004913struct wl_output *
4914output_get_wl_output(struct output *output)
4915{
4916 return output->output;
4917}
4918
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004919enum wl_output_transform
4920output_get_transform(struct output *output)
4921{
4922 return output->transform;
4923}
4924
Alexander Larssonafd319a2013-05-22 14:41:27 +02004925uint32_t
4926output_get_scale(struct output *output)
4927{
4928 return output->scale;
4929}
4930
Jason Ekstrand738715d2014-04-02 19:53:50 -05004931const char *
4932output_get_make(struct output *output)
4933{
4934 return output->make;
4935}
4936
4937const char *
4938output_get_model(struct output *output)
4939{
4940 return output->model;
4941}
4942
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004943static void
Daniel Stone97f68542012-05-30 16:32:01 +01004944fini_xkb(struct input *input)
4945{
4946 xkb_state_unref(input->xkb.state);
4947 xkb_map_unref(input->xkb.keymap);
4948}
4949
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004950#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004951
Daniel Stone97f68542012-05-30 16:32:01 +01004952static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004953display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004954{
4955 struct input *input;
4956
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004957 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004958 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004959 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004960 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004961 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004962 input->pointer_focus = NULL;
4963 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004964 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004965 wl_list_insert(d->input_list.prev, &input->link);
4966
Daniel Stone37816df2012-05-16 18:45:18 +01004967 wl_seat_add_listener(input->seat, &seat_listener, input);
4968 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004969
Jason Ekstranda669bd52014-04-02 19:53:51 -05004970 if (d->data_device_manager) {
4971 input->data_device =
4972 wl_data_device_manager_get_data_device(d->data_device_manager,
4973 input->seat);
4974 wl_data_device_add_listener(input->data_device,
4975 &data_device_listener,
4976 input);
4977 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004978
4979 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004980
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004981 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4982 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004983 input->repeat_task.run = keyboard_repeat_func;
4984 display_watch_fd(d, input->repeat_timer_fd,
4985 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004986}
4987
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004988static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004989input_destroy(struct input *input)
4990{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004991 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004992 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004993
4994 if (input->drag_offer)
4995 data_offer_destroy(input->drag_offer);
4996
4997 if (input->selection_offer)
4998 data_offer_destroy(input->selection_offer);
4999
Jason Ekstranda669bd52014-04-02 19:53:51 -05005000 if (input->data_device)
5001 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005002
5003 if (input->display->seat_version >= 3) {
5004 if (input->pointer)
5005 wl_pointer_release(input->pointer);
5006 if (input->keyboard)
5007 wl_keyboard_release(input->keyboard);
5008 }
5009
Daniel Stone97f68542012-05-30 16:32:01 +01005010 fini_xkb(input);
5011
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005012 wl_surface_destroy(input->pointer_surface);
5013
Pekka Paalanene1207c72011-12-16 12:02:09 +02005014 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005015 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005016 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005017 free(input);
5018}
5019
5020static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005021init_workspace_manager(struct display *d, uint32_t id)
5022{
5023 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005024 wl_registry_bind(d->registry, id,
5025 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005026 if (d->workspace_manager != NULL)
5027 workspace_manager_add_listener(d->workspace_manager,
5028 &workspace_manager_listener,
5029 d);
5030}
5031
5032static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005033shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5034{
5035 struct display *d = data;
5036
5037 if (format == WL_SHM_FORMAT_RGB565)
5038 d->has_rgb565 = 1;
5039}
5040
5041struct wl_shm_listener shm_listener = {
5042 shm_format
5043};
5044
5045static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005046xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5047{
5048 xdg_shell_pong(shell, serial);
5049}
5050
5051static const struct xdg_shell_listener xdg_shell_listener = {
5052 xdg_shell_ping,
5053};
5054
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08005055#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005056#ifdef static_assert
5057static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5058 "Interface version doesn't match implementation version");
5059#endif
5060
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005061static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005062registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5063 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005064{
5065 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005066 struct global *global;
5067
Brian Lovinbc919262013-08-07 15:34:59 -07005068 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005069 global->name = id;
5070 global->interface = strdup(interface);
5071 global->version = version;
5072 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005073
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005074 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005075 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005076 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005077 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005078 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005079 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005080 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005081 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005082 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005083 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005084 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005085 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5086 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005087 wl_registry_bind(registry, id,
5088 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005089 } else if (strcmp(interface, "xdg_shell") == 0) {
5090 d->xdg_shell = wl_registry_bind(registry, id,
5091 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005092 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005093 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005094 } else if (strcmp(interface, "text_cursor_position") == 0) {
5095 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005096 wl_registry_bind(registry, id,
5097 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005098 } else if (strcmp(interface, "workspace_manager") == 0) {
5099 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005100 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5101 d->subcompositor =
5102 wl_registry_bind(registry, id,
5103 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005104 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005105
5106 if (d->global_handler)
5107 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005108}
5109
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005110static void
5111registry_handle_global_remove(void *data, struct wl_registry *registry,
5112 uint32_t name)
5113{
5114 struct display *d = data;
5115 struct global *global;
5116 struct global *tmp;
5117
5118 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5119 if (global->name != name)
5120 continue;
5121
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005122 if (strcmp(global->interface, "wl_output") == 0)
5123 display_destroy_output(d, name);
5124
5125 /* XXX: Should destroy remaining bound globals */
5126
5127 if (d->global_handler_remove)
5128 d->global_handler_remove(d, name, global->interface,
5129 global->version, d->user_data);
5130
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005131 wl_list_remove(&global->link);
5132 free(global->interface);
5133 free(global);
5134 }
5135}
5136
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005137void *
5138display_bind(struct display *display, uint32_t name,
5139 const struct wl_interface *interface, uint32_t version)
5140{
5141 return wl_registry_bind(display->registry, name, interface, version);
5142}
5143
5144static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005145 registry_handle_global,
5146 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005147};
5148
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005149#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005150static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005151init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005152{
5153 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005154 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005155
Rob Clark6396ed32012-03-11 19:48:41 -05005156#ifdef USE_CAIRO_GLESV2
5157# define GL_BIT EGL_OPENGL_ES2_BIT
5158#else
5159# define GL_BIT EGL_OPENGL_BIT
5160#endif
5161
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005162 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005163 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005164 EGL_RED_SIZE, 1,
5165 EGL_GREEN_SIZE, 1,
5166 EGL_BLUE_SIZE, 1,
5167 EGL_ALPHA_SIZE, 1,
5168 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005169 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005170 EGL_NONE
5171 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005172
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005173#ifdef USE_CAIRO_GLESV2
5174 static const EGLint context_attribs[] = {
5175 EGL_CONTEXT_CLIENT_VERSION, 2,
5176 EGL_NONE
5177 };
5178 EGLint api = EGL_OPENGL_ES_API;
5179#else
5180 EGLint *context_attribs = NULL;
5181 EGLint api = EGL_OPENGL_API;
5182#endif
5183
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005184 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005185 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005186 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005187 return -1;
5188 }
5189
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005190 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005191 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005192 return -1;
5193 }
5194
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005195 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5196 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005197 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005198 return -1;
5199 }
5200
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005201 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005202 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005203 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005204 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005205 return -1;
5206 }
5207
Benjamin Franzke0c991632011-09-27 21:57:31 +02005208 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5209 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005210 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005211 return -1;
5212 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005213
5214 return 0;
5215}
5216
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005217static void
5218fini_egl(struct display *display)
5219{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005220 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005221
5222 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5223 EGL_NO_CONTEXT);
5224
5225 eglTerminate(display->dpy);
5226 eglReleaseThread();
5227}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005228#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005229
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005230static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005231init_dummy_surface(struct display *display)
5232{
5233 int len;
5234 void *data;
5235
5236 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5237 data = malloc(len);
5238 display->dummy_surface =
5239 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5240 1, 1, len);
5241 display->dummy_surface_data = data;
5242}
5243
5244static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005245handle_display_data(struct task *task, uint32_t events)
5246{
5247 struct display *display =
5248 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005249 struct epoll_event ep;
5250 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005251
5252 display->display_fd_events = events;
5253
5254 if (events & EPOLLERR || events & EPOLLHUP) {
5255 display_exit(display);
5256 return;
5257 }
5258
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005259 if (events & EPOLLIN) {
5260 ret = wl_display_dispatch(display->display);
5261 if (ret == -1) {
5262 display_exit(display);
5263 return;
5264 }
5265 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005266
5267 if (events & EPOLLOUT) {
5268 ret = wl_display_flush(display->display);
5269 if (ret == 0) {
5270 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5271 ep.data.ptr = &display->display_task;
5272 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5273 display->display_fd, &ep);
5274 } else if (ret == -1 && errno != EAGAIN) {
5275 display_exit(display);
5276 return;
5277 }
5278 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005279}
5280
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005281static void
5282log_handler(const char *format, va_list args)
5283{
5284 vfprintf(stderr, format, args);
5285}
5286
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005287struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005288display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005289{
5290 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005291
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005292 wl_log_set_handler_client(log_handler);
5293
Peter Huttererf3d62272013-08-08 11:57:05 +10005294 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005295 if (d == NULL)
5296 return NULL;
5297
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005298 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005299 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005300 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005301 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005302 return NULL;
5303 }
5304
Rob Bradford5ab9c752013-07-26 16:29:43 +01005305 d->xkb_context = xkb_context_new(0);
5306 if (d->xkb_context == NULL) {
5307 fprintf(stderr, "Failed to create XKB context\n");
5308 free(d);
5309 return NULL;
5310 }
5311
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005312 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005313 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005314 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005315 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5316 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005317
5318 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005319 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005320 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005321 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005322
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005323 d->workspace = 0;
5324 d->workspace_count = 1;
5325
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005326 d->registry = wl_display_get_registry(d->display);
5327 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005328
5329 if (wl_display_dispatch(d->display) < 0) {
5330 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5331 return NULL;
5332 }
5333
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005334#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005335 if (init_egl(d) < 0)
5336 fprintf(stderr, "EGL does not seem to work, "
5337 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005338#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005339
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005340 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005341
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005342 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005343
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005344 wl_list_init(&d->window_list);
5345
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005346 init_dummy_surface(d);
5347
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005348 return d;
5349}
5350
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005351static void
5352display_destroy_outputs(struct display *display)
5353{
5354 struct output *tmp;
5355 struct output *output;
5356
5357 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5358 output_destroy(output);
5359}
5360
Pekka Paalanene1207c72011-12-16 12:02:09 +02005361static void
5362display_destroy_inputs(struct display *display)
5363{
5364 struct input *tmp;
5365 struct input *input;
5366
5367 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5368 input_destroy(input);
5369}
5370
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005371void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005372display_destroy(struct display *display)
5373{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005374 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005375 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5376 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005377
5378 if (!wl_list_empty(&display->deferred_list))
5379 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5380
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005381 cairo_surface_destroy(display->dummy_surface);
5382 free(display->dummy_surface_data);
5383
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005384 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005385 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005386
Daniel Stone97f68542012-05-30 16:32:01 +01005387 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005388
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005389 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005390 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005391
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005392#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005393 if (display->argb_device)
5394 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005395#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005396
Pekka Paalanen35e82632013-04-25 13:57:48 +03005397 if (display->subcompositor)
5398 wl_subcompositor_destroy(display->subcompositor);
5399
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005400 if (display->xdg_shell)
5401 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005402
5403 if (display->shm)
5404 wl_shm_destroy(display->shm);
5405
5406 if (display->data_device_manager)
5407 wl_data_device_manager_destroy(display->data_device_manager);
5408
5409 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005410 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005411
5412 close(display->epoll_fd);
5413
U. Artie Eoff44874d92012-10-02 21:12:35 -07005414 if (!(display->display_fd_events & EPOLLERR) &&
5415 !(display->display_fd_events & EPOLLHUP))
5416 wl_display_flush(display->display);
5417
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005418 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005419 free(display);
5420}
5421
5422void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005423display_set_user_data(struct display *display, void *data)
5424{
5425 display->user_data = data;
5426}
5427
5428void *
5429display_get_user_data(struct display *display)
5430{
5431 return display->user_data;
5432}
5433
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005434struct wl_display *
5435display_get_display(struct display *display)
5436{
5437 return display->display;
5438}
5439
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005440int
5441display_has_subcompositor(struct display *display)
5442{
5443 if (display->subcompositor)
5444 return 1;
5445
5446 wl_display_roundtrip(display->display);
5447
5448 return display->subcompositor != NULL;
5449}
5450
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005451cairo_device_t *
5452display_get_cairo_device(struct display *display)
5453{
5454 return display->argb_device;
5455}
5456
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005457struct output *
5458display_get_output(struct display *display)
5459{
5460 return container_of(display->output_list.next, struct output, link);
5461}
5462
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005463struct wl_compositor *
5464display_get_compositor(struct display *display)
5465{
5466 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005467}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005468
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005469uint32_t
5470display_get_serial(struct display *display)
5471{
5472 return display->serial;
5473}
5474
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005475EGLDisplay
5476display_get_egl_display(struct display *d)
5477{
5478 return d->dpy;
5479}
5480
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005481struct wl_data_source *
5482display_create_data_source(struct display *display)
5483{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005484 if (display->data_device_manager)
5485 return wl_data_device_manager_create_data_source(display->data_device_manager);
5486 else
5487 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005488}
5489
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005490EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005491display_get_argb_egl_config(struct display *d)
5492{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005493 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005494}
5495
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005496int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005497display_acquire_window_surface(struct display *display,
5498 struct window *window,
5499 EGLContext ctx)
5500{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005501 struct surface *surface = window->main_surface;
5502
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005503 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005504 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005505
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005506 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005507 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005508}
5509
5510void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005511display_release_window_surface(struct display *display,
5512 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005513{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005514 struct surface *surface = window->main_surface;
5515
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005516 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005517 return;
5518
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005519 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005520}
5521
5522void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005523display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005524{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005525 wl_list_insert(&display->deferred_list, &task->link);
5526}
5527
5528void
5529display_watch_fd(struct display *display,
5530 int fd, uint32_t events, struct task *task)
5531{
5532 struct epoll_event ep;
5533
5534 ep.events = events;
5535 ep.data.ptr = task;
5536 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5537}
5538
5539void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005540display_unwatch_fd(struct display *display, int fd)
5541{
5542 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5543}
5544
5545void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005546display_run(struct display *display)
5547{
5548 struct task *task;
5549 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005550 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005551
Pekka Paalanen826d7952011-12-15 10:14:07 +02005552 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005553 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005554 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005555 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005556 struct task, link);
5557 wl_list_remove(&task->link);
5558 task->run(task, 0);
5559 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005560
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005561 wl_display_dispatch_pending(display->display);
5562
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005563 if (!display->running)
5564 break;
5565
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005566 ret = wl_display_flush(display->display);
5567 if (ret < 0 && errno == EAGAIN) {
5568 ep[0].events =
5569 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5570 ep[0].data.ptr = &display->display_task;
5571
5572 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5573 display->display_fd, &ep[0]);
5574 } else if (ret < 0) {
5575 break;
5576 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005577
5578 count = epoll_wait(display->epoll_fd,
5579 ep, ARRAY_LENGTH(ep), -1);
5580 for (i = 0; i < count; i++) {
5581 task = ep[i].data.ptr;
5582 task->run(task, ep[i].events);
5583 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005584 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005585}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005586
5587void
5588display_exit(struct display *display)
5589{
5590 display->running = 0;
5591}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005592
5593void
5594keysym_modifiers_add(struct wl_array *modifiers_map,
5595 const char *name)
5596{
5597 size_t len = strlen(name) + 1;
5598 char *p;
5599
5600 p = wl_array_add(modifiers_map, len);
5601
5602 if (p == NULL)
5603 return;
5604
5605 strncpy(p, name, len);
5606}
5607
5608static xkb_mod_index_t
5609keysym_modifiers_get_index(struct wl_array *modifiers_map,
5610 const char *name)
5611{
5612 xkb_mod_index_t index = 0;
5613 char *p = modifiers_map->data;
5614
5615 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5616 if (strcmp(p, name) == 0)
5617 return index;
5618
5619 index++;
5620 p += strlen(p) + 1;
5621 }
5622
5623 return XKB_MOD_INVALID;
5624}
5625
5626xkb_mod_mask_t
5627keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5628 const char *name)
5629{
5630 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5631
5632 if (index == XKB_MOD_INVALID)
5633 return XKB_MOD_INVALID;
5634
5635 return 1 << index;
5636}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005637
5638void *
5639fail_on_null(void *p)
5640{
5641 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005642 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005643 exit(EXIT_FAILURE);
5644 }
5645
5646 return p;
5647}
5648
5649void *
5650xmalloc(size_t s)
5651{
5652 return fail_on_null(malloc(s));
5653}
5654
Peter Huttererf3d62272013-08-08 11:57:05 +10005655void *
5656xzalloc(size_t s)
5657{
5658 return fail_on_null(zalloc(s));
5659}
5660
Kristian Høgsbergce278412013-07-25 15:20:20 -07005661char *
5662xstrdup(const char *s)
5663{
5664 return fail_on_null(strdup(s));
5665}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005666
5667void *
5668xrealloc(char *p, size_t s)
5669{
5670 return fail_on_null(realloc(p, s));
5671}