blob: d586244e636580eff03828331ec8db471641bc09 [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"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060068#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020069#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030070#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050072#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050073
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070074struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030075
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040076struct global {
77 uint32_t name;
78 char *interface;
79 uint32_t version;
80 struct wl_list link;
81};
82
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050083struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050084 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040085 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050086 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030087 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040088 struct wl_shell *shell;
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;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020095 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040097 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040098
99 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700100 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400101 struct task display_task;
102
103 int epoll_fd;
104 struct wl_list deferred_list;
105
Pekka Paalanen826d7952011-12-15 10:14:07 +0200106 int running;
107
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400108 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400109 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500111 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400112
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400113 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400114
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300115 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300116 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400117
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800120 display_global_handler_t global_handler_remove;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200121
122 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100123
124 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200125
126 uint32_t workspace;
127 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200128
129 /* A hack to get text extents for tooltips */
130 cairo_surface_t *dummy_surface;
131 void *dummy_surface_data;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200132
133 int has_rgb565;
Rob Bradford08031182013-08-13 20:11:03 +0100134 int seat_version;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500135};
136
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400137enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400138 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400139 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400140 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500141 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500143 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400144 TYPE_CUSTOM
145};
Rob Bradford7507b572012-05-15 17:55:34 +0100146
147struct window_output {
148 struct output *output;
149 struct wl_list link;
150};
151
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200152struct toysurface {
153 /*
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200157 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200160 * Returns the Cairo surface to draw to.
161 */
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200163 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200164 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200165
166 /*
167 * Post the surface to the server, returning the server allocation
168 * rectangle. The Cairo surface from prepare() must be destroyed
169 * after calling this.
170 */
171 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200172 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200173 struct rectangle *server_allocation);
174
175 /*
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
178 */
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
180
181 /*
182 * Release the toysurface from the EGL context, returning control
183 * to Cairo.
184 */
185 void (*release)(struct toysurface *base);
186
187 /*
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
190 */
191 void (*destroy)(struct toysurface *base);
192};
193
Pekka Paalanen4e373742013-02-13 16:17:13 +0200194struct surface {
195 struct window *window;
196
197 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300198 struct wl_subsurface *subsurface;
199 int synchronized;
200 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200201 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200202 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300203 int redraw_needed;
204 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300205 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200206
207 struct rectangle allocation;
208 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200210 struct wl_region *input_region;
211 struct wl_region *opaque_region;
212
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200213 enum window_buffer_type buffer_type;
214 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200215 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200216
217 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300218
219 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200220};
221
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500222struct window {
223 struct display *display;
Rob Bradford7507b572012-05-15 17:55:34 +0100224 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500225 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200226 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400227 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500228 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500229 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400230 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400231 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300232 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400233 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400234 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100235 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400236 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400237 int focus_count;
238
Pekka Paalanen99436862012-11-19 17:15:59 +0200239 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200240 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000241 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400242
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200243 enum preferred_format preferred_format;
244
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500245 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500246 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500249 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400250 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200251 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252
Pekka Paalanen4e373742013-02-13 16:17:13 +0200253 struct surface *main_surface;
254 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300255
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500256 struct window_frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500257
Pekka Paalanen35e82632013-04-25 13:57:48 +0300258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
260
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500261 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400262 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500263};
264
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500265struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500266 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200267 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300268 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500269 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400270 struct wl_list link;
271 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500276 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500277 widget_button_handler_t button_handler;
Rusty Lynch041815a2013-08-08 21:20:38 -0700278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200283 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400284 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500285 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300286 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500287 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400288};
289
Rusty Lynch041815a2013-08-08 21:20:38 -0700290struct touch_point {
291 int32_t id;
292 struct widget *widget;
293 struct wl_list link;
294};
295
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400296struct input {
297 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
Rusty Lynch041815a2013-08-08 21:20:38 -0700301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400303 struct window *pointer_focus;
304 struct window *keyboard_focus;
Rusty Lynch041815a2013-08-08 21:20:38 -0700305 struct window *touch_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300306 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300309 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400310 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400311 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400312 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400313 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400315
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500316 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500317 struct widget *grab;
318 uint32_t grab_button;
319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800323 uint32_t touch_grab;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
Daniel Stone97f68542012-05-30 16:32:01 +0100328
329 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
335 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400336
337 struct task repeat_task;
338 int repeat_timer_fd;
339 uint32_t repeat_sym;
340 uint32_t repeat_key;
341 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400342};
343
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500344struct output {
345 struct display *display;
346 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800347 uint32_t server_output_id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500348 struct rectangle allocation;
349 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600350 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200351 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200352
353 display_output_handler_t destroy_handler;
354 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500355};
356
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500357struct window_frame {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500358 struct widget *widget;
359 struct widget *child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -0500360 struct frame *frame;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500361};
362
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500363struct menu {
364 struct window *window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -0500365 struct window *parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500366 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500367 struct input *input;
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700368 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500369 const char **entries;
370 uint32_t time;
371 int current;
372 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400373 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500374 menu_func_t func;
375};
376
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300377struct tooltip {
378 struct widget *parent;
379 struct window *window;
380 struct widget *widget;
381 char *entry;
382 struct task tooltip_task;
383 int tooltip_fd;
384 float x, y;
385};
386
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700387struct shm_pool {
388 struct wl_shm_pool *pool;
389 size_t size;
390 size_t used;
391 void *data;
392};
393
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400394enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300395 CURSOR_DEFAULT = 100,
396 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400397};
398
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500399enum window_location {
400 WINDOW_INTERIOR = 0,
401 WINDOW_RESIZING_TOP = 1,
402 WINDOW_RESIZING_BOTTOM = 2,
403 WINDOW_RESIZING_LEFT = 4,
404 WINDOW_RESIZING_TOP_LEFT = 5,
405 WINDOW_RESIZING_BOTTOM_LEFT = 6,
406 WINDOW_RESIZING_RIGHT = 8,
407 WINDOW_RESIZING_TOP_RIGHT = 9,
408 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
409 WINDOW_RESIZING_MASK = 15,
410 WINDOW_EXTERIOR = 16,
411 WINDOW_TITLEBAR = 17,
412 WINDOW_CLIENT_AREA = 18,
413};
414
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200415static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400416
Pekka Paalanen97777442013-05-22 10:20:05 +0300417/* #define DEBUG */
418
419#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300420
421static void
422debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
423__attribute__ ((format (printf, 4, 5)));
424
425static void
426debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
427{
428 va_list ap;
429 struct timeval tv;
430
431 gettimeofday(&tv, NULL);
432 fprintf(stderr, "%8ld.%03ld ",
433 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
434
435 if (proxy)
436 fprintf(stderr, "%s@%d ",
437 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
438
439 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
440 fprintf(stderr, "%s ", func);
441
442 va_start(ap, fmt);
443 vfprintf(stderr, fmt, ap);
444 va_end(ap);
445}
446
447#define DBG(fmt, ...) \
448 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
449
450#define DBG_OBJ(obj, fmt, ...) \
451 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
452
453#else
454
455#define DBG(...) do {} while (0)
456#define DBG_OBJ(...) do {} while (0)
457
458#endif
459
Alexander Larsson1818e312013-05-22 14:41:31 +0200460static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200461surface_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 +0200462{
463 int32_t tmp;
464
465 switch (buffer_transform) {
466 case WL_OUTPUT_TRANSFORM_90:
467 case WL_OUTPUT_TRANSFORM_270:
468 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
469 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
470 tmp = *width;
471 *width = *height;
472 *height = tmp;
473 break;
474 default:
475 break;
476 }
477
478 *width *= buffer_scale;
479 *height *= buffer_scale;
480}
481
482static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200483buffer_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 +0200484{
485 int32_t tmp;
486
487 switch (buffer_transform) {
488 case WL_OUTPUT_TRANSFORM_90:
489 case WL_OUTPUT_TRANSFORM_270:
490 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
491 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
492 tmp = *width;
493 *width = *height;
494 *height = tmp;
495 break;
496 default:
497 break;
498 }
499
500 *width /= buffer_scale;
501 *height /= buffer_scale;
502}
503
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500504#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400505
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200506struct egl_window_surface {
507 struct toysurface base;
508 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100509 struct display *display;
510 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200511 struct wl_egl_window *egl_window;
512 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100513};
514
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200515static struct egl_window_surface *
516to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100517{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200518 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100519}
520
521static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200522egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200523 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200524 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526 struct egl_window_surface *surface = to_egl_window_surface(base);
527
Alexander Larsson1818e312013-05-22 14:41:31 +0200528 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
529
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
531 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
532
533 return cairo_surface_reference(surface->cairo_surface);
534}
535
536static void
537egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200538 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200539 struct rectangle *server_allocation)
540{
541 struct egl_window_surface *surface = to_egl_window_surface(base);
542
543 cairo_gl_surface_swapbuffers(surface->cairo_surface);
544 wl_egl_window_get_attached_size(surface->egl_window,
545 &server_allocation->width,
546 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200547
548 buffer_to_surface_size (buffer_transform, buffer_scale,
549 &server_allocation->width,
550 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200551}
552
553static int
554egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
555{
556 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200557 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100558
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559 device = cairo_surface_get_device(surface->cairo_surface);
560 if (!device)
561 return -1;
562
563 if (!ctx) {
564 if (device == surface->display->argb_device)
565 ctx = surface->display->argb_ctx;
566 else
567 assert(0);
568 }
569
570 cairo_device_flush(device);
571 cairo_device_acquire(device);
572 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
573 surface->egl_surface, ctx))
574 fprintf(stderr, "failed to make surface current\n");
575
576 return 0;
577}
578
579static void
580egl_window_surface_release(struct toysurface *base)
581{
582 struct egl_window_surface *surface = to_egl_window_surface(base);
583 cairo_device_t *device;
584
585 device = cairo_surface_get_device(surface->cairo_surface);
586 if (!device)
587 return;
588
589 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
590 surface->display->argb_ctx))
591 fprintf(stderr, "failed to make context current\n");
592
593 cairo_device_release(device);
594}
595
596static void
597egl_window_surface_destroy(struct toysurface *base)
598{
599 struct egl_window_surface *surface = to_egl_window_surface(base);
600 struct display *d = surface->display;
601
602 cairo_surface_destroy(surface->cairo_surface);
603 eglDestroySurface(d->dpy, surface->egl_surface);
604 wl_egl_window_destroy(surface->egl_window);
605 surface->surface = NULL;
606
607 free(surface);
608}
609
610static struct toysurface *
611egl_window_surface_create(struct display *display,
612 struct wl_surface *wl_surface,
613 uint32_t flags,
614 struct rectangle *rectangle)
615{
616 struct egl_window_surface *surface;
617
Pekka Paalanenb3627362012-11-19 17:16:00 +0200618 if (display->dpy == EGL_NO_DISPLAY)
619 return NULL;
620
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200621 surface = calloc(1, sizeof *surface);
622 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100623 return NULL;
624
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200625 surface->base.prepare = egl_window_surface_prepare;
626 surface->base.swap = egl_window_surface_swap;
627 surface->base.acquire = egl_window_surface_acquire;
628 surface->base.release = egl_window_surface_release;
629 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100630
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200631 surface->display = display;
632 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400633
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200634 surface->egl_window = wl_egl_window_create(surface->surface,
635 rectangle->width,
636 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100637
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200638 surface->egl_surface = eglCreateWindowSurface(display->dpy,
639 display->argb_config,
640 surface->egl_window,
641 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100642
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200643 surface->cairo_surface =
644 cairo_gl_surface_create_for_egl(display->argb_device,
645 surface->egl_surface,
646 rectangle->width,
647 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100648
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200649 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650}
651
Pekka Paalanenb3627362012-11-19 17:16:00 +0200652#else
653
654static struct toysurface *
655egl_window_surface_create(struct display *display,
656 struct wl_surface *wl_surface,
657 uint32_t flags,
658 struct rectangle *rectangle)
659{
660 return NULL;
661}
662
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400663#endif
664
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200665struct shm_surface_data {
666 struct wl_buffer *buffer;
667 struct shm_pool *pool;
668};
669
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400670struct wl_buffer *
671display_get_buffer_for_surface(struct display *display,
672 cairo_surface_t *surface)
673{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200674 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400675
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200676 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400677
678 return data->buffer;
679}
680
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500681static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700682shm_pool_destroy(struct shm_pool *pool);
683
684static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400685shm_surface_data_destroy(void *p)
686{
687 struct shm_surface_data *data = p;
688
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200689 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700690 if (data->pool)
691 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300692
693 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400694}
695
Kristian Høgsberg16626282012-04-03 11:21:27 -0400696static struct wl_shm_pool *
697make_shm_pool(struct display *display, int size, void **data)
698{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400699 struct wl_shm_pool *pool;
700 int fd;
701
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300702 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400703 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300704 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
705 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400706 return NULL;
707 }
708
709 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400710 if (*data == MAP_FAILED) {
711 fprintf(stderr, "mmap failed: %m\n");
712 close(fd);
713 return NULL;
714 }
715
716 pool = wl_shm_create_pool(display->shm, fd, size);
717
718 close(fd);
719
720 return pool;
721}
722
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700723static struct shm_pool *
724shm_pool_create(struct display *display, size_t size)
725{
726 struct shm_pool *pool = malloc(sizeof *pool);
727
728 if (!pool)
729 return NULL;
730
731 pool->pool = make_shm_pool(display, size, &pool->data);
732 if (!pool->pool) {
733 free(pool);
734 return NULL;
735 }
736
737 pool->size = size;
738 pool->used = 0;
739
740 return pool;
741}
742
743static void *
744shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
745{
746 if (pool->used + size > pool->size)
747 return NULL;
748
749 *offset = pool->used;
750 pool->used += size;
751
752 return (char *) pool->data + *offset;
753}
754
755/* destroy the pool. this does not unmap the memory though */
756static void
757shm_pool_destroy(struct shm_pool *pool)
758{
759 munmap(pool->data, pool->size);
760 wl_shm_pool_destroy(pool->pool);
761 free(pool);
762}
763
764/* Start allocating from the beginning of the pool again */
765static void
766shm_pool_reset(struct shm_pool *pool)
767{
768 pool->used = 0;
769}
770
771static int
772data_length_for_shm_surface(struct rectangle *rect)
773{
774 int stride;
775
776 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
777 rect->width);
778 return stride * rect->height;
779}
780
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500781static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700782display_create_shm_surface_from_pool(struct display *display,
783 struct rectangle *rectangle,
784 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400785{
786 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400787 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400788 cairo_surface_t *surface;
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200789 cairo_format_t cairo_format;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700790 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400791 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400792
793 data = malloc(sizeof *data);
794 if (data == NULL)
795 return NULL;
796
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200797 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
798 cairo_format = CAIRO_FORMAT_RGB16_565;
799 else
800 cairo_format = CAIRO_FORMAT_ARGB32;
801
802 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700803 length = stride * rectangle->height;
804 data->pool = NULL;
805 map = shm_pool_allocate(pool, length, &offset);
806
807 if (!map) {
808 free(data);
809 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400810 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400811
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400812 surface = cairo_image_surface_create_for_data (map,
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200813 cairo_format,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400814 rectangle->width,
815 rectangle->height,
816 stride);
817
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200818 cairo_surface_set_user_data(surface, &shm_surface_data_key,
819 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400820
Tomeu Vizosobee45a12013-08-06 20:05:54 +0200821 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
822 format = WL_SHM_FORMAT_RGB565;
823 else {
824 if (flags & SURFACE_OPAQUE)
825 format = WL_SHM_FORMAT_XRGB8888;
826 else
827 format = WL_SHM_FORMAT_ARGB8888;
828 }
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400829
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200830 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
831 rectangle->width,
832 rectangle->height,
833 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400834
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700835 return surface;
836}
837
838static cairo_surface_t *
839display_create_shm_surface(struct display *display,
840 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200841 struct shm_pool *alternate_pool,
842 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700843{
844 struct shm_surface_data *data;
845 struct shm_pool *pool;
846 cairo_surface_t *surface;
847
Pekka Paalanen99436862012-11-19 17:15:59 +0200848 if (alternate_pool) {
849 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700850 surface = display_create_shm_surface_from_pool(display,
851 rectangle,
852 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200853 alternate_pool);
854 if (surface) {
855 data = cairo_surface_get_user_data(surface,
856 &shm_surface_data_key);
857 goto out;
858 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700859 }
860
861 pool = shm_pool_create(display,
862 data_length_for_shm_surface(rectangle));
863 if (!pool)
864 return NULL;
865
866 surface =
867 display_create_shm_surface_from_pool(display, rectangle,
868 flags, pool);
869
870 if (!surface) {
871 shm_pool_destroy(pool);
872 return NULL;
873 }
874
875 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200876 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700877 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400878
Pekka Paalanen99436862012-11-19 17:15:59 +0200879out:
880 if (data_ret)
881 *data_ret = data;
882
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400883 return surface;
884}
885
nobled7b87cb02011-02-01 18:51:47 +0000886static int
887check_size(struct rectangle *rect)
888{
889 if (rect->width && rect->height)
890 return 0;
891
892 fprintf(stderr, "tried to create surface of "
893 "width: %d, height: %d\n", rect->width, rect->height);
894 return -1;
895}
896
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400897cairo_surface_t *
898display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100899 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400900 struct rectangle *rectangle,
901 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400902{
nobled7b87cb02011-02-01 18:51:47 +0000903 if (check_size(rectangle) < 0)
904 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200905
906 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200907 return display_create_shm_surface(display, rectangle, flags,
908 NULL, NULL);
909}
910
Pekka Paalanena4eda732012-11-19 17:16:02 +0200911struct shm_surface_leaf {
912 cairo_surface_t *cairo_surface;
913 /* 'data' is automatically destroyed, when 'cairo_surface' is */
914 struct shm_surface_data *data;
915
916 struct shm_pool *resize_pool;
917 int busy;
918};
919
920static void
921shm_surface_leaf_release(struct shm_surface_leaf *leaf)
922{
923 if (leaf->cairo_surface)
924 cairo_surface_destroy(leaf->cairo_surface);
925 /* leaf->data already destroyed via cairo private */
926
927 if (leaf->resize_pool)
928 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200929
930 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200931}
932
Pekka Paalanenaef02542013-04-25 13:57:47 +0300933#define MAX_LEAVES 3
934
Pekka Paalanen99436862012-11-19 17:15:59 +0200935struct shm_surface {
936 struct toysurface base;
937 struct display *display;
938 struct wl_surface *surface;
939 uint32_t flags;
940 int dx, dy;
941
Pekka Paalanenaef02542013-04-25 13:57:47 +0300942 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200943 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200944};
945
946static struct shm_surface *
947to_shm_surface(struct toysurface *base)
948{
949 return container_of(base, struct shm_surface, base);
950}
951
Pekka Paalanena4eda732012-11-19 17:16:02 +0200952static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300953shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
954{
955#ifdef DEBUG
956 struct shm_surface_leaf *leaf;
957 char bufs[MAX_LEAVES + 1];
958 int i;
959
960 for (i = 0; i < MAX_LEAVES; i++) {
961 leaf = &surface->leaf[i];
962
963 if (leaf->busy)
964 bufs[i] = 'b';
965 else if (leaf->cairo_surface)
966 bufs[i] = 'a';
967 else
968 bufs[i] = ' ';
969 }
970
971 bufs[MAX_LEAVES] = '\0';
972 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
973#endif
974}
975
976static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200977shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
978{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200979 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300980 struct shm_surface_leaf *leaf;
981 int i;
982 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300983
984 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200985
Pekka Paalanenaef02542013-04-25 13:57:47 +0300986 for (i = 0; i < MAX_LEAVES; i++) {
987 leaf = &surface->leaf[i];
988 if (leaf->data && leaf->data->buffer == buffer) {
989 leaf->busy = 0;
990 break;
991 }
992 }
993 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200994
Pekka Paalanenaef02542013-04-25 13:57:47 +0300995 /* Leave one free leaf with storage, release others */
996 free_found = 0;
997 for (i = 0; i < MAX_LEAVES; i++) {
998 leaf = &surface->leaf[i];
999
1000 if (!leaf->cairo_surface || leaf->busy)
1001 continue;
1002
1003 if (!free_found)
1004 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001005 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001006 shm_surface_leaf_release(leaf);
1007 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001008
Pekka Paalanen97777442013-05-22 10:20:05 +03001009 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001010}
1011
1012static const struct wl_buffer_listener shm_surface_buffer_listener = {
1013 shm_surface_buffer_release
1014};
1015
Pekka Paalanen99436862012-11-19 17:15:59 +02001016static cairo_surface_t *
1017shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001018 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001019 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001020{
Pekka Paalanenec076692012-11-30 13:37:27 +02001021 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001022 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001023 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001024 struct shm_surface_leaf *leaf = NULL;
1025 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001026
1027 surface->dx = dx;
1028 surface->dy = dy;
1029
Pekka Paalanenaef02542013-04-25 13:57:47 +03001030 /* pick a free buffer, preferrably one that already has storage */
1031 for (i = 0; i < MAX_LEAVES; i++) {
1032 if (surface->leaf[i].busy)
1033 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001034
Pekka Paalanenaef02542013-04-25 13:57:47 +03001035 if (!leaf || surface->leaf[i].cairo_surface)
1036 leaf = &surface->leaf[i];
1037 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001038 DBG_OBJ(surface->surface, "pick leaf %d\n",
1039 (int)(leaf - &surface->leaf[0]));
1040
Pekka Paalanenaef02542013-04-25 13:57:47 +03001041 if (!leaf) {
1042 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001043 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001044 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001045 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001046 }
1047
Pekka Paalanena4eda732012-11-19 17:16:02 +02001048 if (!resize_hint && leaf->resize_pool) {
1049 cairo_surface_destroy(leaf->cairo_surface);
1050 leaf->cairo_surface = NULL;
1051 shm_pool_destroy(leaf->resize_pool);
1052 leaf->resize_pool = NULL;
1053 }
1054
Alexander Larsson1818e312013-05-22 14:41:31 +02001055 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1056
Pekka Paalanena4eda732012-11-19 17:16:02 +02001057 if (leaf->cairo_surface &&
1058 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1059 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001060 goto out;
1061
Pekka Paalanena4eda732012-11-19 17:16:02 +02001062 if (leaf->cairo_surface)
1063 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001064
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001065#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001066 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001067 /* Create a big pool to allocate from, while continuously
1068 * resizing. Mmapping a new pool in the server
1069 * is relatively expensive, so reusing a pool performs
1070 * better, but may temporarily reserve unneeded memory.
1071 */
1072 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001073 leaf->resize_pool = shm_pool_create(surface->display,
1074 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001075 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001076#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001077
Alexander Larsson1818e312013-05-22 14:41:31 +02001078 rect.width = width;
1079 rect.height = height;
1080
Pekka Paalanena4eda732012-11-19 17:16:02 +02001081 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001082 display_create_shm_surface(surface->display, &rect,
1083 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001084 leaf->resize_pool,
1085 &leaf->data);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02001086 if (!leaf->cairo_surface)
1087 return NULL;
1088
Pekka Paalanena4eda732012-11-19 17:16:02 +02001089 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001090 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001091
1092out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001093 surface->current = leaf;
1094
1095 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001096}
1097
1098static void
1099shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001100 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001101 struct rectangle *server_allocation)
1102{
1103 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001104 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001105
1106 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001107 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001108 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001109 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001110
Alexander Larsson1818e312013-05-22 14:41:31 +02001111 buffer_to_surface_size (buffer_transform, buffer_scale,
1112 &server_allocation->width,
1113 &server_allocation->height);
1114
Pekka Paalanena4eda732012-11-19 17:16:02 +02001115 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001116 surface->dx, surface->dy);
1117 wl_surface_damage(surface->surface, 0, 0,
1118 server_allocation->width, server_allocation->height);
1119 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120
Pekka Paalanen71233882013-04-25 13:57:53 +03001121 DBG_OBJ(surface->surface, "leaf %d busy\n",
1122 (int)(leaf - &surface->leaf[0]));
1123
Pekka Paalanena4eda732012-11-19 17:16:02 +02001124 leaf->busy = 1;
1125 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001126}
1127
1128static int
1129shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1130{
1131 return -1;
1132}
1133
1134static void
1135shm_surface_release(struct toysurface *base)
1136{
1137}
1138
1139static void
1140shm_surface_destroy(struct toysurface *base)
1141{
1142 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001143 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001144
Pekka Paalanenaef02542013-04-25 13:57:47 +03001145 for (i = 0; i < MAX_LEAVES; i++)
1146 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001147
1148 free(surface);
1149}
1150
1151static struct toysurface *
1152shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1153 uint32_t flags, struct rectangle *rectangle)
1154{
1155 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001156 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001157
Brian Lovinbc919262013-08-07 15:34:59 -07001158 surface = xmalloc(sizeof *surface);
1159 memset(surface, 0, sizeof *surface);
1160
Pekka Paalanen99436862012-11-19 17:15:59 +02001161 if (!surface)
1162 return NULL;
1163
1164 surface->base.prepare = shm_surface_prepare;
1165 surface->base.swap = shm_surface_swap;
1166 surface->base.acquire = shm_surface_acquire;
1167 surface->base.release = shm_surface_release;
1168 surface->base.destroy = shm_surface_destroy;
1169
1170 surface->display = display;
1171 surface->surface = wl_surface;
1172 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001173
1174 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001175}
1176
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001177/*
1178 * The following correspondences between file names and cursors was copied
1179 * from: https://bugs.kde.org/attachment.cgi?id=67313
1180 */
1181
1182static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001183 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001184 "sw-resize",
1185 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001186};
1187
1188static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001189 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001190 "se-resize",
1191 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192};
1193
1194static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001195 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001196 "s-resize",
1197 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001198};
1199
1200static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001201 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001202 "closedhand",
1203 "208530c400c041818281048008011002"
1204};
1205
1206static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001207 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001208 "default",
1209 "top_left_arrow",
1210 "left-arrow"
1211};
1212
1213static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001215 "w-resize",
1216 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001217};
1218
1219static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001220 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001221 "e-resize",
1222 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001223};
1224
1225static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001227 "nw-resize",
1228 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229};
1230
1231static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001233 "ne-resize",
1234 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235};
1236
1237static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001239 "n-resize",
1240 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001241};
1242
1243static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001244 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001245 "ibeam",
1246 "text"
1247};
1248
1249static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001250 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001251 "pointer",
1252 "pointing_hand",
1253 "e29285e634086352946a0e7090d73106"
1254};
1255
1256static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001257 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001258 "wait",
1259 "0426c94ea35c87780ff01dc239897213"
1260};
1261
1262struct cursor_alternatives {
1263 const char **names;
1264 size_t count;
1265};
1266
1267static const struct cursor_alternatives cursors[] = {
1268 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1269 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1270 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1271 {grabbings, ARRAY_LENGTH(grabbings)},
1272 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1273 {left_sides, ARRAY_LENGTH(left_sides)},
1274 {right_sides, ARRAY_LENGTH(right_sides)},
1275 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1276 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1277 {top_sides, ARRAY_LENGTH(top_sides)},
1278 {xterms, ARRAY_LENGTH(xterms)},
1279 {hand1s, ARRAY_LENGTH(hand1s)},
1280 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001281};
1282
1283static void
1284create_cursors(struct display *display)
1285{
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001286 struct weston_config *config;
1287 struct weston_config_section *s;
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001288 int size;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001289 char *theme = NULL;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001290 unsigned int i, j;
1291 struct wl_cursor *cursor;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001292
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001293 config = weston_config_parse("weston.ini");
Kristian Høgsberg8c079ae2013-09-21 22:26:10 -07001294 s = weston_config_get_section(config, "shell", NULL, NULL);
1295 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1296 weston_config_section_get_int(s, "cursor-size", &size, 32);
1297 weston_config_destroy(config);
1298
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001299 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Kristian Høgsbergb5c973c2013-10-09 13:02:04 -07001300 free(theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001301 display->cursors =
Brian Lovinbc919262013-08-07 15:34:59 -07001302 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001303
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001304 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001305 cursor = NULL;
1306 for (j = 0; !cursor && j < cursors[i].count; ++j)
1307 cursor = wl_cursor_theme_get_cursor(
1308 display->cursor_theme, cursors[i].names[j]);
1309
1310 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001311 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001312 cursors[i].names[0]);
1313
1314 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001315 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001316}
1317
1318static void
1319destroy_cursors(struct display *display)
1320{
1321 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001322 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001323}
1324
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001325struct wl_cursor_image *
1326display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001327{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001328 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001329
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001330 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001331}
1332
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001333static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001334surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001335{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001336 if (!surface->cairo_surface)
1337 return;
1338
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001339 if (surface->opaque_region) {
1340 wl_surface_set_opaque_region(surface->surface,
1341 surface->opaque_region);
1342 wl_region_destroy(surface->opaque_region);
1343 surface->opaque_region = NULL;
1344 }
1345
1346 if (surface->input_region) {
1347 wl_surface_set_input_region(surface->surface,
1348 surface->input_region);
1349 wl_region_destroy(surface->input_region);
1350 surface->input_region = NULL;
1351 }
1352
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001353 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001354 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001355 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001356
Pekka Paalanen89dee002013-02-13 16:17:20 +02001357 cairo_surface_destroy(surface->cairo_surface);
1358 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001359}
1360
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001361int
1362window_has_focus(struct window *window)
1363{
1364 return window->focus_count > 0;
1365}
1366
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001367static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001368window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001369{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001370 struct surface *surface;
1371
Pekka Paalanen89dee002013-02-13 16:17:20 +02001372 if (window->type == TYPE_NONE) {
1373 window->type = TYPE_TOPLEVEL;
1374 if (window->shell_surface)
1375 wl_shell_surface_set_toplevel(window->shell_surface);
1376 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001377
Pekka Paalanen35e82632013-04-25 13:57:48 +03001378 wl_list_for_each(surface, &window->subsurface_list, link) {
1379 if (surface == window->main_surface)
1380 continue;
1381
1382 surface_flush(surface);
1383 }
1384
Pekka Paalanen89dee002013-02-13 16:17:20 +02001385 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001386}
1387
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001388struct display *
1389window_get_display(struct window *window)
1390{
1391 return window->display;
1392}
1393
Pekka Paalanen89dee002013-02-13 16:17:20 +02001394static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001395surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001396{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001397 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001398 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001399
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001400 if (!surface->toysurface && display->dpy &&
1401 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001402 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001403 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001404 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001405 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001406 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001407 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001408
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001409 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001410 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001411 surface->surface,
1412 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001413
Pekka Paalanen89dee002013-02-13 16:17:20 +02001414 surface->cairo_surface = surface->toysurface->prepare(
1415 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001416 allocation.width, allocation.height, flags,
1417 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001418}
1419
1420static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001421window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001422{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001423 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001425 int dx = 0;
1426 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001427
Pekka Paalanenec076692012-11-30 13:37:27 +02001428 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001429 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001430
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001431 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1432 flags |= SURFACE_HINT_RGB565;
1433
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001434 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1435 dx = surface->server_allocation.width -
1436 surface->allocation.width;
1437
1438 if (window->resize_edges & WINDOW_RESIZING_TOP)
1439 dy = surface->server_allocation.height -
1440 surface->allocation.height;
1441
1442 window->resize_edges = 0;
1443
Pekka Paalanen89dee002013-02-13 16:17:20 +02001444 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001445}
1446
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001447int
1448window_get_buffer_transform(struct window *window)
1449{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001450 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001451}
1452
1453void
1454window_set_buffer_transform(struct window *window,
1455 enum wl_output_transform transform)
1456{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001457 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001458 wl_surface_set_buffer_transform(window->main_surface->surface,
1459 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001460}
1461
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001462void
1463window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001464 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001465{
1466 window->main_surface->buffer_scale = scale;
1467 wl_surface_set_buffer_scale(window->main_surface->surface,
1468 scale);
1469}
1470
1471uint32_t
1472window_get_buffer_scale(struct window *window)
1473{
1474 return window->main_surface->buffer_scale;
1475}
1476
Alexander Larssond68f5232013-05-22 14:41:33 +02001477uint32_t
1478window_get_output_scale(struct window *window)
1479{
1480 struct window_output *window_output;
1481 struct window_output *window_output_tmp;
1482 int scale = 1;
1483
1484 wl_list_for_each_safe(window_output, window_output_tmp,
1485 &window->window_output_list, link) {
1486 if (window_output->output->scale > scale)
1487 scale = window_output->output->scale;
1488 }
1489
1490 return scale;
1491}
1492
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05001493static void window_frame_destroy(struct window_frame *frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001494
Pekka Paalanen4e373742013-02-13 16:17:13 +02001495static void
1496surface_destroy(struct surface *surface)
1497{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001498 if (surface->frame_cb)
1499 wl_callback_destroy(surface->frame_cb);
1500
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001501 if (surface->input_region)
1502 wl_region_destroy(surface->input_region);
1503
1504 if (surface->opaque_region)
1505 wl_region_destroy(surface->opaque_region);
1506
Pekka Paalanen35e82632013-04-25 13:57:48 +03001507 if (surface->subsurface)
1508 wl_subsurface_destroy(surface->subsurface);
1509
Pekka Paalanen4e373742013-02-13 16:17:13 +02001510 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001511
1512 if (surface->toysurface)
1513 surface->toysurface->destroy(surface->toysurface);
1514
Pekka Paalanen35e82632013-04-25 13:57:48 +03001515 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001516 free(surface);
1517}
1518
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001519void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001520window_destroy(struct window *window)
1521{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001522 struct display *display = window->display;
1523 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001524 struct window_output *window_output;
1525 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001526
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001527 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001528
Rusty Lynch1084da52013-08-15 09:10:08 -07001529 wl_list_for_each(input, &display->input_list, link) {
1530 if (input->touch_focus == window)
1531 input->touch_focus = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001532 if (input->pointer_focus == window)
1533 input->pointer_focus = NULL;
1534 if (input->keyboard_focus == window)
1535 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001536 if (input->focus_widget &&
1537 input->focus_widget->window == window)
1538 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001539 }
1540
Rob Bradford7507b572012-05-15 17:55:34 +01001541 wl_list_for_each_safe(window_output, window_output_tmp,
1542 &window->window_output_list, link) {
1543 free (window_output);
1544 }
1545
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001546 if (window->frame)
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05001547 window_frame_destroy(window->frame);
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001548
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001549 if (window->shell_surface)
1550 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001551
1552 surface_destroy(window->main_surface);
1553
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001554 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001555
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001556 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001557 free(window);
1558}
1559
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001560static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001561widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001562{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001563 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001564
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001565 wl_list_for_each(child, &widget->child_list, link) {
1566 target = widget_find_widget(child, x, y);
1567 if (target)
1568 return target;
1569 }
1570
1571 if (widget->allocation.x <= x &&
1572 x < widget->allocation.x + widget->allocation.width &&
1573 widget->allocation.y <= y &&
1574 y < widget->allocation.y + widget->allocation.height) {
1575 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001576 }
1577
1578 return NULL;
1579}
1580
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001581static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001582window_find_widget(struct window *window, int32_t x, int32_t y)
1583{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001584 struct surface *surface;
1585 struct widget *widget;
1586
1587 wl_list_for_each(surface, &window->subsurface_list, link) {
1588 widget = widget_find_widget(surface->widget, x, y);
1589 if (widget)
1590 return widget;
1591 }
1592
1593 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001594}
1595
1596static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001597widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001598{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001599 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001600
Peter Huttererf3d62272013-08-08 11:57:05 +10001601 widget = xzalloc(sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001602 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001603 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001604 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001605 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001606 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001607 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001608 widget->tooltip = NULL;
1609 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001610 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001611
1612 return widget;
1613}
1614
1615struct widget *
1616window_add_widget(struct window *window, void *data)
1617{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001618 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001619
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001620 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001621 wl_list_init(&widget->link);
1622 window->main_surface->widget = widget;
1623
1624 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001625}
1626
1627struct widget *
1628widget_add_widget(struct widget *parent, void *data)
1629{
1630 struct widget *widget;
1631
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001632 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001633 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001634
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001635 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001636}
1637
1638void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001639widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001640{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001641 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001642 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001643 struct input *input;
1644
Pekka Paalanen35e82632013-04-25 13:57:48 +03001645 /* Destroy the sub-surface along with the root widget */
1646 if (surface->widget == widget && surface->subsurface)
1647 surface_destroy(widget->surface);
1648
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001649 if (widget->tooltip) {
1650 free(widget->tooltip);
1651 widget->tooltip = NULL;
1652 }
1653
Pekka Paalanene156fb62012-01-19 13:51:38 +02001654 wl_list_for_each(input, &display->input_list, link) {
1655 if (input->focus_widget == widget)
1656 input->focus_widget = NULL;
1657 }
1658
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001659 wl_list_remove(&widget->link);
1660 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001661}
1662
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001663void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001664widget_set_default_cursor(struct widget *widget, int cursor)
1665{
1666 widget->default_cursor = cursor;
1667}
1668
1669void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001670widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001671{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001672 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001673}
1674
1675void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001676widget_set_size(struct widget *widget, int32_t width, int32_t height)
1677{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001678 widget->allocation.width = width;
1679 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001680}
1681
1682void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001683widget_set_allocation(struct widget *widget,
1684 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001685{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001686 widget->allocation.x = x;
1687 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001688 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001689}
1690
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001691void
1692widget_set_transparent(struct widget *widget, int transparent)
1693{
1694 widget->opaque = !transparent;
1695}
1696
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001697void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001698widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001699{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001700 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001701}
1702
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001703static cairo_surface_t *
1704widget_get_cairo_surface(struct widget *widget)
1705{
1706 struct surface *surface = widget->surface;
1707 struct window *window = widget->window;
1708
1709 if (!surface->cairo_surface) {
1710 if (surface == window->main_surface)
1711 window_create_main_surface(window);
1712 else
1713 surface_create_surface(surface, 0, 0, 0);
1714 }
1715
1716 return surface->cairo_surface;
1717}
1718
Alexander Larsson15901f02013-05-22 14:41:25 +02001719static void
1720widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1721{
1722 struct surface *surface = widget->surface;
1723 double angle;
1724 cairo_matrix_t m;
1725 enum wl_output_transform transform;
1726 int surface_width, surface_height;
1727 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001728 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001729
1730 surface_width = surface->allocation.width;
1731 surface_height = surface->allocation.height;
1732
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001733 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001734 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001735
Alexander Larsson15901f02013-05-22 14:41:25 +02001736 switch (transform) {
1737 case WL_OUTPUT_TRANSFORM_FLIPPED:
1738 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1739 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1740 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1741 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1742 break;
1743 default:
1744 cairo_matrix_init_identity(&m);
1745 break;
1746 }
1747
1748 switch (transform) {
1749 case WL_OUTPUT_TRANSFORM_NORMAL:
1750 default:
1751 angle = 0;
1752 translate_x = 0;
1753 translate_y = 0;
1754 break;
1755 case WL_OUTPUT_TRANSFORM_FLIPPED:
1756 angle = 0;
1757 translate_x = surface_width;
1758 translate_y = 0;
1759 break;
1760 case WL_OUTPUT_TRANSFORM_90:
1761 angle = M_PI_2;
1762 translate_x = surface_height;
1763 translate_y = 0;
1764 break;
1765 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1766 angle = M_PI_2;
1767 translate_x = surface_height;
1768 translate_y = surface_width;
1769 break;
1770 case WL_OUTPUT_TRANSFORM_180:
1771 angle = M_PI;
1772 translate_x = surface_width;
1773 translate_y = surface_height;
1774 break;
1775 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1776 angle = M_PI;
1777 translate_x = 0;
1778 translate_y = surface_height;
1779 break;
1780 case WL_OUTPUT_TRANSFORM_270:
1781 angle = M_PI + M_PI_2;
1782 translate_x = 0;
1783 translate_y = surface_width;
1784 break;
1785 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1786 angle = M_PI + M_PI_2;
1787 translate_x = 0;
1788 translate_y = 0;
1789 break;
1790 }
1791
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001792 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001793 cairo_translate(cr, translate_x, translate_y);
1794 cairo_rotate(cr, angle);
1795 cairo_transform(cr, &m);
1796}
1797
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001798cairo_t *
1799widget_cairo_create(struct widget *widget)
1800{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001801 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001802 cairo_surface_t *cairo_surface;
1803 cairo_t *cr;
1804
1805 cairo_surface = widget_get_cairo_surface(widget);
1806 cr = cairo_create(cairo_surface);
1807
Alexander Larsson15901f02013-05-22 14:41:25 +02001808 widget_cairo_update_transform(widget, cr);
1809
Pekka Paalanen35e82632013-04-25 13:57:48 +03001810 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1811
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001812 return cr;
1813}
1814
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001815struct wl_surface *
1816widget_get_wl_surface(struct widget *widget)
1817{
1818 return widget->surface->surface;
1819}
1820
Neil Roberts5e10a042013-09-09 00:40:17 +01001821struct wl_subsurface *
1822widget_get_wl_subsurface(struct widget *widget)
1823{
1824 return widget->surface->subsurface;
1825}
1826
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001827uint32_t
1828widget_get_last_time(struct widget *widget)
1829{
1830 return widget->surface->last_time;
1831}
1832
1833void
1834widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1835{
1836 struct wl_compositor *comp = widget->window->display->compositor;
1837 struct surface *surface = widget->surface;
1838
1839 if (!surface->input_region)
1840 surface->input_region = wl_compositor_create_region(comp);
1841
1842 if (rect) {
1843 wl_region_add(surface->input_region,
1844 rect->x, rect->y, rect->width, rect->height);
1845 }
1846}
1847
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001848void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001849widget_set_resize_handler(struct widget *widget,
1850 widget_resize_handler_t handler)
1851{
1852 widget->resize_handler = handler;
1853}
1854
1855void
1856widget_set_redraw_handler(struct widget *widget,
1857 widget_redraw_handler_t handler)
1858{
1859 widget->redraw_handler = handler;
1860}
1861
1862void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001863widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001864{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001865 widget->enter_handler = handler;
1866}
1867
1868void
1869widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1870{
1871 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001872}
1873
1874void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001875widget_set_motion_handler(struct widget *widget,
1876 widget_motion_handler_t handler)
1877{
1878 widget->motion_handler = handler;
1879}
1880
1881void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001882widget_set_button_handler(struct widget *widget,
1883 widget_button_handler_t handler)
1884{
1885 widget->button_handler = handler;
1886}
1887
1888void
Rusty Lynch041815a2013-08-08 21:20:38 -07001889widget_set_touch_up_handler(struct widget *widget,
1890 widget_touch_up_handler_t handler)
1891{
1892 widget->touch_up_handler = handler;
1893}
1894
1895void
1896widget_set_touch_down_handler(struct widget *widget,
1897 widget_touch_down_handler_t handler)
1898{
1899 widget->touch_down_handler = handler;
1900}
1901
1902void
1903widget_set_touch_motion_handler(struct widget *widget,
1904 widget_touch_motion_handler_t handler)
1905{
1906 widget->touch_motion_handler = handler;
1907}
1908
1909void
1910widget_set_touch_frame_handler(struct widget *widget,
1911 widget_touch_frame_handler_t handler)
1912{
1913 widget->touch_frame_handler = handler;
1914}
1915
1916void
1917widget_set_touch_cancel_handler(struct widget *widget,
1918 widget_touch_cancel_handler_t handler)
1919{
1920 widget->touch_cancel_handler = handler;
1921}
1922
1923void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001924widget_set_axis_handler(struct widget *widget,
1925 widget_axis_handler_t handler)
1926{
1927 widget->axis_handler = handler;
1928}
1929
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001930static void
1931window_schedule_redraw_task(struct window *window);
1932
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001933void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001934widget_schedule_redraw(struct widget *widget)
1935{
Pekka Paalanen71233882013-04-25 13:57:53 +03001936 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001937 widget->surface->redraw_needed = 1;
1938 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001939}
1940
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001941cairo_surface_t *
1942window_get_surface(struct window *window)
1943{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001944 cairo_surface_t *cairo_surface;
1945
1946 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1947
1948 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001949}
1950
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001951struct wl_surface *
1952window_get_wl_surface(struct window *window)
1953{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001954 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001955}
1956
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001957static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001958tooltip_redraw_handler(struct widget *widget, void *data)
1959{
1960 cairo_t *cr;
1961 const int32_t r = 3;
1962 struct tooltip *tooltip = data;
1963 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001964
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001965 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001966 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1967 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1968 cairo_paint(cr);
1969
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001970 width = widget->allocation.width;
1971 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001972 rounded_rect(cr, 0, 0, width, height, r);
1973
1974 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1975 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1976 cairo_fill(cr);
1977
1978 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1979 cairo_move_to(cr, 10, 16);
1980 cairo_show_text(cr, tooltip->entry);
1981 cairo_destroy(cr);
1982}
1983
1984static cairo_text_extents_t
1985get_text_extents(struct tooltip *tooltip)
1986{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001987 cairo_t *cr;
1988 cairo_text_extents_t extents;
1989
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001990 /* Use the dummy_surface because tooltip's surface was not
1991 * created yet, and parent does not have a valid surface
1992 * outside repaint, either.
1993 */
1994 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001995 cairo_text_extents(cr, tooltip->entry, &extents);
1996 cairo_destroy(cr);
1997
1998 return extents;
1999}
2000
2001static int
2002window_create_tooltip(struct tooltip *tooltip)
2003{
2004 struct widget *parent = tooltip->parent;
2005 struct display *display = parent->window->display;
2006 struct window *window;
2007 const int offset_y = 27;
2008 const int margin = 3;
2009 cairo_text_extents_t extents;
2010
2011 if (tooltip->widget)
2012 return 0;
2013
2014 window = window_create_transient(display, parent->window, tooltip->x,
2015 tooltip->y + offset_y,
2016 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2017 if (!window)
2018 return -1;
2019
2020 tooltip->window = window;
2021 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2022
2023 extents = get_text_extents(tooltip);
2024 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2025 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2026
2027 return 0;
2028}
2029
2030void
2031widget_destroy_tooltip(struct widget *parent)
2032{
2033 struct tooltip *tooltip = parent->tooltip;
2034
2035 parent->tooltip_count = 0;
2036 if (!tooltip)
2037 return;
2038
2039 if (tooltip->widget) {
2040 widget_destroy(tooltip->widget);
2041 window_destroy(tooltip->window);
2042 tooltip->widget = NULL;
2043 tooltip->window = NULL;
2044 }
2045
2046 close(tooltip->tooltip_fd);
2047 free(tooltip->entry);
2048 free(tooltip);
2049 parent->tooltip = NULL;
2050}
2051
2052static void
2053tooltip_func(struct task *task, uint32_t events)
2054{
2055 struct tooltip *tooltip =
2056 container_of(task, struct tooltip, tooltip_task);
2057 uint64_t exp;
2058
Martin Olsson8df662a2012-07-08 03:03:47 +02002059 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2060 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002061 window_create_tooltip(tooltip);
2062}
2063
2064#define TOOLTIP_TIMEOUT 500
2065static int
2066tooltip_timer_reset(struct tooltip *tooltip)
2067{
2068 struct itimerspec its;
2069
2070 its.it_interval.tv_sec = 0;
2071 its.it_interval.tv_nsec = 0;
2072 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2073 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2074 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2075 fprintf(stderr, "could not set timerfd\n: %m");
2076 return -1;
2077 }
2078
2079 return 0;
2080}
2081
2082int
2083widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2084{
2085 struct tooltip *tooltip = parent->tooltip;
2086
2087 parent->tooltip_count++;
2088 if (tooltip) {
2089 tooltip->x = x;
2090 tooltip->y = y;
2091 tooltip_timer_reset(tooltip);
2092 return 0;
2093 }
2094
2095 /* the handler might be triggered too fast via input device motion, so
2096 * we need this check here to make sure tooltip is fully initialized */
2097 if (parent->tooltip_count > 1)
2098 return 0;
2099
2100 tooltip = malloc(sizeof *tooltip);
2101 if (!tooltip)
2102 return -1;
2103
2104 parent->tooltip = tooltip;
2105 tooltip->parent = parent;
2106 tooltip->widget = NULL;
2107 tooltip->window = NULL;
2108 tooltip->x = x;
2109 tooltip->y = y;
2110 tooltip->entry = strdup(entry);
2111 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2112 if (tooltip->tooltip_fd < 0) {
2113 fprintf(stderr, "could not create timerfd\n: %m");
2114 return -1;
2115 }
2116
2117 tooltip->tooltip_task.run = tooltip_func;
2118 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2119 EPOLLIN, &tooltip->tooltip_task);
2120 tooltip_timer_reset(tooltip);
2121
2122 return 0;
2123}
2124
2125static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002126workspace_manager_state(void *data,
2127 struct workspace_manager *workspace_manager,
2128 uint32_t current,
2129 uint32_t count)
2130{
2131 struct display *display = data;
2132
2133 display->workspace = current;
2134 display->workspace_count = count;
2135}
2136
2137static const struct workspace_manager_listener workspace_manager_listener = {
2138 workspace_manager_state
2139};
2140
2141static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002142frame_resize_handler(struct widget *widget,
2143 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002144{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002145 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146 struct widget *child = frame->child;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002147 struct rectangle interior;
2148 struct rectangle input;
2149 struct rectangle opaque;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002150
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002151 if (widget->window->type == TYPE_FULLSCREEN) {
2152 interior.x = 0;
2153 interior.y = 0;
2154 interior.width = width;
2155 interior.height = height;
2156 } else {
2157 if (widget->window->type == TYPE_MAXIMIZED) {
2158 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2159 } else {
2160 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2161 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002162
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002163 frame_resize(frame->frame, width, height);
2164 frame_interior(frame->frame, &interior.x, &interior.y,
2165 &interior.width, &interior.height);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002166 }
2167
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002168 widget_set_allocation(child, interior.x, interior.y,
2169 interior.width, interior.height);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002170
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002171 if (child->resize_handler) {
2172 child->resize_handler(child, interior.width, interior.height,
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002173 child->user_data);
2174
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002175 if (widget->window->type == TYPE_FULLSCREEN) {
2176 width = child->allocation.width;
2177 height = child->allocation.height;
2178 } else {
2179 frame_resize_inside(frame->frame,
2180 child->allocation.width,
2181 child->allocation.height);
2182 width = frame_width(frame->frame);
2183 height = frame_height(frame->frame);
2184 }
Kristian Høgsberg023be102012-07-31 11:59:12 -04002185 }
2186
Scott Moreauf7e498c2012-05-14 11:39:29 -06002187 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002188
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002189 widget->surface->input_region =
2190 wl_compositor_create_region(widget->window->display->compositor);
2191 if (widget->window->type != TYPE_FULLSCREEN) {
2192 frame_input_rect(frame->frame, &input.x, &input.y,
2193 &input.width, &input.height);
2194 wl_region_add(widget->surface->input_region,
2195 input.x, input.y, input.width, input.height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002196 } else {
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002197 wl_region_add(widget->surface->input_region, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002198 }
2199
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002200 widget_set_allocation(widget, 0, 0, width, height);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002201
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002202 if (child->opaque) {
Kristian Høgsberg598477d2013-10-16 16:06:18 -07002203 if (widget->window->type != TYPE_FULLSCREEN) {
2204 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2205 &opaque.width, &opaque.height);
2206
2207 wl_region_add(widget->surface->opaque_region,
2208 opaque.x, opaque.y,
2209 opaque.width, opaque.height);
2210 } else {
2211 wl_region_add(widget->surface->opaque_region,
2212 0, 0, width, height);
2213 }
Martin Minarik1998b152012-05-10 02:04:35 +02002214 }
2215
Martin Minarik1998b152012-05-10 02:04:35 +02002216
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002217 widget_schedule_redraw(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002218}
2219
2220static void
2221frame_redraw_handler(struct widget *widget, void *data)
2222{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002223 cairo_t *cr;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002224 struct window_frame *frame = data;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002225 struct window *window = widget->window;
2226
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002227 if (window->type == TYPE_FULLSCREEN)
2228 return;
2229
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002230 if (window->focus_count) {
2231 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2232 } else {
2233 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2234 }
2235
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002236 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002237
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002238 frame_repaint(frame->frame, cr);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002239
2240 cairo_destroy(cr);
2241}
2242
2243static int
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002244frame_get_pointer_image_for_location(struct window_frame *frame,
2245 enum theme_location location)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002246{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002247 struct window *window = frame->widget->window;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002248
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002249 if (window->type != TYPE_TOPLEVEL)
2250 return CURSOR_LEFT_PTR;
2251
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002252 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002253 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002254 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002255 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002256 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002257 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002258 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002259 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002260 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002261 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002262 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002263 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002264 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002265 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002266 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002267 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002268 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002269 case THEME_LOCATION_EXTERIOR:
2270 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002271 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002272 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002273 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002274}
2275
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002276static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002277frame_menu_func(struct window *window,
2278 struct input *input, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002279{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002280 struct display *display;
2281
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002282 switch (index) {
2283 case 0: /* close */
2284 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002285 window->close_handler(window->user_data);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002286 else
2287 display_exit(window->display);
2288 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002289 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002290 display = window->display;
2291 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002292 workspace_manager_move_surface(
2293 display->workspace_manager,
2294 window->main_surface->surface,
2295 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002296 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002297 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002298 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002299 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002300 workspace_manager_move_surface(
2301 display->workspace_manager,
2302 window->main_surface->surface,
2303 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002304 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002305 case 3: /* fullscreen */
2306 /* we don't have a way to get out of fullscreen for now */
2307 if (window->fullscreen_handler)
2308 window->fullscreen_handler(window, window->user_data);
2309 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002310 }
2311}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002312
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002313void
2314window_show_frame_menu(struct window *window,
2315 struct input *input, uint32_t time)
2316{
2317 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002318 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002319
2320 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002321 "Close",
2322 "Move to workspace above", "Move to workspace below",
2323 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002324 };
2325
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002326 if (window->fullscreen_handler)
2327 count = ARRAY_LENGTH(entries);
2328 else
2329 count = ARRAY_LENGTH(entries) - 1;
2330
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002331 input_get_position(input, &x, &y);
2332 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002333 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002334}
2335
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002336static int
2337frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002338 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002339{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002340 struct window_frame *frame = data;
2341 enum theme_location location;
2342
2343 location = frame_pointer_enter(frame->frame, input, x, y);
2344 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2345 widget_schedule_redraw(frame->widget);
2346
2347 return frame_get_pointer_image_for_location(data, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002348}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002349
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002350static int
2351frame_motion_handler(struct widget *widget,
2352 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002353 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002354{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002355 struct window_frame *frame = data;
2356 enum theme_location location;
2357
2358 location = frame_pointer_motion(frame->frame, input, x, y);
2359 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2360 widget_schedule_redraw(frame->widget);
2361
2362 return frame_get_pointer_image_for_location(data, location);
2363}
2364
2365static void
2366frame_leave_handler(struct widget *widget,
2367 struct input *input, void *data)
2368{
2369 struct window_frame *frame = data;
2370
2371 frame_pointer_leave(frame->frame, input);
2372 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2373 widget_schedule_redraw(frame->widget);
2374}
2375
2376static void
2377frame_handle_status(struct window_frame *frame, struct input *input,
2378 uint32_t time, enum theme_location location)
2379{
2380 struct window *window = frame->widget->window;
2381 uint32_t status;
2382
2383 status = frame_status(frame->frame);
2384 if (status & FRAME_STATUS_REPAINT)
2385 widget_schedule_redraw(frame->widget);
2386
2387 if (status & FRAME_STATUS_MINIMIZE)
2388 fprintf(stderr,"Minimize stub\n");
2389
2390 if (status & FRAME_STATUS_MENU) {
2391 window_show_frame_menu(window, input, time);
2392 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2393 }
2394
2395 if (status & FRAME_STATUS_MAXIMIZE) {
2396 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2397 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2398 }
2399
2400 if (status & FRAME_STATUS_CLOSE) {
2401 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002402 window->close_handler(window->user_data);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002403 else
2404 display_exit(window->display);
Jason Ekstrand4a7409a2013-10-27 21:32:54 -05002405 return;
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002406 }
2407
2408 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2409 input_ungrab(input);
2410 wl_shell_surface_move(window->shell_surface,
2411 input_get_seat(input),
2412 window->display->serial);
2413
2414 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2415 }
2416
2417 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2418 input_ungrab(input);
2419
2420 window->resizing = 1;
2421 wl_shell_surface_resize(window->shell_surface,
2422 input_get_seat(input),
2423 window->display->serial,
2424 location);
2425
2426 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2427 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002428}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002429
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002430static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002431frame_button_handler(struct widget *widget,
2432 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002433 uint32_t button, enum wl_pointer_button_state state,
2434 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002435
2436{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002437 struct window_frame *frame = data;
2438 enum theme_location location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002439
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002440 location = frame_pointer_button(frame->frame, input, button, state);
2441 frame_handle_status(frame, input, time, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002442}
2443
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002444static void
Rusty Lynch1084da52013-08-15 09:10:08 -07002445frame_touch_down_handler(struct widget *widget, struct input *input,
2446 uint32_t serial, uint32_t time, int32_t id,
2447 float x, float y, void *data)
2448{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002449 struct window_frame *frame = data;
2450
2451 frame_touch_down(frame->frame, input, id, x, y);
2452 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2453}
2454
2455static void
2456frame_touch_up_handler(struct widget *widget,
2457 struct input *input, uint32_t serial, uint32_t time,
2458 int32_t id, void *data)
2459{
2460 struct window_frame *frame = data;
2461
2462 frame_touch_up(frame->frame, input, id);
2463 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
Rusty Lynch1084da52013-08-15 09:10:08 -07002464}
2465
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002466struct widget *
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002467window_frame_create(struct window *window, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002468{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002469 struct window_frame *frame;
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002470 uint32_t buttons;
2471
2472 if (window->type == TYPE_CUSTOM) {
2473 buttons = FRAME_BUTTON_NONE;
2474 } else {
2475 buttons = FRAME_BUTTON_ALL;
2476 }
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002477
Peter Huttererf3d62272013-08-08 11:57:05 +10002478 frame = xzalloc(sizeof *frame);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002479 frame->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsbergc152ee12013-12-31 15:35:39 -08002480 buttons, window->title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002481
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002482 frame->widget = window_add_widget(window, frame);
2483 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002484
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002485 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2486 widget_set_resize_handler(frame->widget, frame_resize_handler);
2487 widget_set_enter_handler(frame->widget, frame_enter_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002488 widget_set_leave_handler(frame->widget, frame_leave_handler);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002489 widget_set_motion_handler(frame->widget, frame_motion_handler);
2490 widget_set_button_handler(frame->widget, frame_button_handler);
Rusty Lynch1084da52013-08-15 09:10:08 -07002491 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002492 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002493
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002494 window->frame = frame;
2495
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002496 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002497}
2498
Kristian Høgsberga1627922012-06-20 17:30:03 -04002499void
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002500window_frame_set_child_size(struct widget *widget, int child_width,
2501 int child_height)
Kristian Høgsberga1627922012-06-20 17:30:03 -04002502{
2503 struct display *display = widget->window->display;
2504 struct theme *t = display->theme;
2505 int decoration_width, decoration_height;
2506 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002507 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002508
2509 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002510 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002511 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002512 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002513
2514 width = child_width + decoration_width;
2515 height = child_height + decoration_height;
2516 } else {
2517 width = child_width;
2518 height = child_height;
2519 }
2520
2521 window_schedule_resize(widget->window, width, height);
2522}
2523
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002524static void
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002525window_frame_destroy(struct window_frame *frame)
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002526{
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05002527 frame_destroy(frame->frame);
Martin Minarik1998b152012-05-10 02:04:35 +02002528
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002529 /* frame->child must be destroyed by the application */
2530 widget_destroy(frame->widget);
2531 free(frame);
2532}
2533
2534static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002535input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002536 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002537{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002538 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002539 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002540
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002541 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002542 return;
2543
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002544 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002545 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002546 widget = old;
2547 if (input->grab)
2548 widget = input->grab;
2549 if (widget->leave_handler)
2550 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002551 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002552 }
2553
2554 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002555 widget = focus;
2556 if (input->grab)
2557 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002558 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002559 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002560 cursor = widget->enter_handler(focus, input, x, y,
2561 widget->user_data);
2562 else
2563 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002564
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002565 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002566 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002567}
2568
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002569void
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08002570touch_grab(struct input *input, int32_t touch_id)
2571{
2572 input->touch_grab = 1;
2573 input->touch_grab_id = touch_id;
2574}
2575
2576void
2577touch_ungrab(struct input *input)
2578{
2579 struct touch_point *tp, *tmp;
2580
2581 input->touch_grab = 0;
2582
2583 wl_list_for_each_safe(tp, tmp,
2584 &input->touch_point_list, link) {
2585 if (tp->id != input->touch_grab_id)
2586 continue;
2587 wl_list_remove(&tp->link);
2588 free(tp);
2589
2590 return;
2591 }
2592}
2593
2594void
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002595input_grab(struct input *input, struct widget *widget, uint32_t button)
2596{
2597 input->grab = widget;
2598 input->grab_button = button;
2599}
2600
2601void
2602input_ungrab(struct input *input)
2603{
2604 struct widget *widget;
2605
2606 input->grab = NULL;
2607 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002608 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002609 input->sx, input->sy);
2610 input_set_focus_widget(input, widget, input->sx, input->sy);
2611 }
2612}
2613
2614static void
2615input_remove_pointer_focus(struct input *input)
2616{
2617 struct window *window = input->pointer_focus;
2618
2619 if (!window)
2620 return;
2621
2622 input_set_focus_widget(input, NULL, 0, 0);
2623
2624 input->pointer_focus = NULL;
2625 input->current_cursor = CURSOR_UNSET;
2626}
2627
2628static void
2629pointer_handle_enter(void *data, struct wl_pointer *pointer,
2630 uint32_t serial, struct wl_surface *surface,
2631 wl_fixed_t sx_w, wl_fixed_t sy_w)
2632{
2633 struct input *input = data;
2634 struct window *window;
2635 struct widget *widget;
2636 float sx = wl_fixed_to_double(sx_w);
2637 float sy = wl_fixed_to_double(sy_w);
2638
2639 if (!surface) {
2640 /* enter event for a window we've just destroyed */
2641 return;
2642 }
2643
2644 input->display->serial = serial;
2645 input->pointer_enter_serial = serial;
2646 input->pointer_focus = wl_surface_get_user_data(surface);
2647 window = input->pointer_focus;
2648
Pekka Paalanen99436862012-11-19 17:15:59 +02002649 if (window->resizing) {
2650 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002651 /* Schedule a redraw to free the pool */
2652 window_schedule_redraw(window);
2653 }
2654
2655 input->sx = sx;
2656 input->sy = sy;
2657
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002658 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002659 input_set_focus_widget(input, widget, sx, sy);
2660}
2661
2662static void
2663pointer_handle_leave(void *data, struct wl_pointer *pointer,
2664 uint32_t serial, struct wl_surface *surface)
2665{
2666 struct input *input = data;
2667
2668 input->display->serial = serial;
2669 input_remove_pointer_focus(input);
2670}
2671
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002672static void
Daniel Stone37816df2012-05-16 18:45:18 +01002673pointer_handle_motion(void *data, struct wl_pointer *pointer,
2674 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002675{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002676 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002677 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002678 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002679 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002680 float sx = wl_fixed_to_double(sx_w);
2681 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002682
Paul Winwoodb22bf572013-08-29 10:52:54 +01002683 input->sx = sx;
2684 input->sy = sy;
2685
2686 if (!window)
2687 return;
2688
Rob Bradford5f087742013-07-11 19:41:27 +01002689 /* when making the window smaller - e.g. after a unmaximise we might
2690 * still have a pending motion event that the compositor has picked
2691 * based on the old surface dimensions
2692 */
2693 if (sx > window->main_surface->allocation.width ||
2694 sy > window->main_surface->allocation.height)
2695 return;
2696
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002697 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002698 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002699 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002700 }
2701
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002702 if (input->grab)
2703 widget = input->grab;
2704 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002705 widget = input->focus_widget;
Kristian Høgsberg1a5f0c32013-08-15 14:15:18 -07002706 if (widget) {
2707 if (widget->motion_handler)
2708 cursor = widget->motion_handler(input->focus_widget,
2709 input, time, sx, sy,
2710 widget->user_data);
2711 else
2712 cursor = widget->default_cursor;
2713 } else
2714 cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002715
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002716 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002717}
2718
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002719static void
Daniel Stone37816df2012-05-16 18:45:18 +01002720pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002721 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002722{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002723 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002724 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002725 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002726
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002727 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002728 if (input->focus_widget && input->grab == NULL &&
2729 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002730 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002731
Neil Roberts6b28aad2012-01-23 19:11:18 +00002732 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002733 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002734 (*widget->button_handler)(widget,
2735 input, time,
2736 button, state,
2737 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002738
Daniel Stone4dbadb12012-05-30 16:31:51 +01002739 if (input->grab && input->grab_button == button &&
2740 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002741 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002742}
2743
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002744static void
Daniel Stone37816df2012-05-16 18:45:18 +01002745pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002746 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002747{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002748 struct input *input = data;
2749 struct widget *widget;
2750
2751 widget = input->focus_widget;
2752 if (input->grab)
2753 widget = input->grab;
2754 if (widget && widget->axis_handler)
2755 (*widget->axis_handler)(widget,
2756 input, time,
2757 axis, value,
2758 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002759}
2760
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002761static const struct wl_pointer_listener pointer_listener = {
2762 pointer_handle_enter,
2763 pointer_handle_leave,
2764 pointer_handle_motion,
2765 pointer_handle_button,
2766 pointer_handle_axis,
2767};
2768
2769static void
2770input_remove_keyboard_focus(struct input *input)
2771{
2772 struct window *window = input->keyboard_focus;
2773 struct itimerspec its;
2774
2775 its.it_interval.tv_sec = 0;
2776 its.it_interval.tv_nsec = 0;
2777 its.it_value.tv_sec = 0;
2778 its.it_value.tv_nsec = 0;
2779 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2780
2781 if (!window)
2782 return;
2783
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002784 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002785 if (window->keyboard_focus_handler)
2786 (*window->keyboard_focus_handler)(window, NULL,
2787 window->user_data);
2788
2789 input->keyboard_focus = NULL;
2790}
2791
2792static void
2793keyboard_repeat_func(struct task *task, uint32_t events)
2794{
2795 struct input *input =
2796 container_of(task, struct input, repeat_task);
2797 struct window *window = input->keyboard_focus;
2798 uint64_t exp;
2799
2800 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2801 /* If we change the timer between the fd becoming
2802 * readable and getting here, there'll be nothing to
2803 * read and we get EAGAIN. */
2804 return;
2805
2806 if (window && window->key_handler) {
2807 (*window->key_handler)(window, input, input->repeat_time,
2808 input->repeat_key, input->repeat_sym,
2809 WL_KEYBOARD_KEY_STATE_PRESSED,
2810 window->user_data);
2811 }
2812}
2813
2814static void
2815keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2816 uint32_t format, int fd, uint32_t size)
2817{
2818 struct input *input = data;
Rui Matos3eccb862013-10-10 19:44:22 +02002819 struct xkb_keymap *keymap;
2820 struct xkb_state *state;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002821 char *map_str;
2822
2823 if (!data) {
2824 close(fd);
2825 return;
2826 }
2827
2828 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2829 close(fd);
2830 return;
2831 }
2832
2833 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2834 if (map_str == MAP_FAILED) {
2835 close(fd);
2836 return;
2837 }
2838
Rui Matos3eccb862013-10-10 19:44:22 +02002839 keymap = xkb_map_new_from_string(input->display->xkb_context,
2840 map_str,
2841 XKB_KEYMAP_FORMAT_TEXT_V1,
2842 0);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002843 munmap(map_str, size);
2844 close(fd);
2845
Rui Matos3eccb862013-10-10 19:44:22 +02002846 if (!keymap) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002847 fprintf(stderr, "failed to compile keymap\n");
2848 return;
2849 }
2850
Rui Matos3eccb862013-10-10 19:44:22 +02002851 state = xkb_state_new(keymap);
2852 if (!state) {
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002853 fprintf(stderr, "failed to create XKB state\n");
Rui Matos3eccb862013-10-10 19:44:22 +02002854 xkb_map_unref(keymap);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002855 return;
2856 }
2857
Rui Matos3eccb862013-10-10 19:44:22 +02002858 xkb_keymap_unref(input->xkb.keymap);
2859 xkb_state_unref(input->xkb.state);
2860 input->xkb.keymap = keymap;
2861 input->xkb.state = state;
2862
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002863 input->xkb.control_mask =
2864 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2865 input->xkb.alt_mask =
2866 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2867 input->xkb.shift_mask =
2868 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2869}
2870
2871static void
2872keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2873 uint32_t serial, struct wl_surface *surface,
2874 struct wl_array *keys)
2875{
2876 struct input *input = data;
2877 struct window *window;
2878
2879 input->display->serial = serial;
2880 input->keyboard_focus = wl_surface_get_user_data(surface);
2881
2882 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002883 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002884 if (window->keyboard_focus_handler)
2885 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002886 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002887}
2888
2889static void
2890keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2891 uint32_t serial, struct wl_surface *surface)
2892{
2893 struct input *input = data;
2894
2895 input->display->serial = serial;
2896 input_remove_keyboard_focus(input);
2897}
2898
Scott Moreau210d0792012-03-22 10:47:01 -06002899static void
Daniel Stone37816df2012-05-16 18:45:18 +01002900keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002901 uint32_t serial, uint32_t time, uint32_t key,
2902 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002903{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002904 struct input *input = data;
2905 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002906 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002907 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002908 const xkb_keysym_t *syms;
2909 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002910 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002911
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002912 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00002913 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002914 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002915 return;
2916
Daniel Stone97f68542012-05-30 16:32:01 +01002917 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002918
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002919 sym = XKB_KEY_NoSymbol;
2920 if (num_syms == 1)
2921 sym = syms[0];
2922
2923 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01002924 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002925 window_set_maximized(window,
2926 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002927 } else if (sym == XKB_KEY_F11 &&
2928 window->fullscreen_handler &&
2929 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2930 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002931 } else if (sym == XKB_KEY_F4 &&
2932 input->modifiers == MOD_ALT_MASK &&
2933 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2934 if (window->close_handler)
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002935 window->close_handler(window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04002936 else
2937 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05002938 } else if (window->key_handler) {
2939 (*window->key_handler)(window, input, time, key,
2940 sym, state, window->user_data);
2941 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04002942
2943 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2944 key == input->repeat_key) {
2945 its.it_interval.tv_sec = 0;
2946 its.it_interval.tv_nsec = 0;
2947 its.it_value.tv_sec = 0;
2948 its.it_value.tv_nsec = 0;
2949 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2950 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2951 input->repeat_sym = sym;
2952 input->repeat_key = key;
2953 input->repeat_time = time;
2954 its.it_interval.tv_sec = 0;
2955 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2956 its.it_value.tv_sec = 0;
2957 its.it_value.tv_nsec = 400 * 1000 * 1000;
2958 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2959 }
2960}
2961
2962static void
Daniel Stone351eb612012-05-31 15:27:47 -04002963keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2964 uint32_t serial, uint32_t mods_depressed,
2965 uint32_t mods_latched, uint32_t mods_locked,
2966 uint32_t group)
2967{
2968 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002969 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04002970
Matt Ropere61561f2013-06-24 16:52:43 +01002971 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2972 if (!input->xkb.keymap)
2973 return;
2974
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002975 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2976 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02002977 mask = xkb_state_serialize_mods(input->xkb.state,
2978 XKB_STATE_DEPRESSED |
2979 XKB_STATE_LATCHED);
2980 input->modifiers = 0;
2981 if (mask & input->xkb.control_mask)
2982 input->modifiers |= MOD_CONTROL_MASK;
2983 if (mask & input->xkb.alt_mask)
2984 input->modifiers |= MOD_ALT_MASK;
2985 if (mask & input->xkb.shift_mask)
2986 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04002987}
2988
Daniel Stone37816df2012-05-16 18:45:18 +01002989static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002990 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01002991 keyboard_handle_enter,
2992 keyboard_handle_leave,
2993 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04002994 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01002995};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05002996
2997static void
Rusty Lynch041815a2013-08-08 21:20:38 -07002998touch_handle_down(void *data, struct wl_touch *wl_touch,
2999 uint32_t serial, uint32_t time, struct wl_surface *surface,
3000 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3001{
3002 struct input *input = data;
3003 struct widget *widget;
3004 float sx = wl_fixed_to_double(x_w);
3005 float sy = wl_fixed_to_double(y_w);
3006
Rusty Lynch1084da52013-08-15 09:10:08 -07003007 input->display->serial = serial;
Rusty Lynch041815a2013-08-08 21:20:38 -07003008 input->touch_focus = wl_surface_get_user_data(surface);
3009 if (!input->touch_focus) {
3010 DBG("Failed to find to touch focus for surface %p\n", surface);
3011 return;
3012 }
3013
3014 widget = window_find_widget(input->touch_focus,
3015 wl_fixed_to_double(x_w),
3016 wl_fixed_to_double(y_w));
3017 if (widget) {
3018 struct touch_point *tp = xmalloc(sizeof *tp);
3019 if (tp) {
3020 tp->id = id;
3021 tp->widget = widget;
3022 wl_list_insert(&input->touch_point_list, &tp->link);
3023
3024 if (widget->touch_down_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003025 (*widget->touch_down_handler)(widget, input,
3026 serial, time, id,
Rusty Lynch041815a2013-08-08 21:20:38 -07003027 sx, sy,
3028 widget->user_data);
3029 }
3030 }
3031}
3032
3033static void
3034touch_handle_up(void *data, struct wl_touch *wl_touch,
3035 uint32_t serial, uint32_t time, int32_t id)
3036{
3037 struct input *input = data;
3038 struct touch_point *tp, *tmp;
3039
Rusty Lynch041815a2013-08-08 21:20:38 -07003040 if (!input->touch_focus) {
3041 DBG("No touch focus found for touch up event!\n");
3042 return;
3043 }
3044
3045 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3046 if (tp->id != id)
3047 continue;
3048
3049 if (tp->widget->touch_up_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003050 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
Rusty Lynch041815a2013-08-08 21:20:38 -07003051 time, id,
3052 tp->widget->user_data);
3053
3054 wl_list_remove(&tp->link);
3055 free(tp);
3056
3057 return;
3058 }
3059}
3060
3061static void
3062touch_handle_motion(void *data, struct wl_touch *wl_touch,
3063 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3064{
3065 struct input *input = data;
3066 struct touch_point *tp;
3067 float sx = wl_fixed_to_double(x_w);
3068 float sy = wl_fixed_to_double(y_w);
3069
3070 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3071
3072 if (!input->touch_focus) {
3073 DBG("No touch focus found for touch motion event!\n");
3074 return;
3075 }
3076
3077 wl_list_for_each(tp, &input->touch_point_list, link) {
3078 if (tp->id != id)
3079 continue;
3080
3081 if (tp->widget->touch_motion_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003082 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
Rusty Lynch041815a2013-08-08 21:20:38 -07003083 id, sx, sy,
3084 tp->widget->user_data);
3085 return;
3086 }
3087}
3088
3089static void
3090touch_handle_frame(void *data, struct wl_touch *wl_touch)
3091{
3092 struct input *input = data;
3093 struct touch_point *tp, *tmp;
3094
3095 DBG("touch_handle_frame\n");
3096
3097 if (!input->touch_focus) {
3098 DBG("No touch focus found for touch frame event!\n");
3099 return;
3100 }
3101
3102 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3103 if (tp->widget->touch_frame_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003104 (*tp->widget->touch_frame_handler)(tp->widget, input,
3105 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003106
3107 wl_list_remove(&tp->link);
3108 free(tp);
3109 }
3110}
3111
3112static void
3113touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3114{
3115 struct input *input = data;
3116 struct touch_point *tp, *tmp;
3117
3118 DBG("touch_handle_cancel\n");
3119
3120 if (!input->touch_focus) {
3121 DBG("No touch focus found for touch cancel event!\n");
3122 return;
3123 }
3124
3125 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3126 if (tp->widget->touch_cancel_handler)
Rusty Lynch1084da52013-08-15 09:10:08 -07003127 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3128 tp->widget->user_data);
Rusty Lynch041815a2013-08-08 21:20:38 -07003129
3130 wl_list_remove(&tp->link);
3131 free(tp);
3132 }
3133}
3134
3135static const struct wl_touch_listener touch_listener = {
3136 touch_handle_down,
3137 touch_handle_up,
3138 touch_handle_motion,
3139 touch_handle_frame,
3140 touch_handle_cancel,
3141};
3142
3143static void
Daniel Stone37816df2012-05-16 18:45:18 +01003144seat_handle_capabilities(void *data, struct wl_seat *seat,
3145 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003146{
Daniel Stone37816df2012-05-16 18:45:18 +01003147 struct input *input = data;
3148
3149 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3150 input->pointer = wl_seat_get_pointer(seat);
3151 wl_pointer_set_user_data(input->pointer, input);
3152 wl_pointer_add_listener(input->pointer, &pointer_listener,
3153 input);
3154 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3155 wl_pointer_destroy(input->pointer);
3156 input->pointer = NULL;
3157 }
3158
3159 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3160 input->keyboard = wl_seat_get_keyboard(seat);
3161 wl_keyboard_set_user_data(input->keyboard, input);
3162 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3163 input);
3164 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3165 wl_keyboard_destroy(input->keyboard);
3166 input->keyboard = NULL;
3167 }
Rusty Lynch041815a2013-08-08 21:20:38 -07003168
3169 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3170 input->touch = wl_seat_get_touch(seat);
3171 wl_touch_set_user_data(input->touch, input);
3172 wl_touch_add_listener(input->touch, &touch_listener, input);
3173 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3174 wl_touch_destroy(input->touch);
3175 input->touch = NULL;
3176 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003177}
3178
Rob Bradford08031182013-08-13 20:11:03 +01003179static void
3180seat_handle_name(void *data, struct wl_seat *seat,
3181 const char *name)
3182{
3183
3184}
3185
Daniel Stone37816df2012-05-16 18:45:18 +01003186static const struct wl_seat_listener seat_listener = {
3187 seat_handle_capabilities,
Rob Bradford08031182013-08-13 20:11:03 +01003188 seat_handle_name
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003189};
3190
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003191void
3192input_get_position(struct input *input, int32_t *x, int32_t *y)
3193{
3194 *x = input->sx;
3195 *y = input->sy;
3196}
3197
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003198struct display *
3199input_get_display(struct input *input)
3200{
3201 return input->display;
3202}
3203
Daniel Stone37816df2012-05-16 18:45:18 +01003204struct wl_seat *
3205input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003206{
Daniel Stone37816df2012-05-16 18:45:18 +01003207 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003208}
3209
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003210uint32_t
3211input_get_modifiers(struct input *input)
3212{
3213 return input->modifiers;
3214}
3215
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003216struct widget *
3217input_get_focus_widget(struct input *input)
3218{
3219 return input->focus_widget;
3220}
3221
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003222struct data_offer {
3223 struct wl_data_offer *offer;
3224 struct input *input;
3225 struct wl_array types;
3226 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003227
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003228 struct task io_task;
3229 int fd;
3230 data_func_t func;
3231 int32_t x, y;
3232 void *user_data;
3233};
3234
3235static void
3236data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3237{
3238 struct data_offer *offer = data;
3239 char **p;
3240
3241 p = wl_array_add(&offer->types, sizeof *p);
3242 *p = strdup(type);
3243}
3244
3245static const struct wl_data_offer_listener data_offer_listener = {
3246 data_offer_offer,
3247};
3248
3249static void
3250data_offer_destroy(struct data_offer *offer)
3251{
3252 char **p;
3253
3254 offer->refcount--;
3255 if (offer->refcount == 0) {
3256 wl_data_offer_destroy(offer->offer);
3257 for (p = offer->types.data; *p; p++)
3258 free(*p);
3259 wl_array_release(&offer->types);
3260 free(offer);
3261 }
3262}
3263
3264static void
3265data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003266 struct wl_data_device *data_device,
3267 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003268{
3269 struct data_offer *offer;
3270
Brian Lovinbc919262013-08-07 15:34:59 -07003271 offer = xmalloc(sizeof *offer);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003272
3273 wl_array_init(&offer->types);
3274 offer->refcount = 1;
3275 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003276 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003277 wl_data_offer_add_listener(offer->offer,
3278 &data_offer_listener, offer);
3279}
3280
3281static void
3282data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003283 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003284 wl_fixed_t x_w, wl_fixed_t y_w,
3285 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003286{
3287 struct input *input = data;
3288 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003289 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003290 float x = wl_fixed_to_double(x_w);
3291 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003292 char **p;
3293
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003294 window = wl_surface_get_user_data(surface);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003295 input->drag_enter_serial = serial;
3296 input->drag_focus = window,
3297 input->drag_x = x;
3298 input->drag_y = y;
3299
3300 if (!input->touch_grab)
3301 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003302
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003303 if (offer) {
3304 input->drag_offer = wl_data_offer_get_user_data(offer);
3305
3306 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3307 *p = NULL;
3308
3309 types_data = input->drag_offer->types.data;
3310 } else {
3311 input->drag_offer = NULL;
3312 types_data = NULL;
3313 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003314
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003315 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003316 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003317 window->user_data);
3318}
3319
3320static void
3321data_device_leave(void *data, struct wl_data_device *data_device)
3322{
3323 struct input *input = data;
3324
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003325 if (input->drag_offer) {
3326 data_offer_destroy(input->drag_offer);
3327 input->drag_offer = NULL;
3328 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003329}
3330
3331static void
3332data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003333 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003334{
3335 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003336 struct window *window = input->drag_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003337 float x = wl_fixed_to_double(x_w);
3338 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003339 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003340
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003341 input->drag_x = x;
3342 input->drag_y = y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003343
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003344 if (input->drag_offer)
3345 types_data = input->drag_offer->types.data;
3346 else
3347 types_data = NULL;
3348
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003349 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003350 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003351 window->user_data);
3352}
3353
3354static void
3355data_device_drop(void *data, struct wl_data_device *data_device)
3356{
3357 struct input *input = data;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003358 struct window *window = input->drag_focus;
3359 float x, y;
3360
3361 x = input->drag_x;
3362 y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003363
3364 if (window->drop_handler)
3365 window->drop_handler(window, input,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003366 x, y, window->user_data);
3367
3368 if (input->touch_grab)
3369 touch_ungrab(input);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003370}
3371
3372static void
3373data_device_selection(void *data,
3374 struct wl_data_device *wl_data_device,
3375 struct wl_data_offer *offer)
3376{
3377 struct input *input = data;
3378 char **p;
3379
3380 if (input->selection_offer)
3381 data_offer_destroy(input->selection_offer);
3382
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003383 if (offer) {
3384 input->selection_offer = wl_data_offer_get_user_data(offer);
3385 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3386 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003387 } else {
3388 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003389 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003390}
3391
3392static const struct wl_data_device_listener data_device_listener = {
3393 data_device_data_offer,
3394 data_device_enter,
3395 data_device_leave,
3396 data_device_motion,
3397 data_device_drop,
3398 data_device_selection
3399};
3400
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003401static void
3402input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003403{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003404 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003405 struct wl_cursor *cursor;
3406 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003407
Daniel Stone80972742012-11-07 17:51:39 +11003408 if (!input->pointer)
3409 return;
3410
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003411 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003412 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003413 return;
3414
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003415 if (index >= (int) cursor->image_count) {
3416 fprintf(stderr, "cursor index out of range\n");
3417 return;
3418 }
3419
3420 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003421 buffer = wl_cursor_image_get_buffer(image);
3422 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003423 return;
3424
Kristian Høgsbergae277372012-08-01 09:41:08 -04003425 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003426 input->pointer_surface,
3427 image->hotspot_x, image->hotspot_y);
3428 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3429 wl_surface_damage(input->pointer_surface, 0, 0,
3430 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003431 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003432}
3433
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003434static const struct wl_callback_listener pointer_surface_listener;
3435
3436static void
3437pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3438 uint32_t time)
3439{
3440 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003441 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003442 int i;
3443
3444 if (callback) {
3445 assert(callback == input->cursor_frame_cb);
3446 wl_callback_destroy(callback);
3447 input->cursor_frame_cb = NULL;
3448 }
3449
Daniel Stone80972742012-11-07 17:51:39 +11003450 if (!input->pointer)
3451 return;
3452
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003453 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003454 wl_pointer_set_cursor(input->pointer,
3455 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003456 NULL, 0, 0);
3457 return;
3458 }
3459
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003460 if (input->current_cursor == CURSOR_UNSET)
3461 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003462 cursor = input->display->cursors[input->current_cursor];
3463 if (!cursor)
3464 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003465
3466 /* FIXME We don't have the current time on the first call so we set
3467 * the animation start to the time of the first frame callback. */
3468 if (time == 0)
3469 input->cursor_anim_start = 0;
3470 else if (input->cursor_anim_start == 0)
3471 input->cursor_anim_start = time;
3472
3473 if (time == 0 || input->cursor_anim_start == 0)
3474 i = 0;
3475 else
3476 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3477
Pekka Paalanenbc106382012-10-10 12:49:31 +03003478 if (cursor->image_count > 1) {
3479 input->cursor_frame_cb =
3480 wl_surface_frame(input->pointer_surface);
3481 wl_callback_add_listener(input->cursor_frame_cb,
3482 &pointer_surface_listener, input);
3483 }
3484
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003485 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003486}
3487
3488static const struct wl_callback_listener pointer_surface_listener = {
3489 pointer_surface_frame_callback
3490};
3491
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003492void
3493input_set_pointer_image(struct input *input, int pointer)
3494{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003495 int force = 0;
3496
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003497 if (!input->pointer)
3498 return;
3499
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003500 if (input->pointer_enter_serial > input->cursor_serial)
3501 force = 1;
3502
3503 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003504 return;
3505
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003506 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003507 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003508 if (!input->cursor_frame_cb)
3509 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003510 else if (force) {
3511 /* The current frame callback may be stuck if, for instance,
3512 * the set cursor request was processed by the server after
3513 * this client lost the focus. In this case the cursor surface
3514 * might not be mapped and the frame callback wouldn't ever
3515 * complete. Send a set_cursor and attach to try to map the
3516 * cursor surface again so that the callback will finish */
3517 input_set_pointer_image_index(input, 0);
3518 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003519}
3520
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003521struct wl_data_device *
3522input_get_data_device(struct input *input)
3523{
3524 return input->data_device;
3525}
3526
3527void
3528input_set_selection(struct input *input,
3529 struct wl_data_source *source, uint32_t time)
3530{
3531 wl_data_device_set_selection(input->data_device, source, time);
3532}
3533
3534void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003535input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003536{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003537 wl_data_offer_accept(input->drag_offer->offer,
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003538 input->drag_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003539}
3540
3541static void
3542offer_io_func(struct task *task, uint32_t events)
3543{
3544 struct data_offer *offer =
3545 container_of(task, struct data_offer, io_task);
3546 unsigned int len;
3547 char buffer[4096];
3548
3549 len = read(offer->fd, buffer, sizeof buffer);
3550 offer->func(buffer, len,
3551 offer->x, offer->y, offer->user_data);
3552
3553 if (len == 0) {
3554 close(offer->fd);
3555 data_offer_destroy(offer);
3556 }
3557}
3558
3559static void
3560data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3561 data_func_t func, void *user_data)
3562{
3563 int p[2];
3564
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003565 if (pipe2(p, O_CLOEXEC) == -1)
3566 return;
3567
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003568 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3569 close(p[1]);
3570
3571 offer->io_task.run = offer_io_func;
3572 offer->fd = p[0];
3573 offer->func = func;
3574 offer->refcount++;
3575 offer->user_data = user_data;
3576
3577 display_watch_fd(offer->input->display,
3578 offer->fd, EPOLLIN, &offer->io_task);
3579}
3580
3581void
3582input_receive_drag_data(struct input *input, const char *mime_type,
3583 data_func_t func, void *data)
3584{
3585 data_offer_receive_data(input->drag_offer, mime_type, func, data);
Xiong Zhangbf3c1c62013-11-25 18:42:51 +08003586 input->drag_offer->x = input->drag_x;
3587 input->drag_offer->y = input->drag_y;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003588}
3589
3590int
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07003591input_receive_drag_data_to_fd(struct input *input,
3592 const char *mime_type, int fd)
3593{
3594 if (input->drag_offer)
3595 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3596
3597 return 0;
3598}
3599
3600int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003601input_receive_selection_data(struct input *input, const char *mime_type,
3602 data_func_t func, void *data)
3603{
3604 char **p;
3605
3606 if (input->selection_offer == NULL)
3607 return -1;
3608
3609 for (p = input->selection_offer->types.data; *p; p++)
3610 if (strcmp(mime_type, *p) == 0)
3611 break;
3612
3613 if (*p == NULL)
3614 return -1;
3615
3616 data_offer_receive_data(input->selection_offer,
3617 mime_type, func, data);
3618 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003619}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003620
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003621int
3622input_receive_selection_data_to_fd(struct input *input,
3623 const char *mime_type, int fd)
3624{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003625 if (input->selection_offer)
3626 wl_data_offer_receive(input->selection_offer->offer,
3627 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003628
3629 return 0;
3630}
3631
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003632void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003633window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003634{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003635 if (!window->shell_surface)
3636 return;
3637
Daniel Stone37816df2012-05-16 18:45:18 +01003638 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003639}
3640
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003641static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003642surface_set_synchronized(struct surface *surface)
3643{
3644 if (!surface->subsurface)
3645 return;
3646
3647 if (surface->synchronized)
3648 return;
3649
3650 wl_subsurface_set_sync(surface->subsurface);
3651 surface->synchronized = 1;
3652}
3653
3654static void
3655surface_set_synchronized_default(struct surface *surface)
3656{
3657 if (!surface->subsurface)
3658 return;
3659
3660 if (surface->synchronized == surface->synchronized_default)
3661 return;
3662
3663 if (surface->synchronized_default)
3664 wl_subsurface_set_sync(surface->subsurface);
3665 else
3666 wl_subsurface_set_desync(surface->subsurface);
3667
3668 surface->synchronized = surface->synchronized_default;
3669}
3670
3671static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003672surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003673{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003674 struct widget *widget = surface->widget;
3675 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003676
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003677 if (surface->input_region) {
3678 wl_region_destroy(surface->input_region);
3679 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003680 }
3681
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003682 if (surface->opaque_region)
3683 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003684
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003685 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003686
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003687 if (widget->resize_handler)
3688 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003689 widget->allocation.width,
3690 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003691 widget->user_data);
3692
Pekka Paalanen35e82632013-04-25 13:57:48 +03003693 if (surface->subsurface &&
3694 (surface->allocation.x != widget->allocation.x ||
3695 surface->allocation.y != widget->allocation.y)) {
3696 wl_subsurface_set_position(surface->subsurface,
3697 widget->allocation.x,
3698 widget->allocation.y);
3699 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003700 if (surface->allocation.width != widget->allocation.width ||
3701 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003702 window_schedule_redraw(widget->window);
3703 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003704 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003705
3706 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003707 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003708 widget->allocation.width,
3709 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003710}
3711
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003712static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003713hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3714{
3715 /*
3716 * This hack should be removed, when EGL respects
3717 * eglSwapInterval(0).
3718 *
3719 * If this window has sub-surfaces, especially a free-running
3720 * EGL-widget, we need to post the parent surface once with
3721 * all the old state to guarantee, that the EGL-widget will
3722 * receive its frame callback soon. Otherwise, a forced call
3723 * to eglSwapBuffers may end up blocking, waiting for a frame
3724 * event that will never come, because we will commit the parent
3725 * surface with all new state only after eglSwapBuffers returns.
3726 *
3727 * This assumes, that:
3728 * 1. When the EGL widget's resize hook is called, it pauses.
3729 * 2. When the EGL widget's redraw hook is called, it forces a
3730 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3731 * In a single threaded application condition 1 is a no-op.
3732 *
3733 * XXX: This should actually be after the surface_resize() calls,
3734 * but cannot, because then it would commit the incomplete state
3735 * accumulated from the widget resize hooks.
3736 */
3737 if (window->subsurface_list.next != &window->main_surface->link ||
3738 window->subsurface_list.prev != &window->main_surface->link)
3739 wl_surface_commit(window->main_surface->surface);
3740}
3741
3742static void
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003743window_do_resize(struct window *window)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003744{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003745 struct surface *surface;
3746
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003747 widget_set_allocation(window->main_surface->widget,
3748 window->pending_allocation.x,
3749 window->pending_allocation.y,
3750 window->pending_allocation.width,
3751 window->pending_allocation.height);
3752
3753 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003754
3755 /* The main surface is in the list, too. Main surface's
3756 * resize_handler is responsible for calling widget_set_allocation()
3757 * on all sub-surface root widgets, so they will be resized
3758 * properly.
3759 */
3760 wl_list_for_each(surface, &window->subsurface_list, link) {
3761 if (surface == window->main_surface)
3762 continue;
3763
3764 surface_set_synchronized(surface);
3765 surface_resize(surface);
3766 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003767}
3768
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003769static void
3770idle_resize(struct window *window)
3771{
3772 window->resize_needed = 0;
3773 window->redraw_needed = 1;
3774
3775 DBG("from %dx%d to %dx%d\n",
3776 window->main_surface->server_allocation.width,
3777 window->main_surface->server_allocation.height,
3778 window->pending_allocation.width,
3779 window->pending_allocation.height);
3780
3781 hack_prevent_EGL_sub_surface_deadlock(window);
3782
3783 window_do_resize(window);
3784}
3785
3786static void
3787undo_resize(struct window *window)
3788{
3789 window->pending_allocation.width =
3790 window->main_surface->server_allocation.width;
3791 window->pending_allocation.height =
3792 window->main_surface->server_allocation.height;
3793
3794 DBG("back to %dx%d\n",
3795 window->main_surface->server_allocation.width,
3796 window->main_surface->server_allocation.height);
3797
3798 window_do_resize(window);
3799
3800 if (window->pending_allocation.width == 0 &&
3801 window->pending_allocation.height == 0) {
3802 fprintf(stderr, "Error: Could not draw a surface, "
3803 "most likely due to insufficient disk space in "
3804 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3805 exit(EXIT_FAILURE);
3806 }
3807}
3808
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003809void
3810window_schedule_resize(struct window *window, int width, int height)
3811{
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003812 /* We should probably get these numbers from the theme. */
3813 const int min_width = 200, min_height = 200;
3814
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003815 window->pending_allocation.x = 0;
3816 window->pending_allocation.y = 0;
3817 window->pending_allocation.width = width;
3818 window->pending_allocation.height = height;
3819
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003820 if (window->min_allocation.width == 0) {
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003821 if (width < min_width && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003822 window->min_allocation.width = min_width;
3823 else
3824 window->min_allocation.width = width;
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003825 if (height < min_height && window->frame)
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003826 window->min_allocation.height = min_height;
3827 else
Kristian Høgsbergafb98282013-10-21 15:23:17 -07003828 window->min_allocation.height = height;
Kristian Høgsberg53ec5602013-10-21 15:05:49 -07003829 }
3830
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003831 if (window->pending_allocation.width < window->min_allocation.width)
3832 window->pending_allocation.width = window->min_allocation.width;
3833 if (window->pending_allocation.height < window->min_allocation.height)
3834 window->pending_allocation.height = window->min_allocation.height;
3835
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003836 window->resize_needed = 1;
3837 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003838}
3839
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003840void
3841widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3842{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003843 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003844}
3845
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003846static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003847handle_ping(void *data, struct wl_shell_surface *shell_surface,
3848 uint32_t serial)
3849{
3850 wl_shell_surface_pong(shell_surface, serial);
3851}
3852
3853static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003854handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003855 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003856{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003857 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003858
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003859 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003860 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003861}
3862
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003863static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003864menu_destroy(struct menu *menu)
3865{
3866 widget_destroy(menu->widget);
3867 window_destroy(menu->window);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07003868 frame_destroy(menu->frame);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003869 free(menu);
3870}
3871
3872static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003873handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3874{
3875 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003876 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003877
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003878 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003879 * device. Or just use wl_callback. And this really needs to
3880 * be a window vfunc that the menu can set. And we need the
3881 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003882
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003883 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003884 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003885}
3886
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003887static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003888 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003889 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003890 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003891};
3892
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003893void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003894window_get_allocation(struct window *window,
3895 struct rectangle *allocation)
3896{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003897 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003898}
3899
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003900static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003901widget_redraw(struct widget *widget)
3902{
3903 struct widget *child;
3904
3905 if (widget->redraw_handler)
3906 widget->redraw_handler(widget, widget->user_data);
3907 wl_list_for_each(child, &widget->child_list, link)
3908 widget_redraw(child);
3909}
3910
3911static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003912frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3913{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003914 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003915
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003916 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003917 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003918 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003919 surface->frame_cb = NULL;
3920
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003921 surface->last_time = time;
3922
Pekka Paalanen71233882013-04-25 13:57:53 +03003923 if (surface->redraw_needed || surface->window->redraw_needed) {
3924 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003925 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003926 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003927}
3928
3929static const struct wl_callback_listener listener = {
3930 frame_callback
3931};
3932
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003933static int
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003934surface_redraw(struct surface *surface)
3935{
Pekka Paalanen71233882013-04-25 13:57:53 +03003936 DBG_OBJ(surface->surface, "begin\n");
3937
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003938 if (!surface->window->redraw_needed && !surface->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003939 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003940
3941 /* Whole-window redraw forces a redraw even if the previous has
3942 * not yet hit the screen.
3943 */
3944 if (surface->frame_cb) {
3945 if (!surface->window->redraw_needed)
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003946 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003947
Pekka Paalanen71233882013-04-25 13:57:53 +03003948 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003949 wl_callback_destroy(surface->frame_cb);
3950 }
3951
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003952 if (!widget_get_cairo_surface(surface->widget)) {
3953 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3954 return -1;
3955 }
3956
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003957 surface->frame_cb = wl_surface_frame(surface->surface);
3958 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003959 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003960
3961 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003962 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003963 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003964 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003965 return 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003966}
3967
3968static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003969idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003970{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003971 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003972 struct surface *surface;
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003973 int failed = 0;
3974 int resized = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003975
Pekka Paalanen71233882013-04-25 13:57:53 +03003976 DBG(" --------- \n");
3977
Pekka Paalaneneebff542013-04-25 13:57:52 +03003978 wl_list_init(&window->redraw_task.link);
3979 window->redraw_task_scheduled = 0;
3980
3981 if (window->resize_needed) {
3982 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003983 if (window->main_surface->frame_cb) {
3984 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003985 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003986 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003987
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003988 idle_resize(window);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003989 resized = 1;
Pekka Paalaneneebff542013-04-25 13:57:52 +03003990 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003991
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02003992 if (surface_redraw(window->main_surface) < 0) {
3993 /*
3994 * Only main_surface failure will cause us to undo the resize.
3995 * If sub-surfaces fail, they will just be broken with old
3996 * content.
3997 */
3998 failed = 1;
3999 } else {
4000 wl_list_for_each(surface, &window->subsurface_list, link) {
4001 if (surface == window->main_surface)
4002 continue;
4003
4004 surface_redraw(surface);
4005 }
4006 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03004007
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04004008 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03004009 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004010
4011 wl_list_for_each(surface, &window->subsurface_list, link)
4012 surface_set_synchronized_default(surface);
Pekka Paalanenfdca95c2013-11-29 17:48:52 +02004013
4014 if (resized && failed) {
4015 /* Restore widget tree to correspond to what is on screen. */
4016 undo_resize(window);
4017 }
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004018}
4019
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004020static void
4021window_schedule_redraw_task(struct window *window)
4022{
4023 if (window->configure_requests)
4024 return;
4025 if (!window->redraw_task_scheduled) {
4026 window->redraw_task.run = idle_redraw;
4027 display_defer(window->display, &window->redraw_task);
4028 window->redraw_task_scheduled = 1;
4029 }
4030}
4031
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004032void
4033window_schedule_redraw(struct window *window)
4034{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004035 struct surface *surface;
4036
Pekka Paalanen71233882013-04-25 13:57:53 +03004037 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4038
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004039 wl_list_for_each(surface, &window->subsurface_list, link)
4040 surface->redraw_needed = 1;
4041
4042 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04004043}
4044
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004045int
4046window_is_fullscreen(struct window *window)
4047{
4048 return window->type == TYPE_FULLSCREEN;
4049}
4050
Philip Withnallc971d2a2013-11-25 18:01:33 +00004051int
4052window_is_transient(struct window *window)
4053{
4054 return window->type == TYPE_TRANSIENT;
4055}
4056
MoD063a8822013-03-02 23:43:57 +00004057static void
4058configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4059{
4060 struct window *window = data;
4061
4062 wl_callback_destroy(callback);
4063 window->configure_requests--;
4064
4065 if (!window->configure_requests)
4066 window_schedule_redraw(window);
4067}
4068
4069static struct wl_callback_listener configure_request_listener = {
4070 configure_request_completed,
4071};
4072
4073static void
4074window_defer_redraw_until_configure(struct window* window)
4075{
4076 struct wl_callback *callback;
4077
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004078 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00004079 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03004080 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00004081 }
4082
4083 callback = wl_display_sync(window->display->display);
4084 wl_callback_add_listener(callback, &configure_request_listener, window);
4085 window->configure_requests++;
4086}
4087
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05004088void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004089window_set_fullscreen(struct window *window, int fullscreen)
4090{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004091 if (!window->display->shell)
4092 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004093
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04004094 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05004095 return;
4096
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004097 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004098 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01004099 if (window->type == TYPE_TOPLEVEL) {
4100 window->saved_allocation = window->main_surface->allocation;
4101 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004102 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004103 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004104 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004105 0, NULL);
MoD063a8822013-03-02 23:43:57 +00004106 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004107 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004108 if (window->saved_type == TYPE_MAXIMIZED) {
4109 window_set_maximized(window, 1);
4110 } else {
4111 window->type = TYPE_TOPLEVEL;
4112 wl_shell_surface_set_toplevel(window->shell_surface);
4113 window_schedule_resize(window,
4114 window->saved_allocation.width,
4115 window->saved_allocation.height);
4116 }
4117
Kristian Høgsberg0395f302008-12-22 12:14:50 -05004118 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004119}
4120
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004121void
4122window_set_fullscreen_method(struct window *window,
4123 enum wl_shell_surface_fullscreen_method method)
4124{
4125 window->fullscreen_method = method;
4126}
4127
Kristian Høgsberg1671e112012-10-10 11:36:24 -04004128int
4129window_is_maximized(struct window *window)
4130{
4131 return window->type == TYPE_MAXIMIZED;
4132}
4133
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004134void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004135window_set_maximized(struct window *window, int maximized)
4136{
4137 if (!window->display->shell)
4138 return;
4139
4140 if ((window->type == TYPE_MAXIMIZED) == maximized)
4141 return;
4142
4143 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004144 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004145 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4146 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004147 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01004148 } else if (window->type == TYPE_FULLSCREEN) {
4149 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4150 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004151 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004152 } else {
4153 wl_shell_surface_set_toplevel(window->shell_surface);
4154 window->type = TYPE_TOPLEVEL;
4155 window_schedule_resize(window,
4156 window->saved_allocation.width,
4157 window->saved_allocation.height);
4158 }
4159}
4160
4161void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004162window_set_user_data(struct window *window, void *data)
4163{
4164 window->user_data = data;
4165}
4166
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004167void *
4168window_get_user_data(struct window *window)
4169{
4170 return window->user_data;
4171}
4172
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004173void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004174window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004175 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004176{
4177 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004178}
4179
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004180void
4181window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004182 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004183{
4184 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004185}
4186
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004187void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004188window_set_data_handler(struct window *window, window_data_handler_t handler)
4189{
4190 window->data_handler = handler;
4191}
4192
4193void
4194window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4195{
4196 window->drop_handler = handler;
4197}
4198
4199void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004200window_set_close_handler(struct window *window,
4201 window_close_handler_t handler)
4202{
4203 window->close_handler = handler;
4204}
4205
4206void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004207window_set_fullscreen_handler(struct window *window,
4208 window_fullscreen_handler_t handler)
4209{
4210 window->fullscreen_handler = handler;
4211}
4212
4213void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004214window_set_output_handler(struct window *window,
4215 window_output_handler_t handler)
4216{
4217 window->output_handler = handler;
4218}
4219
4220void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004221window_set_title(struct window *window, const char *title)
4222{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004223 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004224 window->title = strdup(title);
Jason Ekstrand3f66cf92013-10-13 19:08:40 -05004225 if (window->frame) {
4226 frame_set_title(window->frame->frame, title);
4227 widget_schedule_redraw(window->frame->widget);
4228 }
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004229 if (window->shell_surface)
4230 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004231}
4232
4233const char *
4234window_get_title(struct window *window)
4235{
4236 return window->title;
4237}
4238
4239void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004240window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4241{
4242 struct text_cursor_position *text_cursor_position =
4243 window->display->text_cursor_position;
4244
Scott Moreau9295ce02012-06-01 12:46:10 -06004245 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004246 return;
4247
4248 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004249 window->main_surface->surface,
4250 wl_fixed_from_int(x),
4251 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004252}
4253
4254void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004255window_damage(struct window *window, int32_t x, int32_t y,
4256 int32_t width, int32_t height)
4257{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004258 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004259}
4260
Casey Dahlin9074db52012-04-19 22:50:09 -04004261static void
4262surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004263 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004264{
Rob Bradford7507b572012-05-15 17:55:34 +01004265 struct window *window = data;
4266 struct output *output;
4267 struct output *output_found = NULL;
4268 struct window_output *window_output;
4269
4270 wl_list_for_each(output, &window->display->output_list, link) {
4271 if (output->output == wl_output) {
4272 output_found = output;
4273 break;
4274 }
4275 }
4276
4277 if (!output_found)
4278 return;
4279
Brian Lovinbc919262013-08-07 15:34:59 -07004280 window_output = xmalloc(sizeof *window_output);
Rob Bradford7507b572012-05-15 17:55:34 +01004281 window_output->output = output_found;
4282
4283 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004284
4285 if (window->output_handler)
4286 window->output_handler(window, output_found, 1,
4287 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004288}
4289
4290static void
4291surface_leave(void *data,
4292 struct wl_surface *wl_surface, struct wl_output *output)
4293{
Rob Bradford7507b572012-05-15 17:55:34 +01004294 struct window *window = data;
4295 struct window_output *window_output;
4296 struct window_output *window_output_found = NULL;
4297
4298 wl_list_for_each(window_output, &window->window_output_list, link) {
4299 if (window_output->output->output == output) {
4300 window_output_found = window_output;
4301 break;
4302 }
4303 }
4304
4305 if (window_output_found) {
4306 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004307
4308 if (window->output_handler)
4309 window->output_handler(window, window_output->output,
4310 0, window->user_data);
4311
Rob Bradford7507b572012-05-15 17:55:34 +01004312 free(window_output_found);
4313 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004314}
4315
4316static const struct wl_surface_listener surface_listener = {
4317 surface_enter,
4318 surface_leave
4319};
4320
Pekka Paalanen4e373742013-02-13 16:17:13 +02004321static struct surface *
4322surface_create(struct window *window)
4323{
4324 struct display *display = window->display;
4325 struct surface *surface;
4326
Brian Lovinbc919262013-08-07 15:34:59 -07004327 surface = xmalloc(sizeof *surface);
4328 memset(surface, 0, sizeof *surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02004329 if (!surface)
4330 return NULL;
4331
4332 surface->window = window;
4333 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004334 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004335 wl_surface_add_listener(surface->surface, &surface_listener, window);
4336
Pekka Paalanen35e82632013-04-25 13:57:48 +03004337 wl_list_insert(&window->subsurface_list, &surface->link);
4338
Pekka Paalanen4e373742013-02-13 16:17:13 +02004339 return surface;
4340}
4341
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004342static struct window *
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004343window_create_internal(struct display *display, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004344{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004345 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004346 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004347
Peter Huttererf3d62272013-08-08 11:57:05 +10004348 window = xzalloc(sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004349 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004350 window->display = display;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004351
4352 surface = surface_create(window);
4353 window->main_surface = surface;
4354
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004355 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004356 window->shell_surface =
4357 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004358 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004359 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004360 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004361
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004362 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004363 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004364 window->configure_requests = 0;
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004365 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004366
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004367 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004368#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004369 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004370#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004371 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004372#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004373 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004374 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004375
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004376 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004377 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004378 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004379
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004380 if (window->shell_surface) {
4381 wl_shell_surface_set_user_data(window->shell_surface, window);
4382 wl_shell_surface_add_listener(window->shell_surface,
4383 &shell_surface_listener, window);
4384 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004385
Rob Bradford7507b572012-05-15 17:55:34 +01004386 wl_list_init (&window->window_output_list);
4387
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004388 return window;
4389}
4390
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004391struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004392window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004393{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004394 return window_create_internal(display, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004395}
4396
4397struct window *
4398window_create_custom(struct display *display)
4399{
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004400 return window_create_internal(display, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004401}
4402
4403struct window *
4404window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004405 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004406{
4407 struct window *window;
4408
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004409 window = window_create_internal(parent->display, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004410
4411 window->x = x;
4412 window->y = y;
4413
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004414 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004415 wl_shell_surface_set_transient(
4416 window->shell_surface,
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004417 parent->main_surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004418 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004419
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004420 return window;
4421}
4422
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004423static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004424menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004425{
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004426 int32_t x, y, width, height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004427 int next;
4428
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004429 frame_interior(menu->frame, &x, &y, &width, &height);
4430 next = (sy - y) / 20;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004431 if (menu->current != next) {
4432 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004433 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004434 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004435}
4436
4437static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004438menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004439 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004440 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004441{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004442 struct menu *menu = data;
4443
4444 if (widget == menu->widget)
4445 menu_set_item(data, y);
4446
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004447 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004448}
4449
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004450static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004451menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004452 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004453{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004454 struct menu *menu = data;
4455
4456 if (widget == menu->widget)
4457 menu_set_item(data, y);
4458
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004459 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004460}
4461
4462static void
4463menu_leave_handler(struct widget *widget, struct input *input, void *data)
4464{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004465 struct menu *menu = data;
4466
4467 if (widget == menu->widget)
4468 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004469}
4470
4471static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004472menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004473 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004474 uint32_t button, enum wl_pointer_button_state state,
4475 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004476
4477{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004478 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004479
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004480 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4481 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004482 /* Either relase after press-drag-release or
4483 * click-motion-click. */
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004484 menu->func(menu->parent, input,
4485 menu->current, menu->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004486 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004487 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004488 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004489 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004490 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004491}
4492
4493static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004494menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004495{
4496 cairo_t *cr;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004497 struct menu *menu = data;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004498 int32_t x, y, width, height, i;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004499
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004500 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004501
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004502 frame_repaint(menu->frame, cr);
4503 frame_interior(menu->frame, &x, &y, &width, &height);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004504
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004505 theme_set_background_source(menu->window->display->theme,
4506 cr, THEME_FRAME_ACTIVE);
4507 cairo_rectangle(cr, x, y, width, height);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004508 cairo_fill(cr);
4509
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004510 cairo_select_font_face(cr, "sans",
4511 CAIRO_FONT_SLANT_NORMAL,
4512 CAIRO_FONT_WEIGHT_NORMAL);
4513 cairo_set_font_size(cr, 12);
4514
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004515 for (i = 0; i < menu->count; i++) {
4516 if (i == menu->current) {
4517 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004518 cairo_rectangle(cr, x, y + i * 20, width, 20);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004519 cairo_fill(cr);
4520 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004521 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004522 cairo_show_text(cr, menu->entries[i]);
4523 } else {
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004524 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4525 cairo_move_to(cr, x + 10, y + i * 20 + 16);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004526 cairo_show_text(cr, menu->entries[i]);
4527 }
4528 }
4529
4530 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004531}
4532
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004533void
4534window_show_menu(struct display *display,
4535 struct input *input, uint32_t time, struct window *parent,
4536 int32_t x, int32_t y,
4537 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004538{
4539 struct window *window;
4540 struct menu *menu;
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004541 int32_t ix, iy;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004542
4543 menu = malloc(sizeof *menu);
4544 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004545 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004546
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004547 window = window_create_internal(parent->display, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004548 if (!window) {
4549 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004550 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004551 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004552
4553 menu->window = window;
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004554 menu->parent = parent;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004555 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004556 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4557 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004558 menu->frame = frame_create(window->display->theme, 0, 0,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -07004559 FRAME_BUTTON_NONE, NULL);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004560 menu->entries = entries;
4561 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004562 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004563 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004564 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004565 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004566 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004567 window->type = TYPE_MENU;
4568 window->x = x;
4569 window->y = y;
4570
Kristian Høgsberg8ae63852013-10-28 22:06:11 -07004571 input_ungrab(input);
4572
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004573 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004574 widget_set_enter_handler(menu->widget, menu_enter_handler);
4575 widget_set_leave_handler(menu->widget, menu_leave_handler);
4576 widget_set_motion_handler(menu->widget, menu_motion_handler);
4577 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004578
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004579 input_grab(input, menu->widget, 0);
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004580 frame_resize_inside(menu->frame, 200, count * 20);
4581 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4582 window_schedule_resize(window, frame_width(menu->frame),
4583 frame_height(menu->frame));
4584
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004585 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4586 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4587 display_get_serial(window->display),
Jasper St. Pierrebf175902013-11-12 20:19:58 -05004588 parent->main_surface->surface,
Kristian Høgsbergc680e902013-10-23 21:49:30 -07004589 window->x - ix, window->y - iy, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004590}
4591
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004592void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004593window_set_buffer_type(struct window *window, enum window_buffer_type type)
4594{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004595 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004596}
4597
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004598void
4599window_set_preferred_format(struct window *window,
4600 enum preferred_format format)
4601{
4602 window->preferred_format = format;
4603}
4604
Pekka Paalanen35e82632013-04-25 13:57:48 +03004605struct widget *
4606window_add_subsurface(struct window *window, void *data,
4607 enum subsurface_mode default_mode)
4608{
4609 struct widget *widget;
4610 struct surface *surface;
4611 struct wl_surface *parent;
4612 struct wl_subcompositor *subcompo = window->display->subcompositor;
4613
Pekka Paalanen35e82632013-04-25 13:57:48 +03004614 surface = surface_create(window);
4615 widget = widget_create(window, surface, data);
4616 wl_list_init(&widget->link);
4617 surface->widget = widget;
4618
4619 parent = window->main_surface->surface;
4620 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4621 surface->surface,
4622 parent);
4623 surface->synchronized = 1;
4624
4625 switch (default_mode) {
4626 case SUBSURFACE_SYNCHRONIZED:
4627 surface->synchronized_default = 1;
4628 break;
4629 case SUBSURFACE_DESYNCHRONIZED:
4630 surface->synchronized_default = 0;
4631 break;
4632 default:
4633 assert(!"bad enum subsurface_mode");
4634 }
4635
4636 return widget;
4637}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004638
4639static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004640display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004641 struct wl_output *wl_output,
4642 int x, int y,
4643 int physical_width,
4644 int physical_height,
4645 int subpixel,
4646 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004647 const char *model,
4648 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004649{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004650 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004651
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004652 output->allocation.x = x;
4653 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004654 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004655}
4656
4657static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004658display_handle_done(void *data,
4659 struct wl_output *wl_output)
4660{
4661}
4662
4663static void
4664display_handle_scale(void *data,
4665 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004666 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004667{
4668 struct output *output = data;
4669
4670 output->scale = scale;
4671}
4672
4673static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004674display_handle_mode(void *data,
4675 struct wl_output *wl_output,
4676 uint32_t flags,
4677 int width,
4678 int height,
4679 int refresh)
4680{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004681 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004682 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004683
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004684 if (flags & WL_OUTPUT_MODE_CURRENT) {
4685 output->allocation.width = width;
4686 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004687 if (display->output_configure_handler)
4688 (*display->output_configure_handler)(
4689 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004690 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004691}
4692
4693static const struct wl_output_listener output_listener = {
4694 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004695 display_handle_mode,
4696 display_handle_done,
4697 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004698};
4699
4700static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004701display_add_output(struct display *d, uint32_t id)
4702{
4703 struct output *output;
4704
Kristian Høgsberg69594cc2013-08-15 14:28:25 -07004705 output = xzalloc(sizeof *output);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004706 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004707 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004708 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004709 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004710 output->server_output_id = id;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004711 wl_list_insert(d->output_list.prev, &output->link);
4712
4713 wl_output_add_listener(output->output, &output_listener, output);
4714}
4715
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004716static void
4717output_destroy(struct output *output)
4718{
4719 if (output->destroy_handler)
4720 (*output->destroy_handler)(output, output->user_data);
4721
4722 wl_output_destroy(output->output);
4723 wl_list_remove(&output->link);
4724 free(output);
4725}
4726
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004727static void
4728display_destroy_output(struct display *d, uint32_t id)
4729{
4730 struct output *output;
4731
4732 wl_list_for_each(output, &d->output_list, link) {
4733 if (output->server_output_id == id) {
4734 output_destroy(output);
4735 break;
4736 }
4737 }
4738}
4739
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004740void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004741display_set_global_handler(struct display *display,
4742 display_global_handler_t handler)
4743{
4744 struct global *global;
4745
4746 display->global_handler = handler;
4747 if (!handler)
4748 return;
4749
4750 wl_list_for_each(global, &display->global_list, link)
4751 display->global_handler(display,
4752 global->name, global->interface,
4753 global->version, display->user_data);
4754}
4755
4756void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004757display_set_global_handler_remove(struct display *display,
4758 display_global_handler_t remove_handler)
4759{
4760 display->global_handler_remove = remove_handler;
4761 if (!remove_handler)
4762 return;
4763}
4764
4765void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004766display_set_output_configure_handler(struct display *display,
4767 display_output_handler_t handler)
4768{
4769 struct output *output;
4770
4771 display->output_configure_handler = handler;
4772 if (!handler)
4773 return;
4774
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004775 wl_list_for_each(output, &display->output_list, link) {
4776 if (output->allocation.width == 0 &&
4777 output->allocation.height == 0)
4778 continue;
4779
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004780 (*display->output_configure_handler)(output,
4781 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004782 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004783}
4784
4785void
4786output_set_user_data(struct output *output, void *data)
4787{
4788 output->user_data = data;
4789}
4790
4791void *
4792output_get_user_data(struct output *output)
4793{
4794 return output->user_data;
4795}
4796
4797void
4798output_set_destroy_handler(struct output *output,
4799 display_output_handler_t handler)
4800{
4801 output->destroy_handler = handler;
4802 /* FIXME: implement this, once we have way to remove outputs */
4803}
4804
4805void
Scott Moreau4e072362012-09-29 02:03:11 -06004806output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004807{
Scott Moreau4e072362012-09-29 02:03:11 -06004808 struct rectangle allocation = output->allocation;
4809
4810 switch (output->transform) {
4811 case WL_OUTPUT_TRANSFORM_90:
4812 case WL_OUTPUT_TRANSFORM_270:
4813 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4814 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4815 /* Swap width and height */
4816 allocation.width = output->allocation.height;
4817 allocation.height = output->allocation.width;
4818 break;
4819 }
4820
4821 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004822}
4823
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004824struct wl_output *
4825output_get_wl_output(struct output *output)
4826{
4827 return output->output;
4828}
4829
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004830enum wl_output_transform
4831output_get_transform(struct output *output)
4832{
4833 return output->transform;
4834}
4835
Alexander Larssonafd319a2013-05-22 14:41:27 +02004836uint32_t
4837output_get_scale(struct output *output)
4838{
4839 return output->scale;
4840}
4841
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004842static void
Daniel Stone97f68542012-05-30 16:32:01 +01004843fini_xkb(struct input *input)
4844{
4845 xkb_state_unref(input->xkb.state);
4846 xkb_map_unref(input->xkb.keymap);
4847}
4848
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004849#define MIN(a,b) ((a) < (b) ? a : b)
Rob Bradford08031182013-08-13 20:11:03 +01004850
Daniel Stone97f68542012-05-30 16:32:01 +01004851static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004852display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004853{
4854 struct input *input;
4855
Kristian Høgsbergadcd54b2013-08-15 14:17:13 -07004856 input = xzalloc(sizeof *input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004857 input->display = d;
Rob Bradford08031182013-08-13 20:11:03 +01004858 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
Jasper St. Pierre47f10432013-11-12 14:37:20 -05004859 MIN(d->seat_version, 3));
Rusty Lynch1084da52013-08-15 09:10:08 -07004860 input->touch_focus = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004861 input->pointer_focus = NULL;
4862 input->keyboard_focus = NULL;
Rusty Lynch041815a2013-08-08 21:20:38 -07004863 wl_list_init(&input->touch_point_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004864 wl_list_insert(d->input_list.prev, &input->link);
4865
Daniel Stone37816df2012-05-16 18:45:18 +01004866 wl_seat_add_listener(input->seat, &seat_listener, input);
4867 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004868
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004869 input->data_device =
4870 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004871 input->seat);
4872 wl_data_device_add_listener(input->data_device, &data_device_listener,
4873 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004874
4875 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004876
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004877 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4878 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004879 input->repeat_task.run = keyboard_repeat_func;
4880 display_watch_fd(d, input->repeat_timer_fd,
4881 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004882}
4883
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004884static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004885input_destroy(struct input *input)
4886{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004887 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004888 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004889
4890 if (input->drag_offer)
4891 data_offer_destroy(input->drag_offer);
4892
4893 if (input->selection_offer)
4894 data_offer_destroy(input->selection_offer);
4895
4896 wl_data_device_destroy(input->data_device);
Rob Bradford08031182013-08-13 20:11:03 +01004897
4898 if (input->display->seat_version >= 3) {
4899 if (input->pointer)
4900 wl_pointer_release(input->pointer);
4901 if (input->keyboard)
4902 wl_keyboard_release(input->keyboard);
4903 }
4904
Daniel Stone97f68542012-05-30 16:32:01 +01004905 fini_xkb(input);
4906
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004907 wl_surface_destroy(input->pointer_surface);
4908
Pekka Paalanene1207c72011-12-16 12:02:09 +02004909 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004910 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004911 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004912 free(input);
4913}
4914
4915static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004916init_workspace_manager(struct display *d, uint32_t id)
4917{
4918 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004919 wl_registry_bind(d->registry, id,
4920 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004921 if (d->workspace_manager != NULL)
4922 workspace_manager_add_listener(d->workspace_manager,
4923 &workspace_manager_listener,
4924 d);
4925}
4926
4927static void
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004928shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4929{
4930 struct display *d = data;
4931
4932 if (format == WL_SHM_FORMAT_RGB565)
4933 d->has_rgb565 = 1;
4934}
4935
4936struct wl_shm_listener shm_listener = {
4937 shm_format
4938};
4939
4940static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004941registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4942 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004943{
4944 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004945 struct global *global;
4946
Brian Lovinbc919262013-08-07 15:34:59 -07004947 global = xmalloc(sizeof *global);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004948 global->name = id;
4949 global->interface = strdup(interface);
4950 global->version = version;
4951 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004952
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004953 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004954 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004955 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004956 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004957 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004958 } else if (strcmp(interface, "wl_seat") == 0) {
Rob Bradford08031182013-08-13 20:11:03 +01004959 d->seat_version = version;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004960 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004961 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004962 d->shell = wl_registry_bind(registry,
4963 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004964 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004965 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02004966 wl_shm_add_listener(d->shm, &shm_listener, d);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004967 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4968 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004969 wl_registry_bind(registry, id,
4970 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004971 } else if (strcmp(interface, "text_cursor_position") == 0) {
4972 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004973 wl_registry_bind(registry, id,
4974 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004975 } else if (strcmp(interface, "workspace_manager") == 0) {
4976 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004977 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4978 d->subcompositor =
4979 wl_registry_bind(registry, id,
4980 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004981 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004982
4983 if (d->global_handler)
4984 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004985}
4986
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004987static void
4988registry_handle_global_remove(void *data, struct wl_registry *registry,
4989 uint32_t name)
4990{
4991 struct display *d = data;
4992 struct global *global;
4993 struct global *tmp;
4994
4995 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4996 if (global->name != name)
4997 continue;
4998
Xiong Zhang83d8ee72013-10-23 13:58:35 +08004999 if (strcmp(global->interface, "wl_output") == 0)
5000 display_destroy_output(d, name);
5001
5002 /* XXX: Should destroy remaining bound globals */
5003
5004 if (d->global_handler_remove)
5005 d->global_handler_remove(d, name, global->interface,
5006 global->version, d->user_data);
5007
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005008 wl_list_remove(&global->link);
5009 free(global->interface);
5010 free(global);
5011 }
5012}
5013
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005014void *
5015display_bind(struct display *display, uint32_t name,
5016 const struct wl_interface *interface, uint32_t version)
5017{
5018 return wl_registry_bind(display->registry, name, interface, version);
5019}
5020
5021static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02005022 registry_handle_global,
5023 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005024};
5025
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005026#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05005027static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05005028init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05005029{
5030 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005031 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04005032
Rob Clark6396ed32012-03-11 19:48:41 -05005033#ifdef USE_CAIRO_GLESV2
5034# define GL_BIT EGL_OPENGL_ES2_BIT
5035#else
5036# define GL_BIT EGL_OPENGL_BIT
5037#endif
5038
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005039 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04005040 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005041 EGL_RED_SIZE, 1,
5042 EGL_GREEN_SIZE, 1,
5043 EGL_BLUE_SIZE, 1,
5044 EGL_ALPHA_SIZE, 1,
5045 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05005046 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005047 EGL_NONE
5048 };
Yuval Fledel45568f62010-12-06 09:18:12 -05005049
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005050#ifdef USE_CAIRO_GLESV2
5051 static const EGLint context_attribs[] = {
5052 EGL_CONTEXT_CLIENT_VERSION, 2,
5053 EGL_NONE
5054 };
5055 EGLint api = EGL_OPENGL_ES_API;
5056#else
5057 EGLint *context_attribs = NULL;
5058 EGLint api = EGL_OPENGL_API;
5059#endif
5060
Kristian Høgsberg91342c62011-04-14 14:44:58 -04005061 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05005062 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005063 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005064 return -1;
5065 }
5066
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005067 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005068 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005069 return -1;
5070 }
5071
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005072 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5073 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005074 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01005075 return -1;
5076 }
5077
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005078 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05005079 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02005080 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005081 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05005082 return -1;
5083 }
5084
Benjamin Franzke0c991632011-09-27 21:57:31 +02005085 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5086 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005087 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02005088 return -1;
5089 }
Yuval Fledel45568f62010-12-06 09:18:12 -05005090
5091 return 0;
5092}
5093
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005094static void
5095fini_egl(struct display *display)
5096{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005097 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005098
5099 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5100 EGL_NO_CONTEXT);
5101
5102 eglTerminate(display->dpy);
5103 eglReleaseThread();
5104}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005105#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005106
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005107static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005108init_dummy_surface(struct display *display)
5109{
5110 int len;
5111 void *data;
5112
5113 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5114 data = malloc(len);
5115 display->dummy_surface =
5116 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5117 1, 1, len);
5118 display->dummy_surface_data = data;
5119}
5120
5121static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005122handle_display_data(struct task *task, uint32_t events)
5123{
5124 struct display *display =
5125 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005126 struct epoll_event ep;
5127 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005128
5129 display->display_fd_events = events;
5130
5131 if (events & EPOLLERR || events & EPOLLHUP) {
5132 display_exit(display);
5133 return;
5134 }
5135
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04005136 if (events & EPOLLIN) {
5137 ret = wl_display_dispatch(display->display);
5138 if (ret == -1) {
5139 display_exit(display);
5140 return;
5141 }
5142 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005143
5144 if (events & EPOLLOUT) {
5145 ret = wl_display_flush(display->display);
5146 if (ret == 0) {
5147 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5148 ep.data.ptr = &display->display_task;
5149 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5150 display->display_fd, &ep);
5151 } else if (ret == -1 && errno != EAGAIN) {
5152 display_exit(display);
5153 return;
5154 }
5155 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005156}
5157
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005158static void
5159log_handler(const char *format, va_list args)
5160{
5161 vfprintf(stderr, format, args);
5162}
5163
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005164struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005165display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005166{
5167 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04005168
Kristian Høgsberg2e437202013-04-16 11:21:48 -04005169 wl_log_set_handler_client(log_handler);
5170
Peter Huttererf3d62272013-08-08 11:57:05 +10005171 d = zalloc(sizeof *d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005172 if (d == NULL)
5173 return NULL;
5174
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05005175 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005176 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02005177 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00005178 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005179 return NULL;
5180 }
5181
Rob Bradford5ab9c752013-07-26 16:29:43 +01005182 d->xkb_context = xkb_context_new(0);
5183 if (d->xkb_context == NULL) {
5184 fprintf(stderr, "Failed to create XKB context\n");
5185 free(d);
5186 return NULL;
5187 }
5188
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03005189 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005190 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005191 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07005192 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5193 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005194
5195 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005196 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005197 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005198 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04005199
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02005200 d->workspace = 0;
5201 d->workspace_count = 1;
5202
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005203 d->registry = wl_display_get_registry(d->display);
5204 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02005205
5206 if (wl_display_dispatch(d->display) < 0) {
5207 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5208 return NULL;
5209 }
5210
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005211#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005212 if (init_egl(d) < 0)
5213 fprintf(stderr, "EGL does not seem to work, "
5214 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005215#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005216
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005217 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005218
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005219 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005220
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005221 wl_list_init(&d->window_list);
5222
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005223 init_dummy_surface(d);
5224
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005225 return d;
5226}
5227
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005228static void
5229display_destroy_outputs(struct display *display)
5230{
5231 struct output *tmp;
5232 struct output *output;
5233
5234 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5235 output_destroy(output);
5236}
5237
Pekka Paalanene1207c72011-12-16 12:02:09 +02005238static void
5239display_destroy_inputs(struct display *display)
5240{
5241 struct input *tmp;
5242 struct input *input;
5243
5244 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5245 input_destroy(input);
5246}
5247
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005248void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005249display_destroy(struct display *display)
5250{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005251 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005252 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5253 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005254
5255 if (!wl_list_empty(&display->deferred_list))
5256 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5257
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005258 cairo_surface_destroy(display->dummy_surface);
5259 free(display->dummy_surface_data);
5260
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005261 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005262 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005263
Daniel Stone97f68542012-05-30 16:32:01 +01005264 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005265
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005266 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005267 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005268
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005269#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005270 if (display->argb_device)
5271 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005272#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005273
Pekka Paalanen35e82632013-04-25 13:57:48 +03005274 if (display->subcompositor)
5275 wl_subcompositor_destroy(display->subcompositor);
5276
Pekka Paalanenc2052982011-12-16 11:41:32 +02005277 if (display->shell)
5278 wl_shell_destroy(display->shell);
5279
5280 if (display->shm)
5281 wl_shm_destroy(display->shm);
5282
5283 if (display->data_device_manager)
5284 wl_data_device_manager_destroy(display->data_device_manager);
5285
5286 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005287 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005288
5289 close(display->epoll_fd);
5290
U. Artie Eoff44874d92012-10-02 21:12:35 -07005291 if (!(display->display_fd_events & EPOLLERR) &&
5292 !(display->display_fd_events & EPOLLHUP))
5293 wl_display_flush(display->display);
5294
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005295 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005296 free(display);
5297}
5298
5299void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005300display_set_user_data(struct display *display, void *data)
5301{
5302 display->user_data = data;
5303}
5304
5305void *
5306display_get_user_data(struct display *display)
5307{
5308 return display->user_data;
5309}
5310
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005311struct wl_display *
5312display_get_display(struct display *display)
5313{
5314 return display->display;
5315}
5316
Kristian Høgsbergb20b0092013-08-15 11:54:03 -07005317int
5318display_has_subcompositor(struct display *display)
5319{
5320 if (display->subcompositor)
5321 return 1;
5322
5323 wl_display_roundtrip(display->display);
5324
5325 return display->subcompositor != NULL;
5326}
5327
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005328cairo_device_t *
5329display_get_cairo_device(struct display *display)
5330{
5331 return display->argb_device;
5332}
5333
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005334struct output *
5335display_get_output(struct display *display)
5336{
5337 return container_of(display->output_list.next, struct output, link);
5338}
5339
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005340struct wl_compositor *
5341display_get_compositor(struct display *display)
5342{
5343 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005344}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005345
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005346uint32_t
5347display_get_serial(struct display *display)
5348{
5349 return display->serial;
5350}
5351
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005352EGLDisplay
5353display_get_egl_display(struct display *d)
5354{
5355 return d->dpy;
5356}
5357
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005358struct wl_data_source *
5359display_create_data_source(struct display *display)
5360{
5361 return wl_data_device_manager_create_data_source(display->data_device_manager);
5362}
5363
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005364EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005365display_get_argb_egl_config(struct display *d)
5366{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005367 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005368}
5369
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005370int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005371display_acquire_window_surface(struct display *display,
5372 struct window *window,
5373 EGLContext ctx)
5374{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005375 struct surface *surface = window->main_surface;
5376
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005377 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005378 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005379
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005380 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005381 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005382}
5383
5384void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005385display_release_window_surface(struct display *display,
5386 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005387{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005388 struct surface *surface = window->main_surface;
5389
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005390 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005391 return;
5392
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005393 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005394}
5395
5396void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005397display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005398{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005399 wl_list_insert(&display->deferred_list, &task->link);
5400}
5401
5402void
5403display_watch_fd(struct display *display,
5404 int fd, uint32_t events, struct task *task)
5405{
5406 struct epoll_event ep;
5407
5408 ep.events = events;
5409 ep.data.ptr = task;
5410 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5411}
5412
5413void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005414display_unwatch_fd(struct display *display, int fd)
5415{
5416 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5417}
5418
5419void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005420display_run(struct display *display)
5421{
5422 struct task *task;
5423 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005424 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005425
Pekka Paalanen826d7952011-12-15 10:14:07 +02005426 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005427 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005428 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005429 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005430 struct task, link);
5431 wl_list_remove(&task->link);
5432 task->run(task, 0);
5433 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005434
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005435 wl_display_dispatch_pending(display->display);
5436
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005437 if (!display->running)
5438 break;
5439
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005440 ret = wl_display_flush(display->display);
5441 if (ret < 0 && errno == EAGAIN) {
5442 ep[0].events =
5443 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5444 ep[0].data.ptr = &display->display_task;
5445
5446 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5447 display->display_fd, &ep[0]);
5448 } else if (ret < 0) {
5449 break;
5450 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005451
5452 count = epoll_wait(display->epoll_fd,
5453 ep, ARRAY_LENGTH(ep), -1);
5454 for (i = 0; i < count; i++) {
5455 task = ep[i].data.ptr;
5456 task->run(task, ep[i].events);
5457 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005458 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005459}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005460
5461void
5462display_exit(struct display *display)
5463{
5464 display->running = 0;
5465}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005466
5467void
5468keysym_modifiers_add(struct wl_array *modifiers_map,
5469 const char *name)
5470{
5471 size_t len = strlen(name) + 1;
5472 char *p;
5473
5474 p = wl_array_add(modifiers_map, len);
5475
5476 if (p == NULL)
5477 return;
5478
5479 strncpy(p, name, len);
5480}
5481
5482static xkb_mod_index_t
5483keysym_modifiers_get_index(struct wl_array *modifiers_map,
5484 const char *name)
5485{
5486 xkb_mod_index_t index = 0;
5487 char *p = modifiers_map->data;
5488
5489 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5490 if (strcmp(p, name) == 0)
5491 return index;
5492
5493 index++;
5494 p += strlen(p) + 1;
5495 }
5496
5497 return XKB_MOD_INVALID;
5498}
5499
5500xkb_mod_mask_t
5501keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5502 const char *name)
5503{
5504 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5505
5506 if (index == XKB_MOD_INVALID)
5507 return XKB_MOD_INVALID;
5508
5509 return 1 << index;
5510}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005511
5512void *
5513fail_on_null(void *p)
5514{
5515 if (p == NULL) {
Peter Hutterer3ca59d32013-08-08 17:13:47 +10005516 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
Kristian Høgsbergce278412013-07-25 15:20:20 -07005517 exit(EXIT_FAILURE);
5518 }
5519
5520 return p;
5521}
5522
5523void *
5524xmalloc(size_t s)
5525{
5526 return fail_on_null(malloc(s));
5527}
5528
Peter Huttererf3d62272013-08-08 11:57:05 +10005529void *
5530xzalloc(size_t s)
5531{
5532 return fail_on_null(zalloc(s));
5533}
5534
Kristian Høgsbergce278412013-07-25 15:20:20 -07005535char *
5536xstrdup(const char *s)
5537{
5538 return fail_on_null(strdup(s));
5539}