blob: 139c7f9075b24221dfca0272eb95aa64f31ca945 [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;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200220 struct rectangle last_geometry;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500221 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400222 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300223 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400224 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400225 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500226 int custom;
227 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400228
Pekka Paalanen99436862012-11-19 17:15:59 +0200229 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400230
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500231 int fullscreen;
232 int maximized;
233
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200234 enum preferred_format preferred_format;
235
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500236 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500237 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400238 window_data_handler_t data_handler;
239 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500240 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400241 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200242 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700243 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400244
Pekka Paalanen4e373742013-02-13 16:17:13 +0200245 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500246 struct xdg_surface *xdg_surface;
247 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300248
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700249 struct window *parent;
Ondřej Majerechb2c18642014-09-13 16:35:45 +0200250 struct wl_surface *last_parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500251
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500252 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500253
Pekka Paalanen35e82632013-04-25 13:57:48 +0300254 /* struct surface::link, contains also main_surface */
255 struct wl_list subsurface_list;
256
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500257 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400258 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500259};
260
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500261struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500262 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200263 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300264 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500265 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400266 struct wl_list link;
267 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500268 widget_resize_handler_t resize_handler;
269 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500270 widget_enter_handler_t enter_handler;
271 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500272 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500273 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700274 widget_touch_down_handler_t touch_down_handler;
275 widget_touch_up_handler_t touch_up_handler;
276 widget_touch_motion_handler_t touch_motion_handler;
277 widget_touch_frame_handler_t touch_frame_handler;
278 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200279 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400280 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500281 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300282 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500283 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000284 /* If this is set to false then no cairo surface will be
285 * created before redrawing the surface. This is useful if the
286 * redraw handler is going to do completely custom rendering
287 * such as using EGL directly */
288 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400289};
290
Rusty Lynch041815a2013-08-08 21:20:38 -0700291struct touch_point {
292 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800293 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700294 struct widget *widget;
295 struct wl_list link;
296};
297
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400298struct input {
299 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100300 struct wl_seat *seat;
301 struct wl_pointer *pointer;
302 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700303 struct wl_touch *touch;
304 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400305 struct window *pointer_focus;
306 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700307 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300308 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300309 uint32_t cursor_anim_start;
310 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300311 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400312 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400313 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400314 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400315 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400317
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500318 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500319 struct widget *grab;
320 uint32_t grab_button;
321
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400322 struct wl_data_device *data_device;
323 struct data_offer *drag_offer;
324 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800325 uint32_t touch_grab;
326 int32_t touch_grab_id;
327 float drag_x, drag_y;
328 struct window *drag_focus;
329 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100330
331 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100332 struct xkb_keymap *keymap;
333 struct xkb_state *state;
334 xkb_mod_mask_t control_mask;
335 xkb_mod_mask_t alt_mask;
336 xkb_mod_mask_t shift_mask;
337 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400338
Jonny Lamb06959082014-08-12 14:58:27 +0200339 int32_t repeat_rate_sec;
340 int32_t repeat_rate_nsec;
341 int32_t repeat_delay_sec;
342 int32_t repeat_delay_nsec;
343
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400344 struct task repeat_task;
345 int repeat_timer_fd;
346 uint32_t repeat_sym;
347 uint32_t repeat_key;
348 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400349};
350
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500351struct output {
352 struct display *display;
353 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800354 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500355 struct rectangle allocation;
356 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600357 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200358 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500359 char *make;
360 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200361
362 display_output_handler_t destroy_handler;
363 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500364};
365
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500366struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500367 struct widget *widget;
368 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500369 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800370
371 uint32_t last_time;
372 uint32_t did_double, double_click;
Xiong Zhang382de462014-06-12 11:06:26 +0800373 int32_t last_id, double_id;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500374};
375
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500376struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400377 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500378 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500379 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500380 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700381 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500382 const char **entries;
383 uint32_t time;
384 int current;
385 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400386 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500387 menu_func_t func;
388};
389
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300390struct tooltip {
391 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300392 struct widget *widget;
393 char *entry;
394 struct task tooltip_task;
395 int tooltip_fd;
396 float x, y;
397};
398
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700399struct shm_pool {
400 struct wl_shm_pool *pool;
401 size_t size;
402 size_t used;
403 void *data;
404};
405
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400406enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300407 CURSOR_DEFAULT = 100,
408 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400409};
410
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200411static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400412
Pekka Paalanen97777442013-05-22 10:20:05 +0300413/* #define DEBUG */
414
415#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300416
417static void
418debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
419__attribute__ ((format (printf, 4, 5)));
420
421static void
422debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
423{
424 va_list ap;
425 struct timeval tv;
426
427 gettimeofday(&tv, NULL);
428 fprintf(stderr, "%8ld.%03ld ",
429 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
430
431 if (proxy)
432 fprintf(stderr, "%s@%d ",
433 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
434
435 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
436 fprintf(stderr, "%s ", func);
437
438 va_start(ap, fmt);
439 vfprintf(stderr, fmt, ap);
440 va_end(ap);
441}
442
443#define DBG(fmt, ...) \
444 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
445
446#define DBG_OBJ(obj, fmt, ...) \
447 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
448
449#else
450
451#define DBG(...) do {} while (0)
452#define DBG_OBJ(...) do {} while (0)
453
454#endif
455
Alexander Larsson1818e312013-05-22 14:41:31 +0200456static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200457surface_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 +0200458{
459 int32_t tmp;
460
461 switch (buffer_transform) {
462 case WL_OUTPUT_TRANSFORM_90:
463 case WL_OUTPUT_TRANSFORM_270:
464 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
465 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
466 tmp = *width;
467 *width = *height;
468 *height = tmp;
469 break;
470 default:
471 break;
472 }
473
474 *width *= buffer_scale;
475 *height *= buffer_scale;
476}
477
478static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200479buffer_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 +0200480{
481 int32_t tmp;
482
483 switch (buffer_transform) {
484 case WL_OUTPUT_TRANSFORM_90:
485 case WL_OUTPUT_TRANSFORM_270:
486 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
487 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
488 tmp = *width;
489 *width = *height;
490 *height = tmp;
491 break;
492 default:
493 break;
494 }
495
496 *width /= buffer_scale;
497 *height /= buffer_scale;
498}
499
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500500#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400501
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200502struct egl_window_surface {
503 struct toysurface base;
504 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100505 struct display *display;
506 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200507 struct wl_egl_window *egl_window;
508 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100509};
510
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511static struct egl_window_surface *
512to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100515}
516
517static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200519 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200520 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522 struct egl_window_surface *surface = to_egl_window_surface(base);
523
Alexander Larsson1818e312013-05-22 14:41:31 +0200524 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
525
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
527 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
528
529 return cairo_surface_reference(surface->cairo_surface);
530}
531
532static void
533egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200534 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200535 struct rectangle *server_allocation)
536{
537 struct egl_window_surface *surface = to_egl_window_surface(base);
538
539 cairo_gl_surface_swapbuffers(surface->cairo_surface);
540 wl_egl_window_get_attached_size(surface->egl_window,
541 &server_allocation->width,
542 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200543
544 buffer_to_surface_size (buffer_transform, buffer_scale,
545 &server_allocation->width,
546 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547}
548
549static int
550egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
551{
552 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200553 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100554
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200555 device = cairo_surface_get_device(surface->cairo_surface);
556 if (!device)
557 return -1;
558
559 if (!ctx) {
560 if (device == surface->display->argb_device)
561 ctx = surface->display->argb_ctx;
562 else
563 assert(0);
564 }
565
566 cairo_device_flush(device);
567 cairo_device_acquire(device);
568 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
569 surface->egl_surface, ctx))
570 fprintf(stderr, "failed to make surface current\n");
571
572 return 0;
573}
574
575static void
576egl_window_surface_release(struct toysurface *base)
577{
578 struct egl_window_surface *surface = to_egl_window_surface(base);
579 cairo_device_t *device;
580
581 device = cairo_surface_get_device(surface->cairo_surface);
582 if (!device)
583 return;
584
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200585 if (!eglMakeCurrent(surface->display->dpy,
586 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200587 fprintf(stderr, "failed to make context current\n");
588
589 cairo_device_release(device);
590}
591
592static void
593egl_window_surface_destroy(struct toysurface *base)
594{
595 struct egl_window_surface *surface = to_egl_window_surface(base);
596 struct display *d = surface->display;
597
598 cairo_surface_destroy(surface->cairo_surface);
599 eglDestroySurface(d->dpy, surface->egl_surface);
600 wl_egl_window_destroy(surface->egl_window);
601 surface->surface = NULL;
602
603 free(surface);
604}
605
606static struct toysurface *
607egl_window_surface_create(struct display *display,
608 struct wl_surface *wl_surface,
609 uint32_t flags,
610 struct rectangle *rectangle)
611{
612 struct egl_window_surface *surface;
613
Pekka Paalanenb3627362012-11-19 17:16:00 +0200614 if (display->dpy == EGL_NO_DISPLAY)
615 return NULL;
616
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200617 surface = calloc(1, sizeof *surface);
618 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100619 return NULL;
620
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200621 surface->base.prepare = egl_window_surface_prepare;
622 surface->base.swap = egl_window_surface_swap;
623 surface->base.acquire = egl_window_surface_acquire;
624 surface->base.release = egl_window_surface_release;
625 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100626
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200627 surface->display = display;
628 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400629
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200630 surface->egl_window = wl_egl_window_create(surface->surface,
631 rectangle->width,
632 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100633
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200634 surface->egl_surface = eglCreateWindowSurface(display->dpy,
635 display->argb_config,
636 surface->egl_window,
637 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200639 surface->cairo_surface =
640 cairo_gl_surface_create_for_egl(display->argb_device,
641 surface->egl_surface,
642 rectangle->width,
643 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100644
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200645 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100646}
647
Pekka Paalanenb3627362012-11-19 17:16:00 +0200648#else
649
650static struct toysurface *
651egl_window_surface_create(struct display *display,
652 struct wl_surface *wl_surface,
653 uint32_t flags,
654 struct rectangle *rectangle)
655{
656 return NULL;
657}
658
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400659#endif
660
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200661struct shm_surface_data {
662 struct wl_buffer *buffer;
663 struct shm_pool *pool;
664};
665
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400666struct wl_buffer *
667display_get_buffer_for_surface(struct display *display,
668 cairo_surface_t *surface)
669{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200670 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400671
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200672 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400673
674 return data->buffer;
675}
676
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500677static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700678shm_pool_destroy(struct shm_pool *pool);
679
680static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400681shm_surface_data_destroy(void *p)
682{
683 struct shm_surface_data *data = p;
684
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200685 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700686 if (data->pool)
687 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300688
689 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400690}
691
Kristian Høgsberg16626282012-04-03 11:21:27 -0400692static struct wl_shm_pool *
693make_shm_pool(struct display *display, int size, void **data)
694{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400695 struct wl_shm_pool *pool;
696 int fd;
697
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300698 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400699 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300700 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
701 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400702 return NULL;
703 }
704
705 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400706 if (*data == MAP_FAILED) {
707 fprintf(stderr, "mmap failed: %m\n");
708 close(fd);
709 return NULL;
710 }
711
712 pool = wl_shm_create_pool(display->shm, fd, size);
713
714 close(fd);
715
716 return pool;
717}
718
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700719static struct shm_pool *
720shm_pool_create(struct display *display, size_t size)
721{
722 struct shm_pool *pool = malloc(sizeof *pool);
723
724 if (!pool)
725 return NULL;
726
727 pool->pool = make_shm_pool(display, size, &pool->data);
728 if (!pool->pool) {
729 free(pool);
730 return NULL;
731 }
732
733 pool->size = size;
734 pool->used = 0;
735
736 return pool;
737}
738
739static void *
740shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
741{
742 if (pool->used + size > pool->size)
743 return NULL;
744
745 *offset = pool->used;
746 pool->used += size;
747
748 return (char *) pool->data + *offset;
749}
750
751/* destroy the pool. this does not unmap the memory though */
752static void
753shm_pool_destroy(struct shm_pool *pool)
754{
755 munmap(pool->data, pool->size);
756 wl_shm_pool_destroy(pool->pool);
757 free(pool);
758}
759
760/* Start allocating from the beginning of the pool again */
761static void
762shm_pool_reset(struct shm_pool *pool)
763{
764 pool->used = 0;
765}
766
767static int
768data_length_for_shm_surface(struct rectangle *rect)
769{
770 int stride;
771
772 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
773 rect->width);
774 return stride * rect->height;
775}
776
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500777static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700778display_create_shm_surface_from_pool(struct display *display,
779 struct rectangle *rectangle,
780 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400781{
782 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400783 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400784 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200785 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700786 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400787 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400788
789 data = malloc(sizeof *data);
790 if (data == NULL)
791 return NULL;
792
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200793 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
794 cairo_format = CAIRO_FORMAT_RGB16_565;
795 else
796 cairo_format = CAIRO_FORMAT_ARGB32;
797
798 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700799 length = stride * rectangle->height;
800 data->pool = NULL;
801 map = shm_pool_allocate(pool, length, &offset);
802
803 if (!map) {
804 free(data);
805 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400806 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400807
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400808 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200809 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810 rectangle->width,
811 rectangle->height,
812 stride);
813
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200814 cairo_surface_set_user_data(surface, &shm_surface_data_key,
815 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400816
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200817 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
818 format = WL_SHM_FORMAT_RGB565;
819 else {
820 if (flags & SURFACE_OPAQUE)
821 format = WL_SHM_FORMAT_XRGB8888;
822 else
823 format = WL_SHM_FORMAT_ARGB8888;
824 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400825
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200826 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
827 rectangle->width,
828 rectangle->height,
829 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400830
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700831 return surface;
832}
833
834static cairo_surface_t *
835display_create_shm_surface(struct display *display,
836 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200837 struct shm_pool *alternate_pool,
838 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700839{
840 struct shm_surface_data *data;
841 struct shm_pool *pool;
842 cairo_surface_t *surface;
843
Pekka Paalanen99436862012-11-19 17:15:59 +0200844 if (alternate_pool) {
845 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700846 surface = display_create_shm_surface_from_pool(display,
847 rectangle,
848 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200849 alternate_pool);
850 if (surface) {
851 data = cairo_surface_get_user_data(surface,
852 &shm_surface_data_key);
853 goto out;
854 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700855 }
856
857 pool = shm_pool_create(display,
858 data_length_for_shm_surface(rectangle));
859 if (!pool)
860 return NULL;
861
862 surface =
863 display_create_shm_surface_from_pool(display, rectangle,
864 flags, pool);
865
866 if (!surface) {
867 shm_pool_destroy(pool);
868 return NULL;
869 }
870
871 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200872 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700873 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400874
Pekka Paalanen99436862012-11-19 17:15:59 +0200875out:
876 if (data_ret)
877 *data_ret = data;
878
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400879 return surface;
880}
881
nobled7b87cb02011-02-01 18:51:47 +0000882static int
883check_size(struct rectangle *rect)
884{
885 if (rect->width && rect->height)
886 return 0;
887
888 fprintf(stderr, "tried to create surface of "
889 "width: %d, height: %d\n", rect->width, rect->height);
890 return -1;
891}
892
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400893cairo_surface_t *
894display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100895 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400896 struct rectangle *rectangle,
897 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400898{
nobled7b87cb02011-02-01 18:51:47 +0000899 if (check_size(rectangle) < 0)
900 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200901
902 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200903 return display_create_shm_surface(display, rectangle, flags,
904 NULL, NULL);
905}
906
Pekka Paalanena4eda732012-11-19 17:16:02 +0200907struct shm_surface_leaf {
908 cairo_surface_t *cairo_surface;
909 /* 'data' is automatically destroyed, when 'cairo_surface' is */
910 struct shm_surface_data *data;
911
912 struct shm_pool *resize_pool;
913 int busy;
914};
915
916static void
917shm_surface_leaf_release(struct shm_surface_leaf *leaf)
918{
919 if (leaf->cairo_surface)
920 cairo_surface_destroy(leaf->cairo_surface);
921 /* leaf->data already destroyed via cairo private */
922
923 if (leaf->resize_pool)
924 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200925
926 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200927}
928
Pekka Paalanenaef02542013-04-25 13:57:47 +0300929#define MAX_LEAVES 3
930
Pekka Paalanen99436862012-11-19 17:15:59 +0200931struct shm_surface {
932 struct toysurface base;
933 struct display *display;
934 struct wl_surface *surface;
935 uint32_t flags;
936 int dx, dy;
937
Pekka Paalanenaef02542013-04-25 13:57:47 +0300938 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200939 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200940};
941
942static struct shm_surface *
943to_shm_surface(struct toysurface *base)
944{
945 return container_of(base, struct shm_surface, base);
946}
947
Pekka Paalanena4eda732012-11-19 17:16:02 +0200948static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300949shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
950{
951#ifdef DEBUG
952 struct shm_surface_leaf *leaf;
953 char bufs[MAX_LEAVES + 1];
954 int i;
955
956 for (i = 0; i < MAX_LEAVES; i++) {
957 leaf = &surface->leaf[i];
958
959 if (leaf->busy)
960 bufs[i] = 'b';
961 else if (leaf->cairo_surface)
962 bufs[i] = 'a';
963 else
964 bufs[i] = ' ';
965 }
966
967 bufs[MAX_LEAVES] = '\0';
968 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
969#endif
970}
971
972static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200973shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
974{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200975 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300976 struct shm_surface_leaf *leaf;
977 int i;
978 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300979
980 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200981
Pekka Paalanenaef02542013-04-25 13:57:47 +0300982 for (i = 0; i < MAX_LEAVES; i++) {
983 leaf = &surface->leaf[i];
984 if (leaf->data && leaf->data->buffer == buffer) {
985 leaf->busy = 0;
986 break;
987 }
988 }
989 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200990
Pekka Paalanenaef02542013-04-25 13:57:47 +0300991 /* Leave one free leaf with storage, release others */
992 free_found = 0;
993 for (i = 0; i < MAX_LEAVES; i++) {
994 leaf = &surface->leaf[i];
995
996 if (!leaf->cairo_surface || leaf->busy)
997 continue;
998
999 if (!free_found)
1000 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001001 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001002 shm_surface_leaf_release(leaf);
1003 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001004
Pekka Paalanen97777442013-05-22 10:20:05 +03001005 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001006}
1007
1008static const struct wl_buffer_listener shm_surface_buffer_listener = {
1009 shm_surface_buffer_release
1010};
1011
Pekka Paalanen99436862012-11-19 17:15:59 +02001012static cairo_surface_t *
1013shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001014 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001015 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001016{
Pekka Paalanenec076692012-11-30 13:37:27 +02001017 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001018 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001019 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001020 struct shm_surface_leaf *leaf = NULL;
1021 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001022
1023 surface->dx = dx;
1024 surface->dy = dy;
1025
Pekka Paalanenaef02542013-04-25 13:57:47 +03001026 /* pick a free buffer, preferrably one that already has storage */
1027 for (i = 0; i < MAX_LEAVES; i++) {
1028 if (surface->leaf[i].busy)
1029 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001030
Pekka Paalanenaef02542013-04-25 13:57:47 +03001031 if (!leaf || surface->leaf[i].cairo_surface)
1032 leaf = &surface->leaf[i];
1033 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001034 DBG_OBJ(surface->surface, "pick leaf %d\n",
1035 (int)(leaf - &surface->leaf[0]));
1036
Pekka Paalanenaef02542013-04-25 13:57:47 +03001037 if (!leaf) {
1038 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001039 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001040 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001041 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001042 }
1043
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 if (!resize_hint && leaf->resize_pool) {
1045 cairo_surface_destroy(leaf->cairo_surface);
1046 leaf->cairo_surface = NULL;
1047 shm_pool_destroy(leaf->resize_pool);
1048 leaf->resize_pool = NULL;
1049 }
1050
Alexander Larsson1818e312013-05-22 14:41:31 +02001051 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1052
Pekka Paalanena4eda732012-11-19 17:16:02 +02001053 if (leaf->cairo_surface &&
1054 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1055 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001056 goto out;
1057
Pekka Paalanena4eda732012-11-19 17:16:02 +02001058 if (leaf->cairo_surface)
1059 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001060
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001061#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001062 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001063 /* Create a big pool to allocate from, while continuously
1064 * resizing. Mmapping a new pool in the server
1065 * is relatively expensive, so reusing a pool performs
1066 * better, but may temporarily reserve unneeded memory.
1067 */
1068 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001069 leaf->resize_pool = shm_pool_create(surface->display,
1070 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001071 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001072#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001073
Alexander Larsson1818e312013-05-22 14:41:31 +02001074 rect.width = width;
1075 rect.height = height;
1076
Pekka Paalanena4eda732012-11-19 17:16:02 +02001077 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001078 display_create_shm_surface(surface->display, &rect,
1079 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001080 leaf->resize_pool,
1081 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001082 if (!leaf->cairo_surface)
1083 return NULL;
1084
Pekka Paalanena4eda732012-11-19 17:16:02 +02001085 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001086 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001087
1088out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001089 surface->current = leaf;
1090
1091 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001092}
1093
1094static void
1095shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001096 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001097 struct rectangle *server_allocation)
1098{
1099 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001101
1102 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001103 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001104 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001106
Alexander Larsson1818e312013-05-22 14:41:31 +02001107 buffer_to_surface_size (buffer_transform, buffer_scale,
1108 &server_allocation->width,
1109 &server_allocation->height);
1110
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001112 surface->dx, surface->dy);
1113 wl_surface_damage(surface->surface, 0, 0,
1114 server_allocation->width, server_allocation->height);
1115 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116
Pekka Paalanen71233882013-04-25 13:57:53 +03001117 DBG_OBJ(surface->surface, "leaf %d busy\n",
1118 (int)(leaf - &surface->leaf[0]));
1119
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 leaf->busy = 1;
1121 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001122}
1123
1124static int
1125shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1126{
1127 return -1;
1128}
1129
1130static void
1131shm_surface_release(struct toysurface *base)
1132{
1133}
1134
1135static void
1136shm_surface_destroy(struct toysurface *base)
1137{
1138 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001139 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001140
Pekka Paalanenaef02542013-04-25 13:57:47 +03001141 for (i = 0; i < MAX_LEAVES; i++)
1142 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001143
1144 free(surface);
1145}
1146
1147static struct toysurface *
1148shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1149 uint32_t flags, struct rectangle *rectangle)
1150{
1151 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001152 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001153
Brian Lovinbc919262013-08-07 15:34:59 -07001154 surface = xmalloc(sizeof *surface);
1155 memset(surface, 0, sizeof *surface);
1156
Pekka Paalanen99436862012-11-19 17:15:59 +02001157 if (!surface)
1158 return NULL;
1159
1160 surface->base.prepare = shm_surface_prepare;
1161 surface->base.swap = shm_surface_swap;
1162 surface->base.acquire = shm_surface_acquire;
1163 surface->base.release = shm_surface_release;
1164 surface->base.destroy = shm_surface_destroy;
1165
1166 surface->display = display;
1167 surface->surface = wl_surface;
1168 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001169
1170 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001171}
1172
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001173/*
1174 * The following correspondences between file names and cursors was copied
1175 * from: https://bugs.kde.org/attachment.cgi?id=67313
1176 */
1177
1178static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001179 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001180 "sw-resize",
1181 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001182};
1183
1184static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001185 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001186 "se-resize",
1187 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001188};
1189
1190static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001191 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001192 "s-resize",
1193 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001194};
1195
1196static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001197 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001198 "closedhand",
1199 "208530c400c041818281048008011002"
1200};
1201
1202static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001203 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001204 "default",
1205 "top_left_arrow",
1206 "left-arrow"
1207};
1208
1209static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001210 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001211 "w-resize",
1212 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001213};
1214
1215static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001216 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001217 "e-resize",
1218 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001219};
1220
1221static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001222 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001223 "nw-resize",
1224 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001225};
1226
1227static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001228 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001229 "ne-resize",
1230 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001231};
1232
1233static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001234 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001235 "n-resize",
1236 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001237};
1238
1239static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001240 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241 "ibeam",
1242 "text"
1243};
1244
1245static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001246 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001247 "pointer",
1248 "pointing_hand",
1249 "e29285e634086352946a0e7090d73106"
1250};
1251
1252static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001253 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001254 "wait",
1255 "0426c94ea35c87780ff01dc239897213"
1256};
1257
1258struct cursor_alternatives {
1259 const char **names;
1260 size_t count;
1261};
1262
1263static const struct cursor_alternatives cursors[] = {
1264 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1265 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1266 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1267 {grabbings, ARRAY_LENGTH(grabbings)},
1268 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1269 {left_sides, ARRAY_LENGTH(left_sides)},
1270 {right_sides, ARRAY_LENGTH(right_sides)},
1271 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1272 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1273 {top_sides, ARRAY_LENGTH(top_sides)},
1274 {xterms, ARRAY_LENGTH(xterms)},
1275 {hand1s, ARRAY_LENGTH(hand1s)},
1276 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001277};
1278
1279static void
1280create_cursors(struct display *display)
1281{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001282 struct weston_config *config;
1283 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001284 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001285 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001286 unsigned int i, j;
1287 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001288
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001289 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001290 s = weston_config_get_section(config, "shell", NULL, NULL);
1291 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1292 weston_config_section_get_int(s, "cursor-size", &size, 32);
1293 weston_config_destroy(config);
1294
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001295 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001296 if (!display->cursor_theme) {
1297 fprintf(stderr, "could not load theme '%s'\n", theme);
1298 return;
1299 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001300 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001301 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001302 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001303
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001304 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001305 cursor = NULL;
1306 for (j = 0; !cursor && j < cursors[i].count; ++j)
1307 cursor = wl_cursor_theme_get_cursor(
1308 display->cursor_theme, cursors[i].names[j]);
1309
1310 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001311 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001312 cursors[i].names[0]);
1313
1314 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001315 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001316}
1317
1318static void
1319destroy_cursors(struct display *display)
1320{
1321 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001322 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001323}
1324
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001325struct wl_cursor_image *
1326display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001327{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001328 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001329
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001330 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001331}
1332
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001333static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001334surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001335{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001336 if (!surface->cairo_surface)
1337 return;
1338
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001339 if (surface->opaque_region) {
1340 wl_surface_set_opaque_region(surface->surface,
1341 surface->opaque_region);
1342 wl_region_destroy(surface->opaque_region);
1343 surface->opaque_region = NULL;
1344 }
1345
1346 if (surface->input_region) {
1347 wl_surface_set_input_region(surface->surface,
1348 surface->input_region);
1349 wl_region_destroy(surface->input_region);
1350 surface->input_region = NULL;
1351 }
1352
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001353 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001354 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001355 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001356
Pekka Paalanen89dee002013-02-13 16:17:20 +02001357 cairo_surface_destroy(surface->cairo_surface);
1358 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001359}
1360
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001361int
1362window_has_focus(struct window *window)
1363{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001364 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001365}
1366
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001367static void
1368window_close(struct window *window)
1369{
1370 if (window->close_handler)
1371 window->close_handler(window->user_data);
1372 else
1373 display_exit(window->display);
1374}
1375
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001376struct display *
1377window_get_display(struct window *window)
1378{
1379 return window->display;
1380}
1381
Pekka Paalanen89dee002013-02-13 16:17:20 +02001382static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001383surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001384{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001385 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001386 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001387
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001388 if (!surface->toysurface && display->dpy &&
1389 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001390 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001391 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001392 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001393 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001394 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001395 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001396
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001397 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001398 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001399 surface->surface,
1400 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001401
Pekka Paalanen89dee002013-02-13 16:17:20 +02001402 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001403 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001404 allocation.width, allocation.height, flags,
1405 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001406}
1407
1408static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001409window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001411 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001412 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001413
Pekka Paalanenec076692012-11-30 13:37:27 +02001414 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001415 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001416
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001417 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1418 flags |= SURFACE_HINT_RGB565;
1419
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001420 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001421}
1422
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001423int
1424window_get_buffer_transform(struct window *window)
1425{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001426 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001427}
1428
1429void
1430window_set_buffer_transform(struct window *window,
1431 enum wl_output_transform transform)
1432{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001433 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001434 wl_surface_set_buffer_transform(window->main_surface->surface,
1435 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001436}
1437
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001438void
1439window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001440 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001441{
1442 window->main_surface->buffer_scale = scale;
1443 wl_surface_set_buffer_scale(window->main_surface->surface,
1444 scale);
1445}
1446
1447uint32_t
1448window_get_buffer_scale(struct window *window)
1449{
1450 return window->main_surface->buffer_scale;
1451}
1452
Alexander Larssond68f5232013-05-22 14:41:33 +02001453uint32_t
1454window_get_output_scale(struct window *window)
1455{
1456 struct window_output *window_output;
1457 struct window_output *window_output_tmp;
1458 int scale = 1;
1459
1460 wl_list_for_each_safe(window_output, window_output_tmp,
1461 &window->window_output_list, link) {
1462 if (window_output->output->scale > scale)
1463 scale = window_output->output->scale;
1464 }
1465
1466 return scale;
1467}
1468
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001469static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001470
Pekka Paalanen4e373742013-02-13 16:17:13 +02001471static void
1472surface_destroy(struct surface *surface)
1473{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001474 if (surface->frame_cb)
1475 wl_callback_destroy(surface->frame_cb);
1476
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001477 if (surface->input_region)
1478 wl_region_destroy(surface->input_region);
1479
1480 if (surface->opaque_region)
1481 wl_region_destroy(surface->opaque_region);
1482
Pekka Paalanen35e82632013-04-25 13:57:48 +03001483 if (surface->subsurface)
1484 wl_subsurface_destroy(surface->subsurface);
1485
Pekka Paalanen4e373742013-02-13 16:17:13 +02001486 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001487
1488 if (surface->toysurface)
1489 surface->toysurface->destroy(surface->toysurface);
1490
Pekka Paalanen35e82632013-04-25 13:57:48 +03001491 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001492 free(surface);
1493}
1494
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001495void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001496window_destroy(struct window *window)
1497{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001498 struct display *display = window->display;
1499 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001500 struct window_output *window_output;
1501 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001502
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001503 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001504
Rusty Lynch1084da52013-08-15 09:10:08 -07001505 wl_list_for_each(input, &display->input_list, link) {
1506 if (input->touch_focus == window)
1507 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001508 if (input->pointer_focus == window)
1509 input->pointer_focus = NULL;
1510 if (input->keyboard_focus == window)
1511 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001512 if (input->focus_widget &&
1513 input->focus_widget->window == window)
1514 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001515 }
1516
Rob Bradford7507b572012-05-15 17:55:34 +01001517 wl_list_for_each_safe(window_output, window_output_tmp,
1518 &window->window_output_list, link) {
1519 free (window_output);
1520 }
1521
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001522 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001523 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001524
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001525 if (window->xdg_surface)
1526 xdg_surface_destroy(window->xdg_surface);
1527 if (window->xdg_popup)
1528 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001529
1530 surface_destroy(window->main_surface);
1531
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001532 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001533
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001534 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001535 free(window);
1536}
1537
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001538static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001539widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001540{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001541 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001542
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001543 wl_list_for_each(child, &widget->child_list, link) {
1544 target = widget_find_widget(child, x, y);
1545 if (target)
1546 return target;
1547 }
1548
1549 if (widget->allocation.x <= x &&
1550 x < widget->allocation.x + widget->allocation.width &&
1551 widget->allocation.y <= y &&
1552 y < widget->allocation.y + widget->allocation.height) {
1553 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001554 }
1555
1556 return NULL;
1557}
1558
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001559static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001560window_find_widget(struct window *window, int32_t x, int32_t y)
1561{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001562 struct surface *surface;
1563 struct widget *widget;
1564
1565 wl_list_for_each(surface, &window->subsurface_list, link) {
1566 widget = widget_find_widget(surface->widget, x, y);
1567 if (widget)
1568 return widget;
1569 }
1570
1571 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001572}
1573
1574static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001575widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001576{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001577 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001578
Peter Huttererf3d62272013-08-08 11:57:05 +10001579 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001580 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001581 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001582 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001583 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001584 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001585 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001586 widget->tooltip = NULL;
1587 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001588 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001589 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001590
1591 return widget;
1592}
1593
1594struct widget *
1595window_add_widget(struct window *window, void *data)
1596{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001597 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001598
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001599 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001600 wl_list_init(&widget->link);
1601 window->main_surface->widget = widget;
1602
1603 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001604}
1605
1606struct widget *
1607widget_add_widget(struct widget *parent, void *data)
1608{
1609 struct widget *widget;
1610
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001611 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001613
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001614 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001615}
1616
1617void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001618widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001619{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001620 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001621 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001622 struct input *input;
1623
Pekka Paalanen35e82632013-04-25 13:57:48 +03001624 /* Destroy the sub-surface along with the root widget */
1625 if (surface->widget == widget && surface->subsurface)
1626 surface_destroy(widget->surface);
1627
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001628 if (widget->tooltip)
1629 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001630
Pekka Paalanene156fb62012-01-19 13:51:38 +02001631 wl_list_for_each(input, &display->input_list, link) {
1632 if (input->focus_widget == widget)
1633 input->focus_widget = NULL;
1634 }
1635
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001636 wl_list_remove(&widget->link);
1637 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001638}
1639
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001640void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001641widget_set_default_cursor(struct widget *widget, int cursor)
1642{
1643 widget->default_cursor = cursor;
1644}
1645
1646void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001647widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001648{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001649 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001650}
1651
1652void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001653widget_set_size(struct widget *widget, int32_t width, int32_t height)
1654{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001655 widget->allocation.width = width;
1656 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001657}
1658
1659void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001660widget_set_allocation(struct widget *widget,
1661 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001662{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001663 widget->allocation.x = x;
1664 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001665 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001666}
1667
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001668void
1669widget_set_transparent(struct widget *widget, int transparent)
1670{
1671 widget->opaque = !transparent;
1672}
1673
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001674void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001675widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001676{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001677 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001678}
1679
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001680static cairo_surface_t *
1681widget_get_cairo_surface(struct widget *widget)
1682{
1683 struct surface *surface = widget->surface;
1684 struct window *window = widget->window;
1685
Neil Roberts97b747c2013-12-19 16:17:12 +00001686 assert(widget->use_cairo);
1687
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001688 if (!surface->cairo_surface) {
1689 if (surface == window->main_surface)
1690 window_create_main_surface(window);
1691 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001692 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001693 }
1694
1695 return surface->cairo_surface;
1696}
1697
Alexander Larsson15901f02013-05-22 14:41:25 +02001698static void
1699widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1700{
1701 struct surface *surface = widget->surface;
1702 double angle;
1703 cairo_matrix_t m;
1704 enum wl_output_transform transform;
1705 int surface_width, surface_height;
1706 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001707 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001708
1709 surface_width = surface->allocation.width;
1710 surface_height = surface->allocation.height;
1711
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001712 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001713 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001714
Alexander Larsson15901f02013-05-22 14:41:25 +02001715 switch (transform) {
1716 case WL_OUTPUT_TRANSFORM_FLIPPED:
1717 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1718 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1719 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1720 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1721 break;
1722 default:
1723 cairo_matrix_init_identity(&m);
1724 break;
1725 }
1726
1727 switch (transform) {
1728 case WL_OUTPUT_TRANSFORM_NORMAL:
1729 default:
1730 angle = 0;
1731 translate_x = 0;
1732 translate_y = 0;
1733 break;
1734 case WL_OUTPUT_TRANSFORM_FLIPPED:
1735 angle = 0;
1736 translate_x = surface_width;
1737 translate_y = 0;
1738 break;
1739 case WL_OUTPUT_TRANSFORM_90:
1740 angle = M_PI_2;
1741 translate_x = surface_height;
1742 translate_y = 0;
1743 break;
1744 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1745 angle = M_PI_2;
1746 translate_x = surface_height;
1747 translate_y = surface_width;
1748 break;
1749 case WL_OUTPUT_TRANSFORM_180:
1750 angle = M_PI;
1751 translate_x = surface_width;
1752 translate_y = surface_height;
1753 break;
1754 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1755 angle = M_PI;
1756 translate_x = 0;
1757 translate_y = surface_height;
1758 break;
1759 case WL_OUTPUT_TRANSFORM_270:
1760 angle = M_PI + M_PI_2;
1761 translate_x = 0;
1762 translate_y = surface_width;
1763 break;
1764 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1765 angle = M_PI + M_PI_2;
1766 translate_x = 0;
1767 translate_y = 0;
1768 break;
1769 }
1770
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001771 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001772 cairo_translate(cr, translate_x, translate_y);
1773 cairo_rotate(cr, angle);
1774 cairo_transform(cr, &m);
1775}
1776
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001777cairo_t *
1778widget_cairo_create(struct widget *widget)
1779{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001780 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001781 cairo_surface_t *cairo_surface;
1782 cairo_t *cr;
1783
1784 cairo_surface = widget_get_cairo_surface(widget);
1785 cr = cairo_create(cairo_surface);
1786
Alexander Larsson15901f02013-05-22 14:41:25 +02001787 widget_cairo_update_transform(widget, cr);
1788
Pekka Paalanen35e82632013-04-25 13:57:48 +03001789 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1790
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001791 return cr;
1792}
1793
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001794struct wl_surface *
1795widget_get_wl_surface(struct widget *widget)
1796{
1797 return widget->surface->surface;
1798}
1799
Neil Roberts5e10a042013-09-09 00:40:17 +01001800struct wl_subsurface *
1801widget_get_wl_subsurface(struct widget *widget)
1802{
1803 return widget->surface->subsurface;
1804}
1805
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001806uint32_t
1807widget_get_last_time(struct widget *widget)
1808{
1809 return widget->surface->last_time;
1810}
1811
1812void
1813widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1814{
1815 struct wl_compositor *comp = widget->window->display->compositor;
1816 struct surface *surface = widget->surface;
1817
1818 if (!surface->input_region)
1819 surface->input_region = wl_compositor_create_region(comp);
1820
1821 if (rect) {
1822 wl_region_add(surface->input_region,
1823 rect->x, rect->y, rect->width, rect->height);
1824 }
1825}
1826
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001827void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001828widget_set_resize_handler(struct widget *widget,
1829 widget_resize_handler_t handler)
1830{
1831 widget->resize_handler = handler;
1832}
1833
1834void
1835widget_set_redraw_handler(struct widget *widget,
1836 widget_redraw_handler_t handler)
1837{
1838 widget->redraw_handler = handler;
1839}
1840
1841void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001842widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001843{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001844 widget->enter_handler = handler;
1845}
1846
1847void
1848widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1849{
1850 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001851}
1852
1853void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001854widget_set_motion_handler(struct widget *widget,
1855 widget_motion_handler_t handler)
1856{
1857 widget->motion_handler = handler;
1858}
1859
1860void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001861widget_set_button_handler(struct widget *widget,
1862 widget_button_handler_t handler)
1863{
1864 widget->button_handler = handler;
1865}
1866
1867void
Rusty Lynch041815a2013-08-08 21:20:38 -07001868widget_set_touch_up_handler(struct widget *widget,
1869 widget_touch_up_handler_t handler)
1870{
1871 widget->touch_up_handler = handler;
1872}
1873
1874void
1875widget_set_touch_down_handler(struct widget *widget,
1876 widget_touch_down_handler_t handler)
1877{
1878 widget->touch_down_handler = handler;
1879}
1880
1881void
1882widget_set_touch_motion_handler(struct widget *widget,
1883 widget_touch_motion_handler_t handler)
1884{
1885 widget->touch_motion_handler = handler;
1886}
1887
1888void
1889widget_set_touch_frame_handler(struct widget *widget,
1890 widget_touch_frame_handler_t handler)
1891{
1892 widget->touch_frame_handler = handler;
1893}
1894
1895void
1896widget_set_touch_cancel_handler(struct widget *widget,
1897 widget_touch_cancel_handler_t handler)
1898{
1899 widget->touch_cancel_handler = handler;
1900}
1901
1902void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001903widget_set_axis_handler(struct widget *widget,
1904 widget_axis_handler_t handler)
1905{
1906 widget->axis_handler = handler;
1907}
1908
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001909static void
1910window_schedule_redraw_task(struct window *window);
1911
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001912void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001913widget_schedule_redraw(struct widget *widget)
1914{
Pekka Paalanen71233882013-04-25 13:57:53 +03001915 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001916 widget->surface->redraw_needed = 1;
1917 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001918}
1919
Neil Roberts97b747c2013-12-19 16:17:12 +00001920void
1921widget_set_use_cairo(struct widget *widget,
1922 int use_cairo)
1923{
1924 widget->use_cairo = use_cairo;
1925}
1926
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001927cairo_surface_t *
1928window_get_surface(struct window *window)
1929{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001930 cairo_surface_t *cairo_surface;
1931
1932 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1933
1934 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001935}
1936
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001937struct wl_surface *
1938window_get_wl_surface(struct window *window)
1939{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001940 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001941}
1942
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001943static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001944tooltip_redraw_handler(struct widget *widget, void *data)
1945{
1946 cairo_t *cr;
1947 const int32_t r = 3;
1948 struct tooltip *tooltip = data;
1949 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001950
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001951 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001952 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001953 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1954 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1955 cairo_paint(cr);
1956
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001957 width = widget->allocation.width;
1958 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001959 rounded_rect(cr, 0, 0, width, height, r);
1960
1961 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1962 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1963 cairo_fill(cr);
1964
1965 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1966 cairo_move_to(cr, 10, 16);
1967 cairo_show_text(cr, tooltip->entry);
1968 cairo_destroy(cr);
1969}
1970
1971static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001972get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001973{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001974 cairo_t *cr;
1975 cairo_text_extents_t extents;
1976
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001977 /* Use the dummy_surface because tooltip's surface was not
1978 * created yet, and parent does not have a valid surface
1979 * outside repaint, either.
1980 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001981 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001982 cairo_text_extents(cr, tooltip->entry, &extents);
1983 cairo_destroy(cr);
1984
1985 return extents;
1986}
1987
1988static int
1989window_create_tooltip(struct tooltip *tooltip)
1990{
1991 struct widget *parent = tooltip->parent;
1992 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001993 const int offset_y = 27;
1994 const int margin = 3;
1995 cairo_text_extents_t extents;
1996
1997 if (tooltip->widget)
1998 return 0;
1999
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002000 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002001
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002002 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002003 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002004 widget_set_allocation(tooltip->widget,
2005 tooltip->x, tooltip->y + offset_y,
2006 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002007
2008 return 0;
2009}
2010
2011void
2012widget_destroy_tooltip(struct widget *parent)
2013{
2014 struct tooltip *tooltip = parent->tooltip;
2015
2016 parent->tooltip_count = 0;
2017 if (!tooltip)
2018 return;
2019
2020 if (tooltip->widget) {
2021 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002022 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002023 }
2024
2025 close(tooltip->tooltip_fd);
2026 free(tooltip->entry);
2027 free(tooltip);
2028 parent->tooltip = NULL;
2029}
2030
2031static void
2032tooltip_func(struct task *task, uint32_t events)
2033{
2034 struct tooltip *tooltip =
2035 container_of(task, struct tooltip, tooltip_task);
2036 uint64_t exp;
2037
Martin Olsson8df662a2012-07-08 03:03:47 +02002038 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2039 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002040 window_create_tooltip(tooltip);
2041}
2042
2043#define TOOLTIP_TIMEOUT 500
2044static int
2045tooltip_timer_reset(struct tooltip *tooltip)
2046{
2047 struct itimerspec its;
2048
2049 its.it_interval.tv_sec = 0;
2050 its.it_interval.tv_nsec = 0;
2051 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2052 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2053 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2054 fprintf(stderr, "could not set timerfd\n: %m");
2055 return -1;
2056 }
2057
2058 return 0;
2059}
2060
2061int
2062widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2063{
2064 struct tooltip *tooltip = parent->tooltip;
2065
2066 parent->tooltip_count++;
2067 if (tooltip) {
2068 tooltip->x = x;
2069 tooltip->y = y;
2070 tooltip_timer_reset(tooltip);
2071 return 0;
2072 }
2073
2074 /* the handler might be triggered too fast via input device motion, so
2075 * we need this check here to make sure tooltip is fully initialized */
2076 if (parent->tooltip_count > 1)
2077 return 0;
2078
2079 tooltip = malloc(sizeof *tooltip);
2080 if (!tooltip)
2081 return -1;
2082
2083 parent->tooltip = tooltip;
2084 tooltip->parent = parent;
2085 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002086 tooltip->x = x;
2087 tooltip->y = y;
2088 tooltip->entry = strdup(entry);
2089 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2090 if (tooltip->tooltip_fd < 0) {
2091 fprintf(stderr, "could not create timerfd\n: %m");
2092 return -1;
2093 }
2094
2095 tooltip->tooltip_task.run = tooltip_func;
2096 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2097 EPOLLIN, &tooltip->tooltip_task);
2098 tooltip_timer_reset(tooltip);
2099
2100 return 0;
2101}
2102
2103static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002104workspace_manager_state(void *data,
2105 struct workspace_manager *workspace_manager,
2106 uint32_t current,
2107 uint32_t count)
2108{
2109 struct display *display = data;
2110
2111 display->workspace = current;
2112 display->workspace_count = count;
2113}
2114
2115static const struct workspace_manager_listener workspace_manager_listener = {
2116 workspace_manager_state
2117};
2118
2119static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002120frame_resize_handler(struct widget *widget,
2121 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002122{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002123 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002124 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002125 struct rectangle interior;
2126 struct rectangle input;
2127 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002128
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002129 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002130 interior.x = 0;
2131 interior.y = 0;
2132 interior.width = width;
2133 interior.height = height;
2134 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002135 frame_resize(frame->frame, width, height);
2136 frame_interior(frame->frame, &interior.x, &interior.y,
2137 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002138 }
2139
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002140 widget_set_allocation(child, interior.x, interior.y,
2141 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002142
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002143 if (child->resize_handler) {
2144 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002145 child->user_data);
2146
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002147 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002148 width = child->allocation.width;
2149 height = child->allocation.height;
2150 } else {
2151 frame_resize_inside(frame->frame,
2152 child->allocation.width,
2153 child->allocation.height);
2154 width = frame_width(frame->frame);
2155 height = frame_height(frame->frame);
2156 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002157 }
2158
Scott Moreauf7e498c2012-05-14 11:39:29 -06002159 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002160
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002161 widget->surface->input_region =
2162 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002163 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002164 frame_input_rect(frame->frame, &input.x, &input.y,
2165 &input.width, &input.height);
2166 wl_region_add(widget->surface->input_region,
2167 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002168 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002170 }
2171
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002172 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002173
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002174 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002175 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002176 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2177 &opaque.width, &opaque.height);
2178
2179 wl_region_add(widget->surface->opaque_region,
2180 opaque.x, opaque.y,
2181 opaque.width, opaque.height);
2182 } else {
2183 wl_region_add(widget->surface->opaque_region,
2184 0, 0, width, height);
2185 }
Martin Minarik1998b152012-05-10 02:04:35 +02002186 }
2187
Martin Minarik1998b152012-05-10 02:04:35 +02002188
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002189 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002190}
2191
2192static void
2193frame_redraw_handler(struct widget *widget, void *data)
2194{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002195 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002196 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002197 struct window *window = widget->window;
2198
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002199 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002200 return;
2201
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002202 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002203
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002204 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002205
2206 cairo_destroy(cr);
2207}
2208
2209static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002210frame_get_pointer_image_for_location(struct window_frame *frame,
2211 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002212{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002213 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002214
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002215 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002216 return CURSOR_LEFT_PTR;
2217
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002218 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002219 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002220 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002221 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002222 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002223 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002224 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002225 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002226 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002227 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002228 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002229 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002230 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002231 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002232 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002233 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002234 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002235 case THEME_LOCATION_EXTERIOR:
2236 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002237 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002238 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002239 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002240}
2241
Pekka Paalanen26237862014-09-10 15:10:30 +03002242static void
2243frame_menu_func(void *data, struct input *input, int index)
2244{
2245 struct window *window = data;
2246 struct display *display;
2247
2248 switch (index) {
2249 case 0: /* close */
2250 window_close(window);
2251 break;
2252 case 1: /* move to workspace above */
2253 display = window->display;
2254 if (display->workspace > 0)
2255 workspace_manager_move_surface(
2256 display->workspace_manager,
2257 window->main_surface->surface,
2258 display->workspace - 1);
2259 break;
2260 case 2: /* move to workspace below */
2261 display = window->display;
2262 if (display->workspace < display->workspace_count - 1)
2263 workspace_manager_move_surface(
2264 display->workspace_manager,
2265 window->main_surface->surface,
2266 display->workspace + 1);
2267 break;
2268 case 3: /* fullscreen */
2269 /* we don't have a way to get out of fullscreen for now */
2270 if (window->fullscreen_handler)
2271 window->fullscreen_handler(window, window->user_data);
2272 break;
2273 }
2274}
2275
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002276void
2277window_show_frame_menu(struct window *window,
2278 struct input *input, uint32_t time)
2279{
2280 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002281 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002282
Pekka Paalanen26237862014-09-10 15:10:30 +03002283 static const char *entries[] = {
2284 "Close",
2285 "Move to workspace above", "Move to workspace below",
2286 "Fullscreen"
2287 };
2288
2289 if (window->fullscreen_handler)
2290 count = ARRAY_LENGTH(entries);
2291 else
2292 count = ARRAY_LENGTH(entries) - 1;
2293
2294 input_get_position(input, &x, &y);
2295 window_show_menu(window->display, input, time, window,
2296 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002297}
2298
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002299static int
2300frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002301 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002302{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002303 struct window_frame *frame = data;
2304 enum theme_location location;
2305
2306 location = frame_pointer_enter(frame->frame, input, x, y);
2307 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2308 widget_schedule_redraw(frame->widget);
2309
2310 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002311}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002312
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002313static int
2314frame_motion_handler(struct widget *widget,
2315 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002316 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002317{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002318 struct window_frame *frame = data;
2319 enum theme_location location;
2320
2321 location = frame_pointer_motion(frame->frame, input, x, y);
2322 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2323 widget_schedule_redraw(frame->widget);
2324
2325 return frame_get_pointer_image_for_location(data, location);
2326}
2327
2328static void
2329frame_leave_handler(struct widget *widget,
2330 struct input *input, void *data)
2331{
2332 struct window_frame *frame = data;
2333
2334 frame_pointer_leave(frame->frame, input);
2335 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2336 widget_schedule_redraw(frame->widget);
2337}
2338
2339static void
2340frame_handle_status(struct window_frame *frame, struct input *input,
2341 uint32_t time, enum theme_location location)
2342{
2343 struct window *window = frame->widget->window;
2344 uint32_t status;
2345
2346 status = frame_status(frame->frame);
2347 if (status & FRAME_STATUS_REPAINT)
2348 widget_schedule_redraw(frame->widget);
2349
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002350 if (status & FRAME_STATUS_MINIMIZE) {
2351 window_set_minimized(window);
2352 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2353 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002354
2355 if (status & FRAME_STATUS_MENU) {
2356 window_show_frame_menu(window, input, time);
2357 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2358 }
2359
2360 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002361 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002362 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2363 }
2364
2365 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002366 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002367 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002368 }
2369
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002370 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002371 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002372 xdg_surface_move(window->xdg_surface,
2373 input_get_seat(input),
2374 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002375
2376 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2377 }
2378
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002379 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002380 input_ungrab(input);
2381
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002382 xdg_surface_resize(window->xdg_surface,
2383 input_get_seat(input),
2384 window->display->serial,
2385 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002386
2387 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2388 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002389}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002390
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002391#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002392static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002393frame_button_handler(struct widget *widget,
2394 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002395 uint32_t button, enum wl_pointer_button_state state,
2396 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002397
2398{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002399 struct window_frame *frame = data;
2400 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002401
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002402 frame->double_click = 0;
2403 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2404 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2405 frame->double_click = 1;
2406 frame->did_double = 1;
2407 } else
2408 frame->did_double = 0;
2409
2410 frame->last_time = time;
2411 } else if (frame->did_double == 1) {
2412 frame->double_click = 1;
2413 frame->did_double = 0;
2414 }
2415
2416 if (frame->double_click)
2417 location = frame_double_click(frame->frame, input,
2418 button, state);
2419 else
2420 location = frame_pointer_button(frame->frame, input,
2421 button, state);
2422
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002423 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002424}
2425
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002426static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002427frame_touch_down_handler(struct widget *widget, struct input *input,
2428 uint32_t serial, uint32_t time, int32_t id,
2429 float x, float y, void *data)
2430{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002431 struct window_frame *frame = data;
2432
Xiong Zhang382de462014-06-12 11:06:26 +08002433 frame->double_click = 0;
2434 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD &&
2435 frame->last_id == id) {
2436 frame->double_click = 1;
2437 frame->did_double = 1;
2438 frame->double_id = id;
2439 } else
2440 frame->did_double = 0;
2441
2442 frame->last_time = time;
2443 frame->last_id = id;
2444
2445 if (frame->double_click)
2446 frame_double_touch_down(frame->frame, input, id, x, y);
2447 else
2448 frame_touch_down(frame->frame, input, id, x, y);
2449
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002450 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2451}
2452
2453static void
2454frame_touch_up_handler(struct widget *widget,
2455 struct input *input, uint32_t serial, uint32_t time,
2456 int32_t id, void *data)
2457{
2458 struct window_frame *frame = data;
2459
Xiong Zhang382de462014-06-12 11:06:26 +08002460 if (frame->double_id == id && frame->did_double) {
2461 frame->did_double = 0;
2462 frame->double_id = 0;
2463 frame_double_touch_up(frame->frame, input, id);
2464 } else
2465 frame_touch_up(frame->frame, input, id);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002466 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002467}
2468
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002469struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002470window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002471{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002472 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002473 uint32_t buttons;
2474
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002475 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002476 buttons = FRAME_BUTTON_NONE;
2477 } else {
2478 buttons = FRAME_BUTTON_ALL;
2479 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002480
Peter Huttererf3d62272013-08-08 11:57:05 +10002481 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002482 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002483 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002484
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002485 frame->widget = window_add_widget(window, frame);
2486 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002487
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002488 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2489 widget_set_resize_handler(frame->widget, frame_resize_handler);
2490 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002491 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002492 widget_set_motion_handler(frame->widget, frame_motion_handler);
2493 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002494 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002495 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002496
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002497 window->frame = frame;
2498
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002499 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002500}
2501
Kristian Høgsberga1627922012-06-20 17:30:03 -04002502void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002503window_frame_set_child_size(struct widget *widget, int child_width,
2504 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002505{
2506 struct display *display = widget->window->display;
2507 struct theme *t = display->theme;
2508 int decoration_width, decoration_height;
2509 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002510 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002511
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002512 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002513 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002514 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002515 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002516
2517 width = child_width + decoration_width;
2518 height = child_height + decoration_height;
2519 } else {
2520 width = child_width;
2521 height = child_height;
2522 }
2523
2524 window_schedule_resize(widget->window, width, height);
2525}
2526
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002527static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002528window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002529{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002530 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002531
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002532 /* frame->child must be destroyed by the application */
2533 widget_destroy(frame->widget);
2534 free(frame);
2535}
2536
2537static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002538input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002539 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002540{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002541 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002542 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002543
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002544 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002545 return;
2546
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002547 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002548 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002549 widget = old;
2550 if (input->grab)
2551 widget = input->grab;
2552 if (widget->leave_handler)
2553 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002554 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002555 }
2556
2557 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002558 widget = focus;
2559 if (input->grab)
2560 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002561 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002562 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002563 cursor = widget->enter_handler(focus, input, x, y,
2564 widget->user_data);
2565 else
2566 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002567
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002568 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002569 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002570}
2571
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002572void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002573touch_grab(struct input *input, int32_t touch_id)
2574{
2575 input->touch_grab = 1;
2576 input->touch_grab_id = touch_id;
2577}
2578
2579void
2580touch_ungrab(struct input *input)
2581{
2582 struct touch_point *tp, *tmp;
2583
2584 input->touch_grab = 0;
2585
2586 wl_list_for_each_safe(tp, tmp,
2587 &input->touch_point_list, link) {
2588 if (tp->id != input->touch_grab_id)
2589 continue;
2590 wl_list_remove(&tp->link);
2591 free(tp);
2592
2593 return;
2594 }
2595}
2596
2597void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002598input_grab(struct input *input, struct widget *widget, uint32_t button)
2599{
2600 input->grab = widget;
2601 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002602
2603 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002604}
2605
2606void
2607input_ungrab(struct input *input)
2608{
2609 struct widget *widget;
2610
2611 input->grab = NULL;
2612 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002613 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002614 input->sx, input->sy);
2615 input_set_focus_widget(input, widget, input->sx, input->sy);
2616 }
2617}
2618
2619static void
2620input_remove_pointer_focus(struct input *input)
2621{
2622 struct window *window = input->pointer_focus;
2623
2624 if (!window)
2625 return;
2626
2627 input_set_focus_widget(input, NULL, 0, 0);
2628
2629 input->pointer_focus = NULL;
2630 input->current_cursor = CURSOR_UNSET;
2631}
2632
2633static void
2634pointer_handle_enter(void *data, struct wl_pointer *pointer,
2635 uint32_t serial, struct wl_surface *surface,
2636 wl_fixed_t sx_w, wl_fixed_t sy_w)
2637{
2638 struct input *input = data;
2639 struct window *window;
2640 struct widget *widget;
2641 float sx = wl_fixed_to_double(sx_w);
2642 float sy = wl_fixed_to_double(sy_w);
2643
2644 if (!surface) {
2645 /* enter event for a window we've just destroyed */
2646 return;
2647 }
2648
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002649 window = wl_surface_get_user_data(surface);
2650 if (surface != window->main_surface->surface) {
2651 DBG("Ignoring input event from subsurface %p\n", surface);
2652 return;
2653 }
2654
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002655 input->display->serial = serial;
2656 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002657 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002658
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002659 input->sx = sx;
2660 input->sy = sy;
2661
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002662 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002663 input_set_focus_widget(input, widget, sx, sy);
2664}
2665
2666static void
2667pointer_handle_leave(void *data, struct wl_pointer *pointer,
2668 uint32_t serial, struct wl_surface *surface)
2669{
2670 struct input *input = data;
2671
2672 input->display->serial = serial;
2673 input_remove_pointer_focus(input);
2674}
2675
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002676static void
Daniel Stone37816df2012-05-16 18:45:18 +01002677pointer_handle_motion(void *data, struct wl_pointer *pointer,
2678 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002679{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002680 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002681 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002682 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002683 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002684 float sx = wl_fixed_to_double(sx_w);
2685 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002686
Paul Winwoodb22bf572013-08-29 10:52:54 +01002687 if (!window)
2688 return;
2689
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002690 input->sx = sx;
2691 input->sy = sy;
2692
Rob Bradford5f087742013-07-11 19:41:27 +01002693 /* when making the window smaller - e.g. after a unmaximise we might
2694 * still have a pending motion event that the compositor has picked
2695 * based on the old surface dimensions
2696 */
2697 if (sx > window->main_surface->allocation.width ||
2698 sy > window->main_surface->allocation.height)
2699 return;
2700
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002701 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002702 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002703 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002704 }
2705
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002706 if (input->grab)
2707 widget = input->grab;
2708 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002709 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002710 if (widget) {
2711 if (widget->motion_handler)
2712 cursor = widget->motion_handler(input->focus_widget,
2713 input, time, sx, sy,
2714 widget->user_data);
2715 else
2716 cursor = widget->default_cursor;
2717 } else
2718 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002719
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002720 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002721}
2722
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002723static void
Daniel Stone37816df2012-05-16 18:45:18 +01002724pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002725 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002726{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002727 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002728 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002729 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002730
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002731 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002732 if (input->focus_widget && input->grab == NULL &&
2733 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002734 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002735
Neil Roberts6b28aad2012-01-23 19:11:18 +00002736 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002737 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002738 (*widget->button_handler)(widget,
2739 input, time,
2740 button, state,
2741 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002742
Daniel Stone4dbadb12012-05-30 16:31:51 +01002743 if (input->grab && input->grab_button == button &&
2744 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002745 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002746}
2747
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002748static void
Daniel Stone37816df2012-05-16 18:45:18 +01002749pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002750 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002751{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002752 struct input *input = data;
2753 struct widget *widget;
2754
2755 widget = input->focus_widget;
2756 if (input->grab)
2757 widget = input->grab;
2758 if (widget && widget->axis_handler)
2759 (*widget->axis_handler)(widget,
2760 input, time,
2761 axis, value,
2762 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002763}
2764
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002765static const struct wl_pointer_listener pointer_listener = {
2766 pointer_handle_enter,
2767 pointer_handle_leave,
2768 pointer_handle_motion,
2769 pointer_handle_button,
2770 pointer_handle_axis,
2771};
2772
2773static void
2774input_remove_keyboard_focus(struct input *input)
2775{
2776 struct window *window = input->keyboard_focus;
2777 struct itimerspec its;
2778
2779 its.it_interval.tv_sec = 0;
2780 its.it_interval.tv_nsec = 0;
2781 its.it_value.tv_sec = 0;
2782 its.it_value.tv_nsec = 0;
2783 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2784
2785 if (!window)
2786 return;
2787
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002788 if (window->keyboard_focus_handler)
2789 (*window->keyboard_focus_handler)(window, NULL,
2790 window->user_data);
2791
2792 input->keyboard_focus = NULL;
2793}
2794
2795static void
2796keyboard_repeat_func(struct task *task, uint32_t events)
2797{
2798 struct input *input =
2799 container_of(task, struct input, repeat_task);
2800 struct window *window = input->keyboard_focus;
2801 uint64_t exp;
2802
2803 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2804 /* If we change the timer between the fd becoming
2805 * readable and getting here, there'll be nothing to
2806 * read and we get EAGAIN. */
2807 return;
2808
2809 if (window && window->key_handler) {
2810 (*window->key_handler)(window, input, input->repeat_time,
2811 input->repeat_key, input->repeat_sym,
2812 WL_KEYBOARD_KEY_STATE_PRESSED,
2813 window->user_data);
2814 }
2815}
2816
2817static void
2818keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2819 uint32_t format, int fd, uint32_t size)
2820{
2821 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002822 struct xkb_keymap *keymap;
2823 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002824 char *map_str;
2825
2826 if (!data) {
2827 close(fd);
2828 return;
2829 }
2830
2831 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2832 close(fd);
2833 return;
2834 }
2835
2836 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2837 if (map_str == MAP_FAILED) {
2838 close(fd);
2839 return;
2840 }
2841
Ran Benita2e1968f2014-08-19 23:59:51 +03002842 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2843 map_str,
2844 XKB_KEYMAP_FORMAT_TEXT_V1,
2845 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002846 munmap(map_str, size);
2847 close(fd);
2848
Rui Matos3eccb862013-10-10 19:44:22 +02002849 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002850 fprintf(stderr, "failed to compile keymap\n");
2851 return;
2852 }
2853
Rui Matos3eccb862013-10-10 19:44:22 +02002854 state = xkb_state_new(keymap);
2855 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002856 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002857 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002858 return;
2859 }
2860
Rui Matos3eccb862013-10-10 19:44:22 +02002861 xkb_keymap_unref(input->xkb.keymap);
2862 xkb_state_unref(input->xkb.state);
2863 input->xkb.keymap = keymap;
2864 input->xkb.state = state;
2865
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002866 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002867 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002868 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002869 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002870 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002871 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002872}
2873
2874static void
2875keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2876 uint32_t serial, struct wl_surface *surface,
2877 struct wl_array *keys)
2878{
2879 struct input *input = data;
2880 struct window *window;
2881
2882 input->display->serial = serial;
2883 input->keyboard_focus = wl_surface_get_user_data(surface);
2884
2885 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002886 if (window->keyboard_focus_handler)
2887 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002888 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002889}
2890
2891static void
2892keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2893 uint32_t serial, struct wl_surface *surface)
2894{
2895 struct input *input = data;
2896
2897 input->display->serial = serial;
2898 input_remove_keyboard_focus(input);
2899}
2900
Scott Moreau210d0792012-03-22 10:47:01 -06002901static void
Daniel Stone37816df2012-05-16 18:45:18 +01002902keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002903 uint32_t serial, uint32_t time, uint32_t key,
2904 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002905{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002906 struct input *input = data;
2907 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002908 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002909 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002910 const xkb_keysym_t *syms;
2911 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002912 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002913
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002914 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002915 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002916 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002917 return;
2918
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002919 /* We only use input grabs for pointer events for now, so just
2920 * ignore key presses if a grab is active. We expand the key
2921 * event delivery mechanism to route events to widgets to
2922 * properly handle key grabs. In the meantime, this prevents
2923 * key event devlivery while a grab is active. */
2924 if (input->grab && input->grab_button == 0)
2925 return;
2926
Ran Benita2e1968f2014-08-19 23:59:51 +03002927 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002928
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002929 sym = XKB_KEY_NoSymbol;
2930 if (num_syms == 1)
2931 sym = syms[0];
2932
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002933
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002934 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002935 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002936 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002937 } else if (sym == XKB_KEY_F11 &&
2938 window->fullscreen_handler &&
2939 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2940 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002941 } else if (sym == XKB_KEY_F4 &&
2942 input->modifiers == MOD_ALT_MASK &&
2943 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002944 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002945 } else if (window->key_handler) {
2946 (*window->key_handler)(window, input, time, key,
2947 sym, state, window->user_data);
2948 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002949
2950 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2951 key == input->repeat_key) {
2952 its.it_interval.tv_sec = 0;
2953 its.it_interval.tv_nsec = 0;
2954 its.it_value.tv_sec = 0;
2955 its.it_value.tv_nsec = 0;
2956 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002957 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2958 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002959 input->repeat_sym = sym;
2960 input->repeat_key = key;
2961 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002962 its.it_interval.tv_sec = input->repeat_rate_sec;
2963 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2964 its.it_value.tv_sec = input->repeat_delay_sec;
2965 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002966 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2967 }
2968}
2969
2970static void
Daniel Stone351eb612012-05-31 15:27:47 -04002971keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2972 uint32_t serial, uint32_t mods_depressed,
2973 uint32_t mods_latched, uint32_t mods_locked,
2974 uint32_t group)
2975{
2976 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002977 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002978
Matt Ropere61561f2013-06-24 16:52:43 +01002979 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2980 if (!input->xkb.keymap)
2981 return;
2982
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002983 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2984 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002985 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002986 XKB_STATE_MODS_DEPRESSED |
2987 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002988 input->modifiers = 0;
2989 if (mask & input->xkb.control_mask)
2990 input->modifiers |= MOD_CONTROL_MASK;
2991 if (mask & input->xkb.alt_mask)
2992 input->modifiers |= MOD_ALT_MASK;
2993 if (mask & input->xkb.shift_mask)
2994 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002995}
2996
Jonny Lamb06959082014-08-12 14:58:27 +02002997static void
2998set_repeat_info(struct input *input, int32_t rate, int32_t delay)
2999{
3000 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3001 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3002
3003 /* a rate of zero disables any repeating, regardless of the delay's
3004 * value */
3005 if (rate == 0)
3006 return;
3007
3008 if (rate == 1)
3009 input->repeat_rate_sec = 1;
3010 else
3011 input->repeat_rate_nsec = 1000000000 / rate;
3012
3013 input->repeat_delay_sec = delay / 1000;
3014 delay -= (input->repeat_delay_sec * 1000);
3015 input->repeat_delay_nsec = delay * 1000 * 1000;
3016}
3017
3018static void
3019keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3020 int32_t rate, int32_t delay)
3021{
3022 struct input *input = data;
3023
3024 set_repeat_info(input, rate, delay);
3025}
3026
Daniel Stone37816df2012-05-16 18:45:18 +01003027static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003028 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003029 keyboard_handle_enter,
3030 keyboard_handle_leave,
3031 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003032 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003033 keyboard_handle_repeat_info
3034
Daniel Stone37816df2012-05-16 18:45:18 +01003035};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003036
3037static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003038touch_handle_down(void *data, struct wl_touch *wl_touch,
3039 uint32_t serial, uint32_t time, struct wl_surface *surface,
3040 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3041{
3042 struct input *input = data;
3043 struct widget *widget;
3044 float sx = wl_fixed_to_double(x_w);
3045 float sy = wl_fixed_to_double(y_w);
3046
Rusty Lynch1084da52013-08-15 09:10:08 -07003047 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003048 input->touch_focus = wl_surface_get_user_data(surface);
3049 if (!input->touch_focus) {
3050 DBG("Failed to find to touch focus for surface %p\n", surface);
3051 return;
3052 }
3053
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003054 if (surface != input->touch_focus->main_surface->surface) {
3055 DBG("Ignoring input event from subsurface %p\n", surface);
3056 input->touch_focus = NULL;
3057 return;
3058 }
3059
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003060 if (input->grab)
3061 widget = input->grab;
3062 else
3063 widget = window_find_widget(input->touch_focus,
3064 wl_fixed_to_double(x_w),
3065 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003066 if (widget) {
3067 struct touch_point *tp = xmalloc(sizeof *tp);
3068 if (tp) {
3069 tp->id = id;
3070 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003071 tp->x = sx;
3072 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003073 wl_list_insert(&input->touch_point_list, &tp->link);
3074
3075 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003076 (*widget->touch_down_handler)(widget, input,
3077 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003078 sx, sy,
3079 widget->user_data);
3080 }
3081 }
3082}
3083
3084static void
3085touch_handle_up(void *data, struct wl_touch *wl_touch,
3086 uint32_t serial, uint32_t time, int32_t id)
3087{
3088 struct input *input = data;
3089 struct touch_point *tp, *tmp;
3090
Rusty Lynch041815a2013-08-08 21:20:38 -07003091 if (!input->touch_focus) {
3092 DBG("No touch focus found for touch up event!\n");
3093 return;
3094 }
3095
3096 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3097 if (tp->id != id)
3098 continue;
3099
3100 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003101 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003102 time, id,
3103 tp->widget->user_data);
3104
3105 wl_list_remove(&tp->link);
3106 free(tp);
3107
3108 return;
3109 }
3110}
3111
3112static void
3113touch_handle_motion(void *data, struct wl_touch *wl_touch,
3114 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3115{
3116 struct input *input = data;
3117 struct touch_point *tp;
3118 float sx = wl_fixed_to_double(x_w);
3119 float sy = wl_fixed_to_double(y_w);
3120
3121 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3122
3123 if (!input->touch_focus) {
3124 DBG("No touch focus found for touch motion event!\n");
3125 return;
3126 }
3127
3128 wl_list_for_each(tp, &input->touch_point_list, link) {
3129 if (tp->id != id)
3130 continue;
3131
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003132 tp->x = sx;
3133 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003134 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003135 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003136 id, sx, sy,
3137 tp->widget->user_data);
3138 return;
3139 }
3140}
3141
3142static void
3143touch_handle_frame(void *data, struct wl_touch *wl_touch)
3144{
3145 struct input *input = data;
3146 struct touch_point *tp, *tmp;
3147
3148 DBG("touch_handle_frame\n");
3149
3150 if (!input->touch_focus) {
3151 DBG("No touch focus found for touch frame event!\n");
3152 return;
3153 }
3154
3155 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3156 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003157 (*tp->widget->touch_frame_handler)(tp->widget, input,
3158 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003159 }
3160}
3161
3162static void
3163touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3164{
3165 struct input *input = data;
3166 struct touch_point *tp, *tmp;
3167
3168 DBG("touch_handle_cancel\n");
3169
3170 if (!input->touch_focus) {
3171 DBG("No touch focus found for touch cancel event!\n");
3172 return;
3173 }
3174
3175 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3176 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003177 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3178 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003179
3180 wl_list_remove(&tp->link);
3181 free(tp);
3182 }
3183}
3184
3185static const struct wl_touch_listener touch_listener = {
3186 touch_handle_down,
3187 touch_handle_up,
3188 touch_handle_motion,
3189 touch_handle_frame,
3190 touch_handle_cancel,
3191};
3192
3193static void
Daniel Stone37816df2012-05-16 18:45:18 +01003194seat_handle_capabilities(void *data, struct wl_seat *seat,
3195 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003196{
Daniel Stone37816df2012-05-16 18:45:18 +01003197 struct input *input = data;
3198
3199 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3200 input->pointer = wl_seat_get_pointer(seat);
3201 wl_pointer_set_user_data(input->pointer, input);
3202 wl_pointer_add_listener(input->pointer, &pointer_listener,
3203 input);
3204 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3205 wl_pointer_destroy(input->pointer);
3206 input->pointer = NULL;
3207 }
3208
3209 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3210 input->keyboard = wl_seat_get_keyboard(seat);
3211 wl_keyboard_set_user_data(input->keyboard, input);
3212 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3213 input);
3214 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3215 wl_keyboard_destroy(input->keyboard);
3216 input->keyboard = NULL;
3217 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003218
3219 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3220 input->touch = wl_seat_get_touch(seat);
3221 wl_touch_set_user_data(input->touch, input);
3222 wl_touch_add_listener(input->touch, &touch_listener, input);
3223 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3224 wl_touch_destroy(input->touch);
3225 input->touch = NULL;
3226 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003227}
3228
Rob Bradford08031182013-08-13 20:11:03 +01003229static void
3230seat_handle_name(void *data, struct wl_seat *seat,
3231 const char *name)
3232{
3233
3234}
3235
Daniel Stone37816df2012-05-16 18:45:18 +01003236static const struct wl_seat_listener seat_listener = {
3237 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003238 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003239};
3240
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003241void
3242input_get_position(struct input *input, int32_t *x, int32_t *y)
3243{
3244 *x = input->sx;
3245 *y = input->sy;
3246}
3247
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003248int
3249input_get_touch(struct input *input, int32_t id, float *x, float *y)
3250{
3251 struct touch_point *tp;
3252
3253 wl_list_for_each(tp, &input->touch_point_list, link) {
3254 if (tp->id != id)
3255 continue;
3256
3257 *x = tp->x;
3258 *y = tp->y;
3259 return 0;
3260 }
3261
3262 return -1;
3263}
3264
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003265struct display *
3266input_get_display(struct input *input)
3267{
3268 return input->display;
3269}
3270
Daniel Stone37816df2012-05-16 18:45:18 +01003271struct wl_seat *
3272input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003273{
Daniel Stone37816df2012-05-16 18:45:18 +01003274 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003275}
3276
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003277uint32_t
3278input_get_modifiers(struct input *input)
3279{
3280 return input->modifiers;
3281}
3282
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003283struct widget *
3284input_get_focus_widget(struct input *input)
3285{
3286 return input->focus_widget;
3287}
3288
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003289struct data_offer {
3290 struct wl_data_offer *offer;
3291 struct input *input;
3292 struct wl_array types;
3293 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003294
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003295 struct task io_task;
3296 int fd;
3297 data_func_t func;
3298 int32_t x, y;
3299 void *user_data;
3300};
3301
3302static void
3303data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3304{
3305 struct data_offer *offer = data;
3306 char **p;
3307
3308 p = wl_array_add(&offer->types, sizeof *p);
3309 *p = strdup(type);
3310}
3311
3312static const struct wl_data_offer_listener data_offer_listener = {
3313 data_offer_offer,
3314};
3315
3316static void
3317data_offer_destroy(struct data_offer *offer)
3318{
3319 char **p;
3320
3321 offer->refcount--;
3322 if (offer->refcount == 0) {
3323 wl_data_offer_destroy(offer->offer);
3324 for (p = offer->types.data; *p; p++)
3325 free(*p);
3326 wl_array_release(&offer->types);
3327 free(offer);
3328 }
3329}
3330
3331static void
3332data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003333 struct wl_data_device *data_device,
3334 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003335{
3336 struct data_offer *offer;
3337
Brian Lovinbc919262013-08-07 15:34:59 -07003338 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003339
3340 wl_array_init(&offer->types);
3341 offer->refcount = 1;
3342 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003343 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003344 wl_data_offer_add_listener(offer->offer,
3345 &data_offer_listener, offer);
3346}
3347
3348static void
3349data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003350 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003351 wl_fixed_t x_w, wl_fixed_t y_w,
3352 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003353{
3354 struct input *input = data;
3355 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003356 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003357 float x = wl_fixed_to_double(x_w);
3358 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003359 char **p;
3360
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003361 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003362 input->drag_enter_serial = serial;
3363 input->drag_focus = window,
3364 input->drag_x = x;
3365 input->drag_y = y;
3366
3367 if (!input->touch_grab)
3368 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003369
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003370 if (offer) {
3371 input->drag_offer = wl_data_offer_get_user_data(offer);
3372
3373 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3374 *p = NULL;
3375
3376 types_data = input->drag_offer->types.data;
3377 } else {
3378 input->drag_offer = NULL;
3379 types_data = NULL;
3380 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003381
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003382 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003383 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003384 window->user_data);
3385}
3386
3387static void
3388data_device_leave(void *data, struct wl_data_device *data_device)
3389{
3390 struct input *input = data;
3391
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003392 if (input->drag_offer) {
3393 data_offer_destroy(input->drag_offer);
3394 input->drag_offer = NULL;
3395 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003396}
3397
3398static void
3399data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003400 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003401{
3402 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003403 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003404 float x = wl_fixed_to_double(x_w);
3405 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003406 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003407
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003408 input->drag_x = x;
3409 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003410
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003411 if (input->drag_offer)
3412 types_data = input->drag_offer->types.data;
3413 else
3414 types_data = NULL;
3415
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003416 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003417 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003418 window->user_data);
3419}
3420
3421static void
3422data_device_drop(void *data, struct wl_data_device *data_device)
3423{
3424 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003425 struct window *window = input->drag_focus;
3426 float x, y;
3427
3428 x = input->drag_x;
3429 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003430
3431 if (window->drop_handler)
3432 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003433 x, y, window->user_data);
3434
3435 if (input->touch_grab)
3436 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003437}
3438
3439static void
3440data_device_selection(void *data,
3441 struct wl_data_device *wl_data_device,
3442 struct wl_data_offer *offer)
3443{
3444 struct input *input = data;
3445 char **p;
3446
3447 if (input->selection_offer)
3448 data_offer_destroy(input->selection_offer);
3449
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003450 if (offer) {
3451 input->selection_offer = wl_data_offer_get_user_data(offer);
3452 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3453 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003454 } else {
3455 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003456 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003457}
3458
3459static const struct wl_data_device_listener data_device_listener = {
3460 data_device_data_offer,
3461 data_device_enter,
3462 data_device_leave,
3463 data_device_motion,
3464 data_device_drop,
3465 data_device_selection
3466};
3467
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003468static void
3469input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003470{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003471 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003472 struct wl_cursor *cursor;
3473 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003474
Daniel Stone80972742012-11-07 17:51:39 +11003475 if (!input->pointer)
3476 return;
3477
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003478 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003479 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003480 return;
3481
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003482 if (index >= (int) cursor->image_count) {
3483 fprintf(stderr, "cursor index out of range\n");
3484 return;
3485 }
3486
3487 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003488 buffer = wl_cursor_image_get_buffer(image);
3489 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003490 return;
3491
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003492 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3493 wl_surface_damage(input->pointer_surface, 0, 0,
3494 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003495 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003496 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3497 input->pointer_surface,
3498 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003499}
3500
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003501static const struct wl_callback_listener pointer_surface_listener;
3502
3503static void
3504pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3505 uint32_t time)
3506{
3507 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003508 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003509 int i;
3510
3511 if (callback) {
3512 assert(callback == input->cursor_frame_cb);
3513 wl_callback_destroy(callback);
3514 input->cursor_frame_cb = NULL;
3515 }
3516
Daniel Stone80972742012-11-07 17:51:39 +11003517 if (!input->pointer)
3518 return;
3519
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003520 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003521 wl_pointer_set_cursor(input->pointer,
3522 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003523 NULL, 0, 0);
3524 return;
3525 }
3526
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003527 if (input->current_cursor == CURSOR_UNSET)
3528 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003529 cursor = input->display->cursors[input->current_cursor];
3530 if (!cursor)
3531 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003532
3533 /* FIXME We don't have the current time on the first call so we set
3534 * the animation start to the time of the first frame callback. */
3535 if (time == 0)
3536 input->cursor_anim_start = 0;
3537 else if (input->cursor_anim_start == 0)
3538 input->cursor_anim_start = time;
3539
3540 if (time == 0 || input->cursor_anim_start == 0)
3541 i = 0;
3542 else
3543 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3544
Pekka Paalanenbc106382012-10-10 12:49:31 +03003545 if (cursor->image_count > 1) {
3546 input->cursor_frame_cb =
3547 wl_surface_frame(input->pointer_surface);
3548 wl_callback_add_listener(input->cursor_frame_cb,
3549 &pointer_surface_listener, input);
3550 }
3551
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003552 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003553}
3554
3555static const struct wl_callback_listener pointer_surface_listener = {
3556 pointer_surface_frame_callback
3557};
3558
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003559void
3560input_set_pointer_image(struct input *input, int pointer)
3561{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003562 int force = 0;
3563
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003564 if (!input->pointer)
3565 return;
3566
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003567 if (input->pointer_enter_serial > input->cursor_serial)
3568 force = 1;
3569
3570 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003571 return;
3572
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003573 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003574 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003575 if (!input->cursor_frame_cb)
3576 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003577 else if (force) {
3578 /* The current frame callback may be stuck if, for instance,
3579 * the set cursor request was processed by the server after
3580 * this client lost the focus. In this case the cursor surface
3581 * might not be mapped and the frame callback wouldn't ever
3582 * complete. Send a set_cursor and attach to try to map the
3583 * cursor surface again so that the callback will finish */
3584 input_set_pointer_image_index(input, 0);
3585 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003586}
3587
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003588struct wl_data_device *
3589input_get_data_device(struct input *input)
3590{
3591 return input->data_device;
3592}
3593
3594void
3595input_set_selection(struct input *input,
3596 struct wl_data_source *source, uint32_t time)
3597{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003598 if (input->data_device)
3599 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003600}
3601
3602void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003603input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003604{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003605 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003606 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003607}
3608
3609static void
3610offer_io_func(struct task *task, uint32_t events)
3611{
3612 struct data_offer *offer =
3613 container_of(task, struct data_offer, io_task);
3614 unsigned int len;
3615 char buffer[4096];
3616
3617 len = read(offer->fd, buffer, sizeof buffer);
3618 offer->func(buffer, len,
3619 offer->x, offer->y, offer->user_data);
3620
3621 if (len == 0) {
3622 close(offer->fd);
3623 data_offer_destroy(offer);
3624 }
3625}
3626
3627static void
3628data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3629 data_func_t func, void *user_data)
3630{
3631 int p[2];
3632
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003633 if (pipe2(p, O_CLOEXEC) == -1)
3634 return;
3635
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003636 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3637 close(p[1]);
3638
3639 offer->io_task.run = offer_io_func;
3640 offer->fd = p[0];
3641 offer->func = func;
3642 offer->refcount++;
3643 offer->user_data = user_data;
3644
3645 display_watch_fd(offer->input->display,
3646 offer->fd, EPOLLIN, &offer->io_task);
3647}
3648
3649void
3650input_receive_drag_data(struct input *input, const char *mime_type,
3651 data_func_t func, void *data)
3652{
3653 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003654 input->drag_offer->x = input->drag_x;
3655 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003656}
3657
3658int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003659input_receive_drag_data_to_fd(struct input *input,
3660 const char *mime_type, int fd)
3661{
3662 if (input->drag_offer)
3663 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3664
3665 return 0;
3666}
3667
3668int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003669input_receive_selection_data(struct input *input, const char *mime_type,
3670 data_func_t func, void *data)
3671{
3672 char **p;
3673
3674 if (input->selection_offer == NULL)
3675 return -1;
3676
3677 for (p = input->selection_offer->types.data; *p; p++)
3678 if (strcmp(mime_type, *p) == 0)
3679 break;
3680
3681 if (*p == NULL)
3682 return -1;
3683
3684 data_offer_receive_data(input->selection_offer,
3685 mime_type, func, data);
3686 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003687}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003688
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003689int
3690input_receive_selection_data_to_fd(struct input *input,
3691 const char *mime_type, int fd)
3692{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003693 if (input->selection_offer)
3694 wl_data_offer_receive(input->selection_offer->offer,
3695 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003696
3697 return 0;
3698}
3699
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003700void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003701window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003702{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003703 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003704 return;
3705
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003706 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003707}
3708
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003709static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003710surface_set_synchronized(struct surface *surface)
3711{
3712 if (!surface->subsurface)
3713 return;
3714
3715 if (surface->synchronized)
3716 return;
3717
3718 wl_subsurface_set_sync(surface->subsurface);
3719 surface->synchronized = 1;
3720}
3721
3722static void
3723surface_set_synchronized_default(struct surface *surface)
3724{
3725 if (!surface->subsurface)
3726 return;
3727
3728 if (surface->synchronized == surface->synchronized_default)
3729 return;
3730
3731 if (surface->synchronized_default)
3732 wl_subsurface_set_sync(surface->subsurface);
3733 else
3734 wl_subsurface_set_desync(surface->subsurface);
3735
3736 surface->synchronized = surface->synchronized_default;
3737}
3738
3739static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003740surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003741{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003742 struct widget *widget = surface->widget;
3743 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003744
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003745 if (surface->input_region) {
3746 wl_region_destroy(surface->input_region);
3747 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003748 }
3749
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003750 if (surface->opaque_region)
3751 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003752
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003753 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003754
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003755 if (widget->resize_handler)
3756 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003757 widget->allocation.width,
3758 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003759 widget->user_data);
3760
Pekka Paalanen35e82632013-04-25 13:57:48 +03003761 if (surface->subsurface &&
3762 (surface->allocation.x != widget->allocation.x ||
3763 surface->allocation.y != widget->allocation.y)) {
3764 wl_subsurface_set_position(surface->subsurface,
3765 widget->allocation.x,
3766 widget->allocation.y);
3767 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003768 if (surface->allocation.width != widget->allocation.width ||
3769 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003770 window_schedule_redraw(widget->window);
3771 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003772 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003773
3774 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003775 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003776 widget->allocation.width,
3777 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003778}
3779
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003780static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003781hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3782{
3783 /*
3784 * This hack should be removed, when EGL respects
3785 * eglSwapInterval(0).
3786 *
3787 * If this window has sub-surfaces, especially a free-running
3788 * EGL-widget, we need to post the parent surface once with
3789 * all the old state to guarantee, that the EGL-widget will
3790 * receive its frame callback soon. Otherwise, a forced call
3791 * to eglSwapBuffers may end up blocking, waiting for a frame
3792 * event that will never come, because we will commit the parent
3793 * surface with all new state only after eglSwapBuffers returns.
3794 *
3795 * This assumes, that:
3796 * 1. When the EGL widget's resize hook is called, it pauses.
3797 * 2. When the EGL widget's redraw hook is called, it forces a
3798 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3799 * In a single threaded application condition 1 is a no-op.
3800 *
3801 * XXX: This should actually be after the surface_resize() calls,
3802 * but cannot, because then it would commit the incomplete state
3803 * accumulated from the widget resize hooks.
3804 */
3805 if (window->subsurface_list.next != &window->main_surface->link ||
3806 window->subsurface_list.prev != &window->main_surface->link)
3807 wl_surface_commit(window->main_surface->surface);
3808}
3809
3810static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003811window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003812{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003813 struct surface *surface;
3814
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003815 widget_set_allocation(window->main_surface->widget,
3816 window->pending_allocation.x,
3817 window->pending_allocation.y,
3818 window->pending_allocation.width,
3819 window->pending_allocation.height);
3820
3821 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003822
3823 /* The main surface is in the list, too. Main surface's
3824 * resize_handler is responsible for calling widget_set_allocation()
3825 * on all sub-surface root widgets, so they will be resized
3826 * properly.
3827 */
3828 wl_list_for_each(surface, &window->subsurface_list, link) {
3829 if (surface == window->main_surface)
3830 continue;
3831
3832 surface_set_synchronized(surface);
3833 surface_resize(surface);
3834 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003835
3836 if (!window->fullscreen && !window->maximized)
3837 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003838}
3839
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003840static void
3841idle_resize(struct window *window)
3842{
3843 window->resize_needed = 0;
3844 window->redraw_needed = 1;
3845
3846 DBG("from %dx%d to %dx%d\n",
3847 window->main_surface->server_allocation.width,
3848 window->main_surface->server_allocation.height,
3849 window->pending_allocation.width,
3850 window->pending_allocation.height);
3851
3852 hack_prevent_EGL_sub_surface_deadlock(window);
3853
3854 window_do_resize(window);
3855}
3856
3857static void
3858undo_resize(struct window *window)
3859{
3860 window->pending_allocation.width =
3861 window->main_surface->server_allocation.width;
3862 window->pending_allocation.height =
3863 window->main_surface->server_allocation.height;
3864
3865 DBG("back to %dx%d\n",
3866 window->main_surface->server_allocation.width,
3867 window->main_surface->server_allocation.height);
3868
3869 window_do_resize(window);
3870
3871 if (window->pending_allocation.width == 0 &&
3872 window->pending_allocation.height == 0) {
3873 fprintf(stderr, "Error: Could not draw a surface, "
3874 "most likely due to insufficient disk space in "
3875 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3876 exit(EXIT_FAILURE);
3877 }
3878}
3879
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003880void
3881window_schedule_resize(struct window *window, int width, int height)
3882{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003883 /* We should probably get these numbers from the theme. */
3884 const int min_width = 200, min_height = 200;
3885
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003886 window->pending_allocation.x = 0;
3887 window->pending_allocation.y = 0;
3888 window->pending_allocation.width = width;
3889 window->pending_allocation.height = height;
3890
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003891 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003892 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003893 window->min_allocation.width = min_width;
3894 else
3895 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003896 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003897 window->min_allocation.height = min_height;
3898 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003899 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003900 }
3901
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003902 if (window->pending_allocation.width < window->min_allocation.width)
3903 window->pending_allocation.width = window->min_allocation.width;
3904 if (window->pending_allocation.height < window->min_allocation.height)
3905 window->pending_allocation.height = window->min_allocation.height;
3906
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003907 window->resize_needed = 1;
3908 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003909}
3910
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003911void
3912widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3913{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003914 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003915}
3916
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003917static int
3918window_get_shadow_margin(struct window *window)
3919{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003920 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003921 return frame_get_shadow_margin(window->frame->frame);
3922 else
3923 return 0;
3924}
3925
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003926static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003927handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003928 int32_t width, int32_t height,
3929 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003930{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003931 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003932 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003933
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003934 window->maximized = 0;
3935 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003936 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003937 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003938
3939 wl_array_for_each(p, states) {
3940 uint32_t state = *p;
3941 switch (state) {
3942 case XDG_SURFACE_STATE_MAXIMIZED:
3943 window->maximized = 1;
3944 break;
3945 case XDG_SURFACE_STATE_FULLSCREEN:
3946 window->fullscreen = 1;
3947 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003948 case XDG_SURFACE_STATE_RESIZING:
3949 window->resizing = 1;
3950 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003951 case XDG_SURFACE_STATE_ACTIVATED:
3952 window->focused = 1;
3953 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003954 default:
3955 /* Unknown state */
3956 break;
3957 }
3958 }
3959
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003960 if (window->frame) {
3961 if (window->maximized) {
3962 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3963 } else {
3964 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3965 }
3966
3967 if (window->focused) {
3968 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3969 } else {
3970 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3971 }
3972 }
3973
Jasper St. Pierref184c382014-05-06 08:33:27 -04003974 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003975 /* The width / height params are for window geometry,
3976 * but window_schedule_resize takes allocation. Add
3977 * on the shadow margin to get the difference. */
3978 int margin = window_get_shadow_margin(window);
3979
3980 window_schedule_resize(window,
3981 width + margin * 2,
3982 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04003983 } else {
3984 window_schedule_resize(window,
3985 window->saved_allocation.width,
3986 window->saved_allocation.height);
3987 }
3988
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07003989 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07003990
3991 if (window->state_changed_handler)
3992 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003993}
3994
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003995static void
3996handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3997{
3998 struct window *window = data;
3999 window_close(window);
4000}
4001
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004002static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004003 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05004004 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004005};
4006
4007static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004008window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004009{
4010 struct wl_surface *parent_surface;
4011
4012 if (!window->xdg_surface)
4013 return;
4014
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004015 if (window->parent)
4016 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004017 else
4018 parent_surface = NULL;
4019
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004020 if (parent_surface == window->last_parent_surface)
4021 return;
4022
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004023 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004024 window->last_parent_surface = parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004025}
4026
4027static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004028window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004029{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004030 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004031 frame_input_rect(window->frame->frame,
4032 &geometry->x,
4033 &geometry->y,
4034 &geometry->width,
4035 &geometry->height);
4036 else
4037 window_get_allocation(window, geometry);
4038}
4039
4040static void
4041window_sync_geometry(struct window *window)
4042{
4043 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004044
4045 if (!window->xdg_surface)
4046 return;
4047
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004048 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004049 if (geometry.x == window->last_geometry.x &&
4050 geometry.y == window->last_geometry.y &&
4051 geometry.width == window->last_geometry.width &&
4052 geometry.height == window->last_geometry.height)
4053 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004054
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004055 xdg_surface_set_window_geometry(window->xdg_surface,
4056 geometry.x,
4057 geometry.y,
4058 geometry.width,
4059 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004060 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004061}
4062
4063static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004064window_flush(struct window *window)
4065{
4066 struct surface *surface;
4067
4068 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004069 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004070 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004071 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004072 }
4073 }
4074
4075 wl_list_for_each(surface, &window->subsurface_list, link) {
4076 if (surface == window->main_surface)
4077 continue;
4078
4079 surface_flush(surface);
4080 }
4081
4082 surface_flush(window->main_surface);
4083}
4084
4085static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004086menu_destroy(struct menu *menu)
4087{
4088 widget_destroy(menu->widget);
4089 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004090 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004091 free(menu);
4092}
4093
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004094void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004095window_get_allocation(struct window *window,
4096 struct rectangle *allocation)
4097{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004098 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004099}
4100
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004101static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004102widget_redraw(struct widget *widget)
4103{
4104 struct widget *child;
4105
4106 if (widget->redraw_handler)
4107 widget->redraw_handler(widget, widget->user_data);
4108 wl_list_for_each(child, &widget->child_list, link)
4109 widget_redraw(child);
4110}
4111
4112static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004113frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4114{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004115 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004116
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004117 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004118 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004119 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004120 surface->frame_cb = NULL;
4121
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004122 surface->last_time = time;
4123
Pekka Paalanen71233882013-04-25 13:57:53 +03004124 if (surface->redraw_needed || surface->window->redraw_needed) {
4125 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004126 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004127 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004128}
4129
4130static const struct wl_callback_listener listener = {
4131 frame_callback
4132};
4133
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004134static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004135surface_redraw(struct surface *surface)
4136{
Pekka Paalanen71233882013-04-25 13:57:53 +03004137 DBG_OBJ(surface->surface, "begin\n");
4138
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004139 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004140 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004141
4142 /* Whole-window redraw forces a redraw even if the previous has
4143 * not yet hit the screen.
4144 */
4145 if (surface->frame_cb) {
4146 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004147 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004148
Pekka Paalanen71233882013-04-25 13:57:53 +03004149 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004150 wl_callback_destroy(surface->frame_cb);
4151 }
4152
Neil Roberts97b747c2013-12-19 16:17:12 +00004153 if (surface->widget->use_cairo &&
4154 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004155 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4156 return -1;
4157 }
4158
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004159 surface->frame_cb = wl_surface_frame(surface->surface);
4160 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004161 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004162
4163 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004164 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004165 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004166 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004167 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004168}
4169
4170static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004171idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004172{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004173 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004174 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004175 int failed = 0;
4176 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004177
Pekka Paalanen71233882013-04-25 13:57:53 +03004178 DBG(" --------- \n");
4179
Pekka Paalaneneebff542013-04-25 13:57:52 +03004180 wl_list_init(&window->redraw_task.link);
4181 window->redraw_task_scheduled = 0;
4182
4183 if (window->resize_needed) {
4184 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004185 if (window->main_surface->frame_cb) {
4186 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004187 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004188 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004189
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004190 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004191 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004192 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004193
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004194 if (surface_redraw(window->main_surface) < 0) {
4195 /*
4196 * Only main_surface failure will cause us to undo the resize.
4197 * If sub-surfaces fail, they will just be broken with old
4198 * content.
4199 */
4200 failed = 1;
4201 } else {
4202 wl_list_for_each(surface, &window->subsurface_list, link) {
4203 if (surface == window->main_surface)
4204 continue;
4205
4206 surface_redraw(surface);
4207 }
4208 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004209
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004210 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004211 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004212
4213 wl_list_for_each(surface, &window->subsurface_list, link)
4214 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004215
4216 if (resized && failed) {
4217 /* Restore widget tree to correspond to what is on screen. */
4218 undo_resize(window);
4219 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004220}
4221
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004222static void
4223window_schedule_redraw_task(struct window *window)
4224{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004225 if (!window->redraw_task_scheduled) {
4226 window->redraw_task.run = idle_redraw;
4227 display_defer(window->display, &window->redraw_task);
4228 window->redraw_task_scheduled = 1;
4229 }
4230}
4231
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004232void
4233window_schedule_redraw(struct window *window)
4234{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004235 struct surface *surface;
4236
Pekka Paalanen71233882013-04-25 13:57:53 +03004237 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4238
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004239 wl_list_for_each(surface, &window->subsurface_list, link)
4240 surface->redraw_needed = 1;
4241
4242 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004243}
4244
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004245int
4246window_is_fullscreen(struct window *window)
4247{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004248 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004249}
4250
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004251void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004252window_set_fullscreen(struct window *window, int fullscreen)
4253{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004254 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004255 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004256
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004257 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004258 return;
4259
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004260 if (fullscreen)
4261 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4262 else
4263 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004264}
4265
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004266int
4267window_is_maximized(struct window *window)
4268{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004269 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004270}
4271
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004272void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004273window_set_maximized(struct window *window, int maximized)
4274{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004275 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004276 return;
4277
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004278 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004279 return;
4280
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004281 if (maximized)
4282 xdg_surface_set_maximized(window->xdg_surface);
4283 else
4284 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004285}
4286
Jasper St. Pierrede680992014-04-10 17:23:49 -07004287int
4288window_is_resizing(struct window *window)
4289{
4290 return window->resizing;
4291}
4292
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004293void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004294window_set_minimized(struct window *window)
4295{
4296 if (!window->xdg_surface)
4297 return;
4298
4299 xdg_surface_set_minimized(window->xdg_surface);
4300}
4301
4302void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004303window_set_user_data(struct window *window, void *data)
4304{
4305 window->user_data = data;
4306}
4307
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004308void *
4309window_get_user_data(struct window *window)
4310{
4311 return window->user_data;
4312}
4313
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004314void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004315window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004316 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004317{
4318 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004319}
4320
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004321void
4322window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004323 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004324{
4325 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004326}
4327
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004328void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004329window_set_data_handler(struct window *window, window_data_handler_t handler)
4330{
4331 window->data_handler = handler;
4332}
4333
4334void
4335window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4336{
4337 window->drop_handler = handler;
4338}
4339
4340void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004341window_set_close_handler(struct window *window,
4342 window_close_handler_t handler)
4343{
4344 window->close_handler = handler;
4345}
4346
4347void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004348window_set_fullscreen_handler(struct window *window,
4349 window_fullscreen_handler_t handler)
4350{
4351 window->fullscreen_handler = handler;
4352}
4353
4354void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004355window_set_output_handler(struct window *window,
4356 window_output_handler_t handler)
4357{
4358 window->output_handler = handler;
4359}
4360
4361void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004362window_set_state_changed_handler(struct window *window,
4363 window_state_changed_handler_t handler)
4364{
4365 window->state_changed_handler = handler;
4366}
4367
4368void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004369window_set_title(struct window *window, const char *title)
4370{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004371 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004372 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004373 if (window->frame) {
4374 frame_set_title(window->frame->frame, title);
4375 widget_schedule_redraw(window->frame->widget);
4376 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004377 if (window->xdg_surface)
4378 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004379}
4380
4381const char *
4382window_get_title(struct window *window)
4383{
4384 return window->title;
4385}
4386
4387void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004388window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4389{
4390 struct text_cursor_position *text_cursor_position =
4391 window->display->text_cursor_position;
4392
Scott Moreau9295ce02012-06-01 12:46:10 -06004393 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004394 return;
4395
4396 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004397 window->main_surface->surface,
4398 wl_fixed_from_int(x),
4399 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004400}
4401
4402void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004403window_damage(struct window *window, int32_t x, int32_t y,
4404 int32_t width, int32_t height)
4405{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004406 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004407}
4408
Casey Dahlin9074db52012-04-19 22:50:09 -04004409static void
4410surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004411 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004412{
Rob Bradford7507b572012-05-15 17:55:34 +01004413 struct window *window = data;
4414 struct output *output;
4415 struct output *output_found = NULL;
4416 struct window_output *window_output;
4417
4418 wl_list_for_each(output, &window->display->output_list, link) {
4419 if (output->output == wl_output) {
4420 output_found = output;
4421 break;
4422 }
4423 }
4424
4425 if (!output_found)
4426 return;
4427
Brian Lovinbc919262013-08-07 15:34:59 -07004428 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004429 window_output->output = output_found;
4430
4431 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004432
4433 if (window->output_handler)
4434 window->output_handler(window, output_found, 1,
4435 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004436}
4437
4438static void
4439surface_leave(void *data,
4440 struct wl_surface *wl_surface, struct wl_output *output)
4441{
Rob Bradford7507b572012-05-15 17:55:34 +01004442 struct window *window = data;
4443 struct window_output *window_output;
4444 struct window_output *window_output_found = NULL;
4445
4446 wl_list_for_each(window_output, &window->window_output_list, link) {
4447 if (window_output->output->output == output) {
4448 window_output_found = window_output;
4449 break;
4450 }
4451 }
4452
4453 if (window_output_found) {
4454 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004455
4456 if (window->output_handler)
4457 window->output_handler(window, window_output->output,
4458 0, window->user_data);
4459
Rob Bradford7507b572012-05-15 17:55:34 +01004460 free(window_output_found);
4461 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004462}
4463
4464static const struct wl_surface_listener surface_listener = {
4465 surface_enter,
4466 surface_leave
4467};
4468
Pekka Paalanen4e373742013-02-13 16:17:13 +02004469static struct surface *
4470surface_create(struct window *window)
4471{
4472 struct display *display = window->display;
4473 struct surface *surface;
4474
Brian Lovinbc919262013-08-07 15:34:59 -07004475 surface = xmalloc(sizeof *surface);
4476 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004477 if (!surface)
4478 return NULL;
4479
4480 surface->window = window;
4481 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004482 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004483 wl_surface_add_listener(surface->surface, &surface_listener, window);
4484
Pekka Paalanen35e82632013-04-25 13:57:48 +03004485 wl_list_insert(&window->subsurface_list, &surface->link);
4486
Pekka Paalanen4e373742013-02-13 16:17:13 +02004487 return surface;
4488}
4489
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004490static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004491get_preferred_buffer_type(struct display *display)
4492{
4493#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004494 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004495 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4496#endif
4497
4498 return WINDOW_BUFFER_TYPE_SHM;
4499}
4500
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004501static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004502window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004503{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004504 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004505 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004506
Peter Huttererf3d62272013-08-08 11:57:05 +10004507 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004508 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004509 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004510
4511 surface = surface_create(window);
4512 window->main_surface = surface;
4513
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004514 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004515
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004516 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004517 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004518
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004519 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004520
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004521 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004522 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004523 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004524
Rob Bradford7507b572012-05-15 17:55:34 +01004525 wl_list_init (&window->window_output_list);
4526
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004527 return window;
4528}
4529
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004530struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004531window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004532{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004533 struct window *window;
4534
4535 window = window_create_internal(display, 0);
4536
4537 window->xdg_surface =
4538 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4539 window->main_surface->surface);
4540 fail_on_null(window->xdg_surface);
4541
4542 xdg_surface_set_user_data(window->xdg_surface, window);
4543 xdg_surface_add_listener(window->xdg_surface,
4544 &xdg_surface_listener, window);
4545
4546 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004547}
4548
4549struct window *
4550window_create_custom(struct display *display)
4551{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004552 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004553}
4554
Jasper St. Pierre53686042013-12-09 15:26:25 -05004555void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004556window_set_parent(struct window *window,
4557 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004558{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004559 window->parent = parent_window;
4560 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004561}
4562
4563struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004564window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004565{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004566 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004567}
4568
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004569static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004570menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004571{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004572 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004573 int next;
4574
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004575 frame_interior(menu->frame, &x, &y, &width, &height);
4576 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004577 if (menu->current != next) {
4578 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004579 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004580 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004581}
4582
4583static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004584menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004585 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004586 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004587{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004588 struct menu *menu = data;
4589
4590 if (widget == menu->widget)
4591 menu_set_item(data, y);
4592
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004593 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004594}
4595
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004596static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004597menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004598 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004599{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004600 struct menu *menu = data;
4601
4602 if (widget == menu->widget)
4603 menu_set_item(data, y);
4604
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004605 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004606}
4607
4608static void
4609menu_leave_handler(struct widget *widget, struct input *input, void *data)
4610{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004611 struct menu *menu = data;
4612
4613 if (widget == menu->widget)
4614 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004615}
4616
4617static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004618menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004619 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004620 uint32_t button, enum wl_pointer_button_state state,
4621 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004622
4623{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004624 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004625
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004626 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4627 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004628 /* Either relase after press-drag-release or
4629 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004630 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004631 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004632 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004633 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004634 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004635 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004636}
4637
4638static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004639menu_touch_up_handler(struct widget *widget,
4640 struct input *input,
4641 uint32_t serial,
4642 uint32_t time,
4643 int32_t id,
4644 void *data)
4645{
4646 struct menu *menu = data;
4647
4648 input_ungrab(input);
4649 menu_destroy(menu);
4650}
4651
4652static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004653menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004654{
4655 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004656 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004657 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004658
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004659 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004660
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004661 frame_repaint(menu->frame, cr);
4662 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004663
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004664 theme_set_background_source(menu->window->display->theme,
4665 cr, THEME_FRAME_ACTIVE);
4666 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004667 cairo_fill(cr);
4668
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004669 cairo_select_font_face(cr, "sans",
4670 CAIRO_FONT_SLANT_NORMAL,
4671 CAIRO_FONT_WEIGHT_NORMAL);
4672 cairo_set_font_size(cr, 12);
4673
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004674 for (i = 0; i < menu->count; i++) {
4675 if (i == menu->current) {
4676 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004677 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004678 cairo_fill(cr);
4679 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004680 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004681 cairo_show_text(cr, menu->entries[i]);
4682 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004683 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4684 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004685 cairo_show_text(cr, menu->entries[i]);
4686 }
4687 }
4688
4689 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004690}
4691
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004692static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004693handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4694{
4695 struct window *window = data;
4696 struct menu *menu = window->main_surface->widget->user_data;
4697
4698 input_ungrab(menu->input);
4699 menu_destroy(menu);
4700}
4701
4702static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004703 handle_popup_popup_done,
4704};
4705
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004706static struct menu *
4707create_menu(struct display *display,
4708 struct input *input, uint32_t time,
4709 menu_func_t func, const char **entries, int count,
4710 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004711{
4712 struct window *window;
4713 struct menu *menu;
4714
4715 menu = malloc(sizeof *menu);
4716 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004717 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004718
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004719 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004720 if (!window) {
4721 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004722 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004723 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004724
4725 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004726 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004727 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004728 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004729 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004730 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004731 menu->entries = entries;
4732 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004733 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004734 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004735 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004736 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004737 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004738
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004739 input_ungrab(input);
4740
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004741 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004742 widget_set_enter_handler(menu->widget, menu_enter_handler);
4743 widget_set_leave_handler(menu->widget, menu_leave_handler);
4744 widget_set_motion_handler(menu->widget, menu_motion_handler);
4745 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004746 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004747
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004748 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004749 frame_resize_inside(menu->frame, 200, count * 20);
4750 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4751 window_schedule_resize(window, frame_width(menu->frame),
4752 frame_height(menu->frame));
4753
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004754 return menu;
4755}
4756
4757struct window *
4758window_create_menu(struct display *display,
4759 struct input *input, uint32_t time,
4760 menu_func_t func, const char **entries, int count,
4761 void *user_data)
4762{
4763 struct menu *menu;
4764 menu = create_menu(display, input, time, func, entries, count, user_data);
4765
4766 if (menu == NULL)
4767 return NULL;
4768
4769 return menu->window;
4770}
4771
4772void
4773window_show_menu(struct display *display,
4774 struct input *input, uint32_t time, struct window *parent,
4775 int32_t x, int32_t y,
4776 menu_func_t func, const char **entries, int count)
4777{
4778 struct menu *menu;
4779 struct window *window;
4780 int32_t ix, iy;
4781
4782 menu = create_menu(display, input, time, func, entries, count, parent);
4783
4784 if (menu == NULL)
4785 return;
4786
4787 window = menu->window;
4788
4789 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4790 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4791
4792 window->x = x;
4793 window->y = y;
4794
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004795 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004796
4797 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4798 window->main_surface->surface,
4799 parent->main_surface->surface,
4800 input->seat,
4801 display_get_serial(window->display),
4802 window->x - ix,
4803 window->y - iy,
4804 0);
4805 fail_on_null(window->xdg_popup);
4806
4807 xdg_popup_set_user_data(window->xdg_popup, window);
4808 xdg_popup_add_listener(window->xdg_popup,
4809 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004810}
4811
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004812void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004813window_set_buffer_type(struct window *window, enum window_buffer_type type)
4814{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004815 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004816}
4817
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004818enum window_buffer_type
4819window_get_buffer_type(struct window *window)
4820{
4821 return window->main_surface->buffer_type;
4822}
4823
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004824void
4825window_set_preferred_format(struct window *window,
4826 enum preferred_format format)
4827{
4828 window->preferred_format = format;
4829}
4830
Pekka Paalanen35e82632013-04-25 13:57:48 +03004831struct widget *
4832window_add_subsurface(struct window *window, void *data,
4833 enum subsurface_mode default_mode)
4834{
4835 struct widget *widget;
4836 struct surface *surface;
4837 struct wl_surface *parent;
4838 struct wl_subcompositor *subcompo = window->display->subcompositor;
4839
Pekka Paalanen35e82632013-04-25 13:57:48 +03004840 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004841 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004842 widget = widget_create(window, surface, data);
4843 wl_list_init(&widget->link);
4844 surface->widget = widget;
4845
4846 parent = window->main_surface->surface;
4847 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4848 surface->surface,
4849 parent);
4850 surface->synchronized = 1;
4851
4852 switch (default_mode) {
4853 case SUBSURFACE_SYNCHRONIZED:
4854 surface->synchronized_default = 1;
4855 break;
4856 case SUBSURFACE_DESYNCHRONIZED:
4857 surface->synchronized_default = 0;
4858 break;
4859 default:
4860 assert(!"bad enum subsurface_mode");
4861 }
4862
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004863 window->resize_needed = 1;
4864 window_schedule_redraw(window);
4865
Pekka Paalanen35e82632013-04-25 13:57:48 +03004866 return widget;
4867}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004868
4869static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004870display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004871 struct wl_output *wl_output,
4872 int x, int y,
4873 int physical_width,
4874 int physical_height,
4875 int subpixel,
4876 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004877 const char *model,
4878 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004879{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004880 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004881
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004882 output->allocation.x = x;
4883 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004884 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004885
4886 if (output->make)
4887 free(output->make);
4888 output->make = strdup(make);
4889
4890 if (output->model)
4891 free(output->model);
4892 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004893}
4894
4895static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004896display_handle_done(void *data,
4897 struct wl_output *wl_output)
4898{
4899}
4900
4901static void
4902display_handle_scale(void *data,
4903 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004904 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004905{
4906 struct output *output = data;
4907
4908 output->scale = scale;
4909}
4910
4911static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004912display_handle_mode(void *data,
4913 struct wl_output *wl_output,
4914 uint32_t flags,
4915 int width,
4916 int height,
4917 int refresh)
4918{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004919 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004920 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004921
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004922 if (flags & WL_OUTPUT_MODE_CURRENT) {
4923 output->allocation.width = width;
4924 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004925 if (display->output_configure_handler)
4926 (*display->output_configure_handler)(
4927 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004928 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004929}
4930
4931static const struct wl_output_listener output_listener = {
4932 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004933 display_handle_mode,
4934 display_handle_done,
4935 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004936};
4937
4938static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004939display_add_output(struct display *d, uint32_t id)
4940{
4941 struct output *output;
4942
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004943 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004944 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004945 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004946 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004947 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004948 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004949 wl_list_insert(d->output_list.prev, &output->link);
4950
4951 wl_output_add_listener(output->output, &output_listener, output);
4952}
4953
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004954static void
4955output_destroy(struct output *output)
4956{
4957 if (output->destroy_handler)
4958 (*output->destroy_handler)(output, output->user_data);
4959
4960 wl_output_destroy(output->output);
4961 wl_list_remove(&output->link);
4962 free(output);
4963}
4964
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004965static void
4966display_destroy_output(struct display *d, uint32_t id)
4967{
4968 struct output *output;
4969
4970 wl_list_for_each(output, &d->output_list, link) {
4971 if (output->server_output_id == id) {
4972 output_destroy(output);
4973 break;
4974 }
4975 }
4976}
4977
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004978void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004979display_set_global_handler(struct display *display,
4980 display_global_handler_t handler)
4981{
4982 struct global *global;
4983
4984 display->global_handler = handler;
4985 if (!handler)
4986 return;
4987
4988 wl_list_for_each(global, &display->global_list, link)
4989 display->global_handler(display,
4990 global->name, global->interface,
4991 global->version, display->user_data);
4992}
4993
4994void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004995display_set_global_handler_remove(struct display *display,
4996 display_global_handler_t remove_handler)
4997{
4998 display->global_handler_remove = remove_handler;
4999 if (!remove_handler)
5000 return;
5001}
5002
5003void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005004display_set_output_configure_handler(struct display *display,
5005 display_output_handler_t handler)
5006{
5007 struct output *output;
5008
5009 display->output_configure_handler = handler;
5010 if (!handler)
5011 return;
5012
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005013 wl_list_for_each(output, &display->output_list, link) {
5014 if (output->allocation.width == 0 &&
5015 output->allocation.height == 0)
5016 continue;
5017
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005018 (*display->output_configure_handler)(output,
5019 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03005020 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005021}
5022
5023void
5024output_set_user_data(struct output *output, void *data)
5025{
5026 output->user_data = data;
5027}
5028
5029void *
5030output_get_user_data(struct output *output)
5031{
5032 return output->user_data;
5033}
5034
5035void
5036output_set_destroy_handler(struct output *output,
5037 display_output_handler_t handler)
5038{
5039 output->destroy_handler = handler;
5040 /* FIXME: implement this, once we have way to remove outputs */
5041}
5042
5043void
Scott Moreau4e072362012-09-29 02:03:11 -06005044output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005045{
Scott Moreau4e072362012-09-29 02:03:11 -06005046 struct rectangle allocation = output->allocation;
5047
5048 switch (output->transform) {
5049 case WL_OUTPUT_TRANSFORM_90:
5050 case WL_OUTPUT_TRANSFORM_270:
5051 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5052 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5053 /* Swap width and height */
5054 allocation.width = output->allocation.height;
5055 allocation.height = output->allocation.width;
5056 break;
5057 }
5058
5059 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005060}
5061
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005062struct wl_output *
5063output_get_wl_output(struct output *output)
5064{
5065 return output->output;
5066}
5067
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005068enum wl_output_transform
5069output_get_transform(struct output *output)
5070{
5071 return output->transform;
5072}
5073
Alexander Larssonafd319a2013-05-22 14:41:27 +02005074uint32_t
5075output_get_scale(struct output *output)
5076{
5077 return output->scale;
5078}
5079
Jason Ekstrand738715d2014-04-02 19:53:50 -05005080const char *
5081output_get_make(struct output *output)
5082{
5083 return output->make;
5084}
5085
5086const char *
5087output_get_model(struct output *output)
5088{
5089 return output->model;
5090}
5091
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005092static void
Daniel Stone97f68542012-05-30 16:32:01 +01005093fini_xkb(struct input *input)
5094{
5095 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005096 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005097}
5098
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005099#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005100
Daniel Stone97f68542012-05-30 16:32:01 +01005101static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005102display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005103{
5104 struct input *input;
5105
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005106 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005107 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005108 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005109 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005110 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005111 input->pointer_focus = NULL;
5112 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005113 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005114 wl_list_insert(d->input_list.prev, &input->link);
5115
Daniel Stone37816df2012-05-16 18:45:18 +01005116 wl_seat_add_listener(input->seat, &seat_listener, input);
5117 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005118
Jason Ekstranda669bd52014-04-02 19:53:51 -05005119 if (d->data_device_manager) {
5120 input->data_device =
5121 wl_data_device_manager_get_data_device(d->data_device_manager,
5122 input->seat);
5123 wl_data_device_add_listener(input->data_device,
5124 &data_device_listener,
5125 input);
5126 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005127
5128 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005129
Jonny Lamb06959082014-08-12 14:58:27 +02005130 set_repeat_info(input, 40, 400);
5131
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005132 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5133 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005134 input->repeat_task.run = keyboard_repeat_func;
5135 display_watch_fd(d, input->repeat_timer_fd,
5136 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005137}
5138
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005139static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005140input_destroy(struct input *input)
5141{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005142 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005143 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005144
5145 if (input->drag_offer)
5146 data_offer_destroy(input->drag_offer);
5147
5148 if (input->selection_offer)
5149 data_offer_destroy(input->selection_offer);
5150
Jason Ekstranda669bd52014-04-02 19:53:51 -05005151 if (input->data_device)
5152 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005153
5154 if (input->display->seat_version >= 3) {
5155 if (input->pointer)
5156 wl_pointer_release(input->pointer);
5157 if (input->keyboard)
5158 wl_keyboard_release(input->keyboard);
5159 }
5160
Daniel Stone97f68542012-05-30 16:32:01 +01005161 fini_xkb(input);
5162
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005163 wl_surface_destroy(input->pointer_surface);
5164
Pekka Paalanene1207c72011-12-16 12:02:09 +02005165 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005166 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005167 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005168 free(input);
5169}
5170
5171static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005172init_workspace_manager(struct display *d, uint32_t id)
5173{
5174 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005175 wl_registry_bind(d->registry, id,
5176 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005177 if (d->workspace_manager != NULL)
5178 workspace_manager_add_listener(d->workspace_manager,
5179 &workspace_manager_listener,
5180 d);
5181}
5182
5183static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005184shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5185{
5186 struct display *d = data;
5187
5188 if (format == WL_SHM_FORMAT_RGB565)
5189 d->has_rgb565 = 1;
5190}
5191
5192struct wl_shm_listener shm_listener = {
5193 shm_format
5194};
5195
5196static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005197xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5198{
5199 xdg_shell_pong(shell, serial);
5200}
5201
5202static const struct xdg_shell_listener xdg_shell_listener = {
5203 xdg_shell_ping,
5204};
5205
Pekka Paalanen71182ae2014-08-21 17:47:20 +03005206#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005207#ifdef static_assert
5208static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5209 "Interface version doesn't match implementation version");
5210#endif
5211
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005212static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005213registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5214 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005215{
5216 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005217 struct global *global;
5218
Brian Lovinbc919262013-08-07 15:34:59 -07005219 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005220 global->name = id;
5221 global->interface = strdup(interface);
5222 global->version = version;
5223 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005224
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005225 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005226 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005227 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005228 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005229 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005230 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005231 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005232 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005233 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005234 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005235 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005236 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5237 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005238 wl_registry_bind(registry, id,
5239 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005240 } else if (strcmp(interface, "xdg_shell") == 0) {
5241 d->xdg_shell = wl_registry_bind(registry, id,
5242 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005243 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005244 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005245 } else if (strcmp(interface, "text_cursor_position") == 0) {
5246 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005247 wl_registry_bind(registry, id,
5248 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005249 } else if (strcmp(interface, "workspace_manager") == 0) {
5250 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005251 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5252 d->subcompositor =
5253 wl_registry_bind(registry, id,
5254 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005255 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005256
5257 if (d->global_handler)
5258 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005259}
5260
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005261static void
5262registry_handle_global_remove(void *data, struct wl_registry *registry,
5263 uint32_t name)
5264{
5265 struct display *d = data;
5266 struct global *global;
5267 struct global *tmp;
5268
5269 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5270 if (global->name != name)
5271 continue;
5272
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005273 if (strcmp(global->interface, "wl_output") == 0)
5274 display_destroy_output(d, name);
5275
5276 /* XXX: Should destroy remaining bound globals */
5277
5278 if (d->global_handler_remove)
5279 d->global_handler_remove(d, name, global->interface,
5280 global->version, d->user_data);
5281
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005282 wl_list_remove(&global->link);
5283 free(global->interface);
5284 free(global);
5285 }
5286}
5287
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005288void *
5289display_bind(struct display *display, uint32_t name,
5290 const struct wl_interface *interface, uint32_t version)
5291{
5292 return wl_registry_bind(display->registry, name, interface, version);
5293}
5294
5295static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005296 registry_handle_global,
5297 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005298};
5299
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005300#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005301static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005302init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005303{
5304 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005305 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005306
Rob Clark6396ed32012-03-11 19:48:41 -05005307#ifdef USE_CAIRO_GLESV2
5308# define GL_BIT EGL_OPENGL_ES2_BIT
5309#else
5310# define GL_BIT EGL_OPENGL_BIT
5311#endif
5312
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005313 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005314 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005315 EGL_RED_SIZE, 1,
5316 EGL_GREEN_SIZE, 1,
5317 EGL_BLUE_SIZE, 1,
5318 EGL_ALPHA_SIZE, 1,
5319 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005320 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005321 EGL_NONE
5322 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005323
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005324#ifdef USE_CAIRO_GLESV2
5325 static const EGLint context_attribs[] = {
5326 EGL_CONTEXT_CLIENT_VERSION, 2,
5327 EGL_NONE
5328 };
5329 EGLint api = EGL_OPENGL_ES_API;
5330#else
5331 EGLint *context_attribs = NULL;
5332 EGLint api = EGL_OPENGL_API;
5333#endif
5334
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005335 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005336 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005337 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005338 return -1;
5339 }
5340
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005341 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005342 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005343 return -1;
5344 }
5345
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005346 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5347 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005348 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005349 return -1;
5350 }
5351
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005352 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005353 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005354 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005355 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005356 return -1;
5357 }
5358
Benjamin Franzke0c991632011-09-27 21:57:31 +02005359 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5360 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005361 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005362 return -1;
5363 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005364
5365 return 0;
5366}
5367
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005368static void
5369fini_egl(struct display *display)
5370{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005371 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005372
5373 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5374 EGL_NO_CONTEXT);
5375
5376 eglTerminate(display->dpy);
5377 eglReleaseThread();
5378}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005379#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005380
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005381static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005382init_dummy_surface(struct display *display)
5383{
5384 int len;
5385 void *data;
5386
5387 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5388 data = malloc(len);
5389 display->dummy_surface =
5390 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5391 1, 1, len);
5392 display->dummy_surface_data = data;
5393}
5394
5395static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005396handle_display_data(struct task *task, uint32_t events)
5397{
5398 struct display *display =
5399 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005400 struct epoll_event ep;
5401 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005402
5403 display->display_fd_events = events;
5404
5405 if (events & EPOLLERR || events & EPOLLHUP) {
5406 display_exit(display);
5407 return;
5408 }
5409
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005410 if (events & EPOLLIN) {
5411 ret = wl_display_dispatch(display->display);
5412 if (ret == -1) {
5413 display_exit(display);
5414 return;
5415 }
5416 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005417
5418 if (events & EPOLLOUT) {
5419 ret = wl_display_flush(display->display);
5420 if (ret == 0) {
5421 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5422 ep.data.ptr = &display->display_task;
5423 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5424 display->display_fd, &ep);
5425 } else if (ret == -1 && errno != EAGAIN) {
5426 display_exit(display);
5427 return;
5428 }
5429 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005430}
5431
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005432static void
5433log_handler(const char *format, va_list args)
5434{
5435 vfprintf(stderr, format, args);
5436}
5437
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005438struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005439display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005440{
5441 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005442
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005443 wl_log_set_handler_client(log_handler);
5444
Peter Huttererf3d62272013-08-08 11:57:05 +10005445 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005446 if (d == NULL)
5447 return NULL;
5448
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005449 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005450 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005451 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005452 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005453 return NULL;
5454 }
5455
Rob Bradford5ab9c752013-07-26 16:29:43 +01005456 d->xkb_context = xkb_context_new(0);
5457 if (d->xkb_context == NULL) {
5458 fprintf(stderr, "Failed to create XKB context\n");
5459 free(d);
5460 return NULL;
5461 }
5462
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005463 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005464 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005465 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005466 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5467 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005468
5469 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005470 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005471 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005472 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005473
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005474 d->workspace = 0;
5475 d->workspace_count = 1;
5476
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005477 d->registry = wl_display_get_registry(d->display);
5478 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005479
5480 if (wl_display_dispatch(d->display) < 0) {
5481 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5482 return NULL;
5483 }
5484
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005485#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005486 if (init_egl(d) < 0)
5487 fprintf(stderr, "EGL does not seem to work, "
5488 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005489#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005490
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005491 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005492
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005493 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005494
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005495 wl_list_init(&d->window_list);
5496
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005497 init_dummy_surface(d);
5498
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005499 return d;
5500}
5501
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005502static void
5503display_destroy_outputs(struct display *display)
5504{
5505 struct output *tmp;
5506 struct output *output;
5507
5508 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5509 output_destroy(output);
5510}
5511
Pekka Paalanene1207c72011-12-16 12:02:09 +02005512static void
5513display_destroy_inputs(struct display *display)
5514{
5515 struct input *tmp;
5516 struct input *input;
5517
5518 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5519 input_destroy(input);
5520}
5521
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005522void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005523display_destroy(struct display *display)
5524{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005525 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005526 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5527 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005528
5529 if (!wl_list_empty(&display->deferred_list))
5530 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5531
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005532 cairo_surface_destroy(display->dummy_surface);
5533 free(display->dummy_surface_data);
5534
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005535 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005536 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005537
Daniel Stone97f68542012-05-30 16:32:01 +01005538 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005539
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005540 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005541 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005542
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005543#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005544 if (display->argb_device)
5545 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005546#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005547
Pekka Paalanen35e82632013-04-25 13:57:48 +03005548 if (display->subcompositor)
5549 wl_subcompositor_destroy(display->subcompositor);
5550
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005551 if (display->xdg_shell)
5552 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005553
5554 if (display->shm)
5555 wl_shm_destroy(display->shm);
5556
5557 if (display->data_device_manager)
5558 wl_data_device_manager_destroy(display->data_device_manager);
5559
5560 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005561 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005562
5563 close(display->epoll_fd);
5564
U. Artie Eoff44874d92012-10-02 21:12:35 -07005565 if (!(display->display_fd_events & EPOLLERR) &&
5566 !(display->display_fd_events & EPOLLHUP))
5567 wl_display_flush(display->display);
5568
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005569 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005570 free(display);
5571}
5572
5573void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005574display_set_user_data(struct display *display, void *data)
5575{
5576 display->user_data = data;
5577}
5578
5579void *
5580display_get_user_data(struct display *display)
5581{
5582 return display->user_data;
5583}
5584
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005585struct wl_display *
5586display_get_display(struct display *display)
5587{
5588 return display->display;
5589}
5590
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005591int
5592display_has_subcompositor(struct display *display)
5593{
5594 if (display->subcompositor)
5595 return 1;
5596
5597 wl_display_roundtrip(display->display);
5598
5599 return display->subcompositor != NULL;
5600}
5601
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005602cairo_device_t *
5603display_get_cairo_device(struct display *display)
5604{
5605 return display->argb_device;
5606}
5607
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005608struct output *
5609display_get_output(struct display *display)
5610{
5611 return container_of(display->output_list.next, struct output, link);
5612}
5613
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005614struct wl_compositor *
5615display_get_compositor(struct display *display)
5616{
5617 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005618}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005619
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005620uint32_t
5621display_get_serial(struct display *display)
5622{
5623 return display->serial;
5624}
5625
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005626EGLDisplay
5627display_get_egl_display(struct display *d)
5628{
5629 return d->dpy;
5630}
5631
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005632struct wl_data_source *
5633display_create_data_source(struct display *display)
5634{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005635 if (display->data_device_manager)
5636 return wl_data_device_manager_create_data_source(display->data_device_manager);
5637 else
5638 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005639}
5640
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005641EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005642display_get_argb_egl_config(struct display *d)
5643{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005644 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005645}
5646
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005647int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005648display_acquire_window_surface(struct display *display,
5649 struct window *window,
5650 EGLContext ctx)
5651{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005652 struct surface *surface = window->main_surface;
5653
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005654 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005655 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005656
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005657 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005658 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005659}
5660
5661void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005662display_release_window_surface(struct display *display,
5663 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005664{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005665 struct surface *surface = window->main_surface;
5666
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005667 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005668 return;
5669
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005670 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005671}
5672
5673void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005674display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005675{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005676 wl_list_insert(&display->deferred_list, &task->link);
5677}
5678
5679void
5680display_watch_fd(struct display *display,
5681 int fd, uint32_t events, struct task *task)
5682{
5683 struct epoll_event ep;
5684
5685 ep.events = events;
5686 ep.data.ptr = task;
5687 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5688}
5689
5690void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005691display_unwatch_fd(struct display *display, int fd)
5692{
5693 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5694}
5695
5696void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005697display_run(struct display *display)
5698{
5699 struct task *task;
5700 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005701 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005702
Pekka Paalanen826d7952011-12-15 10:14:07 +02005703 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005704 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005705 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005706 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005707 struct task, link);
5708 wl_list_remove(&task->link);
5709 task->run(task, 0);
5710 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005711
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005712 wl_display_dispatch_pending(display->display);
5713
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005714 if (!display->running)
5715 break;
5716
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005717 ret = wl_display_flush(display->display);
5718 if (ret < 0 && errno == EAGAIN) {
5719 ep[0].events =
5720 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5721 ep[0].data.ptr = &display->display_task;
5722
5723 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5724 display->display_fd, &ep[0]);
5725 } else if (ret < 0) {
5726 break;
5727 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005728
5729 count = epoll_wait(display->epoll_fd,
5730 ep, ARRAY_LENGTH(ep), -1);
5731 for (i = 0; i < count; i++) {
5732 task = ep[i].data.ptr;
5733 task->run(task, ep[i].events);
5734 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005735 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005736}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005737
5738void
5739display_exit(struct display *display)
5740{
5741 display->running = 0;
5742}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005743
5744void
5745keysym_modifiers_add(struct wl_array *modifiers_map,
5746 const char *name)
5747{
5748 size_t len = strlen(name) + 1;
5749 char *p;
5750
5751 p = wl_array_add(modifiers_map, len);
5752
5753 if (p == NULL)
5754 return;
5755
5756 strncpy(p, name, len);
5757}
5758
5759static xkb_mod_index_t
5760keysym_modifiers_get_index(struct wl_array *modifiers_map,
5761 const char *name)
5762{
5763 xkb_mod_index_t index = 0;
5764 char *p = modifiers_map->data;
5765
5766 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5767 if (strcmp(p, name) == 0)
5768 return index;
5769
5770 index++;
5771 p += strlen(p) + 1;
5772 }
5773
5774 return XKB_MOD_INVALID;
5775}
5776
5777xkb_mod_mask_t
5778keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5779 const char *name)
5780{
5781 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5782
5783 if (index == XKB_MOD_INVALID)
5784 return XKB_MOD_INVALID;
5785
5786 return 1 << index;
5787}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005788
5789void *
5790fail_on_null(void *p)
5791{
5792 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005793 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005794 exit(EXIT_FAILURE);
5795 }
5796
5797 return p;
5798}
5799
5800void *
5801xmalloc(size_t s)
5802{
5803 return fail_on_null(malloc(s));
5804}
5805
Peter Huttererf3d62272013-08-08 11:57:05 +10005806void *
5807xzalloc(size_t s)
5808{
5809 return fail_on_null(zalloc(s));
5810}
5811
Kristian Høgsbergce278412013-07-25 15:20:20 -07005812char *
5813xstrdup(const char *s)
5814{
5815 return fail_on_null(strdup(s));
5816}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005817
5818void *
5819xrealloc(char *p, size_t s)
5820{
5821 return fail_on_null(realloc(p, s));
5822}