blob: d5fbd5496dde4d42e6dccd2408cbac2c58855e0f [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050068#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030088 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050093 struct xdg_shell *xdg_shell;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800121 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200133
134 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100135 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500136};
137
Rob Bradford7507b572012-05-15 17:55:34 +0100138struct window_output {
139 struct output *output;
140 struct wl_list link;
141};
142
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200143struct toysurface {
144 /*
145 * Prepare the surface for drawing. Makes sure there is a surface
146 * of the right size available for rendering, and returns it.
147 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200148 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200149 * If flags has SURFACE_HINT_RESIZE set, the user is
150 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151 * Returns the Cairo surface to draw to.
152 */
153 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200154 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200155 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156
157 /*
158 * Post the surface to the server, returning the server allocation
159 * rectangle. The Cairo surface from prepare() must be destroyed
160 * after calling this.
161 */
162 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200163 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164 struct rectangle *server_allocation);
165
166 /*
167 * Make the toysurface current with the given EGL context.
168 * Returns 0 on success, and negative of failure.
169 */
170 int (*acquire)(struct toysurface *base, EGLContext ctx);
171
172 /*
173 * Release the toysurface from the EGL context, returning control
174 * to Cairo.
175 */
176 void (*release)(struct toysurface *base);
177
178 /*
179 * Destroy the toysurface, including the Cairo surface, any
180 * backing storage, and the Wayland protocol objects.
181 */
182 void (*destroy)(struct toysurface *base);
183};
184
Pekka Paalanen4e373742013-02-13 16:17:13 +0200185struct surface {
186 struct window *window;
187
188 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300189 struct wl_subsurface *subsurface;
190 int synchronized;
191 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200192 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200193 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300194 int redraw_needed;
195 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300196 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197
198 struct rectangle allocation;
199 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200200
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200201 struct wl_region *input_region;
202 struct wl_region *opaque_region;
203
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200204 enum window_buffer_type buffer_type;
205 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200206 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200207
208 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300209
210 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200211};
212
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500213struct window {
214 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100215 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500216 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200217 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400218 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500219 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500220 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400221 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300222 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400223 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400224 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500225 int custom;
226 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400227
Pekka Paalanen99436862012-11-19 17:15:59 +0200228 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400229
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500230 int fullscreen;
231 int maximized;
232
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200233 enum preferred_format preferred_format;
234
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500235 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500236 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400237 window_data_handler_t data_handler;
238 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500239 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400240 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200241 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700242 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400243
Pekka Paalanen4e373742013-02-13 16:17:13 +0200244 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500245 struct xdg_surface *xdg_surface;
246 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300247
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700248 struct window *parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500249
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500250 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500251
Pekka Paalanen35e82632013-04-25 13:57:48 +0300252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
254
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500255 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400256 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500257};
258
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500259struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500260 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200261 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300262 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500263 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400264 struct wl_list link;
265 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500270 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500271 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700272 widget_touch_down_handler_t touch_down_handler;
273 widget_touch_up_handler_t touch_up_handler;
274 widget_touch_motion_handler_t touch_motion_handler;
275 widget_touch_frame_handler_t touch_frame_handler;
276 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200277 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400278 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500279 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300280 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500281 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000282 /* If this is set to false then no cairo surface will be
283 * created before redrawing the surface. This is useful if the
284 * redraw handler is going to do completely custom rendering
285 * such as using EGL directly */
286 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400287};
288
Rusty Lynch041815a2013-08-08 21:20:38 -0700289struct touch_point {
290 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800291 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700292 struct widget *widget;
293 struct wl_list link;
294};
295
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296struct input {
297 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303 struct window *pointer_focus;
304 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300306 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300309 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400310 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400311 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400312 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400313 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400315
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500316 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500317 struct widget *grab;
318 uint32_t grab_button;
319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800323 uint32_t touch_grab;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100328
329 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
335 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400336
337 struct task repeat_task;
338 int repeat_timer_fd;
339 uint32_t repeat_sym;
340 uint32_t repeat_key;
341 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400342};
343
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500344struct output {
345 struct display *display;
346 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800347 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500348 struct rectangle allocation;
349 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600350 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200351 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500352 char *make;
353 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200354
355 display_output_handler_t destroy_handler;
356 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500357};
358
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500359struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500360 struct widget *widget;
361 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500362 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500363};
364
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500365struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400366 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500367 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500368 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500369 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700370 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500371 const char **entries;
372 uint32_t time;
373 int current;
374 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400375 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500376 menu_func_t func;
377};
378
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300379struct tooltip {
380 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300381 struct widget *widget;
382 char *entry;
383 struct task tooltip_task;
384 int tooltip_fd;
385 float x, y;
386};
387
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700388struct shm_pool {
389 struct wl_shm_pool *pool;
390 size_t size;
391 size_t used;
392 void *data;
393};
394
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400395enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300396 CURSOR_DEFAULT = 100,
397 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400398};
399
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200400static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400401
Pekka Paalanen97777442013-05-22 10:20:05 +0300402/* #define DEBUG */
403
404#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300405
406static void
407debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
408__attribute__ ((format (printf, 4, 5)));
409
410static void
411debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
412{
413 va_list ap;
414 struct timeval tv;
415
416 gettimeofday(&tv, NULL);
417 fprintf(stderr, "%8ld.%03ld ",
418 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
419
420 if (proxy)
421 fprintf(stderr, "%s@%d ",
422 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
423
424 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
425 fprintf(stderr, "%s ", func);
426
427 va_start(ap, fmt);
428 vfprintf(stderr, fmt, ap);
429 va_end(ap);
430}
431
432#define DBG(fmt, ...) \
433 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
434
435#define DBG_OBJ(obj, fmt, ...) \
436 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
437
438#else
439
440#define DBG(...) do {} while (0)
441#define DBG_OBJ(...) do {} while (0)
442
443#endif
444
Alexander Larsson1818e312013-05-22 14:41:31 +0200445static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200446surface_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 +0200447{
448 int32_t tmp;
449
450 switch (buffer_transform) {
451 case WL_OUTPUT_TRANSFORM_90:
452 case WL_OUTPUT_TRANSFORM_270:
453 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
454 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
455 tmp = *width;
456 *width = *height;
457 *height = tmp;
458 break;
459 default:
460 break;
461 }
462
463 *width *= buffer_scale;
464 *height *= buffer_scale;
465}
466
467static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200468buffer_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 +0200469{
470 int32_t tmp;
471
472 switch (buffer_transform) {
473 case WL_OUTPUT_TRANSFORM_90:
474 case WL_OUTPUT_TRANSFORM_270:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
477 tmp = *width;
478 *width = *height;
479 *height = tmp;
480 break;
481 default:
482 break;
483 }
484
485 *width /= buffer_scale;
486 *height /= buffer_scale;
487}
488
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500489#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200491struct egl_window_surface {
492 struct toysurface base;
493 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100494 struct display *display;
495 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200496 struct wl_egl_window *egl_window;
497 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100498};
499
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200500static struct egl_window_surface *
501to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100502{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200503 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100504}
505
506static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200507egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200508 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200509 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100510{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511 struct egl_window_surface *surface = to_egl_window_surface(base);
512
Alexander Larsson1818e312013-05-22 14:41:31 +0200513 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
514
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
516 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
517
518 return cairo_surface_reference(surface->cairo_surface);
519}
520
521static void
522egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200523 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200524 struct rectangle *server_allocation)
525{
526 struct egl_window_surface *surface = to_egl_window_surface(base);
527
528 cairo_gl_surface_swapbuffers(surface->cairo_surface);
529 wl_egl_window_get_attached_size(surface->egl_window,
530 &server_allocation->width,
531 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200532
533 buffer_to_surface_size (buffer_transform, buffer_scale,
534 &server_allocation->width,
535 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200536}
537
538static int
539egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
540{
541 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200542 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100543
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200544 device = cairo_surface_get_device(surface->cairo_surface);
545 if (!device)
546 return -1;
547
548 if (!ctx) {
549 if (device == surface->display->argb_device)
550 ctx = surface->display->argb_ctx;
551 else
552 assert(0);
553 }
554
555 cairo_device_flush(device);
556 cairo_device_acquire(device);
557 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
558 surface->egl_surface, ctx))
559 fprintf(stderr, "failed to make surface current\n");
560
561 return 0;
562}
563
564static void
565egl_window_surface_release(struct toysurface *base)
566{
567 struct egl_window_surface *surface = to_egl_window_surface(base);
568 cairo_device_t *device;
569
570 device = cairo_surface_get_device(surface->cairo_surface);
571 if (!device)
572 return;
573
574 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
575 surface->display->argb_ctx))
576 fprintf(stderr, "failed to make context current\n");
577
578 cairo_device_release(device);
579}
580
581static void
582egl_window_surface_destroy(struct toysurface *base)
583{
584 struct egl_window_surface *surface = to_egl_window_surface(base);
585 struct display *d = surface->display;
586
587 cairo_surface_destroy(surface->cairo_surface);
588 eglDestroySurface(d->dpy, surface->egl_surface);
589 wl_egl_window_destroy(surface->egl_window);
590 surface->surface = NULL;
591
592 free(surface);
593}
594
595static struct toysurface *
596egl_window_surface_create(struct display *display,
597 struct wl_surface *wl_surface,
598 uint32_t flags,
599 struct rectangle *rectangle)
600{
601 struct egl_window_surface *surface;
602
Pekka Paalanenb3627362012-11-19 17:16:00 +0200603 if (display->dpy == EGL_NO_DISPLAY)
604 return NULL;
605
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200606 surface = calloc(1, sizeof *surface);
607 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100608 return NULL;
609
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200610 surface->base.prepare = egl_window_surface_prepare;
611 surface->base.swap = egl_window_surface_swap;
612 surface->base.acquire = egl_window_surface_acquire;
613 surface->base.release = egl_window_surface_release;
614 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100615
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200616 surface->display = display;
617 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400618
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200619 surface->egl_window = wl_egl_window_create(surface->surface,
620 rectangle->width,
621 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100622
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200623 surface->egl_surface = eglCreateWindowSurface(display->dpy,
624 display->argb_config,
625 surface->egl_window,
626 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100627
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200628 surface->cairo_surface =
629 cairo_gl_surface_create_for_egl(display->argb_device,
630 surface->egl_surface,
631 rectangle->width,
632 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100633
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200634 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635}
636
Pekka Paalanenb3627362012-11-19 17:16:00 +0200637#else
638
639static struct toysurface *
640egl_window_surface_create(struct display *display,
641 struct wl_surface *wl_surface,
642 uint32_t flags,
643 struct rectangle *rectangle)
644{
645 return NULL;
646}
647
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400648#endif
649
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200650struct shm_surface_data {
651 struct wl_buffer *buffer;
652 struct shm_pool *pool;
653};
654
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400655struct wl_buffer *
656display_get_buffer_for_surface(struct display *display,
657 cairo_surface_t *surface)
658{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200659 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400660
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200661 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400662
663 return data->buffer;
664}
665
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500666static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700667shm_pool_destroy(struct shm_pool *pool);
668
669static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670shm_surface_data_destroy(void *p)
671{
672 struct shm_surface_data *data = p;
673
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200674 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700675 if (data->pool)
676 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300677
678 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400679}
680
Kristian Høgsberg16626282012-04-03 11:21:27 -0400681static struct wl_shm_pool *
682make_shm_pool(struct display *display, int size, void **data)
683{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400684 struct wl_shm_pool *pool;
685 int fd;
686
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300687 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400688 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300689 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
690 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400691 return NULL;
692 }
693
694 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400695 if (*data == MAP_FAILED) {
696 fprintf(stderr, "mmap failed: %m\n");
697 close(fd);
698 return NULL;
699 }
700
701 pool = wl_shm_create_pool(display->shm, fd, size);
702
703 close(fd);
704
705 return pool;
706}
707
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700708static struct shm_pool *
709shm_pool_create(struct display *display, size_t size)
710{
711 struct shm_pool *pool = malloc(sizeof *pool);
712
713 if (!pool)
714 return NULL;
715
716 pool->pool = make_shm_pool(display, size, &pool->data);
717 if (!pool->pool) {
718 free(pool);
719 return NULL;
720 }
721
722 pool->size = size;
723 pool->used = 0;
724
725 return pool;
726}
727
728static void *
729shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
730{
731 if (pool->used + size > pool->size)
732 return NULL;
733
734 *offset = pool->used;
735 pool->used += size;
736
737 return (char *) pool->data + *offset;
738}
739
740/* destroy the pool. this does not unmap the memory though */
741static void
742shm_pool_destroy(struct shm_pool *pool)
743{
744 munmap(pool->data, pool->size);
745 wl_shm_pool_destroy(pool->pool);
746 free(pool);
747}
748
749/* Start allocating from the beginning of the pool again */
750static void
751shm_pool_reset(struct shm_pool *pool)
752{
753 pool->used = 0;
754}
755
756static int
757data_length_for_shm_surface(struct rectangle *rect)
758{
759 int stride;
760
761 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
762 rect->width);
763 return stride * rect->height;
764}
765
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500766static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700767display_create_shm_surface_from_pool(struct display *display,
768 struct rectangle *rectangle,
769 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400770{
771 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400772 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400773 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200774 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700775 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400776 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400777
778 data = malloc(sizeof *data);
779 if (data == NULL)
780 return NULL;
781
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200782 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
783 cairo_format = CAIRO_FORMAT_RGB16_565;
784 else
785 cairo_format = CAIRO_FORMAT_ARGB32;
786
787 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700788 length = stride * rectangle->height;
789 data->pool = NULL;
790 map = shm_pool_allocate(pool, length, &offset);
791
792 if (!map) {
793 free(data);
794 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400795 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400796
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400797 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200798 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400799 rectangle->width,
800 rectangle->height,
801 stride);
802
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200803 cairo_surface_set_user_data(surface, &shm_surface_data_key,
804 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400805
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200806 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
807 format = WL_SHM_FORMAT_RGB565;
808 else {
809 if (flags & SURFACE_OPAQUE)
810 format = WL_SHM_FORMAT_XRGB8888;
811 else
812 format = WL_SHM_FORMAT_ARGB8888;
813 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400814
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200815 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
816 rectangle->width,
817 rectangle->height,
818 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400819
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700820 return surface;
821}
822
823static cairo_surface_t *
824display_create_shm_surface(struct display *display,
825 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200826 struct shm_pool *alternate_pool,
827 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700828{
829 struct shm_surface_data *data;
830 struct shm_pool *pool;
831 cairo_surface_t *surface;
832
Pekka Paalanen99436862012-11-19 17:15:59 +0200833 if (alternate_pool) {
834 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700835 surface = display_create_shm_surface_from_pool(display,
836 rectangle,
837 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200838 alternate_pool);
839 if (surface) {
840 data = cairo_surface_get_user_data(surface,
841 &shm_surface_data_key);
842 goto out;
843 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700844 }
845
846 pool = shm_pool_create(display,
847 data_length_for_shm_surface(rectangle));
848 if (!pool)
849 return NULL;
850
851 surface =
852 display_create_shm_surface_from_pool(display, rectangle,
853 flags, pool);
854
855 if (!surface) {
856 shm_pool_destroy(pool);
857 return NULL;
858 }
859
860 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200861 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700862 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400863
Pekka Paalanen99436862012-11-19 17:15:59 +0200864out:
865 if (data_ret)
866 *data_ret = data;
867
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400868 return surface;
869}
870
nobled7b87cb02011-02-01 18:51:47 +0000871static int
872check_size(struct rectangle *rect)
873{
874 if (rect->width && rect->height)
875 return 0;
876
877 fprintf(stderr, "tried to create surface of "
878 "width: %d, height: %d\n", rect->width, rect->height);
879 return -1;
880}
881
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400882cairo_surface_t *
883display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100884 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400885 struct rectangle *rectangle,
886 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400887{
nobled7b87cb02011-02-01 18:51:47 +0000888 if (check_size(rectangle) < 0)
889 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200890
891 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200892 return display_create_shm_surface(display, rectangle, flags,
893 NULL, NULL);
894}
895
Pekka Paalanena4eda732012-11-19 17:16:02 +0200896struct shm_surface_leaf {
897 cairo_surface_t *cairo_surface;
898 /* 'data' is automatically destroyed, when 'cairo_surface' is */
899 struct shm_surface_data *data;
900
901 struct shm_pool *resize_pool;
902 int busy;
903};
904
905static void
906shm_surface_leaf_release(struct shm_surface_leaf *leaf)
907{
908 if (leaf->cairo_surface)
909 cairo_surface_destroy(leaf->cairo_surface);
910 /* leaf->data already destroyed via cairo private */
911
912 if (leaf->resize_pool)
913 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200914
915 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200916}
917
Pekka Paalanenaef02542013-04-25 13:57:47 +0300918#define MAX_LEAVES 3
919
Pekka Paalanen99436862012-11-19 17:15:59 +0200920struct shm_surface {
921 struct toysurface base;
922 struct display *display;
923 struct wl_surface *surface;
924 uint32_t flags;
925 int dx, dy;
926
Pekka Paalanenaef02542013-04-25 13:57:47 +0300927 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200928 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200929};
930
931static struct shm_surface *
932to_shm_surface(struct toysurface *base)
933{
934 return container_of(base, struct shm_surface, base);
935}
936
Pekka Paalanena4eda732012-11-19 17:16:02 +0200937static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300938shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
939{
940#ifdef DEBUG
941 struct shm_surface_leaf *leaf;
942 char bufs[MAX_LEAVES + 1];
943 int i;
944
945 for (i = 0; i < MAX_LEAVES; i++) {
946 leaf = &surface->leaf[i];
947
948 if (leaf->busy)
949 bufs[i] = 'b';
950 else if (leaf->cairo_surface)
951 bufs[i] = 'a';
952 else
953 bufs[i] = ' ';
954 }
955
956 bufs[MAX_LEAVES] = '\0';
957 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
958#endif
959}
960
961static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200962shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
963{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200964 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300965 struct shm_surface_leaf *leaf;
966 int i;
967 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300968
969 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200970
Pekka Paalanenaef02542013-04-25 13:57:47 +0300971 for (i = 0; i < MAX_LEAVES; i++) {
972 leaf = &surface->leaf[i];
973 if (leaf->data && leaf->data->buffer == buffer) {
974 leaf->busy = 0;
975 break;
976 }
977 }
978 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200979
Pekka Paalanenaef02542013-04-25 13:57:47 +0300980 /* Leave one free leaf with storage, release others */
981 free_found = 0;
982 for (i = 0; i < MAX_LEAVES; i++) {
983 leaf = &surface->leaf[i];
984
985 if (!leaf->cairo_surface || leaf->busy)
986 continue;
987
988 if (!free_found)
989 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300990 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300991 shm_surface_leaf_release(leaf);
992 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300993
Pekka Paalanen97777442013-05-22 10:20:05 +0300994 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200995}
996
997static const struct wl_buffer_listener shm_surface_buffer_listener = {
998 shm_surface_buffer_release
999};
1000
Pekka Paalanen99436862012-11-19 17:15:59 +02001001static cairo_surface_t *
1002shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001003 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001004 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001005{
Pekka Paalanenec076692012-11-30 13:37:27 +02001006 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001007 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001008 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001009 struct shm_surface_leaf *leaf = NULL;
1010 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001011
1012 surface->dx = dx;
1013 surface->dy = dy;
1014
Pekka Paalanenaef02542013-04-25 13:57:47 +03001015 /* pick a free buffer, preferrably one that already has storage */
1016 for (i = 0; i < MAX_LEAVES; i++) {
1017 if (surface->leaf[i].busy)
1018 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001019
Pekka Paalanenaef02542013-04-25 13:57:47 +03001020 if (!leaf || surface->leaf[i].cairo_surface)
1021 leaf = &surface->leaf[i];
1022 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001023 DBG_OBJ(surface->surface, "pick leaf %d\n",
1024 (int)(leaf - &surface->leaf[0]));
1025
Pekka Paalanenaef02542013-04-25 13:57:47 +03001026 if (!leaf) {
1027 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001028 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001029 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001030 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001031 }
1032
Pekka Paalanena4eda732012-11-19 17:16:02 +02001033 if (!resize_hint && leaf->resize_pool) {
1034 cairo_surface_destroy(leaf->cairo_surface);
1035 leaf->cairo_surface = NULL;
1036 shm_pool_destroy(leaf->resize_pool);
1037 leaf->resize_pool = NULL;
1038 }
1039
Alexander Larsson1818e312013-05-22 14:41:31 +02001040 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1041
Pekka Paalanena4eda732012-11-19 17:16:02 +02001042 if (leaf->cairo_surface &&
1043 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1044 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001045 goto out;
1046
Pekka Paalanena4eda732012-11-19 17:16:02 +02001047 if (leaf->cairo_surface)
1048 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001049
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001050#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001051 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001052 /* Create a big pool to allocate from, while continuously
1053 * resizing. Mmapping a new pool in the server
1054 * is relatively expensive, so reusing a pool performs
1055 * better, but may temporarily reserve unneeded memory.
1056 */
1057 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001058 leaf->resize_pool = shm_pool_create(surface->display,
1059 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001060 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001061#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001062
Alexander Larsson1818e312013-05-22 14:41:31 +02001063 rect.width = width;
1064 rect.height = height;
1065
Pekka Paalanena4eda732012-11-19 17:16:02 +02001066 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001067 display_create_shm_surface(surface->display, &rect,
1068 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 leaf->resize_pool,
1070 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001071 if (!leaf->cairo_surface)
1072 return NULL;
1073
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001075 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
1077out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001078 surface->current = leaf;
1079
1080 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001081}
1082
1083static void
1084shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001085 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001086 struct rectangle *server_allocation)
1087{
1088 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001089 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001090
1091 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001092 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001093 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001094 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001095
Alexander Larsson1818e312013-05-22 14:41:31 +02001096 buffer_to_surface_size (buffer_transform, buffer_scale,
1097 &server_allocation->width,
1098 &server_allocation->height);
1099
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001101 surface->dx, surface->dy);
1102 wl_surface_damage(surface->surface, 0, 0,
1103 server_allocation->width, server_allocation->height);
1104 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105
Pekka Paalanen71233882013-04-25 13:57:53 +03001106 DBG_OBJ(surface->surface, "leaf %d busy\n",
1107 (int)(leaf - &surface->leaf[0]));
1108
Pekka Paalanena4eda732012-11-19 17:16:02 +02001109 leaf->busy = 1;
1110 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001111}
1112
1113static int
1114shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1115{
1116 return -1;
1117}
1118
1119static void
1120shm_surface_release(struct toysurface *base)
1121{
1122}
1123
1124static void
1125shm_surface_destroy(struct toysurface *base)
1126{
1127 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001128 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001129
Pekka Paalanenaef02542013-04-25 13:57:47 +03001130 for (i = 0; i < MAX_LEAVES; i++)
1131 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001132
1133 free(surface);
1134}
1135
1136static struct toysurface *
1137shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1138 uint32_t flags, struct rectangle *rectangle)
1139{
1140 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001141 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001142
Brian Lovinbc919262013-08-07 15:34:59 -07001143 surface = xmalloc(sizeof *surface);
1144 memset(surface, 0, sizeof *surface);
1145
Pekka Paalanen99436862012-11-19 17:15:59 +02001146 if (!surface)
1147 return NULL;
1148
1149 surface->base.prepare = shm_surface_prepare;
1150 surface->base.swap = shm_surface_swap;
1151 surface->base.acquire = shm_surface_acquire;
1152 surface->base.release = shm_surface_release;
1153 surface->base.destroy = shm_surface_destroy;
1154
1155 surface->display = display;
1156 surface->surface = wl_surface;
1157 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001158
1159 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001160}
1161
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001162/*
1163 * The following correspondences between file names and cursors was copied
1164 * from: https://bugs.kde.org/attachment.cgi?id=67313
1165 */
1166
1167static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001168 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001169 "sw-resize",
1170 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001171};
1172
1173static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001174 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001175 "se-resize",
1176 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001177};
1178
1179static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001180 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001181 "s-resize",
1182 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001183};
1184
1185static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001186 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001187 "closedhand",
1188 "208530c400c041818281048008011002"
1189};
1190
1191static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001192 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001193 "default",
1194 "top_left_arrow",
1195 "left-arrow"
1196};
1197
1198static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001199 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001200 "w-resize",
1201 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001202};
1203
1204static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001205 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001206 "e-resize",
1207 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001208};
1209
1210static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001211 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001212 "nw-resize",
1213 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001214};
1215
1216static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001217 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001218 "ne-resize",
1219 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001220};
1221
1222static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001223 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001224 "n-resize",
1225 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001226};
1227
1228static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001229 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230 "ibeam",
1231 "text"
1232};
1233
1234static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001235 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236 "pointer",
1237 "pointing_hand",
1238 "e29285e634086352946a0e7090d73106"
1239};
1240
1241static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001242 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001243 "wait",
1244 "0426c94ea35c87780ff01dc239897213"
1245};
1246
1247struct cursor_alternatives {
1248 const char **names;
1249 size_t count;
1250};
1251
1252static const struct cursor_alternatives cursors[] = {
1253 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1254 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1255 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1256 {grabbings, ARRAY_LENGTH(grabbings)},
1257 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1258 {left_sides, ARRAY_LENGTH(left_sides)},
1259 {right_sides, ARRAY_LENGTH(right_sides)},
1260 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1261 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1262 {top_sides, ARRAY_LENGTH(top_sides)},
1263 {xterms, ARRAY_LENGTH(xterms)},
1264 {hand1s, ARRAY_LENGTH(hand1s)},
1265 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001266};
1267
1268static void
1269create_cursors(struct display *display)
1270{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001271 struct weston_config *config;
1272 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001273 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001274 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001275 unsigned int i, j;
1276 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001277
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001278 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001279 s = weston_config_get_section(config, "shell", NULL, NULL);
1280 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1281 weston_config_section_get_int(s, "cursor-size", &size, 32);
1282 weston_config_destroy(config);
1283
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001284 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001285 if (!display->cursor_theme) {
1286 fprintf(stderr, "could not load theme '%s'\n", theme);
1287 return;
1288 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001289 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001290 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001291 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001292
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001293 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001294 cursor = NULL;
1295 for (j = 0; !cursor && j < cursors[i].count; ++j)
1296 cursor = wl_cursor_theme_get_cursor(
1297 display->cursor_theme, cursors[i].names[j]);
1298
1299 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001300 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001301 cursors[i].names[0]);
1302
1303 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001304 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001305}
1306
1307static void
1308destroy_cursors(struct display *display)
1309{
1310 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001311 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001312}
1313
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001314struct wl_cursor_image *
1315display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001316{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001317 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001318
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001319 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001320}
1321
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001322static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001323surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001324{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001325 if (!surface->cairo_surface)
1326 return;
1327
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001328 if (surface->opaque_region) {
1329 wl_surface_set_opaque_region(surface->surface,
1330 surface->opaque_region);
1331 wl_region_destroy(surface->opaque_region);
1332 surface->opaque_region = NULL;
1333 }
1334
1335 if (surface->input_region) {
1336 wl_surface_set_input_region(surface->surface,
1337 surface->input_region);
1338 wl_region_destroy(surface->input_region);
1339 surface->input_region = NULL;
1340 }
1341
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001342 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001343 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001344 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001345
Pekka Paalanen89dee002013-02-13 16:17:20 +02001346 cairo_surface_destroy(surface->cairo_surface);
1347 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001348}
1349
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001350int
1351window_has_focus(struct window *window)
1352{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001353 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001354}
1355
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001356static void
1357window_close(struct window *window)
1358{
1359 if (window->close_handler)
1360 window->close_handler(window->user_data);
1361 else
1362 display_exit(window->display);
1363}
1364
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001365struct display *
1366window_get_display(struct window *window)
1367{
1368 return window->display;
1369}
1370
Pekka Paalanen89dee002013-02-13 16:17:20 +02001371static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001372surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001373{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001374 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001375 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001376
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001377 if (!surface->toysurface && display->dpy &&
1378 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001379 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001380 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001381 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001382 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001383 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001384 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001385
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001386 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001387 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001388 surface->surface,
1389 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001390
Pekka Paalanen89dee002013-02-13 16:17:20 +02001391 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001392 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001393 allocation.width, allocation.height, flags,
1394 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001395}
1396
1397static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001398window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001399{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001400 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001401 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001402
Pekka Paalanenec076692012-11-30 13:37:27 +02001403 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001404 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001405
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001406 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1407 flags |= SURFACE_HINT_RGB565;
1408
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001409 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001410}
1411
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001412int
1413window_get_buffer_transform(struct window *window)
1414{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001415 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001416}
1417
1418void
1419window_set_buffer_transform(struct window *window,
1420 enum wl_output_transform transform)
1421{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001422 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001423 wl_surface_set_buffer_transform(window->main_surface->surface,
1424 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001425}
1426
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001427void
1428window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001429 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001430{
1431 window->main_surface->buffer_scale = scale;
1432 wl_surface_set_buffer_scale(window->main_surface->surface,
1433 scale);
1434}
1435
1436uint32_t
1437window_get_buffer_scale(struct window *window)
1438{
1439 return window->main_surface->buffer_scale;
1440}
1441
Alexander Larssond68f5232013-05-22 14:41:33 +02001442uint32_t
1443window_get_output_scale(struct window *window)
1444{
1445 struct window_output *window_output;
1446 struct window_output *window_output_tmp;
1447 int scale = 1;
1448
1449 wl_list_for_each_safe(window_output, window_output_tmp,
1450 &window->window_output_list, link) {
1451 if (window_output->output->scale > scale)
1452 scale = window_output->output->scale;
1453 }
1454
1455 return scale;
1456}
1457
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001458static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001459
Pekka Paalanen4e373742013-02-13 16:17:13 +02001460static void
1461surface_destroy(struct surface *surface)
1462{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001463 if (surface->frame_cb)
1464 wl_callback_destroy(surface->frame_cb);
1465
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001466 if (surface->input_region)
1467 wl_region_destroy(surface->input_region);
1468
1469 if (surface->opaque_region)
1470 wl_region_destroy(surface->opaque_region);
1471
Pekka Paalanen35e82632013-04-25 13:57:48 +03001472 if (surface->subsurface)
1473 wl_subsurface_destroy(surface->subsurface);
1474
Pekka Paalanen4e373742013-02-13 16:17:13 +02001475 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001476
1477 if (surface->toysurface)
1478 surface->toysurface->destroy(surface->toysurface);
1479
Pekka Paalanen35e82632013-04-25 13:57:48 +03001480 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001481 free(surface);
1482}
1483
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001484void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001485window_destroy(struct window *window)
1486{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001487 struct display *display = window->display;
1488 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001489 struct window_output *window_output;
1490 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001491
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001492 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001493
Rusty Lynch1084da52013-08-15 09:10:08 -07001494 wl_list_for_each(input, &display->input_list, link) {
1495 if (input->touch_focus == window)
1496 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001497 if (input->pointer_focus == window)
1498 input->pointer_focus = NULL;
1499 if (input->keyboard_focus == window)
1500 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001501 if (input->focus_widget &&
1502 input->focus_widget->window == window)
1503 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001504 }
1505
Rob Bradford7507b572012-05-15 17:55:34 +01001506 wl_list_for_each_safe(window_output, window_output_tmp,
1507 &window->window_output_list, link) {
1508 free (window_output);
1509 }
1510
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001511 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001512 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001513
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001514 if (window->xdg_surface)
1515 xdg_surface_destroy(window->xdg_surface);
1516 if (window->xdg_popup)
1517 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001518
1519 surface_destroy(window->main_surface);
1520
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001521 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001522
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001523 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001524 free(window);
1525}
1526
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001527static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001528widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001529{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001530 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001531
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001532 wl_list_for_each(child, &widget->child_list, link) {
1533 target = widget_find_widget(child, x, y);
1534 if (target)
1535 return target;
1536 }
1537
1538 if (widget->allocation.x <= x &&
1539 x < widget->allocation.x + widget->allocation.width &&
1540 widget->allocation.y <= y &&
1541 y < widget->allocation.y + widget->allocation.height) {
1542 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001543 }
1544
1545 return NULL;
1546}
1547
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001548static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001549window_find_widget(struct window *window, int32_t x, int32_t y)
1550{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001551 struct surface *surface;
1552 struct widget *widget;
1553
1554 wl_list_for_each(surface, &window->subsurface_list, link) {
1555 widget = widget_find_widget(surface->widget, x, y);
1556 if (widget)
1557 return widget;
1558 }
1559
1560 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001561}
1562
1563static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001564widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001565{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001566 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001567
Peter Huttererf3d62272013-08-08 11:57:05 +10001568 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001569 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001570 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001571 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001572 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001573 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001574 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001575 widget->tooltip = NULL;
1576 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001577 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001578 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001579
1580 return widget;
1581}
1582
1583struct widget *
1584window_add_widget(struct window *window, void *data)
1585{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001586 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001588 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001589 wl_list_init(&widget->link);
1590 window->main_surface->widget = widget;
1591
1592 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001593}
1594
1595struct widget *
1596widget_add_widget(struct widget *parent, void *data)
1597{
1598 struct widget *widget;
1599
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001600 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001601 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001602
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001603 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001604}
1605
1606void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001607widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001608{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001609 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001610 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001611 struct input *input;
1612
Pekka Paalanen35e82632013-04-25 13:57:48 +03001613 /* Destroy the sub-surface along with the root widget */
1614 if (surface->widget == widget && surface->subsurface)
1615 surface_destroy(widget->surface);
1616
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001617 if (widget->tooltip)
1618 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001619
Pekka Paalanene156fb62012-01-19 13:51:38 +02001620 wl_list_for_each(input, &display->input_list, link) {
1621 if (input->focus_widget == widget)
1622 input->focus_widget = NULL;
1623 }
1624
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001625 wl_list_remove(&widget->link);
1626 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001627}
1628
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001629void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001630widget_set_default_cursor(struct widget *widget, int cursor)
1631{
1632 widget->default_cursor = cursor;
1633}
1634
1635void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001636widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001637{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001638 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001639}
1640
1641void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001642widget_set_size(struct widget *widget, int32_t width, int32_t height)
1643{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001644 widget->allocation.width = width;
1645 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001646}
1647
1648void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001649widget_set_allocation(struct widget *widget,
1650 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001651{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001652 widget->allocation.x = x;
1653 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001654 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001655}
1656
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001657void
1658widget_set_transparent(struct widget *widget, int transparent)
1659{
1660 widget->opaque = !transparent;
1661}
1662
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001664widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001665{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001666 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001667}
1668
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001669static cairo_surface_t *
1670widget_get_cairo_surface(struct widget *widget)
1671{
1672 struct surface *surface = widget->surface;
1673 struct window *window = widget->window;
1674
Neil Roberts97b747c2013-12-19 16:17:12 +00001675 assert(widget->use_cairo);
1676
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001677 if (!surface->cairo_surface) {
1678 if (surface == window->main_surface)
1679 window_create_main_surface(window);
1680 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001681 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001682 }
1683
1684 return surface->cairo_surface;
1685}
1686
Alexander Larsson15901f02013-05-22 14:41:25 +02001687static void
1688widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1689{
1690 struct surface *surface = widget->surface;
1691 double angle;
1692 cairo_matrix_t m;
1693 enum wl_output_transform transform;
1694 int surface_width, surface_height;
1695 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001696 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001697
1698 surface_width = surface->allocation.width;
1699 surface_height = surface->allocation.height;
1700
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001701 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001702 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001703
Alexander Larsson15901f02013-05-22 14:41:25 +02001704 switch (transform) {
1705 case WL_OUTPUT_TRANSFORM_FLIPPED:
1706 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1707 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1708 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1709 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1710 break;
1711 default:
1712 cairo_matrix_init_identity(&m);
1713 break;
1714 }
1715
1716 switch (transform) {
1717 case WL_OUTPUT_TRANSFORM_NORMAL:
1718 default:
1719 angle = 0;
1720 translate_x = 0;
1721 translate_y = 0;
1722 break;
1723 case WL_OUTPUT_TRANSFORM_FLIPPED:
1724 angle = 0;
1725 translate_x = surface_width;
1726 translate_y = 0;
1727 break;
1728 case WL_OUTPUT_TRANSFORM_90:
1729 angle = M_PI_2;
1730 translate_x = surface_height;
1731 translate_y = 0;
1732 break;
1733 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1734 angle = M_PI_2;
1735 translate_x = surface_height;
1736 translate_y = surface_width;
1737 break;
1738 case WL_OUTPUT_TRANSFORM_180:
1739 angle = M_PI;
1740 translate_x = surface_width;
1741 translate_y = surface_height;
1742 break;
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1744 angle = M_PI;
1745 translate_x = 0;
1746 translate_y = surface_height;
1747 break;
1748 case WL_OUTPUT_TRANSFORM_270:
1749 angle = M_PI + M_PI_2;
1750 translate_x = 0;
1751 translate_y = surface_width;
1752 break;
1753 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1754 angle = M_PI + M_PI_2;
1755 translate_x = 0;
1756 translate_y = 0;
1757 break;
1758 }
1759
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001760 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001761 cairo_translate(cr, translate_x, translate_y);
1762 cairo_rotate(cr, angle);
1763 cairo_transform(cr, &m);
1764}
1765
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001766cairo_t *
1767widget_cairo_create(struct widget *widget)
1768{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001769 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001770 cairo_surface_t *cairo_surface;
1771 cairo_t *cr;
1772
1773 cairo_surface = widget_get_cairo_surface(widget);
1774 cr = cairo_create(cairo_surface);
1775
Alexander Larsson15901f02013-05-22 14:41:25 +02001776 widget_cairo_update_transform(widget, cr);
1777
Pekka Paalanen35e82632013-04-25 13:57:48 +03001778 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1779
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001780 return cr;
1781}
1782
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001783struct wl_surface *
1784widget_get_wl_surface(struct widget *widget)
1785{
1786 return widget->surface->surface;
1787}
1788
Neil Roberts5e10a042013-09-09 00:40:17 +01001789struct wl_subsurface *
1790widget_get_wl_subsurface(struct widget *widget)
1791{
1792 return widget->surface->subsurface;
1793}
1794
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001795uint32_t
1796widget_get_last_time(struct widget *widget)
1797{
1798 return widget->surface->last_time;
1799}
1800
1801void
1802widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1803{
1804 struct wl_compositor *comp = widget->window->display->compositor;
1805 struct surface *surface = widget->surface;
1806
1807 if (!surface->input_region)
1808 surface->input_region = wl_compositor_create_region(comp);
1809
1810 if (rect) {
1811 wl_region_add(surface->input_region,
1812 rect->x, rect->y, rect->width, rect->height);
1813 }
1814}
1815
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001816void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001817widget_set_resize_handler(struct widget *widget,
1818 widget_resize_handler_t handler)
1819{
1820 widget->resize_handler = handler;
1821}
1822
1823void
1824widget_set_redraw_handler(struct widget *widget,
1825 widget_redraw_handler_t handler)
1826{
1827 widget->redraw_handler = handler;
1828}
1829
1830void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001831widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001832{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001833 widget->enter_handler = handler;
1834}
1835
1836void
1837widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1838{
1839 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001840}
1841
1842void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001843widget_set_motion_handler(struct widget *widget,
1844 widget_motion_handler_t handler)
1845{
1846 widget->motion_handler = handler;
1847}
1848
1849void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001850widget_set_button_handler(struct widget *widget,
1851 widget_button_handler_t handler)
1852{
1853 widget->button_handler = handler;
1854}
1855
1856void
Rusty Lynch041815a2013-08-08 21:20:38 -07001857widget_set_touch_up_handler(struct widget *widget,
1858 widget_touch_up_handler_t handler)
1859{
1860 widget->touch_up_handler = handler;
1861}
1862
1863void
1864widget_set_touch_down_handler(struct widget *widget,
1865 widget_touch_down_handler_t handler)
1866{
1867 widget->touch_down_handler = handler;
1868}
1869
1870void
1871widget_set_touch_motion_handler(struct widget *widget,
1872 widget_touch_motion_handler_t handler)
1873{
1874 widget->touch_motion_handler = handler;
1875}
1876
1877void
1878widget_set_touch_frame_handler(struct widget *widget,
1879 widget_touch_frame_handler_t handler)
1880{
1881 widget->touch_frame_handler = handler;
1882}
1883
1884void
1885widget_set_touch_cancel_handler(struct widget *widget,
1886 widget_touch_cancel_handler_t handler)
1887{
1888 widget->touch_cancel_handler = handler;
1889}
1890
1891void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001892widget_set_axis_handler(struct widget *widget,
1893 widget_axis_handler_t handler)
1894{
1895 widget->axis_handler = handler;
1896}
1897
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001898static void
1899window_schedule_redraw_task(struct window *window);
1900
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001901void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001902widget_schedule_redraw(struct widget *widget)
1903{
Pekka Paalanen71233882013-04-25 13:57:53 +03001904 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001905 widget->surface->redraw_needed = 1;
1906 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001907}
1908
Neil Roberts97b747c2013-12-19 16:17:12 +00001909void
1910widget_set_use_cairo(struct widget *widget,
1911 int use_cairo)
1912{
1913 widget->use_cairo = use_cairo;
1914}
1915
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001916cairo_surface_t *
1917window_get_surface(struct window *window)
1918{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001919 cairo_surface_t *cairo_surface;
1920
1921 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1922
1923 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001924}
1925
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001926struct wl_surface *
1927window_get_wl_surface(struct window *window)
1928{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001929 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001930}
1931
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001932static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001933tooltip_redraw_handler(struct widget *widget, void *data)
1934{
1935 cairo_t *cr;
1936 const int32_t r = 3;
1937 struct tooltip *tooltip = data;
1938 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001939
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001940 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001941 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001942 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1943 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1944 cairo_paint(cr);
1945
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001946 width = widget->allocation.width;
1947 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001948 rounded_rect(cr, 0, 0, width, height, r);
1949
1950 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1951 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1952 cairo_fill(cr);
1953
1954 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1955 cairo_move_to(cr, 10, 16);
1956 cairo_show_text(cr, tooltip->entry);
1957 cairo_destroy(cr);
1958}
1959
1960static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001961get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001962{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001963 cairo_t *cr;
1964 cairo_text_extents_t extents;
1965
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001966 /* Use the dummy_surface because tooltip's surface was not
1967 * created yet, and parent does not have a valid surface
1968 * outside repaint, either.
1969 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001970 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001971 cairo_text_extents(cr, tooltip->entry, &extents);
1972 cairo_destroy(cr);
1973
1974 return extents;
1975}
1976
1977static int
1978window_create_tooltip(struct tooltip *tooltip)
1979{
1980 struct widget *parent = tooltip->parent;
1981 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001982 const int offset_y = 27;
1983 const int margin = 3;
1984 cairo_text_extents_t extents;
1985
1986 if (tooltip->widget)
1987 return 0;
1988
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001989 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001990
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001991 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001992 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001993 widget_set_allocation(tooltip->widget,
1994 tooltip->x, tooltip->y + offset_y,
1995 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001996
1997 return 0;
1998}
1999
2000void
2001widget_destroy_tooltip(struct widget *parent)
2002{
2003 struct tooltip *tooltip = parent->tooltip;
2004
2005 parent->tooltip_count = 0;
2006 if (!tooltip)
2007 return;
2008
2009 if (tooltip->widget) {
2010 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002011 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002012 }
2013
2014 close(tooltip->tooltip_fd);
2015 free(tooltip->entry);
2016 free(tooltip);
2017 parent->tooltip = NULL;
2018}
2019
2020static void
2021tooltip_func(struct task *task, uint32_t events)
2022{
2023 struct tooltip *tooltip =
2024 container_of(task, struct tooltip, tooltip_task);
2025 uint64_t exp;
2026
Martin Olsson8df662a2012-07-08 03:03:47 +02002027 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2028 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002029 window_create_tooltip(tooltip);
2030}
2031
2032#define TOOLTIP_TIMEOUT 500
2033static int
2034tooltip_timer_reset(struct tooltip *tooltip)
2035{
2036 struct itimerspec its;
2037
2038 its.it_interval.tv_sec = 0;
2039 its.it_interval.tv_nsec = 0;
2040 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2041 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2042 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2043 fprintf(stderr, "could not set timerfd\n: %m");
2044 return -1;
2045 }
2046
2047 return 0;
2048}
2049
2050int
2051widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2052{
2053 struct tooltip *tooltip = parent->tooltip;
2054
2055 parent->tooltip_count++;
2056 if (tooltip) {
2057 tooltip->x = x;
2058 tooltip->y = y;
2059 tooltip_timer_reset(tooltip);
2060 return 0;
2061 }
2062
2063 /* the handler might be triggered too fast via input device motion, so
2064 * we need this check here to make sure tooltip is fully initialized */
2065 if (parent->tooltip_count > 1)
2066 return 0;
2067
2068 tooltip = malloc(sizeof *tooltip);
2069 if (!tooltip)
2070 return -1;
2071
2072 parent->tooltip = tooltip;
2073 tooltip->parent = parent;
2074 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002075 tooltip->x = x;
2076 tooltip->y = y;
2077 tooltip->entry = strdup(entry);
2078 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2079 if (tooltip->tooltip_fd < 0) {
2080 fprintf(stderr, "could not create timerfd\n: %m");
2081 return -1;
2082 }
2083
2084 tooltip->tooltip_task.run = tooltip_func;
2085 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2086 EPOLLIN, &tooltip->tooltip_task);
2087 tooltip_timer_reset(tooltip);
2088
2089 return 0;
2090}
2091
2092static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002093workspace_manager_state(void *data,
2094 struct workspace_manager *workspace_manager,
2095 uint32_t current,
2096 uint32_t count)
2097{
2098 struct display *display = data;
2099
2100 display->workspace = current;
2101 display->workspace_count = count;
2102}
2103
2104static const struct workspace_manager_listener workspace_manager_listener = {
2105 workspace_manager_state
2106};
2107
2108static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002109frame_resize_handler(struct widget *widget,
2110 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002111{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002112 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002113 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002114 struct rectangle interior;
2115 struct rectangle input;
2116 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002117
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002118 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002119 interior.x = 0;
2120 interior.y = 0;
2121 interior.width = width;
2122 interior.height = height;
2123 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002124 frame_resize(frame->frame, width, height);
2125 frame_interior(frame->frame, &interior.x, &interior.y,
2126 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002127 }
2128
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002129 widget_set_allocation(child, interior.x, interior.y,
2130 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002131
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002132 if (child->resize_handler) {
2133 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002134 child->user_data);
2135
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002136 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002137 width = child->allocation.width;
2138 height = child->allocation.height;
2139 } else {
2140 frame_resize_inside(frame->frame,
2141 child->allocation.width,
2142 child->allocation.height);
2143 width = frame_width(frame->frame);
2144 height = frame_height(frame->frame);
2145 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002146 }
2147
Scott Moreauf7e498c2012-05-14 11:39:29 -06002148 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002149
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002150 widget->surface->input_region =
2151 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002152 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002153 frame_input_rect(frame->frame, &input.x, &input.y,
2154 &input.width, &input.height);
2155 wl_region_add(widget->surface->input_region,
2156 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002157 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002158 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002159 }
2160
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002161 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002162
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002163 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002164 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002165 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2166 &opaque.width, &opaque.height);
2167
2168 wl_region_add(widget->surface->opaque_region,
2169 opaque.x, opaque.y,
2170 opaque.width, opaque.height);
2171 } else {
2172 wl_region_add(widget->surface->opaque_region,
2173 0, 0, width, height);
2174 }
Martin Minarik1998b152012-05-10 02:04:35 +02002175 }
2176
Martin Minarik1998b152012-05-10 02:04:35 +02002177
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002178 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002179}
2180
2181static void
2182frame_redraw_handler(struct widget *widget, void *data)
2183{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002184 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002185 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002186 struct window *window = widget->window;
2187
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002188 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002189 return;
2190
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002191 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194
2195 cairo_destroy(cr);
2196}
2197
2198static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002199frame_get_pointer_image_for_location(struct window_frame *frame,
2200 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002201{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002202 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002203
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002204 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002205 return CURSOR_LEFT_PTR;
2206
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002207 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002208 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002209 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002210 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002211 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002212 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002213 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002214 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002215 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002216 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002217 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002218 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002219 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002220 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002221 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002222 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002223 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002224 case THEME_LOCATION_EXTERIOR:
2225 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002226 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002227 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002228 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002229}
2230
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002231static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002232frame_menu_func(void *data, struct input *input, int index)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002233{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002234 struct window *window = data;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002235 struct display *display;
2236
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002237 switch (index) {
2238 case 0: /* close */
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002239 window_close(window);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002240 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002241 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002242 display = window->display;
2243 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002244 workspace_manager_move_surface(
2245 display->workspace_manager,
2246 window->main_surface->surface,
2247 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002248 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002249 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002250 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002251 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002252 workspace_manager_move_surface(
2253 display->workspace_manager,
2254 window->main_surface->surface,
2255 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002256 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002257 case 3: /* fullscreen */
2258 /* we don't have a way to get out of fullscreen for now */
2259 if (window->fullscreen_handler)
2260 window->fullscreen_handler(window, window->user_data);
2261 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002262 }
2263}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002264
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002265void
2266window_show_frame_menu(struct window *window,
2267 struct input *input, uint32_t time)
2268{
2269 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002270 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002271
2272 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002273 "Close",
2274 "Move to workspace above", "Move to workspace below",
2275 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002276 };
2277
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002278 if (window->fullscreen_handler)
2279 count = ARRAY_LENGTH(entries);
2280 else
2281 count = ARRAY_LENGTH(entries) - 1;
2282
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002283 input_get_position(input, &x, &y);
2284 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002285 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002286}
2287
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002288static int
2289frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002290 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002291{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002292 struct window_frame *frame = data;
2293 enum theme_location location;
2294
2295 location = frame_pointer_enter(frame->frame, input, x, y);
2296 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2297 widget_schedule_redraw(frame->widget);
2298
2299 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002300}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002301
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002302static int
2303frame_motion_handler(struct widget *widget,
2304 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002305 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002306{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002307 struct window_frame *frame = data;
2308 enum theme_location location;
2309
2310 location = frame_pointer_motion(frame->frame, input, x, y);
2311 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2312 widget_schedule_redraw(frame->widget);
2313
2314 return frame_get_pointer_image_for_location(data, location);
2315}
2316
2317static void
2318frame_leave_handler(struct widget *widget,
2319 struct input *input, void *data)
2320{
2321 struct window_frame *frame = data;
2322
2323 frame_pointer_leave(frame->frame, input);
2324 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2325 widget_schedule_redraw(frame->widget);
2326}
2327
2328static void
2329frame_handle_status(struct window_frame *frame, struct input *input,
2330 uint32_t time, enum theme_location location)
2331{
2332 struct window *window = frame->widget->window;
2333 uint32_t status;
2334
2335 status = frame_status(frame->frame);
2336 if (status & FRAME_STATUS_REPAINT)
2337 widget_schedule_redraw(frame->widget);
2338
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002339 if (status & FRAME_STATUS_MINIMIZE) {
2340 window_set_minimized(window);
2341 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2342 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002343
2344 if (status & FRAME_STATUS_MENU) {
2345 window_show_frame_menu(window, input, time);
2346 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2347 }
2348
2349 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002350 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002351 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2352 }
2353
2354 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002355 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002356 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002357 }
2358
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002359 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002360 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002361 xdg_surface_move(window->xdg_surface,
2362 input_get_seat(input),
2363 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002364
2365 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2366 }
2367
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002368 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002369 input_ungrab(input);
2370
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002371 xdg_surface_resize(window->xdg_surface,
2372 input_get_seat(input),
2373 window->display->serial,
2374 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002375
2376 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2377 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002378}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002379
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002380static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002381frame_button_handler(struct widget *widget,
2382 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002383 uint32_t button, enum wl_pointer_button_state state,
2384 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002385
2386{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002387 struct window_frame *frame = data;
2388 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002389
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002390 location = frame_pointer_button(frame->frame, input, button, state);
2391 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002392}
2393
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002394static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002395frame_touch_down_handler(struct widget *widget, struct input *input,
2396 uint32_t serial, uint32_t time, int32_t id,
2397 float x, float y, void *data)
2398{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002399 struct window_frame *frame = data;
2400
2401 frame_touch_down(frame->frame, input, id, x, y);
2402 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2403}
2404
2405static void
2406frame_touch_up_handler(struct widget *widget,
2407 struct input *input, uint32_t serial, uint32_t time,
2408 int32_t id, void *data)
2409{
2410 struct window_frame *frame = data;
2411
2412 frame_touch_up(frame->frame, input, id);
2413 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002414}
2415
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002416struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002417window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002419 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002420 uint32_t buttons;
2421
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002422 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002423 buttons = FRAME_BUTTON_NONE;
2424 } else {
2425 buttons = FRAME_BUTTON_ALL;
2426 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002427
Peter Huttererf3d62272013-08-08 11:57:05 +10002428 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002429 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002430 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002431
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002432 frame->widget = window_add_widget(window, frame);
2433 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002434
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002435 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2436 widget_set_resize_handler(frame->widget, frame_resize_handler);
2437 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002438 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002439 widget_set_motion_handler(frame->widget, frame_motion_handler);
2440 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002441 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002442 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002443
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002444 window->frame = frame;
2445
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002446 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002447}
2448
Kristian Høgsberga1627922012-06-20 17:30:03 -04002449void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002450window_frame_set_child_size(struct widget *widget, int child_width,
2451 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002452{
2453 struct display *display = widget->window->display;
2454 struct theme *t = display->theme;
2455 int decoration_width, decoration_height;
2456 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002457 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002458
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002459 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002460 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002461 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002462 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002463
2464 width = child_width + decoration_width;
2465 height = child_height + decoration_height;
2466 } else {
2467 width = child_width;
2468 height = child_height;
2469 }
2470
2471 window_schedule_resize(widget->window, width, height);
2472}
2473
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002474static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002475window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002476{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002477 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002478
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002479 /* frame->child must be destroyed by the application */
2480 widget_destroy(frame->widget);
2481 free(frame);
2482}
2483
2484static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002485input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002486 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002487{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002488 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002489 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002490
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002491 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002492 return;
2493
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002494 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002495 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002496 widget = old;
2497 if (input->grab)
2498 widget = input->grab;
2499 if (widget->leave_handler)
2500 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002501 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002502 }
2503
2504 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002505 widget = focus;
2506 if (input->grab)
2507 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002508 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002509 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002510 cursor = widget->enter_handler(focus, input, x, y,
2511 widget->user_data);
2512 else
2513 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002514
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002515 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002516 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002517}
2518
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002519void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002520touch_grab(struct input *input, int32_t touch_id)
2521{
2522 input->touch_grab = 1;
2523 input->touch_grab_id = touch_id;
2524}
2525
2526void
2527touch_ungrab(struct input *input)
2528{
2529 struct touch_point *tp, *tmp;
2530
2531 input->touch_grab = 0;
2532
2533 wl_list_for_each_safe(tp, tmp,
2534 &input->touch_point_list, link) {
2535 if (tp->id != input->touch_grab_id)
2536 continue;
2537 wl_list_remove(&tp->link);
2538 free(tp);
2539
2540 return;
2541 }
2542}
2543
2544void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002545input_grab(struct input *input, struct widget *widget, uint32_t button)
2546{
2547 input->grab = widget;
2548 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002549
2550 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002551}
2552
2553void
2554input_ungrab(struct input *input)
2555{
2556 struct widget *widget;
2557
2558 input->grab = NULL;
2559 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002560 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002561 input->sx, input->sy);
2562 input_set_focus_widget(input, widget, input->sx, input->sy);
2563 }
2564}
2565
2566static void
2567input_remove_pointer_focus(struct input *input)
2568{
2569 struct window *window = input->pointer_focus;
2570
2571 if (!window)
2572 return;
2573
2574 input_set_focus_widget(input, NULL, 0, 0);
2575
2576 input->pointer_focus = NULL;
2577 input->current_cursor = CURSOR_UNSET;
2578}
2579
2580static void
2581pointer_handle_enter(void *data, struct wl_pointer *pointer,
2582 uint32_t serial, struct wl_surface *surface,
2583 wl_fixed_t sx_w, wl_fixed_t sy_w)
2584{
2585 struct input *input = data;
2586 struct window *window;
2587 struct widget *widget;
2588 float sx = wl_fixed_to_double(sx_w);
2589 float sy = wl_fixed_to_double(sy_w);
2590
2591 if (!surface) {
2592 /* enter event for a window we've just destroyed */
2593 return;
2594 }
2595
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002596 window = wl_surface_get_user_data(surface);
2597 if (surface != window->main_surface->surface) {
2598 DBG("Ignoring input event from subsurface %p\n", surface);
2599 return;
2600 }
2601
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002602 input->display->serial = serial;
2603 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002604 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002605
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002606 input->sx = sx;
2607 input->sy = sy;
2608
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002609 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002610 input_set_focus_widget(input, widget, sx, sy);
2611}
2612
2613static void
2614pointer_handle_leave(void *data, struct wl_pointer *pointer,
2615 uint32_t serial, struct wl_surface *surface)
2616{
2617 struct input *input = data;
2618
2619 input->display->serial = serial;
2620 input_remove_pointer_focus(input);
2621}
2622
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002623static void
Daniel Stone37816df2012-05-16 18:45:18 +01002624pointer_handle_motion(void *data, struct wl_pointer *pointer,
2625 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002626{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002627 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002628 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002629 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002630 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002631 float sx = wl_fixed_to_double(sx_w);
2632 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002633
Paul Winwoodb22bf572013-08-29 10:52:54 +01002634 if (!window)
2635 return;
2636
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002637 input->sx = sx;
2638 input->sy = sy;
2639
Rob Bradford5f087742013-07-11 19:41:27 +01002640 /* when making the window smaller - e.g. after a unmaximise we might
2641 * still have a pending motion event that the compositor has picked
2642 * based on the old surface dimensions
2643 */
2644 if (sx > window->main_surface->allocation.width ||
2645 sy > window->main_surface->allocation.height)
2646 return;
2647
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002648 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002649 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002650 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002651 }
2652
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002653 if (input->grab)
2654 widget = input->grab;
2655 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002656 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002657 if (widget) {
2658 if (widget->motion_handler)
2659 cursor = widget->motion_handler(input->focus_widget,
2660 input, time, sx, sy,
2661 widget->user_data);
2662 else
2663 cursor = widget->default_cursor;
2664 } else
2665 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002666
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002667 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002668}
2669
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002670static void
Daniel Stone37816df2012-05-16 18:45:18 +01002671pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002672 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002673{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002674 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002675 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002676 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002677
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002678 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002679 if (input->focus_widget && input->grab == NULL &&
2680 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002681 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002682
Neil Roberts6b28aad2012-01-23 19:11:18 +00002683 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002684 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002685 (*widget->button_handler)(widget,
2686 input, time,
2687 button, state,
2688 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002689
Daniel Stone4dbadb12012-05-30 16:31:51 +01002690 if (input->grab && input->grab_button == button &&
2691 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002692 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002693}
2694
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002695static void
Daniel Stone37816df2012-05-16 18:45:18 +01002696pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002697 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002698{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002699 struct input *input = data;
2700 struct widget *widget;
2701
2702 widget = input->focus_widget;
2703 if (input->grab)
2704 widget = input->grab;
2705 if (widget && widget->axis_handler)
2706 (*widget->axis_handler)(widget,
2707 input, time,
2708 axis, value,
2709 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002710}
2711
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002712static const struct wl_pointer_listener pointer_listener = {
2713 pointer_handle_enter,
2714 pointer_handle_leave,
2715 pointer_handle_motion,
2716 pointer_handle_button,
2717 pointer_handle_axis,
2718};
2719
2720static void
2721input_remove_keyboard_focus(struct input *input)
2722{
2723 struct window *window = input->keyboard_focus;
2724 struct itimerspec its;
2725
2726 its.it_interval.tv_sec = 0;
2727 its.it_interval.tv_nsec = 0;
2728 its.it_value.tv_sec = 0;
2729 its.it_value.tv_nsec = 0;
2730 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2731
2732 if (!window)
2733 return;
2734
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002735 if (window->keyboard_focus_handler)
2736 (*window->keyboard_focus_handler)(window, NULL,
2737 window->user_data);
2738
2739 input->keyboard_focus = NULL;
2740}
2741
2742static void
2743keyboard_repeat_func(struct task *task, uint32_t events)
2744{
2745 struct input *input =
2746 container_of(task, struct input, repeat_task);
2747 struct window *window = input->keyboard_focus;
2748 uint64_t exp;
2749
2750 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2751 /* If we change the timer between the fd becoming
2752 * readable and getting here, there'll be nothing to
2753 * read and we get EAGAIN. */
2754 return;
2755
2756 if (window && window->key_handler) {
2757 (*window->key_handler)(window, input, input->repeat_time,
2758 input->repeat_key, input->repeat_sym,
2759 WL_KEYBOARD_KEY_STATE_PRESSED,
2760 window->user_data);
2761 }
2762}
2763
2764static void
2765keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2766 uint32_t format, int fd, uint32_t size)
2767{
2768 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002769 struct xkb_keymap *keymap;
2770 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002771 char *map_str;
2772
2773 if (!data) {
2774 close(fd);
2775 return;
2776 }
2777
2778 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2779 close(fd);
2780 return;
2781 }
2782
2783 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2784 if (map_str == MAP_FAILED) {
2785 close(fd);
2786 return;
2787 }
2788
Rui Matos3eccb862013-10-10 19:44:22 +02002789 keymap = xkb_map_new_from_string(input->display->xkb_context,
2790 map_str,
2791 XKB_KEYMAP_FORMAT_TEXT_V1,
2792 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002793 munmap(map_str, size);
2794 close(fd);
2795
Rui Matos3eccb862013-10-10 19:44:22 +02002796 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002797 fprintf(stderr, "failed to compile keymap\n");
2798 return;
2799 }
2800
Rui Matos3eccb862013-10-10 19:44:22 +02002801 state = xkb_state_new(keymap);
2802 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002803 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002804 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002805 return;
2806 }
2807
Rui Matos3eccb862013-10-10 19:44:22 +02002808 xkb_keymap_unref(input->xkb.keymap);
2809 xkb_state_unref(input->xkb.state);
2810 input->xkb.keymap = keymap;
2811 input->xkb.state = state;
2812
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002813 input->xkb.control_mask =
2814 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2815 input->xkb.alt_mask =
2816 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2817 input->xkb.shift_mask =
2818 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2819}
2820
2821static void
2822keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2823 uint32_t serial, struct wl_surface *surface,
2824 struct wl_array *keys)
2825{
2826 struct input *input = data;
2827 struct window *window;
2828
2829 input->display->serial = serial;
2830 input->keyboard_focus = wl_surface_get_user_data(surface);
2831
2832 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002833 if (window->keyboard_focus_handler)
2834 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002835 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002836}
2837
2838static void
2839keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2840 uint32_t serial, struct wl_surface *surface)
2841{
2842 struct input *input = data;
2843
2844 input->display->serial = serial;
2845 input_remove_keyboard_focus(input);
2846}
2847
Scott Moreau210d0792012-03-22 10:47:01 -06002848static void
Daniel Stone37816df2012-05-16 18:45:18 +01002849keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002850 uint32_t serial, uint32_t time, uint32_t key,
2851 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002852{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002853 struct input *input = data;
2854 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002855 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002856 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002857 const xkb_keysym_t *syms;
2858 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002859 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002860
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002861 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002862 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002863 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002864 return;
2865
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002866 /* We only use input grabs for pointer events for now, so just
2867 * ignore key presses if a grab is active. We expand the key
2868 * event delivery mechanism to route events to widgets to
2869 * properly handle key grabs. In the meantime, this prevents
2870 * key event devlivery while a grab is active. */
2871 if (input->grab && input->grab_button == 0)
2872 return;
2873
Daniel Stone97f68542012-05-30 16:32:01 +01002874 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002875
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002876 sym = XKB_KEY_NoSymbol;
2877 if (num_syms == 1)
2878 sym = syms[0];
2879
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002880
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002881 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002882 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002883 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002884 } else if (sym == XKB_KEY_F11 &&
2885 window->fullscreen_handler &&
2886 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2887 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002888 } else if (sym == XKB_KEY_F4 &&
2889 input->modifiers == MOD_ALT_MASK &&
2890 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002891 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002892 } else if (window->key_handler) {
2893 (*window->key_handler)(window, input, time, key,
2894 sym, state, window->user_data);
2895 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002896
2897 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2898 key == input->repeat_key) {
2899 its.it_interval.tv_sec = 0;
2900 its.it_interval.tv_nsec = 0;
2901 its.it_value.tv_sec = 0;
2902 its.it_value.tv_nsec = 0;
2903 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002904 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2905 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002906 input->repeat_sym = sym;
2907 input->repeat_key = key;
2908 input->repeat_time = time;
2909 its.it_interval.tv_sec = 0;
2910 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2911 its.it_value.tv_sec = 0;
2912 its.it_value.tv_nsec = 400 * 1000 * 1000;
2913 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2914 }
2915}
2916
2917static void
Daniel Stone351eb612012-05-31 15:27:47 -04002918keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2919 uint32_t serial, uint32_t mods_depressed,
2920 uint32_t mods_latched, uint32_t mods_locked,
2921 uint32_t group)
2922{
2923 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002924 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002925
Matt Ropere61561f2013-06-24 16:52:43 +01002926 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2927 if (!input->xkb.keymap)
2928 return;
2929
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002930 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2931 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002932 mask = xkb_state_serialize_mods(input->xkb.state,
2933 XKB_STATE_DEPRESSED |
2934 XKB_STATE_LATCHED);
2935 input->modifiers = 0;
2936 if (mask & input->xkb.control_mask)
2937 input->modifiers |= MOD_CONTROL_MASK;
2938 if (mask & input->xkb.alt_mask)
2939 input->modifiers |= MOD_ALT_MASK;
2940 if (mask & input->xkb.shift_mask)
2941 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002942}
2943
Daniel Stone37816df2012-05-16 18:45:18 +01002944static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002945 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002946 keyboard_handle_enter,
2947 keyboard_handle_leave,
2948 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002949 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002950};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002951
2952static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002953touch_handle_down(void *data, struct wl_touch *wl_touch,
2954 uint32_t serial, uint32_t time, struct wl_surface *surface,
2955 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2956{
2957 struct input *input = data;
2958 struct widget *widget;
2959 float sx = wl_fixed_to_double(x_w);
2960 float sy = wl_fixed_to_double(y_w);
2961
Rusty Lynch1084da52013-08-15 09:10:08 -07002962 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002963 input->touch_focus = wl_surface_get_user_data(surface);
2964 if (!input->touch_focus) {
2965 DBG("Failed to find to touch focus for surface %p\n", surface);
2966 return;
2967 }
2968
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002969 if (surface != input->touch_focus->main_surface->surface) {
2970 DBG("Ignoring input event from subsurface %p\n", surface);
2971 input->touch_focus = NULL;
2972 return;
2973 }
2974
Kristian Høgsberg1f671172014-04-29 14:30:44 -07002975 if (input->grab)
2976 widget = input->grab;
2977 else
2978 widget = window_find_widget(input->touch_focus,
2979 wl_fixed_to_double(x_w),
2980 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07002981 if (widget) {
2982 struct touch_point *tp = xmalloc(sizeof *tp);
2983 if (tp) {
2984 tp->id = id;
2985 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08002986 tp->x = sx;
2987 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07002988 wl_list_insert(&input->touch_point_list, &tp->link);
2989
2990 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07002991 (*widget->touch_down_handler)(widget, input,
2992 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002993 sx, sy,
2994 widget->user_data);
2995 }
2996 }
2997}
2998
2999static void
3000touch_handle_up(void *data, struct wl_touch *wl_touch,
3001 uint32_t serial, uint32_t time, int32_t id)
3002{
3003 struct input *input = data;
3004 struct touch_point *tp, *tmp;
3005
Rusty Lynch041815a2013-08-08 21:20:38 -07003006 if (!input->touch_focus) {
3007 DBG("No touch focus found for touch up event!\n");
3008 return;
3009 }
3010
3011 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3012 if (tp->id != id)
3013 continue;
3014
3015 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003016 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003017 time, id,
3018 tp->widget->user_data);
3019
3020 wl_list_remove(&tp->link);
3021 free(tp);
3022
3023 return;
3024 }
3025}
3026
3027static void
3028touch_handle_motion(void *data, struct wl_touch *wl_touch,
3029 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3030{
3031 struct input *input = data;
3032 struct touch_point *tp;
3033 float sx = wl_fixed_to_double(x_w);
3034 float sy = wl_fixed_to_double(y_w);
3035
3036 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3037
3038 if (!input->touch_focus) {
3039 DBG("No touch focus found for touch motion event!\n");
3040 return;
3041 }
3042
3043 wl_list_for_each(tp, &input->touch_point_list, link) {
3044 if (tp->id != id)
3045 continue;
3046
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003047 tp->x = sx;
3048 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003049 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003050 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003051 id, sx, sy,
3052 tp->widget->user_data);
3053 return;
3054 }
3055}
3056
3057static void
3058touch_handle_frame(void *data, struct wl_touch *wl_touch)
3059{
3060 struct input *input = data;
3061 struct touch_point *tp, *tmp;
3062
3063 DBG("touch_handle_frame\n");
3064
3065 if (!input->touch_focus) {
3066 DBG("No touch focus found for touch frame event!\n");
3067 return;
3068 }
3069
3070 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3071 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003072 (*tp->widget->touch_frame_handler)(tp->widget, input,
3073 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003074 }
3075}
3076
3077static void
3078touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3079{
3080 struct input *input = data;
3081 struct touch_point *tp, *tmp;
3082
3083 DBG("touch_handle_cancel\n");
3084
3085 if (!input->touch_focus) {
3086 DBG("No touch focus found for touch cancel event!\n");
3087 return;
3088 }
3089
3090 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3091 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003092 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3093 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003094
3095 wl_list_remove(&tp->link);
3096 free(tp);
3097 }
3098}
3099
3100static const struct wl_touch_listener touch_listener = {
3101 touch_handle_down,
3102 touch_handle_up,
3103 touch_handle_motion,
3104 touch_handle_frame,
3105 touch_handle_cancel,
3106};
3107
3108static void
Daniel Stone37816df2012-05-16 18:45:18 +01003109seat_handle_capabilities(void *data, struct wl_seat *seat,
3110 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003111{
Daniel Stone37816df2012-05-16 18:45:18 +01003112 struct input *input = data;
3113
3114 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3115 input->pointer = wl_seat_get_pointer(seat);
3116 wl_pointer_set_user_data(input->pointer, input);
3117 wl_pointer_add_listener(input->pointer, &pointer_listener,
3118 input);
3119 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3120 wl_pointer_destroy(input->pointer);
3121 input->pointer = NULL;
3122 }
3123
3124 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3125 input->keyboard = wl_seat_get_keyboard(seat);
3126 wl_keyboard_set_user_data(input->keyboard, input);
3127 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3128 input);
3129 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3130 wl_keyboard_destroy(input->keyboard);
3131 input->keyboard = NULL;
3132 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003133
3134 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3135 input->touch = wl_seat_get_touch(seat);
3136 wl_touch_set_user_data(input->touch, input);
3137 wl_touch_add_listener(input->touch, &touch_listener, input);
3138 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3139 wl_touch_destroy(input->touch);
3140 input->touch = NULL;
3141 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003142}
3143
Rob Bradford08031182013-08-13 20:11:03 +01003144static void
3145seat_handle_name(void *data, struct wl_seat *seat,
3146 const char *name)
3147{
3148
3149}
3150
Daniel Stone37816df2012-05-16 18:45:18 +01003151static const struct wl_seat_listener seat_listener = {
3152 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003153 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003154};
3155
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003156void
3157input_get_position(struct input *input, int32_t *x, int32_t *y)
3158{
3159 *x = input->sx;
3160 *y = input->sy;
3161}
3162
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003163int
3164input_get_touch(struct input *input, int32_t id, float *x, float *y)
3165{
3166 struct touch_point *tp;
3167
3168 wl_list_for_each(tp, &input->touch_point_list, link) {
3169 if (tp->id != id)
3170 continue;
3171
3172 *x = tp->x;
3173 *y = tp->y;
3174 return 0;
3175 }
3176
3177 return -1;
3178}
3179
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003180struct display *
3181input_get_display(struct input *input)
3182{
3183 return input->display;
3184}
3185
Daniel Stone37816df2012-05-16 18:45:18 +01003186struct wl_seat *
3187input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003188{
Daniel Stone37816df2012-05-16 18:45:18 +01003189 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003190}
3191
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003192uint32_t
3193input_get_modifiers(struct input *input)
3194{
3195 return input->modifiers;
3196}
3197
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003198struct widget *
3199input_get_focus_widget(struct input *input)
3200{
3201 return input->focus_widget;
3202}
3203
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003204struct data_offer {
3205 struct wl_data_offer *offer;
3206 struct input *input;
3207 struct wl_array types;
3208 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003209
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003210 struct task io_task;
3211 int fd;
3212 data_func_t func;
3213 int32_t x, y;
3214 void *user_data;
3215};
3216
3217static void
3218data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3219{
3220 struct data_offer *offer = data;
3221 char **p;
3222
3223 p = wl_array_add(&offer->types, sizeof *p);
3224 *p = strdup(type);
3225}
3226
3227static const struct wl_data_offer_listener data_offer_listener = {
3228 data_offer_offer,
3229};
3230
3231static void
3232data_offer_destroy(struct data_offer *offer)
3233{
3234 char **p;
3235
3236 offer->refcount--;
3237 if (offer->refcount == 0) {
3238 wl_data_offer_destroy(offer->offer);
3239 for (p = offer->types.data; *p; p++)
3240 free(*p);
3241 wl_array_release(&offer->types);
3242 free(offer);
3243 }
3244}
3245
3246static void
3247data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003248 struct wl_data_device *data_device,
3249 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003250{
3251 struct data_offer *offer;
3252
Brian Lovinbc919262013-08-07 15:34:59 -07003253 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003254
3255 wl_array_init(&offer->types);
3256 offer->refcount = 1;
3257 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003258 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003259 wl_data_offer_add_listener(offer->offer,
3260 &data_offer_listener, offer);
3261}
3262
3263static void
3264data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003265 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003266 wl_fixed_t x_w, wl_fixed_t y_w,
3267 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003268{
3269 struct input *input = data;
3270 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003271 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003272 float x = wl_fixed_to_double(x_w);
3273 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003274 char **p;
3275
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003276 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003277 input->drag_enter_serial = serial;
3278 input->drag_focus = window,
3279 input->drag_x = x;
3280 input->drag_y = y;
3281
3282 if (!input->touch_grab)
3283 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003284
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003285 if (offer) {
3286 input->drag_offer = wl_data_offer_get_user_data(offer);
3287
3288 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3289 *p = NULL;
3290
3291 types_data = input->drag_offer->types.data;
3292 } else {
3293 input->drag_offer = NULL;
3294 types_data = NULL;
3295 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003296
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003297 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003298 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003299 window->user_data);
3300}
3301
3302static void
3303data_device_leave(void *data, struct wl_data_device *data_device)
3304{
3305 struct input *input = data;
3306
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003307 if (input->drag_offer) {
3308 data_offer_destroy(input->drag_offer);
3309 input->drag_offer = NULL;
3310 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003311}
3312
3313static void
3314data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003315 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003316{
3317 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003318 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003319 float x = wl_fixed_to_double(x_w);
3320 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003321 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003322
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003323 input->drag_x = x;
3324 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003325
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003326 if (input->drag_offer)
3327 types_data = input->drag_offer->types.data;
3328 else
3329 types_data = NULL;
3330
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003331 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003332 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003333 window->user_data);
3334}
3335
3336static void
3337data_device_drop(void *data, struct wl_data_device *data_device)
3338{
3339 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003340 struct window *window = input->drag_focus;
3341 float x, y;
3342
3343 x = input->drag_x;
3344 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003345
3346 if (window->drop_handler)
3347 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003348 x, y, window->user_data);
3349
3350 if (input->touch_grab)
3351 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003352}
3353
3354static void
3355data_device_selection(void *data,
3356 struct wl_data_device *wl_data_device,
3357 struct wl_data_offer *offer)
3358{
3359 struct input *input = data;
3360 char **p;
3361
3362 if (input->selection_offer)
3363 data_offer_destroy(input->selection_offer);
3364
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003365 if (offer) {
3366 input->selection_offer = wl_data_offer_get_user_data(offer);
3367 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3368 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003369 } else {
3370 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003371 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003372}
3373
3374static const struct wl_data_device_listener data_device_listener = {
3375 data_device_data_offer,
3376 data_device_enter,
3377 data_device_leave,
3378 data_device_motion,
3379 data_device_drop,
3380 data_device_selection
3381};
3382
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003383static void
3384input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003385{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003386 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003387 struct wl_cursor *cursor;
3388 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003389
Daniel Stone80972742012-11-07 17:51:39 +11003390 if (!input->pointer)
3391 return;
3392
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003393 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003394 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003395 return;
3396
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003397 if (index >= (int) cursor->image_count) {
3398 fprintf(stderr, "cursor index out of range\n");
3399 return;
3400 }
3401
3402 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003403 buffer = wl_cursor_image_get_buffer(image);
3404 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003405 return;
3406
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003407 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3408 wl_surface_damage(input->pointer_surface, 0, 0,
3409 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003410 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003411 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3412 input->pointer_surface,
3413 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003414}
3415
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003416static const struct wl_callback_listener pointer_surface_listener;
3417
3418static void
3419pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3420 uint32_t time)
3421{
3422 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003423 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003424 int i;
3425
3426 if (callback) {
3427 assert(callback == input->cursor_frame_cb);
3428 wl_callback_destroy(callback);
3429 input->cursor_frame_cb = NULL;
3430 }
3431
Daniel Stone80972742012-11-07 17:51:39 +11003432 if (!input->pointer)
3433 return;
3434
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003435 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003436 wl_pointer_set_cursor(input->pointer,
3437 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003438 NULL, 0, 0);
3439 return;
3440 }
3441
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003442 if (input->current_cursor == CURSOR_UNSET)
3443 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003444 cursor = input->display->cursors[input->current_cursor];
3445 if (!cursor)
3446 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003447
3448 /* FIXME We don't have the current time on the first call so we set
3449 * the animation start to the time of the first frame callback. */
3450 if (time == 0)
3451 input->cursor_anim_start = 0;
3452 else if (input->cursor_anim_start == 0)
3453 input->cursor_anim_start = time;
3454
3455 if (time == 0 || input->cursor_anim_start == 0)
3456 i = 0;
3457 else
3458 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3459
Pekka Paalanenbc106382012-10-10 12:49:31 +03003460 if (cursor->image_count > 1) {
3461 input->cursor_frame_cb =
3462 wl_surface_frame(input->pointer_surface);
3463 wl_callback_add_listener(input->cursor_frame_cb,
3464 &pointer_surface_listener, input);
3465 }
3466
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003467 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003468}
3469
3470static const struct wl_callback_listener pointer_surface_listener = {
3471 pointer_surface_frame_callback
3472};
3473
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003474void
3475input_set_pointer_image(struct input *input, int pointer)
3476{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003477 int force = 0;
3478
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003479 if (!input->pointer)
3480 return;
3481
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003482 if (input->pointer_enter_serial > input->cursor_serial)
3483 force = 1;
3484
3485 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003486 return;
3487
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003488 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003489 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003490 if (!input->cursor_frame_cb)
3491 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003492 else if (force) {
3493 /* The current frame callback may be stuck if, for instance,
3494 * the set cursor request was processed by the server after
3495 * this client lost the focus. In this case the cursor surface
3496 * might not be mapped and the frame callback wouldn't ever
3497 * complete. Send a set_cursor and attach to try to map the
3498 * cursor surface again so that the callback will finish */
3499 input_set_pointer_image_index(input, 0);
3500 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003501}
3502
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003503struct wl_data_device *
3504input_get_data_device(struct input *input)
3505{
3506 return input->data_device;
3507}
3508
3509void
3510input_set_selection(struct input *input,
3511 struct wl_data_source *source, uint32_t time)
3512{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003513 if (input->data_device)
3514 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003515}
3516
3517void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003518input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003519{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003520 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003521 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003522}
3523
3524static void
3525offer_io_func(struct task *task, uint32_t events)
3526{
3527 struct data_offer *offer =
3528 container_of(task, struct data_offer, io_task);
3529 unsigned int len;
3530 char buffer[4096];
3531
3532 len = read(offer->fd, buffer, sizeof buffer);
3533 offer->func(buffer, len,
3534 offer->x, offer->y, offer->user_data);
3535
3536 if (len == 0) {
3537 close(offer->fd);
3538 data_offer_destroy(offer);
3539 }
3540}
3541
3542static void
3543data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3544 data_func_t func, void *user_data)
3545{
3546 int p[2];
3547
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003548 if (pipe2(p, O_CLOEXEC) == -1)
3549 return;
3550
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003551 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3552 close(p[1]);
3553
3554 offer->io_task.run = offer_io_func;
3555 offer->fd = p[0];
3556 offer->func = func;
3557 offer->refcount++;
3558 offer->user_data = user_data;
3559
3560 display_watch_fd(offer->input->display,
3561 offer->fd, EPOLLIN, &offer->io_task);
3562}
3563
3564void
3565input_receive_drag_data(struct input *input, const char *mime_type,
3566 data_func_t func, void *data)
3567{
3568 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003569 input->drag_offer->x = input->drag_x;
3570 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003571}
3572
3573int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003574input_receive_drag_data_to_fd(struct input *input,
3575 const char *mime_type, int fd)
3576{
3577 if (input->drag_offer)
3578 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3579
3580 return 0;
3581}
3582
3583int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003584input_receive_selection_data(struct input *input, const char *mime_type,
3585 data_func_t func, void *data)
3586{
3587 char **p;
3588
3589 if (input->selection_offer == NULL)
3590 return -1;
3591
3592 for (p = input->selection_offer->types.data; *p; p++)
3593 if (strcmp(mime_type, *p) == 0)
3594 break;
3595
3596 if (*p == NULL)
3597 return -1;
3598
3599 data_offer_receive_data(input->selection_offer,
3600 mime_type, func, data);
3601 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003602}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003603
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003604int
3605input_receive_selection_data_to_fd(struct input *input,
3606 const char *mime_type, int fd)
3607{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003608 if (input->selection_offer)
3609 wl_data_offer_receive(input->selection_offer->offer,
3610 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003611
3612 return 0;
3613}
3614
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003615void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003616window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003617{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003618 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003619 return;
3620
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003621 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003622}
3623
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003624static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003625surface_set_synchronized(struct surface *surface)
3626{
3627 if (!surface->subsurface)
3628 return;
3629
3630 if (surface->synchronized)
3631 return;
3632
3633 wl_subsurface_set_sync(surface->subsurface);
3634 surface->synchronized = 1;
3635}
3636
3637static void
3638surface_set_synchronized_default(struct surface *surface)
3639{
3640 if (!surface->subsurface)
3641 return;
3642
3643 if (surface->synchronized == surface->synchronized_default)
3644 return;
3645
3646 if (surface->synchronized_default)
3647 wl_subsurface_set_sync(surface->subsurface);
3648 else
3649 wl_subsurface_set_desync(surface->subsurface);
3650
3651 surface->synchronized = surface->synchronized_default;
3652}
3653
3654static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003655surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003656{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003657 struct widget *widget = surface->widget;
3658 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003659
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003660 if (surface->input_region) {
3661 wl_region_destroy(surface->input_region);
3662 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003663 }
3664
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003665 if (surface->opaque_region)
3666 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003667
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003668 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003669
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003670 if (widget->resize_handler)
3671 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003672 widget->allocation.width,
3673 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003674 widget->user_data);
3675
Pekka Paalanen35e82632013-04-25 13:57:48 +03003676 if (surface->subsurface &&
3677 (surface->allocation.x != widget->allocation.x ||
3678 surface->allocation.y != widget->allocation.y)) {
3679 wl_subsurface_set_position(surface->subsurface,
3680 widget->allocation.x,
3681 widget->allocation.y);
3682 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003683 if (surface->allocation.width != widget->allocation.width ||
3684 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003685 window_schedule_redraw(widget->window);
3686 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003687 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003688
3689 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003690 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003691 widget->allocation.width,
3692 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003693}
3694
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003695static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003696hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3697{
3698 /*
3699 * This hack should be removed, when EGL respects
3700 * eglSwapInterval(0).
3701 *
3702 * If this window has sub-surfaces, especially a free-running
3703 * EGL-widget, we need to post the parent surface once with
3704 * all the old state to guarantee, that the EGL-widget will
3705 * receive its frame callback soon. Otherwise, a forced call
3706 * to eglSwapBuffers may end up blocking, waiting for a frame
3707 * event that will never come, because we will commit the parent
3708 * surface with all new state only after eglSwapBuffers returns.
3709 *
3710 * This assumes, that:
3711 * 1. When the EGL widget's resize hook is called, it pauses.
3712 * 2. When the EGL widget's redraw hook is called, it forces a
3713 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3714 * In a single threaded application condition 1 is a no-op.
3715 *
3716 * XXX: This should actually be after the surface_resize() calls,
3717 * but cannot, because then it would commit the incomplete state
3718 * accumulated from the widget resize hooks.
3719 */
3720 if (window->subsurface_list.next != &window->main_surface->link ||
3721 window->subsurface_list.prev != &window->main_surface->link)
3722 wl_surface_commit(window->main_surface->surface);
3723}
3724
3725static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003726window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003727{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003728 struct surface *surface;
3729
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003730 widget_set_allocation(window->main_surface->widget,
3731 window->pending_allocation.x,
3732 window->pending_allocation.y,
3733 window->pending_allocation.width,
3734 window->pending_allocation.height);
3735
3736 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003737
3738 /* The main surface is in the list, too. Main surface's
3739 * resize_handler is responsible for calling widget_set_allocation()
3740 * on all sub-surface root widgets, so they will be resized
3741 * properly.
3742 */
3743 wl_list_for_each(surface, &window->subsurface_list, link) {
3744 if (surface == window->main_surface)
3745 continue;
3746
3747 surface_set_synchronized(surface);
3748 surface_resize(surface);
3749 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003750
3751 if (!window->fullscreen && !window->maximized)
3752 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003753}
3754
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003755static void
3756idle_resize(struct window *window)
3757{
3758 window->resize_needed = 0;
3759 window->redraw_needed = 1;
3760
3761 DBG("from %dx%d to %dx%d\n",
3762 window->main_surface->server_allocation.width,
3763 window->main_surface->server_allocation.height,
3764 window->pending_allocation.width,
3765 window->pending_allocation.height);
3766
3767 hack_prevent_EGL_sub_surface_deadlock(window);
3768
3769 window_do_resize(window);
3770}
3771
3772static void
3773undo_resize(struct window *window)
3774{
3775 window->pending_allocation.width =
3776 window->main_surface->server_allocation.width;
3777 window->pending_allocation.height =
3778 window->main_surface->server_allocation.height;
3779
3780 DBG("back to %dx%d\n",
3781 window->main_surface->server_allocation.width,
3782 window->main_surface->server_allocation.height);
3783
3784 window_do_resize(window);
3785
3786 if (window->pending_allocation.width == 0 &&
3787 window->pending_allocation.height == 0) {
3788 fprintf(stderr, "Error: Could not draw a surface, "
3789 "most likely due to insufficient disk space in "
3790 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3791 exit(EXIT_FAILURE);
3792 }
3793}
3794
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003795void
3796window_schedule_resize(struct window *window, int width, int height)
3797{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003798 /* We should probably get these numbers from the theme. */
3799 const int min_width = 200, min_height = 200;
3800
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003801 window->pending_allocation.x = 0;
3802 window->pending_allocation.y = 0;
3803 window->pending_allocation.width = width;
3804 window->pending_allocation.height = height;
3805
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003806 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003807 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003808 window->min_allocation.width = min_width;
3809 else
3810 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003811 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003812 window->min_allocation.height = min_height;
3813 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003814 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003815 }
3816
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003817 if (window->pending_allocation.width < window->min_allocation.width)
3818 window->pending_allocation.width = window->min_allocation.width;
3819 if (window->pending_allocation.height < window->min_allocation.height)
3820 window->pending_allocation.height = window->min_allocation.height;
3821
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003822 window->resize_needed = 1;
3823 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003824}
3825
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003826void
3827widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3828{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003829 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003830}
3831
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003832static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003833handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003834 int32_t width, int32_t height,
3835 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003836{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003837 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003838 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003839
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003840 window->maximized = 0;
3841 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003842 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003843 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003844
3845 wl_array_for_each(p, states) {
3846 uint32_t state = *p;
3847 switch (state) {
3848 case XDG_SURFACE_STATE_MAXIMIZED:
3849 window->maximized = 1;
3850 break;
3851 case XDG_SURFACE_STATE_FULLSCREEN:
3852 window->fullscreen = 1;
3853 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003854 case XDG_SURFACE_STATE_RESIZING:
3855 window->resizing = 1;
3856 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003857 case XDG_SURFACE_STATE_ACTIVATED:
3858 window->focused = 1;
3859 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003860 default:
3861 /* Unknown state */
3862 break;
3863 }
3864 }
3865
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003866 if (window->frame) {
3867 if (window->maximized) {
3868 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3869 } else {
3870 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3871 }
3872
3873 if (window->focused) {
3874 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3875 } else {
3876 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3877 }
3878 }
3879
Jasper St. Pierref184c382014-05-06 08:33:27 -04003880 if (width > 0 && height > 0) {
3881 window_schedule_resize(window, width, height);
3882 } else {
3883 window_schedule_resize(window,
3884 window->saved_allocation.width,
3885 window->saved_allocation.height);
3886 }
3887
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07003888 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07003889
3890 if (window->state_changed_handler)
3891 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003892}
3893
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003894static void
3895handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3896{
3897 struct window *window = data;
3898 window_close(window);
3899}
3900
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003901static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003902 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003903 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003904};
3905
3906static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003907window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05003908{
3909 struct wl_surface *parent_surface;
3910
3911 if (!window->xdg_surface)
3912 return;
3913
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003914 if (window->parent)
3915 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05003916 else
3917 parent_surface = NULL;
3918
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003919 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre53686042013-12-09 15:26:25 -05003920}
3921
3922static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003923window_sync_margin(struct window *window)
3924{
3925 int margin;
3926
3927 if (!window->xdg_surface)
3928 return;
3929
3930 if (!window->frame)
3931 return;
3932
3933 margin = frame_get_shadow_margin(window->frame->frame);
3934
3935 /* Shadow size is the same on every side. */
3936 xdg_surface_set_margin(window->xdg_surface,
3937 margin,
3938 margin,
3939 margin,
3940 margin);
3941}
3942
3943static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003944window_flush(struct window *window)
3945{
3946 struct surface *surface;
3947
3948 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003949 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003950 window_sync_parent(window);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003951 window_sync_margin(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003952 }
3953 }
3954
3955 wl_list_for_each(surface, &window->subsurface_list, link) {
3956 if (surface == window->main_surface)
3957 continue;
3958
3959 surface_flush(surface);
3960 }
3961
3962 surface_flush(window->main_surface);
3963}
3964
3965static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003966menu_destroy(struct menu *menu)
3967{
3968 widget_destroy(menu->widget);
3969 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003970 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003971 free(menu);
3972}
3973
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003974void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003975window_get_allocation(struct window *window,
3976 struct rectangle *allocation)
3977{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003978 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003979}
3980
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003981static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003982widget_redraw(struct widget *widget)
3983{
3984 struct widget *child;
3985
3986 if (widget->redraw_handler)
3987 widget->redraw_handler(widget, widget->user_data);
3988 wl_list_for_each(child, &widget->child_list, link)
3989 widget_redraw(child);
3990}
3991
3992static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003993frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3994{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003995 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003996
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003997 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003998 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003999 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004000 surface->frame_cb = NULL;
4001
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004002 surface->last_time = time;
4003
Pekka Paalanen71233882013-04-25 13:57:53 +03004004 if (surface->redraw_needed || surface->window->redraw_needed) {
4005 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004006 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004007 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004008}
4009
4010static const struct wl_callback_listener listener = {
4011 frame_callback
4012};
4013
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004014static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004015surface_redraw(struct surface *surface)
4016{
Pekka Paalanen71233882013-04-25 13:57:53 +03004017 DBG_OBJ(surface->surface, "begin\n");
4018
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004019 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004020 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004021
4022 /* Whole-window redraw forces a redraw even if the previous has
4023 * not yet hit the screen.
4024 */
4025 if (surface->frame_cb) {
4026 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004027 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004028
Pekka Paalanen71233882013-04-25 13:57:53 +03004029 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004030 wl_callback_destroy(surface->frame_cb);
4031 }
4032
Neil Roberts97b747c2013-12-19 16:17:12 +00004033 if (surface->widget->use_cairo &&
4034 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004035 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4036 return -1;
4037 }
4038
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004039 surface->frame_cb = wl_surface_frame(surface->surface);
4040 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004041 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004042
4043 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004044 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004045 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004046 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004047 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004048}
4049
4050static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004051idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004052{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004053 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004054 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004055 int failed = 0;
4056 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004057
Pekka Paalanen71233882013-04-25 13:57:53 +03004058 DBG(" --------- \n");
4059
Pekka Paalaneneebff542013-04-25 13:57:52 +03004060 wl_list_init(&window->redraw_task.link);
4061 window->redraw_task_scheduled = 0;
4062
4063 if (window->resize_needed) {
4064 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004065 if (window->main_surface->frame_cb) {
4066 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004067 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004068 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004069
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004070 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004071 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004072 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004073
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004074 if (surface_redraw(window->main_surface) < 0) {
4075 /*
4076 * Only main_surface failure will cause us to undo the resize.
4077 * If sub-surfaces fail, they will just be broken with old
4078 * content.
4079 */
4080 failed = 1;
4081 } else {
4082 wl_list_for_each(surface, &window->subsurface_list, link) {
4083 if (surface == window->main_surface)
4084 continue;
4085
4086 surface_redraw(surface);
4087 }
4088 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004089
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004090 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004091 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004092
4093 wl_list_for_each(surface, &window->subsurface_list, link)
4094 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004095
4096 if (resized && failed) {
4097 /* Restore widget tree to correspond to what is on screen. */
4098 undo_resize(window);
4099 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004100}
4101
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004102static void
4103window_schedule_redraw_task(struct window *window)
4104{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004105 if (!window->redraw_task_scheduled) {
4106 window->redraw_task.run = idle_redraw;
4107 display_defer(window->display, &window->redraw_task);
4108 window->redraw_task_scheduled = 1;
4109 }
4110}
4111
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004112void
4113window_schedule_redraw(struct window *window)
4114{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004115 struct surface *surface;
4116
Pekka Paalanen71233882013-04-25 13:57:53 +03004117 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4118
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004119 wl_list_for_each(surface, &window->subsurface_list, link)
4120 surface->redraw_needed = 1;
4121
4122 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004123}
4124
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004125int
4126window_is_fullscreen(struct window *window)
4127{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004128 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004129}
4130
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004131void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004132window_set_fullscreen(struct window *window, int fullscreen)
4133{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004134 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004135 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004136
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004137 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004138 return;
4139
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004140 if (fullscreen)
4141 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4142 else
4143 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004144}
4145
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004146int
4147window_is_maximized(struct window *window)
4148{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004149 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004150}
4151
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004152void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004153window_set_maximized(struct window *window, int maximized)
4154{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004155 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004156 return;
4157
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004158 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004159 return;
4160
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004161 if (maximized)
4162 xdg_surface_set_maximized(window->xdg_surface);
4163 else
4164 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004165}
4166
Jasper St. Pierrede680992014-04-10 17:23:49 -07004167int
4168window_is_resizing(struct window *window)
4169{
4170 return window->resizing;
4171}
4172
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004173void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004174window_set_minimized(struct window *window)
4175{
4176 if (!window->xdg_surface)
4177 return;
4178
4179 xdg_surface_set_minimized(window->xdg_surface);
4180}
4181
4182void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004183window_set_user_data(struct window *window, void *data)
4184{
4185 window->user_data = data;
4186}
4187
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004188void *
4189window_get_user_data(struct window *window)
4190{
4191 return window->user_data;
4192}
4193
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004194void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004195window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004196 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004197{
4198 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004199}
4200
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004201void
4202window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004203 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004204{
4205 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004206}
4207
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004208void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004209window_set_data_handler(struct window *window, window_data_handler_t handler)
4210{
4211 window->data_handler = handler;
4212}
4213
4214void
4215window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4216{
4217 window->drop_handler = handler;
4218}
4219
4220void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004221window_set_close_handler(struct window *window,
4222 window_close_handler_t handler)
4223{
4224 window->close_handler = handler;
4225}
4226
4227void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004228window_set_fullscreen_handler(struct window *window,
4229 window_fullscreen_handler_t handler)
4230{
4231 window->fullscreen_handler = handler;
4232}
4233
4234void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004235window_set_output_handler(struct window *window,
4236 window_output_handler_t handler)
4237{
4238 window->output_handler = handler;
4239}
4240
4241void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004242window_set_state_changed_handler(struct window *window,
4243 window_state_changed_handler_t handler)
4244{
4245 window->state_changed_handler = handler;
4246}
4247
4248void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004249window_set_title(struct window *window, const char *title)
4250{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004251 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004252 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004253 if (window->frame) {
4254 frame_set_title(window->frame->frame, title);
4255 widget_schedule_redraw(window->frame->widget);
4256 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004257 if (window->xdg_surface)
4258 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004259}
4260
4261const char *
4262window_get_title(struct window *window)
4263{
4264 return window->title;
4265}
4266
4267void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004268window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4269{
4270 struct text_cursor_position *text_cursor_position =
4271 window->display->text_cursor_position;
4272
Scott Moreau9295ce02012-06-01 12:46:10 -06004273 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004274 return;
4275
4276 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004277 window->main_surface->surface,
4278 wl_fixed_from_int(x),
4279 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004280}
4281
4282void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004283window_damage(struct window *window, int32_t x, int32_t y,
4284 int32_t width, int32_t height)
4285{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004286 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004287}
4288
Casey Dahlin9074db52012-04-19 22:50:09 -04004289static void
4290surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004291 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004292{
Rob Bradford7507b572012-05-15 17:55:34 +01004293 struct window *window = data;
4294 struct output *output;
4295 struct output *output_found = NULL;
4296 struct window_output *window_output;
4297
4298 wl_list_for_each(output, &window->display->output_list, link) {
4299 if (output->output == wl_output) {
4300 output_found = output;
4301 break;
4302 }
4303 }
4304
4305 if (!output_found)
4306 return;
4307
Brian Lovinbc919262013-08-07 15:34:59 -07004308 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004309 window_output->output = output_found;
4310
4311 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004312
4313 if (window->output_handler)
4314 window->output_handler(window, output_found, 1,
4315 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004316}
4317
4318static void
4319surface_leave(void *data,
4320 struct wl_surface *wl_surface, struct wl_output *output)
4321{
Rob Bradford7507b572012-05-15 17:55:34 +01004322 struct window *window = data;
4323 struct window_output *window_output;
4324 struct window_output *window_output_found = NULL;
4325
4326 wl_list_for_each(window_output, &window->window_output_list, link) {
4327 if (window_output->output->output == output) {
4328 window_output_found = window_output;
4329 break;
4330 }
4331 }
4332
4333 if (window_output_found) {
4334 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004335
4336 if (window->output_handler)
4337 window->output_handler(window, window_output->output,
4338 0, window->user_data);
4339
Rob Bradford7507b572012-05-15 17:55:34 +01004340 free(window_output_found);
4341 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004342}
4343
4344static const struct wl_surface_listener surface_listener = {
4345 surface_enter,
4346 surface_leave
4347};
4348
Pekka Paalanen4e373742013-02-13 16:17:13 +02004349static struct surface *
4350surface_create(struct window *window)
4351{
4352 struct display *display = window->display;
4353 struct surface *surface;
4354
Brian Lovinbc919262013-08-07 15:34:59 -07004355 surface = xmalloc(sizeof *surface);
4356 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004357 if (!surface)
4358 return NULL;
4359
4360 surface->window = window;
4361 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004362 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004363 wl_surface_add_listener(surface->surface, &surface_listener, window);
4364
Pekka Paalanen35e82632013-04-25 13:57:48 +03004365 wl_list_insert(&window->subsurface_list, &surface->link);
4366
Pekka Paalanen4e373742013-02-13 16:17:13 +02004367 return surface;
4368}
4369
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004370static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004371get_preferred_buffer_type(struct display *display)
4372{
4373#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004374 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004375 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4376#endif
4377
4378 return WINDOW_BUFFER_TYPE_SHM;
4379}
4380
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004381static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004382window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004383{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004384 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004385 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004386
Peter Huttererf3d62272013-08-08 11:57:05 +10004387 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004388 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004389 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004390
4391 surface = surface_create(window);
4392 window->main_surface = surface;
4393
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004394 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004395
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004396 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004397 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004398
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004399 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004400
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004401 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004402 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004403 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004404
Rob Bradford7507b572012-05-15 17:55:34 +01004405 wl_list_init (&window->window_output_list);
4406
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004407 return window;
4408}
4409
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004410struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004411window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004412{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004413 struct window *window;
4414
4415 window = window_create_internal(display, 0);
4416
4417 window->xdg_surface =
4418 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4419 window->main_surface->surface);
4420 fail_on_null(window->xdg_surface);
4421
4422 xdg_surface_set_user_data(window->xdg_surface, window);
4423 xdg_surface_add_listener(window->xdg_surface,
4424 &xdg_surface_listener, window);
4425
4426 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004427}
4428
4429struct window *
4430window_create_custom(struct display *display)
4431{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004432 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004433}
4434
Jasper St. Pierre53686042013-12-09 15:26:25 -05004435void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004436window_set_parent(struct window *window,
4437 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004438{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004439 window->parent = parent_window;
4440 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004441}
4442
4443struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004444window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004445{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004446 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004447}
4448
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004449static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004450menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004451{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004452 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004453 int next;
4454
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004455 frame_interior(menu->frame, &x, &y, &width, &height);
4456 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004457 if (menu->current != next) {
4458 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004459 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004460 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004461}
4462
4463static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004464menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004465 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004466 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004467{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004468 struct menu *menu = data;
4469
4470 if (widget == menu->widget)
4471 menu_set_item(data, y);
4472
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004473 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004474}
4475
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004476static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004477menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004478 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004479{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004480 struct menu *menu = data;
4481
4482 if (widget == menu->widget)
4483 menu_set_item(data, y);
4484
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004485 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004486}
4487
4488static void
4489menu_leave_handler(struct widget *widget, struct input *input, void *data)
4490{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004491 struct menu *menu = data;
4492
4493 if (widget == menu->widget)
4494 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004495}
4496
4497static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004498menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004499 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004500 uint32_t button, enum wl_pointer_button_state state,
4501 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004502
4503{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004504 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004505
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004506 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4507 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004508 /* Either relase after press-drag-release or
4509 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004510 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004511 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004512 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004513 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004514 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004515 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004516}
4517
4518static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004519menu_touch_up_handler(struct widget *widget,
4520 struct input *input,
4521 uint32_t serial,
4522 uint32_t time,
4523 int32_t id,
4524 void *data)
4525{
4526 struct menu *menu = data;
4527
4528 input_ungrab(input);
4529 menu_destroy(menu);
4530}
4531
4532static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004533menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004534{
4535 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004536 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004537 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004538
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004539 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004540
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004541 frame_repaint(menu->frame, cr);
4542 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004543
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004544 theme_set_background_source(menu->window->display->theme,
4545 cr, THEME_FRAME_ACTIVE);
4546 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547 cairo_fill(cr);
4548
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004549 cairo_select_font_face(cr, "sans",
4550 CAIRO_FONT_SLANT_NORMAL,
4551 CAIRO_FONT_WEIGHT_NORMAL);
4552 cairo_set_font_size(cr, 12);
4553
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004554 for (i = 0; i < menu->count; i++) {
4555 if (i == menu->current) {
4556 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004557 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004558 cairo_fill(cr);
4559 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004560 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004561 cairo_show_text(cr, menu->entries[i]);
4562 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004563 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4564 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004565 cairo_show_text(cr, menu->entries[i]);
4566 }
4567 }
4568
4569 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004570}
4571
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004572static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004573handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4574{
4575 struct window *window = data;
4576 struct menu *menu = window->main_surface->widget->user_data;
4577
4578 input_ungrab(menu->input);
4579 menu_destroy(menu);
4580}
4581
4582static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004583 handle_popup_popup_done,
4584};
4585
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004586static struct menu *
4587create_menu(struct display *display,
4588 struct input *input, uint32_t time,
4589 menu_func_t func, const char **entries, int count,
4590 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004591{
4592 struct window *window;
4593 struct menu *menu;
4594
4595 menu = malloc(sizeof *menu);
4596 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004597 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004598
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004599 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004600 if (!window) {
4601 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004602 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004603 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004604
4605 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004606 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004607 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004608 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004609 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004610 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004611 menu->entries = entries;
4612 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004613 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004614 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004615 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004616 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004617 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004618
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004619 input_ungrab(input);
4620
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004621 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004622 widget_set_enter_handler(menu->widget, menu_enter_handler);
4623 widget_set_leave_handler(menu->widget, menu_leave_handler);
4624 widget_set_motion_handler(menu->widget, menu_motion_handler);
4625 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004626 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004627
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004628 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004629 frame_resize_inside(menu->frame, 200, count * 20);
4630 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4631 window_schedule_resize(window, frame_width(menu->frame),
4632 frame_height(menu->frame));
4633
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004634 return menu;
4635}
4636
4637struct window *
4638window_create_menu(struct display *display,
4639 struct input *input, uint32_t time,
4640 menu_func_t func, const char **entries, int count,
4641 void *user_data)
4642{
4643 struct menu *menu;
4644 menu = create_menu(display, input, time, func, entries, count, user_data);
4645
4646 if (menu == NULL)
4647 return NULL;
4648
4649 return menu->window;
4650}
4651
4652void
4653window_show_menu(struct display *display,
4654 struct input *input, uint32_t time, struct window *parent,
4655 int32_t x, int32_t y,
4656 menu_func_t func, const char **entries, int count)
4657{
4658 struct menu *menu;
4659 struct window *window;
4660 int32_t ix, iy;
4661
4662 menu = create_menu(display, input, time, func, entries, count, parent);
4663
4664 if (menu == NULL)
4665 return;
4666
4667 window = menu->window;
4668
4669 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4670 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4671
4672 window->x = x;
4673 window->y = y;
4674
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004675 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004676
4677 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4678 window->main_surface->surface,
4679 parent->main_surface->surface,
4680 input->seat,
4681 display_get_serial(window->display),
4682 window->x - ix,
4683 window->y - iy,
4684 0);
4685 fail_on_null(window->xdg_popup);
4686
4687 xdg_popup_set_user_data(window->xdg_popup, window);
4688 xdg_popup_add_listener(window->xdg_popup,
4689 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004690}
4691
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004692void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004693window_set_buffer_type(struct window *window, enum window_buffer_type type)
4694{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004695 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004696}
4697
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004698enum window_buffer_type
4699window_get_buffer_type(struct window *window)
4700{
4701 return window->main_surface->buffer_type;
4702}
4703
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004704void
4705window_set_preferred_format(struct window *window,
4706 enum preferred_format format)
4707{
4708 window->preferred_format = format;
4709}
4710
Pekka Paalanen35e82632013-04-25 13:57:48 +03004711struct widget *
4712window_add_subsurface(struct window *window, void *data,
4713 enum subsurface_mode default_mode)
4714{
4715 struct widget *widget;
4716 struct surface *surface;
4717 struct wl_surface *parent;
4718 struct wl_subcompositor *subcompo = window->display->subcompositor;
4719
Pekka Paalanen35e82632013-04-25 13:57:48 +03004720 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004721 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004722 widget = widget_create(window, surface, data);
4723 wl_list_init(&widget->link);
4724 surface->widget = widget;
4725
4726 parent = window->main_surface->surface;
4727 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4728 surface->surface,
4729 parent);
4730 surface->synchronized = 1;
4731
4732 switch (default_mode) {
4733 case SUBSURFACE_SYNCHRONIZED:
4734 surface->synchronized_default = 1;
4735 break;
4736 case SUBSURFACE_DESYNCHRONIZED:
4737 surface->synchronized_default = 0;
4738 break;
4739 default:
4740 assert(!"bad enum subsurface_mode");
4741 }
4742
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004743 window->resize_needed = 1;
4744 window_schedule_redraw(window);
4745
Pekka Paalanen35e82632013-04-25 13:57:48 +03004746 return widget;
4747}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004748
4749static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004750display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004751 struct wl_output *wl_output,
4752 int x, int y,
4753 int physical_width,
4754 int physical_height,
4755 int subpixel,
4756 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004757 const char *model,
4758 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004759{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004760 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004761
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004762 output->allocation.x = x;
4763 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004764 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004765
4766 if (output->make)
4767 free(output->make);
4768 output->make = strdup(make);
4769
4770 if (output->model)
4771 free(output->model);
4772 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004773}
4774
4775static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004776display_handle_done(void *data,
4777 struct wl_output *wl_output)
4778{
4779}
4780
4781static void
4782display_handle_scale(void *data,
4783 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004784 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004785{
4786 struct output *output = data;
4787
4788 output->scale = scale;
4789}
4790
4791static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004792display_handle_mode(void *data,
4793 struct wl_output *wl_output,
4794 uint32_t flags,
4795 int width,
4796 int height,
4797 int refresh)
4798{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004799 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004800 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004801
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004802 if (flags & WL_OUTPUT_MODE_CURRENT) {
4803 output->allocation.width = width;
4804 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004805 if (display->output_configure_handler)
4806 (*display->output_configure_handler)(
4807 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004808 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004809}
4810
4811static const struct wl_output_listener output_listener = {
4812 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004813 display_handle_mode,
4814 display_handle_done,
4815 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004816};
4817
4818static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004819display_add_output(struct display *d, uint32_t id)
4820{
4821 struct output *output;
4822
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004823 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004824 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004825 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004826 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004827 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004828 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004829 wl_list_insert(d->output_list.prev, &output->link);
4830
4831 wl_output_add_listener(output->output, &output_listener, output);
4832}
4833
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004834static void
4835output_destroy(struct output *output)
4836{
4837 if (output->destroy_handler)
4838 (*output->destroy_handler)(output, output->user_data);
4839
4840 wl_output_destroy(output->output);
4841 wl_list_remove(&output->link);
4842 free(output);
4843}
4844
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004845static void
4846display_destroy_output(struct display *d, uint32_t id)
4847{
4848 struct output *output;
4849
4850 wl_list_for_each(output, &d->output_list, link) {
4851 if (output->server_output_id == id) {
4852 output_destroy(output);
4853 break;
4854 }
4855 }
4856}
4857
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004858void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004859display_set_global_handler(struct display *display,
4860 display_global_handler_t handler)
4861{
4862 struct global *global;
4863
4864 display->global_handler = handler;
4865 if (!handler)
4866 return;
4867
4868 wl_list_for_each(global, &display->global_list, link)
4869 display->global_handler(display,
4870 global->name, global->interface,
4871 global->version, display->user_data);
4872}
4873
4874void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004875display_set_global_handler_remove(struct display *display,
4876 display_global_handler_t remove_handler)
4877{
4878 display->global_handler_remove = remove_handler;
4879 if (!remove_handler)
4880 return;
4881}
4882
4883void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004884display_set_output_configure_handler(struct display *display,
4885 display_output_handler_t handler)
4886{
4887 struct output *output;
4888
4889 display->output_configure_handler = handler;
4890 if (!handler)
4891 return;
4892
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004893 wl_list_for_each(output, &display->output_list, link) {
4894 if (output->allocation.width == 0 &&
4895 output->allocation.height == 0)
4896 continue;
4897
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004898 (*display->output_configure_handler)(output,
4899 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004900 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004901}
4902
4903void
4904output_set_user_data(struct output *output, void *data)
4905{
4906 output->user_data = data;
4907}
4908
4909void *
4910output_get_user_data(struct output *output)
4911{
4912 return output->user_data;
4913}
4914
4915void
4916output_set_destroy_handler(struct output *output,
4917 display_output_handler_t handler)
4918{
4919 output->destroy_handler = handler;
4920 /* FIXME: implement this, once we have way to remove outputs */
4921}
4922
4923void
Scott Moreau4e072362012-09-29 02:03:11 -06004924output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004925{
Scott Moreau4e072362012-09-29 02:03:11 -06004926 struct rectangle allocation = output->allocation;
4927
4928 switch (output->transform) {
4929 case WL_OUTPUT_TRANSFORM_90:
4930 case WL_OUTPUT_TRANSFORM_270:
4931 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4932 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4933 /* Swap width and height */
4934 allocation.width = output->allocation.height;
4935 allocation.height = output->allocation.width;
4936 break;
4937 }
4938
4939 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004940}
4941
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004942struct wl_output *
4943output_get_wl_output(struct output *output)
4944{
4945 return output->output;
4946}
4947
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004948enum wl_output_transform
4949output_get_transform(struct output *output)
4950{
4951 return output->transform;
4952}
4953
Alexander Larssonafd319a2013-05-22 14:41:27 +02004954uint32_t
4955output_get_scale(struct output *output)
4956{
4957 return output->scale;
4958}
4959
Jason Ekstrand738715d2014-04-02 19:53:50 -05004960const char *
4961output_get_make(struct output *output)
4962{
4963 return output->make;
4964}
4965
4966const char *
4967output_get_model(struct output *output)
4968{
4969 return output->model;
4970}
4971
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004972static void
Daniel Stone97f68542012-05-30 16:32:01 +01004973fini_xkb(struct input *input)
4974{
4975 xkb_state_unref(input->xkb.state);
4976 xkb_map_unref(input->xkb.keymap);
4977}
4978
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004979#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004980
Daniel Stone97f68542012-05-30 16:32:01 +01004981static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004982display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004983{
4984 struct input *input;
4985
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004986 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004987 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004988 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004989 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004990 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004991 input->pointer_focus = NULL;
4992 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004993 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004994 wl_list_insert(d->input_list.prev, &input->link);
4995
Daniel Stone37816df2012-05-16 18:45:18 +01004996 wl_seat_add_listener(input->seat, &seat_listener, input);
4997 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004998
Jason Ekstranda669bd52014-04-02 19:53:51 -05004999 if (d->data_device_manager) {
5000 input->data_device =
5001 wl_data_device_manager_get_data_device(d->data_device_manager,
5002 input->seat);
5003 wl_data_device_add_listener(input->data_device,
5004 &data_device_listener,
5005 input);
5006 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005007
5008 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005009
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005010 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5011 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005012 input->repeat_task.run = keyboard_repeat_func;
5013 display_watch_fd(d, input->repeat_timer_fd,
5014 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005015}
5016
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005017static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005018input_destroy(struct input *input)
5019{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005020 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005021 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005022
5023 if (input->drag_offer)
5024 data_offer_destroy(input->drag_offer);
5025
5026 if (input->selection_offer)
5027 data_offer_destroy(input->selection_offer);
5028
Jason Ekstranda669bd52014-04-02 19:53:51 -05005029 if (input->data_device)
5030 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005031
5032 if (input->display->seat_version >= 3) {
5033 if (input->pointer)
5034 wl_pointer_release(input->pointer);
5035 if (input->keyboard)
5036 wl_keyboard_release(input->keyboard);
5037 }
5038
Daniel Stone97f68542012-05-30 16:32:01 +01005039 fini_xkb(input);
5040
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005041 wl_surface_destroy(input->pointer_surface);
5042
Pekka Paalanene1207c72011-12-16 12:02:09 +02005043 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005044 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005045 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005046 free(input);
5047}
5048
5049static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005050init_workspace_manager(struct display *d, uint32_t id)
5051{
5052 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005053 wl_registry_bind(d->registry, id,
5054 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005055 if (d->workspace_manager != NULL)
5056 workspace_manager_add_listener(d->workspace_manager,
5057 &workspace_manager_listener,
5058 d);
5059}
5060
5061static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005062shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5063{
5064 struct display *d = data;
5065
5066 if (format == WL_SHM_FORMAT_RGB565)
5067 d->has_rgb565 = 1;
5068}
5069
5070struct wl_shm_listener shm_listener = {
5071 shm_format
5072};
5073
5074static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005075xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5076{
5077 xdg_shell_pong(shell, serial);
5078}
5079
5080static const struct xdg_shell_listener xdg_shell_listener = {
5081 xdg_shell_ping,
5082};
5083
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08005084#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005085#ifdef static_assert
5086static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5087 "Interface version doesn't match implementation version");
5088#endif
5089
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005090static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005091registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5092 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005093{
5094 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005095 struct global *global;
5096
Brian Lovinbc919262013-08-07 15:34:59 -07005097 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005098 global->name = id;
5099 global->interface = strdup(interface);
5100 global->version = version;
5101 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005102
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005103 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005104 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005105 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005106 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005107 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005108 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005109 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005110 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005111 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005112 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005113 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005114 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5115 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005116 wl_registry_bind(registry, id,
5117 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005118 } else if (strcmp(interface, "xdg_shell") == 0) {
5119 d->xdg_shell = wl_registry_bind(registry, id,
5120 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005121 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005122 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005123 } else if (strcmp(interface, "text_cursor_position") == 0) {
5124 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005125 wl_registry_bind(registry, id,
5126 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005127 } else if (strcmp(interface, "workspace_manager") == 0) {
5128 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005129 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5130 d->subcompositor =
5131 wl_registry_bind(registry, id,
5132 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005133 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005134
5135 if (d->global_handler)
5136 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005137}
5138
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005139static void
5140registry_handle_global_remove(void *data, struct wl_registry *registry,
5141 uint32_t name)
5142{
5143 struct display *d = data;
5144 struct global *global;
5145 struct global *tmp;
5146
5147 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5148 if (global->name != name)
5149 continue;
5150
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005151 if (strcmp(global->interface, "wl_output") == 0)
5152 display_destroy_output(d, name);
5153
5154 /* XXX: Should destroy remaining bound globals */
5155
5156 if (d->global_handler_remove)
5157 d->global_handler_remove(d, name, global->interface,
5158 global->version, d->user_data);
5159
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005160 wl_list_remove(&global->link);
5161 free(global->interface);
5162 free(global);
5163 }
5164}
5165
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005166void *
5167display_bind(struct display *display, uint32_t name,
5168 const struct wl_interface *interface, uint32_t version)
5169{
5170 return wl_registry_bind(display->registry, name, interface, version);
5171}
5172
5173static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005174 registry_handle_global,
5175 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005176};
5177
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005178#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005179static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005180init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005181{
5182 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005183 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005184
Rob Clark6396ed32012-03-11 19:48:41 -05005185#ifdef USE_CAIRO_GLESV2
5186# define GL_BIT EGL_OPENGL_ES2_BIT
5187#else
5188# define GL_BIT EGL_OPENGL_BIT
5189#endif
5190
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005191 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005192 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005193 EGL_RED_SIZE, 1,
5194 EGL_GREEN_SIZE, 1,
5195 EGL_BLUE_SIZE, 1,
5196 EGL_ALPHA_SIZE, 1,
5197 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005198 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005199 EGL_NONE
5200 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005201
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005202#ifdef USE_CAIRO_GLESV2
5203 static const EGLint context_attribs[] = {
5204 EGL_CONTEXT_CLIENT_VERSION, 2,
5205 EGL_NONE
5206 };
5207 EGLint api = EGL_OPENGL_ES_API;
5208#else
5209 EGLint *context_attribs = NULL;
5210 EGLint api = EGL_OPENGL_API;
5211#endif
5212
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005213 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005214 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005215 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005216 return -1;
5217 }
5218
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005219 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005220 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005221 return -1;
5222 }
5223
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005224 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5225 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005226 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005227 return -1;
5228 }
5229
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005230 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005231 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005232 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005233 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005234 return -1;
5235 }
5236
Benjamin Franzke0c991632011-09-27 21:57:31 +02005237 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5238 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005239 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005240 return -1;
5241 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005242
5243 return 0;
5244}
5245
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005246static void
5247fini_egl(struct display *display)
5248{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005249 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005250
5251 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5252 EGL_NO_CONTEXT);
5253
5254 eglTerminate(display->dpy);
5255 eglReleaseThread();
5256}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005257#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005258
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005259static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005260init_dummy_surface(struct display *display)
5261{
5262 int len;
5263 void *data;
5264
5265 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5266 data = malloc(len);
5267 display->dummy_surface =
5268 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5269 1, 1, len);
5270 display->dummy_surface_data = data;
5271}
5272
5273static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005274handle_display_data(struct task *task, uint32_t events)
5275{
5276 struct display *display =
5277 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005278 struct epoll_event ep;
5279 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005280
5281 display->display_fd_events = events;
5282
5283 if (events & EPOLLERR || events & EPOLLHUP) {
5284 display_exit(display);
5285 return;
5286 }
5287
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005288 if (events & EPOLLIN) {
5289 ret = wl_display_dispatch(display->display);
5290 if (ret == -1) {
5291 display_exit(display);
5292 return;
5293 }
5294 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005295
5296 if (events & EPOLLOUT) {
5297 ret = wl_display_flush(display->display);
5298 if (ret == 0) {
5299 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5300 ep.data.ptr = &display->display_task;
5301 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5302 display->display_fd, &ep);
5303 } else if (ret == -1 && errno != EAGAIN) {
5304 display_exit(display);
5305 return;
5306 }
5307 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005308}
5309
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005310static void
5311log_handler(const char *format, va_list args)
5312{
5313 vfprintf(stderr, format, args);
5314}
5315
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005316struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005317display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005318{
5319 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005320
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005321 wl_log_set_handler_client(log_handler);
5322
Peter Huttererf3d62272013-08-08 11:57:05 +10005323 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005324 if (d == NULL)
5325 return NULL;
5326
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005327 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005328 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005329 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005330 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005331 return NULL;
5332 }
5333
Rob Bradford5ab9c752013-07-26 16:29:43 +01005334 d->xkb_context = xkb_context_new(0);
5335 if (d->xkb_context == NULL) {
5336 fprintf(stderr, "Failed to create XKB context\n");
5337 free(d);
5338 return NULL;
5339 }
5340
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005341 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005342 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005343 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005344 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5345 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005346
5347 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005348 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005349 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005350 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005351
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005352 d->workspace = 0;
5353 d->workspace_count = 1;
5354
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005355 d->registry = wl_display_get_registry(d->display);
5356 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005357
5358 if (wl_display_dispatch(d->display) < 0) {
5359 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5360 return NULL;
5361 }
5362
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005363#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005364 if (init_egl(d) < 0)
5365 fprintf(stderr, "EGL does not seem to work, "
5366 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005367#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005368
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005369 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005370
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005371 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005372
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005373 wl_list_init(&d->window_list);
5374
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005375 init_dummy_surface(d);
5376
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005377 return d;
5378}
5379
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005380static void
5381display_destroy_outputs(struct display *display)
5382{
5383 struct output *tmp;
5384 struct output *output;
5385
5386 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5387 output_destroy(output);
5388}
5389
Pekka Paalanene1207c72011-12-16 12:02:09 +02005390static void
5391display_destroy_inputs(struct display *display)
5392{
5393 struct input *tmp;
5394 struct input *input;
5395
5396 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5397 input_destroy(input);
5398}
5399
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005400void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005401display_destroy(struct display *display)
5402{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005403 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005404 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5405 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005406
5407 if (!wl_list_empty(&display->deferred_list))
5408 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5409
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005410 cairo_surface_destroy(display->dummy_surface);
5411 free(display->dummy_surface_data);
5412
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005413 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005414 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005415
Daniel Stone97f68542012-05-30 16:32:01 +01005416 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005417
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005418 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005419 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005420
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005421#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005422 if (display->argb_device)
5423 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005424#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005425
Pekka Paalanen35e82632013-04-25 13:57:48 +03005426 if (display->subcompositor)
5427 wl_subcompositor_destroy(display->subcompositor);
5428
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005429 if (display->xdg_shell)
5430 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005431
5432 if (display->shm)
5433 wl_shm_destroy(display->shm);
5434
5435 if (display->data_device_manager)
5436 wl_data_device_manager_destroy(display->data_device_manager);
5437
5438 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005439 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005440
5441 close(display->epoll_fd);
5442
U. Artie Eoff44874d92012-10-02 21:12:35 -07005443 if (!(display->display_fd_events & EPOLLERR) &&
5444 !(display->display_fd_events & EPOLLHUP))
5445 wl_display_flush(display->display);
5446
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005447 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005448 free(display);
5449}
5450
5451void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005452display_set_user_data(struct display *display, void *data)
5453{
5454 display->user_data = data;
5455}
5456
5457void *
5458display_get_user_data(struct display *display)
5459{
5460 return display->user_data;
5461}
5462
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005463struct wl_display *
5464display_get_display(struct display *display)
5465{
5466 return display->display;
5467}
5468
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005469int
5470display_has_subcompositor(struct display *display)
5471{
5472 if (display->subcompositor)
5473 return 1;
5474
5475 wl_display_roundtrip(display->display);
5476
5477 return display->subcompositor != NULL;
5478}
5479
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005480cairo_device_t *
5481display_get_cairo_device(struct display *display)
5482{
5483 return display->argb_device;
5484}
5485
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005486struct output *
5487display_get_output(struct display *display)
5488{
5489 return container_of(display->output_list.next, struct output, link);
5490}
5491
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005492struct wl_compositor *
5493display_get_compositor(struct display *display)
5494{
5495 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005496}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005497
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005498uint32_t
5499display_get_serial(struct display *display)
5500{
5501 return display->serial;
5502}
5503
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005504EGLDisplay
5505display_get_egl_display(struct display *d)
5506{
5507 return d->dpy;
5508}
5509
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005510struct wl_data_source *
5511display_create_data_source(struct display *display)
5512{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005513 if (display->data_device_manager)
5514 return wl_data_device_manager_create_data_source(display->data_device_manager);
5515 else
5516 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005517}
5518
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005519EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005520display_get_argb_egl_config(struct display *d)
5521{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005522 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005523}
5524
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005525int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005526display_acquire_window_surface(struct display *display,
5527 struct window *window,
5528 EGLContext ctx)
5529{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005530 struct surface *surface = window->main_surface;
5531
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005532 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005533 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005534
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005535 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005536 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005537}
5538
5539void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005540display_release_window_surface(struct display *display,
5541 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005542{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005543 struct surface *surface = window->main_surface;
5544
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005545 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005546 return;
5547
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005548 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005549}
5550
5551void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005552display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005553{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005554 wl_list_insert(&display->deferred_list, &task->link);
5555}
5556
5557void
5558display_watch_fd(struct display *display,
5559 int fd, uint32_t events, struct task *task)
5560{
5561 struct epoll_event ep;
5562
5563 ep.events = events;
5564 ep.data.ptr = task;
5565 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5566}
5567
5568void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005569display_unwatch_fd(struct display *display, int fd)
5570{
5571 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5572}
5573
5574void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005575display_run(struct display *display)
5576{
5577 struct task *task;
5578 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005579 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005580
Pekka Paalanen826d7952011-12-15 10:14:07 +02005581 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005582 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005583 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005584 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005585 struct task, link);
5586 wl_list_remove(&task->link);
5587 task->run(task, 0);
5588 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005589
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005590 wl_display_dispatch_pending(display->display);
5591
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005592 if (!display->running)
5593 break;
5594
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005595 ret = wl_display_flush(display->display);
5596 if (ret < 0 && errno == EAGAIN) {
5597 ep[0].events =
5598 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5599 ep[0].data.ptr = &display->display_task;
5600
5601 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5602 display->display_fd, &ep[0]);
5603 } else if (ret < 0) {
5604 break;
5605 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005606
5607 count = epoll_wait(display->epoll_fd,
5608 ep, ARRAY_LENGTH(ep), -1);
5609 for (i = 0; i < count; i++) {
5610 task = ep[i].data.ptr;
5611 task->run(task, ep[i].events);
5612 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005613 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005614}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005615
5616void
5617display_exit(struct display *display)
5618{
5619 display->running = 0;
5620}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005621
5622void
5623keysym_modifiers_add(struct wl_array *modifiers_map,
5624 const char *name)
5625{
5626 size_t len = strlen(name) + 1;
5627 char *p;
5628
5629 p = wl_array_add(modifiers_map, len);
5630
5631 if (p == NULL)
5632 return;
5633
5634 strncpy(p, name, len);
5635}
5636
5637static xkb_mod_index_t
5638keysym_modifiers_get_index(struct wl_array *modifiers_map,
5639 const char *name)
5640{
5641 xkb_mod_index_t index = 0;
5642 char *p = modifiers_map->data;
5643
5644 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5645 if (strcmp(p, name) == 0)
5646 return index;
5647
5648 index++;
5649 p += strlen(p) + 1;
5650 }
5651
5652 return XKB_MOD_INVALID;
5653}
5654
5655xkb_mod_mask_t
5656keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5657 const char *name)
5658{
5659 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5660
5661 if (index == XKB_MOD_INVALID)
5662 return XKB_MOD_INVALID;
5663
5664 return 1 << index;
5665}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005666
5667void *
5668fail_on_null(void *p)
5669{
5670 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005671 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005672 exit(EXIT_FAILURE);
5673 }
5674
5675 return p;
5676}
5677
5678void *
5679xmalloc(size_t s)
5680{
5681 return fail_on_null(malloc(s));
5682}
5683
Peter Huttererf3d62272013-08-08 11:57:05 +10005684void *
5685xzalloc(size_t s)
5686{
5687 return fail_on_null(zalloc(s));
5688}
5689
Kristian Høgsbergce278412013-07-25 15:20:20 -07005690char *
5691xstrdup(const char *s)
5692{
5693 return fail_on_null(strdup(s));
5694}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005695
5696void *
5697xrealloc(char *p, size_t s)
5698{
5699 return fail_on_null(realloc(p, s));
5700}