blob: 9ec12c7ef223378a76e1609ec45056805445b223 [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;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400244
Pekka Paalanen4e373742013-02-13 16:17:13 +0200245 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500246 struct xdg_surface *xdg_surface;
247 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300248
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700249 struct window *parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500250
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500251 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500252
Pekka Paalanen35e82632013-04-25 13:57:48 +0300253 /* struct surface::link, contains also main_surface */
254 struct wl_list subsurface_list;
255
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500256 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400257 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500258};
259
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500260struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500261 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200262 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300263 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500264 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400265 struct wl_list link;
266 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500267 widget_resize_handler_t resize_handler;
268 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500269 widget_enter_handler_t enter_handler;
270 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500271 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500272 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700273 widget_touch_down_handler_t touch_down_handler;
274 widget_touch_up_handler_t touch_up_handler;
275 widget_touch_motion_handler_t touch_motion_handler;
276 widget_touch_frame_handler_t touch_frame_handler;
277 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200278 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400279 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500280 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300281 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500282 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000283 /* If this is set to false then no cairo surface will be
284 * created before redrawing the surface. This is useful if the
285 * redraw handler is going to do completely custom rendering
286 * such as using EGL directly */
287 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400288};
289
Rusty Lynch041815a2013-08-08 21:20:38 -0700290struct touch_point {
291 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800292 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700293 struct widget *widget;
294 struct wl_list link;
295};
296
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400297struct input {
298 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100299 struct wl_seat *seat;
300 struct wl_pointer *pointer;
301 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700302 struct wl_touch *touch;
303 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400304 struct window *pointer_focus;
305 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700306 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300307 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300308 uint32_t cursor_anim_start;
309 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300310 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400311 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400312 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400313 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400314 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400315 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400316
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500317 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500318 struct widget *grab;
319 uint32_t grab_button;
320
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400321 struct wl_data_device *data_device;
322 struct data_offer *drag_offer;
323 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800324 uint32_t touch_grab;
325 int32_t touch_grab_id;
326 float drag_x, drag_y;
327 struct window *drag_focus;
328 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100329
330 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100331 struct xkb_keymap *keymap;
332 struct xkb_state *state;
333 xkb_mod_mask_t control_mask;
334 xkb_mod_mask_t alt_mask;
335 xkb_mod_mask_t shift_mask;
336 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400337
338 struct task repeat_task;
339 int repeat_timer_fd;
340 uint32_t repeat_sym;
341 uint32_t repeat_key;
342 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400343};
344
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500345struct output {
346 struct display *display;
347 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800348 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500349 struct rectangle allocation;
350 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600351 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200352 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500353 char *make;
354 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200355
356 display_output_handler_t destroy_handler;
357 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500358};
359
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500360struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500361 struct widget *widget;
362 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500363 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500364};
365
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500366struct menu {
367 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500368 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500369 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500370 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700371 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500372 const char **entries;
373 uint32_t time;
374 int current;
375 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400376 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500377 menu_func_t func;
378};
379
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300380struct tooltip {
381 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300382 struct widget *widget;
383 char *entry;
384 struct task tooltip_task;
385 int tooltip_fd;
386 float x, y;
387};
388
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700389struct shm_pool {
390 struct wl_shm_pool *pool;
391 size_t size;
392 size_t used;
393 void *data;
394};
395
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400396enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300397 CURSOR_DEFAULT = 100,
398 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400399};
400
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200401static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400402
Pekka Paalanen97777442013-05-22 10:20:05 +0300403/* #define DEBUG */
404
405#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300406
407static void
408debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
409__attribute__ ((format (printf, 4, 5)));
410
411static void
412debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
413{
414 va_list ap;
415 struct timeval tv;
416
417 gettimeofday(&tv, NULL);
418 fprintf(stderr, "%8ld.%03ld ",
419 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
420
421 if (proxy)
422 fprintf(stderr, "%s@%d ",
423 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
424
425 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
426 fprintf(stderr, "%s ", func);
427
428 va_start(ap, fmt);
429 vfprintf(stderr, fmt, ap);
430 va_end(ap);
431}
432
433#define DBG(fmt, ...) \
434 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
435
436#define DBG_OBJ(obj, fmt, ...) \
437 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
438
439#else
440
441#define DBG(...) do {} while (0)
442#define DBG_OBJ(...) do {} while (0)
443
444#endif
445
Alexander Larsson1818e312013-05-22 14:41:31 +0200446static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200447surface_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 +0200448{
449 int32_t tmp;
450
451 switch (buffer_transform) {
452 case WL_OUTPUT_TRANSFORM_90:
453 case WL_OUTPUT_TRANSFORM_270:
454 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
455 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
456 tmp = *width;
457 *width = *height;
458 *height = tmp;
459 break;
460 default:
461 break;
462 }
463
464 *width *= buffer_scale;
465 *height *= buffer_scale;
466}
467
468static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200469buffer_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 +0200470{
471 int32_t tmp;
472
473 switch (buffer_transform) {
474 case WL_OUTPUT_TRANSFORM_90:
475 case WL_OUTPUT_TRANSFORM_270:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
477 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
478 tmp = *width;
479 *width = *height;
480 *height = tmp;
481 break;
482 default:
483 break;
484 }
485
486 *width /= buffer_scale;
487 *height /= buffer_scale;
488}
489
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500490#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400491
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200492struct egl_window_surface {
493 struct toysurface base;
494 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100495 struct display *display;
496 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200497 struct wl_egl_window *egl_window;
498 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100499};
500
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200501static struct egl_window_surface *
502to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100503{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200504 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100505}
506
507static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200508egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200509 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200510 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100511{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200512 struct egl_window_surface *surface = to_egl_window_surface(base);
513
Alexander Larsson1818e312013-05-22 14:41:31 +0200514 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
515
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200516 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
517 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
518
519 return cairo_surface_reference(surface->cairo_surface);
520}
521
522static void
523egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200524 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200525 struct rectangle *server_allocation)
526{
527 struct egl_window_surface *surface = to_egl_window_surface(base);
528
529 cairo_gl_surface_swapbuffers(surface->cairo_surface);
530 wl_egl_window_get_attached_size(surface->egl_window,
531 &server_allocation->width,
532 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200533
534 buffer_to_surface_size (buffer_transform, buffer_scale,
535 &server_allocation->width,
536 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200537}
538
539static int
540egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
541{
542 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200543 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100544
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200545 device = cairo_surface_get_device(surface->cairo_surface);
546 if (!device)
547 return -1;
548
549 if (!ctx) {
550 if (device == surface->display->argb_device)
551 ctx = surface->display->argb_ctx;
552 else
553 assert(0);
554 }
555
556 cairo_device_flush(device);
557 cairo_device_acquire(device);
558 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
559 surface->egl_surface, ctx))
560 fprintf(stderr, "failed to make surface current\n");
561
562 return 0;
563}
564
565static void
566egl_window_surface_release(struct toysurface *base)
567{
568 struct egl_window_surface *surface = to_egl_window_surface(base);
569 cairo_device_t *device;
570
571 device = cairo_surface_get_device(surface->cairo_surface);
572 if (!device)
573 return;
574
575 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
576 surface->display->argb_ctx))
577 fprintf(stderr, "failed to make context current\n");
578
579 cairo_device_release(device);
580}
581
582static void
583egl_window_surface_destroy(struct toysurface *base)
584{
585 struct egl_window_surface *surface = to_egl_window_surface(base);
586 struct display *d = surface->display;
587
588 cairo_surface_destroy(surface->cairo_surface);
589 eglDestroySurface(d->dpy, surface->egl_surface);
590 wl_egl_window_destroy(surface->egl_window);
591 surface->surface = NULL;
592
593 free(surface);
594}
595
596static struct toysurface *
597egl_window_surface_create(struct display *display,
598 struct wl_surface *wl_surface,
599 uint32_t flags,
600 struct rectangle *rectangle)
601{
602 struct egl_window_surface *surface;
603
Pekka Paalanenb3627362012-11-19 17:16:00 +0200604 if (display->dpy == EGL_NO_DISPLAY)
605 return NULL;
606
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200607 surface = calloc(1, sizeof *surface);
608 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100609 return NULL;
610
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200611 surface->base.prepare = egl_window_surface_prepare;
612 surface->base.swap = egl_window_surface_swap;
613 surface->base.acquire = egl_window_surface_acquire;
614 surface->base.release = egl_window_surface_release;
615 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100616
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200617 surface->display = display;
618 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400619
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200620 surface->egl_window = wl_egl_window_create(surface->surface,
621 rectangle->width,
622 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200624 surface->egl_surface = eglCreateWindowSurface(display->dpy,
625 display->argb_config,
626 surface->egl_window,
627 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100628
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200629 surface->cairo_surface =
630 cairo_gl_surface_create_for_egl(display->argb_device,
631 surface->egl_surface,
632 rectangle->width,
633 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100634
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200635 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100636}
637
Pekka Paalanenb3627362012-11-19 17:16:00 +0200638#else
639
640static struct toysurface *
641egl_window_surface_create(struct display *display,
642 struct wl_surface *wl_surface,
643 uint32_t flags,
644 struct rectangle *rectangle)
645{
646 return NULL;
647}
648
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400649#endif
650
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200651struct shm_surface_data {
652 struct wl_buffer *buffer;
653 struct shm_pool *pool;
654};
655
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400656struct wl_buffer *
657display_get_buffer_for_surface(struct display *display,
658 cairo_surface_t *surface)
659{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200660 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400661
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200662 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400663
664 return data->buffer;
665}
666
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500667static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700668shm_pool_destroy(struct shm_pool *pool);
669
670static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400671shm_surface_data_destroy(void *p)
672{
673 struct shm_surface_data *data = p;
674
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200675 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700676 if (data->pool)
677 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300678
679 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400680}
681
Kristian Høgsberg16626282012-04-03 11:21:27 -0400682static struct wl_shm_pool *
683make_shm_pool(struct display *display, int size, void **data)
684{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400685 struct wl_shm_pool *pool;
686 int fd;
687
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300688 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400689 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300690 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
691 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400692 return NULL;
693 }
694
695 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400696 if (*data == MAP_FAILED) {
697 fprintf(stderr, "mmap failed: %m\n");
698 close(fd);
699 return NULL;
700 }
701
702 pool = wl_shm_create_pool(display->shm, fd, size);
703
704 close(fd);
705
706 return pool;
707}
708
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700709static struct shm_pool *
710shm_pool_create(struct display *display, size_t size)
711{
712 struct shm_pool *pool = malloc(sizeof *pool);
713
714 if (!pool)
715 return NULL;
716
717 pool->pool = make_shm_pool(display, size, &pool->data);
718 if (!pool->pool) {
719 free(pool);
720 return NULL;
721 }
722
723 pool->size = size;
724 pool->used = 0;
725
726 return pool;
727}
728
729static void *
730shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
731{
732 if (pool->used + size > pool->size)
733 return NULL;
734
735 *offset = pool->used;
736 pool->used += size;
737
738 return (char *) pool->data + *offset;
739}
740
741/* destroy the pool. this does not unmap the memory though */
742static void
743shm_pool_destroy(struct shm_pool *pool)
744{
745 munmap(pool->data, pool->size);
746 wl_shm_pool_destroy(pool->pool);
747 free(pool);
748}
749
750/* Start allocating from the beginning of the pool again */
751static void
752shm_pool_reset(struct shm_pool *pool)
753{
754 pool->used = 0;
755}
756
757static int
758data_length_for_shm_surface(struct rectangle *rect)
759{
760 int stride;
761
762 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
763 rect->width);
764 return stride * rect->height;
765}
766
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500767static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700768display_create_shm_surface_from_pool(struct display *display,
769 struct rectangle *rectangle,
770 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400771{
772 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400773 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400774 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200775 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700776 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400777 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400778
779 data = malloc(sizeof *data);
780 if (data == NULL)
781 return NULL;
782
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200783 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
784 cairo_format = CAIRO_FORMAT_RGB16_565;
785 else
786 cairo_format = CAIRO_FORMAT_ARGB32;
787
788 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700789 length = stride * rectangle->height;
790 data->pool = NULL;
791 map = shm_pool_allocate(pool, length, &offset);
792
793 if (!map) {
794 free(data);
795 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400796 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400798 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200799 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400800 rectangle->width,
801 rectangle->height,
802 stride);
803
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200804 cairo_surface_set_user_data(surface, &shm_surface_data_key,
805 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400806
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200807 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
808 format = WL_SHM_FORMAT_RGB565;
809 else {
810 if (flags & SURFACE_OPAQUE)
811 format = WL_SHM_FORMAT_XRGB8888;
812 else
813 format = WL_SHM_FORMAT_ARGB8888;
814 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400815
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200816 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
817 rectangle->width,
818 rectangle->height,
819 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400820
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700821 return surface;
822}
823
824static cairo_surface_t *
825display_create_shm_surface(struct display *display,
826 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200827 struct shm_pool *alternate_pool,
828 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700829{
830 struct shm_surface_data *data;
831 struct shm_pool *pool;
832 cairo_surface_t *surface;
833
Pekka Paalanen99436862012-11-19 17:15:59 +0200834 if (alternate_pool) {
835 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700836 surface = display_create_shm_surface_from_pool(display,
837 rectangle,
838 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200839 alternate_pool);
840 if (surface) {
841 data = cairo_surface_get_user_data(surface,
842 &shm_surface_data_key);
843 goto out;
844 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700845 }
846
847 pool = shm_pool_create(display,
848 data_length_for_shm_surface(rectangle));
849 if (!pool)
850 return NULL;
851
852 surface =
853 display_create_shm_surface_from_pool(display, rectangle,
854 flags, pool);
855
856 if (!surface) {
857 shm_pool_destroy(pool);
858 return NULL;
859 }
860
861 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200862 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700863 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400864
Pekka Paalanen99436862012-11-19 17:15:59 +0200865out:
866 if (data_ret)
867 *data_ret = data;
868
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400869 return surface;
870}
871
nobled7b87cb02011-02-01 18:51:47 +0000872static int
873check_size(struct rectangle *rect)
874{
875 if (rect->width && rect->height)
876 return 0;
877
878 fprintf(stderr, "tried to create surface of "
879 "width: %d, height: %d\n", rect->width, rect->height);
880 return -1;
881}
882
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883cairo_surface_t *
884display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100885 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400886 struct rectangle *rectangle,
887 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400888{
nobled7b87cb02011-02-01 18:51:47 +0000889 if (check_size(rectangle) < 0)
890 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200891
892 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200893 return display_create_shm_surface(display, rectangle, flags,
894 NULL, NULL);
895}
896
Pekka Paalanena4eda732012-11-19 17:16:02 +0200897struct shm_surface_leaf {
898 cairo_surface_t *cairo_surface;
899 /* 'data' is automatically destroyed, when 'cairo_surface' is */
900 struct shm_surface_data *data;
901
902 struct shm_pool *resize_pool;
903 int busy;
904};
905
906static void
907shm_surface_leaf_release(struct shm_surface_leaf *leaf)
908{
909 if (leaf->cairo_surface)
910 cairo_surface_destroy(leaf->cairo_surface);
911 /* leaf->data already destroyed via cairo private */
912
913 if (leaf->resize_pool)
914 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200915
916 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200917}
918
Pekka Paalanenaef02542013-04-25 13:57:47 +0300919#define MAX_LEAVES 3
920
Pekka Paalanen99436862012-11-19 17:15:59 +0200921struct shm_surface {
922 struct toysurface base;
923 struct display *display;
924 struct wl_surface *surface;
925 uint32_t flags;
926 int dx, dy;
927
Pekka Paalanenaef02542013-04-25 13:57:47 +0300928 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200929 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200930};
931
932static struct shm_surface *
933to_shm_surface(struct toysurface *base)
934{
935 return container_of(base, struct shm_surface, base);
936}
937
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300939shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
940{
941#ifdef DEBUG
942 struct shm_surface_leaf *leaf;
943 char bufs[MAX_LEAVES + 1];
944 int i;
945
946 for (i = 0; i < MAX_LEAVES; i++) {
947 leaf = &surface->leaf[i];
948
949 if (leaf->busy)
950 bufs[i] = 'b';
951 else if (leaf->cairo_surface)
952 bufs[i] = 'a';
953 else
954 bufs[i] = ' ';
955 }
956
957 bufs[MAX_LEAVES] = '\0';
958 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
959#endif
960}
961
962static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200963shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
964{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200965 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300966 struct shm_surface_leaf *leaf;
967 int i;
968 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300969
970 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200971
Pekka Paalanenaef02542013-04-25 13:57:47 +0300972 for (i = 0; i < MAX_LEAVES; i++) {
973 leaf = &surface->leaf[i];
974 if (leaf->data && leaf->data->buffer == buffer) {
975 leaf->busy = 0;
976 break;
977 }
978 }
979 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200980
Pekka Paalanenaef02542013-04-25 13:57:47 +0300981 /* Leave one free leaf with storage, release others */
982 free_found = 0;
983 for (i = 0; i < MAX_LEAVES; i++) {
984 leaf = &surface->leaf[i];
985
986 if (!leaf->cairo_surface || leaf->busy)
987 continue;
988
989 if (!free_found)
990 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300991 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300992 shm_surface_leaf_release(leaf);
993 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300994
Pekka Paalanen97777442013-05-22 10:20:05 +0300995 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200996}
997
998static const struct wl_buffer_listener shm_surface_buffer_listener = {
999 shm_surface_buffer_release
1000};
1001
Pekka Paalanen99436862012-11-19 17:15:59 +02001002static cairo_surface_t *
1003shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001004 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001005 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001006{
Pekka Paalanenec076692012-11-30 13:37:27 +02001007 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001008 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001009 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001010 struct shm_surface_leaf *leaf = NULL;
1011 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001012
1013 surface->dx = dx;
1014 surface->dy = dy;
1015
Pekka Paalanenaef02542013-04-25 13:57:47 +03001016 /* pick a free buffer, preferrably one that already has storage */
1017 for (i = 0; i < MAX_LEAVES; i++) {
1018 if (surface->leaf[i].busy)
1019 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001020
Pekka Paalanenaef02542013-04-25 13:57:47 +03001021 if (!leaf || surface->leaf[i].cairo_surface)
1022 leaf = &surface->leaf[i];
1023 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001024 DBG_OBJ(surface->surface, "pick leaf %d\n",
1025 (int)(leaf - &surface->leaf[0]));
1026
Pekka Paalanenaef02542013-04-25 13:57:47 +03001027 if (!leaf) {
1028 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001029 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001030 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001031 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001032 }
1033
Pekka Paalanena4eda732012-11-19 17:16:02 +02001034 if (!resize_hint && leaf->resize_pool) {
1035 cairo_surface_destroy(leaf->cairo_surface);
1036 leaf->cairo_surface = NULL;
1037 shm_pool_destroy(leaf->resize_pool);
1038 leaf->resize_pool = NULL;
1039 }
1040
Alexander Larsson1818e312013-05-22 14:41:31 +02001041 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1042
Pekka Paalanena4eda732012-11-19 17:16:02 +02001043 if (leaf->cairo_surface &&
1044 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1045 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001046 goto out;
1047
Pekka Paalanena4eda732012-11-19 17:16:02 +02001048 if (leaf->cairo_surface)
1049 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001050
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001051#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001052 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001053 /* Create a big pool to allocate from, while continuously
1054 * resizing. Mmapping a new pool in the server
1055 * is relatively expensive, so reusing a pool performs
1056 * better, but may temporarily reserve unneeded memory.
1057 */
1058 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001059 leaf->resize_pool = shm_pool_create(surface->display,
1060 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001061 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001062#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001063
Alexander Larsson1818e312013-05-22 14:41:31 +02001064 rect.width = width;
1065 rect.height = height;
1066
Pekka Paalanena4eda732012-11-19 17:16:02 +02001067 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001068 display_create_shm_surface(surface->display, &rect,
1069 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001070 leaf->resize_pool,
1071 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001072 if (!leaf->cairo_surface)
1073 return NULL;
1074
Pekka Paalanena4eda732012-11-19 17:16:02 +02001075 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001076 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001077
1078out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 surface->current = leaf;
1080
1081 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001082}
1083
1084static void
1085shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001086 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001087 struct rectangle *server_allocation)
1088{
1089 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001090 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001091
1092 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001094 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001095 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001096
Alexander Larsson1818e312013-05-22 14:41:31 +02001097 buffer_to_surface_size (buffer_transform, buffer_scale,
1098 &server_allocation->width,
1099 &server_allocation->height);
1100
Pekka Paalanena4eda732012-11-19 17:16:02 +02001101 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001102 surface->dx, surface->dy);
1103 wl_surface_damage(surface->surface, 0, 0,
1104 server_allocation->width, server_allocation->height);
1105 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001106
Pekka Paalanen71233882013-04-25 13:57:53 +03001107 DBG_OBJ(surface->surface, "leaf %d busy\n",
1108 (int)(leaf - &surface->leaf[0]));
1109
Pekka Paalanena4eda732012-11-19 17:16:02 +02001110 leaf->busy = 1;
1111 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001112}
1113
1114static int
1115shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1116{
1117 return -1;
1118}
1119
1120static void
1121shm_surface_release(struct toysurface *base)
1122{
1123}
1124
1125static void
1126shm_surface_destroy(struct toysurface *base)
1127{
1128 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001129 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001130
Pekka Paalanenaef02542013-04-25 13:57:47 +03001131 for (i = 0; i < MAX_LEAVES; i++)
1132 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001133
1134 free(surface);
1135}
1136
1137static struct toysurface *
1138shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1139 uint32_t flags, struct rectangle *rectangle)
1140{
1141 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001142 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001143
Brian Lovinbc919262013-08-07 15:34:59 -07001144 surface = xmalloc(sizeof *surface);
1145 memset(surface, 0, sizeof *surface);
1146
Pekka Paalanen99436862012-11-19 17:15:59 +02001147 if (!surface)
1148 return NULL;
1149
1150 surface->base.prepare = shm_surface_prepare;
1151 surface->base.swap = shm_surface_swap;
1152 surface->base.acquire = shm_surface_acquire;
1153 surface->base.release = shm_surface_release;
1154 surface->base.destroy = shm_surface_destroy;
1155
1156 surface->display = display;
1157 surface->surface = wl_surface;
1158 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001159
1160 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001161}
1162
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001163/*
1164 * The following correspondences between file names and cursors was copied
1165 * from: https://bugs.kde.org/attachment.cgi?id=67313
1166 */
1167
1168static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001169 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001170 "sw-resize",
1171 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001172};
1173
1174static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001175 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001176 "se-resize",
1177 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001178};
1179
1180static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001181 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001182 "s-resize",
1183 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001184};
1185
1186static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001187 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001188 "closedhand",
1189 "208530c400c041818281048008011002"
1190};
1191
1192static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001193 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001194 "default",
1195 "top_left_arrow",
1196 "left-arrow"
1197};
1198
1199static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001200 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001201 "w-resize",
1202 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001203};
1204
1205static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001206 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001207 "e-resize",
1208 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001209};
1210
1211static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001212 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001213 "nw-resize",
1214 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001215};
1216
1217static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001218 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001219 "ne-resize",
1220 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001221};
1222
1223static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001224 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001225 "n-resize",
1226 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001227};
1228
1229static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001230 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001231 "ibeam",
1232 "text"
1233};
1234
1235static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001236 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001237 "pointer",
1238 "pointing_hand",
1239 "e29285e634086352946a0e7090d73106"
1240};
1241
1242static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001243 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001244 "wait",
1245 "0426c94ea35c87780ff01dc239897213"
1246};
1247
1248struct cursor_alternatives {
1249 const char **names;
1250 size_t count;
1251};
1252
1253static const struct cursor_alternatives cursors[] = {
1254 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1255 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1256 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1257 {grabbings, ARRAY_LENGTH(grabbings)},
1258 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1259 {left_sides, ARRAY_LENGTH(left_sides)},
1260 {right_sides, ARRAY_LENGTH(right_sides)},
1261 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1262 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1263 {top_sides, ARRAY_LENGTH(top_sides)},
1264 {xterms, ARRAY_LENGTH(xterms)},
1265 {hand1s, ARRAY_LENGTH(hand1s)},
1266 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001267};
1268
1269static void
1270create_cursors(struct display *display)
1271{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001272 struct weston_config *config;
1273 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001274 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001275 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001276 unsigned int i, j;
1277 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001278
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001279 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001280 s = weston_config_get_section(config, "shell", NULL, NULL);
1281 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1282 weston_config_section_get_int(s, "cursor-size", &size, 32);
1283 weston_config_destroy(config);
1284
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001285 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001286 if (!display->cursor_theme) {
1287 fprintf(stderr, "could not load theme '%s'\n", theme);
1288 return;
1289 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001290 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001291 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001292 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001293
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001294 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001295 cursor = NULL;
1296 for (j = 0; !cursor && j < cursors[i].count; ++j)
1297 cursor = wl_cursor_theme_get_cursor(
1298 display->cursor_theme, cursors[i].names[j]);
1299
1300 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001301 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001302 cursors[i].names[0]);
1303
1304 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001305 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001306}
1307
1308static void
1309destroy_cursors(struct display *display)
1310{
1311 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001312 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001313}
1314
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001315struct wl_cursor_image *
1316display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001317{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001318 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001319
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001320 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001321}
1322
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001323static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001324surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001325{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001326 if (!surface->cairo_surface)
1327 return;
1328
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001329 if (surface->opaque_region) {
1330 wl_surface_set_opaque_region(surface->surface,
1331 surface->opaque_region);
1332 wl_region_destroy(surface->opaque_region);
1333 surface->opaque_region = NULL;
1334 }
1335
1336 if (surface->input_region) {
1337 wl_surface_set_input_region(surface->surface,
1338 surface->input_region);
1339 wl_region_destroy(surface->input_region);
1340 surface->input_region = NULL;
1341 }
1342
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07001343 if (surface->window->next_attach_serial > 0) {
1344 xdg_surface_ack_configure(surface->window->xdg_surface,
1345 surface->window->next_attach_serial);
1346 surface->window->next_attach_serial = 0;
1347 }
1348
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001349 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001350 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001351 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352
Pekka Paalanen89dee002013-02-13 16:17:20 +02001353 cairo_surface_destroy(surface->cairo_surface);
1354 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001355}
1356
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001357int
1358window_has_focus(struct window *window)
1359{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001360 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001361}
1362
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001363static void
1364window_close(struct window *window)
1365{
1366 if (window->close_handler)
1367 window->close_handler(window->user_data);
1368 else
1369 display_exit(window->display);
1370}
1371
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001372struct display *
1373window_get_display(struct window *window)
1374{
1375 return window->display;
1376}
1377
Pekka Paalanen89dee002013-02-13 16:17:20 +02001378static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001379surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001380{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001381 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001382 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001383
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001384 if (!surface->toysurface && display->dpy &&
1385 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001386 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001387 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001388 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001389 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001390 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001391 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001392
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001393 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001394 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001395 surface->surface,
1396 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001397
Pekka Paalanen89dee002013-02-13 16:17:20 +02001398 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001399 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001400 allocation.width, allocation.height, flags,
1401 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001402}
1403
1404static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001405window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001406{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001407 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001408 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409
Pekka Paalanenec076692012-11-30 13:37:27 +02001410 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001412
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001413 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1414 flags |= SURFACE_HINT_RGB565;
1415
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001416 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001417}
1418
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001419int
1420window_get_buffer_transform(struct window *window)
1421{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001422 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001423}
1424
1425void
1426window_set_buffer_transform(struct window *window,
1427 enum wl_output_transform transform)
1428{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001429 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001430 wl_surface_set_buffer_transform(window->main_surface->surface,
1431 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001432}
1433
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001434void
1435window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001436 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001437{
1438 window->main_surface->buffer_scale = scale;
1439 wl_surface_set_buffer_scale(window->main_surface->surface,
1440 scale);
1441}
1442
1443uint32_t
1444window_get_buffer_scale(struct window *window)
1445{
1446 return window->main_surface->buffer_scale;
1447}
1448
Alexander Larssond68f5232013-05-22 14:41:33 +02001449uint32_t
1450window_get_output_scale(struct window *window)
1451{
1452 struct window_output *window_output;
1453 struct window_output *window_output_tmp;
1454 int scale = 1;
1455
1456 wl_list_for_each_safe(window_output, window_output_tmp,
1457 &window->window_output_list, link) {
1458 if (window_output->output->scale > scale)
1459 scale = window_output->output->scale;
1460 }
1461
1462 return scale;
1463}
1464
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001465static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001466
Pekka Paalanen4e373742013-02-13 16:17:13 +02001467static void
1468surface_destroy(struct surface *surface)
1469{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001470 if (surface->frame_cb)
1471 wl_callback_destroy(surface->frame_cb);
1472
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001473 if (surface->input_region)
1474 wl_region_destroy(surface->input_region);
1475
1476 if (surface->opaque_region)
1477 wl_region_destroy(surface->opaque_region);
1478
Pekka Paalanen35e82632013-04-25 13:57:48 +03001479 if (surface->subsurface)
1480 wl_subsurface_destroy(surface->subsurface);
1481
Pekka Paalanen4e373742013-02-13 16:17:13 +02001482 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001483
1484 if (surface->toysurface)
1485 surface->toysurface->destroy(surface->toysurface);
1486
Pekka Paalanen35e82632013-04-25 13:57:48 +03001487 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001488 free(surface);
1489}
1490
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001491void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001492window_destroy(struct window *window)
1493{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001494 struct display *display = window->display;
1495 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001496 struct window_output *window_output;
1497 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001498
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001499 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001500
Rusty Lynch1084da52013-08-15 09:10:08 -07001501 wl_list_for_each(input, &display->input_list, link) {
1502 if (input->touch_focus == window)
1503 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001504 if (input->pointer_focus == window)
1505 input->pointer_focus = NULL;
1506 if (input->keyboard_focus == window)
1507 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001508 if (input->focus_widget &&
1509 input->focus_widget->window == window)
1510 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001511 }
1512
Rob Bradford7507b572012-05-15 17:55:34 +01001513 wl_list_for_each_safe(window_output, window_output_tmp,
1514 &window->window_output_list, link) {
1515 free (window_output);
1516 }
1517
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001518 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001519 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001520
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001521 if (window->xdg_surface)
1522 xdg_surface_destroy(window->xdg_surface);
1523 if (window->xdg_popup)
1524 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001525
1526 surface_destroy(window->main_surface);
1527
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001528 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001529
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001530 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001531 free(window);
1532}
1533
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001534static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001535widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001536{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001537 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001538
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001539 wl_list_for_each(child, &widget->child_list, link) {
1540 target = widget_find_widget(child, x, y);
1541 if (target)
1542 return target;
1543 }
1544
1545 if (widget->allocation.x <= x &&
1546 x < widget->allocation.x + widget->allocation.width &&
1547 widget->allocation.y <= y &&
1548 y < widget->allocation.y + widget->allocation.height) {
1549 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001550 }
1551
1552 return NULL;
1553}
1554
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001555static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001556window_find_widget(struct window *window, int32_t x, int32_t y)
1557{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001558 struct surface *surface;
1559 struct widget *widget;
1560
1561 wl_list_for_each(surface, &window->subsurface_list, link) {
1562 widget = widget_find_widget(surface->widget, x, y);
1563 if (widget)
1564 return widget;
1565 }
1566
1567 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001568}
1569
1570static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001571widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001572{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001573 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001574
Peter Huttererf3d62272013-08-08 11:57:05 +10001575 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001576 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001577 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001578 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001579 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001580 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001581 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001582 widget->tooltip = NULL;
1583 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001584 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001585 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001586
1587 return widget;
1588}
1589
1590struct widget *
1591window_add_widget(struct window *window, void *data)
1592{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001593 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001594
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001595 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001596 wl_list_init(&widget->link);
1597 window->main_surface->widget = widget;
1598
1599 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001600}
1601
1602struct widget *
1603widget_add_widget(struct widget *parent, void *data)
1604{
1605 struct widget *widget;
1606
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001607 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001608 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001609
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001610 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001611}
1612
1613void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001614widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001615{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001616 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001617 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001618 struct input *input;
1619
Pekka Paalanen35e82632013-04-25 13:57:48 +03001620 /* Destroy the sub-surface along with the root widget */
1621 if (surface->widget == widget && surface->subsurface)
1622 surface_destroy(widget->surface);
1623
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001624 if (widget->tooltip)
1625 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001626
Pekka Paalanene156fb62012-01-19 13:51:38 +02001627 wl_list_for_each(input, &display->input_list, link) {
1628 if (input->focus_widget == widget)
1629 input->focus_widget = NULL;
1630 }
1631
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001632 wl_list_remove(&widget->link);
1633 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001634}
1635
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001636void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001637widget_set_default_cursor(struct widget *widget, int cursor)
1638{
1639 widget->default_cursor = cursor;
1640}
1641
1642void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001643widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001644{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001645 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001646}
1647
1648void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001649widget_set_size(struct widget *widget, int32_t width, int32_t height)
1650{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001651 widget->allocation.width = width;
1652 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001653}
1654
1655void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001656widget_set_allocation(struct widget *widget,
1657 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001658{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001659 widget->allocation.x = x;
1660 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001661 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662}
1663
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001664void
1665widget_set_transparent(struct widget *widget, int transparent)
1666{
1667 widget->opaque = !transparent;
1668}
1669
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001670void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001671widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001672{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001673 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001674}
1675
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001676static cairo_surface_t *
1677widget_get_cairo_surface(struct widget *widget)
1678{
1679 struct surface *surface = widget->surface;
1680 struct window *window = widget->window;
1681
Neil Roberts97b747c2013-12-19 16:17:12 +00001682 assert(widget->use_cairo);
1683
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001684 if (!surface->cairo_surface) {
1685 if (surface == window->main_surface)
1686 window_create_main_surface(window);
1687 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001688 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001689 }
1690
1691 return surface->cairo_surface;
1692}
1693
Alexander Larsson15901f02013-05-22 14:41:25 +02001694static void
1695widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1696{
1697 struct surface *surface = widget->surface;
1698 double angle;
1699 cairo_matrix_t m;
1700 enum wl_output_transform transform;
1701 int surface_width, surface_height;
1702 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001703 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001704
1705 surface_width = surface->allocation.width;
1706 surface_height = surface->allocation.height;
1707
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001708 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001709 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001710
Alexander Larsson15901f02013-05-22 14:41:25 +02001711 switch (transform) {
1712 case WL_OUTPUT_TRANSFORM_FLIPPED:
1713 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1714 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1715 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1716 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1717 break;
1718 default:
1719 cairo_matrix_init_identity(&m);
1720 break;
1721 }
1722
1723 switch (transform) {
1724 case WL_OUTPUT_TRANSFORM_NORMAL:
1725 default:
1726 angle = 0;
1727 translate_x = 0;
1728 translate_y = 0;
1729 break;
1730 case WL_OUTPUT_TRANSFORM_FLIPPED:
1731 angle = 0;
1732 translate_x = surface_width;
1733 translate_y = 0;
1734 break;
1735 case WL_OUTPUT_TRANSFORM_90:
1736 angle = M_PI_2;
1737 translate_x = surface_height;
1738 translate_y = 0;
1739 break;
1740 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1741 angle = M_PI_2;
1742 translate_x = surface_height;
1743 translate_y = surface_width;
1744 break;
1745 case WL_OUTPUT_TRANSFORM_180:
1746 angle = M_PI;
1747 translate_x = surface_width;
1748 translate_y = surface_height;
1749 break;
1750 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1751 angle = M_PI;
1752 translate_x = 0;
1753 translate_y = surface_height;
1754 break;
1755 case WL_OUTPUT_TRANSFORM_270:
1756 angle = M_PI + M_PI_2;
1757 translate_x = 0;
1758 translate_y = surface_width;
1759 break;
1760 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1761 angle = M_PI + M_PI_2;
1762 translate_x = 0;
1763 translate_y = 0;
1764 break;
1765 }
1766
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001767 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001768 cairo_translate(cr, translate_x, translate_y);
1769 cairo_rotate(cr, angle);
1770 cairo_transform(cr, &m);
1771}
1772
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001773cairo_t *
1774widget_cairo_create(struct widget *widget)
1775{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001776 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001777 cairo_surface_t *cairo_surface;
1778 cairo_t *cr;
1779
1780 cairo_surface = widget_get_cairo_surface(widget);
1781 cr = cairo_create(cairo_surface);
1782
Alexander Larsson15901f02013-05-22 14:41:25 +02001783 widget_cairo_update_transform(widget, cr);
1784
Pekka Paalanen35e82632013-04-25 13:57:48 +03001785 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1786
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001787 return cr;
1788}
1789
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001790struct wl_surface *
1791widget_get_wl_surface(struct widget *widget)
1792{
1793 return widget->surface->surface;
1794}
1795
Neil Roberts5e10a042013-09-09 00:40:17 +01001796struct wl_subsurface *
1797widget_get_wl_subsurface(struct widget *widget)
1798{
1799 return widget->surface->subsurface;
1800}
1801
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001802uint32_t
1803widget_get_last_time(struct widget *widget)
1804{
1805 return widget->surface->last_time;
1806}
1807
1808void
1809widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1810{
1811 struct wl_compositor *comp = widget->window->display->compositor;
1812 struct surface *surface = widget->surface;
1813
1814 if (!surface->input_region)
1815 surface->input_region = wl_compositor_create_region(comp);
1816
1817 if (rect) {
1818 wl_region_add(surface->input_region,
1819 rect->x, rect->y, rect->width, rect->height);
1820 }
1821}
1822
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001823void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001824widget_set_resize_handler(struct widget *widget,
1825 widget_resize_handler_t handler)
1826{
1827 widget->resize_handler = handler;
1828}
1829
1830void
1831widget_set_redraw_handler(struct widget *widget,
1832 widget_redraw_handler_t handler)
1833{
1834 widget->redraw_handler = handler;
1835}
1836
1837void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001838widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001839{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001840 widget->enter_handler = handler;
1841}
1842
1843void
1844widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1845{
1846 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001847}
1848
1849void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001850widget_set_motion_handler(struct widget *widget,
1851 widget_motion_handler_t handler)
1852{
1853 widget->motion_handler = handler;
1854}
1855
1856void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001857widget_set_button_handler(struct widget *widget,
1858 widget_button_handler_t handler)
1859{
1860 widget->button_handler = handler;
1861}
1862
1863void
Rusty Lynch041815a2013-08-08 21:20:38 -07001864widget_set_touch_up_handler(struct widget *widget,
1865 widget_touch_up_handler_t handler)
1866{
1867 widget->touch_up_handler = handler;
1868}
1869
1870void
1871widget_set_touch_down_handler(struct widget *widget,
1872 widget_touch_down_handler_t handler)
1873{
1874 widget->touch_down_handler = handler;
1875}
1876
1877void
1878widget_set_touch_motion_handler(struct widget *widget,
1879 widget_touch_motion_handler_t handler)
1880{
1881 widget->touch_motion_handler = handler;
1882}
1883
1884void
1885widget_set_touch_frame_handler(struct widget *widget,
1886 widget_touch_frame_handler_t handler)
1887{
1888 widget->touch_frame_handler = handler;
1889}
1890
1891void
1892widget_set_touch_cancel_handler(struct widget *widget,
1893 widget_touch_cancel_handler_t handler)
1894{
1895 widget->touch_cancel_handler = handler;
1896}
1897
1898void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001899widget_set_axis_handler(struct widget *widget,
1900 widget_axis_handler_t handler)
1901{
1902 widget->axis_handler = handler;
1903}
1904
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001905static void
1906window_schedule_redraw_task(struct window *window);
1907
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001908void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001909widget_schedule_redraw(struct widget *widget)
1910{
Pekka Paalanen71233882013-04-25 13:57:53 +03001911 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001912 widget->surface->redraw_needed = 1;
1913 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001914}
1915
Neil Roberts97b747c2013-12-19 16:17:12 +00001916void
1917widget_set_use_cairo(struct widget *widget,
1918 int use_cairo)
1919{
1920 widget->use_cairo = use_cairo;
1921}
1922
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001923cairo_surface_t *
1924window_get_surface(struct window *window)
1925{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001926 cairo_surface_t *cairo_surface;
1927
1928 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1929
1930 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001931}
1932
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001933struct wl_surface *
1934window_get_wl_surface(struct window *window)
1935{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001936 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001937}
1938
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001939static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001940tooltip_redraw_handler(struct widget *widget, void *data)
1941{
1942 cairo_t *cr;
1943 const int32_t r = 3;
1944 struct tooltip *tooltip = data;
1945 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001946
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001947 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001948 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001949 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1950 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1951 cairo_paint(cr);
1952
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001953 width = widget->allocation.width;
1954 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001955 rounded_rect(cr, 0, 0, width, height, r);
1956
1957 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1958 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1959 cairo_fill(cr);
1960
1961 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1962 cairo_move_to(cr, 10, 16);
1963 cairo_show_text(cr, tooltip->entry);
1964 cairo_destroy(cr);
1965}
1966
1967static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001968get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001969{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001970 cairo_t *cr;
1971 cairo_text_extents_t extents;
1972
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001973 /* Use the dummy_surface because tooltip's surface was not
1974 * created yet, and parent does not have a valid surface
1975 * outside repaint, either.
1976 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001977 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001978 cairo_text_extents(cr, tooltip->entry, &extents);
1979 cairo_destroy(cr);
1980
1981 return extents;
1982}
1983
1984static int
1985window_create_tooltip(struct tooltip *tooltip)
1986{
1987 struct widget *parent = tooltip->parent;
1988 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001989 const int offset_y = 27;
1990 const int margin = 3;
1991 cairo_text_extents_t extents;
1992
1993 if (tooltip->widget)
1994 return 0;
1995
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001996 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001997
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001998 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001999 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002000 widget_set_allocation(tooltip->widget,
2001 tooltip->x, tooltip->y + offset_y,
2002 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002003
2004 return 0;
2005}
2006
2007void
2008widget_destroy_tooltip(struct widget *parent)
2009{
2010 struct tooltip *tooltip = parent->tooltip;
2011
2012 parent->tooltip_count = 0;
2013 if (!tooltip)
2014 return;
2015
2016 if (tooltip->widget) {
2017 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002018 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 }
2020
2021 close(tooltip->tooltip_fd);
2022 free(tooltip->entry);
2023 free(tooltip);
2024 parent->tooltip = NULL;
2025}
2026
2027static void
2028tooltip_func(struct task *task, uint32_t events)
2029{
2030 struct tooltip *tooltip =
2031 container_of(task, struct tooltip, tooltip_task);
2032 uint64_t exp;
2033
Martin Olsson8df662a2012-07-08 03:03:47 +02002034 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2035 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002036 window_create_tooltip(tooltip);
2037}
2038
2039#define TOOLTIP_TIMEOUT 500
2040static int
2041tooltip_timer_reset(struct tooltip *tooltip)
2042{
2043 struct itimerspec its;
2044
2045 its.it_interval.tv_sec = 0;
2046 its.it_interval.tv_nsec = 0;
2047 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2048 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2049 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2050 fprintf(stderr, "could not set timerfd\n: %m");
2051 return -1;
2052 }
2053
2054 return 0;
2055}
2056
2057int
2058widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2059{
2060 struct tooltip *tooltip = parent->tooltip;
2061
2062 parent->tooltip_count++;
2063 if (tooltip) {
2064 tooltip->x = x;
2065 tooltip->y = y;
2066 tooltip_timer_reset(tooltip);
2067 return 0;
2068 }
2069
2070 /* the handler might be triggered too fast via input device motion, so
2071 * we need this check here to make sure tooltip is fully initialized */
2072 if (parent->tooltip_count > 1)
2073 return 0;
2074
2075 tooltip = malloc(sizeof *tooltip);
2076 if (!tooltip)
2077 return -1;
2078
2079 parent->tooltip = tooltip;
2080 tooltip->parent = parent;
2081 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002082 tooltip->x = x;
2083 tooltip->y = y;
2084 tooltip->entry = strdup(entry);
2085 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2086 if (tooltip->tooltip_fd < 0) {
2087 fprintf(stderr, "could not create timerfd\n: %m");
2088 return -1;
2089 }
2090
2091 tooltip->tooltip_task.run = tooltip_func;
2092 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2093 EPOLLIN, &tooltip->tooltip_task);
2094 tooltip_timer_reset(tooltip);
2095
2096 return 0;
2097}
2098
2099static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002100workspace_manager_state(void *data,
2101 struct workspace_manager *workspace_manager,
2102 uint32_t current,
2103 uint32_t count)
2104{
2105 struct display *display = data;
2106
2107 display->workspace = current;
2108 display->workspace_count = count;
2109}
2110
2111static const struct workspace_manager_listener workspace_manager_listener = {
2112 workspace_manager_state
2113};
2114
2115static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002116frame_resize_handler(struct widget *widget,
2117 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002118{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002119 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002121 struct rectangle interior;
2122 struct rectangle input;
2123 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002124
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002125 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002126 interior.x = 0;
2127 interior.y = 0;
2128 interior.width = width;
2129 interior.height = height;
2130 } else {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002131 if (widget->window->maximized) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002132 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2133 } else {
2134 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2135 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002136
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002137 frame_resize(frame->frame, width, height);
2138 frame_interior(frame->frame, &interior.x, &interior.y,
2139 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002140 }
2141
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002142 widget_set_allocation(child, interior.x, interior.y,
2143 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002144
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002145 if (child->resize_handler) {
2146 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002147 child->user_data);
2148
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002149 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002150 width = child->allocation.width;
2151 height = child->allocation.height;
2152 } else {
2153 frame_resize_inside(frame->frame,
2154 child->allocation.width,
2155 child->allocation.height);
2156 width = frame_width(frame->frame);
2157 height = frame_height(frame->frame);
2158 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002159 }
2160
Scott Moreauf7e498c2012-05-14 11:39:29 -06002161 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002162
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002163 widget->surface->input_region =
2164 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002165 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002166 frame_input_rect(frame->frame, &input.x, &input.y,
2167 &input.width, &input.height);
2168 wl_region_add(widget->surface->input_region,
2169 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002170 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002171 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002172 }
2173
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002174 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002175
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002176 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002177 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002178 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2179 &opaque.width, &opaque.height);
2180
2181 wl_region_add(widget->surface->opaque_region,
2182 opaque.x, opaque.y,
2183 opaque.width, opaque.height);
2184 } else {
2185 wl_region_add(widget->surface->opaque_region,
2186 0, 0, width, height);
2187 }
Martin Minarik1998b152012-05-10 02:04:35 +02002188 }
2189
Martin Minarik1998b152012-05-10 02:04:35 +02002190
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002191 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192}
2193
2194static void
2195frame_redraw_handler(struct widget *widget, void *data)
2196{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002199 struct window *window = widget->window;
2200
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002201 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002202 return;
2203
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002204 if (window->focused) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002205 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2206 } else {
2207 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2208 }
2209
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002210 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002211
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002212 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002213
2214 cairo_destroy(cr);
2215}
2216
2217static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002218frame_get_pointer_image_for_location(struct window_frame *frame,
2219 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002220{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002221 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002222
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002223 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002224 return CURSOR_LEFT_PTR;
2225
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002226 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002227 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002228 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002229 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002230 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002231 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002232 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002233 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002234 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002235 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002236 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002237 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002238 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002239 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002240 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002241 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002242 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002243 case THEME_LOCATION_EXTERIOR:
2244 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002245 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002246 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002247 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002248}
2249
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002250static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002251frame_menu_func(struct window *window,
2252 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002253{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002254 struct display *display;
2255
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002256 switch (index) {
2257 case 0: /* close */
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002258 window_close(window);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002259 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002260 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002261 display = window->display;
2262 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002263 workspace_manager_move_surface(
2264 display->workspace_manager,
2265 window->main_surface->surface,
2266 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002267 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002268 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002269 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002270 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002271 workspace_manager_move_surface(
2272 display->workspace_manager,
2273 window->main_surface->surface,
2274 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002275 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002276 case 3: /* fullscreen */
2277 /* we don't have a way to get out of fullscreen for now */
2278 if (window->fullscreen_handler)
2279 window->fullscreen_handler(window, window->user_data);
2280 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002281 }
2282}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002283
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002284void
2285window_show_frame_menu(struct window *window,
2286 struct input *input, uint32_t time)
2287{
2288 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002289 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002290
2291 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002292 "Close",
2293 "Move to workspace above", "Move to workspace below",
2294 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002295 };
2296
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002297 if (window->fullscreen_handler)
2298 count = ARRAY_LENGTH(entries);
2299 else
2300 count = ARRAY_LENGTH(entries) - 1;
2301
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002302 input_get_position(input, &x, &y);
2303 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002304 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002305}
2306
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002307static int
2308frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002309 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002310{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002311 struct window_frame *frame = data;
2312 enum theme_location location;
2313
2314 location = frame_pointer_enter(frame->frame, input, x, y);
2315 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2316 widget_schedule_redraw(frame->widget);
2317
2318 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002319}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002320
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002321static int
2322frame_motion_handler(struct widget *widget,
2323 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002324 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002325{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002326 struct window_frame *frame = data;
2327 enum theme_location location;
2328
2329 location = frame_pointer_motion(frame->frame, input, x, y);
2330 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2331 widget_schedule_redraw(frame->widget);
2332
2333 return frame_get_pointer_image_for_location(data, location);
2334}
2335
2336static void
2337frame_leave_handler(struct widget *widget,
2338 struct input *input, void *data)
2339{
2340 struct window_frame *frame = data;
2341
2342 frame_pointer_leave(frame->frame, input);
2343 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2344 widget_schedule_redraw(frame->widget);
2345}
2346
2347static void
2348frame_handle_status(struct window_frame *frame, struct input *input,
2349 uint32_t time, enum theme_location location)
2350{
2351 struct window *window = frame->widget->window;
2352 uint32_t status;
2353
2354 status = frame_status(frame->frame);
2355 if (status & FRAME_STATUS_REPAINT)
2356 widget_schedule_redraw(frame->widget);
2357
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002358 if (status & FRAME_STATUS_MINIMIZE) {
2359 window_set_minimized(window);
2360 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2361 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002362
2363 if (status & FRAME_STATUS_MENU) {
2364 window_show_frame_menu(window, input, time);
2365 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2366 }
2367
2368 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002369 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002370 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2371 }
2372
2373 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002374 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002375 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002376 }
2377
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002378 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002379 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002380 xdg_surface_move(window->xdg_surface,
2381 input_get_seat(input),
2382 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002383
2384 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2385 }
2386
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002387 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002388 input_ungrab(input);
2389
2390 window->resizing = 1;
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
Pekka Paalanen99436862012-11-19 17:15:59 +02002626 if (window->resizing) {
2627 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002628 /* Schedule a redraw to free the pool */
2629 window_schedule_redraw(window);
2630 }
2631
2632 input->sx = sx;
2633 input->sy = sy;
2634
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002635 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002636 input_set_focus_widget(input, widget, sx, sy);
2637}
2638
2639static void
2640pointer_handle_leave(void *data, struct wl_pointer *pointer,
2641 uint32_t serial, struct wl_surface *surface)
2642{
2643 struct input *input = data;
2644
2645 input->display->serial = serial;
2646 input_remove_pointer_focus(input);
2647}
2648
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002649static void
Daniel Stone37816df2012-05-16 18:45:18 +01002650pointer_handle_motion(void *data, struct wl_pointer *pointer,
2651 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002652{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002653 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002654 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002655 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002656 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002657 float sx = wl_fixed_to_double(sx_w);
2658 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002659
Paul Winwoodb22bf572013-08-29 10:52:54 +01002660 if (!window)
2661 return;
2662
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002663 input->sx = sx;
2664 input->sy = sy;
2665
Rob Bradford5f087742013-07-11 19:41:27 +01002666 /* when making the window smaller - e.g. after a unmaximise we might
2667 * still have a pending motion event that the compositor has picked
2668 * based on the old surface dimensions
2669 */
2670 if (sx > window->main_surface->allocation.width ||
2671 sy > window->main_surface->allocation.height)
2672 return;
2673
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002674 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002675 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002676 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002677 }
2678
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002679 if (input->grab)
2680 widget = input->grab;
2681 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002682 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002683 if (widget) {
2684 if (widget->motion_handler)
2685 cursor = widget->motion_handler(input->focus_widget,
2686 input, time, sx, sy,
2687 widget->user_data);
2688 else
2689 cursor = widget->default_cursor;
2690 } else
2691 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002692
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002693 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002694}
2695
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002696static void
Daniel Stone37816df2012-05-16 18:45:18 +01002697pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002698 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002699{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002700 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002701 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002702 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002703
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002704 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002705 if (input->focus_widget && input->grab == NULL &&
2706 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002707 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002708
Neil Roberts6b28aad2012-01-23 19:11:18 +00002709 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002710 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002711 (*widget->button_handler)(widget,
2712 input, time,
2713 button, state,
2714 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002715
Daniel Stone4dbadb12012-05-30 16:31:51 +01002716 if (input->grab && input->grab_button == button &&
2717 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002718 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002719}
2720
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002721static void
Daniel Stone37816df2012-05-16 18:45:18 +01002722pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002723 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002724{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002725 struct input *input = data;
2726 struct widget *widget;
2727
2728 widget = input->focus_widget;
2729 if (input->grab)
2730 widget = input->grab;
2731 if (widget && widget->axis_handler)
2732 (*widget->axis_handler)(widget,
2733 input, time,
2734 axis, value,
2735 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002736}
2737
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002738static const struct wl_pointer_listener pointer_listener = {
2739 pointer_handle_enter,
2740 pointer_handle_leave,
2741 pointer_handle_motion,
2742 pointer_handle_button,
2743 pointer_handle_axis,
2744};
2745
2746static void
2747input_remove_keyboard_focus(struct input *input)
2748{
2749 struct window *window = input->keyboard_focus;
2750 struct itimerspec its;
2751
2752 its.it_interval.tv_sec = 0;
2753 its.it_interval.tv_nsec = 0;
2754 its.it_value.tv_sec = 0;
2755 its.it_value.tv_nsec = 0;
2756 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2757
2758 if (!window)
2759 return;
2760
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002761 if (window->keyboard_focus_handler)
2762 (*window->keyboard_focus_handler)(window, NULL,
2763 window->user_data);
2764
2765 input->keyboard_focus = NULL;
2766}
2767
2768static void
2769keyboard_repeat_func(struct task *task, uint32_t events)
2770{
2771 struct input *input =
2772 container_of(task, struct input, repeat_task);
2773 struct window *window = input->keyboard_focus;
2774 uint64_t exp;
2775
2776 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2777 /* If we change the timer between the fd becoming
2778 * readable and getting here, there'll be nothing to
2779 * read and we get EAGAIN. */
2780 return;
2781
2782 if (window && window->key_handler) {
2783 (*window->key_handler)(window, input, input->repeat_time,
2784 input->repeat_key, input->repeat_sym,
2785 WL_KEYBOARD_KEY_STATE_PRESSED,
2786 window->user_data);
2787 }
2788}
2789
2790static void
2791keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2792 uint32_t format, int fd, uint32_t size)
2793{
2794 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002795 struct xkb_keymap *keymap;
2796 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002797 char *map_str;
2798
2799 if (!data) {
2800 close(fd);
2801 return;
2802 }
2803
2804 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2805 close(fd);
2806 return;
2807 }
2808
2809 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2810 if (map_str == MAP_FAILED) {
2811 close(fd);
2812 return;
2813 }
2814
Rui Matos3eccb862013-10-10 19:44:22 +02002815 keymap = xkb_map_new_from_string(input->display->xkb_context,
2816 map_str,
2817 XKB_KEYMAP_FORMAT_TEXT_V1,
2818 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002819 munmap(map_str, size);
2820 close(fd);
2821
Rui Matos3eccb862013-10-10 19:44:22 +02002822 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002823 fprintf(stderr, "failed to compile keymap\n");
2824 return;
2825 }
2826
Rui Matos3eccb862013-10-10 19:44:22 +02002827 state = xkb_state_new(keymap);
2828 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002829 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002830 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002831 return;
2832 }
2833
Rui Matos3eccb862013-10-10 19:44:22 +02002834 xkb_keymap_unref(input->xkb.keymap);
2835 xkb_state_unref(input->xkb.state);
2836 input->xkb.keymap = keymap;
2837 input->xkb.state = state;
2838
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002839 input->xkb.control_mask =
2840 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2841 input->xkb.alt_mask =
2842 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2843 input->xkb.shift_mask =
2844 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2845}
2846
2847static void
2848keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2849 uint32_t serial, struct wl_surface *surface,
2850 struct wl_array *keys)
2851{
2852 struct input *input = data;
2853 struct window *window;
2854
2855 input->display->serial = serial;
2856 input->keyboard_focus = wl_surface_get_user_data(surface);
2857
2858 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002859 if (window->keyboard_focus_handler)
2860 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002861 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002862}
2863
2864static void
2865keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2866 uint32_t serial, struct wl_surface *surface)
2867{
2868 struct input *input = data;
2869
2870 input->display->serial = serial;
2871 input_remove_keyboard_focus(input);
2872}
2873
Scott Moreau210d0792012-03-22 10:47:01 -06002874static void
Daniel Stone37816df2012-05-16 18:45:18 +01002875keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002876 uint32_t serial, uint32_t time, uint32_t key,
2877 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002878{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002879 struct input *input = data;
2880 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002881 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002882 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002883 const xkb_keysym_t *syms;
2884 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002885 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002886
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002887 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002888 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002889 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002890 return;
2891
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002892 /* We only use input grabs for pointer events for now, so just
2893 * ignore key presses if a grab is active. We expand the key
2894 * event delivery mechanism to route events to widgets to
2895 * properly handle key grabs. In the meantime, this prevents
2896 * key event devlivery while a grab is active. */
2897 if (input->grab && input->grab_button == 0)
2898 return;
2899
Daniel Stone97f68542012-05-30 16:32:01 +01002900 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002901
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002902 sym = XKB_KEY_NoSymbol;
2903 if (num_syms == 1)
2904 sym = syms[0];
2905
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002906
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002907 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002908 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002909 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002910 } else if (sym == XKB_KEY_F11 &&
2911 window->fullscreen_handler &&
2912 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2913 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002914 } else if (sym == XKB_KEY_F4 &&
2915 input->modifiers == MOD_ALT_MASK &&
2916 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002917 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002918 } else if (window->key_handler) {
2919 (*window->key_handler)(window, input, time, key,
2920 sym, state, window->user_data);
2921 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002922
2923 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2924 key == input->repeat_key) {
2925 its.it_interval.tv_sec = 0;
2926 its.it_interval.tv_nsec = 0;
2927 its.it_value.tv_sec = 0;
2928 its.it_value.tv_nsec = 0;
2929 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002930 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2931 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002932 input->repeat_sym = sym;
2933 input->repeat_key = key;
2934 input->repeat_time = time;
2935 its.it_interval.tv_sec = 0;
2936 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2937 its.it_value.tv_sec = 0;
2938 its.it_value.tv_nsec = 400 * 1000 * 1000;
2939 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2940 }
2941}
2942
2943static void
Daniel Stone351eb612012-05-31 15:27:47 -04002944keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2945 uint32_t serial, uint32_t mods_depressed,
2946 uint32_t mods_latched, uint32_t mods_locked,
2947 uint32_t group)
2948{
2949 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002950 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002951
Matt Ropere61561f2013-06-24 16:52:43 +01002952 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2953 if (!input->xkb.keymap)
2954 return;
2955
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002956 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2957 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002958 mask = xkb_state_serialize_mods(input->xkb.state,
2959 XKB_STATE_DEPRESSED |
2960 XKB_STATE_LATCHED);
2961 input->modifiers = 0;
2962 if (mask & input->xkb.control_mask)
2963 input->modifiers |= MOD_CONTROL_MASK;
2964 if (mask & input->xkb.alt_mask)
2965 input->modifiers |= MOD_ALT_MASK;
2966 if (mask & input->xkb.shift_mask)
2967 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002968}
2969
Daniel Stone37816df2012-05-16 18:45:18 +01002970static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002971 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002972 keyboard_handle_enter,
2973 keyboard_handle_leave,
2974 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002975 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002976};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002977
2978static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002979touch_handle_down(void *data, struct wl_touch *wl_touch,
2980 uint32_t serial, uint32_t time, struct wl_surface *surface,
2981 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2982{
2983 struct input *input = data;
2984 struct widget *widget;
2985 float sx = wl_fixed_to_double(x_w);
2986 float sy = wl_fixed_to_double(y_w);
2987
Rusty Lynch1084da52013-08-15 09:10:08 -07002988 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002989 input->touch_focus = wl_surface_get_user_data(surface);
2990 if (!input->touch_focus) {
2991 DBG("Failed to find to touch focus for surface %p\n", surface);
2992 return;
2993 }
2994
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002995 if (surface != input->touch_focus->main_surface->surface) {
2996 DBG("Ignoring input event from subsurface %p\n", surface);
2997 input->touch_focus = NULL;
2998 return;
2999 }
3000
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003001 if (input->grab)
3002 widget = input->grab;
3003 else
3004 widget = window_find_widget(input->touch_focus,
3005 wl_fixed_to_double(x_w),
3006 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003007 if (widget) {
3008 struct touch_point *tp = xmalloc(sizeof *tp);
3009 if (tp) {
3010 tp->id = id;
3011 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003012 tp->x = sx;
3013 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003014 wl_list_insert(&input->touch_point_list, &tp->link);
3015
3016 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003017 (*widget->touch_down_handler)(widget, input,
3018 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003019 sx, sy,
3020 widget->user_data);
3021 }
3022 }
3023}
3024
3025static void
3026touch_handle_up(void *data, struct wl_touch *wl_touch,
3027 uint32_t serial, uint32_t time, int32_t id)
3028{
3029 struct input *input = data;
3030 struct touch_point *tp, *tmp;
3031
Rusty Lynch041815a2013-08-08 21:20:38 -07003032 if (!input->touch_focus) {
3033 DBG("No touch focus found for touch up event!\n");
3034 return;
3035 }
3036
3037 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3038 if (tp->id != id)
3039 continue;
3040
3041 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003042 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003043 time, id,
3044 tp->widget->user_data);
3045
3046 wl_list_remove(&tp->link);
3047 free(tp);
3048
3049 return;
3050 }
3051}
3052
3053static void
3054touch_handle_motion(void *data, struct wl_touch *wl_touch,
3055 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3056{
3057 struct input *input = data;
3058 struct touch_point *tp;
3059 float sx = wl_fixed_to_double(x_w);
3060 float sy = wl_fixed_to_double(y_w);
3061
3062 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3063
3064 if (!input->touch_focus) {
3065 DBG("No touch focus found for touch motion event!\n");
3066 return;
3067 }
3068
3069 wl_list_for_each(tp, &input->touch_point_list, link) {
3070 if (tp->id != id)
3071 continue;
3072
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003073 tp->x = sx;
3074 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003075 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003076 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003077 id, sx, sy,
3078 tp->widget->user_data);
3079 return;
3080 }
3081}
3082
3083static void
3084touch_handle_frame(void *data, struct wl_touch *wl_touch)
3085{
3086 struct input *input = data;
3087 struct touch_point *tp, *tmp;
3088
3089 DBG("touch_handle_frame\n");
3090
3091 if (!input->touch_focus) {
3092 DBG("No touch focus found for touch frame event!\n");
3093 return;
3094 }
3095
3096 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3097 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003098 (*tp->widget->touch_frame_handler)(tp->widget, input,
3099 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003100 }
3101}
3102
3103static void
3104touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3105{
3106 struct input *input = data;
3107 struct touch_point *tp, *tmp;
3108
3109 DBG("touch_handle_cancel\n");
3110
3111 if (!input->touch_focus) {
3112 DBG("No touch focus found for touch cancel event!\n");
3113 return;
3114 }
3115
3116 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3117 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003118 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3119 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003120
3121 wl_list_remove(&tp->link);
3122 free(tp);
3123 }
3124}
3125
3126static const struct wl_touch_listener touch_listener = {
3127 touch_handle_down,
3128 touch_handle_up,
3129 touch_handle_motion,
3130 touch_handle_frame,
3131 touch_handle_cancel,
3132};
3133
3134static void
Daniel Stone37816df2012-05-16 18:45:18 +01003135seat_handle_capabilities(void *data, struct wl_seat *seat,
3136 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003137{
Daniel Stone37816df2012-05-16 18:45:18 +01003138 struct input *input = data;
3139
3140 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3141 input->pointer = wl_seat_get_pointer(seat);
3142 wl_pointer_set_user_data(input->pointer, input);
3143 wl_pointer_add_listener(input->pointer, &pointer_listener,
3144 input);
3145 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3146 wl_pointer_destroy(input->pointer);
3147 input->pointer = NULL;
3148 }
3149
3150 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3151 input->keyboard = wl_seat_get_keyboard(seat);
3152 wl_keyboard_set_user_data(input->keyboard, input);
3153 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3154 input);
3155 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3156 wl_keyboard_destroy(input->keyboard);
3157 input->keyboard = NULL;
3158 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003159
3160 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3161 input->touch = wl_seat_get_touch(seat);
3162 wl_touch_set_user_data(input->touch, input);
3163 wl_touch_add_listener(input->touch, &touch_listener, input);
3164 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3165 wl_touch_destroy(input->touch);
3166 input->touch = NULL;
3167 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003168}
3169
Rob Bradford08031182013-08-13 20:11:03 +01003170static void
3171seat_handle_name(void *data, struct wl_seat *seat,
3172 const char *name)
3173{
3174
3175}
3176
Daniel Stone37816df2012-05-16 18:45:18 +01003177static const struct wl_seat_listener seat_listener = {
3178 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003179 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003180};
3181
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003182void
3183input_get_position(struct input *input, int32_t *x, int32_t *y)
3184{
3185 *x = input->sx;
3186 *y = input->sy;
3187}
3188
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003189int
3190input_get_touch(struct input *input, int32_t id, float *x, float *y)
3191{
3192 struct touch_point *tp;
3193
3194 wl_list_for_each(tp, &input->touch_point_list, link) {
3195 if (tp->id != id)
3196 continue;
3197
3198 *x = tp->x;
3199 *y = tp->y;
3200 return 0;
3201 }
3202
3203 return -1;
3204}
3205
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003206struct display *
3207input_get_display(struct input *input)
3208{
3209 return input->display;
3210}
3211
Daniel Stone37816df2012-05-16 18:45:18 +01003212struct wl_seat *
3213input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003214{
Daniel Stone37816df2012-05-16 18:45:18 +01003215 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003216}
3217
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003218uint32_t
3219input_get_modifiers(struct input *input)
3220{
3221 return input->modifiers;
3222}
3223
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003224struct widget *
3225input_get_focus_widget(struct input *input)
3226{
3227 return input->focus_widget;
3228}
3229
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003230struct data_offer {
3231 struct wl_data_offer *offer;
3232 struct input *input;
3233 struct wl_array types;
3234 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003235
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003236 struct task io_task;
3237 int fd;
3238 data_func_t func;
3239 int32_t x, y;
3240 void *user_data;
3241};
3242
3243static void
3244data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3245{
3246 struct data_offer *offer = data;
3247 char **p;
3248
3249 p = wl_array_add(&offer->types, sizeof *p);
3250 *p = strdup(type);
3251}
3252
3253static const struct wl_data_offer_listener data_offer_listener = {
3254 data_offer_offer,
3255};
3256
3257static void
3258data_offer_destroy(struct data_offer *offer)
3259{
3260 char **p;
3261
3262 offer->refcount--;
3263 if (offer->refcount == 0) {
3264 wl_data_offer_destroy(offer->offer);
3265 for (p = offer->types.data; *p; p++)
3266 free(*p);
3267 wl_array_release(&offer->types);
3268 free(offer);
3269 }
3270}
3271
3272static void
3273data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003274 struct wl_data_device *data_device,
3275 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003276{
3277 struct data_offer *offer;
3278
Brian Lovinbc919262013-08-07 15:34:59 -07003279 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003280
3281 wl_array_init(&offer->types);
3282 offer->refcount = 1;
3283 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003284 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003285 wl_data_offer_add_listener(offer->offer,
3286 &data_offer_listener, offer);
3287}
3288
3289static void
3290data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003291 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003292 wl_fixed_t x_w, wl_fixed_t y_w,
3293 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294{
3295 struct input *input = data;
3296 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003297 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003298 float x = wl_fixed_to_double(x_w);
3299 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003300 char **p;
3301
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003302 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003303 input->drag_enter_serial = serial;
3304 input->drag_focus = window,
3305 input->drag_x = x;
3306 input->drag_y = y;
3307
3308 if (!input->touch_grab)
3309 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003310
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003311 if (offer) {
3312 input->drag_offer = wl_data_offer_get_user_data(offer);
3313
3314 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3315 *p = NULL;
3316
3317 types_data = input->drag_offer->types.data;
3318 } else {
3319 input->drag_offer = NULL;
3320 types_data = NULL;
3321 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003322
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003323 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003324 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003325 window->user_data);
3326}
3327
3328static void
3329data_device_leave(void *data, struct wl_data_device *data_device)
3330{
3331 struct input *input = data;
3332
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003333 if (input->drag_offer) {
3334 data_offer_destroy(input->drag_offer);
3335 input->drag_offer = NULL;
3336 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003337}
3338
3339static void
3340data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003341 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003342{
3343 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003344 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003345 float x = wl_fixed_to_double(x_w);
3346 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003347 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003348
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003349 input->drag_x = x;
3350 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003351
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003352 if (input->drag_offer)
3353 types_data = input->drag_offer->types.data;
3354 else
3355 types_data = NULL;
3356
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003357 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003358 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003359 window->user_data);
3360}
3361
3362static void
3363data_device_drop(void *data, struct wl_data_device *data_device)
3364{
3365 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003366 struct window *window = input->drag_focus;
3367 float x, y;
3368
3369 x = input->drag_x;
3370 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003371
3372 if (window->drop_handler)
3373 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003374 x, y, window->user_data);
3375
3376 if (input->touch_grab)
3377 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003378}
3379
3380static void
3381data_device_selection(void *data,
3382 struct wl_data_device *wl_data_device,
3383 struct wl_data_offer *offer)
3384{
3385 struct input *input = data;
3386 char **p;
3387
3388 if (input->selection_offer)
3389 data_offer_destroy(input->selection_offer);
3390
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003391 if (offer) {
3392 input->selection_offer = wl_data_offer_get_user_data(offer);
3393 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3394 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003395 } else {
3396 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003397 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003398}
3399
3400static const struct wl_data_device_listener data_device_listener = {
3401 data_device_data_offer,
3402 data_device_enter,
3403 data_device_leave,
3404 data_device_motion,
3405 data_device_drop,
3406 data_device_selection
3407};
3408
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003409static void
3410input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003411{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003412 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003413 struct wl_cursor *cursor;
3414 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003415
Daniel Stone80972742012-11-07 17:51:39 +11003416 if (!input->pointer)
3417 return;
3418
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003419 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003420 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003421 return;
3422
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003423 if (index >= (int) cursor->image_count) {
3424 fprintf(stderr, "cursor index out of range\n");
3425 return;
3426 }
3427
3428 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003429 buffer = wl_cursor_image_get_buffer(image);
3430 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003431 return;
3432
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003433 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3434 wl_surface_damage(input->pointer_surface, 0, 0,
3435 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003436 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003437 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3438 input->pointer_surface,
3439 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003440}
3441
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003442static const struct wl_callback_listener pointer_surface_listener;
3443
3444static void
3445pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3446 uint32_t time)
3447{
3448 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003449 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003450 int i;
3451
3452 if (callback) {
3453 assert(callback == input->cursor_frame_cb);
3454 wl_callback_destroy(callback);
3455 input->cursor_frame_cb = NULL;
3456 }
3457
Daniel Stone80972742012-11-07 17:51:39 +11003458 if (!input->pointer)
3459 return;
3460
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003461 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003462 wl_pointer_set_cursor(input->pointer,
3463 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003464 NULL, 0, 0);
3465 return;
3466 }
3467
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003468 if (input->current_cursor == CURSOR_UNSET)
3469 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003470 cursor = input->display->cursors[input->current_cursor];
3471 if (!cursor)
3472 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003473
3474 /* FIXME We don't have the current time on the first call so we set
3475 * the animation start to the time of the first frame callback. */
3476 if (time == 0)
3477 input->cursor_anim_start = 0;
3478 else if (input->cursor_anim_start == 0)
3479 input->cursor_anim_start = time;
3480
3481 if (time == 0 || input->cursor_anim_start == 0)
3482 i = 0;
3483 else
3484 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3485
Pekka Paalanenbc106382012-10-10 12:49:31 +03003486 if (cursor->image_count > 1) {
3487 input->cursor_frame_cb =
3488 wl_surface_frame(input->pointer_surface);
3489 wl_callback_add_listener(input->cursor_frame_cb,
3490 &pointer_surface_listener, input);
3491 }
3492
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003493 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003494}
3495
3496static const struct wl_callback_listener pointer_surface_listener = {
3497 pointer_surface_frame_callback
3498};
3499
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003500void
3501input_set_pointer_image(struct input *input, int pointer)
3502{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003503 int force = 0;
3504
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003505 if (!input->pointer)
3506 return;
3507
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003508 if (input->pointer_enter_serial > input->cursor_serial)
3509 force = 1;
3510
3511 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003512 return;
3513
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003514 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003515 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003516 if (!input->cursor_frame_cb)
3517 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003518 else if (force) {
3519 /* The current frame callback may be stuck if, for instance,
3520 * the set cursor request was processed by the server after
3521 * this client lost the focus. In this case the cursor surface
3522 * might not be mapped and the frame callback wouldn't ever
3523 * complete. Send a set_cursor and attach to try to map the
3524 * cursor surface again so that the callback will finish */
3525 input_set_pointer_image_index(input, 0);
3526 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003527}
3528
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003529struct wl_data_device *
3530input_get_data_device(struct input *input)
3531{
3532 return input->data_device;
3533}
3534
3535void
3536input_set_selection(struct input *input,
3537 struct wl_data_source *source, uint32_t time)
3538{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003539 if (input->data_device)
3540 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003541}
3542
3543void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003544input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003545{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003546 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003547 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003548}
3549
3550static void
3551offer_io_func(struct task *task, uint32_t events)
3552{
3553 struct data_offer *offer =
3554 container_of(task, struct data_offer, io_task);
3555 unsigned int len;
3556 char buffer[4096];
3557
3558 len = read(offer->fd, buffer, sizeof buffer);
3559 offer->func(buffer, len,
3560 offer->x, offer->y, offer->user_data);
3561
3562 if (len == 0) {
3563 close(offer->fd);
3564 data_offer_destroy(offer);
3565 }
3566}
3567
3568static void
3569data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3570 data_func_t func, void *user_data)
3571{
3572 int p[2];
3573
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003574 if (pipe2(p, O_CLOEXEC) == -1)
3575 return;
3576
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003577 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3578 close(p[1]);
3579
3580 offer->io_task.run = offer_io_func;
3581 offer->fd = p[0];
3582 offer->func = func;
3583 offer->refcount++;
3584 offer->user_data = user_data;
3585
3586 display_watch_fd(offer->input->display,
3587 offer->fd, EPOLLIN, &offer->io_task);
3588}
3589
3590void
3591input_receive_drag_data(struct input *input, const char *mime_type,
3592 data_func_t func, void *data)
3593{
3594 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003595 input->drag_offer->x = input->drag_x;
3596 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003597}
3598
3599int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003600input_receive_drag_data_to_fd(struct input *input,
3601 const char *mime_type, int fd)
3602{
3603 if (input->drag_offer)
3604 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3605
3606 return 0;
3607}
3608
3609int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003610input_receive_selection_data(struct input *input, const char *mime_type,
3611 data_func_t func, void *data)
3612{
3613 char **p;
3614
3615 if (input->selection_offer == NULL)
3616 return -1;
3617
3618 for (p = input->selection_offer->types.data; *p; p++)
3619 if (strcmp(mime_type, *p) == 0)
3620 break;
3621
3622 if (*p == NULL)
3623 return -1;
3624
3625 data_offer_receive_data(input->selection_offer,
3626 mime_type, func, data);
3627 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003628}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003629
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003630int
3631input_receive_selection_data_to_fd(struct input *input,
3632 const char *mime_type, int fd)
3633{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003634 if (input->selection_offer)
3635 wl_data_offer_receive(input->selection_offer->offer,
3636 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003637
3638 return 0;
3639}
3640
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003641void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003642window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003643{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003644 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003645 return;
3646
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003647 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003648}
3649
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003650static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003651surface_set_synchronized(struct surface *surface)
3652{
3653 if (!surface->subsurface)
3654 return;
3655
3656 if (surface->synchronized)
3657 return;
3658
3659 wl_subsurface_set_sync(surface->subsurface);
3660 surface->synchronized = 1;
3661}
3662
3663static void
3664surface_set_synchronized_default(struct surface *surface)
3665{
3666 if (!surface->subsurface)
3667 return;
3668
3669 if (surface->synchronized == surface->synchronized_default)
3670 return;
3671
3672 if (surface->synchronized_default)
3673 wl_subsurface_set_sync(surface->subsurface);
3674 else
3675 wl_subsurface_set_desync(surface->subsurface);
3676
3677 surface->synchronized = surface->synchronized_default;
3678}
3679
3680static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003681surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003682{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003683 struct widget *widget = surface->widget;
3684 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003685
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003686 if (surface->input_region) {
3687 wl_region_destroy(surface->input_region);
3688 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003689 }
3690
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003691 if (surface->opaque_region)
3692 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003693
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003694 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003695
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003696 if (widget->resize_handler)
3697 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003698 widget->allocation.width,
3699 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003700 widget->user_data);
3701
Pekka Paalanen35e82632013-04-25 13:57:48 +03003702 if (surface->subsurface &&
3703 (surface->allocation.x != widget->allocation.x ||
3704 surface->allocation.y != widget->allocation.y)) {
3705 wl_subsurface_set_position(surface->subsurface,
3706 widget->allocation.x,
3707 widget->allocation.y);
3708 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003709 if (surface->allocation.width != widget->allocation.width ||
3710 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003711 window_schedule_redraw(widget->window);
3712 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003713 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003714
3715 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003716 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003717 widget->allocation.width,
3718 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003719}
3720
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003721static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003722hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3723{
3724 /*
3725 * This hack should be removed, when EGL respects
3726 * eglSwapInterval(0).
3727 *
3728 * If this window has sub-surfaces, especially a free-running
3729 * EGL-widget, we need to post the parent surface once with
3730 * all the old state to guarantee, that the EGL-widget will
3731 * receive its frame callback soon. Otherwise, a forced call
3732 * to eglSwapBuffers may end up blocking, waiting for a frame
3733 * event that will never come, because we will commit the parent
3734 * surface with all new state only after eglSwapBuffers returns.
3735 *
3736 * This assumes, that:
3737 * 1. When the EGL widget's resize hook is called, it pauses.
3738 * 2. When the EGL widget's redraw hook is called, it forces a
3739 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3740 * In a single threaded application condition 1 is a no-op.
3741 *
3742 * XXX: This should actually be after the surface_resize() calls,
3743 * but cannot, because then it would commit the incomplete state
3744 * accumulated from the widget resize hooks.
3745 */
3746 if (window->subsurface_list.next != &window->main_surface->link ||
3747 window->subsurface_list.prev != &window->main_surface->link)
3748 wl_surface_commit(window->main_surface->surface);
3749}
3750
3751static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003752window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003753{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003754 struct surface *surface;
3755
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003756 widget_set_allocation(window->main_surface->widget,
3757 window->pending_allocation.x,
3758 window->pending_allocation.y,
3759 window->pending_allocation.width,
3760 window->pending_allocation.height);
3761
3762 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003763
3764 /* The main surface is in the list, too. Main surface's
3765 * resize_handler is responsible for calling widget_set_allocation()
3766 * on all sub-surface root widgets, so they will be resized
3767 * properly.
3768 */
3769 wl_list_for_each(surface, &window->subsurface_list, link) {
3770 if (surface == window->main_surface)
3771 continue;
3772
3773 surface_set_synchronized(surface);
3774 surface_resize(surface);
3775 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003776
3777 if (!window->fullscreen && !window->maximized)
3778 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003779}
3780
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003781static void
3782idle_resize(struct window *window)
3783{
3784 window->resize_needed = 0;
3785 window->redraw_needed = 1;
3786
3787 DBG("from %dx%d to %dx%d\n",
3788 window->main_surface->server_allocation.width,
3789 window->main_surface->server_allocation.height,
3790 window->pending_allocation.width,
3791 window->pending_allocation.height);
3792
3793 hack_prevent_EGL_sub_surface_deadlock(window);
3794
3795 window_do_resize(window);
3796}
3797
3798static void
3799undo_resize(struct window *window)
3800{
3801 window->pending_allocation.width =
3802 window->main_surface->server_allocation.width;
3803 window->pending_allocation.height =
3804 window->main_surface->server_allocation.height;
3805
3806 DBG("back to %dx%d\n",
3807 window->main_surface->server_allocation.width,
3808 window->main_surface->server_allocation.height);
3809
3810 window_do_resize(window);
3811
3812 if (window->pending_allocation.width == 0 &&
3813 window->pending_allocation.height == 0) {
3814 fprintf(stderr, "Error: Could not draw a surface, "
3815 "most likely due to insufficient disk space in "
3816 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3817 exit(EXIT_FAILURE);
3818 }
3819}
3820
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003821void
3822window_schedule_resize(struct window *window, int width, int height)
3823{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003824 /* We should probably get these numbers from the theme. */
3825 const int min_width = 200, min_height = 200;
3826
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003827 window->pending_allocation.x = 0;
3828 window->pending_allocation.y = 0;
3829 window->pending_allocation.width = width;
3830 window->pending_allocation.height = height;
3831
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003832 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003833 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003834 window->min_allocation.width = min_width;
3835 else
3836 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003837 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003838 window->min_allocation.height = min_height;
3839 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003840 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003841 }
3842
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003843 if (window->pending_allocation.width < window->min_allocation.width)
3844 window->pending_allocation.width = window->min_allocation.width;
3845 if (window->pending_allocation.height < window->min_allocation.height)
3846 window->pending_allocation.height = window->min_allocation.height;
3847
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003848 window->resize_needed = 1;
3849 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003850}
3851
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003852void
3853widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3854{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003855 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003856}
3857
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003858static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003859handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003860 int32_t width, int32_t height,
3861 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003862{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003863 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003864 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003865
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003866 if (width > 0 && height > 0) {
3867 window_schedule_resize(window, width, height);
3868 } else {
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003869 window_schedule_resize(window,
3870 window->saved_allocation.width,
3871 window->saved_allocation.height);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003872 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003873
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003874 window->maximized = 0;
3875 window->fullscreen = 0;
3876
3877 wl_array_for_each(p, states) {
3878 uint32_t state = *p;
3879 switch (state) {
3880 case XDG_SURFACE_STATE_MAXIMIZED:
3881 window->maximized = 1;
3882 break;
3883 case XDG_SURFACE_STATE_FULLSCREEN:
3884 window->fullscreen = 1;
3885 break;
3886 default:
3887 /* Unknown state */
3888 break;
3889 }
3890 }
3891
3892 window->next_attach_serial = serial;
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003893}
3894
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003895static void
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003896handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003897{
3898 struct window *window = data;
3899 window->focused = 1;
3900}
3901
3902static void
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003903handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003904{
3905 struct window *window = data;
3906 window->focused = 0;
3907}
3908
3909static void
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003910handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3911{
3912 struct window *window = data;
3913 window_close(window);
3914}
3915
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003916static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003917 handle_surface_configure,
Jasper St. Pierreb223a722014-02-08 18:11:53 -05003918 handle_surface_activated,
3919 handle_surface_deactivated,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003920 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003921};
3922
3923static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003924window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05003925{
3926 struct wl_surface *parent_surface;
3927
3928 if (!window->xdg_surface)
3929 return;
3930
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003931 if (window->parent)
3932 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05003933 else
3934 parent_surface = NULL;
3935
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003936 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre53686042013-12-09 15:26:25 -05003937}
3938
3939static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003940window_sync_margin(struct window *window)
3941{
3942 int margin;
3943
3944 if (!window->xdg_surface)
3945 return;
3946
3947 if (!window->frame)
3948 return;
3949
3950 margin = frame_get_shadow_margin(window->frame->frame);
3951
3952 /* Shadow size is the same on every side. */
3953 xdg_surface_set_margin(window->xdg_surface,
3954 margin,
3955 margin,
3956 margin,
3957 margin);
3958}
3959
3960static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003961window_flush(struct window *window)
3962{
3963 struct surface *surface;
3964
3965 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003966 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003967 window_sync_parent(window);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003968 window_sync_margin(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003969 }
3970 }
3971
3972 wl_list_for_each(surface, &window->subsurface_list, link) {
3973 if (surface == window->main_surface)
3974 continue;
3975
3976 surface_flush(surface);
3977 }
3978
3979 surface_flush(window->main_surface);
3980}
3981
3982static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003983menu_destroy(struct menu *menu)
3984{
3985 widget_destroy(menu->widget);
3986 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003987 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003988 free(menu);
3989}
3990
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003991void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003992window_get_allocation(struct window *window,
3993 struct rectangle *allocation)
3994{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003995 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003996}
3997
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003998static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003999widget_redraw(struct widget *widget)
4000{
4001 struct widget *child;
4002
4003 if (widget->redraw_handler)
4004 widget->redraw_handler(widget, widget->user_data);
4005 wl_list_for_each(child, &widget->child_list, link)
4006 widget_redraw(child);
4007}
4008
4009static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004010frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4011{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004012 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004013
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004014 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004015 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004016 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004017 surface->frame_cb = NULL;
4018
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004019 surface->last_time = time;
4020
Pekka Paalanen71233882013-04-25 13:57:53 +03004021 if (surface->redraw_needed || surface->window->redraw_needed) {
4022 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004023 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004024 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004025}
4026
4027static const struct wl_callback_listener listener = {
4028 frame_callback
4029};
4030
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004031static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004032surface_redraw(struct surface *surface)
4033{
Pekka Paalanen71233882013-04-25 13:57:53 +03004034 DBG_OBJ(surface->surface, "begin\n");
4035
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004036 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004037 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004038
4039 /* Whole-window redraw forces a redraw even if the previous has
4040 * not yet hit the screen.
4041 */
4042 if (surface->frame_cb) {
4043 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004044 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004045
Pekka Paalanen71233882013-04-25 13:57:53 +03004046 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004047 wl_callback_destroy(surface->frame_cb);
4048 }
4049
Neil Roberts97b747c2013-12-19 16:17:12 +00004050 if (surface->widget->use_cairo &&
4051 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004052 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4053 return -1;
4054 }
4055
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004056 surface->frame_cb = wl_surface_frame(surface->surface);
4057 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004058 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004059
4060 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004061 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004062 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004063 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004064 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004065}
4066
4067static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004068idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004069{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004070 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004071 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004072 int failed = 0;
4073 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004074
Pekka Paalanen71233882013-04-25 13:57:53 +03004075 DBG(" --------- \n");
4076
Pekka Paalaneneebff542013-04-25 13:57:52 +03004077 wl_list_init(&window->redraw_task.link);
4078 window->redraw_task_scheduled = 0;
4079
4080 if (window->resize_needed) {
4081 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004082 if (window->main_surface->frame_cb) {
4083 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004084 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004085 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004086
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004087 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004088 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004089 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004090
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004091 if (surface_redraw(window->main_surface) < 0) {
4092 /*
4093 * Only main_surface failure will cause us to undo the resize.
4094 * If sub-surfaces fail, they will just be broken with old
4095 * content.
4096 */
4097 failed = 1;
4098 } else {
4099 wl_list_for_each(surface, &window->subsurface_list, link) {
4100 if (surface == window->main_surface)
4101 continue;
4102
4103 surface_redraw(surface);
4104 }
4105 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004106
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004107 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004108 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004109
4110 wl_list_for_each(surface, &window->subsurface_list, link)
4111 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004112
4113 if (resized && failed) {
4114 /* Restore widget tree to correspond to what is on screen. */
4115 undo_resize(window);
4116 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004117}
4118
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004119static void
4120window_schedule_redraw_task(struct window *window)
4121{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004122 if (!window->redraw_task_scheduled) {
4123 window->redraw_task.run = idle_redraw;
4124 display_defer(window->display, &window->redraw_task);
4125 window->redraw_task_scheduled = 1;
4126 }
4127}
4128
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004129void
4130window_schedule_redraw(struct window *window)
4131{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004132 struct surface *surface;
4133
Pekka Paalanen71233882013-04-25 13:57:53 +03004134 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4135
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004136 wl_list_for_each(surface, &window->subsurface_list, link)
4137 surface->redraw_needed = 1;
4138
4139 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004140}
4141
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004142int
4143window_is_fullscreen(struct window *window)
4144{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004145 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004146}
4147
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004148void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004149window_set_fullscreen(struct window *window, int fullscreen)
4150{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004151 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004152 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004153
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004154 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004155 return;
4156
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004157 if (fullscreen)
4158 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4159 else
4160 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004161}
4162
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004163int
4164window_is_maximized(struct window *window)
4165{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004166 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004167}
4168
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004169void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004170window_set_maximized(struct window *window, int maximized)
4171{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004172 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004173 return;
4174
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004175 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004176 return;
4177
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004178 if (maximized)
4179 xdg_surface_set_maximized(window->xdg_surface);
4180 else
4181 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004182}
4183
4184void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004185window_set_minimized(struct window *window)
4186{
4187 if (!window->xdg_surface)
4188 return;
4189
4190 xdg_surface_set_minimized(window->xdg_surface);
4191}
4192
4193void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004194window_set_user_data(struct window *window, void *data)
4195{
4196 window->user_data = data;
4197}
4198
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004199void *
4200window_get_user_data(struct window *window)
4201{
4202 return window->user_data;
4203}
4204
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004205void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004206window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004207 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004208{
4209 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004210}
4211
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004212void
4213window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004214 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004215{
4216 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004217}
4218
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004219void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004220window_set_data_handler(struct window *window, window_data_handler_t handler)
4221{
4222 window->data_handler = handler;
4223}
4224
4225void
4226window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4227{
4228 window->drop_handler = handler;
4229}
4230
4231void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004232window_set_close_handler(struct window *window,
4233 window_close_handler_t handler)
4234{
4235 window->close_handler = handler;
4236}
4237
4238void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004239window_set_fullscreen_handler(struct window *window,
4240 window_fullscreen_handler_t handler)
4241{
4242 window->fullscreen_handler = handler;
4243}
4244
4245void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004246window_set_output_handler(struct window *window,
4247 window_output_handler_t handler)
4248{
4249 window->output_handler = handler;
4250}
4251
4252void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004253window_set_title(struct window *window, const char *title)
4254{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004255 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004256 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004257 if (window->frame) {
4258 frame_set_title(window->frame->frame, title);
4259 widget_schedule_redraw(window->frame->widget);
4260 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004261 if (window->xdg_surface)
4262 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004263}
4264
4265const char *
4266window_get_title(struct window *window)
4267{
4268 return window->title;
4269}
4270
4271void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004272window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4273{
4274 struct text_cursor_position *text_cursor_position =
4275 window->display->text_cursor_position;
4276
Scott Moreau9295ce02012-06-01 12:46:10 -06004277 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004278 return;
4279
4280 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004281 window->main_surface->surface,
4282 wl_fixed_from_int(x),
4283 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004284}
4285
4286void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004287window_damage(struct window *window, int32_t x, int32_t y,
4288 int32_t width, int32_t height)
4289{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004290 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004291}
4292
Casey Dahlin9074db52012-04-19 22:50:09 -04004293static void
4294surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004295 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004296{
Rob Bradford7507b572012-05-15 17:55:34 +01004297 struct window *window = data;
4298 struct output *output;
4299 struct output *output_found = NULL;
4300 struct window_output *window_output;
4301
4302 wl_list_for_each(output, &window->display->output_list, link) {
4303 if (output->output == wl_output) {
4304 output_found = output;
4305 break;
4306 }
4307 }
4308
4309 if (!output_found)
4310 return;
4311
Brian Lovinbc919262013-08-07 15:34:59 -07004312 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004313 window_output->output = output_found;
4314
4315 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004316
4317 if (window->output_handler)
4318 window->output_handler(window, output_found, 1,
4319 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004320}
4321
4322static void
4323surface_leave(void *data,
4324 struct wl_surface *wl_surface, struct wl_output *output)
4325{
Rob Bradford7507b572012-05-15 17:55:34 +01004326 struct window *window = data;
4327 struct window_output *window_output;
4328 struct window_output *window_output_found = NULL;
4329
4330 wl_list_for_each(window_output, &window->window_output_list, link) {
4331 if (window_output->output->output == output) {
4332 window_output_found = window_output;
4333 break;
4334 }
4335 }
4336
4337 if (window_output_found) {
4338 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004339
4340 if (window->output_handler)
4341 window->output_handler(window, window_output->output,
4342 0, window->user_data);
4343
Rob Bradford7507b572012-05-15 17:55:34 +01004344 free(window_output_found);
4345 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004346}
4347
4348static const struct wl_surface_listener surface_listener = {
4349 surface_enter,
4350 surface_leave
4351};
4352
Pekka Paalanen4e373742013-02-13 16:17:13 +02004353static struct surface *
4354surface_create(struct window *window)
4355{
4356 struct display *display = window->display;
4357 struct surface *surface;
4358
Brian Lovinbc919262013-08-07 15:34:59 -07004359 surface = xmalloc(sizeof *surface);
4360 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004361 if (!surface)
4362 return NULL;
4363
4364 surface->window = window;
4365 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004366 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004367 wl_surface_add_listener(surface->surface, &surface_listener, window);
4368
Pekka Paalanen35e82632013-04-25 13:57:48 +03004369 wl_list_insert(&window->subsurface_list, &surface->link);
4370
Pekka Paalanen4e373742013-02-13 16:17:13 +02004371 return surface;
4372}
4373
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004374static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004375get_preferred_buffer_type(struct display *display)
4376{
4377#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004378 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004379 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4380#endif
4381
4382 return WINDOW_BUFFER_TYPE_SHM;
4383}
4384
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004385static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004386window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004387{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004388 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004389 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004390
Peter Huttererf3d62272013-08-08 11:57:05 +10004391 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004392 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004393 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004394
4395 surface = surface_create(window);
4396 window->main_surface = surface;
4397
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004398 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004399
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004400 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004401 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004402
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004403 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004404
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004405 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004406 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004407 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004408
Rob Bradford7507b572012-05-15 17:55:34 +01004409 wl_list_init (&window->window_output_list);
4410
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004411 return window;
4412}
4413
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004414struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004415window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004416{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004417 struct window *window;
4418
4419 window = window_create_internal(display, 0);
4420
4421 window->xdg_surface =
4422 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4423 window->main_surface->surface);
4424 fail_on_null(window->xdg_surface);
4425
4426 xdg_surface_set_user_data(window->xdg_surface, window);
4427 xdg_surface_add_listener(window->xdg_surface,
4428 &xdg_surface_listener, window);
4429
4430 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004431}
4432
4433struct window *
4434window_create_custom(struct display *display)
4435{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004436 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004437}
4438
Jasper St. Pierre53686042013-12-09 15:26:25 -05004439void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004440window_set_parent(struct window *window,
4441 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004442{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004443 window->parent = parent_window;
4444 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004445}
4446
4447struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004448window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004449{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004450 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004451}
4452
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004453static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004454menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004455{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004456 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004457 int next;
4458
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004459 frame_interior(menu->frame, &x, &y, &width, &height);
4460 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004461 if (menu->current != next) {
4462 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004463 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004464 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004465}
4466
4467static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004468menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004469 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004470 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004471{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004472 struct menu *menu = data;
4473
4474 if (widget == menu->widget)
4475 menu_set_item(data, y);
4476
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004477 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004478}
4479
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004480static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004481menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004482 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004483{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004484 struct menu *menu = data;
4485
4486 if (widget == menu->widget)
4487 menu_set_item(data, y);
4488
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004489 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004490}
4491
4492static void
4493menu_leave_handler(struct widget *widget, struct input *input, void *data)
4494{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004495 struct menu *menu = data;
4496
4497 if (widget == menu->widget)
4498 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004499}
4500
4501static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004502menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004503 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004504 uint32_t button, enum wl_pointer_button_state state,
4505 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004506
4507{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004508 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004509
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004510 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4511 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004512 /* Either relase after press-drag-release or
4513 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004514 menu->func(menu->parent, input,
4515 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004516 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004517 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004518 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004519 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004520 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004521}
4522
4523static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004524menu_touch_up_handler(struct widget *widget,
4525 struct input *input,
4526 uint32_t serial,
4527 uint32_t time,
4528 int32_t id,
4529 void *data)
4530{
4531 struct menu *menu = data;
4532
4533 input_ungrab(input);
4534 menu_destroy(menu);
4535}
4536
4537static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004538menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004539{
4540 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004541 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004542 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004543
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004544 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004545
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004546 frame_repaint(menu->frame, cr);
4547 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004548
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004549 theme_set_background_source(menu->window->display->theme,
4550 cr, THEME_FRAME_ACTIVE);
4551 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004552 cairo_fill(cr);
4553
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004554 cairo_select_font_face(cr, "sans",
4555 CAIRO_FONT_SLANT_NORMAL,
4556 CAIRO_FONT_WEIGHT_NORMAL);
4557 cairo_set_font_size(cr, 12);
4558
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004559 for (i = 0; i < menu->count; i++) {
4560 if (i == menu->current) {
4561 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004562 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004563 cairo_fill(cr);
4564 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004565 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004566 cairo_show_text(cr, menu->entries[i]);
4567 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004568 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4569 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004570 cairo_show_text(cr, menu->entries[i]);
4571 }
4572 }
4573
4574 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004575}
4576
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004577static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004578handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4579{
4580 struct window *window = data;
4581 struct menu *menu = window->main_surface->widget->user_data;
4582
4583 input_ungrab(menu->input);
4584 menu_destroy(menu);
4585}
4586
4587static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004588 handle_popup_popup_done,
4589};
4590
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004591void
4592window_show_menu(struct display *display,
4593 struct input *input, uint32_t time, struct window *parent,
4594 int32_t x, int32_t y,
4595 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004596{
4597 struct window *window;
4598 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004599 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004600
4601 menu = malloc(sizeof *menu);
4602 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004603 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004604
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004605 window = window_create_internal(parent->display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004606 if (!window) {
4607 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004608 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004609 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004610
4611 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004612 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004613 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004614 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4615 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004616 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004617 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004618 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004619 menu->entries = entries;
4620 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004621 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004622 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004623 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004624 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004625 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004626 window->x = x;
4627 window->y = y;
4628
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004629 input_ungrab(input);
4630
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004631 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004632 widget_set_enter_handler(menu->widget, menu_enter_handler);
4633 widget_set_leave_handler(menu->widget, menu_leave_handler);
4634 widget_set_motion_handler(menu->widget, menu_motion_handler);
4635 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004636 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004637
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004638 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004639 frame_resize_inside(menu->frame, 200, count * 20);
4640 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4641 window_schedule_resize(window, frame_width(menu->frame),
4642 frame_height(menu->frame));
4643
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004644 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004645
4646 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4647 window->main_surface->surface,
4648 parent->main_surface->surface,
4649 input->seat,
4650 display_get_serial(window->display),
4651 window->x - ix,
4652 window->y - iy,
4653 0);
4654 fail_on_null(window->xdg_popup);
4655
4656 xdg_popup_set_user_data(window->xdg_popup, window);
4657 xdg_popup_add_listener(window->xdg_popup,
4658 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004659}
4660
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004661void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004662window_set_buffer_type(struct window *window, enum window_buffer_type type)
4663{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004664 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004665}
4666
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004667enum window_buffer_type
4668window_get_buffer_type(struct window *window)
4669{
4670 return window->main_surface->buffer_type;
4671}
4672
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004673void
4674window_set_preferred_format(struct window *window,
4675 enum preferred_format format)
4676{
4677 window->preferred_format = format;
4678}
4679
Pekka Paalanen35e82632013-04-25 13:57:48 +03004680struct widget *
4681window_add_subsurface(struct window *window, void *data,
4682 enum subsurface_mode default_mode)
4683{
4684 struct widget *widget;
4685 struct surface *surface;
4686 struct wl_surface *parent;
4687 struct wl_subcompositor *subcompo = window->display->subcompositor;
4688
Pekka Paalanen35e82632013-04-25 13:57:48 +03004689 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004690 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004691 widget = widget_create(window, surface, data);
4692 wl_list_init(&widget->link);
4693 surface->widget = widget;
4694
4695 parent = window->main_surface->surface;
4696 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4697 surface->surface,
4698 parent);
4699 surface->synchronized = 1;
4700
4701 switch (default_mode) {
4702 case SUBSURFACE_SYNCHRONIZED:
4703 surface->synchronized_default = 1;
4704 break;
4705 case SUBSURFACE_DESYNCHRONIZED:
4706 surface->synchronized_default = 0;
4707 break;
4708 default:
4709 assert(!"bad enum subsurface_mode");
4710 }
4711
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004712 window->resize_needed = 1;
4713 window_schedule_redraw(window);
4714
Pekka Paalanen35e82632013-04-25 13:57:48 +03004715 return widget;
4716}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004717
4718static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004719display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004720 struct wl_output *wl_output,
4721 int x, int y,
4722 int physical_width,
4723 int physical_height,
4724 int subpixel,
4725 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004726 const char *model,
4727 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004728{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004729 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004730
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004731 output->allocation.x = x;
4732 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004733 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004734
4735 if (output->make)
4736 free(output->make);
4737 output->make = strdup(make);
4738
4739 if (output->model)
4740 free(output->model);
4741 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004742}
4743
4744static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004745display_handle_done(void *data,
4746 struct wl_output *wl_output)
4747{
4748}
4749
4750static void
4751display_handle_scale(void *data,
4752 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004753 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004754{
4755 struct output *output = data;
4756
4757 output->scale = scale;
4758}
4759
4760static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004761display_handle_mode(void *data,
4762 struct wl_output *wl_output,
4763 uint32_t flags,
4764 int width,
4765 int height,
4766 int refresh)
4767{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004768 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004769 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004770
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004771 if (flags & WL_OUTPUT_MODE_CURRENT) {
4772 output->allocation.width = width;
4773 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004774 if (display->output_configure_handler)
4775 (*display->output_configure_handler)(
4776 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004777 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004778}
4779
4780static const struct wl_output_listener output_listener = {
4781 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004782 display_handle_mode,
4783 display_handle_done,
4784 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004785};
4786
4787static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004788display_add_output(struct display *d, uint32_t id)
4789{
4790 struct output *output;
4791
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004792 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004793 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004794 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004795 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004796 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004797 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004798 wl_list_insert(d->output_list.prev, &output->link);
4799
4800 wl_output_add_listener(output->output, &output_listener, output);
4801}
4802
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004803static void
4804output_destroy(struct output *output)
4805{
4806 if (output->destroy_handler)
4807 (*output->destroy_handler)(output, output->user_data);
4808
4809 wl_output_destroy(output->output);
4810 wl_list_remove(&output->link);
4811 free(output);
4812}
4813
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004814static void
4815display_destroy_output(struct display *d, uint32_t id)
4816{
4817 struct output *output;
4818
4819 wl_list_for_each(output, &d->output_list, link) {
4820 if (output->server_output_id == id) {
4821 output_destroy(output);
4822 break;
4823 }
4824 }
4825}
4826
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004827void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004828display_set_global_handler(struct display *display,
4829 display_global_handler_t handler)
4830{
4831 struct global *global;
4832
4833 display->global_handler = handler;
4834 if (!handler)
4835 return;
4836
4837 wl_list_for_each(global, &display->global_list, link)
4838 display->global_handler(display,
4839 global->name, global->interface,
4840 global->version, display->user_data);
4841}
4842
4843void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004844display_set_global_handler_remove(struct display *display,
4845 display_global_handler_t remove_handler)
4846{
4847 display->global_handler_remove = remove_handler;
4848 if (!remove_handler)
4849 return;
4850}
4851
4852void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004853display_set_output_configure_handler(struct display *display,
4854 display_output_handler_t handler)
4855{
4856 struct output *output;
4857
4858 display->output_configure_handler = handler;
4859 if (!handler)
4860 return;
4861
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004862 wl_list_for_each(output, &display->output_list, link) {
4863 if (output->allocation.width == 0 &&
4864 output->allocation.height == 0)
4865 continue;
4866
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004867 (*display->output_configure_handler)(output,
4868 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004869 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004870}
4871
4872void
4873output_set_user_data(struct output *output, void *data)
4874{
4875 output->user_data = data;
4876}
4877
4878void *
4879output_get_user_data(struct output *output)
4880{
4881 return output->user_data;
4882}
4883
4884void
4885output_set_destroy_handler(struct output *output,
4886 display_output_handler_t handler)
4887{
4888 output->destroy_handler = handler;
4889 /* FIXME: implement this, once we have way to remove outputs */
4890}
4891
4892void
Scott Moreau4e072362012-09-29 02:03:11 -06004893output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004894{
Scott Moreau4e072362012-09-29 02:03:11 -06004895 struct rectangle allocation = output->allocation;
4896
4897 switch (output->transform) {
4898 case WL_OUTPUT_TRANSFORM_90:
4899 case WL_OUTPUT_TRANSFORM_270:
4900 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4901 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4902 /* Swap width and height */
4903 allocation.width = output->allocation.height;
4904 allocation.height = output->allocation.width;
4905 break;
4906 }
4907
4908 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004909}
4910
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004911struct wl_output *
4912output_get_wl_output(struct output *output)
4913{
4914 return output->output;
4915}
4916
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004917enum wl_output_transform
4918output_get_transform(struct output *output)
4919{
4920 return output->transform;
4921}
4922
Alexander Larssonafd319a2013-05-22 14:41:27 +02004923uint32_t
4924output_get_scale(struct output *output)
4925{
4926 return output->scale;
4927}
4928
Jason Ekstrand738715d2014-04-02 19:53:50 -05004929const char *
4930output_get_make(struct output *output)
4931{
4932 return output->make;
4933}
4934
4935const char *
4936output_get_model(struct output *output)
4937{
4938 return output->model;
4939}
4940
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004941static void
Daniel Stone97f68542012-05-30 16:32:01 +01004942fini_xkb(struct input *input)
4943{
4944 xkb_state_unref(input->xkb.state);
4945 xkb_map_unref(input->xkb.keymap);
4946}
4947
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004948#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004949
Daniel Stone97f68542012-05-30 16:32:01 +01004950static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004951display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004952{
4953 struct input *input;
4954
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004955 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004956 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004957 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004958 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004959 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004960 input->pointer_focus = NULL;
4961 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004962 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004963 wl_list_insert(d->input_list.prev, &input->link);
4964
Daniel Stone37816df2012-05-16 18:45:18 +01004965 wl_seat_add_listener(input->seat, &seat_listener, input);
4966 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004967
Jason Ekstranda669bd52014-04-02 19:53:51 -05004968 if (d->data_device_manager) {
4969 input->data_device =
4970 wl_data_device_manager_get_data_device(d->data_device_manager,
4971 input->seat);
4972 wl_data_device_add_listener(input->data_device,
4973 &data_device_listener,
4974 input);
4975 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004976
4977 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004978
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004979 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4980 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004981 input->repeat_task.run = keyboard_repeat_func;
4982 display_watch_fd(d, input->repeat_timer_fd,
4983 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004984}
4985
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004986static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004987input_destroy(struct input *input)
4988{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004989 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004990 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004991
4992 if (input->drag_offer)
4993 data_offer_destroy(input->drag_offer);
4994
4995 if (input->selection_offer)
4996 data_offer_destroy(input->selection_offer);
4997
Jason Ekstranda669bd52014-04-02 19:53:51 -05004998 if (input->data_device)
4999 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005000
5001 if (input->display->seat_version >= 3) {
5002 if (input->pointer)
5003 wl_pointer_release(input->pointer);
5004 if (input->keyboard)
5005 wl_keyboard_release(input->keyboard);
5006 }
5007
Daniel Stone97f68542012-05-30 16:32:01 +01005008 fini_xkb(input);
5009
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005010 wl_surface_destroy(input->pointer_surface);
5011
Pekka Paalanene1207c72011-12-16 12:02:09 +02005012 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005013 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005014 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005015 free(input);
5016}
5017
5018static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005019init_workspace_manager(struct display *d, uint32_t id)
5020{
5021 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005022 wl_registry_bind(d->registry, id,
5023 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005024 if (d->workspace_manager != NULL)
5025 workspace_manager_add_listener(d->workspace_manager,
5026 &workspace_manager_listener,
5027 d);
5028}
5029
5030static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005031shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5032{
5033 struct display *d = data;
5034
5035 if (format == WL_SHM_FORMAT_RGB565)
5036 d->has_rgb565 = 1;
5037}
5038
5039struct wl_shm_listener shm_listener = {
5040 shm_format
5041};
5042
5043static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005044xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5045{
5046 xdg_shell_pong(shell, serial);
5047}
5048
5049static const struct xdg_shell_listener xdg_shell_listener = {
5050 xdg_shell_ping,
5051};
5052
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08005053#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005054#ifdef static_assert
5055static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5056 "Interface version doesn't match implementation version");
5057#endif
5058
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005059static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005060registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5061 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005062{
5063 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005064 struct global *global;
5065
Brian Lovinbc919262013-08-07 15:34:59 -07005066 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005067 global->name = id;
5068 global->interface = strdup(interface);
5069 global->version = version;
5070 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005071
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005072 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005073 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005074 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005075 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005076 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005077 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005078 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005079 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005080 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005081 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005082 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005083 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5084 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005085 wl_registry_bind(registry, id,
5086 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005087 } else if (strcmp(interface, "xdg_shell") == 0) {
5088 d->xdg_shell = wl_registry_bind(registry, id,
5089 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005090 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005091 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005092 } else if (strcmp(interface, "text_cursor_position") == 0) {
5093 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005094 wl_registry_bind(registry, id,
5095 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005096 } else if (strcmp(interface, "workspace_manager") == 0) {
5097 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005098 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5099 d->subcompositor =
5100 wl_registry_bind(registry, id,
5101 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005102 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005103
5104 if (d->global_handler)
5105 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005106}
5107
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005108static void
5109registry_handle_global_remove(void *data, struct wl_registry *registry,
5110 uint32_t name)
5111{
5112 struct display *d = data;
5113 struct global *global;
5114 struct global *tmp;
5115
5116 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5117 if (global->name != name)
5118 continue;
5119
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005120 if (strcmp(global->interface, "wl_output") == 0)
5121 display_destroy_output(d, name);
5122
5123 /* XXX: Should destroy remaining bound globals */
5124
5125 if (d->global_handler_remove)
5126 d->global_handler_remove(d, name, global->interface,
5127 global->version, d->user_data);
5128
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005129 wl_list_remove(&global->link);
5130 free(global->interface);
5131 free(global);
5132 }
5133}
5134
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005135void *
5136display_bind(struct display *display, uint32_t name,
5137 const struct wl_interface *interface, uint32_t version)
5138{
5139 return wl_registry_bind(display->registry, name, interface, version);
5140}
5141
5142static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005143 registry_handle_global,
5144 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005145};
5146
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005147#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005148static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005149init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005150{
5151 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005152 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005153
Rob Clark6396ed32012-03-11 19:48:41 -05005154#ifdef USE_CAIRO_GLESV2
5155# define GL_BIT EGL_OPENGL_ES2_BIT
5156#else
5157# define GL_BIT EGL_OPENGL_BIT
5158#endif
5159
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005160 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005161 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005162 EGL_RED_SIZE, 1,
5163 EGL_GREEN_SIZE, 1,
5164 EGL_BLUE_SIZE, 1,
5165 EGL_ALPHA_SIZE, 1,
5166 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005167 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005168 EGL_NONE
5169 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005170
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005171#ifdef USE_CAIRO_GLESV2
5172 static const EGLint context_attribs[] = {
5173 EGL_CONTEXT_CLIENT_VERSION, 2,
5174 EGL_NONE
5175 };
5176 EGLint api = EGL_OPENGL_ES_API;
5177#else
5178 EGLint *context_attribs = NULL;
5179 EGLint api = EGL_OPENGL_API;
5180#endif
5181
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005182 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005183 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005184 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005185 return -1;
5186 }
5187
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005188 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005189 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005190 return -1;
5191 }
5192
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005193 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5194 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005195 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005196 return -1;
5197 }
5198
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005199 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005200 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005201 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005202 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005203 return -1;
5204 }
5205
Benjamin Franzke0c991632011-09-27 21:57:31 +02005206 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5207 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005208 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005209 return -1;
5210 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005211
5212 return 0;
5213}
5214
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005215static void
5216fini_egl(struct display *display)
5217{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005218 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005219
5220 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5221 EGL_NO_CONTEXT);
5222
5223 eglTerminate(display->dpy);
5224 eglReleaseThread();
5225}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005226#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005227
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005228static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005229init_dummy_surface(struct display *display)
5230{
5231 int len;
5232 void *data;
5233
5234 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5235 data = malloc(len);
5236 display->dummy_surface =
5237 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5238 1, 1, len);
5239 display->dummy_surface_data = data;
5240}
5241
5242static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005243handle_display_data(struct task *task, uint32_t events)
5244{
5245 struct display *display =
5246 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005247 struct epoll_event ep;
5248 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005249
5250 display->display_fd_events = events;
5251
5252 if (events & EPOLLERR || events & EPOLLHUP) {
5253 display_exit(display);
5254 return;
5255 }
5256
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005257 if (events & EPOLLIN) {
5258 ret = wl_display_dispatch(display->display);
5259 if (ret == -1) {
5260 display_exit(display);
5261 return;
5262 }
5263 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005264
5265 if (events & EPOLLOUT) {
5266 ret = wl_display_flush(display->display);
5267 if (ret == 0) {
5268 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5269 ep.data.ptr = &display->display_task;
5270 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5271 display->display_fd, &ep);
5272 } else if (ret == -1 && errno != EAGAIN) {
5273 display_exit(display);
5274 return;
5275 }
5276 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005277}
5278
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005279static void
5280log_handler(const char *format, va_list args)
5281{
5282 vfprintf(stderr, format, args);
5283}
5284
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005285struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005286display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005287{
5288 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005289
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005290 wl_log_set_handler_client(log_handler);
5291
Peter Huttererf3d62272013-08-08 11:57:05 +10005292 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005293 if (d == NULL)
5294 return NULL;
5295
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005296 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005297 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005298 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005299 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005300 return NULL;
5301 }
5302
Rob Bradford5ab9c752013-07-26 16:29:43 +01005303 d->xkb_context = xkb_context_new(0);
5304 if (d->xkb_context == NULL) {
5305 fprintf(stderr, "Failed to create XKB context\n");
5306 free(d);
5307 return NULL;
5308 }
5309
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005310 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005311 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005312 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005313 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5314 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005315
5316 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005317 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005318 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005319 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005320
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005321 d->workspace = 0;
5322 d->workspace_count = 1;
5323
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005324 d->registry = wl_display_get_registry(d->display);
5325 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005326
5327 if (wl_display_dispatch(d->display) < 0) {
5328 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5329 return NULL;
5330 }
5331
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005332#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005333 if (init_egl(d) < 0)
5334 fprintf(stderr, "EGL does not seem to work, "
5335 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005336#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005337
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005338 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005339
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005340 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005341
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005342 wl_list_init(&d->window_list);
5343
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005344 init_dummy_surface(d);
5345
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005346 return d;
5347}
5348
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005349static void
5350display_destroy_outputs(struct display *display)
5351{
5352 struct output *tmp;
5353 struct output *output;
5354
5355 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5356 output_destroy(output);
5357}
5358
Pekka Paalanene1207c72011-12-16 12:02:09 +02005359static void
5360display_destroy_inputs(struct display *display)
5361{
5362 struct input *tmp;
5363 struct input *input;
5364
5365 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5366 input_destroy(input);
5367}
5368
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005369void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005370display_destroy(struct display *display)
5371{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005372 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005373 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5374 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005375
5376 if (!wl_list_empty(&display->deferred_list))
5377 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5378
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005379 cairo_surface_destroy(display->dummy_surface);
5380 free(display->dummy_surface_data);
5381
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005382 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005383 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005384
Daniel Stone97f68542012-05-30 16:32:01 +01005385 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005386
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005387 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005388 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005389
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005390#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005391 if (display->argb_device)
5392 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005393#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005394
Pekka Paalanen35e82632013-04-25 13:57:48 +03005395 if (display->subcompositor)
5396 wl_subcompositor_destroy(display->subcompositor);
5397
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005398 if (display->xdg_shell)
5399 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005400
5401 if (display->shm)
5402 wl_shm_destroy(display->shm);
5403
5404 if (display->data_device_manager)
5405 wl_data_device_manager_destroy(display->data_device_manager);
5406
5407 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005408 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005409
5410 close(display->epoll_fd);
5411
U. Artie Eoff44874d92012-10-02 21:12:35 -07005412 if (!(display->display_fd_events & EPOLLERR) &&
5413 !(display->display_fd_events & EPOLLHUP))
5414 wl_display_flush(display->display);
5415
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005416 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005417 free(display);
5418}
5419
5420void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005421display_set_user_data(struct display *display, void *data)
5422{
5423 display->user_data = data;
5424}
5425
5426void *
5427display_get_user_data(struct display *display)
5428{
5429 return display->user_data;
5430}
5431
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005432struct wl_display *
5433display_get_display(struct display *display)
5434{
5435 return display->display;
5436}
5437
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005438int
5439display_has_subcompositor(struct display *display)
5440{
5441 if (display->subcompositor)
5442 return 1;
5443
5444 wl_display_roundtrip(display->display);
5445
5446 return display->subcompositor != NULL;
5447}
5448
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005449cairo_device_t *
5450display_get_cairo_device(struct display *display)
5451{
5452 return display->argb_device;
5453}
5454
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005455struct output *
5456display_get_output(struct display *display)
5457{
5458 return container_of(display->output_list.next, struct output, link);
5459}
5460
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005461struct wl_compositor *
5462display_get_compositor(struct display *display)
5463{
5464 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005465}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005466
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005467uint32_t
5468display_get_serial(struct display *display)
5469{
5470 return display->serial;
5471}
5472
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005473EGLDisplay
5474display_get_egl_display(struct display *d)
5475{
5476 return d->dpy;
5477}
5478
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005479struct wl_data_source *
5480display_create_data_source(struct display *display)
5481{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005482 if (display->data_device_manager)
5483 return wl_data_device_manager_create_data_source(display->data_device_manager);
5484 else
5485 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005486}
5487
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005488EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005489display_get_argb_egl_config(struct display *d)
5490{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005491 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005492}
5493
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005494int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005495display_acquire_window_surface(struct display *display,
5496 struct window *window,
5497 EGLContext ctx)
5498{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005499 struct surface *surface = window->main_surface;
5500
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005501 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005502 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005503
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005504 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005505 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005506}
5507
5508void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005509display_release_window_surface(struct display *display,
5510 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005511{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005512 struct surface *surface = window->main_surface;
5513
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005514 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005515 return;
5516
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005517 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005518}
5519
5520void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005521display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005522{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005523 wl_list_insert(&display->deferred_list, &task->link);
5524}
5525
5526void
5527display_watch_fd(struct display *display,
5528 int fd, uint32_t events, struct task *task)
5529{
5530 struct epoll_event ep;
5531
5532 ep.events = events;
5533 ep.data.ptr = task;
5534 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5535}
5536
5537void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005538display_unwatch_fd(struct display *display, int fd)
5539{
5540 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5541}
5542
5543void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005544display_run(struct display *display)
5545{
5546 struct task *task;
5547 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005548 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005549
Pekka Paalanen826d7952011-12-15 10:14:07 +02005550 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005551 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005552 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005553 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005554 struct task, link);
5555 wl_list_remove(&task->link);
5556 task->run(task, 0);
5557 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005558
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005559 wl_display_dispatch_pending(display->display);
5560
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005561 if (!display->running)
5562 break;
5563
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005564 ret = wl_display_flush(display->display);
5565 if (ret < 0 && errno == EAGAIN) {
5566 ep[0].events =
5567 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5568 ep[0].data.ptr = &display->display_task;
5569
5570 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5571 display->display_fd, &ep[0]);
5572 } else if (ret < 0) {
5573 break;
5574 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005575
5576 count = epoll_wait(display->epoll_fd,
5577 ep, ARRAY_LENGTH(ep), -1);
5578 for (i = 0; i < count; i++) {
5579 task = ep[i].data.ptr;
5580 task->run(task, ep[i].events);
5581 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005582 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005583}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005584
5585void
5586display_exit(struct display *display)
5587{
5588 display->running = 0;
5589}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005590
5591void
5592keysym_modifiers_add(struct wl_array *modifiers_map,
5593 const char *name)
5594{
5595 size_t len = strlen(name) + 1;
5596 char *p;
5597
5598 p = wl_array_add(modifiers_map, len);
5599
5600 if (p == NULL)
5601 return;
5602
5603 strncpy(p, name, len);
5604}
5605
5606static xkb_mod_index_t
5607keysym_modifiers_get_index(struct wl_array *modifiers_map,
5608 const char *name)
5609{
5610 xkb_mod_index_t index = 0;
5611 char *p = modifiers_map->data;
5612
5613 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5614 if (strcmp(p, name) == 0)
5615 return index;
5616
5617 index++;
5618 p += strlen(p) + 1;
5619 }
5620
5621 return XKB_MOD_INVALID;
5622}
5623
5624xkb_mod_mask_t
5625keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5626 const char *name)
5627{
5628 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5629
5630 if (index == XKB_MOD_INVALID)
5631 return XKB_MOD_INVALID;
5632
5633 return 1 << index;
5634}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005635
5636void *
5637fail_on_null(void *p)
5638{
5639 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005640 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005641 exit(EXIT_FAILURE);
5642 }
5643
5644 return p;
5645}
5646
5647void *
5648xmalloc(size_t s)
5649{
5650 return fail_on_null(malloc(s));
5651}
5652
Peter Huttererf3d62272013-08-08 11:57:05 +10005653void *
5654xzalloc(size_t s)
5655{
5656 return fail_on_null(zalloc(s));
5657}
5658
Kristian Høgsbergce278412013-07-25 15:20:20 -07005659char *
5660xstrdup(const char *s)
5661{
5662 return fail_on_null(strdup(s));
5663}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005664
5665void *
5666xrealloc(char *p, size_t s)
5667{
5668 return fail_on_null(realloc(p, s));
5669}