blob: 9c48155df8209dd958d598b835d54b6a7cbde8e7 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Jasper St. Pierre0790e392013-12-09 14:58:00 -050068#include "xdg-shell-client-protocol.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060069#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020070#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030071#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050074
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070075struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030076
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040077struct global {
78 uint32_t name;
79 char *interface;
80 uint32_t version;
81 struct wl_list link;
82};
83
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050084struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040086 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050087 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030088 struct wl_subcompositor *subcompositor;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Jasper St. Pierre0790e392013-12-09 14:58:00 -050093 struct xdg_shell *xdg_shell;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050095 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020097 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040098 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040099
100 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700101 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400102 struct task display_task;
103
104 int epoll_fd;
105 struct wl_list deferred_list;
106
Pekka Paalanen826d7952011-12-15 10:14:07 +0200107 int running;
108
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400109 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400110 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500112 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400113
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400114 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400115
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300116 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300117 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400118
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200119 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400120 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800121 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200122
123 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100124
125 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200126
127 uint32_t workspace;
128 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200129
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200133
134 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100135 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500136};
137
Rob Bradford7507b572012-05-15 17:55:34 +0100138struct window_output {
139 struct output *output;
140 struct wl_list link;
141};
142
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200143struct toysurface {
144 /*
145 * Prepare the surface for drawing. Makes sure there is a surface
146 * of the right size available for rendering, and returns it.
147 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200148 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200149 * If flags has SURFACE_HINT_RESIZE set, the user is
150 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200151 * Returns the Cairo surface to draw to.
152 */
153 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200154 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200155 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156
157 /*
158 * Post the surface to the server, returning the server allocation
159 * rectangle. The Cairo surface from prepare() must be destroyed
160 * after calling this.
161 */
162 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200163 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200164 struct rectangle *server_allocation);
165
166 /*
167 * Make the toysurface current with the given EGL context.
168 * Returns 0 on success, and negative of failure.
169 */
170 int (*acquire)(struct toysurface *base, EGLContext ctx);
171
172 /*
173 * Release the toysurface from the EGL context, returning control
174 * to Cairo.
175 */
176 void (*release)(struct toysurface *base);
177
178 /*
179 * Destroy the toysurface, including the Cairo surface, any
180 * backing storage, and the Wayland protocol objects.
181 */
182 void (*destroy)(struct toysurface *base);
183};
184
Pekka Paalanen4e373742013-02-13 16:17:13 +0200185struct surface {
186 struct window *window;
187
188 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300189 struct wl_subsurface *subsurface;
190 int synchronized;
191 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200192 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200193 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300194 int redraw_needed;
195 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300196 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197
198 struct rectangle allocation;
199 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200200
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200201 struct wl_region *input_region;
202 struct wl_region *opaque_region;
203
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200204 enum window_buffer_type buffer_type;
205 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200206 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200207
208 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300209
210 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200211};
212
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500213struct window {
214 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100215 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500216 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200217 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400218 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500219 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500220 int x, y;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400221 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300222 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400223 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400224 int resize_needed;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500225 int custom;
226 int focused;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400227
Pekka Paalanen99436862012-11-19 17:15:59 +0200228 int resizing;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400229
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -0500230 int fullscreen;
231 int maximized;
232
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200233 enum preferred_format preferred_format;
234
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500235 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500236 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400237 window_data_handler_t data_handler;
238 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500239 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400240 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200241 window_output_handler_t output_handler;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700242 window_state_changed_handler_t state_changed_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400243
Pekka Paalanen4e373742013-02-13 16:17:13 +0200244 struct surface *main_surface;
Jasper St. Pierre0790e392013-12-09 14:58:00 -0500245 struct xdg_surface *xdg_surface;
246 struct xdg_popup *xdg_popup;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300247
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700248 struct window *parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -0500249
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500250 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500251
Pekka Paalanen35e82632013-04-25 13:57:48 +0300252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
254
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500255 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400256 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500257};
258
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500259struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500260 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200261 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300262 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500263 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400264 struct wl_list link;
265 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500270 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500271 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700272 widget_touch_down_handler_t touch_down_handler;
273 widget_touch_up_handler_t touch_up_handler;
274 widget_touch_motion_handler_t touch_motion_handler;
275 widget_touch_frame_handler_t touch_frame_handler;
276 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200277 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400278 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500279 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300280 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500281 int default_cursor;
Neil Roberts97b747c2013-12-19 16:17:12 +0000282 /* If this is set to false then no cairo surface will be
283 * created before redrawing the surface. This is useful if the
284 * redraw handler is going to do completely custom rendering
285 * such as using EGL directly */
286 int use_cairo;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400287};
288
Rusty Lynch041815a2013-08-08 21:20:38 -0700289struct touch_point {
290 int32_t id;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -0800291 float x, y;
Rusty Lynch041815a2013-08-08 21:20:38 -0700292 struct widget *widget;
293 struct wl_list link;
294};
295
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296struct input {
297 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303 struct window *pointer_focus;
304 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300306 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300309 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400310 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400311 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400312 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400313 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400315
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500316 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500317 struct widget *grab;
318 uint32_t grab_button;
319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800323 uint32_t touch_grab;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100328
329 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
335 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400336
Jonny Lamb06959082014-08-12 14:58:27 +0200337 int32_t repeat_rate_sec;
338 int32_t repeat_rate_nsec;
339 int32_t repeat_delay_sec;
340 int32_t repeat_delay_nsec;
341
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400342 struct task repeat_task;
343 int repeat_timer_fd;
344 uint32_t repeat_sym;
345 uint32_t repeat_key;
346 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400347};
348
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500349struct output {
350 struct display *display;
351 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800352 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500353 struct rectangle allocation;
354 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600355 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200356 int scale;
Jason Ekstrand738715d2014-04-02 19:53:50 -0500357 char *make;
358 char *model;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200359
360 display_output_handler_t destroy_handler;
361 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500362};
363
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500364struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500365 struct widget *widget;
366 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500367 struct frame *frame;
Xiong Zhangbfb4ade2014-06-12 11:06:25 +0800368
369 uint32_t last_time;
370 uint32_t did_double, double_click;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500371};
372
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500373struct menu {
Jasper St. Pierredda93132014-03-13 12:06:00 -0400374 void *user_data;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500375 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500376 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500377 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700378 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500379 const char **entries;
380 uint32_t time;
381 int current;
382 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400383 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500384 menu_func_t func;
385};
386
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300387struct tooltip {
388 struct widget *parent;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300389 struct widget *widget;
390 char *entry;
391 struct task tooltip_task;
392 int tooltip_fd;
393 float x, y;
394};
395
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700396struct shm_pool {
397 struct wl_shm_pool *pool;
398 size_t size;
399 size_t used;
400 void *data;
401};
402
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400403enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300404 CURSOR_DEFAULT = 100,
405 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400406};
407
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200408static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400409
Pekka Paalanen97777442013-05-22 10:20:05 +0300410/* #define DEBUG */
411
412#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300413
414static void
415debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
416__attribute__ ((format (printf, 4, 5)));
417
418static void
419debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
420{
421 va_list ap;
422 struct timeval tv;
423
424 gettimeofday(&tv, NULL);
425 fprintf(stderr, "%8ld.%03ld ",
426 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
427
428 if (proxy)
429 fprintf(stderr, "%s@%d ",
430 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
431
432 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
433 fprintf(stderr, "%s ", func);
434
435 va_start(ap, fmt);
436 vfprintf(stderr, fmt, ap);
437 va_end(ap);
438}
439
440#define DBG(fmt, ...) \
441 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
442
443#define DBG_OBJ(obj, fmt, ...) \
444 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
445
446#else
447
448#define DBG(...) do {} while (0)
449#define DBG_OBJ(...) do {} while (0)
450
451#endif
452
Alexander Larsson1818e312013-05-22 14:41:31 +0200453static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200454surface_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 +0200455{
456 int32_t tmp;
457
458 switch (buffer_transform) {
459 case WL_OUTPUT_TRANSFORM_90:
460 case WL_OUTPUT_TRANSFORM_270:
461 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
462 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
463 tmp = *width;
464 *width = *height;
465 *height = tmp;
466 break;
467 default:
468 break;
469 }
470
471 *width *= buffer_scale;
472 *height *= buffer_scale;
473}
474
475static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200476buffer_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 +0200477{
478 int32_t tmp;
479
480 switch (buffer_transform) {
481 case WL_OUTPUT_TRANSFORM_90:
482 case WL_OUTPUT_TRANSFORM_270:
483 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
484 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
485 tmp = *width;
486 *width = *height;
487 *height = tmp;
488 break;
489 default:
490 break;
491 }
492
493 *width /= buffer_scale;
494 *height /= buffer_scale;
495}
496
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500497#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400498
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200499struct egl_window_surface {
500 struct toysurface base;
501 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100502 struct display *display;
503 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200504 struct wl_egl_window *egl_window;
505 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100506};
507
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200508static struct egl_window_surface *
509to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100510{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100512}
513
514static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200516 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200517 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100518{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519 struct egl_window_surface *surface = to_egl_window_surface(base);
520
Alexander Larsson1818e312013-05-22 14:41:31 +0200521 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
522
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
524 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
525
526 return cairo_surface_reference(surface->cairo_surface);
527}
528
529static void
530egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200531 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200532 struct rectangle *server_allocation)
533{
534 struct egl_window_surface *surface = to_egl_window_surface(base);
535
536 cairo_gl_surface_swapbuffers(surface->cairo_surface);
537 wl_egl_window_get_attached_size(surface->egl_window,
538 &server_allocation->width,
539 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200540
541 buffer_to_surface_size (buffer_transform, buffer_scale,
542 &server_allocation->width,
543 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200544}
545
546static int
547egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
548{
549 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200550 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100551
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200552 device = cairo_surface_get_device(surface->cairo_surface);
553 if (!device)
554 return -1;
555
556 if (!ctx) {
557 if (device == surface->display->argb_device)
558 ctx = surface->display->argb_ctx;
559 else
560 assert(0);
561 }
562
563 cairo_device_flush(device);
564 cairo_device_acquire(device);
565 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
566 surface->egl_surface, ctx))
567 fprintf(stderr, "failed to make surface current\n");
568
569 return 0;
570}
571
572static void
573egl_window_surface_release(struct toysurface *base)
574{
575 struct egl_window_surface *surface = to_egl_window_surface(base);
576 cairo_device_t *device;
577
578 device = cairo_surface_get_device(surface->cairo_surface);
579 if (!device)
580 return;
581
Arnaud Vrac38d90be2014-08-25 20:56:43 +0200582 if (!eglMakeCurrent(surface->display->dpy,
583 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200584 fprintf(stderr, "failed to make context current\n");
585
586 cairo_device_release(device);
587}
588
589static void
590egl_window_surface_destroy(struct toysurface *base)
591{
592 struct egl_window_surface *surface = to_egl_window_surface(base);
593 struct display *d = surface->display;
594
595 cairo_surface_destroy(surface->cairo_surface);
596 eglDestroySurface(d->dpy, surface->egl_surface);
597 wl_egl_window_destroy(surface->egl_window);
598 surface->surface = NULL;
599
600 free(surface);
601}
602
603static struct toysurface *
604egl_window_surface_create(struct display *display,
605 struct wl_surface *wl_surface,
606 uint32_t flags,
607 struct rectangle *rectangle)
608{
609 struct egl_window_surface *surface;
610
Pekka Paalanenb3627362012-11-19 17:16:00 +0200611 if (display->dpy == EGL_NO_DISPLAY)
612 return NULL;
613
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200614 surface = calloc(1, sizeof *surface);
615 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100616 return NULL;
617
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200618 surface->base.prepare = egl_window_surface_prepare;
619 surface->base.swap = egl_window_surface_swap;
620 surface->base.acquire = egl_window_surface_acquire;
621 surface->base.release = egl_window_surface_release;
622 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200624 surface->display = display;
625 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400626
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200627 surface->egl_window = wl_egl_window_create(surface->surface,
628 rectangle->width,
629 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100630
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200631 surface->egl_surface = eglCreateWindowSurface(display->dpy,
632 display->argb_config,
633 surface->egl_window,
634 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100635
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200636 surface->cairo_surface =
637 cairo_gl_surface_create_for_egl(display->argb_device,
638 surface->egl_surface,
639 rectangle->width,
640 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100641
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200642 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100643}
644
Pekka Paalanenb3627362012-11-19 17:16:00 +0200645#else
646
647static struct toysurface *
648egl_window_surface_create(struct display *display,
649 struct wl_surface *wl_surface,
650 uint32_t flags,
651 struct rectangle *rectangle)
652{
653 return NULL;
654}
655
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400656#endif
657
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200658struct shm_surface_data {
659 struct wl_buffer *buffer;
660 struct shm_pool *pool;
661};
662
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400663struct wl_buffer *
664display_get_buffer_for_surface(struct display *display,
665 cairo_surface_t *surface)
666{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200667 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400668
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200669 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670
671 return data->buffer;
672}
673
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500674static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700675shm_pool_destroy(struct shm_pool *pool);
676
677static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400678shm_surface_data_destroy(void *p)
679{
680 struct shm_surface_data *data = p;
681
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200682 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700683 if (data->pool)
684 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300685
686 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400687}
688
Kristian Høgsberg16626282012-04-03 11:21:27 -0400689static struct wl_shm_pool *
690make_shm_pool(struct display *display, int size, void **data)
691{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400692 struct wl_shm_pool *pool;
693 int fd;
694
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300695 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400696 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300697 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
698 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400699 return NULL;
700 }
701
702 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400703 if (*data == MAP_FAILED) {
704 fprintf(stderr, "mmap failed: %m\n");
705 close(fd);
706 return NULL;
707 }
708
709 pool = wl_shm_create_pool(display->shm, fd, size);
710
711 close(fd);
712
713 return pool;
714}
715
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700716static struct shm_pool *
717shm_pool_create(struct display *display, size_t size)
718{
719 struct shm_pool *pool = malloc(sizeof *pool);
720
721 if (!pool)
722 return NULL;
723
724 pool->pool = make_shm_pool(display, size, &pool->data);
725 if (!pool->pool) {
726 free(pool);
727 return NULL;
728 }
729
730 pool->size = size;
731 pool->used = 0;
732
733 return pool;
734}
735
736static void *
737shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
738{
739 if (pool->used + size > pool->size)
740 return NULL;
741
742 *offset = pool->used;
743 pool->used += size;
744
745 return (char *) pool->data + *offset;
746}
747
748/* destroy the pool. this does not unmap the memory though */
749static void
750shm_pool_destroy(struct shm_pool *pool)
751{
752 munmap(pool->data, pool->size);
753 wl_shm_pool_destroy(pool->pool);
754 free(pool);
755}
756
757/* Start allocating from the beginning of the pool again */
758static void
759shm_pool_reset(struct shm_pool *pool)
760{
761 pool->used = 0;
762}
763
764static int
765data_length_for_shm_surface(struct rectangle *rect)
766{
767 int stride;
768
769 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
770 rect->width);
771 return stride * rect->height;
772}
773
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500774static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700775display_create_shm_surface_from_pool(struct display *display,
776 struct rectangle *rectangle,
777 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400778{
779 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400780 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400781 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200782 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700783 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400784 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400785
786 data = malloc(sizeof *data);
787 if (data == NULL)
788 return NULL;
789
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200790 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
791 cairo_format = CAIRO_FORMAT_RGB16_565;
792 else
793 cairo_format = CAIRO_FORMAT_ARGB32;
794
795 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700796 length = stride * rectangle->height;
797 data->pool = NULL;
798 map = shm_pool_allocate(pool, length, &offset);
799
800 if (!map) {
801 free(data);
802 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400803 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400805 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200806 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400807 rectangle->width,
808 rectangle->height,
809 stride);
810
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200811 cairo_surface_set_user_data(surface, &shm_surface_data_key,
812 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400813
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200814 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
815 format = WL_SHM_FORMAT_RGB565;
816 else {
817 if (flags & SURFACE_OPAQUE)
818 format = WL_SHM_FORMAT_XRGB8888;
819 else
820 format = WL_SHM_FORMAT_ARGB8888;
821 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400822
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200823 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
824 rectangle->width,
825 rectangle->height,
826 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400827
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700828 return surface;
829}
830
831static cairo_surface_t *
832display_create_shm_surface(struct display *display,
833 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200834 struct shm_pool *alternate_pool,
835 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700836{
837 struct shm_surface_data *data;
838 struct shm_pool *pool;
839 cairo_surface_t *surface;
840
Pekka Paalanen99436862012-11-19 17:15:59 +0200841 if (alternate_pool) {
842 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700843 surface = display_create_shm_surface_from_pool(display,
844 rectangle,
845 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200846 alternate_pool);
847 if (surface) {
848 data = cairo_surface_get_user_data(surface,
849 &shm_surface_data_key);
850 goto out;
851 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700852 }
853
854 pool = shm_pool_create(display,
855 data_length_for_shm_surface(rectangle));
856 if (!pool)
857 return NULL;
858
859 surface =
860 display_create_shm_surface_from_pool(display, rectangle,
861 flags, pool);
862
863 if (!surface) {
864 shm_pool_destroy(pool);
865 return NULL;
866 }
867
868 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200869 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700870 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400871
Pekka Paalanen99436862012-11-19 17:15:59 +0200872out:
873 if (data_ret)
874 *data_ret = data;
875
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400876 return surface;
877}
878
nobled7b87cb02011-02-01 18:51:47 +0000879static int
880check_size(struct rectangle *rect)
881{
882 if (rect->width && rect->height)
883 return 0;
884
885 fprintf(stderr, "tried to create surface of "
886 "width: %d, height: %d\n", rect->width, rect->height);
887 return -1;
888}
889
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400890cairo_surface_t *
891display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100892 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400893 struct rectangle *rectangle,
894 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400895{
nobled7b87cb02011-02-01 18:51:47 +0000896 if (check_size(rectangle) < 0)
897 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200898
899 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200900 return display_create_shm_surface(display, rectangle, flags,
901 NULL, NULL);
902}
903
Pekka Paalanena4eda732012-11-19 17:16:02 +0200904struct shm_surface_leaf {
905 cairo_surface_t *cairo_surface;
906 /* 'data' is automatically destroyed, when 'cairo_surface' is */
907 struct shm_surface_data *data;
908
909 struct shm_pool *resize_pool;
910 int busy;
911};
912
913static void
914shm_surface_leaf_release(struct shm_surface_leaf *leaf)
915{
916 if (leaf->cairo_surface)
917 cairo_surface_destroy(leaf->cairo_surface);
918 /* leaf->data already destroyed via cairo private */
919
920 if (leaf->resize_pool)
921 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200922
923 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200924}
925
Pekka Paalanenaef02542013-04-25 13:57:47 +0300926#define MAX_LEAVES 3
927
Pekka Paalanen99436862012-11-19 17:15:59 +0200928struct shm_surface {
929 struct toysurface base;
930 struct display *display;
931 struct wl_surface *surface;
932 uint32_t flags;
933 int dx, dy;
934
Pekka Paalanenaef02542013-04-25 13:57:47 +0300935 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200936 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200937};
938
939static struct shm_surface *
940to_shm_surface(struct toysurface *base)
941{
942 return container_of(base, struct shm_surface, base);
943}
944
Pekka Paalanena4eda732012-11-19 17:16:02 +0200945static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300946shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
947{
948#ifdef DEBUG
949 struct shm_surface_leaf *leaf;
950 char bufs[MAX_LEAVES + 1];
951 int i;
952
953 for (i = 0; i < MAX_LEAVES; i++) {
954 leaf = &surface->leaf[i];
955
956 if (leaf->busy)
957 bufs[i] = 'b';
958 else if (leaf->cairo_surface)
959 bufs[i] = 'a';
960 else
961 bufs[i] = ' ';
962 }
963
964 bufs[MAX_LEAVES] = '\0';
965 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
966#endif
967}
968
969static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200970shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
971{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200972 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300973 struct shm_surface_leaf *leaf;
974 int i;
975 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300976
977 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200978
Pekka Paalanenaef02542013-04-25 13:57:47 +0300979 for (i = 0; i < MAX_LEAVES; i++) {
980 leaf = &surface->leaf[i];
981 if (leaf->data && leaf->data->buffer == buffer) {
982 leaf->busy = 0;
983 break;
984 }
985 }
986 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200987
Pekka Paalanenaef02542013-04-25 13:57:47 +0300988 /* Leave one free leaf with storage, release others */
989 free_found = 0;
990 for (i = 0; i < MAX_LEAVES; i++) {
991 leaf = &surface->leaf[i];
992
993 if (!leaf->cairo_surface || leaf->busy)
994 continue;
995
996 if (!free_found)
997 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +0300998 else
Pekka Paalanenaef02542013-04-25 13:57:47 +0300999 shm_surface_leaf_release(leaf);
1000 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001001
Pekka Paalanen97777442013-05-22 10:20:05 +03001002 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001003}
1004
1005static const struct wl_buffer_listener shm_surface_buffer_listener = {
1006 shm_surface_buffer_release
1007};
1008
Pekka Paalanen99436862012-11-19 17:15:59 +02001009static cairo_surface_t *
1010shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001011 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001012 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001013{
Pekka Paalanenec076692012-11-30 13:37:27 +02001014 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001015 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001016 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001017 struct shm_surface_leaf *leaf = NULL;
1018 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001019
1020 surface->dx = dx;
1021 surface->dy = dy;
1022
Pekka Paalanenaef02542013-04-25 13:57:47 +03001023 /* pick a free buffer, preferrably one that already has storage */
1024 for (i = 0; i < MAX_LEAVES; i++) {
1025 if (surface->leaf[i].busy)
1026 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001027
Pekka Paalanenaef02542013-04-25 13:57:47 +03001028 if (!leaf || surface->leaf[i].cairo_surface)
1029 leaf = &surface->leaf[i];
1030 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001031 DBG_OBJ(surface->surface, "pick leaf %d\n",
1032 (int)(leaf - &surface->leaf[0]));
1033
Pekka Paalanenaef02542013-04-25 13:57:47 +03001034 if (!leaf) {
1035 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001036 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001037 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001038 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001039 }
1040
Pekka Paalanena4eda732012-11-19 17:16:02 +02001041 if (!resize_hint && leaf->resize_pool) {
1042 cairo_surface_destroy(leaf->cairo_surface);
1043 leaf->cairo_surface = NULL;
1044 shm_pool_destroy(leaf->resize_pool);
1045 leaf->resize_pool = NULL;
1046 }
1047
Alexander Larsson1818e312013-05-22 14:41:31 +02001048 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1049
Pekka Paalanena4eda732012-11-19 17:16:02 +02001050 if (leaf->cairo_surface &&
1051 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1052 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001053 goto out;
1054
Pekka Paalanena4eda732012-11-19 17:16:02 +02001055 if (leaf->cairo_surface)
1056 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001057
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001058#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001059 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001060 /* Create a big pool to allocate from, while continuously
1061 * resizing. Mmapping a new pool in the server
1062 * is relatively expensive, so reusing a pool performs
1063 * better, but may temporarily reserve unneeded memory.
1064 */
1065 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001066 leaf->resize_pool = shm_pool_create(surface->display,
1067 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001068 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001069#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001070
Alexander Larsson1818e312013-05-22 14:41:31 +02001071 rect.width = width;
1072 rect.height = height;
1073
Pekka Paalanena4eda732012-11-19 17:16:02 +02001074 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001075 display_create_shm_surface(surface->display, &rect,
1076 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001077 leaf->resize_pool,
1078 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001079 if (!leaf->cairo_surface)
1080 return NULL;
1081
Pekka Paalanena4eda732012-11-19 17:16:02 +02001082 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001083 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001084
1085out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001086 surface->current = leaf;
1087
1088 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001089}
1090
1091static void
1092shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001093 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001094 struct rectangle *server_allocation)
1095{
1096 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001097 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001098
1099 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001101 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001102 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001103
Alexander Larsson1818e312013-05-22 14:41:31 +02001104 buffer_to_surface_size (buffer_transform, buffer_scale,
1105 &server_allocation->width,
1106 &server_allocation->height);
1107
Pekka Paalanena4eda732012-11-19 17:16:02 +02001108 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001109 surface->dx, surface->dy);
1110 wl_surface_damage(surface->surface, 0, 0,
1111 server_allocation->width, server_allocation->height);
1112 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001113
Pekka Paalanen71233882013-04-25 13:57:53 +03001114 DBG_OBJ(surface->surface, "leaf %d busy\n",
1115 (int)(leaf - &surface->leaf[0]));
1116
Pekka Paalanena4eda732012-11-19 17:16:02 +02001117 leaf->busy = 1;
1118 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001119}
1120
1121static int
1122shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1123{
1124 return -1;
1125}
1126
1127static void
1128shm_surface_release(struct toysurface *base)
1129{
1130}
1131
1132static void
1133shm_surface_destroy(struct toysurface *base)
1134{
1135 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001136 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001137
Pekka Paalanenaef02542013-04-25 13:57:47 +03001138 for (i = 0; i < MAX_LEAVES; i++)
1139 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001140
1141 free(surface);
1142}
1143
1144static struct toysurface *
1145shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1146 uint32_t flags, struct rectangle *rectangle)
1147{
1148 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001149 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001150
Brian Lovinbc919262013-08-07 15:34:59 -07001151 surface = xmalloc(sizeof *surface);
1152 memset(surface, 0, sizeof *surface);
1153
Pekka Paalanen99436862012-11-19 17:15:59 +02001154 if (!surface)
1155 return NULL;
1156
1157 surface->base.prepare = shm_surface_prepare;
1158 surface->base.swap = shm_surface_swap;
1159 surface->base.acquire = shm_surface_acquire;
1160 surface->base.release = shm_surface_release;
1161 surface->base.destroy = shm_surface_destroy;
1162
1163 surface->display = display;
1164 surface->surface = wl_surface;
1165 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001166
1167 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001168}
1169
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001170/*
1171 * The following correspondences between file names and cursors was copied
1172 * from: https://bugs.kde.org/attachment.cgi?id=67313
1173 */
1174
1175static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001176 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001177 "sw-resize",
1178 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001179};
1180
1181static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001182 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001183 "se-resize",
1184 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001185};
1186
1187static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001188 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001189 "s-resize",
1190 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001191};
1192
1193static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001194 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001195 "closedhand",
1196 "208530c400c041818281048008011002"
1197};
1198
1199static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001200 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001201 "default",
1202 "top_left_arrow",
1203 "left-arrow"
1204};
1205
1206static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001207 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001208 "w-resize",
1209 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001210};
1211
1212static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001213 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001214 "e-resize",
1215 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001216};
1217
1218static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001219 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001220 "nw-resize",
1221 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001222};
1223
1224static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001225 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001226 "ne-resize",
1227 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001228};
1229
1230static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001231 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001232 "n-resize",
1233 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001234};
1235
1236static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001237 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001238 "ibeam",
1239 "text"
1240};
1241
1242static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001243 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001244 "pointer",
1245 "pointing_hand",
1246 "e29285e634086352946a0e7090d73106"
1247};
1248
1249static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001250 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001251 "wait",
1252 "0426c94ea35c87780ff01dc239897213"
1253};
1254
1255struct cursor_alternatives {
1256 const char **names;
1257 size_t count;
1258};
1259
1260static const struct cursor_alternatives cursors[] = {
1261 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1262 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1263 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1264 {grabbings, ARRAY_LENGTH(grabbings)},
1265 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1266 {left_sides, ARRAY_LENGTH(left_sides)},
1267 {right_sides, ARRAY_LENGTH(right_sides)},
1268 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1269 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1270 {top_sides, ARRAY_LENGTH(top_sides)},
1271 {xterms, ARRAY_LENGTH(xterms)},
1272 {hand1s, ARRAY_LENGTH(hand1s)},
1273 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001274};
1275
1276static void
1277create_cursors(struct display *display)
1278{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001279 struct weston_config *config;
1280 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001281 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001282 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001283 unsigned int i, j;
1284 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001285
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001286 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001287 s = weston_config_get_section(config, "shell", NULL, NULL);
1288 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1289 weston_config_section_get_int(s, "cursor-size", &size, 32);
1290 weston_config_destroy(config);
1291
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001292 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Hardening842a36a2014-03-18 14:12:50 +01001293 if (!display->cursor_theme) {
1294 fprintf(stderr, "could not load theme '%s'\n", theme);
1295 return;
1296 }
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001297 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001298 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001299 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001300
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001301 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001302 cursor = NULL;
1303 for (j = 0; !cursor && j < cursors[i].count; ++j)
1304 cursor = wl_cursor_theme_get_cursor(
1305 display->cursor_theme, cursors[i].names[j]);
1306
1307 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001308 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001309 cursors[i].names[0]);
1310
1311 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001312 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001313}
1314
1315static void
1316destroy_cursors(struct display *display)
1317{
1318 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001319 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001320}
1321
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001322struct wl_cursor_image *
1323display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001325 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001326
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001327 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001328}
1329
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001330static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001331surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001332{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001333 if (!surface->cairo_surface)
1334 return;
1335
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001336 if (surface->opaque_region) {
1337 wl_surface_set_opaque_region(surface->surface,
1338 surface->opaque_region);
1339 wl_region_destroy(surface->opaque_region);
1340 surface->opaque_region = NULL;
1341 }
1342
1343 if (surface->input_region) {
1344 wl_surface_set_input_region(surface->surface,
1345 surface->input_region);
1346 wl_region_destroy(surface->input_region);
1347 surface->input_region = NULL;
1348 }
1349
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001351 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001353
Pekka Paalanen89dee002013-02-13 16:17:20 +02001354 cairo_surface_destroy(surface->cairo_surface);
1355 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001356}
1357
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001358int
1359window_has_focus(struct window *window)
1360{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001361 return window->focused;
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001362}
1363
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05001364static void
1365window_close(struct window *window)
1366{
1367 if (window->close_handler)
1368 window->close_handler(window->user_data);
1369 else
1370 display_exit(window->display);
1371}
1372
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001373struct display *
1374window_get_display(struct window *window)
1375{
1376 return window->display;
1377}
1378
Pekka Paalanen89dee002013-02-13 16:17:20 +02001379static void
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001380surface_create_surface(struct surface *surface, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001381{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001382 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001383 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001384
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001385 if (!surface->toysurface && display->dpy &&
1386 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001387 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001388 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001389 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001390 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001391 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001392 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001393
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001394 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001395 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001396 surface->surface,
1397 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001398
Pekka Paalanen89dee002013-02-13 16:17:20 +02001399 surface->cairo_surface = surface->toysurface->prepare(
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001400 surface->toysurface, 0, 0,
Alexander Larsson1818e312013-05-22 14:41:31 +02001401 allocation.width, allocation.height, flags,
1402 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001403}
1404
1405static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001406window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001408 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001409 uint32_t flags = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410
Pekka Paalanenec076692012-11-30 13:37:27 +02001411 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001412 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001413
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001414 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1415 flags |= SURFACE_HINT_RGB565;
1416
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001417 surface_create_surface(surface, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001418}
1419
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001420int
1421window_get_buffer_transform(struct window *window)
1422{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001423 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001424}
1425
1426void
1427window_set_buffer_transform(struct window *window,
1428 enum wl_output_transform transform)
1429{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001430 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001431 wl_surface_set_buffer_transform(window->main_surface->surface,
1432 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001433}
1434
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001435void
1436window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001437 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001438{
1439 window->main_surface->buffer_scale = scale;
1440 wl_surface_set_buffer_scale(window->main_surface->surface,
1441 scale);
1442}
1443
1444uint32_t
1445window_get_buffer_scale(struct window *window)
1446{
1447 return window->main_surface->buffer_scale;
1448}
1449
Alexander Larssond68f5232013-05-22 14:41:33 +02001450uint32_t
1451window_get_output_scale(struct window *window)
1452{
1453 struct window_output *window_output;
1454 struct window_output *window_output_tmp;
1455 int scale = 1;
1456
1457 wl_list_for_each_safe(window_output, window_output_tmp,
1458 &window->window_output_list, link) {
1459 if (window_output->output->scale > scale)
1460 scale = window_output->output->scale;
1461 }
1462
1463 return scale;
1464}
1465
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001466static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001467
Pekka Paalanen4e373742013-02-13 16:17:13 +02001468static void
1469surface_destroy(struct surface *surface)
1470{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001471 if (surface->frame_cb)
1472 wl_callback_destroy(surface->frame_cb);
1473
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001474 if (surface->input_region)
1475 wl_region_destroy(surface->input_region);
1476
1477 if (surface->opaque_region)
1478 wl_region_destroy(surface->opaque_region);
1479
Pekka Paalanen35e82632013-04-25 13:57:48 +03001480 if (surface->subsurface)
1481 wl_subsurface_destroy(surface->subsurface);
1482
Pekka Paalanen4e373742013-02-13 16:17:13 +02001483 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001484
1485 if (surface->toysurface)
1486 surface->toysurface->destroy(surface->toysurface);
1487
Pekka Paalanen35e82632013-04-25 13:57:48 +03001488 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001489 free(surface);
1490}
1491
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001492void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001493window_destroy(struct window *window)
1494{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001495 struct display *display = window->display;
1496 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001497 struct window_output *window_output;
1498 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001499
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001500 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001501
Rusty Lynch1084da52013-08-15 09:10:08 -07001502 wl_list_for_each(input, &display->input_list, link) {
1503 if (input->touch_focus == window)
1504 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001505 if (input->pointer_focus == window)
1506 input->pointer_focus = NULL;
1507 if (input->keyboard_focus == window)
1508 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001509 if (input->focus_widget &&
1510 input->focus_widget->window == window)
1511 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001512 }
1513
Rob Bradford7507b572012-05-15 17:55:34 +01001514 wl_list_for_each_safe(window_output, window_output_tmp,
1515 &window->window_output_list, link) {
1516 free (window_output);
1517 }
1518
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001519 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001520 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001521
Jasper St. Pierre0790e392013-12-09 14:58:00 -05001522 if (window->xdg_surface)
1523 xdg_surface_destroy(window->xdg_surface);
1524 if (window->xdg_popup)
1525 xdg_popup_destroy(window->xdg_popup);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001526
1527 surface_destroy(window->main_surface);
1528
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001529 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001530
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001531 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001532 free(window);
1533}
1534
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001535static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001536widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001537{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001538 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001539
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001540 wl_list_for_each(child, &widget->child_list, link) {
1541 target = widget_find_widget(child, x, y);
1542 if (target)
1543 return target;
1544 }
1545
1546 if (widget->allocation.x <= x &&
1547 x < widget->allocation.x + widget->allocation.width &&
1548 widget->allocation.y <= y &&
1549 y < widget->allocation.y + widget->allocation.height) {
1550 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001551 }
1552
1553 return NULL;
1554}
1555
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001556static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001557window_find_widget(struct window *window, int32_t x, int32_t y)
1558{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001559 struct surface *surface;
1560 struct widget *widget;
1561
1562 wl_list_for_each(surface, &window->subsurface_list, link) {
1563 widget = widget_find_widget(surface->widget, x, y);
1564 if (widget)
1565 return widget;
1566 }
1567
1568 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001569}
1570
1571static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001572widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001573{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001574 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001575
Peter Huttererf3d62272013-08-08 11:57:05 +10001576 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001577 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001578 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001579 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001580 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001581 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001582 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001583 widget->tooltip = NULL;
1584 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001585 widget->default_cursor = CURSOR_LEFT_PTR;
Neil Roberts97b747c2013-12-19 16:17:12 +00001586 widget->use_cairo = 1;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001587
1588 return widget;
1589}
1590
1591struct widget *
1592window_add_widget(struct window *window, void *data)
1593{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001594 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001595
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001596 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001597 wl_list_init(&widget->link);
1598 window->main_surface->widget = widget;
1599
1600 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001601}
1602
1603struct widget *
1604widget_add_widget(struct widget *parent, void *data)
1605{
1606 struct widget *widget;
1607
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001608 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001609 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001610
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001611 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001612}
1613
1614void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001615widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001616{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001617 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001618 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001619 struct input *input;
1620
Pekka Paalanen35e82632013-04-25 13:57:48 +03001621 /* Destroy the sub-surface along with the root widget */
1622 if (surface->widget == widget && surface->subsurface)
1623 surface_destroy(widget->surface);
1624
Kristian Høgsbergb637a402014-01-10 00:27:35 -08001625 if (widget->tooltip)
1626 widget_destroy_tooltip(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001627
Pekka Paalanene156fb62012-01-19 13:51:38 +02001628 wl_list_for_each(input, &display->input_list, link) {
1629 if (input->focus_widget == widget)
1630 input->focus_widget = NULL;
1631 }
1632
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001633 wl_list_remove(&widget->link);
1634 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001635}
1636
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001637void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001638widget_set_default_cursor(struct widget *widget, int cursor)
1639{
1640 widget->default_cursor = cursor;
1641}
1642
1643void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001644widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001645{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001646 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001647}
1648
1649void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001650widget_set_size(struct widget *widget, int32_t width, int32_t height)
1651{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001652 widget->allocation.width = width;
1653 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001654}
1655
1656void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001657widget_set_allocation(struct widget *widget,
1658 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001659{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001660 widget->allocation.x = x;
1661 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001662 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663}
1664
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001665void
1666widget_set_transparent(struct widget *widget, int transparent)
1667{
1668 widget->opaque = !transparent;
1669}
1670
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001671void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001672widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001673{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001674 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001675}
1676
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001677static cairo_surface_t *
1678widget_get_cairo_surface(struct widget *widget)
1679{
1680 struct surface *surface = widget->surface;
1681 struct window *window = widget->window;
1682
Neil Roberts97b747c2013-12-19 16:17:12 +00001683 assert(widget->use_cairo);
1684
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001685 if (!surface->cairo_surface) {
1686 if (surface == window->main_surface)
1687 window_create_main_surface(window);
1688 else
Jasper St. Pierree81a1752014-03-10 11:35:48 -04001689 surface_create_surface(surface, 0);
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001690 }
1691
1692 return surface->cairo_surface;
1693}
1694
Alexander Larsson15901f02013-05-22 14:41:25 +02001695static void
1696widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1697{
1698 struct surface *surface = widget->surface;
1699 double angle;
1700 cairo_matrix_t m;
1701 enum wl_output_transform transform;
1702 int surface_width, surface_height;
1703 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001704 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001705
1706 surface_width = surface->allocation.width;
1707 surface_height = surface->allocation.height;
1708
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001709 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001710 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001711
Alexander Larsson15901f02013-05-22 14:41:25 +02001712 switch (transform) {
1713 case WL_OUTPUT_TRANSFORM_FLIPPED:
1714 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1715 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1716 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1717 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1718 break;
1719 default:
1720 cairo_matrix_init_identity(&m);
1721 break;
1722 }
1723
1724 switch (transform) {
1725 case WL_OUTPUT_TRANSFORM_NORMAL:
1726 default:
1727 angle = 0;
1728 translate_x = 0;
1729 translate_y = 0;
1730 break;
1731 case WL_OUTPUT_TRANSFORM_FLIPPED:
1732 angle = 0;
1733 translate_x = surface_width;
1734 translate_y = 0;
1735 break;
1736 case WL_OUTPUT_TRANSFORM_90:
1737 angle = M_PI_2;
1738 translate_x = surface_height;
1739 translate_y = 0;
1740 break;
1741 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1742 angle = M_PI_2;
1743 translate_x = surface_height;
1744 translate_y = surface_width;
1745 break;
1746 case WL_OUTPUT_TRANSFORM_180:
1747 angle = M_PI;
1748 translate_x = surface_width;
1749 translate_y = surface_height;
1750 break;
1751 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1752 angle = M_PI;
1753 translate_x = 0;
1754 translate_y = surface_height;
1755 break;
1756 case WL_OUTPUT_TRANSFORM_270:
1757 angle = M_PI + M_PI_2;
1758 translate_x = 0;
1759 translate_y = surface_width;
1760 break;
1761 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1762 angle = M_PI + M_PI_2;
1763 translate_x = 0;
1764 translate_y = 0;
1765 break;
1766 }
1767
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001768 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001769 cairo_translate(cr, translate_x, translate_y);
1770 cairo_rotate(cr, angle);
1771 cairo_transform(cr, &m);
1772}
1773
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001774cairo_t *
1775widget_cairo_create(struct widget *widget)
1776{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001777 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001778 cairo_surface_t *cairo_surface;
1779 cairo_t *cr;
1780
1781 cairo_surface = widget_get_cairo_surface(widget);
1782 cr = cairo_create(cairo_surface);
1783
Alexander Larsson15901f02013-05-22 14:41:25 +02001784 widget_cairo_update_transform(widget, cr);
1785
Pekka Paalanen35e82632013-04-25 13:57:48 +03001786 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1787
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001788 return cr;
1789}
1790
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001791struct wl_surface *
1792widget_get_wl_surface(struct widget *widget)
1793{
1794 return widget->surface->surface;
1795}
1796
Neil Roberts5e10a042013-09-09 00:40:17 +01001797struct wl_subsurface *
1798widget_get_wl_subsurface(struct widget *widget)
1799{
1800 return widget->surface->subsurface;
1801}
1802
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001803uint32_t
1804widget_get_last_time(struct widget *widget)
1805{
1806 return widget->surface->last_time;
1807}
1808
1809void
1810widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1811{
1812 struct wl_compositor *comp = widget->window->display->compositor;
1813 struct surface *surface = widget->surface;
1814
1815 if (!surface->input_region)
1816 surface->input_region = wl_compositor_create_region(comp);
1817
1818 if (rect) {
1819 wl_region_add(surface->input_region,
1820 rect->x, rect->y, rect->width, rect->height);
1821 }
1822}
1823
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001824void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001825widget_set_resize_handler(struct widget *widget,
1826 widget_resize_handler_t handler)
1827{
1828 widget->resize_handler = handler;
1829}
1830
1831void
1832widget_set_redraw_handler(struct widget *widget,
1833 widget_redraw_handler_t handler)
1834{
1835 widget->redraw_handler = handler;
1836}
1837
1838void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001839widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001840{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001841 widget->enter_handler = handler;
1842}
1843
1844void
1845widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1846{
1847 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001848}
1849
1850void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001851widget_set_motion_handler(struct widget *widget,
1852 widget_motion_handler_t handler)
1853{
1854 widget->motion_handler = handler;
1855}
1856
1857void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001858widget_set_button_handler(struct widget *widget,
1859 widget_button_handler_t handler)
1860{
1861 widget->button_handler = handler;
1862}
1863
1864void
Rusty Lynch041815a2013-08-08 21:20:38 -07001865widget_set_touch_up_handler(struct widget *widget,
1866 widget_touch_up_handler_t handler)
1867{
1868 widget->touch_up_handler = handler;
1869}
1870
1871void
1872widget_set_touch_down_handler(struct widget *widget,
1873 widget_touch_down_handler_t handler)
1874{
1875 widget->touch_down_handler = handler;
1876}
1877
1878void
1879widget_set_touch_motion_handler(struct widget *widget,
1880 widget_touch_motion_handler_t handler)
1881{
1882 widget->touch_motion_handler = handler;
1883}
1884
1885void
1886widget_set_touch_frame_handler(struct widget *widget,
1887 widget_touch_frame_handler_t handler)
1888{
1889 widget->touch_frame_handler = handler;
1890}
1891
1892void
1893widget_set_touch_cancel_handler(struct widget *widget,
1894 widget_touch_cancel_handler_t handler)
1895{
1896 widget->touch_cancel_handler = handler;
1897}
1898
1899void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001900widget_set_axis_handler(struct widget *widget,
1901 widget_axis_handler_t handler)
1902{
1903 widget->axis_handler = handler;
1904}
1905
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001906static void
1907window_schedule_redraw_task(struct window *window);
1908
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001909void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001910widget_schedule_redraw(struct widget *widget)
1911{
Pekka Paalanen71233882013-04-25 13:57:53 +03001912 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001913 widget->surface->redraw_needed = 1;
1914 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001915}
1916
Neil Roberts97b747c2013-12-19 16:17:12 +00001917void
1918widget_set_use_cairo(struct widget *widget,
1919 int use_cairo)
1920{
1921 widget->use_cairo = use_cairo;
1922}
1923
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001924cairo_surface_t *
1925window_get_surface(struct window *window)
1926{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001927 cairo_surface_t *cairo_surface;
1928
1929 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1930
1931 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001932}
1933
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001934struct wl_surface *
1935window_get_wl_surface(struct window *window)
1936{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001937 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001938}
1939
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001940static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001941tooltip_redraw_handler(struct widget *widget, void *data)
1942{
1943 cairo_t *cr;
1944 const int32_t r = 3;
1945 struct tooltip *tooltip = data;
1946 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001947
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001948 cr = widget_cairo_create(widget);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001949 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001950 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1951 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1952 cairo_paint(cr);
1953
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001954 width = widget->allocation.width;
1955 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001956 rounded_rect(cr, 0, 0, width, height, r);
1957
1958 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1959 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1960 cairo_fill(cr);
1961
1962 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1963 cairo_move_to(cr, 10, 16);
1964 cairo_show_text(cr, tooltip->entry);
1965 cairo_destroy(cr);
1966}
1967
1968static cairo_text_extents_t
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001969get_text_extents(struct display *display, struct tooltip *tooltip)
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001970{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001971 cairo_t *cr;
1972 cairo_text_extents_t extents;
1973
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001974 /* Use the dummy_surface because tooltip's surface was not
1975 * created yet, and parent does not have a valid surface
1976 * outside repaint, either.
1977 */
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001978 cr = cairo_create(display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001979 cairo_text_extents(cr, tooltip->entry, &extents);
1980 cairo_destroy(cr);
1981
1982 return extents;
1983}
1984
1985static int
1986window_create_tooltip(struct tooltip *tooltip)
1987{
1988 struct widget *parent = tooltip->parent;
1989 struct display *display = parent->window->display;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001990 const int offset_y = 27;
1991 const int margin = 3;
1992 cairo_text_extents_t extents;
1993
1994 if (tooltip->widget)
1995 return 0;
1996
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001997 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001998
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05001999 extents = get_text_extents(display, tooltip);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002000 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
Jasper St. Pierre7f4386e2013-11-11 19:42:23 -05002001 widget_set_allocation(tooltip->widget,
2002 tooltip->x, tooltip->y + offset_y,
2003 extents.width + 20, 20 + margin * 2);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002004
2005 return 0;
2006}
2007
2008void
2009widget_destroy_tooltip(struct widget *parent)
2010{
2011 struct tooltip *tooltip = parent->tooltip;
2012
2013 parent->tooltip_count = 0;
2014 if (!tooltip)
2015 return;
2016
2017 if (tooltip->widget) {
2018 widget_destroy(tooltip->widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002020 }
2021
2022 close(tooltip->tooltip_fd);
2023 free(tooltip->entry);
2024 free(tooltip);
2025 parent->tooltip = NULL;
2026}
2027
2028static void
2029tooltip_func(struct task *task, uint32_t events)
2030{
2031 struct tooltip *tooltip =
2032 container_of(task, struct tooltip, tooltip_task);
2033 uint64_t exp;
2034
Martin Olsson8df662a2012-07-08 03:03:47 +02002035 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2036 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002037 window_create_tooltip(tooltip);
2038}
2039
2040#define TOOLTIP_TIMEOUT 500
2041static int
2042tooltip_timer_reset(struct tooltip *tooltip)
2043{
2044 struct itimerspec its;
2045
2046 its.it_interval.tv_sec = 0;
2047 its.it_interval.tv_nsec = 0;
2048 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2049 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2050 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2051 fprintf(stderr, "could not set timerfd\n: %m");
2052 return -1;
2053 }
2054
2055 return 0;
2056}
2057
2058int
2059widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2060{
2061 struct tooltip *tooltip = parent->tooltip;
2062
2063 parent->tooltip_count++;
2064 if (tooltip) {
2065 tooltip->x = x;
2066 tooltip->y = y;
2067 tooltip_timer_reset(tooltip);
2068 return 0;
2069 }
2070
2071 /* the handler might be triggered too fast via input device motion, so
2072 * we need this check here to make sure tooltip is fully initialized */
2073 if (parent->tooltip_count > 1)
2074 return 0;
2075
2076 tooltip = malloc(sizeof *tooltip);
2077 if (!tooltip)
2078 return -1;
2079
2080 parent->tooltip = tooltip;
2081 tooltip->parent = parent;
2082 tooltip->widget = NULL;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002083 tooltip->x = x;
2084 tooltip->y = y;
2085 tooltip->entry = strdup(entry);
2086 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2087 if (tooltip->tooltip_fd < 0) {
2088 fprintf(stderr, "could not create timerfd\n: %m");
2089 return -1;
2090 }
2091
2092 tooltip->tooltip_task.run = tooltip_func;
2093 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2094 EPOLLIN, &tooltip->tooltip_task);
2095 tooltip_timer_reset(tooltip);
2096
2097 return 0;
2098}
2099
2100static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002101workspace_manager_state(void *data,
2102 struct workspace_manager *workspace_manager,
2103 uint32_t current,
2104 uint32_t count)
2105{
2106 struct display *display = data;
2107
2108 display->workspace = current;
2109 display->workspace_count = count;
2110}
2111
2112static const struct workspace_manager_listener workspace_manager_listener = {
2113 workspace_manager_state
2114};
2115
2116static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002117frame_resize_handler(struct widget *widget,
2118 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002119{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002120 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002121 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002122 struct rectangle interior;
2123 struct rectangle input;
2124 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002125
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002126 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002127 interior.x = 0;
2128 interior.y = 0;
2129 interior.width = width;
2130 interior.height = height;
2131 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002132 frame_resize(frame->frame, width, height);
2133 frame_interior(frame->frame, &interior.x, &interior.y,
2134 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002135 }
2136
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002137 widget_set_allocation(child, interior.x, interior.y,
2138 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002139
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002140 if (child->resize_handler) {
2141 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002142 child->user_data);
2143
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002144 if (widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002145 width = child->allocation.width;
2146 height = child->allocation.height;
2147 } else {
2148 frame_resize_inside(frame->frame,
2149 child->allocation.width,
2150 child->allocation.height);
2151 width = frame_width(frame->frame);
2152 height = frame_height(frame->frame);
2153 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002154 }
2155
Scott Moreauf7e498c2012-05-14 11:39:29 -06002156 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002157
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002158 widget->surface->input_region =
2159 wl_compositor_create_region(widget->window->display->compositor);
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002160 if (!widget->window->fullscreen) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002161 frame_input_rect(frame->frame, &input.x, &input.y,
2162 &input.width, &input.height);
2163 wl_region_add(widget->surface->input_region,
2164 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002165 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002166 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002167 }
2168
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002169 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002170
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002171 if (child->opaque) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002172 if (!widget->window->fullscreen) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002173 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2174 &opaque.width, &opaque.height);
2175
2176 wl_region_add(widget->surface->opaque_region,
2177 opaque.x, opaque.y,
2178 opaque.width, opaque.height);
2179 } else {
2180 wl_region_add(widget->surface->opaque_region,
2181 0, 0, width, height);
2182 }
Martin Minarik1998b152012-05-10 02:04:35 +02002183 }
2184
Martin Minarik1998b152012-05-10 02:04:35 +02002185
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002186 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002187}
2188
2189static void
2190frame_redraw_handler(struct widget *widget, void *data)
2191{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002192 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002193 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002194 struct window *window = widget->window;
2195
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002196 if (window->fullscreen)
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002197 return;
2198
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002199 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002200
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002201 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002202
2203 cairo_destroy(cr);
2204}
2205
2206static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002207frame_get_pointer_image_for_location(struct window_frame *frame,
2208 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002209{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002210 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002211
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002212 if (window->custom)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002213 return CURSOR_LEFT_PTR;
2214
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002215 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002216 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002217 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002218 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002219 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002220 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002221 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002222 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002223 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002224 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002225 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002226 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002227 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002228 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002229 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002230 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002231 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002232 case THEME_LOCATION_EXTERIOR:
2233 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002234 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002235 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002236 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002237}
2238
Pekka Paalanen26237862014-09-10 15:10:30 +03002239static void
2240frame_menu_func(void *data, struct input *input, int index)
2241{
2242 struct window *window = data;
2243 struct display *display;
2244
2245 switch (index) {
2246 case 0: /* close */
2247 window_close(window);
2248 break;
2249 case 1: /* move to workspace above */
2250 display = window->display;
2251 if (display->workspace > 0)
2252 workspace_manager_move_surface(
2253 display->workspace_manager,
2254 window->main_surface->surface,
2255 display->workspace - 1);
2256 break;
2257 case 2: /* move to workspace below */
2258 display = window->display;
2259 if (display->workspace < display->workspace_count - 1)
2260 workspace_manager_move_surface(
2261 display->workspace_manager,
2262 window->main_surface->surface,
2263 display->workspace + 1);
2264 break;
2265 case 3: /* fullscreen */
2266 /* we don't have a way to get out of fullscreen for now */
2267 if (window->fullscreen_handler)
2268 window->fullscreen_handler(window, window->user_data);
2269 break;
2270 }
2271}
2272
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002273void
2274window_show_frame_menu(struct window *window,
2275 struct input *input, uint32_t time)
2276{
2277 int32_t x, y;
Pekka Paalanen26237862014-09-10 15:10:30 +03002278 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002279
Pekka Paalanen26237862014-09-10 15:10:30 +03002280 static const char *entries[] = {
2281 "Close",
2282 "Move to workspace above", "Move to workspace below",
2283 "Fullscreen"
2284 };
2285
2286 if (window->fullscreen_handler)
2287 count = ARRAY_LENGTH(entries);
2288 else
2289 count = ARRAY_LENGTH(entries) - 1;
2290
2291 input_get_position(input, &x, &y);
2292 window_show_menu(window->display, input, time, window,
2293 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002294}
2295
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002296static int
2297frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002298 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002299{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002300 struct window_frame *frame = data;
2301 enum theme_location location;
2302
2303 location = frame_pointer_enter(frame->frame, input, x, y);
2304 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2305 widget_schedule_redraw(frame->widget);
2306
2307 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002308}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002309
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002310static int
2311frame_motion_handler(struct widget *widget,
2312 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002313 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002314{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002315 struct window_frame *frame = data;
2316 enum theme_location location;
2317
2318 location = frame_pointer_motion(frame->frame, input, x, y);
2319 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2320 widget_schedule_redraw(frame->widget);
2321
2322 return frame_get_pointer_image_for_location(data, location);
2323}
2324
2325static void
2326frame_leave_handler(struct widget *widget,
2327 struct input *input, void *data)
2328{
2329 struct window_frame *frame = data;
2330
2331 frame_pointer_leave(frame->frame, input);
2332 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2333 widget_schedule_redraw(frame->widget);
2334}
2335
2336static void
2337frame_handle_status(struct window_frame *frame, struct input *input,
2338 uint32_t time, enum theme_location location)
2339{
2340 struct window *window = frame->widget->window;
2341 uint32_t status;
2342
2343 status = frame_status(frame->frame);
2344 if (status & FRAME_STATUS_REPAINT)
2345 widget_schedule_redraw(frame->widget);
2346
Jasper St. Pierre5a183322014-02-18 19:18:42 -05002347 if (status & FRAME_STATUS_MINIMIZE) {
2348 window_set_minimized(window);
2349 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2350 }
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002351
2352 if (status & FRAME_STATUS_MENU) {
2353 window_show_frame_menu(window, input, time);
2354 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2355 }
2356
2357 if (status & FRAME_STATUS_MAXIMIZE) {
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002358 window_set_maximized(window, !window->maximized);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002359 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2360 }
2361
2362 if (status & FRAME_STATUS_CLOSE) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002363 window_close(window);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002364 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002365 }
2366
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002367 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002368 input_ungrab(input);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002369 xdg_surface_move(window->xdg_surface,
2370 input_get_seat(input),
2371 window->display->serial);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002372
2373 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2374 }
2375
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002376 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002377 input_ungrab(input);
2378
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002379 xdg_surface_resize(window->xdg_surface,
2380 input_get_seat(input),
2381 window->display->serial,
2382 location);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002383
2384 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2385 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002386}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002387
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002388#define DOUBLE_CLICK_PERIOD 250
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002389static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002390frame_button_handler(struct widget *widget,
2391 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002392 uint32_t button, enum wl_pointer_button_state state,
2393 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002394
2395{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002396 struct window_frame *frame = data;
2397 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002398
Xiong Zhangbfb4ade2014-06-12 11:06:25 +08002399 frame->double_click = 0;
2400 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2401 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2402 frame->double_click = 1;
2403 frame->did_double = 1;
2404 } else
2405 frame->did_double = 0;
2406
2407 frame->last_time = time;
2408 } else if (frame->did_double == 1) {
2409 frame->double_click = 1;
2410 frame->did_double = 0;
2411 }
2412
2413 if (frame->double_click)
2414 location = frame_double_click(frame->frame, input,
2415 button, state);
2416 else
2417 location = frame_pointer_button(frame->frame, input,
2418 button, state);
2419
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002420 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002421}
2422
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002423static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002424frame_touch_down_handler(struct widget *widget, struct input *input,
2425 uint32_t serial, uint32_t time, int32_t id,
2426 float x, float y, void *data)
2427{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002428 struct window_frame *frame = data;
2429
2430 frame_touch_down(frame->frame, input, id, x, y);
2431 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2432}
2433
2434static void
2435frame_touch_up_handler(struct widget *widget,
2436 struct input *input, uint32_t serial, uint32_t time,
2437 int32_t id, void *data)
2438{
2439 struct window_frame *frame = data;
2440
2441 frame_touch_up(frame->frame, input, id);
2442 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002443}
2444
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002445struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002446window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002447{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002448 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002449 uint32_t buttons;
2450
Jasper St. Pierre0790e392013-12-09 14:58:00 -05002451 if (window->custom) {
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002452 buttons = FRAME_BUTTON_NONE;
2453 } else {
2454 buttons = FRAME_BUTTON_ALL;
2455 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002456
Peter Huttererf3d62272013-08-08 11:57:05 +10002457 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002458 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002459 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002460
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002461 frame->widget = window_add_widget(window, frame);
2462 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002463
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002464 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2465 widget_set_resize_handler(frame->widget, frame_resize_handler);
2466 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002467 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002468 widget_set_motion_handler(frame->widget, frame_motion_handler);
2469 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002470 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002471 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002472
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002473 window->frame = frame;
2474
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002475 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002476}
2477
Kristian Høgsberga1627922012-06-20 17:30:03 -04002478void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002479window_frame_set_child_size(struct widget *widget, int child_width,
2480 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002481{
2482 struct display *display = widget->window->display;
2483 struct theme *t = display->theme;
2484 int decoration_width, decoration_height;
2485 int width, height;
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002486 int margin = widget->window->maximized ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002487
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002488 if (!widget->window->fullscreen) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002489 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002490 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002491 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002492
2493 width = child_width + decoration_width;
2494 height = child_height + decoration_height;
2495 } else {
2496 width = child_width;
2497 height = child_height;
2498 }
2499
2500 window_schedule_resize(widget->window, width, height);
2501}
2502
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002503static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002504window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002505{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002506 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002507
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002508 /* frame->child must be destroyed by the application */
2509 widget_destroy(frame->widget);
2510 free(frame);
2511}
2512
2513static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002514input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002515 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002516{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002517 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002518 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002519
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002520 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002521 return;
2522
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002523 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002524 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002525 widget = old;
2526 if (input->grab)
2527 widget = input->grab;
2528 if (widget->leave_handler)
2529 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002530 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002531 }
2532
2533 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002534 widget = focus;
2535 if (input->grab)
2536 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002537 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002538 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002539 cursor = widget->enter_handler(focus, input, x, y,
2540 widget->user_data);
2541 else
2542 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002543
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002544 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002545 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002546}
2547
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002548void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002549touch_grab(struct input *input, int32_t touch_id)
2550{
2551 input->touch_grab = 1;
2552 input->touch_grab_id = touch_id;
2553}
2554
2555void
2556touch_ungrab(struct input *input)
2557{
2558 struct touch_point *tp, *tmp;
2559
2560 input->touch_grab = 0;
2561
2562 wl_list_for_each_safe(tp, tmp,
2563 &input->touch_point_list, link) {
2564 if (tp->id != input->touch_grab_id)
2565 continue;
2566 wl_list_remove(&tp->link);
2567 free(tp);
2568
2569 return;
2570 }
2571}
2572
2573void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002574input_grab(struct input *input, struct widget *widget, uint32_t button)
2575{
2576 input->grab = widget;
2577 input->grab_button = button;
Kristian Høgsberg41f7ebc2014-04-29 14:11:26 -07002578
2579 input_set_focus_widget(input, widget, input->sx, input->sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002580}
2581
2582void
2583input_ungrab(struct input *input)
2584{
2585 struct widget *widget;
2586
2587 input->grab = NULL;
2588 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002589 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002590 input->sx, input->sy);
2591 input_set_focus_widget(input, widget, input->sx, input->sy);
2592 }
2593}
2594
2595static void
2596input_remove_pointer_focus(struct input *input)
2597{
2598 struct window *window = input->pointer_focus;
2599
2600 if (!window)
2601 return;
2602
2603 input_set_focus_widget(input, NULL, 0, 0);
2604
2605 input->pointer_focus = NULL;
2606 input->current_cursor = CURSOR_UNSET;
2607}
2608
2609static void
2610pointer_handle_enter(void *data, struct wl_pointer *pointer,
2611 uint32_t serial, struct wl_surface *surface,
2612 wl_fixed_t sx_w, wl_fixed_t sy_w)
2613{
2614 struct input *input = data;
2615 struct window *window;
2616 struct widget *widget;
2617 float sx = wl_fixed_to_double(sx_w);
2618 float sy = wl_fixed_to_double(sy_w);
2619
2620 if (!surface) {
2621 /* enter event for a window we've just destroyed */
2622 return;
2623 }
2624
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002625 window = wl_surface_get_user_data(surface);
2626 if (surface != window->main_surface->surface) {
2627 DBG("Ignoring input event from subsurface %p\n", surface);
2628 return;
2629 }
2630
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002631 input->display->serial = serial;
2632 input->pointer_enter_serial = serial;
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002633 input->pointer_focus = window;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002634
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002635 input->sx = sx;
2636 input->sy = sy;
2637
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002638 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002639 input_set_focus_widget(input, widget, sx, sy);
2640}
2641
2642static void
2643pointer_handle_leave(void *data, struct wl_pointer *pointer,
2644 uint32_t serial, struct wl_surface *surface)
2645{
2646 struct input *input = data;
2647
2648 input->display->serial = serial;
2649 input_remove_pointer_focus(input);
2650}
2651
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002652static void
Daniel Stone37816df2012-05-16 18:45:18 +01002653pointer_handle_motion(void *data, struct wl_pointer *pointer,
2654 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002655{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002656 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002657 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002658 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002659 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002660 float sx = wl_fixed_to_double(sx_w);
2661 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002662
Paul Winwoodb22bf572013-08-29 10:52:54 +01002663 if (!window)
2664 return;
2665
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03002666 input->sx = sx;
2667 input->sy = sy;
2668
Rob Bradford5f087742013-07-11 19:41:27 +01002669 /* when making the window smaller - e.g. after a unmaximise we might
2670 * still have a pending motion event that the compositor has picked
2671 * based on the old surface dimensions
2672 */
2673 if (sx > window->main_surface->allocation.width ||
2674 sy > window->main_surface->allocation.height)
2675 return;
2676
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002677 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002678 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002679 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002680 }
2681
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002682 if (input->grab)
2683 widget = input->grab;
2684 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002685 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002686 if (widget) {
2687 if (widget->motion_handler)
2688 cursor = widget->motion_handler(input->focus_widget,
2689 input, time, sx, sy,
2690 widget->user_data);
2691 else
2692 cursor = widget->default_cursor;
2693 } else
2694 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002695
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002696 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002697}
2698
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002699static void
Daniel Stone37816df2012-05-16 18:45:18 +01002700pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002701 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002702{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002703 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002704 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002705 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002706
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002707 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002708 if (input->focus_widget && input->grab == NULL &&
2709 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002710 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002711
Neil Roberts6b28aad2012-01-23 19:11:18 +00002712 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002713 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002714 (*widget->button_handler)(widget,
2715 input, time,
2716 button, state,
2717 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002718
Daniel Stone4dbadb12012-05-30 16:31:51 +01002719 if (input->grab && input->grab_button == button &&
2720 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002721 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002722}
2723
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002724static void
Daniel Stone37816df2012-05-16 18:45:18 +01002725pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002726 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002727{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002728 struct input *input = data;
2729 struct widget *widget;
2730
2731 widget = input->focus_widget;
2732 if (input->grab)
2733 widget = input->grab;
2734 if (widget && widget->axis_handler)
2735 (*widget->axis_handler)(widget,
2736 input, time,
2737 axis, value,
2738 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002739}
2740
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002741static const struct wl_pointer_listener pointer_listener = {
2742 pointer_handle_enter,
2743 pointer_handle_leave,
2744 pointer_handle_motion,
2745 pointer_handle_button,
2746 pointer_handle_axis,
2747};
2748
2749static void
2750input_remove_keyboard_focus(struct input *input)
2751{
2752 struct window *window = input->keyboard_focus;
2753 struct itimerspec its;
2754
2755 its.it_interval.tv_sec = 0;
2756 its.it_interval.tv_nsec = 0;
2757 its.it_value.tv_sec = 0;
2758 its.it_value.tv_nsec = 0;
2759 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2760
2761 if (!window)
2762 return;
2763
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002764 if (window->keyboard_focus_handler)
2765 (*window->keyboard_focus_handler)(window, NULL,
2766 window->user_data);
2767
2768 input->keyboard_focus = NULL;
2769}
2770
2771static void
2772keyboard_repeat_func(struct task *task, uint32_t events)
2773{
2774 struct input *input =
2775 container_of(task, struct input, repeat_task);
2776 struct window *window = input->keyboard_focus;
2777 uint64_t exp;
2778
2779 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2780 /* If we change the timer between the fd becoming
2781 * readable and getting here, there'll be nothing to
2782 * read and we get EAGAIN. */
2783 return;
2784
2785 if (window && window->key_handler) {
2786 (*window->key_handler)(window, input, input->repeat_time,
2787 input->repeat_key, input->repeat_sym,
2788 WL_KEYBOARD_KEY_STATE_PRESSED,
2789 window->user_data);
2790 }
2791}
2792
2793static void
2794keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2795 uint32_t format, int fd, uint32_t size)
2796{
2797 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002798 struct xkb_keymap *keymap;
2799 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002800 char *map_str;
2801
2802 if (!data) {
2803 close(fd);
2804 return;
2805 }
2806
2807 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2808 close(fd);
2809 return;
2810 }
2811
2812 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2813 if (map_str == MAP_FAILED) {
2814 close(fd);
2815 return;
2816 }
2817
Ran Benita2e1968f2014-08-19 23:59:51 +03002818 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2819 map_str,
2820 XKB_KEYMAP_FORMAT_TEXT_V1,
2821 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002822 munmap(map_str, size);
2823 close(fd);
2824
Rui Matos3eccb862013-10-10 19:44:22 +02002825 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002826 fprintf(stderr, "failed to compile keymap\n");
2827 return;
2828 }
2829
Rui Matos3eccb862013-10-10 19:44:22 +02002830 state = xkb_state_new(keymap);
2831 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002832 fprintf(stderr, "failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +03002833 xkb_keymap_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002834 return;
2835 }
2836
Rui Matos3eccb862013-10-10 19:44:22 +02002837 xkb_keymap_unref(input->xkb.keymap);
2838 xkb_state_unref(input->xkb.state);
2839 input->xkb.keymap = keymap;
2840 input->xkb.state = state;
2841
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002842 input->xkb.control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002843 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002844 input->xkb.alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002845 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002846 input->xkb.shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +03002847 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002848}
2849
2850static void
2851keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2852 uint32_t serial, struct wl_surface *surface,
2853 struct wl_array *keys)
2854{
2855 struct input *input = data;
2856 struct window *window;
2857
2858 input->display->serial = serial;
2859 input->keyboard_focus = wl_surface_get_user_data(surface);
2860
2861 window = input->keyboard_focus;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002862 if (window->keyboard_focus_handler)
2863 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002864 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002865}
2866
2867static void
2868keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2869 uint32_t serial, struct wl_surface *surface)
2870{
2871 struct input *input = data;
2872
2873 input->display->serial = serial;
2874 input_remove_keyboard_focus(input);
2875}
2876
Scott Moreau210d0792012-03-22 10:47:01 -06002877static void
Daniel Stone37816df2012-05-16 18:45:18 +01002878keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002879 uint32_t serial, uint32_t time, uint32_t key,
2880 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002881{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002882 struct input *input = data;
2883 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002884 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002885 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002886 const xkb_keysym_t *syms;
2887 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002888 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002889
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002890 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002891 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002892 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002893 return;
2894
Kristian Høgsbergc31f6242014-04-29 14:19:20 -07002895 /* We only use input grabs for pointer events for now, so just
2896 * ignore key presses if a grab is active. We expand the key
2897 * event delivery mechanism to route events to widgets to
2898 * properly handle key grabs. In the meantime, this prevents
2899 * key event devlivery while a grab is active. */
2900 if (input->grab && input->grab_button == 0)
2901 return;
2902
Ran Benita2e1968f2014-08-19 23:59:51 +03002903 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002904
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002905 sym = XKB_KEY_NoSymbol;
2906 if (num_syms == 1)
2907 sym = syms[0];
2908
Kristian Høgsberg211b5172014-01-11 13:10:21 -08002909
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002910 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002911 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05002912 window_set_maximized(window, !window->maximized);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002913 } else if (sym == XKB_KEY_F11 &&
2914 window->fullscreen_handler &&
2915 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2916 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002917 } else if (sym == XKB_KEY_F4 &&
2918 input->modifiers == MOD_ALT_MASK &&
2919 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05002920 window_close(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002921 } else if (window->key_handler) {
2922 (*window->key_handler)(window, input, time, key,
2923 sym, state, window->user_data);
2924 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002925
2926 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2927 key == input->repeat_key) {
2928 its.it_interval.tv_sec = 0;
2929 its.it_interval.tv_nsec = 0;
2930 its.it_value.tv_sec = 0;
2931 its.it_value.tv_nsec = 0;
2932 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
Kristian Høgsbergd2a02132014-02-05 13:43:44 -08002933 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2934 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002935 input->repeat_sym = sym;
2936 input->repeat_key = key;
2937 input->repeat_time = time;
Jonny Lamb06959082014-08-12 14:58:27 +02002938 its.it_interval.tv_sec = input->repeat_rate_sec;
2939 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2940 its.it_value.tv_sec = input->repeat_delay_sec;
2941 its.it_value.tv_nsec = input->repeat_delay_nsec;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002942 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2943 }
2944}
2945
2946static void
Daniel Stone351eb612012-05-31 15:27:47 -04002947keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2948 uint32_t serial, uint32_t mods_depressed,
2949 uint32_t mods_latched, uint32_t mods_locked,
2950 uint32_t group)
2951{
2952 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002953 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002954
Matt Ropere61561f2013-06-24 16:52:43 +01002955 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2956 if (!input->xkb.keymap)
2957 return;
2958
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002959 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2960 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002961 mask = xkb_state_serialize_mods(input->xkb.state,
Ran Benita2e1968f2014-08-19 23:59:51 +03002962 XKB_STATE_MODS_DEPRESSED |
2963 XKB_STATE_MODS_LATCHED);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002964 input->modifiers = 0;
2965 if (mask & input->xkb.control_mask)
2966 input->modifiers |= MOD_CONTROL_MASK;
2967 if (mask & input->xkb.alt_mask)
2968 input->modifiers |= MOD_ALT_MASK;
2969 if (mask & input->xkb.shift_mask)
2970 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002971}
2972
Jonny Lamb06959082014-08-12 14:58:27 +02002973static void
2974set_repeat_info(struct input *input, int32_t rate, int32_t delay)
2975{
2976 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
2977 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
2978
2979 /* a rate of zero disables any repeating, regardless of the delay's
2980 * value */
2981 if (rate == 0)
2982 return;
2983
2984 if (rate == 1)
2985 input->repeat_rate_sec = 1;
2986 else
2987 input->repeat_rate_nsec = 1000000000 / rate;
2988
2989 input->repeat_delay_sec = delay / 1000;
2990 delay -= (input->repeat_delay_sec * 1000);
2991 input->repeat_delay_nsec = delay * 1000 * 1000;
2992}
2993
2994static void
2995keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
2996 int32_t rate, int32_t delay)
2997{
2998 struct input *input = data;
2999
3000 set_repeat_info(input, rate, delay);
3001}
3002
Daniel Stone37816df2012-05-16 18:45:18 +01003003static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003004 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003005 keyboard_handle_enter,
3006 keyboard_handle_leave,
3007 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003008 keyboard_handle_modifiers,
Jonny Lamb06959082014-08-12 14:58:27 +02003009 keyboard_handle_repeat_info
3010
Daniel Stone37816df2012-05-16 18:45:18 +01003011};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003012
3013static void
Rusty Lynch041815a2013-08-08 21:20:38 -07003014touch_handle_down(void *data, struct wl_touch *wl_touch,
3015 uint32_t serial, uint32_t time, struct wl_surface *surface,
3016 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3017{
3018 struct input *input = data;
3019 struct widget *widget;
3020 float sx = wl_fixed_to_double(x_w);
3021 float sy = wl_fixed_to_double(y_w);
3022
Rusty Lynch1084da52013-08-15 09:10:08 -07003023 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003024 input->touch_focus = wl_surface_get_user_data(surface);
3025 if (!input->touch_focus) {
3026 DBG("Failed to find to touch focus for surface %p\n", surface);
3027 return;
3028 }
3029
Ander Conselvan de Oliveiraa57c9f12014-05-06 15:25:40 +03003030 if (surface != input->touch_focus->main_surface->surface) {
3031 DBG("Ignoring input event from subsurface %p\n", surface);
3032 input->touch_focus = NULL;
3033 return;
3034 }
3035
Kristian Høgsberg1f671172014-04-29 14:30:44 -07003036 if (input->grab)
3037 widget = input->grab;
3038 else
3039 widget = window_find_widget(input->touch_focus,
3040 wl_fixed_to_double(x_w),
3041 wl_fixed_to_double(y_w));
Rusty Lynch041815a2013-08-08 21:20:38 -07003042 if (widget) {
3043 struct touch_point *tp = xmalloc(sizeof *tp);
3044 if (tp) {
3045 tp->id = id;
3046 tp->widget = widget;
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003047 tp->x = sx;
3048 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003049 wl_list_insert(&input->touch_point_list, &tp->link);
3050
3051 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003052 (*widget->touch_down_handler)(widget, input,
3053 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003054 sx, sy,
3055 widget->user_data);
3056 }
3057 }
3058}
3059
3060static void
3061touch_handle_up(void *data, struct wl_touch *wl_touch,
3062 uint32_t serial, uint32_t time, int32_t id)
3063{
3064 struct input *input = data;
3065 struct touch_point *tp, *tmp;
3066
Rusty Lynch041815a2013-08-08 21:20:38 -07003067 if (!input->touch_focus) {
3068 DBG("No touch focus found for touch up event!\n");
3069 return;
3070 }
3071
3072 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3073 if (tp->id != id)
3074 continue;
3075
3076 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003077 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003078 time, id,
3079 tp->widget->user_data);
3080
3081 wl_list_remove(&tp->link);
3082 free(tp);
3083
3084 return;
3085 }
3086}
3087
3088static void
3089touch_handle_motion(void *data, struct wl_touch *wl_touch,
3090 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3091{
3092 struct input *input = data;
3093 struct touch_point *tp;
3094 float sx = wl_fixed_to_double(x_w);
3095 float sy = wl_fixed_to_double(y_w);
3096
3097 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3098
3099 if (!input->touch_focus) {
3100 DBG("No touch focus found for touch motion event!\n");
3101 return;
3102 }
3103
3104 wl_list_for_each(tp, &input->touch_point_list, link) {
3105 if (tp->id != id)
3106 continue;
3107
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003108 tp->x = sx;
3109 tp->y = sy;
Rusty Lynch041815a2013-08-08 21:20:38 -07003110 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003111 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003112 id, sx, sy,
3113 tp->widget->user_data);
3114 return;
3115 }
3116}
3117
3118static void
3119touch_handle_frame(void *data, struct wl_touch *wl_touch)
3120{
3121 struct input *input = data;
3122 struct touch_point *tp, *tmp;
3123
3124 DBG("touch_handle_frame\n");
3125
3126 if (!input->touch_focus) {
3127 DBG("No touch focus found for touch frame event!\n");
3128 return;
3129 }
3130
3131 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3132 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003133 (*tp->widget->touch_frame_handler)(tp->widget, input,
3134 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003135 }
3136}
3137
3138static void
3139touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3140{
3141 struct input *input = data;
3142 struct touch_point *tp, *tmp;
3143
3144 DBG("touch_handle_cancel\n");
3145
3146 if (!input->touch_focus) {
3147 DBG("No touch focus found for touch cancel event!\n");
3148 return;
3149 }
3150
3151 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3152 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003153 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3154 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003155
3156 wl_list_remove(&tp->link);
3157 free(tp);
3158 }
3159}
3160
3161static const struct wl_touch_listener touch_listener = {
3162 touch_handle_down,
3163 touch_handle_up,
3164 touch_handle_motion,
3165 touch_handle_frame,
3166 touch_handle_cancel,
3167};
3168
3169static void
Daniel Stone37816df2012-05-16 18:45:18 +01003170seat_handle_capabilities(void *data, struct wl_seat *seat,
3171 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003172{
Daniel Stone37816df2012-05-16 18:45:18 +01003173 struct input *input = data;
3174
3175 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3176 input->pointer = wl_seat_get_pointer(seat);
3177 wl_pointer_set_user_data(input->pointer, input);
3178 wl_pointer_add_listener(input->pointer, &pointer_listener,
3179 input);
3180 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3181 wl_pointer_destroy(input->pointer);
3182 input->pointer = NULL;
3183 }
3184
3185 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3186 input->keyboard = wl_seat_get_keyboard(seat);
3187 wl_keyboard_set_user_data(input->keyboard, input);
3188 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3189 input);
3190 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3191 wl_keyboard_destroy(input->keyboard);
3192 input->keyboard = NULL;
3193 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003194
3195 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3196 input->touch = wl_seat_get_touch(seat);
3197 wl_touch_set_user_data(input->touch, input);
3198 wl_touch_add_listener(input->touch, &touch_listener, input);
3199 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3200 wl_touch_destroy(input->touch);
3201 input->touch = NULL;
3202 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003203}
3204
Rob Bradford08031182013-08-13 20:11:03 +01003205static void
3206seat_handle_name(void *data, struct wl_seat *seat,
3207 const char *name)
3208{
3209
3210}
3211
Daniel Stone37816df2012-05-16 18:45:18 +01003212static const struct wl_seat_listener seat_listener = {
3213 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003214 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003215};
3216
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003217void
3218input_get_position(struct input *input, int32_t *x, int32_t *y)
3219{
3220 *x = input->sx;
3221 *y = input->sy;
3222}
3223
Kristian Høgsbergef9c8eb2014-01-07 12:57:59 -08003224int
3225input_get_touch(struct input *input, int32_t id, float *x, float *y)
3226{
3227 struct touch_point *tp;
3228
3229 wl_list_for_each(tp, &input->touch_point_list, link) {
3230 if (tp->id != id)
3231 continue;
3232
3233 *x = tp->x;
3234 *y = tp->y;
3235 return 0;
3236 }
3237
3238 return -1;
3239}
3240
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003241struct display *
3242input_get_display(struct input *input)
3243{
3244 return input->display;
3245}
3246
Daniel Stone37816df2012-05-16 18:45:18 +01003247struct wl_seat *
3248input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003249{
Daniel Stone37816df2012-05-16 18:45:18 +01003250 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003251}
3252
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003253uint32_t
3254input_get_modifiers(struct input *input)
3255{
3256 return input->modifiers;
3257}
3258
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003259struct widget *
3260input_get_focus_widget(struct input *input)
3261{
3262 return input->focus_widget;
3263}
3264
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003265struct data_offer {
3266 struct wl_data_offer *offer;
3267 struct input *input;
3268 struct wl_array types;
3269 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003270
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003271 struct task io_task;
3272 int fd;
3273 data_func_t func;
3274 int32_t x, y;
3275 void *user_data;
3276};
3277
3278static void
3279data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3280{
3281 struct data_offer *offer = data;
3282 char **p;
3283
3284 p = wl_array_add(&offer->types, sizeof *p);
3285 *p = strdup(type);
3286}
3287
3288static const struct wl_data_offer_listener data_offer_listener = {
3289 data_offer_offer,
3290};
3291
3292static void
3293data_offer_destroy(struct data_offer *offer)
3294{
3295 char **p;
3296
3297 offer->refcount--;
3298 if (offer->refcount == 0) {
3299 wl_data_offer_destroy(offer->offer);
3300 for (p = offer->types.data; *p; p++)
3301 free(*p);
3302 wl_array_release(&offer->types);
3303 free(offer);
3304 }
3305}
3306
3307static void
3308data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003309 struct wl_data_device *data_device,
3310 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003311{
3312 struct data_offer *offer;
3313
Brian Lovinbc919262013-08-07 15:34:59 -07003314 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003315
3316 wl_array_init(&offer->types);
3317 offer->refcount = 1;
3318 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003319 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003320 wl_data_offer_add_listener(offer->offer,
3321 &data_offer_listener, offer);
3322}
3323
3324static void
3325data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003326 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003327 wl_fixed_t x_w, wl_fixed_t y_w,
3328 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003329{
3330 struct input *input = data;
3331 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003332 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003333 float x = wl_fixed_to_double(x_w);
3334 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003335 char **p;
3336
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003337 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003338 input->drag_enter_serial = serial;
3339 input->drag_focus = window,
3340 input->drag_x = x;
3341 input->drag_y = y;
3342
3343 if (!input->touch_grab)
3344 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003345
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003346 if (offer) {
3347 input->drag_offer = wl_data_offer_get_user_data(offer);
3348
3349 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3350 *p = NULL;
3351
3352 types_data = input->drag_offer->types.data;
3353 } else {
3354 input->drag_offer = NULL;
3355 types_data = NULL;
3356 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003357
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003358 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003359 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003360 window->user_data);
3361}
3362
3363static void
3364data_device_leave(void *data, struct wl_data_device *data_device)
3365{
3366 struct input *input = data;
3367
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003368 if (input->drag_offer) {
3369 data_offer_destroy(input->drag_offer);
3370 input->drag_offer = NULL;
3371 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003372}
3373
3374static void
3375data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003376 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003377{
3378 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003379 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003380 float x = wl_fixed_to_double(x_w);
3381 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003382 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003383
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003384 input->drag_x = x;
3385 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003386
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003387 if (input->drag_offer)
3388 types_data = input->drag_offer->types.data;
3389 else
3390 types_data = NULL;
3391
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003392 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003393 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003394 window->user_data);
3395}
3396
3397static void
3398data_device_drop(void *data, struct wl_data_device *data_device)
3399{
3400 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003401 struct window *window = input->drag_focus;
3402 float x, y;
3403
3404 x = input->drag_x;
3405 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003406
3407 if (window->drop_handler)
3408 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003409 x, y, window->user_data);
3410
3411 if (input->touch_grab)
3412 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003413}
3414
3415static void
3416data_device_selection(void *data,
3417 struct wl_data_device *wl_data_device,
3418 struct wl_data_offer *offer)
3419{
3420 struct input *input = data;
3421 char **p;
3422
3423 if (input->selection_offer)
3424 data_offer_destroy(input->selection_offer);
3425
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003426 if (offer) {
3427 input->selection_offer = wl_data_offer_get_user_data(offer);
3428 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3429 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003430 } else {
3431 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003432 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003433}
3434
3435static const struct wl_data_device_listener data_device_listener = {
3436 data_device_data_offer,
3437 data_device_enter,
3438 data_device_leave,
3439 data_device_motion,
3440 data_device_drop,
3441 data_device_selection
3442};
3443
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003444static void
3445input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003446{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003447 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003448 struct wl_cursor *cursor;
3449 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003450
Daniel Stone80972742012-11-07 17:51:39 +11003451 if (!input->pointer)
3452 return;
3453
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003454 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003455 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003456 return;
3457
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003458 if (index >= (int) cursor->image_count) {
3459 fprintf(stderr, "cursor index out of range\n");
3460 return;
3461 }
3462
3463 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003464 buffer = wl_cursor_image_get_buffer(image);
3465 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003466 return;
3467
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003468 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3469 wl_surface_damage(input->pointer_surface, 0, 0,
3470 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003471 wl_surface_commit(input->pointer_surface);
Ander Conselvan de Oliveira23900f72014-01-31 16:07:51 +02003472 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3473 input->pointer_surface,
3474 image->hotspot_x, image->hotspot_y);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003475}
3476
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003477static const struct wl_callback_listener pointer_surface_listener;
3478
3479static void
3480pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3481 uint32_t time)
3482{
3483 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003484 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003485 int i;
3486
3487 if (callback) {
3488 assert(callback == input->cursor_frame_cb);
3489 wl_callback_destroy(callback);
3490 input->cursor_frame_cb = NULL;
3491 }
3492
Daniel Stone80972742012-11-07 17:51:39 +11003493 if (!input->pointer)
3494 return;
3495
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003496 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003497 wl_pointer_set_cursor(input->pointer,
3498 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003499 NULL, 0, 0);
3500 return;
3501 }
3502
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003503 if (input->current_cursor == CURSOR_UNSET)
3504 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003505 cursor = input->display->cursors[input->current_cursor];
3506 if (!cursor)
3507 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003508
3509 /* FIXME We don't have the current time on the first call so we set
3510 * the animation start to the time of the first frame callback. */
3511 if (time == 0)
3512 input->cursor_anim_start = 0;
3513 else if (input->cursor_anim_start == 0)
3514 input->cursor_anim_start = time;
3515
3516 if (time == 0 || input->cursor_anim_start == 0)
3517 i = 0;
3518 else
3519 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3520
Pekka Paalanenbc106382012-10-10 12:49:31 +03003521 if (cursor->image_count > 1) {
3522 input->cursor_frame_cb =
3523 wl_surface_frame(input->pointer_surface);
3524 wl_callback_add_listener(input->cursor_frame_cb,
3525 &pointer_surface_listener, input);
3526 }
3527
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003528 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003529}
3530
3531static const struct wl_callback_listener pointer_surface_listener = {
3532 pointer_surface_frame_callback
3533};
3534
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003535void
3536input_set_pointer_image(struct input *input, int pointer)
3537{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003538 int force = 0;
3539
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003540 if (!input->pointer)
3541 return;
3542
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003543 if (input->pointer_enter_serial > input->cursor_serial)
3544 force = 1;
3545
3546 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003547 return;
3548
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003549 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003550 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003551 if (!input->cursor_frame_cb)
3552 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003553 else if (force) {
3554 /* The current frame callback may be stuck if, for instance,
3555 * the set cursor request was processed by the server after
3556 * this client lost the focus. In this case the cursor surface
3557 * might not be mapped and the frame callback wouldn't ever
3558 * complete. Send a set_cursor and attach to try to map the
3559 * cursor surface again so that the callback will finish */
3560 input_set_pointer_image_index(input, 0);
3561 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003562}
3563
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003564struct wl_data_device *
3565input_get_data_device(struct input *input)
3566{
3567 return input->data_device;
3568}
3569
3570void
3571input_set_selection(struct input *input,
3572 struct wl_data_source *source, uint32_t time)
3573{
Jason Ekstranda669bd52014-04-02 19:53:51 -05003574 if (input->data_device)
3575 wl_data_device_set_selection(input->data_device, source, time);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003576}
3577
3578void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003579input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003580{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003581 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003582 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003583}
3584
3585static void
3586offer_io_func(struct task *task, uint32_t events)
3587{
3588 struct data_offer *offer =
3589 container_of(task, struct data_offer, io_task);
3590 unsigned int len;
3591 char buffer[4096];
3592
3593 len = read(offer->fd, buffer, sizeof buffer);
3594 offer->func(buffer, len,
3595 offer->x, offer->y, offer->user_data);
3596
3597 if (len == 0) {
3598 close(offer->fd);
3599 data_offer_destroy(offer);
3600 }
3601}
3602
3603static void
3604data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3605 data_func_t func, void *user_data)
3606{
3607 int p[2];
3608
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003609 if (pipe2(p, O_CLOEXEC) == -1)
3610 return;
3611
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003612 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3613 close(p[1]);
3614
3615 offer->io_task.run = offer_io_func;
3616 offer->fd = p[0];
3617 offer->func = func;
3618 offer->refcount++;
3619 offer->user_data = user_data;
3620
3621 display_watch_fd(offer->input->display,
3622 offer->fd, EPOLLIN, &offer->io_task);
3623}
3624
3625void
3626input_receive_drag_data(struct input *input, const char *mime_type,
3627 data_func_t func, void *data)
3628{
3629 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003630 input->drag_offer->x = input->drag_x;
3631 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003632}
3633
3634int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003635input_receive_drag_data_to_fd(struct input *input,
3636 const char *mime_type, int fd)
3637{
3638 if (input->drag_offer)
3639 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3640
3641 return 0;
3642}
3643
3644int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003645input_receive_selection_data(struct input *input, const char *mime_type,
3646 data_func_t func, void *data)
3647{
3648 char **p;
3649
3650 if (input->selection_offer == NULL)
3651 return -1;
3652
3653 for (p = input->selection_offer->types.data; *p; p++)
3654 if (strcmp(mime_type, *p) == 0)
3655 break;
3656
3657 if (*p == NULL)
3658 return -1;
3659
3660 data_offer_receive_data(input->selection_offer,
3661 mime_type, func, data);
3662 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003663}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003664
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003665int
3666input_receive_selection_data_to_fd(struct input *input,
3667 const char *mime_type, int fd)
3668{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003669 if (input->selection_offer)
3670 wl_data_offer_receive(input->selection_offer->offer,
3671 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003672
3673 return 0;
3674}
3675
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003676void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003677window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003678{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003679 if (!window->xdg_surface)
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003680 return;
3681
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003682 xdg_surface_move(window->xdg_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003683}
3684
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003685static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003686surface_set_synchronized(struct surface *surface)
3687{
3688 if (!surface->subsurface)
3689 return;
3690
3691 if (surface->synchronized)
3692 return;
3693
3694 wl_subsurface_set_sync(surface->subsurface);
3695 surface->synchronized = 1;
3696}
3697
3698static void
3699surface_set_synchronized_default(struct surface *surface)
3700{
3701 if (!surface->subsurface)
3702 return;
3703
3704 if (surface->synchronized == surface->synchronized_default)
3705 return;
3706
3707 if (surface->synchronized_default)
3708 wl_subsurface_set_sync(surface->subsurface);
3709 else
3710 wl_subsurface_set_desync(surface->subsurface);
3711
3712 surface->synchronized = surface->synchronized_default;
3713}
3714
3715static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003716surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003717{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003718 struct widget *widget = surface->widget;
3719 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003720
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003721 if (surface->input_region) {
3722 wl_region_destroy(surface->input_region);
3723 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003724 }
3725
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003726 if (surface->opaque_region)
3727 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003728
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003729 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003730
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003731 if (widget->resize_handler)
3732 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003733 widget->allocation.width,
3734 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003735 widget->user_data);
3736
Pekka Paalanen35e82632013-04-25 13:57:48 +03003737 if (surface->subsurface &&
3738 (surface->allocation.x != widget->allocation.x ||
3739 surface->allocation.y != widget->allocation.y)) {
3740 wl_subsurface_set_position(surface->subsurface,
3741 widget->allocation.x,
3742 widget->allocation.y);
3743 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003744 if (surface->allocation.width != widget->allocation.width ||
3745 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003746 window_schedule_redraw(widget->window);
3747 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003748 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003749
3750 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003751 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003752 widget->allocation.width,
3753 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003754}
3755
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003756static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003757hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3758{
3759 /*
3760 * This hack should be removed, when EGL respects
3761 * eglSwapInterval(0).
3762 *
3763 * If this window has sub-surfaces, especially a free-running
3764 * EGL-widget, we need to post the parent surface once with
3765 * all the old state to guarantee, that the EGL-widget will
3766 * receive its frame callback soon. Otherwise, a forced call
3767 * to eglSwapBuffers may end up blocking, waiting for a frame
3768 * event that will never come, because we will commit the parent
3769 * surface with all new state only after eglSwapBuffers returns.
3770 *
3771 * This assumes, that:
3772 * 1. When the EGL widget's resize hook is called, it pauses.
3773 * 2. When the EGL widget's redraw hook is called, it forces a
3774 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3775 * In a single threaded application condition 1 is a no-op.
3776 *
3777 * XXX: This should actually be after the surface_resize() calls,
3778 * but cannot, because then it would commit the incomplete state
3779 * accumulated from the widget resize hooks.
3780 */
3781 if (window->subsurface_list.next != &window->main_surface->link ||
3782 window->subsurface_list.prev != &window->main_surface->link)
3783 wl_surface_commit(window->main_surface->surface);
3784}
3785
3786static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003787window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003788{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003789 struct surface *surface;
3790
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003791 widget_set_allocation(window->main_surface->widget,
3792 window->pending_allocation.x,
3793 window->pending_allocation.y,
3794 window->pending_allocation.width,
3795 window->pending_allocation.height);
3796
3797 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003798
3799 /* The main surface is in the list, too. Main surface's
3800 * resize_handler is responsible for calling widget_set_allocation()
3801 * on all sub-surface root widgets, so they will be resized
3802 * properly.
3803 */
3804 wl_list_for_each(surface, &window->subsurface_list, link) {
3805 if (surface == window->main_surface)
3806 continue;
3807
3808 surface_set_synchronized(surface);
3809 surface_resize(surface);
3810 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003811
3812 if (!window->fullscreen && !window->maximized)
3813 window->saved_allocation = window->pending_allocation;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003814}
3815
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003816static void
3817idle_resize(struct window *window)
3818{
3819 window->resize_needed = 0;
3820 window->redraw_needed = 1;
3821
3822 DBG("from %dx%d to %dx%d\n",
3823 window->main_surface->server_allocation.width,
3824 window->main_surface->server_allocation.height,
3825 window->pending_allocation.width,
3826 window->pending_allocation.height);
3827
3828 hack_prevent_EGL_sub_surface_deadlock(window);
3829
3830 window_do_resize(window);
3831}
3832
3833static void
3834undo_resize(struct window *window)
3835{
3836 window->pending_allocation.width =
3837 window->main_surface->server_allocation.width;
3838 window->pending_allocation.height =
3839 window->main_surface->server_allocation.height;
3840
3841 DBG("back to %dx%d\n",
3842 window->main_surface->server_allocation.width,
3843 window->main_surface->server_allocation.height);
3844
3845 window_do_resize(window);
3846
3847 if (window->pending_allocation.width == 0 &&
3848 window->pending_allocation.height == 0) {
3849 fprintf(stderr, "Error: Could not draw a surface, "
3850 "most likely due to insufficient disk space in "
3851 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3852 exit(EXIT_FAILURE);
3853 }
3854}
3855
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003856void
3857window_schedule_resize(struct window *window, int width, int height)
3858{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003859 /* We should probably get these numbers from the theme. */
3860 const int min_width = 200, min_height = 200;
3861
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003862 window->pending_allocation.x = 0;
3863 window->pending_allocation.y = 0;
3864 window->pending_allocation.width = width;
3865 window->pending_allocation.height = height;
3866
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003867 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003868 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003869 window->min_allocation.width = min_width;
3870 else
3871 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003872 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003873 window->min_allocation.height = min_height;
3874 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003875 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003876 }
3877
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003878 if (window->pending_allocation.width < window->min_allocation.width)
3879 window->pending_allocation.width = window->min_allocation.width;
3880 if (window->pending_allocation.height < window->min_allocation.height)
3881 window->pending_allocation.height = window->min_allocation.height;
3882
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003883 window->resize_needed = 1;
3884 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003885}
3886
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003887void
3888widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3889{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003890 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003891}
3892
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003893static int
3894window_get_shadow_margin(struct window *window)
3895{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04003896 if (window->frame && !window->fullscreen)
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003897 return frame_get_shadow_margin(window->frame->frame);
3898 else
3899 return 0;
3900}
3901
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003902static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003903handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003904 int32_t width, int32_t height,
3905 struct wl_array *states, uint32_t serial)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003906{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003907 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003908 uint32_t *p;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003909
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003910 window->maximized = 0;
3911 window->fullscreen = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003912 window->resizing = 0;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003913 window->focused = 0;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003914
3915 wl_array_for_each(p, states) {
3916 uint32_t state = *p;
3917 switch (state) {
3918 case XDG_SURFACE_STATE_MAXIMIZED:
3919 window->maximized = 1;
3920 break;
3921 case XDG_SURFACE_STATE_FULLSCREEN:
3922 window->fullscreen = 1;
3923 break;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003924 case XDG_SURFACE_STATE_RESIZING:
3925 window->resizing = 1;
3926 break;
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003927 case XDG_SURFACE_STATE_ACTIVATED:
3928 window->focused = 1;
3929 break;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003930 default:
3931 /* Unknown state */
3932 break;
3933 }
3934 }
3935
Jasper St. Pierreccf908b2014-05-06 08:20:22 -04003936 if (window->frame) {
3937 if (window->maximized) {
3938 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3939 } else {
3940 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3941 }
3942
3943 if (window->focused) {
3944 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3945 } else {
3946 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3947 }
3948 }
3949
Jasper St. Pierref184c382014-05-06 08:33:27 -04003950 if (width > 0 && height > 0) {
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04003951 /* The width / height params are for window geometry,
3952 * but window_schedule_resize takes allocation. Add
3953 * on the shadow margin to get the difference. */
3954 int margin = window_get_shadow_margin(window);
3955
3956 window_schedule_resize(window,
3957 width + margin * 2,
3958 height + margin * 2);
Jasper St. Pierref184c382014-05-06 08:33:27 -04003959 } else {
3960 window_schedule_resize(window,
3961 window->saved_allocation.width,
3962 window->saved_allocation.height);
3963 }
3964
Kristian Høgsbergbe803ad2014-05-12 23:30:28 -07003965 xdg_surface_ack_configure(window->xdg_surface, serial);
Jasper St. Pierrede680992014-04-10 17:23:49 -07003966
3967 if (window->state_changed_handler)
3968 window->state_changed_handler(window, window->user_data);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003969}
3970
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003971static void
3972handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3973{
3974 struct window *window = data;
3975 window_close(window);
3976}
3977
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003978static const struct xdg_surface_listener xdg_surface_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003979 handle_surface_configure,
Jasper St. Pierre2097fe12014-02-01 18:17:34 -05003980 handle_surface_delete,
Jasper St. Pierre0790e392013-12-09 14:58:00 -05003981};
3982
3983static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003984window_sync_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05003985{
3986 struct wl_surface *parent_surface;
3987
3988 if (!window->xdg_surface)
3989 return;
3990
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003991 if (window->parent)
3992 parent_surface = window->parent->main_surface->surface;
Jasper St. Pierre53686042013-12-09 15:26:25 -05003993 else
3994 parent_surface = NULL;
3995
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003996 xdg_surface_set_parent(window->xdg_surface, parent_surface);
Jasper St. Pierre53686042013-12-09 15:26:25 -05003997}
3998
3999static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004000window_get_geometry(struct window *window, struct rectangle *geometry)
Jasper St. Pierre74073452014-02-01 18:36:41 -05004001{
Jasper St. Pierrea177df02014-08-04 13:43:25 -04004002 if (window->frame && !window->fullscreen)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004003 frame_input_rect(window->frame->frame,
4004 &geometry->x,
4005 &geometry->y,
4006 &geometry->width,
4007 &geometry->height);
4008 else
4009 window_get_allocation(window, geometry);
4010}
4011
4012static void
4013window_sync_geometry(struct window *window)
4014{
4015 struct rectangle geometry;
Jasper St. Pierre74073452014-02-01 18:36:41 -05004016
4017 if (!window->xdg_surface)
4018 return;
4019
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004020 window_get_geometry(window, &geometry);
Jasper St. Pierre74073452014-02-01 18:36:41 -05004021
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004022 xdg_surface_set_window_geometry(window->xdg_surface,
4023 geometry.x,
4024 geometry.y,
4025 geometry.width,
4026 geometry.height);
Jasper St. Pierre74073452014-02-01 18:36:41 -05004027}
4028
4029static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004030window_flush(struct window *window)
4031{
4032 struct surface *surface;
4033
4034 if (!window->custom) {
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004035 if (window->xdg_surface) {
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004036 window_sync_parent(window);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04004037 window_sync_geometry(window);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004038 }
4039 }
4040
4041 wl_list_for_each(surface, &window->subsurface_list, link) {
4042 if (surface == window->main_surface)
4043 continue;
4044
4045 surface_flush(surface);
4046 }
4047
4048 surface_flush(window->main_surface);
4049}
4050
4051static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004052menu_destroy(struct menu *menu)
4053{
4054 widget_destroy(menu->widget);
4055 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004056 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004057 free(menu);
4058}
4059
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004060void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004061window_get_allocation(struct window *window,
4062 struct rectangle *allocation)
4063{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004064 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01004065}
4066
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004067static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05004068widget_redraw(struct widget *widget)
4069{
4070 struct widget *child;
4071
4072 if (widget->redraw_handler)
4073 widget->redraw_handler(widget, widget->user_data);
4074 wl_list_for_each(child, &widget->child_list, link)
4075 widget_redraw(child);
4076}
4077
4078static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004079frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4080{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004081 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004082
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004083 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03004084 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004085 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004086 surface->frame_cb = NULL;
4087
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03004088 surface->last_time = time;
4089
Pekka Paalanen71233882013-04-25 13:57:53 +03004090 if (surface->redraw_needed || surface->window->redraw_needed) {
4091 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004092 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03004093 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004094}
4095
4096static const struct wl_callback_listener listener = {
4097 frame_callback
4098};
4099
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004100static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004101surface_redraw(struct surface *surface)
4102{
Pekka Paalanen71233882013-04-25 13:57:53 +03004103 DBG_OBJ(surface->surface, "begin\n");
4104
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004105 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004106 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004107
4108 /* Whole-window redraw forces a redraw even if the previous has
4109 * not yet hit the screen.
4110 */
4111 if (surface->frame_cb) {
4112 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004113 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004114
Pekka Paalanen71233882013-04-25 13:57:53 +03004115 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004116 wl_callback_destroy(surface->frame_cb);
4117 }
4118
Neil Roberts97b747c2013-12-19 16:17:12 +00004119 if (surface->widget->use_cairo &&
4120 !widget_get_cairo_surface(surface->widget)) {
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004121 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4122 return -1;
4123 }
4124
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004125 surface->frame_cb = wl_surface_frame(surface->surface);
4126 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03004127 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004128
4129 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03004130 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004131 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03004132 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004133 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004134}
4135
4136static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004137idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004138{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004139 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004140 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004141 int failed = 0;
4142 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004143
Pekka Paalanen71233882013-04-25 13:57:53 +03004144 DBG(" --------- \n");
4145
Pekka Paalaneneebff542013-04-25 13:57:52 +03004146 wl_list_init(&window->redraw_task.link);
4147 window->redraw_task_scheduled = 0;
4148
4149 if (window->resize_needed) {
4150 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03004151 if (window->main_surface->frame_cb) {
4152 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03004153 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03004154 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03004155
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004156 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004157 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03004158 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04004159
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004160 if (surface_redraw(window->main_surface) < 0) {
4161 /*
4162 * Only main_surface failure will cause us to undo the resize.
4163 * If sub-surfaces fail, they will just be broken with old
4164 * content.
4165 */
4166 failed = 1;
4167 } else {
4168 wl_list_for_each(surface, &window->subsurface_list, link) {
4169 if (surface == window->main_surface)
4170 continue;
4171
4172 surface_redraw(surface);
4173 }
4174 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004175
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004176 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004177 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004178
4179 wl_list_for_each(surface, &window->subsurface_list, link)
4180 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004181
4182 if (resized && failed) {
4183 /* Restore widget tree to correspond to what is on screen. */
4184 undo_resize(window);
4185 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004186}
4187
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004188static void
4189window_schedule_redraw_task(struct window *window)
4190{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004191 if (!window->redraw_task_scheduled) {
4192 window->redraw_task.run = idle_redraw;
4193 display_defer(window->display, &window->redraw_task);
4194 window->redraw_task_scheduled = 1;
4195 }
4196}
4197
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004198void
4199window_schedule_redraw(struct window *window)
4200{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004201 struct surface *surface;
4202
Pekka Paalanen71233882013-04-25 13:57:53 +03004203 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4204
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004205 wl_list_for_each(surface, &window->subsurface_list, link)
4206 surface->redraw_needed = 1;
4207
4208 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004209}
4210
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004211int
4212window_is_fullscreen(struct window *window)
4213{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004214 return window->fullscreen;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004215}
4216
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004217void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004218window_set_fullscreen(struct window *window, int fullscreen)
4219{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004220 if (!window->xdg_surface)
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004221 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004222
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004223 if (window->fullscreen == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004224 return;
4225
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004226 if (fullscreen)
4227 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4228 else
4229 xdg_surface_unset_fullscreen(window->xdg_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004230}
4231
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004232int
4233window_is_maximized(struct window *window)
4234{
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004235 return window->maximized;
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004236}
4237
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004238void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004239window_set_maximized(struct window *window, int maximized)
4240{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004241 if (!window->xdg_surface)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004242 return;
4243
Jasper St. Pierrec0f17ab2013-11-11 19:16:11 -05004244 if (window->maximized == maximized)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004245 return;
4246
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004247 if (maximized)
4248 xdg_surface_set_maximized(window->xdg_surface);
4249 else
4250 xdg_surface_unset_maximized(window->xdg_surface);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004251}
4252
Jasper St. Pierrede680992014-04-10 17:23:49 -07004253int
4254window_is_resizing(struct window *window)
4255{
4256 return window->resizing;
4257}
4258
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004259void
Jasper St. Pierre5a183322014-02-18 19:18:42 -05004260window_set_minimized(struct window *window)
4261{
4262 if (!window->xdg_surface)
4263 return;
4264
4265 xdg_surface_set_minimized(window->xdg_surface);
4266}
4267
4268void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004269window_set_user_data(struct window *window, void *data)
4270{
4271 window->user_data = data;
4272}
4273
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004274void *
4275window_get_user_data(struct window *window)
4276{
4277 return window->user_data;
4278}
4279
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004280void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004281window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004282 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004283{
4284 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004285}
4286
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004287void
4288window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004289 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004290{
4291 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004292}
4293
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004294void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004295window_set_data_handler(struct window *window, window_data_handler_t handler)
4296{
4297 window->data_handler = handler;
4298}
4299
4300void
4301window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4302{
4303 window->drop_handler = handler;
4304}
4305
4306void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004307window_set_close_handler(struct window *window,
4308 window_close_handler_t handler)
4309{
4310 window->close_handler = handler;
4311}
4312
4313void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004314window_set_fullscreen_handler(struct window *window,
4315 window_fullscreen_handler_t handler)
4316{
4317 window->fullscreen_handler = handler;
4318}
4319
4320void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004321window_set_output_handler(struct window *window,
4322 window_output_handler_t handler)
4323{
4324 window->output_handler = handler;
4325}
4326
4327void
Jasper St. Pierrede680992014-04-10 17:23:49 -07004328window_set_state_changed_handler(struct window *window,
4329 window_state_changed_handler_t handler)
4330{
4331 window->state_changed_handler = handler;
4332}
4333
4334void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004335window_set_title(struct window *window, const char *title)
4336{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004337 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004338 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004339 if (window->frame) {
4340 frame_set_title(window->frame->frame, title);
4341 widget_schedule_redraw(window->frame->widget);
4342 }
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004343 if (window->xdg_surface)
4344 xdg_surface_set_title(window->xdg_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004345}
4346
4347const char *
4348window_get_title(struct window *window)
4349{
4350 return window->title;
4351}
4352
4353void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004354window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4355{
4356 struct text_cursor_position *text_cursor_position =
4357 window->display->text_cursor_position;
4358
Scott Moreau9295ce02012-06-01 12:46:10 -06004359 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004360 return;
4361
4362 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004363 window->main_surface->surface,
4364 wl_fixed_from_int(x),
4365 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004366}
4367
4368void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004369window_damage(struct window *window, int32_t x, int32_t y,
4370 int32_t width, int32_t height)
4371{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004372 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004373}
4374
Casey Dahlin9074db52012-04-19 22:50:09 -04004375static void
4376surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004377 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004378{
Rob Bradford7507b572012-05-15 17:55:34 +01004379 struct window *window = data;
4380 struct output *output;
4381 struct output *output_found = NULL;
4382 struct window_output *window_output;
4383
4384 wl_list_for_each(output, &window->display->output_list, link) {
4385 if (output->output == wl_output) {
4386 output_found = output;
4387 break;
4388 }
4389 }
4390
4391 if (!output_found)
4392 return;
4393
Brian Lovinbc919262013-08-07 15:34:59 -07004394 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004395 window_output->output = output_found;
4396
4397 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004398
4399 if (window->output_handler)
4400 window->output_handler(window, output_found, 1,
4401 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004402}
4403
4404static void
4405surface_leave(void *data,
4406 struct wl_surface *wl_surface, struct wl_output *output)
4407{
Rob Bradford7507b572012-05-15 17:55:34 +01004408 struct window *window = data;
4409 struct window_output *window_output;
4410 struct window_output *window_output_found = NULL;
4411
4412 wl_list_for_each(window_output, &window->window_output_list, link) {
4413 if (window_output->output->output == output) {
4414 window_output_found = window_output;
4415 break;
4416 }
4417 }
4418
4419 if (window_output_found) {
4420 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004421
4422 if (window->output_handler)
4423 window->output_handler(window, window_output->output,
4424 0, window->user_data);
4425
Rob Bradford7507b572012-05-15 17:55:34 +01004426 free(window_output_found);
4427 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004428}
4429
4430static const struct wl_surface_listener surface_listener = {
4431 surface_enter,
4432 surface_leave
4433};
4434
Pekka Paalanen4e373742013-02-13 16:17:13 +02004435static struct surface *
4436surface_create(struct window *window)
4437{
4438 struct display *display = window->display;
4439 struct surface *surface;
4440
Brian Lovinbc919262013-08-07 15:34:59 -07004441 surface = xmalloc(sizeof *surface);
4442 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004443 if (!surface)
4444 return NULL;
4445
4446 surface->window = window;
4447 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004448 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004449 wl_surface_add_listener(surface->surface, &surface_listener, window);
4450
Pekka Paalanen35e82632013-04-25 13:57:48 +03004451 wl_list_insert(&window->subsurface_list, &surface->link);
4452
Pekka Paalanen4e373742013-02-13 16:17:13 +02004453 return surface;
4454}
4455
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004456static enum window_buffer_type
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004457get_preferred_buffer_type(struct display *display)
4458{
4459#ifdef HAVE_CAIRO_EGL
Jasper St. Pierrebf39e5e2014-04-28 11:19:32 -04004460 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004461 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4462#endif
4463
4464 return WINDOW_BUFFER_TYPE_SHM;
4465}
4466
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004467static struct window *
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004468window_create_internal(struct display *display, int custom)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004469{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004470 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004471 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004472
Peter Huttererf3d62272013-08-08 11:57:05 +10004473 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004474 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004475 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004476
4477 surface = surface_create(window);
4478 window->main_surface = surface;
4479
Jason Ekstrandce97a6b2014-04-02 19:53:49 -05004480 assert(custom || display->xdg_shell);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004481
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004482 window->custom = custom;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004483 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004484
Jasper St. Pierrebd600772014-04-28 11:19:31 -04004485 surface->buffer_type = get_preferred_buffer_type(display);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004486
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004487 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004488 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004489 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004490
Rob Bradford7507b572012-05-15 17:55:34 +01004491 wl_list_init (&window->window_output_list);
4492
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004493 return window;
4494}
4495
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004496struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004497window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004498{
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07004499 struct window *window;
4500
4501 window = window_create_internal(display, 0);
4502
4503 window->xdg_surface =
4504 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4505 window->main_surface->surface);
4506 fail_on_null(window->xdg_surface);
4507
4508 xdg_surface_set_user_data(window->xdg_surface, window);
4509 xdg_surface_add_listener(window->xdg_surface,
4510 &xdg_surface_listener, window);
4511
4512 return window;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004513}
4514
4515struct window *
4516window_create_custom(struct display *display)
4517{
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004518 return window_create_internal(display, 1);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004519}
4520
Jasper St. Pierre53686042013-12-09 15:26:25 -05004521void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004522window_set_parent(struct window *window,
4523 struct window *parent_window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004524{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004525 window->parent = parent_window;
4526 window_sync_parent(window);
Jasper St. Pierre53686042013-12-09 15:26:25 -05004527}
4528
4529struct window *
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004530window_get_parent(struct window *window)
Jasper St. Pierre53686042013-12-09 15:26:25 -05004531{
Jasper St. Pierrec815d622014-04-10 18:37:54 -07004532 return window->parent;
Jasper St. Pierre53686042013-12-09 15:26:25 -05004533}
4534
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004535static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004536menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004537{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004538 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004539 int next;
4540
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004541 frame_interior(menu->frame, &x, &y, &width, &height);
4542 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004543 if (menu->current != next) {
4544 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004545 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004546 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004547}
4548
4549static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004550menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004551 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004552 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004553{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004554 struct menu *menu = data;
4555
4556 if (widget == menu->widget)
4557 menu_set_item(data, y);
4558
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004559 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004560}
4561
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004562static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004563menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004564 struct input *input, 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øgsberg391649b2012-01-09 09:22:30 -05004572}
4573
4574static void
4575menu_leave_handler(struct widget *widget, struct input *input, void *data)
4576{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004577 struct menu *menu = data;
4578
4579 if (widget == menu->widget)
4580 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004581}
4582
4583static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004584menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004585 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004586 uint32_t button, enum wl_pointer_button_state state,
4587 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004588
4589{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004590 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004591
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004592 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4593 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004594 /* Either relase after press-drag-release or
4595 * click-motion-click. */
Jasper St. Pierredda93132014-03-13 12:06:00 -04004596 menu->func(menu->user_data, input, menu->current);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004597 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004598 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004599 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004600 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004601 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004602}
4603
4604static void
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004605menu_touch_up_handler(struct widget *widget,
4606 struct input *input,
4607 uint32_t serial,
4608 uint32_t time,
4609 int32_t id,
4610 void *data)
4611{
4612 struct menu *menu = data;
4613
4614 input_ungrab(input);
4615 menu_destroy(menu);
4616}
4617
4618static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004619menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004620{
4621 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004622 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004623 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004624
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004625 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004626
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004627 frame_repaint(menu->frame, cr);
4628 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004629
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004630 theme_set_background_source(menu->window->display->theme,
4631 cr, THEME_FRAME_ACTIVE);
4632 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004633 cairo_fill(cr);
4634
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004635 cairo_select_font_face(cr, "sans",
4636 CAIRO_FONT_SLANT_NORMAL,
4637 CAIRO_FONT_WEIGHT_NORMAL);
4638 cairo_set_font_size(cr, 12);
4639
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004640 for (i = 0; i < menu->count; i++) {
4641 if (i == menu->current) {
4642 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004643 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004644 cairo_fill(cr);
4645 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004646 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004647 cairo_show_text(cr, menu->entries[i]);
4648 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004649 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4650 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004651 cairo_show_text(cr, menu->entries[i]);
4652 }
4653 }
4654
4655 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004656}
4657
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004658static void
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004659handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4660{
4661 struct window *window = data;
4662 struct menu *menu = window->main_surface->widget->user_data;
4663
4664 input_ungrab(menu->input);
4665 menu_destroy(menu);
4666}
4667
4668static const struct xdg_popup_listener xdg_popup_listener = {
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004669 handle_popup_popup_done,
4670};
4671
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004672static struct menu *
4673create_menu(struct display *display,
4674 struct input *input, uint32_t time,
4675 menu_func_t func, const char **entries, int count,
4676 void *user_data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004677{
4678 struct window *window;
4679 struct menu *menu;
4680
4681 menu = malloc(sizeof *menu);
4682 if (!menu)
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004683 return NULL;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004684
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004685 window = window_create_internal(display, 0);
Martin Olsson444799a2012-07-08 03:03:40 +02004686 if (!window) {
4687 free(menu);
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004688 return NULL;
Martin Olsson444799a2012-07-08 03:03:40 +02004689 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004690
4691 menu->window = window;
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004692 menu->user_data = user_data;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004693 menu->widget = window_add_widget(menu->window, menu);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004694 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004695 FRAME_BUTTON_NONE, NULL);
U. Artie Eoffbae79ca2014-01-15 13:38:51 -08004696 fail_on_null(menu->frame);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004697 menu->entries = entries;
4698 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004699 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004700 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004701 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004702 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004703 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004704
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004705 input_ungrab(input);
4706
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004707 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004708 widget_set_enter_handler(menu->widget, menu_enter_handler);
4709 widget_set_leave_handler(menu->widget, menu_leave_handler);
4710 widget_set_motion_handler(menu->widget, menu_motion_handler);
4711 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg9c609332014-04-29 14:47:19 -07004712 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004713
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004714 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004715 frame_resize_inside(menu->frame, 200, count * 20);
4716 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4717 window_schedule_resize(window, frame_width(menu->frame),
4718 frame_height(menu->frame));
4719
Jasper St. Pierrede8bd502014-03-13 11:57:31 -04004720 return menu;
4721}
4722
4723struct window *
4724window_create_menu(struct display *display,
4725 struct input *input, uint32_t time,
4726 menu_func_t func, const char **entries, int count,
4727 void *user_data)
4728{
4729 struct menu *menu;
4730 menu = create_menu(display, input, time, func, entries, count, user_data);
4731
4732 if (menu == NULL)
4733 return NULL;
4734
4735 return menu->window;
4736}
4737
4738void
4739window_show_menu(struct display *display,
4740 struct input *input, uint32_t time, struct window *parent,
4741 int32_t x, int32_t y,
4742 menu_func_t func, const char **entries, int count)
4743{
4744 struct menu *menu;
4745 struct window *window;
4746 int32_t ix, iy;
4747
4748 menu = create_menu(display, input, time, func, entries, count, parent);
4749
4750 if (menu == NULL)
4751 return;
4752
4753 window = menu->window;
4754
4755 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4756 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4757
4758 window->x = x;
4759 window->y = y;
4760
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004761 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05004762
4763 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4764 window->main_surface->surface,
4765 parent->main_surface->surface,
4766 input->seat,
4767 display_get_serial(window->display),
4768 window->x - ix,
4769 window->y - iy,
4770 0);
4771 fail_on_null(window->xdg_popup);
4772
4773 xdg_popup_set_user_data(window->xdg_popup, window);
4774 xdg_popup_add_listener(window->xdg_popup,
4775 &xdg_popup_listener, window);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004776}
4777
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004778void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004779window_set_buffer_type(struct window *window, enum window_buffer_type type)
4780{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004781 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004782}
4783
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004784enum window_buffer_type
4785window_get_buffer_type(struct window *window)
4786{
4787 return window->main_surface->buffer_type;
4788}
4789
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004790void
4791window_set_preferred_format(struct window *window,
4792 enum preferred_format format)
4793{
4794 window->preferred_format = format;
4795}
4796
Pekka Paalanen35e82632013-04-25 13:57:48 +03004797struct widget *
4798window_add_subsurface(struct window *window, void *data,
4799 enum subsurface_mode default_mode)
4800{
4801 struct widget *widget;
4802 struct surface *surface;
4803 struct wl_surface *parent;
4804 struct wl_subcompositor *subcompo = window->display->subcompositor;
4805
Pekka Paalanen35e82632013-04-25 13:57:48 +03004806 surface = surface_create(window);
Manuel Bachmanncd186fb2014-04-04 10:04:18 +02004807 surface->buffer_type = window_get_buffer_type(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004808 widget = widget_create(window, surface, data);
4809 wl_list_init(&widget->link);
4810 surface->widget = widget;
4811
4812 parent = window->main_surface->surface;
4813 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4814 surface->surface,
4815 parent);
4816 surface->synchronized = 1;
4817
4818 switch (default_mode) {
4819 case SUBSURFACE_SYNCHRONIZED:
4820 surface->synchronized_default = 1;
4821 break;
4822 case SUBSURFACE_DESYNCHRONIZED:
4823 surface->synchronized_default = 0;
4824 break;
4825 default:
4826 assert(!"bad enum subsurface_mode");
4827 }
4828
Jasper St. Pierree22952b2013-11-11 20:07:33 -05004829 window->resize_needed = 1;
4830 window_schedule_redraw(window);
4831
Pekka Paalanen35e82632013-04-25 13:57:48 +03004832 return widget;
4833}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004834
4835static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004836display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004837 struct wl_output *wl_output,
4838 int x, int y,
4839 int physical_width,
4840 int physical_height,
4841 int subpixel,
4842 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004843 const char *model,
4844 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004845{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004846 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004847
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004848 output->allocation.x = x;
4849 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004850 output->transform = transform;
Jason Ekstrand738715d2014-04-02 19:53:50 -05004851
4852 if (output->make)
4853 free(output->make);
4854 output->make = strdup(make);
4855
4856 if (output->model)
4857 free(output->model);
4858 output->model = strdup(model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004859}
4860
4861static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004862display_handle_done(void *data,
4863 struct wl_output *wl_output)
4864{
4865}
4866
4867static void
4868display_handle_scale(void *data,
4869 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004870 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004871{
4872 struct output *output = data;
4873
4874 output->scale = scale;
4875}
4876
4877static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004878display_handle_mode(void *data,
4879 struct wl_output *wl_output,
4880 uint32_t flags,
4881 int width,
4882 int height,
4883 int refresh)
4884{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004885 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004886 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004887
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004888 if (flags & WL_OUTPUT_MODE_CURRENT) {
4889 output->allocation.width = width;
4890 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004891 if (display->output_configure_handler)
4892 (*display->output_configure_handler)(
4893 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004894 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004895}
4896
4897static const struct wl_output_listener output_listener = {
4898 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004899 display_handle_mode,
4900 display_handle_done,
4901 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004902};
4903
4904static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004905display_add_output(struct display *d, uint32_t id)
4906{
4907 struct output *output;
4908
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004909 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004910 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004911 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004912 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004913 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004914 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004915 wl_list_insert(d->output_list.prev, &output->link);
4916
4917 wl_output_add_listener(output->output, &output_listener, output);
4918}
4919
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004920static void
4921output_destroy(struct output *output)
4922{
4923 if (output->destroy_handler)
4924 (*output->destroy_handler)(output, output->user_data);
4925
4926 wl_output_destroy(output->output);
4927 wl_list_remove(&output->link);
4928 free(output);
4929}
4930
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004931static void
4932display_destroy_output(struct display *d, uint32_t id)
4933{
4934 struct output *output;
4935
4936 wl_list_for_each(output, &d->output_list, link) {
4937 if (output->server_output_id == id) {
4938 output_destroy(output);
4939 break;
4940 }
4941 }
4942}
4943
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004944void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004945display_set_global_handler(struct display *display,
4946 display_global_handler_t handler)
4947{
4948 struct global *global;
4949
4950 display->global_handler = handler;
4951 if (!handler)
4952 return;
4953
4954 wl_list_for_each(global, &display->global_list, link)
4955 display->global_handler(display,
4956 global->name, global->interface,
4957 global->version, display->user_data);
4958}
4959
4960void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004961display_set_global_handler_remove(struct display *display,
4962 display_global_handler_t remove_handler)
4963{
4964 display->global_handler_remove = remove_handler;
4965 if (!remove_handler)
4966 return;
4967}
4968
4969void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004970display_set_output_configure_handler(struct display *display,
4971 display_output_handler_t handler)
4972{
4973 struct output *output;
4974
4975 display->output_configure_handler = handler;
4976 if (!handler)
4977 return;
4978
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004979 wl_list_for_each(output, &display->output_list, link) {
4980 if (output->allocation.width == 0 &&
4981 output->allocation.height == 0)
4982 continue;
4983
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004984 (*display->output_configure_handler)(output,
4985 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004986 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004987}
4988
4989void
4990output_set_user_data(struct output *output, void *data)
4991{
4992 output->user_data = data;
4993}
4994
4995void *
4996output_get_user_data(struct output *output)
4997{
4998 return output->user_data;
4999}
5000
5001void
5002output_set_destroy_handler(struct output *output,
5003 display_output_handler_t handler)
5004{
5005 output->destroy_handler = handler;
5006 /* FIXME: implement this, once we have way to remove outputs */
5007}
5008
5009void
Scott Moreau4e072362012-09-29 02:03:11 -06005010output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005011{
Scott Moreau4e072362012-09-29 02:03:11 -06005012 struct rectangle allocation = output->allocation;
5013
5014 switch (output->transform) {
5015 case WL_OUTPUT_TRANSFORM_90:
5016 case WL_OUTPUT_TRANSFORM_270:
5017 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5018 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5019 /* Swap width and height */
5020 allocation.width = output->allocation.height;
5021 allocation.height = output->allocation.width;
5022 break;
5023 }
5024
5025 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005026}
5027
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005028struct wl_output *
5029output_get_wl_output(struct output *output)
5030{
5031 return output->output;
5032}
5033
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02005034enum wl_output_transform
5035output_get_transform(struct output *output)
5036{
5037 return output->transform;
5038}
5039
Alexander Larssonafd319a2013-05-22 14:41:27 +02005040uint32_t
5041output_get_scale(struct output *output)
5042{
5043 return output->scale;
5044}
5045
Jason Ekstrand738715d2014-04-02 19:53:50 -05005046const char *
5047output_get_make(struct output *output)
5048{
5049 return output->make;
5050}
5051
5052const char *
5053output_get_model(struct output *output)
5054{
5055 return output->model;
5056}
5057
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005058static void
Daniel Stone97f68542012-05-30 16:32:01 +01005059fini_xkb(struct input *input)
5060{
5061 xkb_state_unref(input->xkb.state);
Ran Benita2e1968f2014-08-19 23:59:51 +03005062 xkb_keymap_unref(input->xkb.keymap);
Daniel Stone97f68542012-05-30 16:32:01 +01005063}
5064
Jasper St. Pierre47f10432013-11-12 14:37:20 -05005065#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01005066
Daniel Stone97f68542012-05-30 16:32:01 +01005067static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005068display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005069{
5070 struct input *input;
5071
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07005072 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005073 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01005074 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jonny Lamb06959082014-08-12 14:58:27 +02005075 MIN(d->seat_version, 4));
Rusty Lynch1084da52013-08-15 09:10:08 -07005076 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005077 input->pointer_focus = NULL;
5078 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07005079 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005080 wl_list_insert(d->input_list.prev, &input->link);
5081
Daniel Stone37816df2012-05-16 18:45:18 +01005082 wl_seat_add_listener(input->seat, &seat_listener, input);
5083 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005084
Jason Ekstranda669bd52014-04-02 19:53:51 -05005085 if (d->data_device_manager) {
5086 input->data_device =
5087 wl_data_device_manager_get_data_device(d->data_device_manager,
5088 input->seat);
5089 wl_data_device_add_listener(input->data_device,
5090 &data_device_listener,
5091 input);
5092 }
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005093
5094 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005095
Jonny Lamb06959082014-08-12 14:58:27 +02005096 set_repeat_info(input, 40, 400);
5097
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04005098 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5099 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005100 input->repeat_task.run = keyboard_repeat_func;
5101 display_watch_fd(d, input->repeat_timer_fd,
5102 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005103}
5104
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005105static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02005106input_destroy(struct input *input)
5107{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05005108 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005109 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005110
5111 if (input->drag_offer)
5112 data_offer_destroy(input->drag_offer);
5113
5114 if (input->selection_offer)
5115 data_offer_destroy(input->selection_offer);
5116
Jason Ekstranda669bd52014-04-02 19:53:51 -05005117 if (input->data_device)
5118 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01005119
5120 if (input->display->seat_version >= 3) {
5121 if (input->pointer)
5122 wl_pointer_release(input->pointer);
5123 if (input->keyboard)
5124 wl_keyboard_release(input->keyboard);
5125 }
5126
Daniel Stone97f68542012-05-30 16:32:01 +01005127 fini_xkb(input);
5128
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03005129 wl_surface_destroy(input->pointer_surface);
5130
Pekka Paalanene1207c72011-12-16 12:02:09 +02005131 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01005132 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04005133 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005134 free(input);
5135}
5136
5137static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005138init_workspace_manager(struct display *d, uint32_t id)
5139{
5140 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005141 wl_registry_bind(d->registry, id,
5142 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005143 if (d->workspace_manager != NULL)
5144 workspace_manager_add_listener(d->workspace_manager,
5145 &workspace_manager_listener,
5146 d);
5147}
5148
5149static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005150shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5151{
5152 struct display *d = data;
5153
5154 if (format == WL_SHM_FORMAT_RGB565)
5155 d->has_rgb565 = 1;
5156}
5157
5158struct wl_shm_listener shm_listener = {
5159 shm_format
5160};
5161
5162static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005163xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5164{
5165 xdg_shell_pong(shell, serial);
5166}
5167
5168static const struct xdg_shell_listener xdg_shell_listener = {
5169 xdg_shell_ping,
5170};
5171
Pekka Paalanen71182ae2014-08-21 17:47:20 +03005172#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005173#ifdef static_assert
5174static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5175 "Interface version doesn't match implementation version");
5176#endif
5177
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005178static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005179registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5180 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005181{
5182 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005183 struct global *global;
5184
Brian Lovinbc919262013-08-07 15:34:59 -07005185 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005186 global->name = id;
5187 global->interface = strdup(interface);
5188 global->version = version;
5189 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005190
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005191 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005192 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05005193 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005194 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005195 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01005196 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01005197 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04005198 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04005199 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005200 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02005201 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005202 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5203 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005204 wl_registry_bind(registry, id,
5205 &wl_data_device_manager_interface, 1);
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005206 } else if (strcmp(interface, "xdg_shell") == 0) {
5207 d->xdg_shell = wl_registry_bind(registry, id,
5208 &xdg_shell_interface, 1);
Kristian Høgsberg239902b2014-02-11 13:50:08 -08005209 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005210 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06005211 } else if (strcmp(interface, "text_cursor_position") == 0) {
5212 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005213 wl_registry_bind(registry, id,
5214 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02005215 } else if (strcmp(interface, "workspace_manager") == 0) {
5216 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03005217 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5218 d->subcompositor =
5219 wl_registry_bind(registry, id,
5220 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005221 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005222
5223 if (d->global_handler)
5224 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005225}
5226
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005227static void
5228registry_handle_global_remove(void *data, struct wl_registry *registry,
5229 uint32_t name)
5230{
5231 struct display *d = data;
5232 struct global *global;
5233 struct global *tmp;
5234
5235 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5236 if (global->name != name)
5237 continue;
5238
Xiong Zhang83d8ee72013-10-23 13:58:35 +08005239 if (strcmp(global->interface, "wl_output") == 0)
5240 display_destroy_output(d, name);
5241
5242 /* XXX: Should destroy remaining bound globals */
5243
5244 if (d->global_handler_remove)
5245 d->global_handler_remove(d, name, global->interface,
5246 global->version, d->user_data);
5247
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005248 wl_list_remove(&global->link);
5249 free(global->interface);
5250 free(global);
5251 }
5252}
5253
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005254void *
5255display_bind(struct display *display, uint32_t name,
5256 const struct wl_interface *interface, uint32_t version)
5257{
5258 return wl_registry_bind(display->registry, name, interface, version);
5259}
5260
5261static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005262 registry_handle_global,
5263 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005264};
5265
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005266#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005267static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005268init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005269{
5270 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005271 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005272
Rob Clark6396ed32012-03-11 19:48:41 -05005273#ifdef USE_CAIRO_GLESV2
5274# define GL_BIT EGL_OPENGL_ES2_BIT
5275#else
5276# define GL_BIT EGL_OPENGL_BIT
5277#endif
5278
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005279 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005280 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005281 EGL_RED_SIZE, 1,
5282 EGL_GREEN_SIZE, 1,
5283 EGL_BLUE_SIZE, 1,
5284 EGL_ALPHA_SIZE, 1,
5285 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005286 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005287 EGL_NONE
5288 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005289
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005290#ifdef USE_CAIRO_GLESV2
5291 static const EGLint context_attribs[] = {
5292 EGL_CONTEXT_CLIENT_VERSION, 2,
5293 EGL_NONE
5294 };
5295 EGLint api = EGL_OPENGL_ES_API;
5296#else
5297 EGLint *context_attribs = NULL;
5298 EGLint api = EGL_OPENGL_API;
5299#endif
5300
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005301 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005302 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005303 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005304 return -1;
5305 }
5306
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005307 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005308 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005309 return -1;
5310 }
5311
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005312 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5313 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005314 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005315 return -1;
5316 }
5317
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005318 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005319 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005320 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005321 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005322 return -1;
5323 }
5324
Benjamin Franzke0c991632011-09-27 21:57:31 +02005325 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5326 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005327 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005328 return -1;
5329 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005330
5331 return 0;
5332}
5333
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005334static void
5335fini_egl(struct display *display)
5336{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005337 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005338
5339 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5340 EGL_NO_CONTEXT);
5341
5342 eglTerminate(display->dpy);
5343 eglReleaseThread();
5344}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005345#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005346
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005347static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005348init_dummy_surface(struct display *display)
5349{
5350 int len;
5351 void *data;
5352
5353 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5354 data = malloc(len);
5355 display->dummy_surface =
5356 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5357 1, 1, len);
5358 display->dummy_surface_data = data;
5359}
5360
5361static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005362handle_display_data(struct task *task, uint32_t events)
5363{
5364 struct display *display =
5365 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005366 struct epoll_event ep;
5367 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005368
5369 display->display_fd_events = events;
5370
5371 if (events & EPOLLERR || events & EPOLLHUP) {
5372 display_exit(display);
5373 return;
5374 }
5375
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005376 if (events & EPOLLIN) {
5377 ret = wl_display_dispatch(display->display);
5378 if (ret == -1) {
5379 display_exit(display);
5380 return;
5381 }
5382 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005383
5384 if (events & EPOLLOUT) {
5385 ret = wl_display_flush(display->display);
5386 if (ret == 0) {
5387 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5388 ep.data.ptr = &display->display_task;
5389 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5390 display->display_fd, &ep);
5391 } else if (ret == -1 && errno != EAGAIN) {
5392 display_exit(display);
5393 return;
5394 }
5395 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005396}
5397
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005398static void
5399log_handler(const char *format, va_list args)
5400{
5401 vfprintf(stderr, format, args);
5402}
5403
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005404struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005405display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005406{
5407 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005408
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005409 wl_log_set_handler_client(log_handler);
5410
Peter Huttererf3d62272013-08-08 11:57:05 +10005411 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005412 if (d == NULL)
5413 return NULL;
5414
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005415 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005416 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005417 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005418 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005419 return NULL;
5420 }
5421
Rob Bradford5ab9c752013-07-26 16:29:43 +01005422 d->xkb_context = xkb_context_new(0);
5423 if (d->xkb_context == NULL) {
5424 fprintf(stderr, "Failed to create XKB context\n");
5425 free(d);
5426 return NULL;
5427 }
5428
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005429 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005430 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005431 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005432 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5433 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005434
5435 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005436 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005437 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005438 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005439
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005440 d->workspace = 0;
5441 d->workspace_count = 1;
5442
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005443 d->registry = wl_display_get_registry(d->display);
5444 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005445
5446 if (wl_display_dispatch(d->display) < 0) {
5447 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5448 return NULL;
5449 }
5450
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005451#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005452 if (init_egl(d) < 0)
5453 fprintf(stderr, "EGL does not seem to work, "
5454 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005455#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005456
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005457 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005458
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005459 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005460
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005461 wl_list_init(&d->window_list);
5462
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005463 init_dummy_surface(d);
5464
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005465 return d;
5466}
5467
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005468static void
5469display_destroy_outputs(struct display *display)
5470{
5471 struct output *tmp;
5472 struct output *output;
5473
5474 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5475 output_destroy(output);
5476}
5477
Pekka Paalanene1207c72011-12-16 12:02:09 +02005478static void
5479display_destroy_inputs(struct display *display)
5480{
5481 struct input *tmp;
5482 struct input *input;
5483
5484 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5485 input_destroy(input);
5486}
5487
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005488void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005489display_destroy(struct display *display)
5490{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005491 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005492 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5493 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005494
5495 if (!wl_list_empty(&display->deferred_list))
5496 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5497
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005498 cairo_surface_destroy(display->dummy_surface);
5499 free(display->dummy_surface_data);
5500
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005501 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005502 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005503
Daniel Stone97f68542012-05-30 16:32:01 +01005504 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005505
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005506 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005507 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005508
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005509#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005510 if (display->argb_device)
5511 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005512#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005513
Pekka Paalanen35e82632013-04-25 13:57:48 +03005514 if (display->subcompositor)
5515 wl_subcompositor_destroy(display->subcompositor);
5516
Jasper St. Pierre0790e392013-12-09 14:58:00 -05005517 if (display->xdg_shell)
5518 xdg_shell_destroy(display->xdg_shell);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005519
5520 if (display->shm)
5521 wl_shm_destroy(display->shm);
5522
5523 if (display->data_device_manager)
5524 wl_data_device_manager_destroy(display->data_device_manager);
5525
5526 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005527 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005528
5529 close(display->epoll_fd);
5530
U. Artie Eoff44874d92012-10-02 21:12:35 -07005531 if (!(display->display_fd_events & EPOLLERR) &&
5532 !(display->display_fd_events & EPOLLHUP))
5533 wl_display_flush(display->display);
5534
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005535 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005536 free(display);
5537}
5538
5539void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005540display_set_user_data(struct display *display, void *data)
5541{
5542 display->user_data = data;
5543}
5544
5545void *
5546display_get_user_data(struct display *display)
5547{
5548 return display->user_data;
5549}
5550
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005551struct wl_display *
5552display_get_display(struct display *display)
5553{
5554 return display->display;
5555}
5556
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005557int
5558display_has_subcompositor(struct display *display)
5559{
5560 if (display->subcompositor)
5561 return 1;
5562
5563 wl_display_roundtrip(display->display);
5564
5565 return display->subcompositor != NULL;
5566}
5567
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005568cairo_device_t *
5569display_get_cairo_device(struct display *display)
5570{
5571 return display->argb_device;
5572}
5573
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005574struct output *
5575display_get_output(struct display *display)
5576{
5577 return container_of(display->output_list.next, struct output, link);
5578}
5579
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005580struct wl_compositor *
5581display_get_compositor(struct display *display)
5582{
5583 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005584}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005585
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005586uint32_t
5587display_get_serial(struct display *display)
5588{
5589 return display->serial;
5590}
5591
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005592EGLDisplay
5593display_get_egl_display(struct display *d)
5594{
5595 return d->dpy;
5596}
5597
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005598struct wl_data_source *
5599display_create_data_source(struct display *display)
5600{
Jason Ekstranda669bd52014-04-02 19:53:51 -05005601 if (display->data_device_manager)
5602 return wl_data_device_manager_create_data_source(display->data_device_manager);
5603 else
5604 return NULL;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005605}
5606
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005607EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005608display_get_argb_egl_config(struct display *d)
5609{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005610 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005611}
5612
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005613int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005614display_acquire_window_surface(struct display *display,
5615 struct window *window,
5616 EGLContext ctx)
5617{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005618 struct surface *surface = window->main_surface;
5619
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005620 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005621 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005622
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005623 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005624 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005625}
5626
5627void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005628display_release_window_surface(struct display *display,
5629 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005630{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005631 struct surface *surface = window->main_surface;
5632
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005633 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005634 return;
5635
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005636 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005637}
5638
5639void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005640display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005641{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005642 wl_list_insert(&display->deferred_list, &task->link);
5643}
5644
5645void
5646display_watch_fd(struct display *display,
5647 int fd, uint32_t events, struct task *task)
5648{
5649 struct epoll_event ep;
5650
5651 ep.events = events;
5652 ep.data.ptr = task;
5653 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5654}
5655
5656void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005657display_unwatch_fd(struct display *display, int fd)
5658{
5659 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5660}
5661
5662void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005663display_run(struct display *display)
5664{
5665 struct task *task;
5666 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005667 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005668
Pekka Paalanen826d7952011-12-15 10:14:07 +02005669 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005670 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005671 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005672 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005673 struct task, link);
5674 wl_list_remove(&task->link);
5675 task->run(task, 0);
5676 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005677
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005678 wl_display_dispatch_pending(display->display);
5679
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005680 if (!display->running)
5681 break;
5682
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005683 ret = wl_display_flush(display->display);
5684 if (ret < 0 && errno == EAGAIN) {
5685 ep[0].events =
5686 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5687 ep[0].data.ptr = &display->display_task;
5688
5689 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5690 display->display_fd, &ep[0]);
5691 } else if (ret < 0) {
5692 break;
5693 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005694
5695 count = epoll_wait(display->epoll_fd,
5696 ep, ARRAY_LENGTH(ep), -1);
5697 for (i = 0; i < count; i++) {
5698 task = ep[i].data.ptr;
5699 task->run(task, ep[i].events);
5700 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005701 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005702}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005703
5704void
5705display_exit(struct display *display)
5706{
5707 display->running = 0;
5708}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005709
5710void
5711keysym_modifiers_add(struct wl_array *modifiers_map,
5712 const char *name)
5713{
5714 size_t len = strlen(name) + 1;
5715 char *p;
5716
5717 p = wl_array_add(modifiers_map, len);
5718
5719 if (p == NULL)
5720 return;
5721
5722 strncpy(p, name, len);
5723}
5724
5725static xkb_mod_index_t
5726keysym_modifiers_get_index(struct wl_array *modifiers_map,
5727 const char *name)
5728{
5729 xkb_mod_index_t index = 0;
5730 char *p = modifiers_map->data;
5731
5732 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5733 if (strcmp(p, name) == 0)
5734 return index;
5735
5736 index++;
5737 p += strlen(p) + 1;
5738 }
5739
5740 return XKB_MOD_INVALID;
5741}
5742
5743xkb_mod_mask_t
5744keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5745 const char *name)
5746{
5747 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5748
5749 if (index == XKB_MOD_INVALID)
5750 return XKB_MOD_INVALID;
5751
5752 return 1 << index;
5753}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005754
5755void *
5756fail_on_null(void *p)
5757{
5758 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005759 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005760 exit(EXIT_FAILURE);
5761 }
5762
5763 return p;
5764}
5765
5766void *
5767xmalloc(size_t s)
5768{
5769 return fail_on_null(malloc(s));
5770}
5771
Peter Huttererf3d62272013-08-08 11:57:05 +10005772void *
5773xzalloc(size_t s)
5774{
5775 return fail_on_null(zalloc(s));
5776}
5777
Kristian Høgsbergce278412013-07-25 15:20:20 -07005778char *
5779xstrdup(const char *s)
5780{
5781 return fail_on_null(strdup(s));
5782}
Kristian Høgsberg700d6ad2014-01-09 23:45:18 -08005783
5784void *
5785xrealloc(char *p, size_t s)
5786{
5787 return fail_on_null(realloc(p, s));
5788}