blob: a8bc26029630afa24cc4ceda827952d20b4f339f [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
Jonny Lamb06959082014-08-12 14:58:27 +0200337 int32_t repeat_rate_sec;
338 int32_t repeat_rate_nsec;
339 int32_t repeat_delay_sec;
340 int32_t repeat_delay_nsec;
341
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400342 struct task repeat_task;
343 int repeat_timer_fd;
344 uint32_t repeat_sym;
345 uint32_t repeat_key;
346 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400347};
348
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500349struct output {
350 struct display *display;
351 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800352 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500353 struct rectangle allocation;
354 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600355 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200356 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500357 char *make;
358 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200359
360 display_output_handler_t destroy_handler;
361 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500362};
363
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500364struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500365 struct widget *widget;
366 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500367 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500368};
369
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500370struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400371 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500372 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500373 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500374 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700375 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500376 const char **entries;
377 uint32_t time;
378 int current;
379 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400380 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500381 menu_func_t func;
382};
383
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300384struct tooltip {
385 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300386 struct widget *widget;
387 char *entry;
388 struct task tooltip_task;
389 int tooltip_fd;
390 float x, y;
391};
392
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700393struct shm_pool {
394 struct wl_shm_pool *pool;
395 size_t size;
396 size_t used;
397 void *data;
398};
399
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400400enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300401 CURSOR_DEFAULT = 100,
402 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400403};
404
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200405static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400406
Pekka Paalanen97777442013-05-22 10:20:05 +0300407/* #define DEBUG */
408
409#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300410
411static void
412debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
413__attribute__ ((format (printf, 4, 5)));
414
415static void
416debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
417{
418 va_list ap;
419 struct timeval tv;
420
421 gettimeofday(&tv, NULL);
422 fprintf(stderr, "%8ld.%03ld ",
423 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
424
425 if (proxy)
426 fprintf(stderr, "%s@%d ",
427 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
428
429 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
430 fprintf(stderr, "%s ", func);
431
432 va_start(ap, fmt);
433 vfprintf(stderr, fmt, ap);
434 va_end(ap);
435}
436
437#define DBG(fmt, ...) \
438 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
439
440#define DBG_OBJ(obj, fmt, ...) \
441 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
442
443#else
444
445#define DBG(...) do {} while (0)
446#define DBG_OBJ(...) do {} while (0)
447
448#endif
449
Alexander Larsson1818e312013-05-22 14:41:31 +0200450static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200451surface_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 +0200452{
453 int32_t tmp;
454
455 switch (buffer_transform) {
456 case WL_OUTPUT_TRANSFORM_90:
457 case WL_OUTPUT_TRANSFORM_270:
458 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
459 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
460 tmp = *width;
461 *width = *height;
462 *height = tmp;
463 break;
464 default:
465 break;
466 }
467
468 *width *= buffer_scale;
469 *height *= buffer_scale;
470}
471
472static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200473buffer_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 +0200474{
475 int32_t tmp;
476
477 switch (buffer_transform) {
478 case WL_OUTPUT_TRANSFORM_90:
479 case WL_OUTPUT_TRANSFORM_270:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
481 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
482 tmp = *width;
483 *width = *height;
484 *height = tmp;
485 break;
486 default:
487 break;
488 }
489
490 *width /= buffer_scale;
491 *height /= buffer_scale;
492}
493
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500494#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400495
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200496struct egl_window_surface {
497 struct toysurface base;
498 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100499 struct display *display;
500 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200501 struct wl_egl_window *egl_window;
502 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100503};
504
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200505static struct egl_window_surface *
506to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100507{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200508 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100509}
510
511static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200512egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200513 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200514 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100515{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200516 struct egl_window_surface *surface = to_egl_window_surface(base);
517
Alexander Larsson1818e312013-05-22 14:41:31 +0200518 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
519
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200520 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
521 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
522
523 return cairo_surface_reference(surface->cairo_surface);
524}
525
526static void
527egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200528 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200529 struct rectangle *server_allocation)
530{
531 struct egl_window_surface *surface = to_egl_window_surface(base);
532
533 cairo_gl_surface_swapbuffers(surface->cairo_surface);
534 wl_egl_window_get_attached_size(surface->egl_window,
535 &server_allocation->width,
536 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200537
538 buffer_to_surface_size (buffer_transform, buffer_scale,
539 &server_allocation->width,
540 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200541}
542
543static int
544egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
545{
546 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200547 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100548
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200549 device = cairo_surface_get_device(surface->cairo_surface);
550 if (!device)
551 return -1;
552
553 if (!ctx) {
554 if (device == surface->display->argb_device)
555 ctx = surface->display->argb_ctx;
556 else
557 assert(0);
558 }
559
560 cairo_device_flush(device);
561 cairo_device_acquire(device);
562 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
563 surface->egl_surface, ctx))
564 fprintf(stderr, "failed to make surface current\n");
565
566 return 0;
567}
568
569static void
570egl_window_surface_release(struct toysurface *base)
571{
572 struct egl_window_surface *surface = to_egl_window_surface(base);
573 cairo_device_t *device;
574
575 device = cairo_surface_get_device(surface->cairo_surface);
576 if (!device)
577 return;
578
579 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
580 surface->display->argb_ctx))
581 fprintf(stderr, "failed to make context current\n");
582
583 cairo_device_release(device);
584}
585
586static void
587egl_window_surface_destroy(struct toysurface *base)
588{
589 struct egl_window_surface *surface = to_egl_window_surface(base);
590 struct display *d = surface->display;
591
592 cairo_surface_destroy(surface->cairo_surface);
593 eglDestroySurface(d->dpy, surface->egl_surface);
594 wl_egl_window_destroy(surface->egl_window);
595 surface->surface = NULL;
596
597 free(surface);
598}
599
600static struct toysurface *
601egl_window_surface_create(struct display *display,
602 struct wl_surface *wl_surface,
603 uint32_t flags,
604 struct rectangle *rectangle)
605{
606 struct egl_window_surface *surface;
607
Pekka Paalanenb3627362012-11-19 17:16:00 +0200608 if (display->dpy == EGL_NO_DISPLAY)
609 return NULL;
610
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200611 surface = calloc(1, sizeof *surface);
612 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100613 return NULL;
614
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200615 surface->base.prepare = egl_window_surface_prepare;
616 surface->base.swap = egl_window_surface_swap;
617 surface->base.acquire = egl_window_surface_acquire;
618 surface->base.release = egl_window_surface_release;
619 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100620
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200621 surface->display = display;
622 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400623
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200624 surface->egl_window = wl_egl_window_create(surface->surface,
625 rectangle->width,
626 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100627
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200628 surface->egl_surface = eglCreateWindowSurface(display->dpy,
629 display->argb_config,
630 surface->egl_window,
631 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface->cairo_surface =
634 cairo_gl_surface_create_for_egl(display->argb_device,
635 surface->egl_surface,
636 rectangle->width,
637 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200639 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100640}
641
Pekka Paalanenb3627362012-11-19 17:16:00 +0200642#else
643
644static struct toysurface *
645egl_window_surface_create(struct display *display,
646 struct wl_surface *wl_surface,
647 uint32_t flags,
648 struct rectangle *rectangle)
649{
650 return NULL;
651}
652
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400653#endif
654
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200655struct shm_surface_data {
656 struct wl_buffer *buffer;
657 struct shm_pool *pool;
658};
659
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400660struct wl_buffer *
661display_get_buffer_for_surface(struct display *display,
662 cairo_surface_t *surface)
663{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200664 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400665
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200666 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400667
668 return data->buffer;
669}
670
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500671static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700672shm_pool_destroy(struct shm_pool *pool);
673
674static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675shm_surface_data_destroy(void *p)
676{
677 struct shm_surface_data *data = p;
678
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200679 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700680 if (data->pool)
681 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300682
683 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400684}
685
Kristian Høgsberg16626282012-04-03 11:21:27 -0400686static struct wl_shm_pool *
687make_shm_pool(struct display *display, int size, void **data)
688{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400689 struct wl_shm_pool *pool;
690 int fd;
691
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300692 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400693 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300694 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
695 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400696 return NULL;
697 }
698
699 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400700 if (*data == MAP_FAILED) {
701 fprintf(stderr, "mmap failed: %m\n");
702 close(fd);
703 return NULL;
704 }
705
706 pool = wl_shm_create_pool(display->shm, fd, size);
707
708 close(fd);
709
710 return pool;
711}
712
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700713static struct shm_pool *
714shm_pool_create(struct display *display, size_t size)
715{
716 struct shm_pool *pool = malloc(sizeof *pool);
717
718 if (!pool)
719 return NULL;
720
721 pool->pool = make_shm_pool(display, size, &pool->data);
722 if (!pool->pool) {
723 free(pool);
724 return NULL;
725 }
726
727 pool->size = size;
728 pool->used = 0;
729
730 return pool;
731}
732
733static void *
734shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
735{
736 if (pool->used + size > pool->size)
737 return NULL;
738
739 *offset = pool->used;
740 pool->used += size;
741
742 return (char *) pool->data + *offset;
743}
744
745/* destroy the pool. this does not unmap the memory though */
746static void
747shm_pool_destroy(struct shm_pool *pool)
748{
749 munmap(pool->data, pool->size);
750 wl_shm_pool_destroy(pool->pool);
751 free(pool);
752}
753
754/* Start allocating from the beginning of the pool again */
755static void
756shm_pool_reset(struct shm_pool *pool)
757{
758 pool->used = 0;
759}
760
761static int
762data_length_for_shm_surface(struct rectangle *rect)
763{
764 int stride;
765
766 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
767 rect->width);
768 return stride * rect->height;
769}
770
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500771static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700772display_create_shm_surface_from_pool(struct display *display,
773 struct rectangle *rectangle,
774 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400775{
776 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400777 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400778 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200779 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700780 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400781 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400782
783 data = malloc(sizeof *data);
784 if (data == NULL)
785 return NULL;
786
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200787 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
788 cairo_format = CAIRO_FORMAT_RGB16_565;
789 else
790 cairo_format = CAIRO_FORMAT_ARGB32;
791
792 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700793 length = stride * rectangle->height;
794 data->pool = NULL;
795 map = shm_pool_allocate(pool, length, &offset);
796
797 if (!map) {
798 free(data);
799 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400800 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400801
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400802 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200803 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804 rectangle->width,
805 rectangle->height,
806 stride);
807
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200808 cairo_surface_set_user_data(surface, &shm_surface_data_key,
809 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200811 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
812 format = WL_SHM_FORMAT_RGB565;
813 else {
814 if (flags & SURFACE_OPAQUE)
815 format = WL_SHM_FORMAT_XRGB8888;
816 else
817 format = WL_SHM_FORMAT_ARGB8888;
818 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400819
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200820 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
821 rectangle->width,
822 rectangle->height,
823 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400824
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700825 return surface;
826}
827
828static cairo_surface_t *
829display_create_shm_surface(struct display *display,
830 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200831 struct shm_pool *alternate_pool,
832 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700833{
834 struct shm_surface_data *data;
835 struct shm_pool *pool;
836 cairo_surface_t *surface;
837
Pekka Paalanen99436862012-11-19 17:15:59 +0200838 if (alternate_pool) {
839 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700840 surface = display_create_shm_surface_from_pool(display,
841 rectangle,
842 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200843 alternate_pool);
844 if (surface) {
845 data = cairo_surface_get_user_data(surface,
846 &shm_surface_data_key);
847 goto out;
848 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700849 }
850
851 pool = shm_pool_create(display,
852 data_length_for_shm_surface(rectangle));
853 if (!pool)
854 return NULL;
855
856 surface =
857 display_create_shm_surface_from_pool(display, rectangle,
858 flags, pool);
859
860 if (!surface) {
861 shm_pool_destroy(pool);
862 return NULL;
863 }
864
865 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200866 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700867 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400868
Pekka Paalanen99436862012-11-19 17:15:59 +0200869out:
870 if (data_ret)
871 *data_ret = data;
872
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400873 return surface;
874}
875
nobled7b87cb02011-02-01 18:51:47 +0000876static int
877check_size(struct rectangle *rect)
878{
879 if (rect->width && rect->height)
880 return 0;
881
882 fprintf(stderr, "tried to create surface of "
883 "width: %d, height: %d\n", rect->width, rect->height);
884 return -1;
885}
886
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400887cairo_surface_t *
888display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100889 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400890 struct rectangle *rectangle,
891 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400892{
nobled7b87cb02011-02-01 18:51:47 +0000893 if (check_size(rectangle) < 0)
894 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200895
896 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200897 return display_create_shm_surface(display, rectangle, flags,
898 NULL, NULL);
899}
900
Pekka Paalanena4eda732012-11-19 17:16:02 +0200901struct shm_surface_leaf {
902 cairo_surface_t *cairo_surface;
903 /* 'data' is automatically destroyed, when 'cairo_surface' is */
904 struct shm_surface_data *data;
905
906 struct shm_pool *resize_pool;
907 int busy;
908};
909
910static void
911shm_surface_leaf_release(struct shm_surface_leaf *leaf)
912{
913 if (leaf->cairo_surface)
914 cairo_surface_destroy(leaf->cairo_surface);
915 /* leaf->data already destroyed via cairo private */
916
917 if (leaf->resize_pool)
918 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200919
920 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200921}
922
Pekka Paalanenaef02542013-04-25 13:57:47 +0300923#define MAX_LEAVES 3
924
Pekka Paalanen99436862012-11-19 17:15:59 +0200925struct shm_surface {
926 struct toysurface base;
927 struct display *display;
928 struct wl_surface *surface;
929 uint32_t flags;
930 int dx, dy;
931
Pekka Paalanenaef02542013-04-25 13:57:47 +0300932 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200933 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200934};
935
936static struct shm_surface *
937to_shm_surface(struct toysurface *base)
938{
939 return container_of(base, struct shm_surface, base);
940}
941
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300943shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
944{
945#ifdef DEBUG
946 struct shm_surface_leaf *leaf;
947 char bufs[MAX_LEAVES + 1];
948 int i;
949
950 for (i = 0; i < MAX_LEAVES; i++) {
951 leaf = &surface->leaf[i];
952
953 if (leaf->busy)
954 bufs[i] = 'b';
955 else if (leaf->cairo_surface)
956 bufs[i] = 'a';
957 else
958 bufs[i] = ' ';
959 }
960
961 bufs[MAX_LEAVES] = '\0';
962 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
963#endif
964}
965
966static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200967shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
968{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200969 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300970 struct shm_surface_leaf *leaf;
971 int i;
972 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300973
974 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200975
Pekka Paalanenaef02542013-04-25 13:57:47 +0300976 for (i = 0; i < MAX_LEAVES; i++) {
977 leaf = &surface->leaf[i];
978 if (leaf->data && leaf->data->buffer == buffer) {
979 leaf->busy = 0;
980 break;
981 }
982 }
983 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200984
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 /* Leave one free leaf with storage, release others */
986 free_found = 0;
987 for (i = 0; i < MAX_LEAVES; i++) {
988 leaf = &surface->leaf[i];
989
990 if (!leaf->cairo_surface || leaf->busy)
991 continue;
992
993 if (!free_found)
994 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300995 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300996 shm_surface_leaf_release(leaf);
997 }
Pekka Paalanen71233882013-04-25 13:57:53 +0300998
Pekka Paalanen97777442013-05-22 10:20:05 +0300999 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001000}
1001
1002static const struct wl_buffer_listener shm_surface_buffer_listener = {
1003 shm_surface_buffer_release
1004};
1005
Pekka Paalanen99436862012-11-19 17:15:59 +02001006static cairo_surface_t *
1007shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001008 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001009 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001010{
Pekka Paalanenec076692012-11-30 13:37:27 +02001011 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001012 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001013 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001014 struct shm_surface_leaf *leaf = NULL;
1015 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001016
1017 surface->dx = dx;
1018 surface->dy = dy;
1019
Pekka Paalanenaef02542013-04-25 13:57:47 +03001020 /* pick a free buffer, preferrably one that already has storage */
1021 for (i = 0; i < MAX_LEAVES; i++) {
1022 if (surface->leaf[i].busy)
1023 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001024
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 if (!leaf || surface->leaf[i].cairo_surface)
1026 leaf = &surface->leaf[i];
1027 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001028 DBG_OBJ(surface->surface, "pick leaf %d\n",
1029 (int)(leaf - &surface->leaf[0]));
1030
Pekka Paalanenaef02542013-04-25 13:57:47 +03001031 if (!leaf) {
1032 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001033 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001034 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001035 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001036 }
1037
Pekka Paalanena4eda732012-11-19 17:16:02 +02001038 if (!resize_hint && leaf->resize_pool) {
1039 cairo_surface_destroy(leaf->cairo_surface);
1040 leaf->cairo_surface = NULL;
1041 shm_pool_destroy(leaf->resize_pool);
1042 leaf->resize_pool = NULL;
1043 }
1044
Alexander Larsson1818e312013-05-22 14:41:31 +02001045 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1046
Pekka Paalanena4eda732012-11-19 17:16:02 +02001047 if (leaf->cairo_surface &&
1048 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1049 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001050 goto out;
1051
Pekka Paalanena4eda732012-11-19 17:16:02 +02001052 if (leaf->cairo_surface)
1053 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001054
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001055#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001056 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001057 /* Create a big pool to allocate from, while continuously
1058 * resizing. Mmapping a new pool in the server
1059 * is relatively expensive, so reusing a pool performs
1060 * better, but may temporarily reserve unneeded memory.
1061 */
1062 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001063 leaf->resize_pool = shm_pool_create(surface->display,
1064 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001065 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001066#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001067
Alexander Larsson1818e312013-05-22 14:41:31 +02001068 rect.width = width;
1069 rect.height = height;
1070
Pekka Paalanena4eda732012-11-19 17:16:02 +02001071 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001072 display_create_shm_surface(surface->display, &rect,
1073 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 leaf->resize_pool,
1075 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001076 if (!leaf->cairo_surface)
1077 return NULL;
1078
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001080 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001081
1082out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001083 surface->current = leaf;
1084
1085 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001086}
1087
1088static void
1089shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001090 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001091 struct rectangle *server_allocation)
1092{
1093 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001094 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001095
1096 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001097 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001098 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001099 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001100
Alexander Larsson1818e312013-05-22 14:41:31 +02001101 buffer_to_surface_size (buffer_transform, buffer_scale,
1102 &server_allocation->width,
1103 &server_allocation->height);
1104
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001106 surface->dx, surface->dy);
1107 wl_surface_damage(surface->surface, 0, 0,
1108 server_allocation->width, server_allocation->height);
1109 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001110
Pekka Paalanen71233882013-04-25 13:57:53 +03001111 DBG_OBJ(surface->surface, "leaf %d busy\n",
1112 (int)(leaf - &surface->leaf[0]));
1113
Pekka Paalanena4eda732012-11-19 17:16:02 +02001114 leaf->busy = 1;
1115 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001116}
1117
1118static int
1119shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1120{
1121 return -1;
1122}
1123
1124static void
1125shm_surface_release(struct toysurface *base)
1126{
1127}
1128
1129static void
1130shm_surface_destroy(struct toysurface *base)
1131{
1132 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001133 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001134
Pekka Paalanenaef02542013-04-25 13:57:47 +03001135 for (i = 0; i < MAX_LEAVES; i++)
1136 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001137
1138 free(surface);
1139}
1140
1141static struct toysurface *
1142shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1143 uint32_t flags, struct rectangle *rectangle)
1144{
1145 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001146 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001147
Brian Lovinbc919262013-08-07 15:34:59 -07001148 surface = xmalloc(sizeof *surface);
1149 memset(surface, 0, sizeof *surface);
1150
Pekka Paalanen99436862012-11-19 17:15:59 +02001151 if (!surface)
1152 return NULL;
1153
1154 surface->base.prepare = shm_surface_prepare;
1155 surface->base.swap = shm_surface_swap;
1156 surface->base.acquire = shm_surface_acquire;
1157 surface->base.release = shm_surface_release;
1158 surface->base.destroy = shm_surface_destroy;
1159
1160 surface->display = display;
1161 surface->surface = wl_surface;
1162 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001163
1164 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001165}
1166
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001167/*
1168 * The following correspondences between file names and cursors was copied
1169 * from: https://bugs.kde.org/attachment.cgi?id=67313
1170 */
1171
1172static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001173 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001174 "sw-resize",
1175 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001176};
1177
1178static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001179 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001180 "se-resize",
1181 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001182};
1183
1184static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001185 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001186 "s-resize",
1187 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001188};
1189
1190static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001191 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192 "closedhand",
1193 "208530c400c041818281048008011002"
1194};
1195
1196static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001197 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001198 "default",
1199 "top_left_arrow",
1200 "left-arrow"
1201};
1202
1203static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001204 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001205 "w-resize",
1206 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001207};
1208
1209static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001210 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001211 "e-resize",
1212 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001213};
1214
1215static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001216 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001217 "nw-resize",
1218 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001219};
1220
1221static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001222 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001223 "ne-resize",
1224 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001225};
1226
1227static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001228 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001229 "n-resize",
1230 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001231};
1232
1233static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001234 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235 "ibeam",
1236 "text"
1237};
1238
1239static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001240 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241 "pointer",
1242 "pointing_hand",
1243 "e29285e634086352946a0e7090d73106"
1244};
1245
1246static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001247 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001248 "wait",
1249 "0426c94ea35c87780ff01dc239897213"
1250};
1251
1252struct cursor_alternatives {
1253 const char **names;
1254 size_t count;
1255};
1256
1257static const struct cursor_alternatives cursors[] = {
1258 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1259 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1260 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1261 {grabbings, ARRAY_LENGTH(grabbings)},
1262 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1263 {left_sides, ARRAY_LENGTH(left_sides)},
1264 {right_sides, ARRAY_LENGTH(right_sides)},
1265 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1266 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1267 {top_sides, ARRAY_LENGTH(top_sides)},
1268 {xterms, ARRAY_LENGTH(xterms)},
1269 {hand1s, ARRAY_LENGTH(hand1s)},
1270 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001271};
1272
1273static void
1274create_cursors(struct display *display)
1275{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001276 struct weston_config *config;
1277 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001278 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001279 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001280 unsigned int i, j;
1281 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001282
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001283 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001284 s = weston_config_get_section(config, "shell", NULL, NULL);
1285 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1286 weston_config_section_get_int(s, "cursor-size", &size, 32);
1287 weston_config_destroy(config);
1288
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001289 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001290 if (!display->cursor_theme) {
1291 fprintf(stderr, "could not load theme '%s'\n", theme);
1292 return;
1293 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001294 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001295 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001296 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001297
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001298 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001299 cursor = NULL;
1300 for (j = 0; !cursor && j < cursors[i].count; ++j)
1301 cursor = wl_cursor_theme_get_cursor(
1302 display->cursor_theme, cursors[i].names[j]);
1303
1304 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001305 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001306 cursors[i].names[0]);
1307
1308 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001309 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001310}
1311
1312static void
1313destroy_cursors(struct display *display)
1314{
1315 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001316 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001317}
1318
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001319struct wl_cursor_image *
1320display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001321{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001322 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001323
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001324 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001325}
1326
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001327static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001328surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001329{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001330 if (!surface->cairo_surface)
1331 return;
1332
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001333 if (surface->opaque_region) {
1334 wl_surface_set_opaque_region(surface->surface,
1335 surface->opaque_region);
1336 wl_region_destroy(surface->opaque_region);
1337 surface->opaque_region = NULL;
1338 }
1339
1340 if (surface->input_region) {
1341 wl_surface_set_input_region(surface->surface,
1342 surface->input_region);
1343 wl_region_destroy(surface->input_region);
1344 surface->input_region = NULL;
1345 }
1346
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001347 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001348 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001349 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350
Pekka Paalanen89dee002013-02-13 16:17:20 +02001351 cairo_surface_destroy(surface->cairo_surface);
1352 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001353}
1354
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001355int
1356window_has_focus(struct window *window)
1357{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001358 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001359}
1360
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001361static void
1362window_close(struct window *window)
1363{
1364 if (window->close_handler)
1365 window->close_handler(window->user_data);
1366 else
1367 display_exit(window->display);
1368}
1369
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001370struct display *
1371window_get_display(struct window *window)
1372{
1373 return window->display;
1374}
1375
Pekka Paalanen89dee002013-02-13 16:17:20 +02001376static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001377surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001378{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001379 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001380 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001381
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001382 if (!surface->toysurface && display->dpy &&
1383 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001384 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001385 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001386 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001387 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001388 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001389 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001390
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001391 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001392 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001393 surface->surface,
1394 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001395
Pekka Paalanen89dee002013-02-13 16:17:20 +02001396 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001397 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001398 allocation.width, allocation.height, flags,
1399 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001400}
1401
1402static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001403window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001404{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001405 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001406 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407
Pekka Paalanenec076692012-11-30 13:37:27 +02001408 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001410
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001411 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1412 flags |= SURFACE_HINT_RGB565;
1413
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001414 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001415}
1416
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001417int
1418window_get_buffer_transform(struct window *window)
1419{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001420 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001421}
1422
1423void
1424window_set_buffer_transform(struct window *window,
1425 enum wl_output_transform transform)
1426{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001427 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001428 wl_surface_set_buffer_transform(window->main_surface->surface,
1429 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001430}
1431
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001432void
1433window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001434 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001435{
1436 window->main_surface->buffer_scale = scale;
1437 wl_surface_set_buffer_scale(window->main_surface->surface,
1438 scale);
1439}
1440
1441uint32_t
1442window_get_buffer_scale(struct window *window)
1443{
1444 return window->main_surface->buffer_scale;
1445}
1446
Alexander Larssond68f5232013-05-22 14:41:33 +02001447uint32_t
1448window_get_output_scale(struct window *window)
1449{
1450 struct window_output *window_output;
1451 struct window_output *window_output_tmp;
1452 int scale = 1;
1453
1454 wl_list_for_each_safe(window_output, window_output_tmp,
1455 &window->window_output_list, link) {
1456 if (window_output->output->scale > scale)
1457 scale = window_output->output->scale;
1458 }
1459
1460 return scale;
1461}
1462
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001463static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001464
Pekka Paalanen4e373742013-02-13 16:17:13 +02001465static void
1466surface_destroy(struct surface *surface)
1467{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001468 if (surface->frame_cb)
1469 wl_callback_destroy(surface->frame_cb);
1470
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001471 if (surface->input_region)
1472 wl_region_destroy(surface->input_region);
1473
1474 if (surface->opaque_region)
1475 wl_region_destroy(surface->opaque_region);
1476
Pekka Paalanen35e82632013-04-25 13:57:48 +03001477 if (surface->subsurface)
1478 wl_subsurface_destroy(surface->subsurface);
1479
Pekka Paalanen4e373742013-02-13 16:17:13 +02001480 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001481
1482 if (surface->toysurface)
1483 surface->toysurface->destroy(surface->toysurface);
1484
Pekka Paalanen35e82632013-04-25 13:57:48 +03001485 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001486 free(surface);
1487}
1488
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001489void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001490window_destroy(struct window *window)
1491{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001492 struct display *display = window->display;
1493 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001494 struct window_output *window_output;
1495 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001496
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001497 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001498
Rusty Lynch1084da52013-08-15 09:10:08 -07001499 wl_list_for_each(input, &display->input_list, link) {
1500 if (input->touch_focus == window)
1501 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001502 if (input->pointer_focus == window)
1503 input->pointer_focus = NULL;
1504 if (input->keyboard_focus == window)
1505 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001506 if (input->focus_widget &&
1507 input->focus_widget->window == window)
1508 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001509 }
1510
Rob Bradford7507b572012-05-15 17:55:34 +01001511 wl_list_for_each_safe(window_output, window_output_tmp,
1512 &window->window_output_list, link) {
1513 free (window_output);
1514 }
1515
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001516 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001517 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001518
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001519 if (window->xdg_surface)
1520 xdg_surface_destroy(window->xdg_surface);
1521 if (window->xdg_popup)
1522 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001523
1524 surface_destroy(window->main_surface);
1525
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001526 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001527
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001528 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001529 free(window);
1530}
1531
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001532static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001533widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001534{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001535 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001536
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001537 wl_list_for_each(child, &widget->child_list, link) {
1538 target = widget_find_widget(child, x, y);
1539 if (target)
1540 return target;
1541 }
1542
1543 if (widget->allocation.x <= x &&
1544 x < widget->allocation.x + widget->allocation.width &&
1545 widget->allocation.y <= y &&
1546 y < widget->allocation.y + widget->allocation.height) {
1547 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001548 }
1549
1550 return NULL;
1551}
1552
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001553static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001554window_find_widget(struct window *window, int32_t x, int32_t y)
1555{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001556 struct surface *surface;
1557 struct widget *widget;
1558
1559 wl_list_for_each(surface, &window->subsurface_list, link) {
1560 widget = widget_find_widget(surface->widget, x, y);
1561 if (widget)
1562 return widget;
1563 }
1564
1565 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001566}
1567
1568static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001569widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001570{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001571 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001572
Peter Huttererf3d62272013-08-08 11:57:05 +10001573 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001574 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001575 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001576 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001577 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001578 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001579 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001580 widget->tooltip = NULL;
1581 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001582 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001583 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001584
1585 return widget;
1586}
1587
1588struct widget *
1589window_add_widget(struct window *window, void *data)
1590{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001591 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001592
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001593 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001594 wl_list_init(&widget->link);
1595 window->main_surface->widget = widget;
1596
1597 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001598}
1599
1600struct widget *
1601widget_add_widget(struct widget *parent, void *data)
1602{
1603 struct widget *widget;
1604
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001605 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001606 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001607
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001608 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001609}
1610
1611void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001613{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001614 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001615 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001616 struct input *input;
1617
Pekka Paalanen35e82632013-04-25 13:57:48 +03001618 /* Destroy the sub-surface along with the root widget */
1619 if (surface->widget == widget && surface->subsurface)
1620 surface_destroy(widget->surface);
1621
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001622 if (widget->tooltip)
1623 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001624
Pekka Paalanene156fb62012-01-19 13:51:38 +02001625 wl_list_for_each(input, &display->input_list, link) {
1626 if (input->focus_widget == widget)
1627 input->focus_widget = NULL;
1628 }
1629
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001630 wl_list_remove(&widget->link);
1631 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001632}
1633
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001634void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001635widget_set_default_cursor(struct widget *widget, int cursor)
1636{
1637 widget->default_cursor = cursor;
1638}
1639
1640void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001641widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001642{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001643 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001644}
1645
1646void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001647widget_set_size(struct widget *widget, int32_t width, int32_t height)
1648{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001649 widget->allocation.width = width;
1650 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001651}
1652
1653void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001654widget_set_allocation(struct widget *widget,
1655 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001656{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001657 widget->allocation.x = x;
1658 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001659 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001660}
1661
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001662void
1663widget_set_transparent(struct widget *widget, int transparent)
1664{
1665 widget->opaque = !transparent;
1666}
1667
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001668void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001669widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001670{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001671 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001672}
1673
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001674static cairo_surface_t *
1675widget_get_cairo_surface(struct widget *widget)
1676{
1677 struct surface *surface = widget->surface;
1678 struct window *window = widget->window;
1679
Neil Roberts97b747c2013-12-19 16:17:12 +00001680 assert(widget->use_cairo);
1681
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001682 if (!surface->cairo_surface) {
1683 if (surface == window->main_surface)
1684 window_create_main_surface(window);
1685 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001686 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001687 }
1688
1689 return surface->cairo_surface;
1690}
1691
Alexander Larsson15901f02013-05-22 14:41:25 +02001692static void
1693widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1694{
1695 struct surface *surface = widget->surface;
1696 double angle;
1697 cairo_matrix_t m;
1698 enum wl_output_transform transform;
1699 int surface_width, surface_height;
1700 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001701 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001702
1703 surface_width = surface->allocation.width;
1704 surface_height = surface->allocation.height;
1705
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001706 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001707 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001708
Alexander Larsson15901f02013-05-22 14:41:25 +02001709 switch (transform) {
1710 case WL_OUTPUT_TRANSFORM_FLIPPED:
1711 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1713 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1714 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1715 break;
1716 default:
1717 cairo_matrix_init_identity(&m);
1718 break;
1719 }
1720
1721 switch (transform) {
1722 case WL_OUTPUT_TRANSFORM_NORMAL:
1723 default:
1724 angle = 0;
1725 translate_x = 0;
1726 translate_y = 0;
1727 break;
1728 case WL_OUTPUT_TRANSFORM_FLIPPED:
1729 angle = 0;
1730 translate_x = surface_width;
1731 translate_y = 0;
1732 break;
1733 case WL_OUTPUT_TRANSFORM_90:
1734 angle = M_PI_2;
1735 translate_x = surface_height;
1736 translate_y = 0;
1737 break;
1738 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1739 angle = M_PI_2;
1740 translate_x = surface_height;
1741 translate_y = surface_width;
1742 break;
1743 case WL_OUTPUT_TRANSFORM_180:
1744 angle = M_PI;
1745 translate_x = surface_width;
1746 translate_y = surface_height;
1747 break;
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1749 angle = M_PI;
1750 translate_x = 0;
1751 translate_y = surface_height;
1752 break;
1753 case WL_OUTPUT_TRANSFORM_270:
1754 angle = M_PI + M_PI_2;
1755 translate_x = 0;
1756 translate_y = surface_width;
1757 break;
1758 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1759 angle = M_PI + M_PI_2;
1760 translate_x = 0;
1761 translate_y = 0;
1762 break;
1763 }
1764
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001765 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001766 cairo_translate(cr, translate_x, translate_y);
1767 cairo_rotate(cr, angle);
1768 cairo_transform(cr, &m);
1769}
1770
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001771cairo_t *
1772widget_cairo_create(struct widget *widget)
1773{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001774 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001775 cairo_surface_t *cairo_surface;
1776 cairo_t *cr;
1777
1778 cairo_surface = widget_get_cairo_surface(widget);
1779 cr = cairo_create(cairo_surface);
1780
Alexander Larsson15901f02013-05-22 14:41:25 +02001781 widget_cairo_update_transform(widget, cr);
1782
Pekka Paalanen35e82632013-04-25 13:57:48 +03001783 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1784
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001785 return cr;
1786}
1787
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001788struct wl_surface *
1789widget_get_wl_surface(struct widget *widget)
1790{
1791 return widget->surface->surface;
1792}
1793
Neil Roberts5e10a042013-09-09 00:40:17 +01001794struct wl_subsurface *
1795widget_get_wl_subsurface(struct widget *widget)
1796{
1797 return widget->surface->subsurface;
1798}
1799
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001800uint32_t
1801widget_get_last_time(struct widget *widget)
1802{
1803 return widget->surface->last_time;
1804}
1805
1806void
1807widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1808{
1809 struct wl_compositor *comp = widget->window->display->compositor;
1810 struct surface *surface = widget->surface;
1811
1812 if (!surface->input_region)
1813 surface->input_region = wl_compositor_create_region(comp);
1814
1815 if (rect) {
1816 wl_region_add(surface->input_region,
1817 rect->x, rect->y, rect->width, rect->height);
1818 }
1819}
1820
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001821void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001822widget_set_resize_handler(struct widget *widget,
1823 widget_resize_handler_t handler)
1824{
1825 widget->resize_handler = handler;
1826}
1827
1828void
1829widget_set_redraw_handler(struct widget *widget,
1830 widget_redraw_handler_t handler)
1831{
1832 widget->redraw_handler = handler;
1833}
1834
1835void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001836widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001837{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001838 widget->enter_handler = handler;
1839}
1840
1841void
1842widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1843{
1844 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001845}
1846
1847void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001848widget_set_motion_handler(struct widget *widget,
1849 widget_motion_handler_t handler)
1850{
1851 widget->motion_handler = handler;
1852}
1853
1854void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001855widget_set_button_handler(struct widget *widget,
1856 widget_button_handler_t handler)
1857{
1858 widget->button_handler = handler;
1859}
1860
1861void
Rusty Lynch041815a2013-08-08 21:20:38 -07001862widget_set_touch_up_handler(struct widget *widget,
1863 widget_touch_up_handler_t handler)
1864{
1865 widget->touch_up_handler = handler;
1866}
1867
1868void
1869widget_set_touch_down_handler(struct widget *widget,
1870 widget_touch_down_handler_t handler)
1871{
1872 widget->touch_down_handler = handler;
1873}
1874
1875void
1876widget_set_touch_motion_handler(struct widget *widget,
1877 widget_touch_motion_handler_t handler)
1878{
1879 widget->touch_motion_handler = handler;
1880}
1881
1882void
1883widget_set_touch_frame_handler(struct widget *widget,
1884 widget_touch_frame_handler_t handler)
1885{
1886 widget->touch_frame_handler = handler;
1887}
1888
1889void
1890widget_set_touch_cancel_handler(struct widget *widget,
1891 widget_touch_cancel_handler_t handler)
1892{
1893 widget->touch_cancel_handler = handler;
1894}
1895
1896void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001897widget_set_axis_handler(struct widget *widget,
1898 widget_axis_handler_t handler)
1899{
1900 widget->axis_handler = handler;
1901}
1902
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001903static void
1904window_schedule_redraw_task(struct window *window);
1905
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001906void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001907widget_schedule_redraw(struct widget *widget)
1908{
Pekka Paalanen71233882013-04-25 13:57:53 +03001909 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001910 widget->surface->redraw_needed = 1;
1911 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001912}
1913
Neil Roberts97b747c2013-12-19 16:17:12 +00001914void
1915widget_set_use_cairo(struct widget *widget,
1916 int use_cairo)
1917{
1918 widget->use_cairo = use_cairo;
1919}
1920
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001921cairo_surface_t *
1922window_get_surface(struct window *window)
1923{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001924 cairo_surface_t *cairo_surface;
1925
1926 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1927
1928 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001929}
1930
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001931struct wl_surface *
1932window_get_wl_surface(struct window *window)
1933{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001934 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001935}
1936
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001937static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001938tooltip_redraw_handler(struct widget *widget, void *data)
1939{
1940 cairo_t *cr;
1941 const int32_t r = 3;
1942 struct tooltip *tooltip = data;
1943 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001944
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001945 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001946 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001947 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1948 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1949 cairo_paint(cr);
1950
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001951 width = widget->allocation.width;
1952 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001953 rounded_rect(cr, 0, 0, width, height, r);
1954
1955 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1956 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1957 cairo_fill(cr);
1958
1959 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1960 cairo_move_to(cr, 10, 16);
1961 cairo_show_text(cr, tooltip->entry);
1962 cairo_destroy(cr);
1963}
1964
1965static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001966get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001967{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001968 cairo_t *cr;
1969 cairo_text_extents_t extents;
1970
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001971 /* Use the dummy_surface because tooltip's surface was not
1972 * created yet, and parent does not have a valid surface
1973 * outside repaint, either.
1974 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001975 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001976 cairo_text_extents(cr, tooltip->entry, &extents);
1977 cairo_destroy(cr);
1978
1979 return extents;
1980}
1981
1982static int
1983window_create_tooltip(struct tooltip *tooltip)
1984{
1985 struct widget *parent = tooltip->parent;
1986 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001987 const int offset_y = 27;
1988 const int margin = 3;
1989 cairo_text_extents_t extents;
1990
1991 if (tooltip->widget)
1992 return 0;
1993
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001994 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001995
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001996 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001997 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001998 widget_set_allocation(tooltip->widget,
1999 tooltip->x, tooltip->y + offset_y,
2000 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002001
2002 return 0;
2003}
2004
2005void
2006widget_destroy_tooltip(struct widget *parent)
2007{
2008 struct tooltip *tooltip = parent->tooltip;
2009
2010 parent->tooltip_count = 0;
2011 if (!tooltip)
2012 return;
2013
2014 if (tooltip->widget) {
2015 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002016 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002017 }
2018
2019 close(tooltip->tooltip_fd);
2020 free(tooltip->entry);
2021 free(tooltip);
2022 parent->tooltip = NULL;
2023}
2024
2025static void
2026tooltip_func(struct task *task, uint32_t events)
2027{
2028 struct tooltip *tooltip =
2029 container_of(task, struct tooltip, tooltip_task);
2030 uint64_t exp;
2031
Martin Olsson8df662a2012-07-08 03:03:47 +02002032 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2033 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002034 window_create_tooltip(tooltip);
2035}
2036
2037#define TOOLTIP_TIMEOUT 500
2038static int
2039tooltip_timer_reset(struct tooltip *tooltip)
2040{
2041 struct itimerspec its;
2042
2043 its.it_interval.tv_sec = 0;
2044 its.it_interval.tv_nsec = 0;
2045 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2046 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2047 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2048 fprintf(stderr, "could not set timerfd\n: %m");
2049 return -1;
2050 }
2051
2052 return 0;
2053}
2054
2055int
2056widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2057{
2058 struct tooltip *tooltip = parent->tooltip;
2059
2060 parent->tooltip_count++;
2061 if (tooltip) {
2062 tooltip->x = x;
2063 tooltip->y = y;
2064 tooltip_timer_reset(tooltip);
2065 return 0;
2066 }
2067
2068 /* the handler might be triggered too fast via input device motion, so
2069 * we need this check here to make sure tooltip is fully initialized */
2070 if (parent->tooltip_count > 1)
2071 return 0;
2072
2073 tooltip = malloc(sizeof *tooltip);
2074 if (!tooltip)
2075 return -1;
2076
2077 parent->tooltip = tooltip;
2078 tooltip->parent = parent;
2079 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002080 tooltip->x = x;
2081 tooltip->y = y;
2082 tooltip->entry = strdup(entry);
2083 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2084 if (tooltip->tooltip_fd < 0) {
2085 fprintf(stderr, "could not create timerfd\n: %m");
2086 return -1;
2087 }
2088
2089 tooltip->tooltip_task.run = tooltip_func;
2090 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2091 EPOLLIN, &tooltip->tooltip_task);
2092 tooltip_timer_reset(tooltip);
2093
2094 return 0;
2095}
2096
2097static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002098workspace_manager_state(void *data,
2099 struct workspace_manager *workspace_manager,
2100 uint32_t current,
2101 uint32_t count)
2102{
2103 struct display *display = data;
2104
2105 display->workspace = current;
2106 display->workspace_count = count;
2107}
2108
2109static const struct workspace_manager_listener workspace_manager_listener = {
2110 workspace_manager_state
2111};
2112
2113static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002114frame_resize_handler(struct widget *widget,
2115 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002116{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002117 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002118 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002119 struct rectangle interior;
2120 struct rectangle input;
2121 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002122
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002123 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002124 interior.x = 0;
2125 interior.y = 0;
2126 interior.width = width;
2127 interior.height = height;
2128 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002129 frame_resize(frame->frame, width, height);
2130 frame_interior(frame->frame, &interior.x, &interior.y,
2131 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002132 }
2133
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002134 widget_set_allocation(child, interior.x, interior.y,
2135 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002136
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002137 if (child->resize_handler) {
2138 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139 child->user_data);
2140
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002141 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002142 width = child->allocation.width;
2143 height = child->allocation.height;
2144 } else {
2145 frame_resize_inside(frame->frame,
2146 child->allocation.width,
2147 child->allocation.height);
2148 width = frame_width(frame->frame);
2149 height = frame_height(frame->frame);
2150 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002151 }
2152
Scott Moreauf7e498c2012-05-14 11:39:29 -06002153 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002154
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002155 widget->surface->input_region =
2156 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002157 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002158 frame_input_rect(frame->frame, &input.x, &input.y,
2159 &input.width, &input.height);
2160 wl_region_add(widget->surface->input_region,
2161 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002162 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002163 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002164 }
2165
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002166 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002167
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002168 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002169 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002170 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2171 &opaque.width, &opaque.height);
2172
2173 wl_region_add(widget->surface->opaque_region,
2174 opaque.x, opaque.y,
2175 opaque.width, opaque.height);
2176 } else {
2177 wl_region_add(widget->surface->opaque_region,
2178 0, 0, width, height);
2179 }
Martin Minarik1998b152012-05-10 02:04:35 +02002180 }
2181
Martin Minarik1998b152012-05-10 02:04:35 +02002182
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002183 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002184}
2185
2186static void
2187frame_redraw_handler(struct widget *widget, void *data)
2188{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002189 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002190 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002191 struct window *window = widget->window;
2192
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002193 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002194 return;
2195
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002196 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002198 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002199
2200 cairo_destroy(cr);
2201}
2202
2203static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002204frame_get_pointer_image_for_location(struct window_frame *frame,
2205 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002206{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002207 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002208
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002209 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002210 return CURSOR_LEFT_PTR;
2211
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002212 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002213 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002214 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002215 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002216 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002217 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002218 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002219 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002220 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002221 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002222 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002223 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002224 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002225 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002226 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002227 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002228 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002229 case THEME_LOCATION_EXTERIOR:
2230 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002231 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002232 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002233 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002234}
2235
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002236void
2237window_show_frame_menu(struct window *window,
2238 struct input *input, uint32_t time)
2239{
2240 int32_t x, y;
2241
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04002242 if (window->xdg_surface) {
2243 input_get_position(input, &x, &y);
2244 xdg_surface_show_window_menu(window->xdg_surface,
2245 input_get_seat(input),
2246 window->display->serial,
2247 x - 10, y - 10);
2248 }
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002249}
2250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002251static int
2252frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002253 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002254{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002255 struct window_frame *frame = data;
2256 enum theme_location location;
2257
2258 location = frame_pointer_enter(frame->frame, input, x, y);
2259 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2260 widget_schedule_redraw(frame->widget);
2261
2262 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002263}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002264
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002265static int
2266frame_motion_handler(struct widget *widget,
2267 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002268 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002269{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002270 struct window_frame *frame = data;
2271 enum theme_location location;
2272
2273 location = frame_pointer_motion(frame->frame, input, x, y);
2274 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2275 widget_schedule_redraw(frame->widget);
2276
2277 return frame_get_pointer_image_for_location(data, location);
2278}
2279
2280static void
2281frame_leave_handler(struct widget *widget,
2282 struct input *input, void *data)
2283{
2284 struct window_frame *frame = data;
2285
2286 frame_pointer_leave(frame->frame, input);
2287 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2288 widget_schedule_redraw(frame->widget);
2289}
2290
2291static void
2292frame_handle_status(struct window_frame *frame, struct input *input,
2293 uint32_t time, enum theme_location location)
2294{
2295 struct window *window = frame->widget->window;
2296 uint32_t status;
2297
2298 status = frame_status(frame->frame);
2299 if (status & FRAME_STATUS_REPAINT)
2300 widget_schedule_redraw(frame->widget);
2301
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002302 if (status & FRAME_STATUS_MINIMIZE) {
2303 window_set_minimized(window);
2304 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2305 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002306
2307 if (status & FRAME_STATUS_MENU) {
2308 window_show_frame_menu(window, input, time);
2309 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2310 }
2311
2312 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002313 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002314 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2315 }
2316
2317 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002318 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002319 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002320 }
2321
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002322 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002323 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002324 xdg_surface_move(window->xdg_surface,
2325 input_get_seat(input),
2326 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002327
2328 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2329 }
2330
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002331 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002332 input_ungrab(input);
2333
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002334 xdg_surface_resize(window->xdg_surface,
2335 input_get_seat(input),
2336 window->display->serial,
2337 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002338
2339 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2340 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002342
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002343static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002344frame_button_handler(struct widget *widget,
2345 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002346 uint32_t button, enum wl_pointer_button_state state,
2347 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002348
2349{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002350 struct window_frame *frame = data;
2351 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002352
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002353 location = frame_pointer_button(frame->frame, input, button, state);
2354 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002355}
2356
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002357static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002358frame_touch_down_handler(struct widget *widget, struct input *input,
2359 uint32_t serial, uint32_t time, int32_t id,
2360 float x, float y, void *data)
2361{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002362 struct window_frame *frame = data;
2363
2364 frame_touch_down(frame->frame, input, id, x, y);
2365 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2366}
2367
2368static void
2369frame_touch_up_handler(struct widget *widget,
2370 struct input *input, uint32_t serial, uint32_t time,
2371 int32_t id, void *data)
2372{
2373 struct window_frame *frame = data;
2374
2375 frame_touch_up(frame->frame, input, id);
2376 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002377}
2378
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002379struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002380window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002381{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002382 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002383 uint32_t buttons;
2384
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002385 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002386 buttons = FRAME_BUTTON_NONE;
2387 } else {
2388 buttons = FRAME_BUTTON_ALL;
2389 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002390
Peter Huttererf3d62272013-08-08 11:57:05 +10002391 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002392 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002393 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002394
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002395 frame->widget = window_add_widget(window, frame);
2396 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002397
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002398 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2399 widget_set_resize_handler(frame->widget, frame_resize_handler);
2400 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002401 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002402 widget_set_motion_handler(frame->widget, frame_motion_handler);
2403 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002404 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002405 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002406
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002407 window->frame = frame;
2408
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002409 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002410}
2411
Kristian Høgsberga1627922012-06-20 17:30:03 -04002412void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002413window_frame_set_child_size(struct widget *widget, int child_width,
2414 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002415{
2416 struct display *display = widget->window->display;
2417 struct theme *t = display->theme;
2418 int decoration_width, decoration_height;
2419 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002420 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002421
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002422 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002423 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002424 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002425 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002426
2427 width = child_width + decoration_width;
2428 height = child_height + decoration_height;
2429 } else {
2430 width = child_width;
2431 height = child_height;
2432 }
2433
2434 window_schedule_resize(widget->window, width, height);
2435}
2436
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002437static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002438window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002439{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002440 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002441
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002442 /* frame->child must be destroyed by the application */
2443 widget_destroy(frame->widget);
2444 free(frame);
2445}
2446
2447static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002448input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002449 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002450{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002451 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002452 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002453
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002454 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002455 return;
2456
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002457 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002458 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002459 widget = old;
2460 if (input->grab)
2461 widget = input->grab;
2462 if (widget->leave_handler)
2463 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002464 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002465 }
2466
2467 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002468 widget = focus;
2469 if (input->grab)
2470 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002471 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002472 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002473 cursor = widget->enter_handler(focus, input, x, y,
2474 widget->user_data);
2475 else
2476 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002477
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002478 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002479 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002480}
2481
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002482void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002483touch_grab(struct input *input, int32_t touch_id)
2484{
2485 input->touch_grab = 1;
2486 input->touch_grab_id = touch_id;
2487}
2488
2489void
2490touch_ungrab(struct input *input)
2491{
2492 struct touch_point *tp, *tmp;
2493
2494 input->touch_grab = 0;
2495
2496 wl_list_for_each_safe(tp, tmp,
2497 &input->touch_point_list, link) {
2498 if (tp->id != input->touch_grab_id)
2499 continue;
2500 wl_list_remove(&tp->link);
2501 free(tp);
2502
2503 return;
2504 }
2505}
2506
2507void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002508input_grab(struct input *input, struct widget *widget, uint32_t button)
2509{
2510 input->grab = widget;
2511 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002512
2513 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002514}
2515
2516void
2517input_ungrab(struct input *input)
2518{
2519 struct widget *widget;
2520
2521 input->grab = NULL;
2522 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002523 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002524 input->sx, input->sy);
2525 input_set_focus_widget(input, widget, input->sx, input->sy);
2526 }
2527}
2528
2529static void
2530input_remove_pointer_focus(struct input *input)
2531{
2532 struct window *window = input->pointer_focus;
2533
2534 if (!window)
2535 return;
2536
2537 input_set_focus_widget(input, NULL, 0, 0);
2538
2539 input->pointer_focus = NULL;
2540 input->current_cursor = CURSOR_UNSET;
2541}
2542
2543static void
2544pointer_handle_enter(void *data, struct wl_pointer *pointer,
2545 uint32_t serial, struct wl_surface *surface,
2546 wl_fixed_t sx_w, wl_fixed_t sy_w)
2547{
2548 struct input *input = data;
2549 struct window *window;
2550 struct widget *widget;
2551 float sx = wl_fixed_to_double(sx_w);
2552 float sy = wl_fixed_to_double(sy_w);
2553
2554 if (!surface) {
2555 /* enter event for a window we've just destroyed */
2556 return;
2557 }
2558
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002559 window = wl_surface_get_user_data(surface);
2560 if (surface != window->main_surface->surface) {
2561 DBG("Ignoring input event from subsurface %p\n", surface);
2562 return;
2563 }
2564
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002565 input->display->serial = serial;
2566 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002567 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002568
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002569 input->sx = sx;
2570 input->sy = sy;
2571
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002572 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002573 input_set_focus_widget(input, widget, sx, sy);
2574}
2575
2576static void
2577pointer_handle_leave(void *data, struct wl_pointer *pointer,
2578 uint32_t serial, struct wl_surface *surface)
2579{
2580 struct input *input = data;
2581
2582 input->display->serial = serial;
2583 input_remove_pointer_focus(input);
2584}
2585
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002586static void
Daniel Stone37816df2012-05-16 18:45:18 +01002587pointer_handle_motion(void *data, struct wl_pointer *pointer,
2588 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002589{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002590 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002591 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002592 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002593 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002594 float sx = wl_fixed_to_double(sx_w);
2595 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002596
Paul Winwoodb22bf572013-08-29 10:52:54 +01002597 if (!window)
2598 return;
2599
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002600 input->sx = sx;
2601 input->sy = sy;
2602
Rob Bradford5f087742013-07-11 19:41:27 +01002603 /* when making the window smaller - e.g. after a unmaximise we might
2604 * still have a pending motion event that the compositor has picked
2605 * based on the old surface dimensions
2606 */
2607 if (sx > window->main_surface->allocation.width ||
2608 sy > window->main_surface->allocation.height)
2609 return;
2610
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002611 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002612 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002613 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002614 }
2615
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002616 if (input->grab)
2617 widget = input->grab;
2618 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002619 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002620 if (widget) {
2621 if (widget->motion_handler)
2622 cursor = widget->motion_handler(input->focus_widget,
2623 input, time, sx, sy,
2624 widget->user_data);
2625 else
2626 cursor = widget->default_cursor;
2627 } else
2628 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002629
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002630 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002631}
2632
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002633static void
Daniel Stone37816df2012-05-16 18:45:18 +01002634pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002635 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002636{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002637 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002638 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002639 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002640
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002641 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002642 if (input->focus_widget && input->grab == NULL &&
2643 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002644 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002645
Neil Roberts6b28aad2012-01-23 19:11:18 +00002646 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002647 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002648 (*widget->button_handler)(widget,
2649 input, time,
2650 button, state,
2651 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002652
Daniel Stone4dbadb12012-05-30 16:31:51 +01002653 if (input->grab && input->grab_button == button &&
2654 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002655 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002656}
2657
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002658static void
Daniel Stone37816df2012-05-16 18:45:18 +01002659pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002660 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002661{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002662 struct input *input = data;
2663 struct widget *widget;
2664
2665 widget = input->focus_widget;
2666 if (input->grab)
2667 widget = input->grab;
2668 if (widget && widget->axis_handler)
2669 (*widget->axis_handler)(widget,
2670 input, time,
2671 axis, value,
2672 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002673}
2674
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002675static const struct wl_pointer_listener pointer_listener = {
2676 pointer_handle_enter,
2677 pointer_handle_leave,
2678 pointer_handle_motion,
2679 pointer_handle_button,
2680 pointer_handle_axis,
2681};
2682
2683static void
2684input_remove_keyboard_focus(struct input *input)
2685{
2686 struct window *window = input->keyboard_focus;
2687 struct itimerspec its;
2688
2689 its.it_interval.tv_sec = 0;
2690 its.it_interval.tv_nsec = 0;
2691 its.it_value.tv_sec = 0;
2692 its.it_value.tv_nsec = 0;
2693 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2694
2695 if (!window)
2696 return;
2697
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002698 if (window->keyboard_focus_handler)
2699 (*window->keyboard_focus_handler)(window, NULL,
2700 window->user_data);
2701
2702 input->keyboard_focus = NULL;
2703}
2704
2705static void
2706keyboard_repeat_func(struct task *task, uint32_t events)
2707{
2708 struct input *input =
2709 container_of(task, struct input, repeat_task);
2710 struct window *window = input->keyboard_focus;
2711 uint64_t exp;
2712
2713 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2714 /* If we change the timer between the fd becoming
2715 * readable and getting here, there'll be nothing to
2716 * read and we get EAGAIN. */
2717 return;
2718
2719 if (window && window->key_handler) {
2720 (*window->key_handler)(window, input, input->repeat_time,
2721 input->repeat_key, input->repeat_sym,
2722 WL_KEYBOARD_KEY_STATE_PRESSED,
2723 window->user_data);
2724 }
2725}
2726
2727static void
2728keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2729 uint32_t format, int fd, uint32_t size)
2730{
2731 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002732 struct xkb_keymap *keymap;
2733 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002734 char *map_str;
2735
2736 if (!data) {
2737 close(fd);
2738 return;
2739 }
2740
2741 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2742 close(fd);
2743 return;
2744 }
2745
2746 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2747 if (map_str == MAP_FAILED) {
2748 close(fd);
2749 return;
2750 }
2751
Rui Matos3eccb862013-10-10 19:44:22 +02002752 keymap = xkb_map_new_from_string(input->display->xkb_context,
2753 map_str,
2754 XKB_KEYMAP_FORMAT_TEXT_V1,
2755 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002756 munmap(map_str, size);
2757 close(fd);
2758
Rui Matos3eccb862013-10-10 19:44:22 +02002759 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002760 fprintf(stderr, "failed to compile keymap\n");
2761 return;
2762 }
2763
Rui Matos3eccb862013-10-10 19:44:22 +02002764 state = xkb_state_new(keymap);
2765 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002766 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002767 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002768 return;
2769 }
2770
Rui Matos3eccb862013-10-10 19:44:22 +02002771 xkb_keymap_unref(input->xkb.keymap);
2772 xkb_state_unref(input->xkb.state);
2773 input->xkb.keymap = keymap;
2774 input->xkb.state = state;
2775
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002776 input->xkb.control_mask =
2777 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2778 input->xkb.alt_mask =
2779 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2780 input->xkb.shift_mask =
2781 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2782}
2783
2784static void
2785keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2786 uint32_t serial, struct wl_surface *surface,
2787 struct wl_array *keys)
2788{
2789 struct input *input = data;
2790 struct window *window;
2791
2792 input->display->serial = serial;
2793 input->keyboard_focus = wl_surface_get_user_data(surface);
2794
2795 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002796 if (window->keyboard_focus_handler)
2797 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002798 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002799}
2800
2801static void
2802keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2803 uint32_t serial, struct wl_surface *surface)
2804{
2805 struct input *input = data;
2806
2807 input->display->serial = serial;
2808 input_remove_keyboard_focus(input);
2809}
2810
Scott Moreau210d0792012-03-22 10:47:01 -06002811static void
Daniel Stone37816df2012-05-16 18:45:18 +01002812keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002813 uint32_t serial, uint32_t time, uint32_t key,
2814 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002815{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002816 struct input *input = data;
2817 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002818 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002819 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002820 const xkb_keysym_t *syms;
2821 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002822 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002823
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002824 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002825 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002826 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002827 return;
2828
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002829 /* We only use input grabs for pointer events for now, so just
2830 * ignore key presses if a grab is active. We expand the key
2831 * event delivery mechanism to route events to widgets to
2832 * properly handle key grabs. In the meantime, this prevents
2833 * key event devlivery while a grab is active. */
2834 if (input->grab && input->grab_button == 0)
2835 return;
2836
Daniel Stone97f68542012-05-30 16:32:01 +01002837 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002838
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002839 sym = XKB_KEY_NoSymbol;
2840 if (num_syms == 1)
2841 sym = syms[0];
2842
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002843
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002844 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002845 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002846 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002847 } else if (sym == XKB_KEY_F11 &&
2848 window->fullscreen_handler &&
2849 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2850 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002851 } else if (sym == XKB_KEY_F4 &&
2852 input->modifiers == MOD_ALT_MASK &&
2853 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002854 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002855 } else if (window->key_handler) {
2856 (*window->key_handler)(window, input, time, key,
2857 sym, state, window->user_data);
2858 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002859
2860 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2861 key == input->repeat_key) {
2862 its.it_interval.tv_sec = 0;
2863 its.it_interval.tv_nsec = 0;
2864 its.it_value.tv_sec = 0;
2865 its.it_value.tv_nsec = 0;
2866 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002867 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2868 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002869 input->repeat_sym = sym;
2870 input->repeat_key = key;
2871 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002872 its.it_interval.tv_sec = input->repeat_rate_sec;
2873 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2874 its.it_value.tv_sec = input->repeat_delay_sec;
2875 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002876 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2877 }
2878}
2879
2880static void
Daniel Stone351eb612012-05-31 15:27:47 -04002881keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2882 uint32_t serial, uint32_t mods_depressed,
2883 uint32_t mods_latched, uint32_t mods_locked,
2884 uint32_t group)
2885{
2886 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002887 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002888
Matt Ropere61561f2013-06-24 16:52:43 +01002889 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2890 if (!input->xkb.keymap)
2891 return;
2892
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002893 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2894 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002895 mask = xkb_state_serialize_mods(input->xkb.state,
2896 XKB_STATE_DEPRESSED |
2897 XKB_STATE_LATCHED);
2898 input->modifiers = 0;
2899 if (mask & input->xkb.control_mask)
2900 input->modifiers |= MOD_CONTROL_MASK;
2901 if (mask & input->xkb.alt_mask)
2902 input->modifiers |= MOD_ALT_MASK;
2903 if (mask & input->xkb.shift_mask)
2904 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002905}
2906
Jonny Lamb06959082014-08-12 14:58:27 +02002907static void
2908set_repeat_info(struct input *input, int32_t rate, int32_t delay)
2909{
2910 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
2911 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
2912
2913 /* a rate of zero disables any repeating, regardless of the delay's
2914 * value */
2915 if (rate == 0)
2916 return;
2917
2918 if (rate == 1)
2919 input->repeat_rate_sec = 1;
2920 else
2921 input->repeat_rate_nsec = 1000000000 / rate;
2922
2923 input->repeat_delay_sec = delay / 1000;
2924 delay -= (input->repeat_delay_sec * 1000);
2925 input->repeat_delay_nsec = delay * 1000 * 1000;
2926}
2927
2928static void
2929keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
2930 int32_t rate, int32_t delay)
2931{
2932 struct input *input = data;
2933
2934 set_repeat_info(input, rate, delay);
2935}
2936
Daniel Stone37816df2012-05-16 18:45:18 +01002937static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002938 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002939 keyboard_handle_enter,
2940 keyboard_handle_leave,
2941 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002942 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02002943 keyboard_handle_repeat_info
2944
Daniel Stone37816df2012-05-16 18:45:18 +01002945};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002946
2947static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002948touch_handle_down(void *data, struct wl_touch *wl_touch,
2949 uint32_t serial, uint32_t time, struct wl_surface *surface,
2950 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2951{
2952 struct input *input = data;
2953 struct widget *widget;
2954 float sx = wl_fixed_to_double(x_w);
2955 float sy = wl_fixed_to_double(y_w);
2956
Rusty Lynch1084da52013-08-15 09:10:08 -07002957 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07002958 input->touch_focus = wl_surface_get_user_data(surface);
2959 if (!input->touch_focus) {
2960 DBG("Failed to find to touch focus for surface %p\n", surface);
2961 return;
2962 }
2963
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002964 if (surface != input->touch_focus->main_surface->surface) {
2965 DBG("Ignoring input event from subsurface %p\n", surface);
2966 input->touch_focus = NULL;
2967 return;
2968 }
2969
Kristian Høgsberg1f671172014-04-29 14:30:44 -07002970 if (input->grab)
2971 widget = input->grab;
2972 else
2973 widget = window_find_widget(input->touch_focus,
2974 wl_fixed_to_double(x_w),
2975 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07002976 if (widget) {
2977 struct touch_point *tp = xmalloc(sizeof *tp);
2978 if (tp) {
2979 tp->id = id;
2980 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08002981 tp->x = sx;
2982 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07002983 wl_list_insert(&input->touch_point_list, &tp->link);
2984
2985 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07002986 (*widget->touch_down_handler)(widget, input,
2987 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07002988 sx, sy,
2989 widget->user_data);
2990 }
2991 }
2992}
2993
2994static void
2995touch_handle_up(void *data, struct wl_touch *wl_touch,
2996 uint32_t serial, uint32_t time, int32_t id)
2997{
2998 struct input *input = data;
2999 struct touch_point *tp, *tmp;
3000
Rusty Lynch041815a2013-08-08 21:20:38 -07003001 if (!input->touch_focus) {
3002 DBG("No touch focus found for touch up event!\n");
3003 return;
3004 }
3005
3006 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3007 if (tp->id != id)
3008 continue;
3009
3010 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003011 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003012 time, id,
3013 tp->widget->user_data);
3014
3015 wl_list_remove(&tp->link);
3016 free(tp);
3017
3018 return;
3019 }
3020}
3021
3022static void
3023touch_handle_motion(void *data, struct wl_touch *wl_touch,
3024 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3025{
3026 struct input *input = data;
3027 struct touch_point *tp;
3028 float sx = wl_fixed_to_double(x_w);
3029 float sy = wl_fixed_to_double(y_w);
3030
3031 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3032
3033 if (!input->touch_focus) {
3034 DBG("No touch focus found for touch motion event!\n");
3035 return;
3036 }
3037
3038 wl_list_for_each(tp, &input->touch_point_list, link) {
3039 if (tp->id != id)
3040 continue;
3041
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003042 tp->x = sx;
3043 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003044 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003045 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003046 id, sx, sy,
3047 tp->widget->user_data);
3048 return;
3049 }
3050}
3051
3052static void
3053touch_handle_frame(void *data, struct wl_touch *wl_touch)
3054{
3055 struct input *input = data;
3056 struct touch_point *tp, *tmp;
3057
3058 DBG("touch_handle_frame\n");
3059
3060 if (!input->touch_focus) {
3061 DBG("No touch focus found for touch frame event!\n");
3062 return;
3063 }
3064
3065 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3066 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003067 (*tp->widget->touch_frame_handler)(tp->widget, input,
3068 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003069 }
3070}
3071
3072static void
3073touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3074{
3075 struct input *input = data;
3076 struct touch_point *tp, *tmp;
3077
3078 DBG("touch_handle_cancel\n");
3079
3080 if (!input->touch_focus) {
3081 DBG("No touch focus found for touch cancel event!\n");
3082 return;
3083 }
3084
3085 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3086 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003087 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3088 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003089
3090 wl_list_remove(&tp->link);
3091 free(tp);
3092 }
3093}
3094
3095static const struct wl_touch_listener touch_listener = {
3096 touch_handle_down,
3097 touch_handle_up,
3098 touch_handle_motion,
3099 touch_handle_frame,
3100 touch_handle_cancel,
3101};
3102
3103static void
Daniel Stone37816df2012-05-16 18:45:18 +01003104seat_handle_capabilities(void *data, struct wl_seat *seat,
3105 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003106{
Daniel Stone37816df2012-05-16 18:45:18 +01003107 struct input *input = data;
3108
3109 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3110 input->pointer = wl_seat_get_pointer(seat);
3111 wl_pointer_set_user_data(input->pointer, input);
3112 wl_pointer_add_listener(input->pointer, &pointer_listener,
3113 input);
3114 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3115 wl_pointer_destroy(input->pointer);
3116 input->pointer = NULL;
3117 }
3118
3119 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3120 input->keyboard = wl_seat_get_keyboard(seat);
3121 wl_keyboard_set_user_data(input->keyboard, input);
3122 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3123 input);
3124 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3125 wl_keyboard_destroy(input->keyboard);
3126 input->keyboard = NULL;
3127 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003128
3129 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3130 input->touch = wl_seat_get_touch(seat);
3131 wl_touch_set_user_data(input->touch, input);
3132 wl_touch_add_listener(input->touch, &touch_listener, input);
3133 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3134 wl_touch_destroy(input->touch);
3135 input->touch = NULL;
3136 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003137}
3138
Rob Bradford08031182013-08-13 20:11:03 +01003139static void
3140seat_handle_name(void *data, struct wl_seat *seat,
3141 const char *name)
3142{
3143
3144}
3145
Daniel Stone37816df2012-05-16 18:45:18 +01003146static const struct wl_seat_listener seat_listener = {
3147 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003148 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003149};
3150
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003151void
3152input_get_position(struct input *input, int32_t *x, int32_t *y)
3153{
3154 *x = input->sx;
3155 *y = input->sy;
3156}
3157
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003158int
3159input_get_touch(struct input *input, int32_t id, float *x, float *y)
3160{
3161 struct touch_point *tp;
3162
3163 wl_list_for_each(tp, &input->touch_point_list, link) {
3164 if (tp->id != id)
3165 continue;
3166
3167 *x = tp->x;
3168 *y = tp->y;
3169 return 0;
3170 }
3171
3172 return -1;
3173}
3174
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003175struct display *
3176input_get_display(struct input *input)
3177{
3178 return input->display;
3179}
3180
Daniel Stone37816df2012-05-16 18:45:18 +01003181struct wl_seat *
3182input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003183{
Daniel Stone37816df2012-05-16 18:45:18 +01003184 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003185}
3186
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003187uint32_t
3188input_get_modifiers(struct input *input)
3189{
3190 return input->modifiers;
3191}
3192
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003193struct widget *
3194input_get_focus_widget(struct input *input)
3195{
3196 return input->focus_widget;
3197}
3198
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003199struct data_offer {
3200 struct wl_data_offer *offer;
3201 struct input *input;
3202 struct wl_array types;
3203 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003204
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003205 struct task io_task;
3206 int fd;
3207 data_func_t func;
3208 int32_t x, y;
3209 void *user_data;
3210};
3211
3212static void
3213data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3214{
3215 struct data_offer *offer = data;
3216 char **p;
3217
3218 p = wl_array_add(&offer->types, sizeof *p);
3219 *p = strdup(type);
3220}
3221
3222static const struct wl_data_offer_listener data_offer_listener = {
3223 data_offer_offer,
3224};
3225
3226static void
3227data_offer_destroy(struct data_offer *offer)
3228{
3229 char **p;
3230
3231 offer->refcount--;
3232 if (offer->refcount == 0) {
3233 wl_data_offer_destroy(offer->offer);
3234 for (p = offer->types.data; *p; p++)
3235 free(*p);
3236 wl_array_release(&offer->types);
3237 free(offer);
3238 }
3239}
3240
3241static void
3242data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003243 struct wl_data_device *data_device,
3244 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003245{
3246 struct data_offer *offer;
3247
Brian Lovinbc919262013-08-07 15:34:59 -07003248 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003249
3250 wl_array_init(&offer->types);
3251 offer->refcount = 1;
3252 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003253 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003254 wl_data_offer_add_listener(offer->offer,
3255 &data_offer_listener, offer);
3256}
3257
3258static void
3259data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003260 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003261 wl_fixed_t x_w, wl_fixed_t y_w,
3262 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003263{
3264 struct input *input = data;
3265 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003266 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003267 float x = wl_fixed_to_double(x_w);
3268 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003269 char **p;
3270
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003271 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003272 input->drag_enter_serial = serial;
3273 input->drag_focus = window,
3274 input->drag_x = x;
3275 input->drag_y = y;
3276
3277 if (!input->touch_grab)
3278 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003279
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003280 if (offer) {
3281 input->drag_offer = wl_data_offer_get_user_data(offer);
3282
3283 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3284 *p = NULL;
3285
3286 types_data = input->drag_offer->types.data;
3287 } else {
3288 input->drag_offer = NULL;
3289 types_data = NULL;
3290 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003291
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003292 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003293 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294 window->user_data);
3295}
3296
3297static void
3298data_device_leave(void *data, struct wl_data_device *data_device)
3299{
3300 struct input *input = data;
3301
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003302 if (input->drag_offer) {
3303 data_offer_destroy(input->drag_offer);
3304 input->drag_offer = NULL;
3305 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003306}
3307
3308static void
3309data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003310 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003311{
3312 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003313 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003314 float x = wl_fixed_to_double(x_w);
3315 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003316 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003317
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003318 input->drag_x = x;
3319 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003320
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003321 if (input->drag_offer)
3322 types_data = input->drag_offer->types.data;
3323 else
3324 types_data = NULL;
3325
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003326 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003327 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003328 window->user_data);
3329}
3330
3331static void
3332data_device_drop(void *data, struct wl_data_device *data_device)
3333{
3334 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003335 struct window *window = input->drag_focus;
3336 float x, y;
3337
3338 x = input->drag_x;
3339 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003340
3341 if (window->drop_handler)
3342 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003343 x, y, window->user_data);
3344
3345 if (input->touch_grab)
3346 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003347}
3348
3349static void
3350data_device_selection(void *data,
3351 struct wl_data_device *wl_data_device,
3352 struct wl_data_offer *offer)
3353{
3354 struct input *input = data;
3355 char **p;
3356
3357 if (input->selection_offer)
3358 data_offer_destroy(input->selection_offer);
3359
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003360 if (offer) {
3361 input->selection_offer = wl_data_offer_get_user_data(offer);
3362 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3363 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003364 } else {
3365 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003366 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003367}
3368
3369static const struct wl_data_device_listener data_device_listener = {
3370 data_device_data_offer,
3371 data_device_enter,
3372 data_device_leave,
3373 data_device_motion,
3374 data_device_drop,
3375 data_device_selection
3376};
3377
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003378static void
3379input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003380{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003381 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003382 struct wl_cursor *cursor;
3383 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003384
Daniel Stone80972742012-11-07 17:51:39 +11003385 if (!input->pointer)
3386 return;
3387
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003388 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003389 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003390 return;
3391
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003392 if (index >= (int) cursor->image_count) {
3393 fprintf(stderr, "cursor index out of range\n");
3394 return;
3395 }
3396
3397 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003398 buffer = wl_cursor_image_get_buffer(image);
3399 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003400 return;
3401
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003402 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3403 wl_surface_damage(input->pointer_surface, 0, 0,
3404 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003405 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003406 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3407 input->pointer_surface,
3408 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003409}
3410
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003411static const struct wl_callback_listener pointer_surface_listener;
3412
3413static void
3414pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3415 uint32_t time)
3416{
3417 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003418 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003419 int i;
3420
3421 if (callback) {
3422 assert(callback == input->cursor_frame_cb);
3423 wl_callback_destroy(callback);
3424 input->cursor_frame_cb = NULL;
3425 }
3426
Daniel Stone80972742012-11-07 17:51:39 +11003427 if (!input->pointer)
3428 return;
3429
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003430 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003431 wl_pointer_set_cursor(input->pointer,
3432 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003433 NULL, 0, 0);
3434 return;
3435 }
3436
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003437 if (input->current_cursor == CURSOR_UNSET)
3438 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003439 cursor = input->display->cursors[input->current_cursor];
3440 if (!cursor)
3441 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003442
3443 /* FIXME We don't have the current time on the first call so we set
3444 * the animation start to the time of the first frame callback. */
3445 if (time == 0)
3446 input->cursor_anim_start = 0;
3447 else if (input->cursor_anim_start == 0)
3448 input->cursor_anim_start = time;
3449
3450 if (time == 0 || input->cursor_anim_start == 0)
3451 i = 0;
3452 else
3453 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3454
Pekka Paalanenbc106382012-10-10 12:49:31 +03003455 if (cursor->image_count > 1) {
3456 input->cursor_frame_cb =
3457 wl_surface_frame(input->pointer_surface);
3458 wl_callback_add_listener(input->cursor_frame_cb,
3459 &pointer_surface_listener, input);
3460 }
3461
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003462 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003463}
3464
3465static const struct wl_callback_listener pointer_surface_listener = {
3466 pointer_surface_frame_callback
3467};
3468
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003469void
3470input_set_pointer_image(struct input *input, int pointer)
3471{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003472 int force = 0;
3473
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003474 if (!input->pointer)
3475 return;
3476
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003477 if (input->pointer_enter_serial > input->cursor_serial)
3478 force = 1;
3479
3480 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003481 return;
3482
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003483 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003484 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003485 if (!input->cursor_frame_cb)
3486 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003487 else if (force) {
3488 /* The current frame callback may be stuck if, for instance,
3489 * the set cursor request was processed by the server after
3490 * this client lost the focus. In this case the cursor surface
3491 * might not be mapped and the frame callback wouldn't ever
3492 * complete. Send a set_cursor and attach to try to map the
3493 * cursor surface again so that the callback will finish */
3494 input_set_pointer_image_index(input, 0);
3495 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003496}
3497
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003498struct wl_data_device *
3499input_get_data_device(struct input *input)
3500{
3501 return input->data_device;
3502}
3503
3504void
3505input_set_selection(struct input *input,
3506 struct wl_data_source *source, uint32_t time)
3507{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003508 if (input->data_device)
3509 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003510}
3511
3512void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003513input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003514{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003515 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003516 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003517}
3518
3519static void
3520offer_io_func(struct task *task, uint32_t events)
3521{
3522 struct data_offer *offer =
3523 container_of(task, struct data_offer, io_task);
3524 unsigned int len;
3525 char buffer[4096];
3526
3527 len = read(offer->fd, buffer, sizeof buffer);
3528 offer->func(buffer, len,
3529 offer->x, offer->y, offer->user_data);
3530
3531 if (len == 0) {
3532 close(offer->fd);
3533 data_offer_destroy(offer);
3534 }
3535}
3536
3537static void
3538data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3539 data_func_t func, void *user_data)
3540{
3541 int p[2];
3542
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003543 if (pipe2(p, O_CLOEXEC) == -1)
3544 return;
3545
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003546 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3547 close(p[1]);
3548
3549 offer->io_task.run = offer_io_func;
3550 offer->fd = p[0];
3551 offer->func = func;
3552 offer->refcount++;
3553 offer->user_data = user_data;
3554
3555 display_watch_fd(offer->input->display,
3556 offer->fd, EPOLLIN, &offer->io_task);
3557}
3558
3559void
3560input_receive_drag_data(struct input *input, const char *mime_type,
3561 data_func_t func, void *data)
3562{
3563 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003564 input->drag_offer->x = input->drag_x;
3565 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003566}
3567
3568int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003569input_receive_drag_data_to_fd(struct input *input,
3570 const char *mime_type, int fd)
3571{
3572 if (input->drag_offer)
3573 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3574
3575 return 0;
3576}
3577
3578int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003579input_receive_selection_data(struct input *input, const char *mime_type,
3580 data_func_t func, void *data)
3581{
3582 char **p;
3583
3584 if (input->selection_offer == NULL)
3585 return -1;
3586
3587 for (p = input->selection_offer->types.data; *p; p++)
3588 if (strcmp(mime_type, *p) == 0)
3589 break;
3590
3591 if (*p == NULL)
3592 return -1;
3593
3594 data_offer_receive_data(input->selection_offer,
3595 mime_type, func, data);
3596 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003597}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003598
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003599int
3600input_receive_selection_data_to_fd(struct input *input,
3601 const char *mime_type, int fd)
3602{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003603 if (input->selection_offer)
3604 wl_data_offer_receive(input->selection_offer->offer,
3605 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003606
3607 return 0;
3608}
3609
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003610void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003611window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003612{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003613 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003614 return;
3615
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003616 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003617}
3618
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003619static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003620surface_set_synchronized(struct surface *surface)
3621{
3622 if (!surface->subsurface)
3623 return;
3624
3625 if (surface->synchronized)
3626 return;
3627
3628 wl_subsurface_set_sync(surface->subsurface);
3629 surface->synchronized = 1;
3630}
3631
3632static void
3633surface_set_synchronized_default(struct surface *surface)
3634{
3635 if (!surface->subsurface)
3636 return;
3637
3638 if (surface->synchronized == surface->synchronized_default)
3639 return;
3640
3641 if (surface->synchronized_default)
3642 wl_subsurface_set_sync(surface->subsurface);
3643 else
3644 wl_subsurface_set_desync(surface->subsurface);
3645
3646 surface->synchronized = surface->synchronized_default;
3647}
3648
3649static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003650surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003651{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003652 struct widget *widget = surface->widget;
3653 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003654
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003655 if (surface->input_region) {
3656 wl_region_destroy(surface->input_region);
3657 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003658 }
3659
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003660 if (surface->opaque_region)
3661 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003662
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003663 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003664
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003665 if (widget->resize_handler)
3666 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003667 widget->allocation.width,
3668 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003669 widget->user_data);
3670
Pekka Paalanen35e82632013-04-25 13:57:48 +03003671 if (surface->subsurface &&
3672 (surface->allocation.x != widget->allocation.x ||
3673 surface->allocation.y != widget->allocation.y)) {
3674 wl_subsurface_set_position(surface->subsurface,
3675 widget->allocation.x,
3676 widget->allocation.y);
3677 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003678 if (surface->allocation.width != widget->allocation.width ||
3679 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003680 window_schedule_redraw(widget->window);
3681 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003682 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003683
3684 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003685 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003686 widget->allocation.width,
3687 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003688}
3689
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003690static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003691hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3692{
3693 /*
3694 * This hack should be removed, when EGL respects
3695 * eglSwapInterval(0).
3696 *
3697 * If this window has sub-surfaces, especially a free-running
3698 * EGL-widget, we need to post the parent surface once with
3699 * all the old state to guarantee, that the EGL-widget will
3700 * receive its frame callback soon. Otherwise, a forced call
3701 * to eglSwapBuffers may end up blocking, waiting for a frame
3702 * event that will never come, because we will commit the parent
3703 * surface with all new state only after eglSwapBuffers returns.
3704 *
3705 * This assumes, that:
3706 * 1. When the EGL widget's resize hook is called, it pauses.
3707 * 2. When the EGL widget's redraw hook is called, it forces a
3708 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3709 * In a single threaded application condition 1 is a no-op.
3710 *
3711 * XXX: This should actually be after the surface_resize() calls,
3712 * but cannot, because then it would commit the incomplete state
3713 * accumulated from the widget resize hooks.
3714 */
3715 if (window->subsurface_list.next != &window->main_surface->link ||
3716 window->subsurface_list.prev != &window->main_surface->link)
3717 wl_surface_commit(window->main_surface->surface);
3718}
3719
3720static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003721window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003722{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003723 struct surface *surface;
3724
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003725 widget_set_allocation(window->main_surface->widget,
3726 window->pending_allocation.x,
3727 window->pending_allocation.y,
3728 window->pending_allocation.width,
3729 window->pending_allocation.height);
3730
3731 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003732
3733 /* The main surface is in the list, too. Main surface's
3734 * resize_handler is responsible for calling widget_set_allocation()
3735 * on all sub-surface root widgets, so they will be resized
3736 * properly.
3737 */
3738 wl_list_for_each(surface, &window->subsurface_list, link) {
3739 if (surface == window->main_surface)
3740 continue;
3741
3742 surface_set_synchronized(surface);
3743 surface_resize(surface);
3744 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003745
3746 if (!window->fullscreen && !window->maximized)
3747 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003748}
3749
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003750static void
3751idle_resize(struct window *window)
3752{
3753 window->resize_needed = 0;
3754 window->redraw_needed = 1;
3755
3756 DBG("from %dx%d to %dx%d\n",
3757 window->main_surface->server_allocation.width,
3758 window->main_surface->server_allocation.height,
3759 window->pending_allocation.width,
3760 window->pending_allocation.height);
3761
3762 hack_prevent_EGL_sub_surface_deadlock(window);
3763
3764 window_do_resize(window);
3765}
3766
3767static void
3768undo_resize(struct window *window)
3769{
3770 window->pending_allocation.width =
3771 window->main_surface->server_allocation.width;
3772 window->pending_allocation.height =
3773 window->main_surface->server_allocation.height;
3774
3775 DBG("back to %dx%d\n",
3776 window->main_surface->server_allocation.width,
3777 window->main_surface->server_allocation.height);
3778
3779 window_do_resize(window);
3780
3781 if (window->pending_allocation.width == 0 &&
3782 window->pending_allocation.height == 0) {
3783 fprintf(stderr, "Error: Could not draw a surface, "
3784 "most likely due to insufficient disk space in "
3785 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3786 exit(EXIT_FAILURE);
3787 }
3788}
3789
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003790void
3791window_schedule_resize(struct window *window, int width, int height)
3792{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003793 /* We should probably get these numbers from the theme. */
3794 const int min_width = 200, min_height = 200;
3795
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003796 window->pending_allocation.x = 0;
3797 window->pending_allocation.y = 0;
3798 window->pending_allocation.width = width;
3799 window->pending_allocation.height = height;
3800
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003801 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003802 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003803 window->min_allocation.width = min_width;
3804 else
3805 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003806 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003807 window->min_allocation.height = min_height;
3808 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003809 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003810 }
3811
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003812 if (window->pending_allocation.width < window->min_allocation.width)
3813 window->pending_allocation.width = window->min_allocation.width;
3814 if (window->pending_allocation.height < window->min_allocation.height)
3815 window->pending_allocation.height = window->min_allocation.height;
3816
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003817 window->resize_needed = 1;
3818 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003819}
3820
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003821void
3822widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3823{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003824 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003825}
3826
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003827static int
3828window_get_shadow_margin(struct window *window)
3829{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003830 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003831 return frame_get_shadow_margin(window->frame->frame);
3832 else
3833 return 0;
3834}
3835
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003836static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003837handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003838 int32_t width, int32_t height,
3839 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003840{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003841 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003842 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003843
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003844 window->maximized = 0;
3845 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003846 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003847 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003848
3849 wl_array_for_each(p, states) {
3850 uint32_t state = *p;
3851 switch (state) {
3852 case XDG_SURFACE_STATE_MAXIMIZED:
3853 window->maximized = 1;
3854 break;
3855 case XDG_SURFACE_STATE_FULLSCREEN:
3856 window->fullscreen = 1;
3857 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003858 case XDG_SURFACE_STATE_RESIZING:
3859 window->resizing = 1;
3860 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003861 case XDG_SURFACE_STATE_ACTIVATED:
3862 window->focused = 1;
3863 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003864 default:
3865 /* Unknown state */
3866 break;
3867 }
3868 }
3869
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003870 if (window->frame) {
3871 if (window->maximized) {
3872 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3873 } else {
3874 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3875 }
3876
3877 if (window->focused) {
3878 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3879 } else {
3880 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3881 }
3882 }
3883
Jasper St. Pierref184c382014-05-06 08:33:27 -04003884 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003885 /* The width / height params are for window geometry,
3886 * but window_schedule_resize takes allocation. Add
3887 * on the shadow margin to get the difference. */
3888 int margin = window_get_shadow_margin(window);
3889
3890 window_schedule_resize(window,
3891 width + margin * 2,
3892 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04003893 } else {
3894 window_schedule_resize(window,
3895 window->saved_allocation.width,
3896 window->saved_allocation.height);
3897 }
3898
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07003899 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07003900
3901 if (window->state_changed_handler)
3902 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003903}
3904
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003905static void
3906handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3907{
3908 struct window *window = data;
3909 window_close(window);
3910}
3911
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003912static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003913 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003914 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003915};
3916
3917static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003918window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05003919{
3920 struct wl_surface *parent_surface;
3921
3922 if (!window->xdg_surface)
3923 return;
3924
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003925 if (window->parent)
3926 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05003927 else
3928 parent_surface = NULL;
3929
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003930 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre53686042013-12-09 15:26:25 -05003931}
3932
3933static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003934window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05003935{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003936 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003937 frame_input_rect(window->frame->frame,
3938 &geometry->x,
3939 &geometry->y,
3940 &geometry->width,
3941 &geometry->height);
3942 else
3943 window_get_allocation(window, geometry);
3944}
3945
3946static void
3947window_sync_geometry(struct window *window)
3948{
3949 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05003950
3951 if (!window->xdg_surface)
3952 return;
3953
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003954 window_get_geometry(window, &geometry);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003955
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003956 xdg_surface_set_window_geometry(window->xdg_surface,
3957 geometry.x,
3958 geometry.y,
3959 geometry.width,
3960 geometry.height);
Jasper St. Pierre74073452014-02-01 18:36:41 -05003961}
3962
3963static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003964window_flush(struct window *window)
3965{
3966 struct surface *surface;
3967
3968 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003969 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003970 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003971 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003972 }
3973 }
3974
3975 wl_list_for_each(surface, &window->subsurface_list, link) {
3976 if (surface == window->main_surface)
3977 continue;
3978
3979 surface_flush(surface);
3980 }
3981
3982 surface_flush(window->main_surface);
3983}
3984
3985static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003986menu_destroy(struct menu *menu)
3987{
3988 widget_destroy(menu->widget);
3989 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003990 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003991 free(menu);
3992}
3993
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003994void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003995window_get_allocation(struct window *window,
3996 struct rectangle *allocation)
3997{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003998 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003999}
4000
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004001static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004002widget_redraw(struct widget *widget)
4003{
4004 struct widget *child;
4005
4006 if (widget->redraw_handler)
4007 widget->redraw_handler(widget, widget->user_data);
4008 wl_list_for_each(child, &widget->child_list, link)
4009 widget_redraw(child);
4010}
4011
4012static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004013frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4014{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004015 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004016
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004017 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004018 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004019 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004020 surface->frame_cb = NULL;
4021
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004022 surface->last_time = time;
4023
Pekka Paalanen71233882013-04-25 13:57:53 +03004024 if (surface->redraw_needed || surface->window->redraw_needed) {
4025 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004026 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004027 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004028}
4029
4030static const struct wl_callback_listener listener = {
4031 frame_callback
4032};
4033
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004034static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004035surface_redraw(struct surface *surface)
4036{
Pekka Paalanen71233882013-04-25 13:57:53 +03004037 DBG_OBJ(surface->surface, "begin\n");
4038
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004039 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004040 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004041
4042 /* Whole-window redraw forces a redraw even if the previous has
4043 * not yet hit the screen.
4044 */
4045 if (surface->frame_cb) {
4046 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004047 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004048
Pekka Paalanen71233882013-04-25 13:57:53 +03004049 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004050 wl_callback_destroy(surface->frame_cb);
4051 }
4052
Neil Roberts97b747c2013-12-19 16:17:12 +00004053 if (surface->widget->use_cairo &&
4054 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004055 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4056 return -1;
4057 }
4058
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004059 surface->frame_cb = wl_surface_frame(surface->surface);
4060 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004061 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004062
4063 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004064 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004065 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004066 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004067 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004068}
4069
4070static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004071idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004072{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004073 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004074 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004075 int failed = 0;
4076 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004077
Pekka Paalanen71233882013-04-25 13:57:53 +03004078 DBG(" --------- \n");
4079
Pekka Paalaneneebff542013-04-25 13:57:52 +03004080 wl_list_init(&window->redraw_task.link);
4081 window->redraw_task_scheduled = 0;
4082
4083 if (window->resize_needed) {
4084 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004085 if (window->main_surface->frame_cb) {
4086 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004087 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004088 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004089
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004090 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004091 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004092 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004093
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004094 if (surface_redraw(window->main_surface) < 0) {
4095 /*
4096 * Only main_surface failure will cause us to undo the resize.
4097 * If sub-surfaces fail, they will just be broken with old
4098 * content.
4099 */
4100 failed = 1;
4101 } else {
4102 wl_list_for_each(surface, &window->subsurface_list, link) {
4103 if (surface == window->main_surface)
4104 continue;
4105
4106 surface_redraw(surface);
4107 }
4108 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004109
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004110 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004111 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004112
4113 wl_list_for_each(surface, &window->subsurface_list, link)
4114 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004115
4116 if (resized && failed) {
4117 /* Restore widget tree to correspond to what is on screen. */
4118 undo_resize(window);
4119 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004120}
4121
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004122static void
4123window_schedule_redraw_task(struct window *window)
4124{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004125 if (!window->redraw_task_scheduled) {
4126 window->redraw_task.run = idle_redraw;
4127 display_defer(window->display, &window->redraw_task);
4128 window->redraw_task_scheduled = 1;
4129 }
4130}
4131
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004132void
4133window_schedule_redraw(struct window *window)
4134{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004135 struct surface *surface;
4136
Pekka Paalanen71233882013-04-25 13:57:53 +03004137 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4138
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004139 wl_list_for_each(surface, &window->subsurface_list, link)
4140 surface->redraw_needed = 1;
4141
4142 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004143}
4144
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004145int
4146window_is_fullscreen(struct window *window)
4147{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004148 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004149}
4150
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004151void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004152window_set_fullscreen(struct window *window, int fullscreen)
4153{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004154 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004155 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004156
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004157 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004158 return;
4159
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004160 if (fullscreen)
4161 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4162 else
4163 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004164}
4165
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004166int
4167window_is_maximized(struct window *window)
4168{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004169 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004170}
4171
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004172void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004173window_set_maximized(struct window *window, int maximized)
4174{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004175 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004176 return;
4177
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004178 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004179 return;
4180
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004181 if (maximized)
4182 xdg_surface_set_maximized(window->xdg_surface);
4183 else
4184 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004185}
4186
Jasper St. Pierrede680992014-04-10 17:23:49 -07004187int
4188window_is_resizing(struct window *window)
4189{
4190 return window->resizing;
4191}
4192
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004193void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004194window_set_minimized(struct window *window)
4195{
4196 if (!window->xdg_surface)
4197 return;
4198
4199 xdg_surface_set_minimized(window->xdg_surface);
4200}
4201
4202void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004203window_set_user_data(struct window *window, void *data)
4204{
4205 window->user_data = data;
4206}
4207
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004208void *
4209window_get_user_data(struct window *window)
4210{
4211 return window->user_data;
4212}
4213
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004214void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004215window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004216 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004217{
4218 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004219}
4220
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004221void
4222window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004223 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004224{
4225 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004226}
4227
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004228void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004229window_set_data_handler(struct window *window, window_data_handler_t handler)
4230{
4231 window->data_handler = handler;
4232}
4233
4234void
4235window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4236{
4237 window->drop_handler = handler;
4238}
4239
4240void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004241window_set_close_handler(struct window *window,
4242 window_close_handler_t handler)
4243{
4244 window->close_handler = handler;
4245}
4246
4247void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004248window_set_fullscreen_handler(struct window *window,
4249 window_fullscreen_handler_t handler)
4250{
4251 window->fullscreen_handler = handler;
4252}
4253
4254void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004255window_set_output_handler(struct window *window,
4256 window_output_handler_t handler)
4257{
4258 window->output_handler = handler;
4259}
4260
4261void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004262window_set_state_changed_handler(struct window *window,
4263 window_state_changed_handler_t handler)
4264{
4265 window->state_changed_handler = handler;
4266}
4267
4268void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004269window_set_title(struct window *window, const char *title)
4270{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004271 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004272 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004273 if (window->frame) {
4274 frame_set_title(window->frame->frame, title);
4275 widget_schedule_redraw(window->frame->widget);
4276 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004277 if (window->xdg_surface)
4278 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004279}
4280
4281const char *
4282window_get_title(struct window *window)
4283{
4284 return window->title;
4285}
4286
4287void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004288window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4289{
4290 struct text_cursor_position *text_cursor_position =
4291 window->display->text_cursor_position;
4292
Scott Moreau9295ce02012-06-01 12:46:10 -06004293 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004294 return;
4295
4296 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004297 window->main_surface->surface,
4298 wl_fixed_from_int(x),
4299 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004300}
4301
4302void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004303window_damage(struct window *window, int32_t x, int32_t y,
4304 int32_t width, int32_t height)
4305{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004306 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004307}
4308
Casey Dahlin9074db52012-04-19 22:50:09 -04004309static void
4310surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004311 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004312{
Rob Bradford7507b572012-05-15 17:55:34 +01004313 struct window *window = data;
4314 struct output *output;
4315 struct output *output_found = NULL;
4316 struct window_output *window_output;
4317
4318 wl_list_for_each(output, &window->display->output_list, link) {
4319 if (output->output == wl_output) {
4320 output_found = output;
4321 break;
4322 }
4323 }
4324
4325 if (!output_found)
4326 return;
4327
Brian Lovinbc919262013-08-07 15:34:59 -07004328 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004329 window_output->output = output_found;
4330
4331 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004332
4333 if (window->output_handler)
4334 window->output_handler(window, output_found, 1,
4335 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004336}
4337
4338static void
4339surface_leave(void *data,
4340 struct wl_surface *wl_surface, struct wl_output *output)
4341{
Rob Bradford7507b572012-05-15 17:55:34 +01004342 struct window *window = data;
4343 struct window_output *window_output;
4344 struct window_output *window_output_found = NULL;
4345
4346 wl_list_for_each(window_output, &window->window_output_list, link) {
4347 if (window_output->output->output == output) {
4348 window_output_found = window_output;
4349 break;
4350 }
4351 }
4352
4353 if (window_output_found) {
4354 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004355
4356 if (window->output_handler)
4357 window->output_handler(window, window_output->output,
4358 0, window->user_data);
4359
Rob Bradford7507b572012-05-15 17:55:34 +01004360 free(window_output_found);
4361 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004362}
4363
4364static const struct wl_surface_listener surface_listener = {
4365 surface_enter,
4366 surface_leave
4367};
4368
Pekka Paalanen4e373742013-02-13 16:17:13 +02004369static struct surface *
4370surface_create(struct window *window)
4371{
4372 struct display *display = window->display;
4373 struct surface *surface;
4374
Brian Lovinbc919262013-08-07 15:34:59 -07004375 surface = xmalloc(sizeof *surface);
4376 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004377 if (!surface)
4378 return NULL;
4379
4380 surface->window = window;
4381 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004382 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004383 wl_surface_add_listener(surface->surface, &surface_listener, window);
4384
Pekka Paalanen35e82632013-04-25 13:57:48 +03004385 wl_list_insert(&window->subsurface_list, &surface->link);
4386
Pekka Paalanen4e373742013-02-13 16:17:13 +02004387 return surface;
4388}
4389
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004390static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004391get_preferred_buffer_type(struct display *display)
4392{
4393#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004394 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004395 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4396#endif
4397
4398 return WINDOW_BUFFER_TYPE_SHM;
4399}
4400
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004401static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004402window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004403{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004404 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004405 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004406
Peter Huttererf3d62272013-08-08 11:57:05 +10004407 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004408 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004409 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004410
4411 surface = surface_create(window);
4412 window->main_surface = surface;
4413
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004414 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004415
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004416 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004417 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004418
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004419 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004420
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004421 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004422 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004423 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004424
Rob Bradford7507b572012-05-15 17:55:34 +01004425 wl_list_init (&window->window_output_list);
4426
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004427 return window;
4428}
4429
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004430struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004431window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004432{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004433 struct window *window;
4434
4435 window = window_create_internal(display, 0);
4436
4437 window->xdg_surface =
4438 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4439 window->main_surface->surface);
4440 fail_on_null(window->xdg_surface);
4441
4442 xdg_surface_set_user_data(window->xdg_surface, window);
4443 xdg_surface_add_listener(window->xdg_surface,
4444 &xdg_surface_listener, window);
4445
4446 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004447}
4448
4449struct window *
4450window_create_custom(struct display *display)
4451{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004452 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004453}
4454
Jasper St. Pierre53686042013-12-09 15:26:25 -05004455void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004456window_set_parent(struct window *window,
4457 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004458{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004459 window->parent = parent_window;
4460 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004461}
4462
4463struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004464window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004465{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004466 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004467}
4468
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004469static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004470menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004471{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004472 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004473 int next;
4474
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004475 frame_interior(menu->frame, &x, &y, &width, &height);
4476 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004477 if (menu->current != next) {
4478 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004479 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004480 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004481}
4482
4483static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004484menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004485 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004486 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004487{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004488 struct menu *menu = data;
4489
4490 if (widget == menu->widget)
4491 menu_set_item(data, y);
4492
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004493 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004494}
4495
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004496static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004497menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004498 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004499{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004500 struct menu *menu = data;
4501
4502 if (widget == menu->widget)
4503 menu_set_item(data, y);
4504
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004505 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004506}
4507
4508static void
4509menu_leave_handler(struct widget *widget, struct input *input, void *data)
4510{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004511 struct menu *menu = data;
4512
4513 if (widget == menu->widget)
4514 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004515}
4516
4517static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004518menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004519 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004520 uint32_t button, enum wl_pointer_button_state state,
4521 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004522
4523{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004524 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004525
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004526 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4527 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004528 /* Either relase after press-drag-release or
4529 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004530 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004531 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004532 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004533 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004534 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004535 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004536}
4537
4538static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004539menu_touch_up_handler(struct widget *widget,
4540 struct input *input,
4541 uint32_t serial,
4542 uint32_t time,
4543 int32_t id,
4544 void *data)
4545{
4546 struct menu *menu = data;
4547
4548 input_ungrab(input);
4549 menu_destroy(menu);
4550}
4551
4552static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004553menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004554{
4555 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004556 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004557 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004558
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004559 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004560
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004561 frame_repaint(menu->frame, cr);
4562 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004563
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004564 theme_set_background_source(menu->window->display->theme,
4565 cr, THEME_FRAME_ACTIVE);
4566 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004567 cairo_fill(cr);
4568
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004569 cairo_select_font_face(cr, "sans",
4570 CAIRO_FONT_SLANT_NORMAL,
4571 CAIRO_FONT_WEIGHT_NORMAL);
4572 cairo_set_font_size(cr, 12);
4573
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004574 for (i = 0; i < menu->count; i++) {
4575 if (i == menu->current) {
4576 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004577 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004578 cairo_fill(cr);
4579 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004580 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004581 cairo_show_text(cr, menu->entries[i]);
4582 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004583 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4584 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004585 cairo_show_text(cr, menu->entries[i]);
4586 }
4587 }
4588
4589 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004590}
4591
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004592static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004593handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4594{
4595 struct window *window = data;
4596 struct menu *menu = window->main_surface->widget->user_data;
4597
4598 input_ungrab(menu->input);
4599 menu_destroy(menu);
4600}
4601
4602static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004603 handle_popup_popup_done,
4604};
4605
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004606static struct menu *
4607create_menu(struct display *display,
4608 struct input *input, uint32_t time,
4609 menu_func_t func, const char **entries, int count,
4610 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004611{
4612 struct window *window;
4613 struct menu *menu;
4614
4615 menu = malloc(sizeof *menu);
4616 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004617 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004618
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004619 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004620 if (!window) {
4621 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004622 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004623 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004624
4625 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004626 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004627 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004628 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004629 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004630 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004631 menu->entries = entries;
4632 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004633 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004634 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004635 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004636 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004637 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004638
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004639 input_ungrab(input);
4640
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004641 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004642 widget_set_enter_handler(menu->widget, menu_enter_handler);
4643 widget_set_leave_handler(menu->widget, menu_leave_handler);
4644 widget_set_motion_handler(menu->widget, menu_motion_handler);
4645 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004646 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004647
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004648 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004649 frame_resize_inside(menu->frame, 200, count * 20);
4650 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4651 window_schedule_resize(window, frame_width(menu->frame),
4652 frame_height(menu->frame));
4653
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004654 return menu;
4655}
4656
4657struct window *
4658window_create_menu(struct display *display,
4659 struct input *input, uint32_t time,
4660 menu_func_t func, const char **entries, int count,
4661 void *user_data)
4662{
4663 struct menu *menu;
4664 menu = create_menu(display, input, time, func, entries, count, user_data);
4665
4666 if (menu == NULL)
4667 return NULL;
4668
4669 return menu->window;
4670}
4671
4672void
4673window_show_menu(struct display *display,
4674 struct input *input, uint32_t time, struct window *parent,
4675 int32_t x, int32_t y,
4676 menu_func_t func, const char **entries, int count)
4677{
4678 struct menu *menu;
4679 struct window *window;
4680 int32_t ix, iy;
4681
4682 menu = create_menu(display, input, time, func, entries, count, parent);
4683
4684 if (menu == NULL)
4685 return;
4686
4687 window = menu->window;
4688
4689 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4690 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4691
4692 window->x = x;
4693 window->y = y;
4694
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004695 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004696
4697 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4698 window->main_surface->surface,
4699 parent->main_surface->surface,
4700 input->seat,
4701 display_get_serial(window->display),
4702 window->x - ix,
4703 window->y - iy,
4704 0);
4705 fail_on_null(window->xdg_popup);
4706
4707 xdg_popup_set_user_data(window->xdg_popup, window);
4708 xdg_popup_add_listener(window->xdg_popup,
4709 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004710}
4711
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004712void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004713window_set_buffer_type(struct window *window, enum window_buffer_type type)
4714{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004715 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004716}
4717
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004718enum window_buffer_type
4719window_get_buffer_type(struct window *window)
4720{
4721 return window->main_surface->buffer_type;
4722}
4723
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004724void
4725window_set_preferred_format(struct window *window,
4726 enum preferred_format format)
4727{
4728 window->preferred_format = format;
4729}
4730
Pekka Paalanen35e82632013-04-25 13:57:48 +03004731struct widget *
4732window_add_subsurface(struct window *window, void *data,
4733 enum subsurface_mode default_mode)
4734{
4735 struct widget *widget;
4736 struct surface *surface;
4737 struct wl_surface *parent;
4738 struct wl_subcompositor *subcompo = window->display->subcompositor;
4739
Pekka Paalanen35e82632013-04-25 13:57:48 +03004740 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004741 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004742 widget = widget_create(window, surface, data);
4743 wl_list_init(&widget->link);
4744 surface->widget = widget;
4745
4746 parent = window->main_surface->surface;
4747 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4748 surface->surface,
4749 parent);
4750 surface->synchronized = 1;
4751
4752 switch (default_mode) {
4753 case SUBSURFACE_SYNCHRONIZED:
4754 surface->synchronized_default = 1;
4755 break;
4756 case SUBSURFACE_DESYNCHRONIZED:
4757 surface->synchronized_default = 0;
4758 break;
4759 default:
4760 assert(!"bad enum subsurface_mode");
4761 }
4762
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004763 window->resize_needed = 1;
4764 window_schedule_redraw(window);
4765
Pekka Paalanen35e82632013-04-25 13:57:48 +03004766 return widget;
4767}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004768
4769static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004770display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004771 struct wl_output *wl_output,
4772 int x, int y,
4773 int physical_width,
4774 int physical_height,
4775 int subpixel,
4776 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004777 const char *model,
4778 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004779{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004780 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004781
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004782 output->allocation.x = x;
4783 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004784 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004785
4786 if (output->make)
4787 free(output->make);
4788 output->make = strdup(make);
4789
4790 if (output->model)
4791 free(output->model);
4792 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004793}
4794
4795static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004796display_handle_done(void *data,
4797 struct wl_output *wl_output)
4798{
4799}
4800
4801static void
4802display_handle_scale(void *data,
4803 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004804 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004805{
4806 struct output *output = data;
4807
4808 output->scale = scale;
4809}
4810
4811static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004812display_handle_mode(void *data,
4813 struct wl_output *wl_output,
4814 uint32_t flags,
4815 int width,
4816 int height,
4817 int refresh)
4818{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004819 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004820 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004821
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004822 if (flags & WL_OUTPUT_MODE_CURRENT) {
4823 output->allocation.width = width;
4824 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004825 if (display->output_configure_handler)
4826 (*display->output_configure_handler)(
4827 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004828 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004829}
4830
4831static const struct wl_output_listener output_listener = {
4832 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004833 display_handle_mode,
4834 display_handle_done,
4835 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004836};
4837
4838static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004839display_add_output(struct display *d, uint32_t id)
4840{
4841 struct output *output;
4842
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004843 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004844 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004845 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004846 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004847 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004848 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004849 wl_list_insert(d->output_list.prev, &output->link);
4850
4851 wl_output_add_listener(output->output, &output_listener, output);
4852}
4853
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004854static void
4855output_destroy(struct output *output)
4856{
4857 if (output->destroy_handler)
4858 (*output->destroy_handler)(output, output->user_data);
4859
4860 wl_output_destroy(output->output);
4861 wl_list_remove(&output->link);
4862 free(output);
4863}
4864
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004865static void
4866display_destroy_output(struct display *d, uint32_t id)
4867{
4868 struct output *output;
4869
4870 wl_list_for_each(output, &d->output_list, link) {
4871 if (output->server_output_id == id) {
4872 output_destroy(output);
4873 break;
4874 }
4875 }
4876}
4877
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004878void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004879display_set_global_handler(struct display *display,
4880 display_global_handler_t handler)
4881{
4882 struct global *global;
4883
4884 display->global_handler = handler;
4885 if (!handler)
4886 return;
4887
4888 wl_list_for_each(global, &display->global_list, link)
4889 display->global_handler(display,
4890 global->name, global->interface,
4891 global->version, display->user_data);
4892}
4893
4894void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004895display_set_global_handler_remove(struct display *display,
4896 display_global_handler_t remove_handler)
4897{
4898 display->global_handler_remove = remove_handler;
4899 if (!remove_handler)
4900 return;
4901}
4902
4903void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004904display_set_output_configure_handler(struct display *display,
4905 display_output_handler_t handler)
4906{
4907 struct output *output;
4908
4909 display->output_configure_handler = handler;
4910 if (!handler)
4911 return;
4912
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004913 wl_list_for_each(output, &display->output_list, link) {
4914 if (output->allocation.width == 0 &&
4915 output->allocation.height == 0)
4916 continue;
4917
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004918 (*display->output_configure_handler)(output,
4919 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004920 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004921}
4922
4923void
4924output_set_user_data(struct output *output, void *data)
4925{
4926 output->user_data = data;
4927}
4928
4929void *
4930output_get_user_data(struct output *output)
4931{
4932 return output->user_data;
4933}
4934
4935void
4936output_set_destroy_handler(struct output *output,
4937 display_output_handler_t handler)
4938{
4939 output->destroy_handler = handler;
4940 /* FIXME: implement this, once we have way to remove outputs */
4941}
4942
4943void
Scott Moreau4e072362012-09-29 02:03:11 -06004944output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004945{
Scott Moreau4e072362012-09-29 02:03:11 -06004946 struct rectangle allocation = output->allocation;
4947
4948 switch (output->transform) {
4949 case WL_OUTPUT_TRANSFORM_90:
4950 case WL_OUTPUT_TRANSFORM_270:
4951 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4952 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4953 /* Swap width and height */
4954 allocation.width = output->allocation.height;
4955 allocation.height = output->allocation.width;
4956 break;
4957 }
4958
4959 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004960}
4961
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004962struct wl_output *
4963output_get_wl_output(struct output *output)
4964{
4965 return output->output;
4966}
4967
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004968enum wl_output_transform
4969output_get_transform(struct output *output)
4970{
4971 return output->transform;
4972}
4973
Alexander Larssonafd319a2013-05-22 14:41:27 +02004974uint32_t
4975output_get_scale(struct output *output)
4976{
4977 return output->scale;
4978}
4979
Jason Ekstrand738715d2014-04-02 19:53:50 -05004980const char *
4981output_get_make(struct output *output)
4982{
4983 return output->make;
4984}
4985
4986const char *
4987output_get_model(struct output *output)
4988{
4989 return output->model;
4990}
4991
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004992static void
Daniel Stone97f68542012-05-30 16:32:01 +01004993fini_xkb(struct input *input)
4994{
4995 xkb_state_unref(input->xkb.state);
4996 xkb_map_unref(input->xkb.keymap);
4997}
4998
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004999#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005000
Daniel Stone97f68542012-05-30 16:32:01 +01005001static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005002display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005003{
5004 struct input *input;
5005
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005006 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005007 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005008 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005009 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005010 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005011 input->pointer_focus = NULL;
5012 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005013 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005014 wl_list_insert(d->input_list.prev, &input->link);
5015
Daniel Stone37816df2012-05-16 18:45:18 +01005016 wl_seat_add_listener(input->seat, &seat_listener, input);
5017 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005018
Jason Ekstranda669bd52014-04-02 19:53:51 -05005019 if (d->data_device_manager) {
5020 input->data_device =
5021 wl_data_device_manager_get_data_device(d->data_device_manager,
5022 input->seat);
5023 wl_data_device_add_listener(input->data_device,
5024 &data_device_listener,
5025 input);
5026 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005027
5028 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005029
Jonny Lamb06959082014-08-12 14:58:27 +02005030 set_repeat_info(input, 40, 400);
5031
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005032 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5033 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005034 input->repeat_task.run = keyboard_repeat_func;
5035 display_watch_fd(d, input->repeat_timer_fd,
5036 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005037}
5038
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005039static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005040input_destroy(struct input *input)
5041{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005042 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005043 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005044
5045 if (input->drag_offer)
5046 data_offer_destroy(input->drag_offer);
5047
5048 if (input->selection_offer)
5049 data_offer_destroy(input->selection_offer);
5050
Jason Ekstranda669bd52014-04-02 19:53:51 -05005051 if (input->data_device)
5052 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005053
5054 if (input->display->seat_version >= 3) {
5055 if (input->pointer)
5056 wl_pointer_release(input->pointer);
5057 if (input->keyboard)
5058 wl_keyboard_release(input->keyboard);
5059 }
5060
Daniel Stone97f68542012-05-30 16:32:01 +01005061 fini_xkb(input);
5062
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005063 wl_surface_destroy(input->pointer_surface);
5064
Pekka Paalanene1207c72011-12-16 12:02:09 +02005065 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005066 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005067 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005068 free(input);
5069}
5070
5071static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005072init_workspace_manager(struct display *d, uint32_t id)
5073{
5074 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005075 wl_registry_bind(d->registry, id,
5076 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005077 if (d->workspace_manager != NULL)
5078 workspace_manager_add_listener(d->workspace_manager,
5079 &workspace_manager_listener,
5080 d);
5081}
5082
5083static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005084shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5085{
5086 struct display *d = data;
5087
5088 if (format == WL_SHM_FORMAT_RGB565)
5089 d->has_rgb565 = 1;
5090}
5091
5092struct wl_shm_listener shm_listener = {
5093 shm_format
5094};
5095
5096static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005097xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5098{
5099 xdg_shell_pong(shell, serial);
5100}
5101
5102static const struct xdg_shell_listener xdg_shell_listener = {
5103 xdg_shell_ping,
5104};
5105
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08005106#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005107#ifdef static_assert
5108static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5109 "Interface version doesn't match implementation version");
5110#endif
5111
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005112static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005113registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5114 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005115{
5116 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005117 struct global *global;
5118
Brian Lovinbc919262013-08-07 15:34:59 -07005119 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005120 global->name = id;
5121 global->interface = strdup(interface);
5122 global->version = version;
5123 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005124
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005125 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005126 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005127 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005128 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005129 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005130 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005131 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005132 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005133 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005134 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005135 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005136 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5137 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005138 wl_registry_bind(registry, id,
5139 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005140 } else if (strcmp(interface, "xdg_shell") == 0) {
5141 d->xdg_shell = wl_registry_bind(registry, id,
5142 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005143 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005144 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005145 } else if (strcmp(interface, "text_cursor_position") == 0) {
5146 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005147 wl_registry_bind(registry, id,
5148 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005149 } else if (strcmp(interface, "workspace_manager") == 0) {
5150 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005151 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5152 d->subcompositor =
5153 wl_registry_bind(registry, id,
5154 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005155 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005156
5157 if (d->global_handler)
5158 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005159}
5160
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005161static void
5162registry_handle_global_remove(void *data, struct wl_registry *registry,
5163 uint32_t name)
5164{
5165 struct display *d = data;
5166 struct global *global;
5167 struct global *tmp;
5168
5169 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5170 if (global->name != name)
5171 continue;
5172
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005173 if (strcmp(global->interface, "wl_output") == 0)
5174 display_destroy_output(d, name);
5175
5176 /* XXX: Should destroy remaining bound globals */
5177
5178 if (d->global_handler_remove)
5179 d->global_handler_remove(d, name, global->interface,
5180 global->version, d->user_data);
5181
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005182 wl_list_remove(&global->link);
5183 free(global->interface);
5184 free(global);
5185 }
5186}
5187
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005188void *
5189display_bind(struct display *display, uint32_t name,
5190 const struct wl_interface *interface, uint32_t version)
5191{
5192 return wl_registry_bind(display->registry, name, interface, version);
5193}
5194
5195static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005196 registry_handle_global,
5197 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005198};
5199
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005200#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005201static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005202init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005203{
5204 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005205 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005206
Rob Clark6396ed32012-03-11 19:48:41 -05005207#ifdef USE_CAIRO_GLESV2
5208# define GL_BIT EGL_OPENGL_ES2_BIT
5209#else
5210# define GL_BIT EGL_OPENGL_BIT
5211#endif
5212
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005213 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005214 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005215 EGL_RED_SIZE, 1,
5216 EGL_GREEN_SIZE, 1,
5217 EGL_BLUE_SIZE, 1,
5218 EGL_ALPHA_SIZE, 1,
5219 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005220 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005221 EGL_NONE
5222 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005223
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005224#ifdef USE_CAIRO_GLESV2
5225 static const EGLint context_attribs[] = {
5226 EGL_CONTEXT_CLIENT_VERSION, 2,
5227 EGL_NONE
5228 };
5229 EGLint api = EGL_OPENGL_ES_API;
5230#else
5231 EGLint *context_attribs = NULL;
5232 EGLint api = EGL_OPENGL_API;
5233#endif
5234
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005235 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005236 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005237 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005238 return -1;
5239 }
5240
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005241 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005242 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005243 return -1;
5244 }
5245
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005246 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5247 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005248 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005249 return -1;
5250 }
5251
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005252 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005253 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005254 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005255 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005256 return -1;
5257 }
5258
Benjamin Franzke0c991632011-09-27 21:57:31 +02005259 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5260 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005261 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005262 return -1;
5263 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005264
5265 return 0;
5266}
5267
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005268static void
5269fini_egl(struct display *display)
5270{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005271 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005272
5273 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5274 EGL_NO_CONTEXT);
5275
5276 eglTerminate(display->dpy);
5277 eglReleaseThread();
5278}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005279#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005280
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005281static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005282init_dummy_surface(struct display *display)
5283{
5284 int len;
5285 void *data;
5286
5287 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5288 data = malloc(len);
5289 display->dummy_surface =
5290 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5291 1, 1, len);
5292 display->dummy_surface_data = data;
5293}
5294
5295static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005296handle_display_data(struct task *task, uint32_t events)
5297{
5298 struct display *display =
5299 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005300 struct epoll_event ep;
5301 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005302
5303 display->display_fd_events = events;
5304
5305 if (events & EPOLLERR || events & EPOLLHUP) {
5306 display_exit(display);
5307 return;
5308 }
5309
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005310 if (events & EPOLLIN) {
5311 ret = wl_display_dispatch(display->display);
5312 if (ret == -1) {
5313 display_exit(display);
5314 return;
5315 }
5316 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005317
5318 if (events & EPOLLOUT) {
5319 ret = wl_display_flush(display->display);
5320 if (ret == 0) {
5321 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5322 ep.data.ptr = &display->display_task;
5323 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5324 display->display_fd, &ep);
5325 } else if (ret == -1 && errno != EAGAIN) {
5326 display_exit(display);
5327 return;
5328 }
5329 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005330}
5331
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005332static void
5333log_handler(const char *format, va_list args)
5334{
5335 vfprintf(stderr, format, args);
5336}
5337
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005338struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005339display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005340{
5341 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005342
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005343 wl_log_set_handler_client(log_handler);
5344
Peter Huttererf3d62272013-08-08 11:57:05 +10005345 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005346 if (d == NULL)
5347 return NULL;
5348
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005349 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005350 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005351 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005352 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005353 return NULL;
5354 }
5355
Rob Bradford5ab9c752013-07-26 16:29:43 +01005356 d->xkb_context = xkb_context_new(0);
5357 if (d->xkb_context == NULL) {
5358 fprintf(stderr, "Failed to create XKB context\n");
5359 free(d);
5360 return NULL;
5361 }
5362
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005363 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005364 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005365 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005366 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5367 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005368
5369 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005370 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005371 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005372 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005373
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005374 d->workspace = 0;
5375 d->workspace_count = 1;
5376
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005377 d->registry = wl_display_get_registry(d->display);
5378 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005379
5380 if (wl_display_dispatch(d->display) < 0) {
5381 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5382 return NULL;
5383 }
5384
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005385#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005386 if (init_egl(d) < 0)
5387 fprintf(stderr, "EGL does not seem to work, "
5388 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005389#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005390
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005391 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005392
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005393 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005394
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005395 wl_list_init(&d->window_list);
5396
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005397 init_dummy_surface(d);
5398
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005399 return d;
5400}
5401
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005402static void
5403display_destroy_outputs(struct display *display)
5404{
5405 struct output *tmp;
5406 struct output *output;
5407
5408 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5409 output_destroy(output);
5410}
5411
Pekka Paalanene1207c72011-12-16 12:02:09 +02005412static void
5413display_destroy_inputs(struct display *display)
5414{
5415 struct input *tmp;
5416 struct input *input;
5417
5418 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5419 input_destroy(input);
5420}
5421
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005422void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005423display_destroy(struct display *display)
5424{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005425 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005426 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5427 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005428
5429 if (!wl_list_empty(&display->deferred_list))
5430 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5431
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005432 cairo_surface_destroy(display->dummy_surface);
5433 free(display->dummy_surface_data);
5434
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005435 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005436 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005437
Daniel Stone97f68542012-05-30 16:32:01 +01005438 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005439
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005440 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005441 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005442
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005443#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005444 if (display->argb_device)
5445 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005446#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005447
Pekka Paalanen35e82632013-04-25 13:57:48 +03005448 if (display->subcompositor)
5449 wl_subcompositor_destroy(display->subcompositor);
5450
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005451 if (display->xdg_shell)
5452 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005453
5454 if (display->shm)
5455 wl_shm_destroy(display->shm);
5456
5457 if (display->data_device_manager)
5458 wl_data_device_manager_destroy(display->data_device_manager);
5459
5460 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005461 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005462
5463 close(display->epoll_fd);
5464
U. Artie Eoff44874d92012-10-02 21:12:35 -07005465 if (!(display->display_fd_events & EPOLLERR) &&
5466 !(display->display_fd_events & EPOLLHUP))
5467 wl_display_flush(display->display);
5468
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005469 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005470 free(display);
5471}
5472
5473void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005474display_set_user_data(struct display *display, void *data)
5475{
5476 display->user_data = data;
5477}
5478
5479void *
5480display_get_user_data(struct display *display)
5481{
5482 return display->user_data;
5483}
5484
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005485struct wl_display *
5486display_get_display(struct display *display)
5487{
5488 return display->display;
5489}
5490
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005491int
5492display_has_subcompositor(struct display *display)
5493{
5494 if (display->subcompositor)
5495 return 1;
5496
5497 wl_display_roundtrip(display->display);
5498
5499 return display->subcompositor != NULL;
5500}
5501
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005502cairo_device_t *
5503display_get_cairo_device(struct display *display)
5504{
5505 return display->argb_device;
5506}
5507
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005508struct output *
5509display_get_output(struct display *display)
5510{
5511 return container_of(display->output_list.next, struct output, link);
5512}
5513
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005514struct wl_compositor *
5515display_get_compositor(struct display *display)
5516{
5517 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005518}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005519
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005520uint32_t
5521display_get_serial(struct display *display)
5522{
5523 return display->serial;
5524}
5525
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005526EGLDisplay
5527display_get_egl_display(struct display *d)
5528{
5529 return d->dpy;
5530}
5531
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005532struct wl_data_source *
5533display_create_data_source(struct display *display)
5534{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005535 if (display->data_device_manager)
5536 return wl_data_device_manager_create_data_source(display->data_device_manager);
5537 else
5538 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005539}
5540
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005541EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005542display_get_argb_egl_config(struct display *d)
5543{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005544 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005545}
5546
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005547int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005548display_acquire_window_surface(struct display *display,
5549 struct window *window,
5550 EGLContext ctx)
5551{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005552 struct surface *surface = window->main_surface;
5553
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005554 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005555 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005556
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005557 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005558 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005559}
5560
5561void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005562display_release_window_surface(struct display *display,
5563 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005564{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005565 struct surface *surface = window->main_surface;
5566
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005567 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005568 return;
5569
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005570 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005571}
5572
5573void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005574display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005575{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005576 wl_list_insert(&display->deferred_list, &task->link);
5577}
5578
5579void
5580display_watch_fd(struct display *display,
5581 int fd, uint32_t events, struct task *task)
5582{
5583 struct epoll_event ep;
5584
5585 ep.events = events;
5586 ep.data.ptr = task;
5587 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5588}
5589
5590void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005591display_unwatch_fd(struct display *display, int fd)
5592{
5593 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5594}
5595
5596void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005597display_run(struct display *display)
5598{
5599 struct task *task;
5600 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005601 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005602
Pekka Paalanen826d7952011-12-15 10:14:07 +02005603 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005604 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005605 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005606 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005607 struct task, link);
5608 wl_list_remove(&task->link);
5609 task->run(task, 0);
5610 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005611
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005612 wl_display_dispatch_pending(display->display);
5613
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005614 if (!display->running)
5615 break;
5616
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005617 ret = wl_display_flush(display->display);
5618 if (ret < 0 && errno == EAGAIN) {
5619 ep[0].events =
5620 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5621 ep[0].data.ptr = &display->display_task;
5622
5623 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5624 display->display_fd, &ep[0]);
5625 } else if (ret < 0) {
5626 break;
5627 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005628
5629 count = epoll_wait(display->epoll_fd,
5630 ep, ARRAY_LENGTH(ep), -1);
5631 for (i = 0; i < count; i++) {
5632 task = ep[i].data.ptr;
5633 task->run(task, ep[i].events);
5634 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005635 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005636}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005637
5638void
5639display_exit(struct display *display)
5640{
5641 display->running = 0;
5642}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005643
5644void
5645keysym_modifiers_add(struct wl_array *modifiers_map,
5646 const char *name)
5647{
5648 size_t len = strlen(name) + 1;
5649 char *p;
5650
5651 p = wl_array_add(modifiers_map, len);
5652
5653 if (p == NULL)
5654 return;
5655
5656 strncpy(p, name, len);
5657}
5658
5659static xkb_mod_index_t
5660keysym_modifiers_get_index(struct wl_array *modifiers_map,
5661 const char *name)
5662{
5663 xkb_mod_index_t index = 0;
5664 char *p = modifiers_map->data;
5665
5666 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5667 if (strcmp(p, name) == 0)
5668 return index;
5669
5670 index++;
5671 p += strlen(p) + 1;
5672 }
5673
5674 return XKB_MOD_INVALID;
5675}
5676
5677xkb_mod_mask_t
5678keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5679 const char *name)
5680{
5681 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5682
5683 if (index == XKB_MOD_INVALID)
5684 return XKB_MOD_INVALID;
5685
5686 return 1 << index;
5687}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005688
5689void *
5690fail_on_null(void *p)
5691{
5692 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005693 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005694 exit(EXIT_FAILURE);
5695 }
5696
5697 return p;
5698}
5699
5700void *
5701xmalloc(size_t s)
5702{
5703 return fail_on_null(malloc(s));
5704}
5705
Peter Huttererf3d62272013-08-08 11:57:05 +10005706void *
5707xzalloc(size_t s)
5708{
5709 return fail_on_null(zalloc(s));
5710}
5711
Kristian Høgsbergce278412013-07-25 15:20:20 -07005712char *
5713xstrdup(const char *s)
5714{
5715 return fail_on_null(strdup(s));
5716}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005717
5718void *
5719xrealloc(char *p, size_t s)
5720{
5721 return fail_on_null(realloc(p, s));
5722}