blob: e44d65c9249ffe325b33df258b7bf190c6477631 [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;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500373};
374
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500375struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400376 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500377 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500378 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500379 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700380 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500381 const char **entries;
382 uint32_t time;
383 int current;
384 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400385 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500386 menu_func_t func;
387};
388
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300389struct tooltip {
390 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300391 struct widget *widget;
392 char *entry;
393 struct task tooltip_task;
394 int tooltip_fd;
395 float x, y;
396};
397
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700398struct shm_pool {
399 struct wl_shm_pool *pool;
400 size_t size;
401 size_t used;
402 void *data;
403};
404
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400405enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300406 CURSOR_DEFAULT = 100,
407 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400408};
409
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200410static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400411
Pekka Paalanen97777442013-05-22 10:20:05 +0300412/* #define DEBUG */
413
414#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300415
416static void
417debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
418__attribute__ ((format (printf, 4, 5)));
419
420static void
421debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
422{
423 va_list ap;
424 struct timeval tv;
425
426 gettimeofday(&tv, NULL);
427 fprintf(stderr, "%8ld.%03ld ",
428 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
429
430 if (proxy)
431 fprintf(stderr, "%s@%d ",
432 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
433
434 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
435 fprintf(stderr, "%s ", func);
436
437 va_start(ap, fmt);
438 vfprintf(stderr, fmt, ap);
439 va_end(ap);
440}
441
442#define DBG(fmt, ...) \
443 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
444
445#define DBG_OBJ(obj, fmt, ...) \
446 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
447
448#else
449
450#define DBG(...) do {} while (0)
451#define DBG_OBJ(...) do {} while (0)
452
453#endif
454
Alexander Larsson1818e312013-05-22 14:41:31 +0200455static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200456surface_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 +0200457{
458 int32_t tmp;
459
460 switch (buffer_transform) {
461 case WL_OUTPUT_TRANSFORM_90:
462 case WL_OUTPUT_TRANSFORM_270:
463 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
464 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
465 tmp = *width;
466 *width = *height;
467 *height = tmp;
468 break;
469 default:
470 break;
471 }
472
473 *width *= buffer_scale;
474 *height *= buffer_scale;
475}
476
477static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200478buffer_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 +0200479{
480 int32_t tmp;
481
482 switch (buffer_transform) {
483 case WL_OUTPUT_TRANSFORM_90:
484 case WL_OUTPUT_TRANSFORM_270:
485 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
486 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
487 tmp = *width;
488 *width = *height;
489 *height = tmp;
490 break;
491 default:
492 break;
493 }
494
495 *width /= buffer_scale;
496 *height /= buffer_scale;
497}
498
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500499#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400500
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200501struct egl_window_surface {
502 struct toysurface base;
503 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100504 struct display *display;
505 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200506 struct wl_egl_window *egl_window;
507 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100508};
509
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200510static struct egl_window_surface *
511to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100512{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200513 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100514}
515
516static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200517egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200518 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200519 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100520{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200521 struct egl_window_surface *surface = to_egl_window_surface(base);
522
Alexander Larsson1818e312013-05-22 14:41:31 +0200523 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
524
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200525 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
526 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
527
528 return cairo_surface_reference(surface->cairo_surface);
529}
530
531static void
532egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200533 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 struct rectangle *server_allocation)
535{
536 struct egl_window_surface *surface = to_egl_window_surface(base);
537
538 cairo_gl_surface_swapbuffers(surface->cairo_surface);
539 wl_egl_window_get_attached_size(surface->egl_window,
540 &server_allocation->width,
541 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200542
543 buffer_to_surface_size (buffer_transform, buffer_scale,
544 &server_allocation->width,
545 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200546}
547
548static int
549egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
550{
551 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200552 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100553
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200554 device = cairo_surface_get_device(surface->cairo_surface);
555 if (!device)
556 return -1;
557
558 if (!ctx) {
559 if (device == surface->display->argb_device)
560 ctx = surface->display->argb_ctx;
561 else
562 assert(0);
563 }
564
565 cairo_device_flush(device);
566 cairo_device_acquire(device);
567 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
568 surface->egl_surface, ctx))
569 fprintf(stderr, "failed to make surface current\n");
570
571 return 0;
572}
573
574static void
575egl_window_surface_release(struct toysurface *base)
576{
577 struct egl_window_surface *surface = to_egl_window_surface(base);
578 cairo_device_t *device;
579
580 device = cairo_surface_get_device(surface->cairo_surface);
581 if (!device)
582 return;
583
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200584 if (!eglMakeCurrent(surface->display->dpy,
585 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200586 fprintf(stderr, "failed to make context current\n");
587
588 cairo_device_release(device);
589}
590
591static void
592egl_window_surface_destroy(struct toysurface *base)
593{
594 struct egl_window_surface *surface = to_egl_window_surface(base);
595 struct display *d = surface->display;
596
597 cairo_surface_destroy(surface->cairo_surface);
598 eglDestroySurface(d->dpy, surface->egl_surface);
599 wl_egl_window_destroy(surface->egl_window);
600 surface->surface = NULL;
601
602 free(surface);
603}
604
605static struct toysurface *
606egl_window_surface_create(struct display *display,
607 struct wl_surface *wl_surface,
608 uint32_t flags,
609 struct rectangle *rectangle)
610{
611 struct egl_window_surface *surface;
612
Pekka Paalanenb3627362012-11-19 17:16:00 +0200613 if (display->dpy == EGL_NO_DISPLAY)
614 return NULL;
615
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200616 surface = calloc(1, sizeof *surface);
617 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100618 return NULL;
619
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200620 surface->base.prepare = egl_window_surface_prepare;
621 surface->base.swap = egl_window_surface_swap;
622 surface->base.acquire = egl_window_surface_acquire;
623 surface->base.release = egl_window_surface_release;
624 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100625
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200626 surface->display = display;
627 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400628
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200629 surface->egl_window = wl_egl_window_create(surface->surface,
630 rectangle->width,
631 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface->egl_surface = eglCreateWindowSurface(display->dpy,
634 display->argb_config,
635 surface->egl_window,
636 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200638 surface->cairo_surface =
639 cairo_gl_surface_create_for_egl(display->argb_device,
640 surface->egl_surface,
641 rectangle->width,
642 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200644 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100645}
646
Pekka Paalanenb3627362012-11-19 17:16:00 +0200647#else
648
649static struct toysurface *
650egl_window_surface_create(struct display *display,
651 struct wl_surface *wl_surface,
652 uint32_t flags,
653 struct rectangle *rectangle)
654{
655 return NULL;
656}
657
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400658#endif
659
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200660struct shm_surface_data {
661 struct wl_buffer *buffer;
662 struct shm_pool *pool;
663};
664
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400665struct wl_buffer *
666display_get_buffer_for_surface(struct display *display,
667 cairo_surface_t *surface)
668{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200669 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200671 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400672
673 return data->buffer;
674}
675
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500676static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700677shm_pool_destroy(struct shm_pool *pool);
678
679static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400680shm_surface_data_destroy(void *p)
681{
682 struct shm_surface_data *data = p;
683
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200684 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700685 if (data->pool)
686 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300687
688 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400689}
690
Kristian Høgsberg16626282012-04-03 11:21:27 -0400691static struct wl_shm_pool *
692make_shm_pool(struct display *display, int size, void **data)
693{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400694 struct wl_shm_pool *pool;
695 int fd;
696
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300697 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400698 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300699 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
700 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400701 return NULL;
702 }
703
704 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400705 if (*data == MAP_FAILED) {
706 fprintf(stderr, "mmap failed: %m\n");
707 close(fd);
708 return NULL;
709 }
710
711 pool = wl_shm_create_pool(display->shm, fd, size);
712
713 close(fd);
714
715 return pool;
716}
717
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700718static struct shm_pool *
719shm_pool_create(struct display *display, size_t size)
720{
721 struct shm_pool *pool = malloc(sizeof *pool);
722
723 if (!pool)
724 return NULL;
725
726 pool->pool = make_shm_pool(display, size, &pool->data);
727 if (!pool->pool) {
728 free(pool);
729 return NULL;
730 }
731
732 pool->size = size;
733 pool->used = 0;
734
735 return pool;
736}
737
738static void *
739shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
740{
741 if (pool->used + size > pool->size)
742 return NULL;
743
744 *offset = pool->used;
745 pool->used += size;
746
747 return (char *) pool->data + *offset;
748}
749
750/* destroy the pool. this does not unmap the memory though */
751static void
752shm_pool_destroy(struct shm_pool *pool)
753{
754 munmap(pool->data, pool->size);
755 wl_shm_pool_destroy(pool->pool);
756 free(pool);
757}
758
759/* Start allocating from the beginning of the pool again */
760static void
761shm_pool_reset(struct shm_pool *pool)
762{
763 pool->used = 0;
764}
765
766static int
767data_length_for_shm_surface(struct rectangle *rect)
768{
769 int stride;
770
771 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
772 rect->width);
773 return stride * rect->height;
774}
775
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500776static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700777display_create_shm_surface_from_pool(struct display *display,
778 struct rectangle *rectangle,
779 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400780{
781 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400782 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400783 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200784 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700785 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400786 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400787
788 data = malloc(sizeof *data);
789 if (data == NULL)
790 return NULL;
791
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200792 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
793 cairo_format = CAIRO_FORMAT_RGB16_565;
794 else
795 cairo_format = CAIRO_FORMAT_ARGB32;
796
797 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700798 length = stride * rectangle->height;
799 data->pool = NULL;
800 map = shm_pool_allocate(pool, length, &offset);
801
802 if (!map) {
803 free(data);
804 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400805 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400806
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400807 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200808 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400809 rectangle->width,
810 rectangle->height,
811 stride);
812
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200813 cairo_surface_set_user_data(surface, &shm_surface_data_key,
814 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400815
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200816 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
817 format = WL_SHM_FORMAT_RGB565;
818 else {
819 if (flags & SURFACE_OPAQUE)
820 format = WL_SHM_FORMAT_XRGB8888;
821 else
822 format = WL_SHM_FORMAT_ARGB8888;
823 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200825 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
826 rectangle->width,
827 rectangle->height,
828 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400829
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700830 return surface;
831}
832
833static cairo_surface_t *
834display_create_shm_surface(struct display *display,
835 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200836 struct shm_pool *alternate_pool,
837 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700838{
839 struct shm_surface_data *data;
840 struct shm_pool *pool;
841 cairo_surface_t *surface;
842
Pekka Paalanen99436862012-11-19 17:15:59 +0200843 if (alternate_pool) {
844 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700845 surface = display_create_shm_surface_from_pool(display,
846 rectangle,
847 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200848 alternate_pool);
849 if (surface) {
850 data = cairo_surface_get_user_data(surface,
851 &shm_surface_data_key);
852 goto out;
853 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700854 }
855
856 pool = shm_pool_create(display,
857 data_length_for_shm_surface(rectangle));
858 if (!pool)
859 return NULL;
860
861 surface =
862 display_create_shm_surface_from_pool(display, rectangle,
863 flags, pool);
864
865 if (!surface) {
866 shm_pool_destroy(pool);
867 return NULL;
868 }
869
870 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200871 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700872 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400873
Pekka Paalanen99436862012-11-19 17:15:59 +0200874out:
875 if (data_ret)
876 *data_ret = data;
877
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400878 return surface;
879}
880
nobled7b87cb02011-02-01 18:51:47 +0000881static int
882check_size(struct rectangle *rect)
883{
884 if (rect->width && rect->height)
885 return 0;
886
887 fprintf(stderr, "tried to create surface of "
888 "width: %d, height: %d\n", rect->width, rect->height);
889 return -1;
890}
891
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400892cairo_surface_t *
893display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100894 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400895 struct rectangle *rectangle,
896 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400897{
nobled7b87cb02011-02-01 18:51:47 +0000898 if (check_size(rectangle) < 0)
899 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200900
901 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200902 return display_create_shm_surface(display, rectangle, flags,
903 NULL, NULL);
904}
905
Pekka Paalanena4eda732012-11-19 17:16:02 +0200906struct shm_surface_leaf {
907 cairo_surface_t *cairo_surface;
908 /* 'data' is automatically destroyed, when 'cairo_surface' is */
909 struct shm_surface_data *data;
910
911 struct shm_pool *resize_pool;
912 int busy;
913};
914
915static void
916shm_surface_leaf_release(struct shm_surface_leaf *leaf)
917{
918 if (leaf->cairo_surface)
919 cairo_surface_destroy(leaf->cairo_surface);
920 /* leaf->data already destroyed via cairo private */
921
922 if (leaf->resize_pool)
923 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200924
925 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200926}
927
Pekka Paalanenaef02542013-04-25 13:57:47 +0300928#define MAX_LEAVES 3
929
Pekka Paalanen99436862012-11-19 17:15:59 +0200930struct shm_surface {
931 struct toysurface base;
932 struct display *display;
933 struct wl_surface *surface;
934 uint32_t flags;
935 int dx, dy;
936
Pekka Paalanenaef02542013-04-25 13:57:47 +0300937 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200938 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200939};
940
941static struct shm_surface *
942to_shm_surface(struct toysurface *base)
943{
944 return container_of(base, struct shm_surface, base);
945}
946
Pekka Paalanena4eda732012-11-19 17:16:02 +0200947static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300948shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
949{
950#ifdef DEBUG
951 struct shm_surface_leaf *leaf;
952 char bufs[MAX_LEAVES + 1];
953 int i;
954
955 for (i = 0; i < MAX_LEAVES; i++) {
956 leaf = &surface->leaf[i];
957
958 if (leaf->busy)
959 bufs[i] = 'b';
960 else if (leaf->cairo_surface)
961 bufs[i] = 'a';
962 else
963 bufs[i] = ' ';
964 }
965
966 bufs[MAX_LEAVES] = '\0';
967 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
968#endif
969}
970
971static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200972shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
973{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200974 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300975 struct shm_surface_leaf *leaf;
976 int i;
977 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300978
979 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200980
Pekka Paalanenaef02542013-04-25 13:57:47 +0300981 for (i = 0; i < MAX_LEAVES; i++) {
982 leaf = &surface->leaf[i];
983 if (leaf->data && leaf->data->buffer == buffer) {
984 leaf->busy = 0;
985 break;
986 }
987 }
988 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200989
Pekka Paalanenaef02542013-04-25 13:57:47 +0300990 /* Leave one free leaf with storage, release others */
991 free_found = 0;
992 for (i = 0; i < MAX_LEAVES; i++) {
993 leaf = &surface->leaf[i];
994
995 if (!leaf->cairo_surface || leaf->busy)
996 continue;
997
998 if (!free_found)
999 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001000 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001001 shm_surface_leaf_release(leaf);
1002 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001003
Pekka Paalanen97777442013-05-22 10:20:05 +03001004 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001005}
1006
1007static const struct wl_buffer_listener shm_surface_buffer_listener = {
1008 shm_surface_buffer_release
1009};
1010
Pekka Paalanen99436862012-11-19 17:15:59 +02001011static cairo_surface_t *
1012shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001013 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001014 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001015{
Pekka Paalanenec076692012-11-30 13:37:27 +02001016 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001017 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001018 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001019 struct shm_surface_leaf *leaf = NULL;
1020 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001021
1022 surface->dx = dx;
1023 surface->dy = dy;
1024
Pekka Paalanenaef02542013-04-25 13:57:47 +03001025 /* pick a free buffer, preferrably one that already has storage */
1026 for (i = 0; i < MAX_LEAVES; i++) {
1027 if (surface->leaf[i].busy)
1028 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001029
Pekka Paalanenaef02542013-04-25 13:57:47 +03001030 if (!leaf || surface->leaf[i].cairo_surface)
1031 leaf = &surface->leaf[i];
1032 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001033 DBG_OBJ(surface->surface, "pick leaf %d\n",
1034 (int)(leaf - &surface->leaf[0]));
1035
Pekka Paalanenaef02542013-04-25 13:57:47 +03001036 if (!leaf) {
1037 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001038 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001039 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001040 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001041 }
1042
Pekka Paalanena4eda732012-11-19 17:16:02 +02001043 if (!resize_hint && leaf->resize_pool) {
1044 cairo_surface_destroy(leaf->cairo_surface);
1045 leaf->cairo_surface = NULL;
1046 shm_pool_destroy(leaf->resize_pool);
1047 leaf->resize_pool = NULL;
1048 }
1049
Alexander Larsson1818e312013-05-22 14:41:31 +02001050 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1051
Pekka Paalanena4eda732012-11-19 17:16:02 +02001052 if (leaf->cairo_surface &&
1053 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1054 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001055 goto out;
1056
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 if (leaf->cairo_surface)
1058 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001059
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001060#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001061 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001062 /* Create a big pool to allocate from, while continuously
1063 * resizing. Mmapping a new pool in the server
1064 * is relatively expensive, so reusing a pool performs
1065 * better, but may temporarily reserve unneeded memory.
1066 */
1067 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001068 leaf->resize_pool = shm_pool_create(surface->display,
1069 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001070 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001071#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001072
Alexander Larsson1818e312013-05-22 14:41:31 +02001073 rect.width = width;
1074 rect.height = height;
1075
Pekka Paalanena4eda732012-11-19 17:16:02 +02001076 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001077 display_create_shm_surface(surface->display, &rect,
1078 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001079 leaf->resize_pool,
1080 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001081 if (!leaf->cairo_surface)
1082 return NULL;
1083
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001085 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001086
1087out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001088 surface->current = leaf;
1089
1090 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001091}
1092
1093static void
1094shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001095 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001096 struct rectangle *server_allocation)
1097{
1098 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001099 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001100
1101 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001102 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001105
Alexander Larsson1818e312013-05-22 14:41:31 +02001106 buffer_to_surface_size (buffer_transform, buffer_scale,
1107 &server_allocation->width,
1108 &server_allocation->height);
1109
Pekka Paalanena4eda732012-11-19 17:16:02 +02001110 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001111 surface->dx, surface->dy);
1112 wl_surface_damage(surface->surface, 0, 0,
1113 server_allocation->width, server_allocation->height);
1114 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115
Pekka Paalanen71233882013-04-25 13:57:53 +03001116 DBG_OBJ(surface->surface, "leaf %d busy\n",
1117 (int)(leaf - &surface->leaf[0]));
1118
Pekka Paalanena4eda732012-11-19 17:16:02 +02001119 leaf->busy = 1;
1120 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001121}
1122
1123static int
1124shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1125{
1126 return -1;
1127}
1128
1129static void
1130shm_surface_release(struct toysurface *base)
1131{
1132}
1133
1134static void
1135shm_surface_destroy(struct toysurface *base)
1136{
1137 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001138 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001139
Pekka Paalanenaef02542013-04-25 13:57:47 +03001140 for (i = 0; i < MAX_LEAVES; i++)
1141 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001142
1143 free(surface);
1144}
1145
1146static struct toysurface *
1147shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1148 uint32_t flags, struct rectangle *rectangle)
1149{
1150 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001151 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001152
Brian Lovinbc919262013-08-07 15:34:59 -07001153 surface = xmalloc(sizeof *surface);
1154 memset(surface, 0, sizeof *surface);
1155
Pekka Paalanen99436862012-11-19 17:15:59 +02001156 if (!surface)
1157 return NULL;
1158
1159 surface->base.prepare = shm_surface_prepare;
1160 surface->base.swap = shm_surface_swap;
1161 surface->base.acquire = shm_surface_acquire;
1162 surface->base.release = shm_surface_release;
1163 surface->base.destroy = shm_surface_destroy;
1164
1165 surface->display = display;
1166 surface->surface = wl_surface;
1167 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001168
1169 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001170}
1171
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001172/*
1173 * The following correspondences between file names and cursors was copied
1174 * from: https://bugs.kde.org/attachment.cgi?id=67313
1175 */
1176
1177static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001178 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001179 "sw-resize",
1180 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001181};
1182
1183static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001184 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001185 "se-resize",
1186 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001187};
1188
1189static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001190 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001191 "s-resize",
1192 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001193};
1194
1195static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001196 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001197 "closedhand",
1198 "208530c400c041818281048008011002"
1199};
1200
1201static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001202 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001203 "default",
1204 "top_left_arrow",
1205 "left-arrow"
1206};
1207
1208static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001209 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001210 "w-resize",
1211 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001212};
1213
1214static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001215 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001216 "e-resize",
1217 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001218};
1219
1220static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001221 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001222 "nw-resize",
1223 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001224};
1225
1226static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001227 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001228 "ne-resize",
1229 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001230};
1231
1232static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001233 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001234 "n-resize",
1235 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001236};
1237
1238static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001239 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001240 "ibeam",
1241 "text"
1242};
1243
1244static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001245 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001246 "pointer",
1247 "pointing_hand",
1248 "e29285e634086352946a0e7090d73106"
1249};
1250
1251static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001252 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001253 "wait",
1254 "0426c94ea35c87780ff01dc239897213"
1255};
1256
1257struct cursor_alternatives {
1258 const char **names;
1259 size_t count;
1260};
1261
1262static const struct cursor_alternatives cursors[] = {
1263 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1264 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1265 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1266 {grabbings, ARRAY_LENGTH(grabbings)},
1267 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1268 {left_sides, ARRAY_LENGTH(left_sides)},
1269 {right_sides, ARRAY_LENGTH(right_sides)},
1270 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1271 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1272 {top_sides, ARRAY_LENGTH(top_sides)},
1273 {xterms, ARRAY_LENGTH(xterms)},
1274 {hand1s, ARRAY_LENGTH(hand1s)},
1275 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001276};
1277
1278static void
1279create_cursors(struct display *display)
1280{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001281 struct weston_config *config;
1282 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001283 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001284 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001285 unsigned int i, j;
1286 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001287
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001288 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001289 s = weston_config_get_section(config, "shell", NULL, NULL);
1290 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1291 weston_config_section_get_int(s, "cursor-size", &size, 32);
1292 weston_config_destroy(config);
1293
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001294 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001295 if (!display->cursor_theme) {
1296 fprintf(stderr, "could not load theme '%s'\n", theme);
1297 return;
1298 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001299 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001300 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001301 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001302
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001303 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001304 cursor = NULL;
1305 for (j = 0; !cursor && j < cursors[i].count; ++j)
1306 cursor = wl_cursor_theme_get_cursor(
1307 display->cursor_theme, cursors[i].names[j]);
1308
1309 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001310 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001311 cursors[i].names[0]);
1312
1313 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001314 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001315}
1316
1317static void
1318destroy_cursors(struct display *display)
1319{
1320 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001321 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001322}
1323
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001324struct wl_cursor_image *
1325display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001326{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001327 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001328
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001329 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001330}
1331
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001332static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001333surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001334{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001335 if (!surface->cairo_surface)
1336 return;
1337
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001338 if (surface->opaque_region) {
1339 wl_surface_set_opaque_region(surface->surface,
1340 surface->opaque_region);
1341 wl_region_destroy(surface->opaque_region);
1342 surface->opaque_region = NULL;
1343 }
1344
1345 if (surface->input_region) {
1346 wl_surface_set_input_region(surface->surface,
1347 surface->input_region);
1348 wl_region_destroy(surface->input_region);
1349 surface->input_region = NULL;
1350 }
1351
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001353 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001354 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001355
Pekka Paalanen89dee002013-02-13 16:17:20 +02001356 cairo_surface_destroy(surface->cairo_surface);
1357 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001358}
1359
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001360int
1361window_has_focus(struct window *window)
1362{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001363 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001364}
1365
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001366static void
1367window_close(struct window *window)
1368{
1369 if (window->close_handler)
1370 window->close_handler(window->user_data);
1371 else
1372 display_exit(window->display);
1373}
1374
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001375struct display *
1376window_get_display(struct window *window)
1377{
1378 return window->display;
1379}
1380
Pekka Paalanen89dee002013-02-13 16:17:20 +02001381static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001382surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001383{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001384 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001385 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001386
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001387 if (!surface->toysurface && display->dpy &&
1388 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001389 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001390 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001391 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001392 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001393 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001394 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001395
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001396 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001397 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001398 surface->surface,
1399 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001400
Pekka Paalanen89dee002013-02-13 16:17:20 +02001401 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001402 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001403 allocation.width, allocation.height, flags,
1404 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001405}
1406
1407static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001408window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001410 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001411 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001412
Pekka Paalanenec076692012-11-30 13:37:27 +02001413 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001414 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001415
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001416 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1417 flags |= SURFACE_HINT_RGB565;
1418
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001419 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001420}
1421
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001422int
1423window_get_buffer_transform(struct window *window)
1424{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001425 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001426}
1427
1428void
1429window_set_buffer_transform(struct window *window,
1430 enum wl_output_transform transform)
1431{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001432 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001433 wl_surface_set_buffer_transform(window->main_surface->surface,
1434 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001435}
1436
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001437void
1438window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001439 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001440{
1441 window->main_surface->buffer_scale = scale;
1442 wl_surface_set_buffer_scale(window->main_surface->surface,
1443 scale);
1444}
1445
1446uint32_t
1447window_get_buffer_scale(struct window *window)
1448{
1449 return window->main_surface->buffer_scale;
1450}
1451
Alexander Larssond68f5232013-05-22 14:41:33 +02001452uint32_t
1453window_get_output_scale(struct window *window)
1454{
1455 struct window_output *window_output;
1456 struct window_output *window_output_tmp;
1457 int scale = 1;
1458
1459 wl_list_for_each_safe(window_output, window_output_tmp,
1460 &window->window_output_list, link) {
1461 if (window_output->output->scale > scale)
1462 scale = window_output->output->scale;
1463 }
1464
1465 return scale;
1466}
1467
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001468static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001469
Pekka Paalanen4e373742013-02-13 16:17:13 +02001470static void
1471surface_destroy(struct surface *surface)
1472{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001473 if (surface->frame_cb)
1474 wl_callback_destroy(surface->frame_cb);
1475
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001476 if (surface->input_region)
1477 wl_region_destroy(surface->input_region);
1478
1479 if (surface->opaque_region)
1480 wl_region_destroy(surface->opaque_region);
1481
Pekka Paalanen35e82632013-04-25 13:57:48 +03001482 if (surface->subsurface)
1483 wl_subsurface_destroy(surface->subsurface);
1484
Pekka Paalanen4e373742013-02-13 16:17:13 +02001485 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001486
1487 if (surface->toysurface)
1488 surface->toysurface->destroy(surface->toysurface);
1489
Pekka Paalanen35e82632013-04-25 13:57:48 +03001490 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001491 free(surface);
1492}
1493
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001494void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001495window_destroy(struct window *window)
1496{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001497 struct display *display = window->display;
1498 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001499 struct window_output *window_output;
1500 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001501
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001502 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001503
Rusty Lynch1084da52013-08-15 09:10:08 -07001504 wl_list_for_each(input, &display->input_list, link) {
1505 if (input->touch_focus == window)
1506 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001507 if (input->pointer_focus == window)
1508 input->pointer_focus = NULL;
1509 if (input->keyboard_focus == window)
1510 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001511 if (input->focus_widget &&
1512 input->focus_widget->window == window)
1513 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001514 }
1515
Rob Bradford7507b572012-05-15 17:55:34 +01001516 wl_list_for_each_safe(window_output, window_output_tmp,
1517 &window->window_output_list, link) {
1518 free (window_output);
1519 }
1520
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001521 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001522 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001523
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001524 if (window->xdg_surface)
1525 xdg_surface_destroy(window->xdg_surface);
1526 if (window->xdg_popup)
1527 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001528
1529 surface_destroy(window->main_surface);
1530
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001531 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001532
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001533 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001534 free(window);
1535}
1536
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001537static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001538widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001539{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001540 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001541
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001542 wl_list_for_each(child, &widget->child_list, link) {
1543 target = widget_find_widget(child, x, y);
1544 if (target)
1545 return target;
1546 }
1547
1548 if (widget->allocation.x <= x &&
1549 x < widget->allocation.x + widget->allocation.width &&
1550 widget->allocation.y <= y &&
1551 y < widget->allocation.y + widget->allocation.height) {
1552 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001553 }
1554
1555 return NULL;
1556}
1557
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001558static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001559window_find_widget(struct window *window, int32_t x, int32_t y)
1560{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001561 struct surface *surface;
1562 struct widget *widget;
1563
1564 wl_list_for_each(surface, &window->subsurface_list, link) {
1565 widget = widget_find_widget(surface->widget, x, y);
1566 if (widget)
1567 return widget;
1568 }
1569
1570 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001571}
1572
1573static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001574widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001575{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001576 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001577
Peter Huttererf3d62272013-08-08 11:57:05 +10001578 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001579 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001580 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001581 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001582 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001583 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001584 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001585 widget->tooltip = NULL;
1586 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001587 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001588 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001589
1590 return widget;
1591}
1592
1593struct widget *
1594window_add_widget(struct window *window, void *data)
1595{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001596 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001597
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001598 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001599 wl_list_init(&widget->link);
1600 window->main_surface->widget = widget;
1601
1602 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001603}
1604
1605struct widget *
1606widget_add_widget(struct widget *parent, void *data)
1607{
1608 struct widget *widget;
1609
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001610 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001611 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001612
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001613 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001614}
1615
1616void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001617widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001618{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001619 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001620 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001621 struct input *input;
1622
Pekka Paalanen35e82632013-04-25 13:57:48 +03001623 /* Destroy the sub-surface along with the root widget */
1624 if (surface->widget == widget && surface->subsurface)
1625 surface_destroy(widget->surface);
1626
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001627 if (widget->tooltip)
1628 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001629
Pekka Paalanene156fb62012-01-19 13:51:38 +02001630 wl_list_for_each(input, &display->input_list, link) {
1631 if (input->focus_widget == widget)
1632 input->focus_widget = NULL;
1633 }
1634
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001635 wl_list_remove(&widget->link);
1636 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001637}
1638
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001639void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001640widget_set_default_cursor(struct widget *widget, int cursor)
1641{
1642 widget->default_cursor = cursor;
1643}
1644
1645void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001646widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001648 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001649}
1650
1651void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001652widget_set_size(struct widget *widget, int32_t width, int32_t height)
1653{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001654 widget->allocation.width = width;
1655 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001656}
1657
1658void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001659widget_set_allocation(struct widget *widget,
1660 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001661{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001662 widget->allocation.x = x;
1663 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001664 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001665}
1666
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001667void
1668widget_set_transparent(struct widget *widget, int transparent)
1669{
1670 widget->opaque = !transparent;
1671}
1672
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001673void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001674widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001675{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001677}
1678
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001679static cairo_surface_t *
1680widget_get_cairo_surface(struct widget *widget)
1681{
1682 struct surface *surface = widget->surface;
1683 struct window *window = widget->window;
1684
Neil Roberts97b747c2013-12-19 16:17:12 +00001685 assert(widget->use_cairo);
1686
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001687 if (!surface->cairo_surface) {
1688 if (surface == window->main_surface)
1689 window_create_main_surface(window);
1690 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001691 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001692 }
1693
1694 return surface->cairo_surface;
1695}
1696
Alexander Larsson15901f02013-05-22 14:41:25 +02001697static void
1698widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1699{
1700 struct surface *surface = widget->surface;
1701 double angle;
1702 cairo_matrix_t m;
1703 enum wl_output_transform transform;
1704 int surface_width, surface_height;
1705 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001706 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001707
1708 surface_width = surface->allocation.width;
1709 surface_height = surface->allocation.height;
1710
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001711 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001712 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001713
Alexander Larsson15901f02013-05-22 14:41:25 +02001714 switch (transform) {
1715 case WL_OUTPUT_TRANSFORM_FLIPPED:
1716 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1717 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1718 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1719 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1720 break;
1721 default:
1722 cairo_matrix_init_identity(&m);
1723 break;
1724 }
1725
1726 switch (transform) {
1727 case WL_OUTPUT_TRANSFORM_NORMAL:
1728 default:
1729 angle = 0;
1730 translate_x = 0;
1731 translate_y = 0;
1732 break;
1733 case WL_OUTPUT_TRANSFORM_FLIPPED:
1734 angle = 0;
1735 translate_x = surface_width;
1736 translate_y = 0;
1737 break;
1738 case WL_OUTPUT_TRANSFORM_90:
1739 angle = M_PI_2;
1740 translate_x = surface_height;
1741 translate_y = 0;
1742 break;
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1744 angle = M_PI_2;
1745 translate_x = surface_height;
1746 translate_y = surface_width;
1747 break;
1748 case WL_OUTPUT_TRANSFORM_180:
1749 angle = M_PI;
1750 translate_x = surface_width;
1751 translate_y = surface_height;
1752 break;
1753 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1754 angle = M_PI;
1755 translate_x = 0;
1756 translate_y = surface_height;
1757 break;
1758 case WL_OUTPUT_TRANSFORM_270:
1759 angle = M_PI + M_PI_2;
1760 translate_x = 0;
1761 translate_y = surface_width;
1762 break;
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1764 angle = M_PI + M_PI_2;
1765 translate_x = 0;
1766 translate_y = 0;
1767 break;
1768 }
1769
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001770 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001771 cairo_translate(cr, translate_x, translate_y);
1772 cairo_rotate(cr, angle);
1773 cairo_transform(cr, &m);
1774}
1775
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001776cairo_t *
1777widget_cairo_create(struct widget *widget)
1778{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001779 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001780 cairo_surface_t *cairo_surface;
1781 cairo_t *cr;
1782
1783 cairo_surface = widget_get_cairo_surface(widget);
1784 cr = cairo_create(cairo_surface);
1785
Alexander Larsson15901f02013-05-22 14:41:25 +02001786 widget_cairo_update_transform(widget, cr);
1787
Pekka Paalanen35e82632013-04-25 13:57:48 +03001788 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1789
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001790 return cr;
1791}
1792
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001793struct wl_surface *
1794widget_get_wl_surface(struct widget *widget)
1795{
1796 return widget->surface->surface;
1797}
1798
Neil Roberts5e10a042013-09-09 00:40:17 +01001799struct wl_subsurface *
1800widget_get_wl_subsurface(struct widget *widget)
1801{
1802 return widget->surface->subsurface;
1803}
1804
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001805uint32_t
1806widget_get_last_time(struct widget *widget)
1807{
1808 return widget->surface->last_time;
1809}
1810
1811void
1812widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1813{
1814 struct wl_compositor *comp = widget->window->display->compositor;
1815 struct surface *surface = widget->surface;
1816
1817 if (!surface->input_region)
1818 surface->input_region = wl_compositor_create_region(comp);
1819
1820 if (rect) {
1821 wl_region_add(surface->input_region,
1822 rect->x, rect->y, rect->width, rect->height);
1823 }
1824}
1825
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001826void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001827widget_set_resize_handler(struct widget *widget,
1828 widget_resize_handler_t handler)
1829{
1830 widget->resize_handler = handler;
1831}
1832
1833void
1834widget_set_redraw_handler(struct widget *widget,
1835 widget_redraw_handler_t handler)
1836{
1837 widget->redraw_handler = handler;
1838}
1839
1840void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001841widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001842{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001843 widget->enter_handler = handler;
1844}
1845
1846void
1847widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1848{
1849 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001850}
1851
1852void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001853widget_set_motion_handler(struct widget *widget,
1854 widget_motion_handler_t handler)
1855{
1856 widget->motion_handler = handler;
1857}
1858
1859void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001860widget_set_button_handler(struct widget *widget,
1861 widget_button_handler_t handler)
1862{
1863 widget->button_handler = handler;
1864}
1865
1866void
Rusty Lynch041815a2013-08-08 21:20:38 -07001867widget_set_touch_up_handler(struct widget *widget,
1868 widget_touch_up_handler_t handler)
1869{
1870 widget->touch_up_handler = handler;
1871}
1872
1873void
1874widget_set_touch_down_handler(struct widget *widget,
1875 widget_touch_down_handler_t handler)
1876{
1877 widget->touch_down_handler = handler;
1878}
1879
1880void
1881widget_set_touch_motion_handler(struct widget *widget,
1882 widget_touch_motion_handler_t handler)
1883{
1884 widget->touch_motion_handler = handler;
1885}
1886
1887void
1888widget_set_touch_frame_handler(struct widget *widget,
1889 widget_touch_frame_handler_t handler)
1890{
1891 widget->touch_frame_handler = handler;
1892}
1893
1894void
1895widget_set_touch_cancel_handler(struct widget *widget,
1896 widget_touch_cancel_handler_t handler)
1897{
1898 widget->touch_cancel_handler = handler;
1899}
1900
1901void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001902widget_set_axis_handler(struct widget *widget,
1903 widget_axis_handler_t handler)
1904{
1905 widget->axis_handler = handler;
1906}
1907
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001908static void
1909window_schedule_redraw_task(struct window *window);
1910
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001911void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001912widget_schedule_redraw(struct widget *widget)
1913{
Pekka Paalanen71233882013-04-25 13:57:53 +03001914 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001915 widget->surface->redraw_needed = 1;
1916 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001917}
1918
Neil Roberts97b747c2013-12-19 16:17:12 +00001919void
1920widget_set_use_cairo(struct widget *widget,
1921 int use_cairo)
1922{
1923 widget->use_cairo = use_cairo;
1924}
1925
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001926cairo_surface_t *
1927window_get_surface(struct window *window)
1928{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001929 cairo_surface_t *cairo_surface;
1930
1931 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1932
1933 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001934}
1935
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001936struct wl_surface *
1937window_get_wl_surface(struct window *window)
1938{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001939 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001940}
1941
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001942static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001943tooltip_redraw_handler(struct widget *widget, void *data)
1944{
1945 cairo_t *cr;
1946 const int32_t r = 3;
1947 struct tooltip *tooltip = data;
1948 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001949
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001950 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001951 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001952 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1953 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1954 cairo_paint(cr);
1955
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001956 width = widget->allocation.width;
1957 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001958 rounded_rect(cr, 0, 0, width, height, r);
1959
1960 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1961 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1962 cairo_fill(cr);
1963
1964 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1965 cairo_move_to(cr, 10, 16);
1966 cairo_show_text(cr, tooltip->entry);
1967 cairo_destroy(cr);
1968}
1969
1970static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001971get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001972{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001973 cairo_t *cr;
1974 cairo_text_extents_t extents;
1975
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001976 /* Use the dummy_surface because tooltip's surface was not
1977 * created yet, and parent does not have a valid surface
1978 * outside repaint, either.
1979 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001980 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001981 cairo_text_extents(cr, tooltip->entry, &extents);
1982 cairo_destroy(cr);
1983
1984 return extents;
1985}
1986
1987static int
1988window_create_tooltip(struct tooltip *tooltip)
1989{
1990 struct widget *parent = tooltip->parent;
1991 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001992 const int offset_y = 27;
1993 const int margin = 3;
1994 cairo_text_extents_t extents;
1995
1996 if (tooltip->widget)
1997 return 0;
1998
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001999 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002000
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002001 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002002 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002003 widget_set_allocation(tooltip->widget,
2004 tooltip->x, tooltip->y + offset_y,
2005 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002006
2007 return 0;
2008}
2009
2010void
2011widget_destroy_tooltip(struct widget *parent)
2012{
2013 struct tooltip *tooltip = parent->tooltip;
2014
2015 parent->tooltip_count = 0;
2016 if (!tooltip)
2017 return;
2018
2019 if (tooltip->widget) {
2020 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002021 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002022 }
2023
2024 close(tooltip->tooltip_fd);
2025 free(tooltip->entry);
2026 free(tooltip);
2027 parent->tooltip = NULL;
2028}
2029
2030static void
2031tooltip_func(struct task *task, uint32_t events)
2032{
2033 struct tooltip *tooltip =
2034 container_of(task, struct tooltip, tooltip_task);
2035 uint64_t exp;
2036
Martin Olsson8df662a2012-07-08 03:03:47 +02002037 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2038 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002039 window_create_tooltip(tooltip);
2040}
2041
2042#define TOOLTIP_TIMEOUT 500
2043static int
2044tooltip_timer_reset(struct tooltip *tooltip)
2045{
2046 struct itimerspec its;
2047
2048 its.it_interval.tv_sec = 0;
2049 its.it_interval.tv_nsec = 0;
2050 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2051 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2052 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2053 fprintf(stderr, "could not set timerfd\n: %m");
2054 return -1;
2055 }
2056
2057 return 0;
2058}
2059
2060int
2061widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2062{
2063 struct tooltip *tooltip = parent->tooltip;
2064
2065 parent->tooltip_count++;
2066 if (tooltip) {
2067 tooltip->x = x;
2068 tooltip->y = y;
2069 tooltip_timer_reset(tooltip);
2070 return 0;
2071 }
2072
2073 /* the handler might be triggered too fast via input device motion, so
2074 * we need this check here to make sure tooltip is fully initialized */
2075 if (parent->tooltip_count > 1)
2076 return 0;
2077
2078 tooltip = malloc(sizeof *tooltip);
2079 if (!tooltip)
2080 return -1;
2081
2082 parent->tooltip = tooltip;
2083 tooltip->parent = parent;
2084 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002085 tooltip->x = x;
2086 tooltip->y = y;
2087 tooltip->entry = strdup(entry);
2088 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2089 if (tooltip->tooltip_fd < 0) {
2090 fprintf(stderr, "could not create timerfd\n: %m");
2091 return -1;
2092 }
2093
2094 tooltip->tooltip_task.run = tooltip_func;
2095 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2096 EPOLLIN, &tooltip->tooltip_task);
2097 tooltip_timer_reset(tooltip);
2098
2099 return 0;
2100}
2101
2102static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002103workspace_manager_state(void *data,
2104 struct workspace_manager *workspace_manager,
2105 uint32_t current,
2106 uint32_t count)
2107{
2108 struct display *display = data;
2109
2110 display->workspace = current;
2111 display->workspace_count = count;
2112}
2113
2114static const struct workspace_manager_listener workspace_manager_listener = {
2115 workspace_manager_state
2116};
2117
2118static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002119frame_resize_handler(struct widget *widget,
2120 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002121{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002122 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002123 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002124 struct rectangle interior;
2125 struct rectangle input;
2126 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002127
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002128 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002129 interior.x = 0;
2130 interior.y = 0;
2131 interior.width = width;
2132 interior.height = height;
2133 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002134 frame_resize(frame->frame, width, height);
2135 frame_interior(frame->frame, &interior.x, &interior.y,
2136 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002137 }
2138
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002139 widget_set_allocation(child, interior.x, interior.y,
2140 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002141
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002142 if (child->resize_handler) {
2143 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002144 child->user_data);
2145
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002146 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002147 width = child->allocation.width;
2148 height = child->allocation.height;
2149 } else {
2150 frame_resize_inside(frame->frame,
2151 child->allocation.width,
2152 child->allocation.height);
2153 width = frame_width(frame->frame);
2154 height = frame_height(frame->frame);
2155 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002156 }
2157
Scott Moreauf7e498c2012-05-14 11:39:29 -06002158 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002159
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002160 widget->surface->input_region =
2161 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002162 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002163 frame_input_rect(frame->frame, &input.x, &input.y,
2164 &input.width, &input.height);
2165 wl_region_add(widget->surface->input_region,
2166 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002167 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002168 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002169 }
2170
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002171 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002172
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002173 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002174 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002175 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2176 &opaque.width, &opaque.height);
2177
2178 wl_region_add(widget->surface->opaque_region,
2179 opaque.x, opaque.y,
2180 opaque.width, opaque.height);
2181 } else {
2182 wl_region_add(widget->surface->opaque_region,
2183 0, 0, width, height);
2184 }
Martin Minarik1998b152012-05-10 02:04:35 +02002185 }
2186
Martin Minarik1998b152012-05-10 02:04:35 +02002187
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002188 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002189}
2190
2191static void
2192frame_redraw_handler(struct widget *widget, void *data)
2193{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002195 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002196 struct window *window = widget->window;
2197
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002198 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002199 return;
2200
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002201 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002202
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002203 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002204
2205 cairo_destroy(cr);
2206}
2207
2208static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002209frame_get_pointer_image_for_location(struct window_frame *frame,
2210 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002211{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002212 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002213
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002214 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002215 return CURSOR_LEFT_PTR;
2216
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002217 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002218 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002219 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002220 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002221 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002222 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002223 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002224 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002225 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002226 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002227 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002228 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002229 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002230 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002231 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002232 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002233 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002234 case THEME_LOCATION_EXTERIOR:
2235 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002236 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002237 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002238 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002239}
2240
Pekka Paalanen26237862014-09-10 15:10:30 +03002241static void
2242frame_menu_func(void *data, struct input *input, int index)
2243{
2244 struct window *window = data;
2245 struct display *display;
2246
2247 switch (index) {
2248 case 0: /* close */
2249 window_close(window);
2250 break;
2251 case 1: /* move to workspace above */
2252 display = window->display;
2253 if (display->workspace > 0)
2254 workspace_manager_move_surface(
2255 display->workspace_manager,
2256 window->main_surface->surface,
2257 display->workspace - 1);
2258 break;
2259 case 2: /* move to workspace below */
2260 display = window->display;
2261 if (display->workspace < display->workspace_count - 1)
2262 workspace_manager_move_surface(
2263 display->workspace_manager,
2264 window->main_surface->surface,
2265 display->workspace + 1);
2266 break;
2267 case 3: /* fullscreen */
2268 /* we don't have a way to get out of fullscreen for now */
2269 if (window->fullscreen_handler)
2270 window->fullscreen_handler(window, window->user_data);
2271 break;
2272 }
2273}
2274
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002275void
2276window_show_frame_menu(struct window *window,
2277 struct input *input, uint32_t time)
2278{
2279 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002280 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002281
Pekka Paalanen26237862014-09-10 15:10:30 +03002282 static const char *entries[] = {
2283 "Close",
2284 "Move to workspace above", "Move to workspace below",
2285 "Fullscreen"
2286 };
2287
2288 if (window->fullscreen_handler)
2289 count = ARRAY_LENGTH(entries);
2290 else
2291 count = ARRAY_LENGTH(entries) - 1;
2292
2293 input_get_position(input, &x, &y);
2294 window_show_menu(window->display, input, time, window,
2295 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002296}
2297
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002298static int
2299frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002300 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002301{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002302 struct window_frame *frame = data;
2303 enum theme_location location;
2304
2305 location = frame_pointer_enter(frame->frame, input, x, y);
2306 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2307 widget_schedule_redraw(frame->widget);
2308
2309 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002310}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002311
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002312static int
2313frame_motion_handler(struct widget *widget,
2314 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002315 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002316{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002317 struct window_frame *frame = data;
2318 enum theme_location location;
2319
2320 location = frame_pointer_motion(frame->frame, input, x, y);
2321 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2322 widget_schedule_redraw(frame->widget);
2323
2324 return frame_get_pointer_image_for_location(data, location);
2325}
2326
2327static void
2328frame_leave_handler(struct widget *widget,
2329 struct input *input, void *data)
2330{
2331 struct window_frame *frame = data;
2332
2333 frame_pointer_leave(frame->frame, input);
2334 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2335 widget_schedule_redraw(frame->widget);
2336}
2337
2338static void
2339frame_handle_status(struct window_frame *frame, struct input *input,
2340 uint32_t time, enum theme_location location)
2341{
2342 struct window *window = frame->widget->window;
2343 uint32_t status;
2344
2345 status = frame_status(frame->frame);
2346 if (status & FRAME_STATUS_REPAINT)
2347 widget_schedule_redraw(frame->widget);
2348
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002349 if (status & FRAME_STATUS_MINIMIZE) {
2350 window_set_minimized(window);
2351 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2352 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002353
2354 if (status & FRAME_STATUS_MENU) {
2355 window_show_frame_menu(window, input, time);
2356 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2357 }
2358
2359 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002360 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002361 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2362 }
2363
2364 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002365 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002366 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002367 }
2368
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002369 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002370 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002371 xdg_surface_move(window->xdg_surface,
2372 input_get_seat(input),
2373 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002374
2375 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2376 }
2377
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002378 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002379 input_ungrab(input);
2380
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002381 xdg_surface_resize(window->xdg_surface,
2382 input_get_seat(input),
2383 window->display->serial,
2384 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002385
2386 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2387 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002388}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002389
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002390#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002391static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002392frame_button_handler(struct widget *widget,
2393 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002394 uint32_t button, enum wl_pointer_button_state state,
2395 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002396
2397{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002398 struct window_frame *frame = data;
2399 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002400
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002401 frame->double_click = 0;
2402 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2403 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2404 frame->double_click = 1;
2405 frame->did_double = 1;
2406 } else
2407 frame->did_double = 0;
2408
2409 frame->last_time = time;
2410 } else if (frame->did_double == 1) {
2411 frame->double_click = 1;
2412 frame->did_double = 0;
2413 }
2414
2415 if (frame->double_click)
2416 location = frame_double_click(frame->frame, input,
2417 button, state);
2418 else
2419 location = frame_pointer_button(frame->frame, input,
2420 button, state);
2421
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002422 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002423}
2424
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002425static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002426frame_touch_down_handler(struct widget *widget, struct input *input,
2427 uint32_t serial, uint32_t time, int32_t id,
2428 float x, float y, void *data)
2429{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002430 struct window_frame *frame = data;
2431
2432 frame_touch_down(frame->frame, input, id, x, y);
2433 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2434}
2435
2436static void
2437frame_touch_up_handler(struct widget *widget,
2438 struct input *input, uint32_t serial, uint32_t time,
2439 int32_t id, void *data)
2440{
2441 struct window_frame *frame = data;
2442
2443 frame_touch_up(frame->frame, input, id);
2444 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002445}
2446
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002447struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002448window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002449{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002450 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002451 uint32_t buttons;
2452
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002453 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002454 buttons = FRAME_BUTTON_NONE;
2455 } else {
2456 buttons = FRAME_BUTTON_ALL;
2457 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002458
Peter Huttererf3d62272013-08-08 11:57:05 +10002459 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002461 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002462
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002463 frame->widget = window_add_widget(window, frame);
2464 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002465
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002466 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2467 widget_set_resize_handler(frame->widget, frame_resize_handler);
2468 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002469 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002470 widget_set_motion_handler(frame->widget, frame_motion_handler);
2471 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002472 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002473 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002474
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002475 window->frame = frame;
2476
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002477 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002478}
2479
Kristian Høgsberga1627922012-06-20 17:30:03 -04002480void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002481window_frame_set_child_size(struct widget *widget, int child_width,
2482 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002483{
2484 struct display *display = widget->window->display;
2485 struct theme *t = display->theme;
2486 int decoration_width, decoration_height;
2487 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002488 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002489
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002490 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002491 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002492 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002493 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002494
2495 width = child_width + decoration_width;
2496 height = child_height + decoration_height;
2497 } else {
2498 width = child_width;
2499 height = child_height;
2500 }
2501
2502 window_schedule_resize(widget->window, width, height);
2503}
2504
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002505static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002506window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002507{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002508 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002509
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002510 /* frame->child must be destroyed by the application */
2511 widget_destroy(frame->widget);
2512 free(frame);
2513}
2514
2515static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002516input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002517 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002518{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002519 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002520 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002521
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002522 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002523 return;
2524
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002525 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002526 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002527 widget = old;
2528 if (input->grab)
2529 widget = input->grab;
2530 if (widget->leave_handler)
2531 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002532 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002533 }
2534
2535 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002536 widget = focus;
2537 if (input->grab)
2538 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002539 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002540 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002541 cursor = widget->enter_handler(focus, input, x, y,
2542 widget->user_data);
2543 else
2544 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002545
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002546 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002547 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002548}
2549
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002550void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002551touch_grab(struct input *input, int32_t touch_id)
2552{
2553 input->touch_grab = 1;
2554 input->touch_grab_id = touch_id;
2555}
2556
2557void
2558touch_ungrab(struct input *input)
2559{
2560 struct touch_point *tp, *tmp;
2561
2562 input->touch_grab = 0;
2563
2564 wl_list_for_each_safe(tp, tmp,
2565 &input->touch_point_list, link) {
2566 if (tp->id != input->touch_grab_id)
2567 continue;
2568 wl_list_remove(&tp->link);
2569 free(tp);
2570
2571 return;
2572 }
2573}
2574
2575void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002576input_grab(struct input *input, struct widget *widget, uint32_t button)
2577{
2578 input->grab = widget;
2579 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002580
2581 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002582}
2583
2584void
2585input_ungrab(struct input *input)
2586{
2587 struct widget *widget;
2588
2589 input->grab = NULL;
2590 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002591 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002592 input->sx, input->sy);
2593 input_set_focus_widget(input, widget, input->sx, input->sy);
2594 }
2595}
2596
2597static void
2598input_remove_pointer_focus(struct input *input)
2599{
2600 struct window *window = input->pointer_focus;
2601
2602 if (!window)
2603 return;
2604
2605 input_set_focus_widget(input, NULL, 0, 0);
2606
2607 input->pointer_focus = NULL;
2608 input->current_cursor = CURSOR_UNSET;
2609}
2610
2611static void
2612pointer_handle_enter(void *data, struct wl_pointer *pointer,
2613 uint32_t serial, struct wl_surface *surface,
2614 wl_fixed_t sx_w, wl_fixed_t sy_w)
2615{
2616 struct input *input = data;
2617 struct window *window;
2618 struct widget *widget;
2619 float sx = wl_fixed_to_double(sx_w);
2620 float sy = wl_fixed_to_double(sy_w);
2621
2622 if (!surface) {
2623 /* enter event for a window we've just destroyed */
2624 return;
2625 }
2626
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002627 window = wl_surface_get_user_data(surface);
2628 if (surface != window->main_surface->surface) {
2629 DBG("Ignoring input event from subsurface %p\n", surface);
2630 return;
2631 }
2632
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002633 input->display->serial = serial;
2634 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002635 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002636
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002637 input->sx = sx;
2638 input->sy = sy;
2639
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002640 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002641 input_set_focus_widget(input, widget, sx, sy);
2642}
2643
2644static void
2645pointer_handle_leave(void *data, struct wl_pointer *pointer,
2646 uint32_t serial, struct wl_surface *surface)
2647{
2648 struct input *input = data;
2649
2650 input->display->serial = serial;
2651 input_remove_pointer_focus(input);
2652}
2653
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002654static void
Daniel Stone37816df2012-05-16 18:45:18 +01002655pointer_handle_motion(void *data, struct wl_pointer *pointer,
2656 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002657{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002658 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002659 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002660 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002661 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002662 float sx = wl_fixed_to_double(sx_w);
2663 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002664
Paul Winwoodb22bf572013-08-29 10:52:54 +01002665 if (!window)
2666 return;
2667
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002668 input->sx = sx;
2669 input->sy = sy;
2670
Rob Bradford5f087742013-07-11 19:41:27 +01002671 /* when making the window smaller - e.g. after a unmaximise we might
2672 * still have a pending motion event that the compositor has picked
2673 * based on the old surface dimensions
2674 */
2675 if (sx > window->main_surface->allocation.width ||
2676 sy > window->main_surface->allocation.height)
2677 return;
2678
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002679 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002680 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002681 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002682 }
2683
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002684 if (input->grab)
2685 widget = input->grab;
2686 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002687 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002688 if (widget) {
2689 if (widget->motion_handler)
2690 cursor = widget->motion_handler(input->focus_widget,
2691 input, time, sx, sy,
2692 widget->user_data);
2693 else
2694 cursor = widget->default_cursor;
2695 } else
2696 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002697
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002698 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002699}
2700
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002701static void
Daniel Stone37816df2012-05-16 18:45:18 +01002702pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002703 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002704{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002705 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002706 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002707 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002708
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002709 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002710 if (input->focus_widget && input->grab == NULL &&
2711 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002712 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002713
Neil Roberts6b28aad2012-01-23 19:11:18 +00002714 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002715 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002716 (*widget->button_handler)(widget,
2717 input, time,
2718 button, state,
2719 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002720
Daniel Stone4dbadb12012-05-30 16:31:51 +01002721 if (input->grab && input->grab_button == button &&
2722 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002723 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002724}
2725
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002726static void
Daniel Stone37816df2012-05-16 18:45:18 +01002727pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002728 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002729{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002730 struct input *input = data;
2731 struct widget *widget;
2732
2733 widget = input->focus_widget;
2734 if (input->grab)
2735 widget = input->grab;
2736 if (widget && widget->axis_handler)
2737 (*widget->axis_handler)(widget,
2738 input, time,
2739 axis, value,
2740 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002741}
2742
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002743static const struct wl_pointer_listener pointer_listener = {
2744 pointer_handle_enter,
2745 pointer_handle_leave,
2746 pointer_handle_motion,
2747 pointer_handle_button,
2748 pointer_handle_axis,
2749};
2750
2751static void
2752input_remove_keyboard_focus(struct input *input)
2753{
2754 struct window *window = input->keyboard_focus;
2755 struct itimerspec its;
2756
2757 its.it_interval.tv_sec = 0;
2758 its.it_interval.tv_nsec = 0;
2759 its.it_value.tv_sec = 0;
2760 its.it_value.tv_nsec = 0;
2761 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2762
2763 if (!window)
2764 return;
2765
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002766 if (window->keyboard_focus_handler)
2767 (*window->keyboard_focus_handler)(window, NULL,
2768 window->user_data);
2769
2770 input->keyboard_focus = NULL;
2771}
2772
2773static void
2774keyboard_repeat_func(struct task *task, uint32_t events)
2775{
2776 struct input *input =
2777 container_of(task, struct input, repeat_task);
2778 struct window *window = input->keyboard_focus;
2779 uint64_t exp;
2780
2781 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2782 /* If we change the timer between the fd becoming
2783 * readable and getting here, there'll be nothing to
2784 * read and we get EAGAIN. */
2785 return;
2786
2787 if (window && window->key_handler) {
2788 (*window->key_handler)(window, input, input->repeat_time,
2789 input->repeat_key, input->repeat_sym,
2790 WL_KEYBOARD_KEY_STATE_PRESSED,
2791 window->user_data);
2792 }
2793}
2794
2795static void
2796keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2797 uint32_t format, int fd, uint32_t size)
2798{
2799 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002800 struct xkb_keymap *keymap;
2801 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002802 char *map_str;
2803
2804 if (!data) {
2805 close(fd);
2806 return;
2807 }
2808
2809 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2810 close(fd);
2811 return;
2812 }
2813
2814 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2815 if (map_str == MAP_FAILED) {
2816 close(fd);
2817 return;
2818 }
2819
Ran Benita2e1968f2014-08-19 23:59:51 +03002820 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2821 map_str,
2822 XKB_KEYMAP_FORMAT_TEXT_V1,
2823 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002824 munmap(map_str, size);
2825 close(fd);
2826
Rui Matos3eccb862013-10-10 19:44:22 +02002827 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002828 fprintf(stderr, "failed to compile keymap\n");
2829 return;
2830 }
2831
Rui Matos3eccb862013-10-10 19:44:22 +02002832 state = xkb_state_new(keymap);
2833 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002834 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002835 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002836 return;
2837 }
2838
Rui Matos3eccb862013-10-10 19:44:22 +02002839 xkb_keymap_unref(input->xkb.keymap);
2840 xkb_state_unref(input->xkb.state);
2841 input->xkb.keymap = keymap;
2842 input->xkb.state = state;
2843
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002844 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002845 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002846 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002847 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002848 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002849 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002850}
2851
2852static void
2853keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2854 uint32_t serial, struct wl_surface *surface,
2855 struct wl_array *keys)
2856{
2857 struct input *input = data;
2858 struct window *window;
2859
2860 input->display->serial = serial;
2861 input->keyboard_focus = wl_surface_get_user_data(surface);
2862
2863 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002864 if (window->keyboard_focus_handler)
2865 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002866 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002867}
2868
2869static void
2870keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2871 uint32_t serial, struct wl_surface *surface)
2872{
2873 struct input *input = data;
2874
2875 input->display->serial = serial;
2876 input_remove_keyboard_focus(input);
2877}
2878
Scott Moreau210d0792012-03-22 10:47:01 -06002879static void
Daniel Stone37816df2012-05-16 18:45:18 +01002880keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002881 uint32_t serial, uint32_t time, uint32_t key,
2882 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002883{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002884 struct input *input = data;
2885 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002886 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002887 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002888 const xkb_keysym_t *syms;
2889 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002890 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002891
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002892 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002893 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002894 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002895 return;
2896
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002897 /* We only use input grabs for pointer events for now, so just
2898 * ignore key presses if a grab is active. We expand the key
2899 * event delivery mechanism to route events to widgets to
2900 * properly handle key grabs. In the meantime, this prevents
2901 * key event devlivery while a grab is active. */
2902 if (input->grab && input->grab_button == 0)
2903 return;
2904
Ran Benita2e1968f2014-08-19 23:59:51 +03002905 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002906
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002907 sym = XKB_KEY_NoSymbol;
2908 if (num_syms == 1)
2909 sym = syms[0];
2910
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002911
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002912 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002913 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002914 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002915 } else if (sym == XKB_KEY_F11 &&
2916 window->fullscreen_handler &&
2917 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2918 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002919 } else if (sym == XKB_KEY_F4 &&
2920 input->modifiers == MOD_ALT_MASK &&
2921 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002922 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002923 } else if (window->key_handler) {
2924 (*window->key_handler)(window, input, time, key,
2925 sym, state, window->user_data);
2926 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002927
2928 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2929 key == input->repeat_key) {
2930 its.it_interval.tv_sec = 0;
2931 its.it_interval.tv_nsec = 0;
2932 its.it_value.tv_sec = 0;
2933 its.it_value.tv_nsec = 0;
2934 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002935 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2936 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002937 input->repeat_sym = sym;
2938 input->repeat_key = key;
2939 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002940 its.it_interval.tv_sec = input->repeat_rate_sec;
2941 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2942 its.it_value.tv_sec = input->repeat_delay_sec;
2943 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002944 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2945 }
2946}
2947
2948static void
Daniel Stone351eb612012-05-31 15:27:47 -04002949keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2950 uint32_t serial, uint32_t mods_depressed,
2951 uint32_t mods_latched, uint32_t mods_locked,
2952 uint32_t group)
2953{
2954 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002955 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002956
Matt Ropere61561f2013-06-24 16:52:43 +01002957 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2958 if (!input->xkb.keymap)
2959 return;
2960
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002961 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2962 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002963 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002964 XKB_STATE_MODS_DEPRESSED |
2965 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002966 input->modifiers = 0;
2967 if (mask & input->xkb.control_mask)
2968 input->modifiers |= MOD_CONTROL_MASK;
2969 if (mask & input->xkb.alt_mask)
2970 input->modifiers |= MOD_ALT_MASK;
2971 if (mask & input->xkb.shift_mask)
2972 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002973}
2974
Jonny Lamb06959082014-08-12 14:58:27 +02002975static void
2976set_repeat_info(struct input *input, int32_t rate, int32_t delay)
2977{
2978 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
2979 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
2980
2981 /* a rate of zero disables any repeating, regardless of the delay's
2982 * value */
2983 if (rate == 0)
2984 return;
2985
2986 if (rate == 1)
2987 input->repeat_rate_sec = 1;
2988 else
2989 input->repeat_rate_nsec = 1000000000 / rate;
2990
2991 input->repeat_delay_sec = delay / 1000;
2992 delay -= (input->repeat_delay_sec * 1000);
2993 input->repeat_delay_nsec = delay * 1000 * 1000;
2994}
2995
2996static void
2997keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
2998 int32_t rate, int32_t delay)
2999{
3000 struct input *input = data;
3001
3002 set_repeat_info(input, rate, delay);
3003}
3004
Daniel Stone37816df2012-05-16 18:45:18 +01003005static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003006 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003007 keyboard_handle_enter,
3008 keyboard_handle_leave,
3009 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003010 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003011 keyboard_handle_repeat_info
3012
Daniel Stone37816df2012-05-16 18:45:18 +01003013};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003014
3015static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003016touch_handle_down(void *data, struct wl_touch *wl_touch,
3017 uint32_t serial, uint32_t time, struct wl_surface *surface,
3018 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3019{
3020 struct input *input = data;
3021 struct widget *widget;
3022 float sx = wl_fixed_to_double(x_w);
3023 float sy = wl_fixed_to_double(y_w);
3024
Rusty Lynch1084da52013-08-15 09:10:08 -07003025 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003026 input->touch_focus = wl_surface_get_user_data(surface);
3027 if (!input->touch_focus) {
3028 DBG("Failed to find to touch focus for surface %p\n", surface);
3029 return;
3030 }
3031
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003032 if (surface != input->touch_focus->main_surface->surface) {
3033 DBG("Ignoring input event from subsurface %p\n", surface);
3034 input->touch_focus = NULL;
3035 return;
3036 }
3037
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003038 if (input->grab)
3039 widget = input->grab;
3040 else
3041 widget = window_find_widget(input->touch_focus,
3042 wl_fixed_to_double(x_w),
3043 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003044 if (widget) {
3045 struct touch_point *tp = xmalloc(sizeof *tp);
3046 if (tp) {
3047 tp->id = id;
3048 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003049 tp->x = sx;
3050 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003051 wl_list_insert(&input->touch_point_list, &tp->link);
3052
3053 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003054 (*widget->touch_down_handler)(widget, input,
3055 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003056 sx, sy,
3057 widget->user_data);
3058 }
3059 }
3060}
3061
3062static void
3063touch_handle_up(void *data, struct wl_touch *wl_touch,
3064 uint32_t serial, uint32_t time, int32_t id)
3065{
3066 struct input *input = data;
3067 struct touch_point *tp, *tmp;
3068
Rusty Lynch041815a2013-08-08 21:20:38 -07003069 if (!input->touch_focus) {
3070 DBG("No touch focus found for touch up event!\n");
3071 return;
3072 }
3073
3074 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3075 if (tp->id != id)
3076 continue;
3077
3078 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003079 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003080 time, id,
3081 tp->widget->user_data);
3082
3083 wl_list_remove(&tp->link);
3084 free(tp);
3085
3086 return;
3087 }
3088}
3089
3090static void
3091touch_handle_motion(void *data, struct wl_touch *wl_touch,
3092 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3093{
3094 struct input *input = data;
3095 struct touch_point *tp;
3096 float sx = wl_fixed_to_double(x_w);
3097 float sy = wl_fixed_to_double(y_w);
3098
3099 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3100
3101 if (!input->touch_focus) {
3102 DBG("No touch focus found for touch motion event!\n");
3103 return;
3104 }
3105
3106 wl_list_for_each(tp, &input->touch_point_list, link) {
3107 if (tp->id != id)
3108 continue;
3109
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003110 tp->x = sx;
3111 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003112 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003113 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003114 id, sx, sy,
3115 tp->widget->user_data);
3116 return;
3117 }
3118}
3119
3120static void
3121touch_handle_frame(void *data, struct wl_touch *wl_touch)
3122{
3123 struct input *input = data;
3124 struct touch_point *tp, *tmp;
3125
3126 DBG("touch_handle_frame\n");
3127
3128 if (!input->touch_focus) {
3129 DBG("No touch focus found for touch frame event!\n");
3130 return;
3131 }
3132
3133 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3134 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003135 (*tp->widget->touch_frame_handler)(tp->widget, input,
3136 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003137 }
3138}
3139
3140static void
3141touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3142{
3143 struct input *input = data;
3144 struct touch_point *tp, *tmp;
3145
3146 DBG("touch_handle_cancel\n");
3147
3148 if (!input->touch_focus) {
3149 DBG("No touch focus found for touch cancel event!\n");
3150 return;
3151 }
3152
3153 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3154 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003155 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3156 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003157
3158 wl_list_remove(&tp->link);
3159 free(tp);
3160 }
3161}
3162
3163static const struct wl_touch_listener touch_listener = {
3164 touch_handle_down,
3165 touch_handle_up,
3166 touch_handle_motion,
3167 touch_handle_frame,
3168 touch_handle_cancel,
3169};
3170
3171static void
Daniel Stone37816df2012-05-16 18:45:18 +01003172seat_handle_capabilities(void *data, struct wl_seat *seat,
3173 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003174{
Daniel Stone37816df2012-05-16 18:45:18 +01003175 struct input *input = data;
3176
3177 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3178 input->pointer = wl_seat_get_pointer(seat);
3179 wl_pointer_set_user_data(input->pointer, input);
3180 wl_pointer_add_listener(input->pointer, &pointer_listener,
3181 input);
3182 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3183 wl_pointer_destroy(input->pointer);
3184 input->pointer = NULL;
3185 }
3186
3187 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3188 input->keyboard = wl_seat_get_keyboard(seat);
3189 wl_keyboard_set_user_data(input->keyboard, input);
3190 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3191 input);
3192 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3193 wl_keyboard_destroy(input->keyboard);
3194 input->keyboard = NULL;
3195 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003196
3197 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3198 input->touch = wl_seat_get_touch(seat);
3199 wl_touch_set_user_data(input->touch, input);
3200 wl_touch_add_listener(input->touch, &touch_listener, input);
3201 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3202 wl_touch_destroy(input->touch);
3203 input->touch = NULL;
3204 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003205}
3206
Rob Bradford08031182013-08-13 20:11:03 +01003207static void
3208seat_handle_name(void *data, struct wl_seat *seat,
3209 const char *name)
3210{
3211
3212}
3213
Daniel Stone37816df2012-05-16 18:45:18 +01003214static const struct wl_seat_listener seat_listener = {
3215 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003216 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003217};
3218
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003219void
3220input_get_position(struct input *input, int32_t *x, int32_t *y)
3221{
3222 *x = input->sx;
3223 *y = input->sy;
3224}
3225
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003226int
3227input_get_touch(struct input *input, int32_t id, float *x, float *y)
3228{
3229 struct touch_point *tp;
3230
3231 wl_list_for_each(tp, &input->touch_point_list, link) {
3232 if (tp->id != id)
3233 continue;
3234
3235 *x = tp->x;
3236 *y = tp->y;
3237 return 0;
3238 }
3239
3240 return -1;
3241}
3242
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003243struct display *
3244input_get_display(struct input *input)
3245{
3246 return input->display;
3247}
3248
Daniel Stone37816df2012-05-16 18:45:18 +01003249struct wl_seat *
3250input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003251{
Daniel Stone37816df2012-05-16 18:45:18 +01003252 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003253}
3254
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003255uint32_t
3256input_get_modifiers(struct input *input)
3257{
3258 return input->modifiers;
3259}
3260
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003261struct widget *
3262input_get_focus_widget(struct input *input)
3263{
3264 return input->focus_widget;
3265}
3266
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003267struct data_offer {
3268 struct wl_data_offer *offer;
3269 struct input *input;
3270 struct wl_array types;
3271 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003272
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003273 struct task io_task;
3274 int fd;
3275 data_func_t func;
3276 int32_t x, y;
3277 void *user_data;
3278};
3279
3280static void
3281data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3282{
3283 struct data_offer *offer = data;
3284 char **p;
3285
3286 p = wl_array_add(&offer->types, sizeof *p);
3287 *p = strdup(type);
3288}
3289
3290static const struct wl_data_offer_listener data_offer_listener = {
3291 data_offer_offer,
3292};
3293
3294static void
3295data_offer_destroy(struct data_offer *offer)
3296{
3297 char **p;
3298
3299 offer->refcount--;
3300 if (offer->refcount == 0) {
3301 wl_data_offer_destroy(offer->offer);
3302 for (p = offer->types.data; *p; p++)
3303 free(*p);
3304 wl_array_release(&offer->types);
3305 free(offer);
3306 }
3307}
3308
3309static void
3310data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003311 struct wl_data_device *data_device,
3312 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003313{
3314 struct data_offer *offer;
3315
Brian Lovinbc919262013-08-07 15:34:59 -07003316 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003317
3318 wl_array_init(&offer->types);
3319 offer->refcount = 1;
3320 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003321 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003322 wl_data_offer_add_listener(offer->offer,
3323 &data_offer_listener, offer);
3324}
3325
3326static void
3327data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003328 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003329 wl_fixed_t x_w, wl_fixed_t y_w,
3330 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003331{
3332 struct input *input = data;
3333 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003334 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003335 float x = wl_fixed_to_double(x_w);
3336 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003337 char **p;
3338
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003339 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003340 input->drag_enter_serial = serial;
3341 input->drag_focus = window,
3342 input->drag_x = x;
3343 input->drag_y = y;
3344
3345 if (!input->touch_grab)
3346 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003347
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003348 if (offer) {
3349 input->drag_offer = wl_data_offer_get_user_data(offer);
3350
3351 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3352 *p = NULL;
3353
3354 types_data = input->drag_offer->types.data;
3355 } else {
3356 input->drag_offer = NULL;
3357 types_data = NULL;
3358 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003359
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003360 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003361 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003362 window->user_data);
3363}
3364
3365static void
3366data_device_leave(void *data, struct wl_data_device *data_device)
3367{
3368 struct input *input = data;
3369
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003370 if (input->drag_offer) {
3371 data_offer_destroy(input->drag_offer);
3372 input->drag_offer = NULL;
3373 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003374}
3375
3376static void
3377data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003378 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003379{
3380 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003381 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003382 float x = wl_fixed_to_double(x_w);
3383 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003384 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003385
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003386 input->drag_x = x;
3387 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003388
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003389 if (input->drag_offer)
3390 types_data = input->drag_offer->types.data;
3391 else
3392 types_data = NULL;
3393
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003394 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003395 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003396 window->user_data);
3397}
3398
3399static void
3400data_device_drop(void *data, struct wl_data_device *data_device)
3401{
3402 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003403 struct window *window = input->drag_focus;
3404 float x, y;
3405
3406 x = input->drag_x;
3407 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003408
3409 if (window->drop_handler)
3410 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003411 x, y, window->user_data);
3412
3413 if (input->touch_grab)
3414 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003415}
3416
3417static void
3418data_device_selection(void *data,
3419 struct wl_data_device *wl_data_device,
3420 struct wl_data_offer *offer)
3421{
3422 struct input *input = data;
3423 char **p;
3424
3425 if (input->selection_offer)
3426 data_offer_destroy(input->selection_offer);
3427
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003428 if (offer) {
3429 input->selection_offer = wl_data_offer_get_user_data(offer);
3430 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3431 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003432 } else {
3433 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003434 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003435}
3436
3437static const struct wl_data_device_listener data_device_listener = {
3438 data_device_data_offer,
3439 data_device_enter,
3440 data_device_leave,
3441 data_device_motion,
3442 data_device_drop,
3443 data_device_selection
3444};
3445
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003446static void
3447input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003448{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003449 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003450 struct wl_cursor *cursor;
3451 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003452
Daniel Stone80972742012-11-07 17:51:39 +11003453 if (!input->pointer)
3454 return;
3455
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003456 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003457 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003458 return;
3459
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003460 if (index >= (int) cursor->image_count) {
3461 fprintf(stderr, "cursor index out of range\n");
3462 return;
3463 }
3464
3465 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003466 buffer = wl_cursor_image_get_buffer(image);
3467 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003468 return;
3469
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003470 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3471 wl_surface_damage(input->pointer_surface, 0, 0,
3472 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003473 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003474 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3475 input->pointer_surface,
3476 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003477}
3478
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003479static const struct wl_callback_listener pointer_surface_listener;
3480
3481static void
3482pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3483 uint32_t time)
3484{
3485 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003486 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003487 int i;
3488
3489 if (callback) {
3490 assert(callback == input->cursor_frame_cb);
3491 wl_callback_destroy(callback);
3492 input->cursor_frame_cb = NULL;
3493 }
3494
Daniel Stone80972742012-11-07 17:51:39 +11003495 if (!input->pointer)
3496 return;
3497
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003498 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003499 wl_pointer_set_cursor(input->pointer,
3500 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003501 NULL, 0, 0);
3502 return;
3503 }
3504
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003505 if (input->current_cursor == CURSOR_UNSET)
3506 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003507 cursor = input->display->cursors[input->current_cursor];
3508 if (!cursor)
3509 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003510
3511 /* FIXME We don't have the current time on the first call so we set
3512 * the animation start to the time of the first frame callback. */
3513 if (time == 0)
3514 input->cursor_anim_start = 0;
3515 else if (input->cursor_anim_start == 0)
3516 input->cursor_anim_start = time;
3517
3518 if (time == 0 || input->cursor_anim_start == 0)
3519 i = 0;
3520 else
3521 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3522
Pekka Paalanenbc106382012-10-10 12:49:31 +03003523 if (cursor->image_count > 1) {
3524 input->cursor_frame_cb =
3525 wl_surface_frame(input->pointer_surface);
3526 wl_callback_add_listener(input->cursor_frame_cb,
3527 &pointer_surface_listener, input);
3528 }
3529
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003530 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003531}
3532
3533static const struct wl_callback_listener pointer_surface_listener = {
3534 pointer_surface_frame_callback
3535};
3536
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003537void
3538input_set_pointer_image(struct input *input, int pointer)
3539{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003540 int force = 0;
3541
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003542 if (!input->pointer)
3543 return;
3544
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003545 if (input->pointer_enter_serial > input->cursor_serial)
3546 force = 1;
3547
3548 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003549 return;
3550
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003551 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003552 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003553 if (!input->cursor_frame_cb)
3554 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003555 else if (force) {
3556 /* The current frame callback may be stuck if, for instance,
3557 * the set cursor request was processed by the server after
3558 * this client lost the focus. In this case the cursor surface
3559 * might not be mapped and the frame callback wouldn't ever
3560 * complete. Send a set_cursor and attach to try to map the
3561 * cursor surface again so that the callback will finish */
3562 input_set_pointer_image_index(input, 0);
3563 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003564}
3565
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003566struct wl_data_device *
3567input_get_data_device(struct input *input)
3568{
3569 return input->data_device;
3570}
3571
3572void
3573input_set_selection(struct input *input,
3574 struct wl_data_source *source, uint32_t time)
3575{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003576 if (input->data_device)
3577 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003578}
3579
3580void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003581input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003582{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003583 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003584 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003585}
3586
3587static void
3588offer_io_func(struct task *task, uint32_t events)
3589{
3590 struct data_offer *offer =
3591 container_of(task, struct data_offer, io_task);
3592 unsigned int len;
3593 char buffer[4096];
3594
3595 len = read(offer->fd, buffer, sizeof buffer);
3596 offer->func(buffer, len,
3597 offer->x, offer->y, offer->user_data);
3598
3599 if (len == 0) {
3600 close(offer->fd);
3601 data_offer_destroy(offer);
3602 }
3603}
3604
3605static void
3606data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3607 data_func_t func, void *user_data)
3608{
3609 int p[2];
3610
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003611 if (pipe2(p, O_CLOEXEC) == -1)
3612 return;
3613
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003614 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3615 close(p[1]);
3616
3617 offer->io_task.run = offer_io_func;
3618 offer->fd = p[0];
3619 offer->func = func;
3620 offer->refcount++;
3621 offer->user_data = user_data;
3622
3623 display_watch_fd(offer->input->display,
3624 offer->fd, EPOLLIN, &offer->io_task);
3625}
3626
3627void
3628input_receive_drag_data(struct input *input, const char *mime_type,
3629 data_func_t func, void *data)
3630{
3631 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003632 input->drag_offer->x = input->drag_x;
3633 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003634}
3635
3636int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003637input_receive_drag_data_to_fd(struct input *input,
3638 const char *mime_type, int fd)
3639{
3640 if (input->drag_offer)
3641 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3642
3643 return 0;
3644}
3645
3646int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003647input_receive_selection_data(struct input *input, const char *mime_type,
3648 data_func_t func, void *data)
3649{
3650 char **p;
3651
3652 if (input->selection_offer == NULL)
3653 return -1;
3654
3655 for (p = input->selection_offer->types.data; *p; p++)
3656 if (strcmp(mime_type, *p) == 0)
3657 break;
3658
3659 if (*p == NULL)
3660 return -1;
3661
3662 data_offer_receive_data(input->selection_offer,
3663 mime_type, func, data);
3664 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003665}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003666
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003667int
3668input_receive_selection_data_to_fd(struct input *input,
3669 const char *mime_type, int fd)
3670{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003671 if (input->selection_offer)
3672 wl_data_offer_receive(input->selection_offer->offer,
3673 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003674
3675 return 0;
3676}
3677
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003678void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003679window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003680{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003681 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003682 return;
3683
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003684 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003685}
3686
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003687static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003688surface_set_synchronized(struct surface *surface)
3689{
3690 if (!surface->subsurface)
3691 return;
3692
3693 if (surface->synchronized)
3694 return;
3695
3696 wl_subsurface_set_sync(surface->subsurface);
3697 surface->synchronized = 1;
3698}
3699
3700static void
3701surface_set_synchronized_default(struct surface *surface)
3702{
3703 if (!surface->subsurface)
3704 return;
3705
3706 if (surface->synchronized == surface->synchronized_default)
3707 return;
3708
3709 if (surface->synchronized_default)
3710 wl_subsurface_set_sync(surface->subsurface);
3711 else
3712 wl_subsurface_set_desync(surface->subsurface);
3713
3714 surface->synchronized = surface->synchronized_default;
3715}
3716
3717static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003718surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003719{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003720 struct widget *widget = surface->widget;
3721 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003722
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003723 if (surface->input_region) {
3724 wl_region_destroy(surface->input_region);
3725 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003726 }
3727
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003728 if (surface->opaque_region)
3729 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003730
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003731 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003732
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003733 if (widget->resize_handler)
3734 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003735 widget->allocation.width,
3736 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003737 widget->user_data);
3738
Pekka Paalanen35e82632013-04-25 13:57:48 +03003739 if (surface->subsurface &&
3740 (surface->allocation.x != widget->allocation.x ||
3741 surface->allocation.y != widget->allocation.y)) {
3742 wl_subsurface_set_position(surface->subsurface,
3743 widget->allocation.x,
3744 widget->allocation.y);
3745 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003746 if (surface->allocation.width != widget->allocation.width ||
3747 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003748 window_schedule_redraw(widget->window);
3749 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003750 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003751
3752 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003753 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003754 widget->allocation.width,
3755 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003756}
3757
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003758static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003759hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3760{
3761 /*
3762 * This hack should be removed, when EGL respects
3763 * eglSwapInterval(0).
3764 *
3765 * If this window has sub-surfaces, especially a free-running
3766 * EGL-widget, we need to post the parent surface once with
3767 * all the old state to guarantee, that the EGL-widget will
3768 * receive its frame callback soon. Otherwise, a forced call
3769 * to eglSwapBuffers may end up blocking, waiting for a frame
3770 * event that will never come, because we will commit the parent
3771 * surface with all new state only after eglSwapBuffers returns.
3772 *
3773 * This assumes, that:
3774 * 1. When the EGL widget's resize hook is called, it pauses.
3775 * 2. When the EGL widget's redraw hook is called, it forces a
3776 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3777 * In a single threaded application condition 1 is a no-op.
3778 *
3779 * XXX: This should actually be after the surface_resize() calls,
3780 * but cannot, because then it would commit the incomplete state
3781 * accumulated from the widget resize hooks.
3782 */
3783 if (window->subsurface_list.next != &window->main_surface->link ||
3784 window->subsurface_list.prev != &window->main_surface->link)
3785 wl_surface_commit(window->main_surface->surface);
3786}
3787
3788static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003789window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003790{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003791 struct surface *surface;
3792
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003793 widget_set_allocation(window->main_surface->widget,
3794 window->pending_allocation.x,
3795 window->pending_allocation.y,
3796 window->pending_allocation.width,
3797 window->pending_allocation.height);
3798
3799 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003800
3801 /* The main surface is in the list, too. Main surface's
3802 * resize_handler is responsible for calling widget_set_allocation()
3803 * on all sub-surface root widgets, so they will be resized
3804 * properly.
3805 */
3806 wl_list_for_each(surface, &window->subsurface_list, link) {
3807 if (surface == window->main_surface)
3808 continue;
3809
3810 surface_set_synchronized(surface);
3811 surface_resize(surface);
3812 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003813
3814 if (!window->fullscreen && !window->maximized)
3815 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003816}
3817
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003818static void
3819idle_resize(struct window *window)
3820{
3821 window->resize_needed = 0;
3822 window->redraw_needed = 1;
3823
3824 DBG("from %dx%d to %dx%d\n",
3825 window->main_surface->server_allocation.width,
3826 window->main_surface->server_allocation.height,
3827 window->pending_allocation.width,
3828 window->pending_allocation.height);
3829
3830 hack_prevent_EGL_sub_surface_deadlock(window);
3831
3832 window_do_resize(window);
3833}
3834
3835static void
3836undo_resize(struct window *window)
3837{
3838 window->pending_allocation.width =
3839 window->main_surface->server_allocation.width;
3840 window->pending_allocation.height =
3841 window->main_surface->server_allocation.height;
3842
3843 DBG("back to %dx%d\n",
3844 window->main_surface->server_allocation.width,
3845 window->main_surface->server_allocation.height);
3846
3847 window_do_resize(window);
3848
3849 if (window->pending_allocation.width == 0 &&
3850 window->pending_allocation.height == 0) {
3851 fprintf(stderr, "Error: Could not draw a surface, "
3852 "most likely due to insufficient disk space in "
3853 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3854 exit(EXIT_FAILURE);
3855 }
3856}
3857
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003858void
3859window_schedule_resize(struct window *window, int width, int height)
3860{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003861 /* We should probably get these numbers from the theme. */
3862 const int min_width = 200, min_height = 200;
3863
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003864 window->pending_allocation.x = 0;
3865 window->pending_allocation.y = 0;
3866 window->pending_allocation.width = width;
3867 window->pending_allocation.height = height;
3868
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003869 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003870 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003871 window->min_allocation.width = min_width;
3872 else
3873 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003874 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003875 window->min_allocation.height = min_height;
3876 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003877 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003878 }
3879
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003880 if (window->pending_allocation.width < window->min_allocation.width)
3881 window->pending_allocation.width = window->min_allocation.width;
3882 if (window->pending_allocation.height < window->min_allocation.height)
3883 window->pending_allocation.height = window->min_allocation.height;
3884
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003885 window->resize_needed = 1;
3886 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003887}
3888
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003889void
3890widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3891{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003892 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003893}
3894
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003895static int
3896window_get_shadow_margin(struct window *window)
3897{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003898 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003899 return frame_get_shadow_margin(window->frame->frame);
3900 else
3901 return 0;
3902}
3903
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003904static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003905handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003906 int32_t width, int32_t height,
3907 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003908{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003909 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003910 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003911
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003912 window->maximized = 0;
3913 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003914 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003915 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003916
3917 wl_array_for_each(p, states) {
3918 uint32_t state = *p;
3919 switch (state) {
3920 case XDG_SURFACE_STATE_MAXIMIZED:
3921 window->maximized = 1;
3922 break;
3923 case XDG_SURFACE_STATE_FULLSCREEN:
3924 window->fullscreen = 1;
3925 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003926 case XDG_SURFACE_STATE_RESIZING:
3927 window->resizing = 1;
3928 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003929 case XDG_SURFACE_STATE_ACTIVATED:
3930 window->focused = 1;
3931 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003932 default:
3933 /* Unknown state */
3934 break;
3935 }
3936 }
3937
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003938 if (window->frame) {
3939 if (window->maximized) {
3940 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3941 } else {
3942 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3943 }
3944
3945 if (window->focused) {
3946 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3947 } else {
3948 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3949 }
3950 }
3951
Jasper St. Pierref184c382014-05-06 08:33:27 -04003952 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003953 /* The width / height params are for window geometry,
3954 * but window_schedule_resize takes allocation. Add
3955 * on the shadow margin to get the difference. */
3956 int margin = window_get_shadow_margin(window);
3957
3958 window_schedule_resize(window,
3959 width + margin * 2,
3960 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04003961 } else {
3962 window_schedule_resize(window,
3963 window->saved_allocation.width,
3964 window->saved_allocation.height);
3965 }
3966
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07003967 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07003968
3969 if (window->state_changed_handler)
3970 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003971}
3972
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003973static void
3974handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3975{
3976 struct window *window = data;
3977 window_close(window);
3978}
3979
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003980static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003981 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003982 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003983};
3984
3985static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003986window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05003987{
3988 struct wl_surface *parent_surface;
3989
3990 if (!window->xdg_surface)
3991 return;
3992
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003993 if (window->parent)
3994 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05003995 else
3996 parent_surface = NULL;
3997
Ondřej Majerechb2c18642014-09-13 16:35:45 +02003998 if (parent_surface == window->last_parent_surface)
3999 return;
4000
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004001 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004002 window->last_parent_surface = parent_surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004003}
4004
4005static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004006window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004007{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004008 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004009 frame_input_rect(window->frame->frame,
4010 &geometry->x,
4011 &geometry->y,
4012 &geometry->width,
4013 &geometry->height);
4014 else
4015 window_get_allocation(window, geometry);
4016}
4017
4018static void
4019window_sync_geometry(struct window *window)
4020{
4021 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004022
4023 if (!window->xdg_surface)
4024 return;
4025
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004026 window_get_geometry(window, &geometry);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004027 if (geometry.x == window->last_geometry.x &&
4028 geometry.y == window->last_geometry.y &&
4029 geometry.width == window->last_geometry.width &&
4030 geometry.height == window->last_geometry.height)
4031 return;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004032
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004033 xdg_surface_set_window_geometry(window->xdg_surface,
4034 geometry.x,
4035 geometry.y,
4036 geometry.width,
4037 geometry.height);
Ondřej Majerechb2c18642014-09-13 16:35:45 +02004038 window->last_geometry = geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004039}
4040
4041static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004042window_flush(struct window *window)
4043{
4044 struct surface *surface;
4045
4046 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004047 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004048 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004049 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004050 }
4051 }
4052
4053 wl_list_for_each(surface, &window->subsurface_list, link) {
4054 if (surface == window->main_surface)
4055 continue;
4056
4057 surface_flush(surface);
4058 }
4059
4060 surface_flush(window->main_surface);
4061}
4062
4063static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004064menu_destroy(struct menu *menu)
4065{
4066 widget_destroy(menu->widget);
4067 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004068 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004069 free(menu);
4070}
4071
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004072void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004073window_get_allocation(struct window *window,
4074 struct rectangle *allocation)
4075{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004076 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004077}
4078
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004079static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004080widget_redraw(struct widget *widget)
4081{
4082 struct widget *child;
4083
4084 if (widget->redraw_handler)
4085 widget->redraw_handler(widget, widget->user_data);
4086 wl_list_for_each(child, &widget->child_list, link)
4087 widget_redraw(child);
4088}
4089
4090static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004091frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4092{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004093 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004094
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004095 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004096 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004097 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004098 surface->frame_cb = NULL;
4099
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004100 surface->last_time = time;
4101
Pekka Paalanen71233882013-04-25 13:57:53 +03004102 if (surface->redraw_needed || surface->window->redraw_needed) {
4103 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004104 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004105 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004106}
4107
4108static const struct wl_callback_listener listener = {
4109 frame_callback
4110};
4111
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004112static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004113surface_redraw(struct surface *surface)
4114{
Pekka Paalanen71233882013-04-25 13:57:53 +03004115 DBG_OBJ(surface->surface, "begin\n");
4116
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004117 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004118 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004119
4120 /* Whole-window redraw forces a redraw even if the previous has
4121 * not yet hit the screen.
4122 */
4123 if (surface->frame_cb) {
4124 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004125 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004126
Pekka Paalanen71233882013-04-25 13:57:53 +03004127 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004128 wl_callback_destroy(surface->frame_cb);
4129 }
4130
Neil Roberts97b747c2013-12-19 16:17:12 +00004131 if (surface->widget->use_cairo &&
4132 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004133 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4134 return -1;
4135 }
4136
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004137 surface->frame_cb = wl_surface_frame(surface->surface);
4138 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004139 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004140
4141 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004142 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004143 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004144 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004145 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004146}
4147
4148static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004149idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004150{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004151 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004152 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004153 int failed = 0;
4154 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004155
Pekka Paalanen71233882013-04-25 13:57:53 +03004156 DBG(" --------- \n");
4157
Pekka Paalaneneebff542013-04-25 13:57:52 +03004158 wl_list_init(&window->redraw_task.link);
4159 window->redraw_task_scheduled = 0;
4160
4161 if (window->resize_needed) {
4162 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004163 if (window->main_surface->frame_cb) {
4164 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004165 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004166 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004167
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004168 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004169 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004170 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004171
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004172 if (surface_redraw(window->main_surface) < 0) {
4173 /*
4174 * Only main_surface failure will cause us to undo the resize.
4175 * If sub-surfaces fail, they will just be broken with old
4176 * content.
4177 */
4178 failed = 1;
4179 } else {
4180 wl_list_for_each(surface, &window->subsurface_list, link) {
4181 if (surface == window->main_surface)
4182 continue;
4183
4184 surface_redraw(surface);
4185 }
4186 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004187
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004188 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004189 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004190
4191 wl_list_for_each(surface, &window->subsurface_list, link)
4192 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004193
4194 if (resized && failed) {
4195 /* Restore widget tree to correspond to what is on screen. */
4196 undo_resize(window);
4197 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004198}
4199
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004200static void
4201window_schedule_redraw_task(struct window *window)
4202{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004203 if (!window->redraw_task_scheduled) {
4204 window->redraw_task.run = idle_redraw;
4205 display_defer(window->display, &window->redraw_task);
4206 window->redraw_task_scheduled = 1;
4207 }
4208}
4209
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004210void
4211window_schedule_redraw(struct window *window)
4212{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004213 struct surface *surface;
4214
Pekka Paalanen71233882013-04-25 13:57:53 +03004215 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4216
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004217 wl_list_for_each(surface, &window->subsurface_list, link)
4218 surface->redraw_needed = 1;
4219
4220 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004221}
4222
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004223int
4224window_is_fullscreen(struct window *window)
4225{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004226 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004227}
4228
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004229void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004230window_set_fullscreen(struct window *window, int fullscreen)
4231{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004232 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004233 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004234
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004235 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004236 return;
4237
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004238 if (fullscreen)
4239 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4240 else
4241 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004242}
4243
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004244int
4245window_is_maximized(struct window *window)
4246{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004247 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004248}
4249
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004250void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004251window_set_maximized(struct window *window, int maximized)
4252{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004253 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004254 return;
4255
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004256 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004257 return;
4258
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004259 if (maximized)
4260 xdg_surface_set_maximized(window->xdg_surface);
4261 else
4262 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004263}
4264
Jasper St. Pierrede680992014-04-10 17:23:49 -07004265int
4266window_is_resizing(struct window *window)
4267{
4268 return window->resizing;
4269}
4270
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004271void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004272window_set_minimized(struct window *window)
4273{
4274 if (!window->xdg_surface)
4275 return;
4276
4277 xdg_surface_set_minimized(window->xdg_surface);
4278}
4279
4280void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004281window_set_user_data(struct window *window, void *data)
4282{
4283 window->user_data = data;
4284}
4285
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004286void *
4287window_get_user_data(struct window *window)
4288{
4289 return window->user_data;
4290}
4291
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004292void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004293window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004294 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004295{
4296 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004297}
4298
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004299void
4300window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004301 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004302{
4303 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004304}
4305
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004306void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004307window_set_data_handler(struct window *window, window_data_handler_t handler)
4308{
4309 window->data_handler = handler;
4310}
4311
4312void
4313window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4314{
4315 window->drop_handler = handler;
4316}
4317
4318void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004319window_set_close_handler(struct window *window,
4320 window_close_handler_t handler)
4321{
4322 window->close_handler = handler;
4323}
4324
4325void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004326window_set_fullscreen_handler(struct window *window,
4327 window_fullscreen_handler_t handler)
4328{
4329 window->fullscreen_handler = handler;
4330}
4331
4332void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004333window_set_output_handler(struct window *window,
4334 window_output_handler_t handler)
4335{
4336 window->output_handler = handler;
4337}
4338
4339void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004340window_set_state_changed_handler(struct window *window,
4341 window_state_changed_handler_t handler)
4342{
4343 window->state_changed_handler = handler;
4344}
4345
4346void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004347window_set_title(struct window *window, const char *title)
4348{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004349 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004350 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004351 if (window->frame) {
4352 frame_set_title(window->frame->frame, title);
4353 widget_schedule_redraw(window->frame->widget);
4354 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004355 if (window->xdg_surface)
4356 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004357}
4358
4359const char *
4360window_get_title(struct window *window)
4361{
4362 return window->title;
4363}
4364
4365void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004366window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4367{
4368 struct text_cursor_position *text_cursor_position =
4369 window->display->text_cursor_position;
4370
Scott Moreau9295ce02012-06-01 12:46:10 -06004371 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004372 return;
4373
4374 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004375 window->main_surface->surface,
4376 wl_fixed_from_int(x),
4377 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004378}
4379
4380void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004381window_damage(struct window *window, int32_t x, int32_t y,
4382 int32_t width, int32_t height)
4383{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004384 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004385}
4386
Casey Dahlin9074db52012-04-19 22:50:09 -04004387static void
4388surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004389 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004390{
Rob Bradford7507b572012-05-15 17:55:34 +01004391 struct window *window = data;
4392 struct output *output;
4393 struct output *output_found = NULL;
4394 struct window_output *window_output;
4395
4396 wl_list_for_each(output, &window->display->output_list, link) {
4397 if (output->output == wl_output) {
4398 output_found = output;
4399 break;
4400 }
4401 }
4402
4403 if (!output_found)
4404 return;
4405
Brian Lovinbc919262013-08-07 15:34:59 -07004406 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004407 window_output->output = output_found;
4408
4409 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004410
4411 if (window->output_handler)
4412 window->output_handler(window, output_found, 1,
4413 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004414}
4415
4416static void
4417surface_leave(void *data,
4418 struct wl_surface *wl_surface, struct wl_output *output)
4419{
Rob Bradford7507b572012-05-15 17:55:34 +01004420 struct window *window = data;
4421 struct window_output *window_output;
4422 struct window_output *window_output_found = NULL;
4423
4424 wl_list_for_each(window_output, &window->window_output_list, link) {
4425 if (window_output->output->output == output) {
4426 window_output_found = window_output;
4427 break;
4428 }
4429 }
4430
4431 if (window_output_found) {
4432 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004433
4434 if (window->output_handler)
4435 window->output_handler(window, window_output->output,
4436 0, window->user_data);
4437
Rob Bradford7507b572012-05-15 17:55:34 +01004438 free(window_output_found);
4439 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004440}
4441
4442static const struct wl_surface_listener surface_listener = {
4443 surface_enter,
4444 surface_leave
4445};
4446
Pekka Paalanen4e373742013-02-13 16:17:13 +02004447static struct surface *
4448surface_create(struct window *window)
4449{
4450 struct display *display = window->display;
4451 struct surface *surface;
4452
Brian Lovinbc919262013-08-07 15:34:59 -07004453 surface = xmalloc(sizeof *surface);
4454 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004455 if (!surface)
4456 return NULL;
4457
4458 surface->window = window;
4459 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004460 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004461 wl_surface_add_listener(surface->surface, &surface_listener, window);
4462
Pekka Paalanen35e82632013-04-25 13:57:48 +03004463 wl_list_insert(&window->subsurface_list, &surface->link);
4464
Pekka Paalanen4e373742013-02-13 16:17:13 +02004465 return surface;
4466}
4467
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004468static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004469get_preferred_buffer_type(struct display *display)
4470{
4471#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004472 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004473 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4474#endif
4475
4476 return WINDOW_BUFFER_TYPE_SHM;
4477}
4478
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004479static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004480window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004481{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004482 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004483 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004484
Peter Huttererf3d62272013-08-08 11:57:05 +10004485 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004486 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004487 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004488
4489 surface = surface_create(window);
4490 window->main_surface = surface;
4491
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004492 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004493
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004494 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004495 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004496
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004497 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004498
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004499 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004500 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004501 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004502
Rob Bradford7507b572012-05-15 17:55:34 +01004503 wl_list_init (&window->window_output_list);
4504
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004505 return window;
4506}
4507
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004508struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004509window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004510{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004511 struct window *window;
4512
4513 window = window_create_internal(display, 0);
4514
4515 window->xdg_surface =
4516 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4517 window->main_surface->surface);
4518 fail_on_null(window->xdg_surface);
4519
4520 xdg_surface_set_user_data(window->xdg_surface, window);
4521 xdg_surface_add_listener(window->xdg_surface,
4522 &xdg_surface_listener, window);
4523
4524 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004525}
4526
4527struct window *
4528window_create_custom(struct display *display)
4529{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004530 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004531}
4532
Jasper St. Pierre53686042013-12-09 15:26:25 -05004533void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004534window_set_parent(struct window *window,
4535 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004536{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004537 window->parent = parent_window;
4538 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004539}
4540
4541struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004542window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004543{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004544 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004545}
4546
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004547static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004548menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004549{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004550 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004551 int next;
4552
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004553 frame_interior(menu->frame, &x, &y, &width, &height);
4554 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004555 if (menu->current != next) {
4556 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004557 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004558 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004559}
4560
4561static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004562menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004563 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004564 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004565{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004566 struct menu *menu = data;
4567
4568 if (widget == menu->widget)
4569 menu_set_item(data, y);
4570
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004571 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004572}
4573
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004574static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004575menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004576 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004577{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004578 struct menu *menu = data;
4579
4580 if (widget == menu->widget)
4581 menu_set_item(data, y);
4582
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004583 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004584}
4585
4586static void
4587menu_leave_handler(struct widget *widget, struct input *input, void *data)
4588{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004589 struct menu *menu = data;
4590
4591 if (widget == menu->widget)
4592 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004593}
4594
4595static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004596menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004597 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004598 uint32_t button, enum wl_pointer_button_state state,
4599 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004600
4601{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004602 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004603
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004604 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4605 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004606 /* Either relase after press-drag-release or
4607 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004608 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004609 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004610 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004611 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004612 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004613 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004614}
4615
4616static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004617menu_touch_up_handler(struct widget *widget,
4618 struct input *input,
4619 uint32_t serial,
4620 uint32_t time,
4621 int32_t id,
4622 void *data)
4623{
4624 struct menu *menu = data;
4625
4626 input_ungrab(input);
4627 menu_destroy(menu);
4628}
4629
4630static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004631menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004632{
4633 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004634 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004635 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004636
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004637 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004638
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004639 frame_repaint(menu->frame, cr);
4640 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004641
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004642 theme_set_background_source(menu->window->display->theme,
4643 cr, THEME_FRAME_ACTIVE);
4644 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004645 cairo_fill(cr);
4646
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004647 cairo_select_font_face(cr, "sans",
4648 CAIRO_FONT_SLANT_NORMAL,
4649 CAIRO_FONT_WEIGHT_NORMAL);
4650 cairo_set_font_size(cr, 12);
4651
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004652 for (i = 0; i < menu->count; i++) {
4653 if (i == menu->current) {
4654 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004655 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004656 cairo_fill(cr);
4657 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004658 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004659 cairo_show_text(cr, menu->entries[i]);
4660 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004661 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4662 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004663 cairo_show_text(cr, menu->entries[i]);
4664 }
4665 }
4666
4667 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004668}
4669
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004670static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004671handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4672{
4673 struct window *window = data;
4674 struct menu *menu = window->main_surface->widget->user_data;
4675
4676 input_ungrab(menu->input);
4677 menu_destroy(menu);
4678}
4679
4680static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004681 handle_popup_popup_done,
4682};
4683
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004684static struct menu *
4685create_menu(struct display *display,
4686 struct input *input, uint32_t time,
4687 menu_func_t func, const char **entries, int count,
4688 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004689{
4690 struct window *window;
4691 struct menu *menu;
4692
4693 menu = malloc(sizeof *menu);
4694 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004695 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004696
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004697 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004698 if (!window) {
4699 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004700 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004701 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004702
4703 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004704 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004705 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004706 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004707 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004708 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004709 menu->entries = entries;
4710 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004711 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004712 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004713 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004714 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004715 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004716
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004717 input_ungrab(input);
4718
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004719 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004720 widget_set_enter_handler(menu->widget, menu_enter_handler);
4721 widget_set_leave_handler(menu->widget, menu_leave_handler);
4722 widget_set_motion_handler(menu->widget, menu_motion_handler);
4723 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004724 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004725
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004726 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004727 frame_resize_inside(menu->frame, 200, count * 20);
4728 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4729 window_schedule_resize(window, frame_width(menu->frame),
4730 frame_height(menu->frame));
4731
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004732 return menu;
4733}
4734
4735struct window *
4736window_create_menu(struct display *display,
4737 struct input *input, uint32_t time,
4738 menu_func_t func, const char **entries, int count,
4739 void *user_data)
4740{
4741 struct menu *menu;
4742 menu = create_menu(display, input, time, func, entries, count, user_data);
4743
4744 if (menu == NULL)
4745 return NULL;
4746
4747 return menu->window;
4748}
4749
4750void
4751window_show_menu(struct display *display,
4752 struct input *input, uint32_t time, struct window *parent,
4753 int32_t x, int32_t y,
4754 menu_func_t func, const char **entries, int count)
4755{
4756 struct menu *menu;
4757 struct window *window;
4758 int32_t ix, iy;
4759
4760 menu = create_menu(display, input, time, func, entries, count, parent);
4761
4762 if (menu == NULL)
4763 return;
4764
4765 window = menu->window;
4766
4767 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4768 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4769
4770 window->x = x;
4771 window->y = y;
4772
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004773 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004774
4775 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4776 window->main_surface->surface,
4777 parent->main_surface->surface,
4778 input->seat,
4779 display_get_serial(window->display),
4780 window->x - ix,
4781 window->y - iy,
4782 0);
4783 fail_on_null(window->xdg_popup);
4784
4785 xdg_popup_set_user_data(window->xdg_popup, window);
4786 xdg_popup_add_listener(window->xdg_popup,
4787 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004788}
4789
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004790void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004791window_set_buffer_type(struct window *window, enum window_buffer_type type)
4792{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004793 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004794}
4795
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004796enum window_buffer_type
4797window_get_buffer_type(struct window *window)
4798{
4799 return window->main_surface->buffer_type;
4800}
4801
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004802void
4803window_set_preferred_format(struct window *window,
4804 enum preferred_format format)
4805{
4806 window->preferred_format = format;
4807}
4808
Pekka Paalanen35e82632013-04-25 13:57:48 +03004809struct widget *
4810window_add_subsurface(struct window *window, void *data,
4811 enum subsurface_mode default_mode)
4812{
4813 struct widget *widget;
4814 struct surface *surface;
4815 struct wl_surface *parent;
4816 struct wl_subcompositor *subcompo = window->display->subcompositor;
4817
Pekka Paalanen35e82632013-04-25 13:57:48 +03004818 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004819 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004820 widget = widget_create(window, surface, data);
4821 wl_list_init(&widget->link);
4822 surface->widget = widget;
4823
4824 parent = window->main_surface->surface;
4825 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4826 surface->surface,
4827 parent);
4828 surface->synchronized = 1;
4829
4830 switch (default_mode) {
4831 case SUBSURFACE_SYNCHRONIZED:
4832 surface->synchronized_default = 1;
4833 break;
4834 case SUBSURFACE_DESYNCHRONIZED:
4835 surface->synchronized_default = 0;
4836 break;
4837 default:
4838 assert(!"bad enum subsurface_mode");
4839 }
4840
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004841 window->resize_needed = 1;
4842 window_schedule_redraw(window);
4843
Pekka Paalanen35e82632013-04-25 13:57:48 +03004844 return widget;
4845}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004846
4847static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004848display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004849 struct wl_output *wl_output,
4850 int x, int y,
4851 int physical_width,
4852 int physical_height,
4853 int subpixel,
4854 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004855 const char *model,
4856 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004857{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004858 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004859
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004860 output->allocation.x = x;
4861 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004862 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004863
4864 if (output->make)
4865 free(output->make);
4866 output->make = strdup(make);
4867
4868 if (output->model)
4869 free(output->model);
4870 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004871}
4872
4873static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004874display_handle_done(void *data,
4875 struct wl_output *wl_output)
4876{
4877}
4878
4879static void
4880display_handle_scale(void *data,
4881 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004882 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004883{
4884 struct output *output = data;
4885
4886 output->scale = scale;
4887}
4888
4889static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004890display_handle_mode(void *data,
4891 struct wl_output *wl_output,
4892 uint32_t flags,
4893 int width,
4894 int height,
4895 int refresh)
4896{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004897 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004898 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004899
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004900 if (flags & WL_OUTPUT_MODE_CURRENT) {
4901 output->allocation.width = width;
4902 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004903 if (display->output_configure_handler)
4904 (*display->output_configure_handler)(
4905 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004906 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004907}
4908
4909static const struct wl_output_listener output_listener = {
4910 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004911 display_handle_mode,
4912 display_handle_done,
4913 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004914};
4915
4916static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004917display_add_output(struct display *d, uint32_t id)
4918{
4919 struct output *output;
4920
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004921 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004922 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004923 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004924 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004925 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004926 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004927 wl_list_insert(d->output_list.prev, &output->link);
4928
4929 wl_output_add_listener(output->output, &output_listener, output);
4930}
4931
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004932static void
4933output_destroy(struct output *output)
4934{
4935 if (output->destroy_handler)
4936 (*output->destroy_handler)(output, output->user_data);
4937
4938 wl_output_destroy(output->output);
4939 wl_list_remove(&output->link);
4940 free(output);
4941}
4942
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004943static void
4944display_destroy_output(struct display *d, uint32_t id)
4945{
4946 struct output *output;
4947
4948 wl_list_for_each(output, &d->output_list, link) {
4949 if (output->server_output_id == id) {
4950 output_destroy(output);
4951 break;
4952 }
4953 }
4954}
4955
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004956void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004957display_set_global_handler(struct display *display,
4958 display_global_handler_t handler)
4959{
4960 struct global *global;
4961
4962 display->global_handler = handler;
4963 if (!handler)
4964 return;
4965
4966 wl_list_for_each(global, &display->global_list, link)
4967 display->global_handler(display,
4968 global->name, global->interface,
4969 global->version, display->user_data);
4970}
4971
4972void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004973display_set_global_handler_remove(struct display *display,
4974 display_global_handler_t remove_handler)
4975{
4976 display->global_handler_remove = remove_handler;
4977 if (!remove_handler)
4978 return;
4979}
4980
4981void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004982display_set_output_configure_handler(struct display *display,
4983 display_output_handler_t handler)
4984{
4985 struct output *output;
4986
4987 display->output_configure_handler = handler;
4988 if (!handler)
4989 return;
4990
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004991 wl_list_for_each(output, &display->output_list, link) {
4992 if (output->allocation.width == 0 &&
4993 output->allocation.height == 0)
4994 continue;
4995
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004996 (*display->output_configure_handler)(output,
4997 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004998 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004999}
5000
5001void
5002output_set_user_data(struct output *output, void *data)
5003{
5004 output->user_data = data;
5005}
5006
5007void *
5008output_get_user_data(struct output *output)
5009{
5010 return output->user_data;
5011}
5012
5013void
5014output_set_destroy_handler(struct output *output,
5015 display_output_handler_t handler)
5016{
5017 output->destroy_handler = handler;
5018 /* FIXME: implement this, once we have way to remove outputs */
5019}
5020
5021void
Scott Moreau4e072362012-09-29 02:03:11 -06005022output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005023{
Scott Moreau4e072362012-09-29 02:03:11 -06005024 struct rectangle allocation = output->allocation;
5025
5026 switch (output->transform) {
5027 case WL_OUTPUT_TRANSFORM_90:
5028 case WL_OUTPUT_TRANSFORM_270:
5029 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5030 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5031 /* Swap width and height */
5032 allocation.width = output->allocation.height;
5033 allocation.height = output->allocation.width;
5034 break;
5035 }
5036
5037 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005038}
5039
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005040struct wl_output *
5041output_get_wl_output(struct output *output)
5042{
5043 return output->output;
5044}
5045
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005046enum wl_output_transform
5047output_get_transform(struct output *output)
5048{
5049 return output->transform;
5050}
5051
Alexander Larssonafd319a2013-05-22 14:41:27 +02005052uint32_t
5053output_get_scale(struct output *output)
5054{
5055 return output->scale;
5056}
5057
Jason Ekstrand738715d2014-04-02 19:53:50 -05005058const char *
5059output_get_make(struct output *output)
5060{
5061 return output->make;
5062}
5063
5064const char *
5065output_get_model(struct output *output)
5066{
5067 return output->model;
5068}
5069
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005070static void
Daniel Stone97f68542012-05-30 16:32:01 +01005071fini_xkb(struct input *input)
5072{
5073 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005074 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005075}
5076
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005077#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005078
Daniel Stone97f68542012-05-30 16:32:01 +01005079static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005080display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005081{
5082 struct input *input;
5083
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005084 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005085 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005086 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005087 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005088 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005089 input->pointer_focus = NULL;
5090 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005091 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005092 wl_list_insert(d->input_list.prev, &input->link);
5093
Daniel Stone37816df2012-05-16 18:45:18 +01005094 wl_seat_add_listener(input->seat, &seat_listener, input);
5095 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005096
Jason Ekstranda669bd52014-04-02 19:53:51 -05005097 if (d->data_device_manager) {
5098 input->data_device =
5099 wl_data_device_manager_get_data_device(d->data_device_manager,
5100 input->seat);
5101 wl_data_device_add_listener(input->data_device,
5102 &data_device_listener,
5103 input);
5104 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005105
5106 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005107
Jonny Lamb06959082014-08-12 14:58:27 +02005108 set_repeat_info(input, 40, 400);
5109
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005110 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5111 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005112 input->repeat_task.run = keyboard_repeat_func;
5113 display_watch_fd(d, input->repeat_timer_fd,
5114 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005115}
5116
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005117static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005118input_destroy(struct input *input)
5119{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005120 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005121 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005122
5123 if (input->drag_offer)
5124 data_offer_destroy(input->drag_offer);
5125
5126 if (input->selection_offer)
5127 data_offer_destroy(input->selection_offer);
5128
Jason Ekstranda669bd52014-04-02 19:53:51 -05005129 if (input->data_device)
5130 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005131
5132 if (input->display->seat_version >= 3) {
5133 if (input->pointer)
5134 wl_pointer_release(input->pointer);
5135 if (input->keyboard)
5136 wl_keyboard_release(input->keyboard);
5137 }
5138
Daniel Stone97f68542012-05-30 16:32:01 +01005139 fini_xkb(input);
5140
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005141 wl_surface_destroy(input->pointer_surface);
5142
Pekka Paalanene1207c72011-12-16 12:02:09 +02005143 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005144 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005145 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005146 free(input);
5147}
5148
5149static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005150init_workspace_manager(struct display *d, uint32_t id)
5151{
5152 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005153 wl_registry_bind(d->registry, id,
5154 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005155 if (d->workspace_manager != NULL)
5156 workspace_manager_add_listener(d->workspace_manager,
5157 &workspace_manager_listener,
5158 d);
5159}
5160
5161static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005162shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5163{
5164 struct display *d = data;
5165
5166 if (format == WL_SHM_FORMAT_RGB565)
5167 d->has_rgb565 = 1;
5168}
5169
5170struct wl_shm_listener shm_listener = {
5171 shm_format
5172};
5173
5174static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005175xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5176{
5177 xdg_shell_pong(shell, serial);
5178}
5179
5180static const struct xdg_shell_listener xdg_shell_listener = {
5181 xdg_shell_ping,
5182};
5183
Pekka Paalanen71182ae2014-08-21 17:47:20 +03005184#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005185#ifdef static_assert
5186static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5187 "Interface version doesn't match implementation version");
5188#endif
5189
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005190static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005191registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5192 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005193{
5194 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005195 struct global *global;
5196
Brian Lovinbc919262013-08-07 15:34:59 -07005197 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005198 global->name = id;
5199 global->interface = strdup(interface);
5200 global->version = version;
5201 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005202
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005203 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005204 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005205 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005206 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005207 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005208 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005209 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005210 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005211 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005212 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005213 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005214 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5215 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005216 wl_registry_bind(registry, id,
5217 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005218 } else if (strcmp(interface, "xdg_shell") == 0) {
5219 d->xdg_shell = wl_registry_bind(registry, id,
5220 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005221 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005222 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005223 } else if (strcmp(interface, "text_cursor_position") == 0) {
5224 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005225 wl_registry_bind(registry, id,
5226 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005227 } else if (strcmp(interface, "workspace_manager") == 0) {
5228 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005229 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5230 d->subcompositor =
5231 wl_registry_bind(registry, id,
5232 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005233 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005234
5235 if (d->global_handler)
5236 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005237}
5238
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005239static void
5240registry_handle_global_remove(void *data, struct wl_registry *registry,
5241 uint32_t name)
5242{
5243 struct display *d = data;
5244 struct global *global;
5245 struct global *tmp;
5246
5247 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5248 if (global->name != name)
5249 continue;
5250
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005251 if (strcmp(global->interface, "wl_output") == 0)
5252 display_destroy_output(d, name);
5253
5254 /* XXX: Should destroy remaining bound globals */
5255
5256 if (d->global_handler_remove)
5257 d->global_handler_remove(d, name, global->interface,
5258 global->version, d->user_data);
5259
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005260 wl_list_remove(&global->link);
5261 free(global->interface);
5262 free(global);
5263 }
5264}
5265
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005266void *
5267display_bind(struct display *display, uint32_t name,
5268 const struct wl_interface *interface, uint32_t version)
5269{
5270 return wl_registry_bind(display->registry, name, interface, version);
5271}
5272
5273static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005274 registry_handle_global,
5275 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005276};
5277
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005278#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005279static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005280init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005281{
5282 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005283 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005284
Rob Clark6396ed32012-03-11 19:48:41 -05005285#ifdef USE_CAIRO_GLESV2
5286# define GL_BIT EGL_OPENGL_ES2_BIT
5287#else
5288# define GL_BIT EGL_OPENGL_BIT
5289#endif
5290
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005291 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005292 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005293 EGL_RED_SIZE, 1,
5294 EGL_GREEN_SIZE, 1,
5295 EGL_BLUE_SIZE, 1,
5296 EGL_ALPHA_SIZE, 1,
5297 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005298 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005299 EGL_NONE
5300 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005301
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005302#ifdef USE_CAIRO_GLESV2
5303 static const EGLint context_attribs[] = {
5304 EGL_CONTEXT_CLIENT_VERSION, 2,
5305 EGL_NONE
5306 };
5307 EGLint api = EGL_OPENGL_ES_API;
5308#else
5309 EGLint *context_attribs = NULL;
5310 EGLint api = EGL_OPENGL_API;
5311#endif
5312
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005313 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005314 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005315 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005316 return -1;
5317 }
5318
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005319 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005320 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005321 return -1;
5322 }
5323
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005324 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5325 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005326 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005327 return -1;
5328 }
5329
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005330 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005331 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005332 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005333 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005334 return -1;
5335 }
5336
Benjamin Franzke0c991632011-09-27 21:57:31 +02005337 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5338 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005339 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005340 return -1;
5341 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005342
5343 return 0;
5344}
5345
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005346static void
5347fini_egl(struct display *display)
5348{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005349 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005350
5351 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5352 EGL_NO_CONTEXT);
5353
5354 eglTerminate(display->dpy);
5355 eglReleaseThread();
5356}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005357#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005358
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005359static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005360init_dummy_surface(struct display *display)
5361{
5362 int len;
5363 void *data;
5364
5365 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5366 data = malloc(len);
5367 display->dummy_surface =
5368 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5369 1, 1, len);
5370 display->dummy_surface_data = data;
5371}
5372
5373static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005374handle_display_data(struct task *task, uint32_t events)
5375{
5376 struct display *display =
5377 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005378 struct epoll_event ep;
5379 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005380
5381 display->display_fd_events = events;
5382
5383 if (events & EPOLLERR || events & EPOLLHUP) {
5384 display_exit(display);
5385 return;
5386 }
5387
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005388 if (events & EPOLLIN) {
5389 ret = wl_display_dispatch(display->display);
5390 if (ret == -1) {
5391 display_exit(display);
5392 return;
5393 }
5394 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005395
5396 if (events & EPOLLOUT) {
5397 ret = wl_display_flush(display->display);
5398 if (ret == 0) {
5399 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5400 ep.data.ptr = &display->display_task;
5401 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5402 display->display_fd, &ep);
5403 } else if (ret == -1 && errno != EAGAIN) {
5404 display_exit(display);
5405 return;
5406 }
5407 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005408}
5409
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005410static void
5411log_handler(const char *format, va_list args)
5412{
5413 vfprintf(stderr, format, args);
5414}
5415
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005416struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005417display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005418{
5419 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005420
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005421 wl_log_set_handler_client(log_handler);
5422
Peter Huttererf3d62272013-08-08 11:57:05 +10005423 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005424 if (d == NULL)
5425 return NULL;
5426
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005427 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005428 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005429 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005430 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005431 return NULL;
5432 }
5433
Rob Bradford5ab9c752013-07-26 16:29:43 +01005434 d->xkb_context = xkb_context_new(0);
5435 if (d->xkb_context == NULL) {
5436 fprintf(stderr, "Failed to create XKB context\n");
5437 free(d);
5438 return NULL;
5439 }
5440
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005441 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005442 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005443 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005444 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5445 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005446
5447 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005448 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005449 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005450 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005451
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005452 d->workspace = 0;
5453 d->workspace_count = 1;
5454
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005455 d->registry = wl_display_get_registry(d->display);
5456 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005457
5458 if (wl_display_dispatch(d->display) < 0) {
5459 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5460 return NULL;
5461 }
5462
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005463#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005464 if (init_egl(d) < 0)
5465 fprintf(stderr, "EGL does not seem to work, "
5466 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005467#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005468
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005469 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005470
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005471 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005472
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005473 wl_list_init(&d->window_list);
5474
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005475 init_dummy_surface(d);
5476
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005477 return d;
5478}
5479
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005480static void
5481display_destroy_outputs(struct display *display)
5482{
5483 struct output *tmp;
5484 struct output *output;
5485
5486 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5487 output_destroy(output);
5488}
5489
Pekka Paalanene1207c72011-12-16 12:02:09 +02005490static void
5491display_destroy_inputs(struct display *display)
5492{
5493 struct input *tmp;
5494 struct input *input;
5495
5496 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5497 input_destroy(input);
5498}
5499
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005500void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005501display_destroy(struct display *display)
5502{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005503 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005504 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5505 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005506
5507 if (!wl_list_empty(&display->deferred_list))
5508 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5509
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005510 cairo_surface_destroy(display->dummy_surface);
5511 free(display->dummy_surface_data);
5512
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005513 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005514 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005515
Daniel Stone97f68542012-05-30 16:32:01 +01005516 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005517
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005518 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005519 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005520
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005521#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005522 if (display->argb_device)
5523 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005524#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005525
Pekka Paalanen35e82632013-04-25 13:57:48 +03005526 if (display->subcompositor)
5527 wl_subcompositor_destroy(display->subcompositor);
5528
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005529 if (display->xdg_shell)
5530 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005531
5532 if (display->shm)
5533 wl_shm_destroy(display->shm);
5534
5535 if (display->data_device_manager)
5536 wl_data_device_manager_destroy(display->data_device_manager);
5537
5538 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005539 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005540
5541 close(display->epoll_fd);
5542
U. Artie Eoff44874d92012-10-02 21:12:35 -07005543 if (!(display->display_fd_events & EPOLLERR) &&
5544 !(display->display_fd_events & EPOLLHUP))
5545 wl_display_flush(display->display);
5546
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005547 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005548 free(display);
5549}
5550
5551void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005552display_set_user_data(struct display *display, void *data)
5553{
5554 display->user_data = data;
5555}
5556
5557void *
5558display_get_user_data(struct display *display)
5559{
5560 return display->user_data;
5561}
5562
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005563struct wl_display *
5564display_get_display(struct display *display)
5565{
5566 return display->display;
5567}
5568
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005569int
5570display_has_subcompositor(struct display *display)
5571{
5572 if (display->subcompositor)
5573 return 1;
5574
5575 wl_display_roundtrip(display->display);
5576
5577 return display->subcompositor != NULL;
5578}
5579
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005580cairo_device_t *
5581display_get_cairo_device(struct display *display)
5582{
5583 return display->argb_device;
5584}
5585
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005586struct output *
5587display_get_output(struct display *display)
5588{
5589 return container_of(display->output_list.next, struct output, link);
5590}
5591
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005592struct wl_compositor *
5593display_get_compositor(struct display *display)
5594{
5595 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005596}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005597
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005598uint32_t
5599display_get_serial(struct display *display)
5600{
5601 return display->serial;
5602}
5603
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005604EGLDisplay
5605display_get_egl_display(struct display *d)
5606{
5607 return d->dpy;
5608}
5609
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005610struct wl_data_source *
5611display_create_data_source(struct display *display)
5612{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005613 if (display->data_device_manager)
5614 return wl_data_device_manager_create_data_source(display->data_device_manager);
5615 else
5616 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005617}
5618
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005619EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005620display_get_argb_egl_config(struct display *d)
5621{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005622 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005623}
5624
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005625int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005626display_acquire_window_surface(struct display *display,
5627 struct window *window,
5628 EGLContext ctx)
5629{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005630 struct surface *surface = window->main_surface;
5631
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005632 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005633 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005634
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005635 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005636 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005637}
5638
5639void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005640display_release_window_surface(struct display *display,
5641 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005642{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005643 struct surface *surface = window->main_surface;
5644
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005645 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005646 return;
5647
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005648 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005649}
5650
5651void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005652display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005653{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005654 wl_list_insert(&display->deferred_list, &task->link);
5655}
5656
5657void
5658display_watch_fd(struct display *display,
5659 int fd, uint32_t events, struct task *task)
5660{
5661 struct epoll_event ep;
5662
5663 ep.events = events;
5664 ep.data.ptr = task;
5665 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5666}
5667
5668void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005669display_unwatch_fd(struct display *display, int fd)
5670{
5671 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5672}
5673
5674void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005675display_run(struct display *display)
5676{
5677 struct task *task;
5678 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005679 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005680
Pekka Paalanen826d7952011-12-15 10:14:07 +02005681 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005682 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005683 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005684 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005685 struct task, link);
5686 wl_list_remove(&task->link);
5687 task->run(task, 0);
5688 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005689
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005690 wl_display_dispatch_pending(display->display);
5691
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005692 if (!display->running)
5693 break;
5694
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005695 ret = wl_display_flush(display->display);
5696 if (ret < 0 && errno == EAGAIN) {
5697 ep[0].events =
5698 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5699 ep[0].data.ptr = &display->display_task;
5700
5701 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5702 display->display_fd, &ep[0]);
5703 } else if (ret < 0) {
5704 break;
5705 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005706
5707 count = epoll_wait(display->epoll_fd,
5708 ep, ARRAY_LENGTH(ep), -1);
5709 for (i = 0; i < count; i++) {
5710 task = ep[i].data.ptr;
5711 task->run(task, ep[i].events);
5712 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005713 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005714}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005715
5716void
5717display_exit(struct display *display)
5718{
5719 display->running = 0;
5720}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005721
5722void
5723keysym_modifiers_add(struct wl_array *modifiers_map,
5724 const char *name)
5725{
5726 size_t len = strlen(name) + 1;
5727 char *p;
5728
5729 p = wl_array_add(modifiers_map, len);
5730
5731 if (p == NULL)
5732 return;
5733
5734 strncpy(p, name, len);
5735}
5736
5737static xkb_mod_index_t
5738keysym_modifiers_get_index(struct wl_array *modifiers_map,
5739 const char *name)
5740{
5741 xkb_mod_index_t index = 0;
5742 char *p = modifiers_map->data;
5743
5744 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5745 if (strcmp(p, name) == 0)
5746 return index;
5747
5748 index++;
5749 p += strlen(p) + 1;
5750 }
5751
5752 return XKB_MOD_INVALID;
5753}
5754
5755xkb_mod_mask_t
5756keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5757 const char *name)
5758{
5759 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5760
5761 if (index == XKB_MOD_INVALID)
5762 return XKB_MOD_INVALID;
5763
5764 return 1 << index;
5765}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005766
5767void *
5768fail_on_null(void *p)
5769{
5770 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005771 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005772 exit(EXIT_FAILURE);
5773 }
5774
5775 return p;
5776}
5777
5778void *
5779xmalloc(size_t s)
5780{
5781 return fail_on_null(malloc(s));
5782}
5783
Peter Huttererf3d62272013-08-08 11:57:05 +10005784void *
5785xzalloc(size_t s)
5786{
5787 return fail_on_null(zalloc(s));
5788}
5789
Kristian Høgsbergce278412013-07-25 15:20:20 -07005790char *
5791xstrdup(const char *s)
5792{
5793 return fail_on_null(strdup(s));
5794}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005795
5796void *
5797xrealloc(char *p, size_t s)
5798{
5799 return fail_on_null(realloc(p, s));
5800}